main.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. # -*- coding: utf-8 -*-
  2. #
  3. # AWL simulator - Raspberry Pi GPIO hardware interface
  4. #
  5. # Copyright 2016-2017 Michael Buesch <m@bues.ch>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. from __future__ import division, absolute_import, print_function, unicode_literals
  22. #from awlsim.common.cython_support cimport * #@cy
  23. from awlsim.common.compat import *
  24. from awlsim.common.util import *
  25. from awlsim.common.exceptions import *
  26. #from awlsimhw_rpigpio.main cimport * #@cy
  27. from awlsim.core.hardware_params import *
  28. from awlsim.core.hardware import * #+cimport
  29. from awlsim.core.operators import * #+cimport
  30. from awlsim.core.offset import * #+cimport
  31. from awlsim.core.cpu import * #+cimport
  32. import re
  33. class HwParamDesc_IOMap(HwParamDesc):
  34. typeStr = "BCM-port-number"
  35. _valueRe = re.compile(r'^\s*(?:BCM)?(\d+)\s*$')
  36. def __init__(self, mem):
  37. HwParamDesc.__init__(self,
  38. name = "%s0.0" % mem,
  39. description = "Example: %s1.4=BCM26" % mem)
  40. def parse(self, value):
  41. try:
  42. if not value:
  43. raise ValueError
  44. m = self._valueRe.match(value.upper())
  45. if not m:
  46. raise ValueError
  47. bcm = int(m.group(1), 10)
  48. if bcm < 0:
  49. raise ValueError
  50. return bcm
  51. except ValueError:
  52. raise self.ParseError("Invalid BCM port number: %s" % value)
  53. def match(self, matchName):
  54. if not matchName:
  55. return False
  56. return bool(self._nameRe.match(matchName))
  57. class HwParamDesc_IMap(HwParamDesc_IOMap):
  58. _nameRe = re.compile(r'^\s*[EI]([0-9]+)\.([0-7])\s*$')
  59. def __init__(self):
  60. HwParamDesc_IOMap.__init__(self, mem = "I")
  61. class HwParamDesc_QMap(HwParamDesc_IOMap):
  62. _nameRe = re.compile(r'^\s*[AQ]([0-9])+\.([0-7])\s*$')
  63. def __init__(self):
  64. HwParamDesc_IOMap.__init__(self, mem = "Q")
  65. class RpiGPIO_BitMapping(object):
  66. """Awlsim -> RaspiGPIO memory bit mapping.
  67. """
  68. def __init__(self):
  69. # Bit number to BCM GPIO number map.
  70. self.bit2bcm = [ None, ] * 8
  71. self.mapList = []
  72. def setBit(self, bitOffset, bcmNumber):
  73. assert(bitOffset >= 0 and bitOffset <= 7)
  74. self.bit2bcm[bitOffset] = bcmNumber
  75. def build(self):
  76. self.mapList = [ (bitOffset, self.bit2bcm[bitOffset])
  77. for bitOffset in range(8)
  78. if self.bit2bcm[bitOffset] is not None ]
  79. def __repr__(self):
  80. return "{ " +\
  81. ", ".join("%d: %s" % (i, str(self.bit2bcm[i]))
  82. for i in range(8)) +\
  83. " }"
  84. class RpiGPIO_HwInterface(AbstractHardwareInterface): #+cdef
  85. """Raspberry Pi GPIO hardware interface.
  86. """
  87. name = "RPi.GPIO"
  88. description = "Raspberry Pi GPIO support.\n"\
  89. "https://www.raspberrypi.org/"
  90. paramDescs = [
  91. HwParamDesc_IMap(),
  92. HwParamDesc_QMap(),
  93. ]
  94. def __init__(self, sim, parameters={}):
  95. AbstractHardwareInterface.__init__(self,
  96. sim = sim,
  97. parameters = parameters)
  98. self.__tmpStoreBytes = bytearray(1)
  99. def doStartup(self):
  100. """Startup the hardware module.
  101. """
  102. # Get the configuration
  103. inputs = self.getParamsByDescType(HwParamDesc_IMap)
  104. outputs = self.getParamsByDescType(HwParamDesc_QMap)
  105. # Import the Raspberry Pi GPIO module
  106. try:
  107. import RPi.GPIO as RPi_GPIO
  108. self.__RPi_GPIO = RPi_GPIO
  109. except ImportError as e:
  110. self.raiseException("Failed to import Raspberry Pi GPIO "
  111. "module 'RPi.GPIO': %s" % str(e))
  112. # Initialize the GPIO library
  113. try:
  114. RPi_GPIO.setmode(self.__RPi_GPIO.BCM)
  115. RPi_GPIO.setwarnings(False)
  116. except RuntimeError as e:
  117. self.raiseException("Failed to init Raspberry Pi "
  118. "GPIO library: %s" % str(e))
  119. # Build the memory mappings
  120. self.__inputMap, self.__inputList = self.__mapGPIO(
  121. inputs, HwParamDesc_IMap._nameRe, RPi_GPIO.IN,
  122. self.inputAddressBase)
  123. self.__outputMap, self.__outputList = self.__mapGPIO(
  124. outputs, HwParamDesc_QMap._nameRe, RPi_GPIO.OUT,
  125. self.outputAddressBase)
  126. def __mapGPIO(self, configs, nameRegEx, gpioDir, byteBaseOffset):
  127. mapDict = {}
  128. RPi_GPIO = self.__RPi_GPIO
  129. for address, bcmNumber in configs:
  130. m = nameRegEx.match(address)
  131. byteOffset = int(m.group(1), 10)
  132. bitOffset = int(m.group(2), 10)
  133. mapping = mapDict.setdefault(byteBaseOffset + byteOffset,
  134. RpiGPIO_BitMapping())
  135. mapping.setBit(bitOffset, bcmNumber)
  136. try:
  137. if gpioDir == RPi_GPIO.IN:
  138. RPi_GPIO.setup(bcmNumber,
  139. gpioDir,
  140. pull_up_down = RPi_GPIO.PUD_DOWN)
  141. else:
  142. RPi_GPIO.setup(bcmNumber,
  143. gpioDir,
  144. initial = RPi_GPIO.LOW)
  145. except RuntimeError as e:
  146. self.raiseException("Failed to init Raspberry Pi "
  147. "BCM%d: %s" % (bcmNumber, str(e)))
  148. for bitMapping in dictValues(mapDict):
  149. bitMapping.build()
  150. mapList = list(sorted(
  151. [ (byteOffset, bitMapping)
  152. for byteOffset, bitMapping in dictItems(mapDict) ],
  153. key = lambda _tuple: _tuple[0]
  154. ))
  155. return mapDict, mapList
  156. def doShutdown(self):
  157. pass # Do nothing
  158. def readInputs(self): #+cdef
  159. #@cy cdef S7CPU cpu
  160. #@cy cdef uint8_t inByte
  161. #@cy cdef uint32_t byteOffset
  162. #@cy cdef uint32_t bitOffset
  163. #@cy cdef bytearray tmpBytes
  164. RPi_GPIO = self.__RPi_GPIO
  165. tmpBytes = self.__tmpStoreBytes
  166. cpu = self.sim.cpu
  167. for byteOffset, bitMapping in self.__inputList:
  168. inByte = 0
  169. for bitOffset, bcmNumber in bitMapping.mapList:
  170. if RPi_GPIO.input(bcmNumber):
  171. inByte |= 1 << bitOffset
  172. tmpBytes[0] = inByte
  173. self.sim.cpu.storeInputRange(byteOffset, tmpBytes)
  174. def writeOutputs(self): #+cdef
  175. #@cy cdef S7CPU cpu
  176. #@cy cdef uint32_t byteOffset
  177. #@cy cdef uint32_t bitOffset
  178. RPi_GPIO = self.__RPi_GPIO
  179. cpu = self.sim.cpu
  180. for byteOffset, bitMapping in self.__outputList:
  181. outByte = cpu.fetchOutputRange(byteOffset, 1)[0]
  182. for bitOffset, bcmNumber in bitMapping.mapList:
  183. RPi_GPIO.output(bcmNumber,
  184. outByte & (1 << bitOffset))
  185. def directReadInput(self, accessWidth, accessOffset): #@nocy
  186. #@cy cdef bytearray directReadInput(self, uint32_t accessWidth, uint32_t accessOffset):
  187. #@cy cdef uint32_t nrBytes
  188. if accessOffset < self.inputAddressBase:
  189. return None
  190. RPi_GPIO = self.__RPi_GPIO
  191. nrBytes = accessWidth // 8
  192. pass#TODO
  193. return bytearray()
  194. def directWriteOutput(self, accessWidth, accessOffset, data): #@nocy
  195. #@cy cdef ExBool_t directWriteOutput(self, uint32_t accessWidth, uint32_t accessOffset, bytearray data) except ExBool_val:
  196. #@cy cdef uint32_t nrBytes
  197. if accessOffset < self.outputAddressBase:
  198. return False
  199. RPi_GPIO = self.__RPi_GPIO
  200. nrBytes = accessWidth // 8
  201. pass#TODO
  202. return True
  203. # Module entry point
  204. HardwareInterface = RpiGPIO_HwInterface