main.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # -*- coding: utf-8 -*-
  2. #
  3. # AWL simulator - PyProfibus hardware interface
  4. #
  5. # Copyright 2013-2014 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.compat import *
  23. from awlsim.core.hardware import *
  24. from awlsim.core.util import *
  25. class HardwareInterface(AbstractHardwareInterface):
  26. name = "PyProfibus"
  27. # Hardware-specific parameters
  28. paramDescs = [
  29. HwParamDesc_int("debug",
  30. minValue = 0,
  31. description = "Debug level."),
  32. HwParamDesc_int("baud",
  33. defaultValue = 19200,
  34. minValue = 9600, maxValue = 12000000,
  35. description = "The PROFIBUS baud rate."),
  36. HwParamDesc_int("masterClass",
  37. defaultValue = 1,
  38. minValue = 1, maxValue = 2,
  39. description = "The DP-Master class."),
  40. HwParamDesc_int("masterAddr",
  41. defaultValue = 1,
  42. minValue = 0, maxValue = 126,
  43. description = "The DP-Address of the master."),
  44. HwParamDesc_int("spiDev",
  45. minValue = 0,
  46. description = "The SPI device number."),
  47. HwParamDesc_int("spiChip",
  48. minValue = 0,
  49. description = "The SPI device chip-select number."),
  50. ]
  51. def __init__(self, sim, parameters={}):
  52. AbstractHardwareInterface.__init__(self,
  53. sim = sim,
  54. parameters = parameters)
  55. def __setupSlaves(self):
  56. #TODO: Rewrite. Must be configurable.
  57. et200s = self.pyprofibus.DpSlaveDesc(identNumber = 0x806A,
  58. slaveAddr = 8,
  59. inputAddressRangeSize = 1,
  60. outputAddressRangeSize = 2)
  61. for elem in (self.pyprofibus.DpCfgDataElement(0),
  62. self.pyprofibus.DpCfgDataElement(0x20),
  63. self.pyprofibus.DpCfgDataElement(0x20),
  64. self.pyprofibus.DpCfgDataElement(0x10)):
  65. et200s.chkCfgTelegram.addCfgDataElement(elem)
  66. et200s.setPrmTelegram.addUserPrmData([0x11 | 0x40])
  67. et200s.setSyncMode(True)
  68. et200s.setFreezeMode(True)
  69. et200s.setGroupMask(1)
  70. et200s.setWatchdog(5000)
  71. self.master.addSlave(et200s)
  72. def __cleanup(self):
  73. if self.master:
  74. self.master.destroy()
  75. self.master = None
  76. self.phy = None
  77. self.cachedInputs = []
  78. def doStartup(self):
  79. # Import the PROFIBUS hardware access modules
  80. # and keep references to it.
  81. try:
  82. import pyprofibus
  83. self.pyprofibus = pyprofibus
  84. except (ImportError, RuntimeError) as e:
  85. self.raiseException("Failed to import PROFIBUS protocol stack "
  86. "module 'pyprofibus':\n%s" % str(e))
  87. # Initialize the DPM
  88. self.phy = None
  89. self.master = None
  90. try:
  91. self.phy = self.pyprofibus.CpPhy(device = self.getParamValueByName("spiDev"),
  92. chipselect = self.getParamValueByName("spiChip"),
  93. debug = True if (self.getParamValueByName("debug") >= 2) else False)
  94. self.phy.profibusSetPhyConfig(baudrate = self.getParamValueByName("baud"))
  95. if self.getParamValueByName("masterClass") == 1:
  96. DPM_cls = self.pyprofibus.DPM1
  97. else:
  98. DPM_cls = self.pyprofibus.DPM2
  99. self.master = DPM_cls(phy = self.phy,
  100. masterAddr = self.getParamValueByName("masterAddr"),
  101. debug = True if (self.getParamValueByName("debug") >= 1) else False)
  102. self.__setupSlaves()
  103. self.master.initialize()
  104. self.slaveList = self.master.getSlaveList()
  105. self.cachedInputs = [None] * len(self.slaveList)
  106. except self.pyprofibus.PhyError as e:
  107. self.raiseException("Profibus-PHY error: %s" % str(e))
  108. self.__cleanup()
  109. except self.pyprofibus.DpError as e:
  110. self.raiseException("Profibus-DP error: %s" % str(e))
  111. self.__cleanup()
  112. except self.pyprofibus.FdlError as e:
  113. self.raiseException("Profibus-FDL error: %s" % str(e))
  114. self.__cleanup()
  115. def doShutdown(self):
  116. self.__cleanup()
  117. def readInputs(self):
  118. address = self.inputAddressBase
  119. for slave in self.slaveList:
  120. # Get the cached slave-data
  121. inData = self.cachedInputs.pop(0)
  122. if not inData:
  123. continue
  124. if len(inData) != slave.inputAddressRangeSize:
  125. self.raiseException("Input data from slave '%s' has "
  126. "invalid length %d (expected %d)" %\
  127. (str(slave), len(inData),
  128. slave.inputAddressRangeSize))
  129. self.sim.cpu.storeInputRange(address, inData)
  130. # Adjust the address base for the next slave.
  131. address += slave.inputAddressRangeSize
  132. assert(not self.cachedInputs)
  133. def writeOutputs(self):
  134. try:
  135. address = self.outputAddressBase
  136. for slave in self.slaveList:
  137. # Get the output data from the CPU
  138. outData = self.sim.cpu.fetchOutputRange(address,
  139. slave.outputAddressRangeSize)
  140. # Send it to the slave and request the input data.
  141. inData = self.master.dataExchange(slave.slaveAddr,
  142. outData)
  143. # Cache the input data for the readInputs() call.
  144. self.cachedInputs.append(inData)
  145. # Adjust the address base for the next slave.
  146. address += slave.outputAddressRangeSize
  147. except self.pyprofibus.ProfibusError as e:
  148. self.raiseException("Hardware error: %s" % str(e))
  149. def directReadInput(self, accessWidth, accessOffset):
  150. return None#TODO
  151. def directWriteOutput(self, accessWidth, accessOffset, data):
  152. return False#TODO