main.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. # -*- coding: utf-8 -*-
  2. #
  3. # AWL simulator - Raspberry Pi GPIO hardware interface
  4. #
  5. # Copyright 2016-2019 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. #cimport cython #@cy
  34. class HwParamDesc_IOMap(HwParamDesc):
  35. typeStr = "BCM-port-number"
  36. _valueRe = re.compile(r'^\s*(?:BCM)?(\d+)\s*$')
  37. def __init__(self, mem):
  38. HwParamDesc.__init__(self,
  39. name = "%s0.0" % mem,
  40. description = "Example: %s1.4=BCM26" % mem)
  41. def parse(self, value):
  42. try:
  43. if not value:
  44. raise ValueError
  45. m = self._valueRe.match(value.upper())
  46. if not m:
  47. raise ValueError
  48. bcm = int(m.group(1), 10)
  49. if bcm < 0:
  50. raise ValueError
  51. return bcm
  52. except ValueError:
  53. raise self.ParseError("Invalid BCM port number: %s" % value)
  54. def match(self, matchName):
  55. if not matchName:
  56. return False
  57. return bool(self._nameRe.match(matchName))
  58. class HwParamDesc_IMap(HwParamDesc_IOMap):
  59. _nameRe = re.compile(r'^\s*[EI]([0-9]+)\.([0-7])\s*$')
  60. def __init__(self):
  61. HwParamDesc_IOMap.__init__(self, mem = "I")
  62. class HwParamDesc_QMap(HwParamDesc_IOMap):
  63. _nameRe = re.compile(r'^\s*[AQ]([0-9])+\.([0-7])\s*$')
  64. def __init__(self):
  65. HwParamDesc_IOMap.__init__(self, mem = "Q")
  66. class RpiGPIO_BitMapping(object): #+cdef
  67. """Awlsim -> RaspiGPIO memory bit mapping.
  68. """
  69. __slots__ = (
  70. "__bit2bcm",
  71. "bitOffsets",
  72. "bcmNumbers",
  73. "currentOutputValues",
  74. "size",
  75. )
  76. def __init__(self):
  77. # Bit number to BCM GPIO number map.
  78. self.__bit2bcm = {}
  79. self.bitOffsets = [None] * 8 #@nocy
  80. self.bcmNumbers = [None] * 8
  81. self.currentOutputValues = [None] * 8 #@nocy
  82. def setBit(self, bitOffset, bcmNumber):
  83. assert(bitOffset >= 0 and bitOffset <= 7)
  84. self.__bit2bcm[bitOffset] = bcmNumber
  85. def build(self):
  86. self.bitOffsets = [None] * 8 #@nocy
  87. self.bcmNumbers = [None] * 8
  88. self.currentOutputValues = [None] * 8 #@nocy
  89. self.size = 0
  90. for bitOffset, bcmNumber in sorted(dictItems(self.__bit2bcm),
  91. key=lambda x: x[0]):
  92. self.bitOffsets[self.size] = bitOffset
  93. self.bcmNumbers[self.size] = bcmNumber
  94. self.currentOutputValues[self.size] = 0xFF # Neither 0 nor 1
  95. self.size += 1
  96. def __repr__(self): #@nocov
  97. return "{ " +\
  98. ", ".join("%d: %s" % (bitOffset, str(bcmNumber))
  99. for bitOffset, bcmNumber in sorted(dictItems(self.__bit2bcm),
  100. key=lambda x: x[0])) +\
  101. " }"
  102. class RpiGPIO_HwInterface(AbstractHardwareInterface): #+cdef
  103. """Raspberry Pi GPIO hardware interface.
  104. """
  105. name = "RPi.GPIO"
  106. description = "Raspberry Pi GPIO support.\n"\
  107. "https://www.raspberrypi.org/"
  108. paramDescs = [
  109. HwParamDesc_IMap(),
  110. HwParamDesc_QMap(),
  111. ]
  112. def __init__(self, sim, parameters={}):
  113. AbstractHardwareInterface.__init__(self,
  114. sim = sim,
  115. parameters = parameters)
  116. def doStartup(self):
  117. """Startup the hardware module.
  118. """
  119. # Get the configuration
  120. inputs = self.getParamsByDescType(HwParamDesc_IMap)
  121. outputs = self.getParamsByDescType(HwParamDesc_QMap)
  122. # Import the Raspberry Pi GPIO module
  123. try:
  124. import RPi.GPIO as RPi_GPIO
  125. self.__RPi_GPIO = RPi_GPIO
  126. except ImportError as e: #@nocov
  127. self.raiseException("Failed to import Raspberry Pi GPIO "
  128. "module 'RPi.GPIO': %s" % str(e))
  129. # Copy shortcuts to Raspberry Pi GPIO module
  130. self.__RPi_GPIO_input = self.__RPi_GPIO.input
  131. self.__RPi_GPIO_output = self.__RPi_GPIO.output
  132. # Initialize the GPIO library
  133. try:
  134. RPi_GPIO.setmode(self.__RPi_GPIO.BCM)
  135. RPi_GPIO.setwarnings(False)
  136. except RuntimeError as e: #@nocov
  137. self.raiseException("Failed to init Raspberry Pi "
  138. "GPIO library: %s" % str(e))
  139. # Build the memory mappings
  140. self.__inputByteOffsetList, self.__inputBitMappingList = self.__mapGPIO(
  141. inputs, HwParamDesc_IMap._nameRe, RPi_GPIO.IN,
  142. self.inputAddressBase)
  143. self.__inputListSize = len(self.__inputByteOffsetList)
  144. self.__outputByteOffsetList, self.__outputBitMappingList = self.__mapGPIO(
  145. outputs, HwParamDesc_QMap._nameRe, RPi_GPIO.OUT,
  146. self.outputAddressBase)
  147. self.__outputListSize = len(self.__outputByteOffsetList)
  148. def __mapGPIO(self, configs, nameRegEx, gpioDir, byteBaseOffset):
  149. mapDict = {}
  150. RPi_GPIO = self.__RPi_GPIO
  151. for address, bcmNumber in configs:
  152. m = nameRegEx.match(address)
  153. byteOffset = int(m.group(1), 10)
  154. bitOffset = int(m.group(2), 10)
  155. mapping = mapDict.setdefault(byteBaseOffset + byteOffset,
  156. RpiGPIO_BitMapping())
  157. mapping.setBit(bitOffset, bcmNumber)
  158. try:
  159. if gpioDir == RPi_GPIO.IN:
  160. RPi_GPIO.setup(bcmNumber,
  161. gpioDir,
  162. pull_up_down = RPi_GPIO.PUD_DOWN)
  163. else:
  164. RPi_GPIO.setup(bcmNumber,
  165. gpioDir,
  166. initial = RPi_GPIO.LOW)
  167. except RuntimeError as e: #@nocov
  168. self.raiseException("Failed to init Raspberry Pi "
  169. "BCM%d: %s" % (bcmNumber, str(e)))
  170. for bitMapping in dictValues(mapDict):
  171. bitMapping.build()
  172. byteOffsetList = []
  173. bitMappingList = []
  174. for byteOffset, bitMapping in sorted(dictItems(mapDict),
  175. key=lambda x: x[0]):
  176. byteOffsetList.append(byteOffset)
  177. bitMappingList.append(bitMapping)
  178. return byteOffsetList, bitMappingList
  179. def doShutdown(self):
  180. pass # Do nothing
  181. #@cy @cython.boundscheck(False)
  182. def readInputs(self): #+cdef
  183. #@cy cdef uint8_t inByte
  184. #@cy cdef RpiGPIO_BitMapping bitMapping
  185. #@cy cdef uint32_t byteOffset
  186. #@cy cdef uint32_t i
  187. #@cy cdef uint32_t j
  188. # Note: Bounds checking of the indexing operator [] is disabled
  189. # by @cython.boundscheck(False) in this method.
  190. for i in range(self.__inputListSize):
  191. byteOffset = self.__inputByteOffsetList[i]
  192. bitMapping = self.__inputBitMappingList[i]
  193. inByte = 0
  194. for j in range(bitMapping.size):
  195. if self.__RPi_GPIO_input(bitMapping.bcmNumbers[j]):
  196. inByte |= 1 << bitMapping.bitOffsets[j] #+suffix-u
  197. self.sim.cpu.storeInputByte(byteOffset, inByte)
  198. #@cy @cython.boundscheck(False)
  199. def writeOutputs(self): #+cdef
  200. #@cy cdef RpiGPIO_BitMapping bitMapping
  201. #@cy cdef uint32_t byteOffset
  202. #@cy cdef uint8_t outByte
  203. #@cy cdef uint8_t newValue
  204. #@cy cdef uint32_t i
  205. #@cy cdef uint32_t j
  206. # Note: Bounds checking of the indexing operator [] is disabled
  207. # by @cython.boundscheck(False) in this method.
  208. for i in range(self.__outputListSize):
  209. byteOffset = self.__outputByteOffsetList[i]
  210. bitMapping = self.__outputBitMappingList[i]
  211. outByte = self.sim.cpu.fetchOutputByte(byteOffset)
  212. for j in range(bitMapping.size):
  213. newValue = (outByte >> bitMapping.bitOffsets[j]) & 1 #+suffix-u
  214. if newValue != bitMapping.currentOutputValues[j]:
  215. self.__RPi_GPIO_output(bitMapping.bcmNumbers[j], newValue)
  216. bitMapping.currentOutputValues[j] = newValue
  217. #@cy @cython.boundscheck(False)
  218. def directReadInput(self, accessWidth, accessOffset): #@nocy
  219. #@cy cdef bytearray directReadInput(self, uint32_t accessWidth, uint32_t accessOffset):
  220. #@cy cdef uint32_t nrBytes
  221. #@cy cdef uint32_t accessEndOffset
  222. #@cy cdef RpiGPIO_BitMapping bitMapping
  223. #@cy cdef uint32_t byteOffset
  224. #@cy cdef uint32_t dataOffset
  225. #@cy cdef uint8_t inByte
  226. #@cy cdef uint32_t i
  227. #@cy cdef uint32_t j
  228. #@cy cdef bytearray retData
  229. # Note: Bounds checking of the indexing operator [] is disabled
  230. # by @cython.boundscheck(False) in this method.
  231. if accessOffset < self.inputAddressBase:
  232. return None
  233. nrBytes = accessWidth // 8 #+suffix-u
  234. accessEndOffset = accessOffset + (nrBytes - 1) #+suffix-u
  235. retData = bytearray(nrBytes)
  236. for i in range(self.__inputListSize):
  237. byteOffset = self.__inputByteOffsetList[i]
  238. if not (accessOffset <= byteOffset <= accessEndOffset):
  239. continue
  240. bitMapping = self.__inputBitMappingList[i]
  241. inByte = 0
  242. for j in range(bitMapping.size):
  243. if self.__RPi_GPIO_input(bitMapping.bcmNumbers[j]):
  244. inByte |= 1 << bitMapping.bitOffsets[j] #+suffix-u
  245. dataOffset = byteOffset - accessOffset
  246. retData[dataOffset] = inByte
  247. return retData
  248. #@cy @cython.boundscheck(False)
  249. def directWriteOutput(self, accessWidth, accessOffset, data): #@nocy
  250. #@cy cdef ExBool_t directWriteOutput(self, uint32_t accessWidth, uint32_t accessOffset, bytearray data) except ExBool_val:
  251. #@cy cdef uint32_t nrBytes
  252. #@cy cdef uint32_t accessEndOffset
  253. #@cy cdef RpiGPIO_BitMapping bitMapping
  254. #@cy cdef uint32_t byteOffset
  255. #@cy cdef uint32_t dataOffset
  256. #@cy cdef uint8_t outByte
  257. #@cy cdef uint8_t newValue
  258. #@cy cdef uint32_t i
  259. #@cy cdef uint32_t j
  260. #@cy cdef _Bool wroteAny
  261. # Note: Bounds checking of the indexing operator [] is disabled
  262. # by @cython.boundscheck(False) in this method.
  263. if accessOffset < self.outputAddressBase:
  264. return False
  265. nrBytes = accessWidth // 8 #+suffix-u
  266. accessEndOffset = accessOffset + (nrBytes - 1) #+suffix-u
  267. wroteAny = False
  268. for i in range(self.__outputListSize):
  269. byteOffset = self.__outputByteOffsetList[i]
  270. if not (accessOffset <= byteOffset <= accessEndOffset):
  271. continue
  272. bitMapping = self.__outputBitMappingList[i]
  273. dataOffset = byteOffset - accessOffset
  274. outByte = data[dataOffset]
  275. for j in range(bitMapping.size):
  276. newValue = (outByte >> bitMapping.bitOffsets[j]) & 1 #+suffix-u
  277. if newValue != bitMapping.currentOutputValues[j]:
  278. self.__RPi_GPIO_output(bitMapping.bcmNumbers[j], newValue)
  279. bitMapping.currentOutputValues[j] = newValue
  280. wroteAny = True
  281. return wroteAny
  282. # Module entry point
  283. HardwareInterface = RpiGPIO_HwInterface