main.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # -*- coding: utf-8 -*-
  2. #
  3. # AWL simulator - PyProfibus hardware interface
  4. #
  5. # Copyright 2013-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_pyprofibus.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. class HardwareInterface_PyProfibus(AbstractHardwareInterface): #+cdef
  33. name = "PyProfibus"
  34. description = "PROFIBUS-DP support with PyProfibus.\n"\
  35. "https://bues.ch/a/profibus"
  36. # Hardware-specific parameters
  37. paramDescs = [
  38. HwParamDesc_str("config",
  39. defaultValue = "awlsimhw_pyprofibus.conf",
  40. description = "Awlsim pyprofibus module config file."),
  41. ]
  42. def __init__(self, sim, parameters={}):
  43. AbstractHardwareInterface.__init__(self,
  44. sim = sim,
  45. parameters = parameters)
  46. def __setupSlaves(self):
  47. setPrmReq = self.pyprofibus.dp.DpTelegram_SetPrm_Req
  48. dp1PrmMask = bytearray((setPrmReq.DPV1PRM0_FAILSAFE,
  49. setPrmReq.DPV1PRM1_REDCFG,
  50. 0x00))
  51. dp1PrmSet = bytearray((setPrmReq.DPV1PRM0_FAILSAFE,
  52. setPrmReq.DPV1PRM1_REDCFG,
  53. 0x00))
  54. for slaveConf in self.__conf.slaveConfs:
  55. slaveDesc = slaveConf.makeDpSlaveDesc()
  56. if slaveConf.gsd.isDPV1():
  57. slaveDesc.setUserPrmData(slaveConf.gsd.getUserPrmData(
  58. dp1PrmMask=dp1PrmMask,
  59. dp1PrmSet=dp1PrmSet))
  60. slaveDesc._awlsimSlaveConf = slaveConf
  61. self.master.addSlave(slaveDesc)
  62. def __cleanup(self):
  63. if self.master:
  64. self.master.destroy()
  65. self.master = None
  66. self.cachedInputs = [None] * (0x7F + 1)
  67. def doStartup(self):
  68. # Import the PROFIBUS hardware access modules
  69. # and keep references to it.
  70. try:
  71. import pyprofibus
  72. self.pyprofibus = pyprofibus
  73. except (ImportError, RuntimeError) as e: #@nocov
  74. self.raiseException("Failed to import PROFIBUS protocol stack "
  75. "module 'pyprofibus':\n%s" % str(e))
  76. # Initialize the DPM
  77. self.master = None
  78. try:
  79. self.__conf = self.pyprofibus.PbConf.fromFile(
  80. self.getParamValueByName("config"))
  81. self.master = self.__conf.makeDPM()
  82. self.__setupSlaves()
  83. self.master.initialize()
  84. self.slaveList = self.master.getSlaveList()
  85. self.cachedInputs = [None] * (0x7F + 1)
  86. except self.pyprofibus.PhyError as e:
  87. self.raiseException("Profibus-PHY error: %s" % str(e))
  88. self.__cleanup()
  89. except self.pyprofibus.DpError as e:
  90. self.raiseException("Profibus-DP error: %s" % str(e))
  91. self.__cleanup()
  92. except self.pyprofibus.FdlError as e:
  93. self.raiseException("Profibus-FDL error: %s" % str(e))
  94. self.__cleanup()
  95. except self.pyprofibus.conf.PbConfError as e:
  96. self.raiseException("Profibus configuration error: %s" % str(e))
  97. self.__cleanup()
  98. def doShutdown(self):
  99. self.__cleanup()
  100. def readInputs(self): #+cdef
  101. address = self.inputAddressBase
  102. for slave in self.slaveList:
  103. # Get the cached slave-data
  104. inData = self.cachedInputs[slave.slaveAddr]
  105. if inData is None:
  106. continue
  107. self.cachedInputs[slave.slaveAddr] = None
  108. inData = bytearray(inData)
  109. inputSize = slave._awlsimSlaveConf.inputSize
  110. if len(inData) > inputSize:
  111. inData = inData[0:inputSize]
  112. if len(inData) < inputSize:
  113. inData += b'\0' * (inputSize - len(inData))
  114. self.sim.cpu.storeInputRange(address, inData)
  115. # Adjust the address base for the next slave.
  116. address += inputSize
  117. def writeOutputs(self): #+cdef
  118. try:
  119. address = self.outputAddressBase
  120. for slave in self.slaveList:
  121. # Get the output data from the CPU
  122. outputSize = slave._awlsimSlaveConf.outputSize
  123. outData = self.sim.cpu.fetchOutputRange(address,
  124. outputSize)
  125. # Write the output data to the pyprofibus subsystem.
  126. slave.setOutData(outData)
  127. # Adjust the address base for the next slave.
  128. address += outputSize
  129. # Run the pyprofibus master state machine.
  130. slave = self.master.run()
  131. if slave:
  132. # Get the input data from the pyprofibus subsystem.
  133. inData = slave.getInData()
  134. # Cache the input data for the readInputs() call.
  135. self.cachedInputs[slave.slaveAddr] = inData
  136. except self.pyprofibus.ProfibusError as e:
  137. self.raiseException("Hardware error: %s" % str(e))
  138. def directReadInput(self, accessWidth, accessOffset): #@nocy
  139. #@cy cdef bytearray directReadInput(self, uint32_t accessWidth, uint32_t accessOffset):
  140. return bytearray()#TODO
  141. def directWriteOutput(self, accessWidth, accessOffset, data): #@nocy
  142. #@cy cdef ExBool_t directWriteOutput(self, uint32_t accessWidth, uint32_t accessOffset, bytearray data) except ExBool_val:
  143. return False#TODO
  144. # Module entry point
  145. HardwareInterface = HardwareInterface_PyProfibus