main.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. # -*- coding: utf-8 -*-
  2. #
  3. # AWL simulator - LinuxCNC HAL interface
  4. #
  5. # Copyright 2013-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_linuxcnc.main cimport * #@cy
  27. from awlsim.common.datatypehelpers import * #+cimport
  28. from awlsim.core.hardware_params import *
  29. from awlsim.core.hardware import * #+cimport
  30. from awlsim.core.operators import * #+cimport
  31. from awlsim.core.offset import * #+cimport
  32. from awlsim.core.cpu import * #+cimport
  33. class SigBit(object):
  34. def __init__(self, hal, halName, byteOffset, bitOffset):
  35. self.hal = hal
  36. self.halName = halName
  37. self.byteOffset = byteOffset
  38. self.bitOffset = bitOffset
  39. self.setMask = 1 << bitOffset
  40. self.clrMask = ~(1 << bitOffset)
  41. def readInput(self, destBuf):
  42. if self.hal[self.halName]:
  43. destBuf[self.byteOffset] |= self.setMask
  44. else:
  45. destBuf[self.byteOffset] &= self.clrMask
  46. def writeOutput(self, srcBuf):
  47. self.hal[self.halName] = (srcBuf[self.byteOffset] >> self.bitOffset) & 1
  48. class SigU8(object):
  49. def __init__(self, hal, halName, offset):
  50. self.hal = hal
  51. self.halName = halName
  52. self.offset = offset
  53. def readInput(self, destBuf):
  54. destBuf[self.offset] = self.hal[self.halName] & 0xFF
  55. def writeOutput(self, srcBuf):
  56. self.hal[self.halName] = srcBuf[self.offset] & 0xFF
  57. class SigU16(object):
  58. def __init__(self, hal, halName, offset):
  59. self.hal = hal
  60. self.halName = halName
  61. self.offset = offset
  62. def readInput(self, destBuf):
  63. word = self.hal[self.halName] & 0xFFFF
  64. destBuf[self.offset] = (word >> 8) & 0xFF
  65. destBuf[self.offset + 1] = word & 0xFF
  66. def writeOutput(self, srcBuf):
  67. word = (srcBuf[self.offset] << 8) |\
  68. srcBuf[self.offset + 1]
  69. self.hal[self.halName] = word & 0xFFFF
  70. class SigS16(object):
  71. def __init__(self, hal, halName, offset):
  72. self.hal = hal
  73. self.halName = halName
  74. self.offset = offset
  75. def readInput(self, destBuf):
  76. word = self.hal[self.halName] & 0xFFFF
  77. destBuf[self.offset] = (word >> 8) & 0xFF
  78. destBuf[self.offset + 1] = word & 0xFF
  79. def writeOutput(self, srcBuf):
  80. word = (srcBuf[self.offset] << 8) |\
  81. srcBuf[self.offset + 1]
  82. self.hal[self.halName] = wordToSignedPyInt(word)
  83. class SigU31(object):
  84. def __init__(self, hal, halName, offset):
  85. self.hal = hal
  86. self.halName = halName
  87. self.offset = offset
  88. def readInput(self, destBuf):
  89. dword = self.hal[self.halName] & 0x7FFFFFFF
  90. destBuf[self.offset] = (dword >> 24) & 0xFF
  91. destBuf[self.offset + 1] = (dword >> 16) & 0xFF
  92. destBuf[self.offset + 2] = (dword >> 8) & 0xFF
  93. destBuf[self.offset + 3] = dword & 0xFF
  94. def writeOutput(self, srcBuf):
  95. dword = (srcBuf[self.offset] << 24) |\
  96. (srcBuf[self.offset + 1] << 16) |\
  97. (srcBuf[self.offset + 2] << 8) |\
  98. srcBuf[self.offset + 3]
  99. self.hal[self.halName] = dword & 0x7FFFFFFF
  100. class SigS32(object):
  101. def __init__(self, hal, halName, offset):
  102. self.hal = hal
  103. self.halName = halName
  104. self.offset = offset
  105. def readInput(self, destBuf):
  106. dword = self.hal[self.halName] & 0xFFFFFFFF
  107. destBuf[self.offset] = (dword >> 24) & 0xFF
  108. destBuf[self.offset + 1] = (dword >> 16) & 0xFF
  109. destBuf[self.offset + 2] = (dword >> 8) & 0xFF
  110. destBuf[self.offset + 3] = dword & 0xFF
  111. def writeOutput(self, srcBuf):
  112. dword = (srcBuf[self.offset] << 24) |\
  113. (srcBuf[self.offset + 1] << 16) |\
  114. (srcBuf[self.offset + 2] << 8) |\
  115. srcBuf[self.offset + 3]
  116. self.hal[self.halName] = dwordToSignedPyInt(dword)
  117. class SigFloat(object):
  118. def __init__(self, hal, halName, offset):
  119. self.hal = hal
  120. self.halName = halName
  121. self.offset = offset
  122. def readInput(self, destBuf):
  123. dword = pyFloatToDWord(self.hal[self.halName])
  124. destBuf[self.offset] = (dword >> 24) & 0xFF
  125. destBuf[self.offset + 1] = (dword >> 16) & 0xFF
  126. destBuf[self.offset + 2] = (dword >> 8) & 0xFF
  127. destBuf[self.offset + 3] = dword & 0xFF
  128. def writeOutput(self, srcBuf):
  129. dword = (srcBuf[self.offset] << 24) |\
  130. (srcBuf[self.offset + 1] << 16) |\
  131. (srcBuf[self.offset + 2] << 8) |\
  132. srcBuf[self.offset + 3]
  133. self.hal[self.halName] = dwordToPyFloat(dword)
  134. class HardwareInterface_LinuxCNC(AbstractHardwareInterface): #+cdef
  135. name = "LinuxCNC"
  136. description = "LinuxCNC and MachineKit hardware support.\n"\
  137. "http://linuxcnc.org/\n"\
  138. "http://www.machinekit.io/"
  139. paramDescs = [
  140. HwParamDesc_pyobject("hal",
  141. description = "LinuxCNC HAL instance object",
  142. mandatory = True),
  143. HwParamDesc_int("inputSize",
  144. description = "Input area size",
  145. defaultValue = 32,
  146. mandatory = True),
  147. HwParamDesc_int("outputSize",
  148. description = "Output area size",
  149. defaultValue = 32,
  150. mandatory = True),
  151. ]
  152. def __init__(self, sim, parameters={}):
  153. AbstractHardwareInterface.__init__(self,
  154. sim = sim,
  155. parameters = parameters)
  156. self.linuxCNC_initialized = False
  157. def doStartup(self):
  158. if not self.linuxCNC_initialized:
  159. try:
  160. import hal as LinuxCNC_HAL
  161. self.LinuxCNC_HAL = LinuxCNC_HAL
  162. except ImportError as e:
  163. self.raiseException("Failed to import LinuxCNC HAL module"
  164. ":\n%s" % str(e))
  165. # Get the LinuxCNC-HAL-component object
  166. self.hal = self.getParamValueByName("hal")
  167. # Get parameters
  168. self.inputSize = self.getParamValueByName("inputSize")
  169. self.outputSize = self.getParamValueByName("outputSize")
  170. self.__configDone = False
  171. # Signal LinuxCNC that we are ready.
  172. self.hal.ready()
  173. self.linuxCNC_initialized = True
  174. def doShutdown(self):
  175. pass
  176. #TODO find overlappings
  177. def __buildTable(self, baseName, addressBase, size):
  178. tab = []
  179. for address in range(addressBase, addressBase + size):
  180. offset = address - addressBase
  181. for bitNr in range(8):
  182. if self.hal["%s.bit.%d.%d.active" % (baseName, address, bitNr)]:
  183. tab.append(SigBit(self.hal,
  184. "%s.bit.%d.%d" % (baseName, address, bitNr),
  185. offset, bitNr))
  186. if self.hal["%s.u8.%d.active" % (baseName, address)]:
  187. tab.append(SigU8(self.hal,
  188. "%s.u8.%d" % (baseName, address),
  189. offset))
  190. if address % 2:
  191. continue
  192. if size - offset < 2:
  193. continue
  194. if self.hal["%s.u16.%d.active" % (baseName, address)]:
  195. tab.append(SigU16(self.hal,
  196. "%s.u16.%d" % (baseName, address),
  197. offset))
  198. if self.hal["%s.s16.%d.active" % (baseName, address)]:
  199. tab.append(SigS16(self.hal,
  200. "%s.s16.%d" % (baseName, address),
  201. offset))
  202. if size - offset < 4:
  203. continue
  204. if self.hal["%s.u31.%d.active" % (baseName, address)]:
  205. tab.append(SigU31(self.hal,
  206. "%s.u31.%d" % (baseName, address),
  207. offset))
  208. if self.hal["%s.s32.%d.active" % (baseName, address)]:
  209. tab.append(SigS32(self.hal,
  210. "%s.s32.%d" % (baseName, address),
  211. offset))
  212. if self.hal["%s.float.%d.active" % (baseName, address)]:
  213. tab.append(SigFloat(self.hal,
  214. "%s.float.%d" % (baseName, address),
  215. offset))
  216. return tab
  217. def __tryBuildConfig(self):
  218. if not self.hal["config.ready"]:
  219. return
  220. self.__activeInputs = self.__buildTable("input",
  221. self.inputAddressBase, self.inputSize)
  222. #TODO dump the input table
  223. self.__activeOutputs = self.__buildTable("output",
  224. self.outputAddressBase, self.outputSize)
  225. #TODO dump the input table
  226. self.__configDone = True
  227. printInfo("HAL configuration done")
  228. def readInputs(self): #+cdef
  229. if not self.__configDone:
  230. self.__tryBuildConfig()
  231. if not self.__configDone:
  232. return
  233. data = bytearray(self.inputSize)
  234. for desc in self.__activeInputs:
  235. desc.readInput(data)
  236. self.sim.cpu.storeInputRange(self.inputAddressBase, data)
  237. def writeOutputs(self): #+cdef
  238. if not self.__configDone:
  239. return
  240. data = self.sim.cpu.fetchOutputRange(self.outputAddressBase,
  241. self.outputSize)
  242. for desc in self.__activeOutputs:
  243. desc.writeOutput(data)
  244. def directReadInput(self, accessWidth, accessOffset): #@nocy
  245. #@cy cdef bytearray directReadInput(self, uint32_t accessWidth, uint32_t accessOffset):
  246. pass#TODO
  247. return bytearray()
  248. def directWriteOutput(self, accessWidth, accessOffset, data): #@nocy
  249. #@cy cdef ExBool_t directWriteOutput(self, uint32_t accessWidth, uint32_t accessOffset, bytearray data) except ExBool_val:
  250. pass#TODO
  251. return False
  252. # Module entry point
  253. HardwareInterface = HardwareInterface_LinuxCNC