123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- from __future__ import division, absolute_import, print_function, unicode_literals
- from awlsim.common.compat import *
- from awlsim.common.util import *
- from awlsim.common.exceptions import *
- from awlsim.core.hardware_params import *
- from awlsim.core.hardware import *
- from awlsim.core.operators import *
- from awlsim.core.offset import *
- from awlsim.core.cpu import *
- import re
- class HwParamDesc_IOMap(HwParamDesc):
- typeStr = "BCM-port-number"
- _valueRe = re.compile(r'^\s*(?:BCM)?(\d+)\s*$')
- def __init__(self, mem):
- HwParamDesc.__init__(self,
- name = "%s0.0" % mem,
- description = "Example: %s1.4=BCM26" % mem)
- def parse(self, value):
- try:
- if not value:
- raise ValueError
- m = self._valueRe.match(value.upper())
- if not m:
- raise ValueError
- bcm = int(m.group(1), 10)
- if bcm < 0:
- raise ValueError
- return bcm
- except ValueError:
- raise self.ParseError("Invalid BCM port number: %s" % value)
- def match(self, matchName):
- if not matchName:
- return False
- return bool(self._nameRe.match(matchName))
- class HwParamDesc_IMap(HwParamDesc_IOMap):
- _nameRe = re.compile(r'^\s*[EI]([0-9]+)\.([0-7])\s*$')
- def __init__(self):
- HwParamDesc_IOMap.__init__(self, mem = "I")
- class HwParamDesc_QMap(HwParamDesc_IOMap):
- _nameRe = re.compile(r'^\s*[AQ]([0-9])+\.([0-7])\s*$')
- def __init__(self):
- HwParamDesc_IOMap.__init__(self, mem = "Q")
- class RpiGPIO_BitMapping(object):
- """Awlsim -> RaspiGPIO memory bit mapping.
- """
- def __init__(self):
-
- self.bit2bcm = [ None, ] * 8
- self.mapList = []
- def setBit(self, bitOffset, bcmNumber):
- assert(bitOffset >= 0 and bitOffset <= 7)
- self.bit2bcm[bitOffset] = bcmNumber
- def build(self):
- self.mapList = [ (bitOffset, self.bit2bcm[bitOffset])
- for bitOffset in range(8)
- if self.bit2bcm[bitOffset] is not None ]
- def __repr__(self):
- return "{ " +\
- ", ".join("%d: %s" % (i, str(self.bit2bcm[i]))
- for i in range(8)) +\
- " }"
- class RpiGPIO_HwInterface(AbstractHardwareInterface):
- """Raspberry Pi GPIO hardware interface.
- """
- name = "RPi.GPIO"
- description = "Raspberry Pi GPIO support.\n"\
- "https://www.raspberrypi.org/"
- paramDescs = [
- HwParamDesc_IMap(),
- HwParamDesc_QMap(),
- ]
- def __init__(self, sim, parameters={}):
- AbstractHardwareInterface.__init__(self,
- sim = sim,
- parameters = parameters)
- self.__tmpStoreBytes = bytearray(1)
- def doStartup(self):
- """Startup the hardware module.
- """
-
- inputs = self.getParamsByDescType(HwParamDesc_IMap)
- outputs = self.getParamsByDescType(HwParamDesc_QMap)
-
- try:
- import RPi.GPIO as RPi_GPIO
- self.__RPi_GPIO = RPi_GPIO
- except ImportError as e:
- self.raiseException("Failed to import Raspberry Pi GPIO "
- "module 'RPi.GPIO': %s" % str(e))
-
- try:
- RPi_GPIO.setmode(self.__RPi_GPIO.BCM)
- RPi_GPIO.setwarnings(False)
- except RuntimeError as e:
- self.raiseException("Failed to init Raspberry Pi "
- "GPIO library: %s" % str(e))
-
- self.__inputMap, self.__inputList = self.__mapGPIO(
- inputs, HwParamDesc_IMap._nameRe, RPi_GPIO.IN,
- self.inputAddressBase)
- self.__outputMap, self.__outputList = self.__mapGPIO(
- outputs, HwParamDesc_QMap._nameRe, RPi_GPIO.OUT,
- self.outputAddressBase)
- def __mapGPIO(self, configs, nameRegEx, gpioDir, byteBaseOffset):
- mapDict = {}
- RPi_GPIO = self.__RPi_GPIO
- for address, bcmNumber in configs:
- m = nameRegEx.match(address)
- byteOffset = int(m.group(1), 10)
- bitOffset = int(m.group(2), 10)
- mapping = mapDict.setdefault(byteBaseOffset + byteOffset,
- RpiGPIO_BitMapping())
- mapping.setBit(bitOffset, bcmNumber)
- try:
- if gpioDir == RPi_GPIO.IN:
- RPi_GPIO.setup(bcmNumber,
- gpioDir,
- pull_up_down = RPi_GPIO.PUD_DOWN)
- else:
- RPi_GPIO.setup(bcmNumber,
- gpioDir,
- initial = RPi_GPIO.LOW)
- except RuntimeError as e:
- self.raiseException("Failed to init Raspberry Pi "
- "BCM%d: %s" % (bcmNumber, str(e)))
- for bitMapping in dictValues(mapDict):
- bitMapping.build()
- mapList = list(sorted(
- [ (byteOffset, bitMapping)
- for byteOffset, bitMapping in dictItems(mapDict) ],
- key = lambda _tuple: _tuple[0]
- ))
- return mapDict, mapList
- def doShutdown(self):
- pass
- def readInputs(self):
- RPi_GPIO = self.__RPi_GPIO
- tmpBytes = self.__tmpStoreBytes
- cpu = self.sim.cpu
- for byteOffset, bitMapping in self.__inputList:
- inByte = 0
- for bitOffset, bcmNumber in bitMapping.mapList:
- if RPi_GPIO.input(bcmNumber):
- inByte |= 1 << bitOffset
- tmpBytes[0] = inByte
- self.sim.cpu.storeInputRange(byteOffset, tmpBytes)
- def writeOutputs(self):
- RPi_GPIO = self.__RPi_GPIO
- cpu = self.sim.cpu
- for byteOffset, bitMapping in self.__outputList:
- outByte = cpu.fetchOutputRange(byteOffset, 1)[0]
- for bitOffset, bcmNumber in bitMapping.mapList:
- RPi_GPIO.output(bcmNumber,
- outByte & (1 << bitOffset))
- def directReadInput(self, accessWidth, accessOffset):
- if accessOffset < self.inputAddressBase:
- return None
- RPi_GPIO = self.__RPi_GPIO
- nrBytes = accessWidth // 8
- pass
- return bytearray()
- def directWriteOutput(self, accessWidth, accessOffset, data):
- if accessOffset < self.outputAddressBase:
- return False
- RPi_GPIO = self.__RPi_GPIO
- nrBytes = accessWidth // 8
- pass
- return True
- HardwareInterface = RpiGPIO_HwInterface
|