1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
# -*- coding: utf-8 -*-
#
# AWL simulator - LinuxCNC HAL interface
#
# Copyright 2013-2017 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
from __future__ import division, absolute_import, print_function, unicode_literals
#from awlsim.common.cython_support cimport * #@cy
from awlsim.common.compat import *
from awlsim.common.util import *
from awlsim.common.exceptions import *
#from awlsimhw_linuxcnc.main cimport * #@cy
from awlsim.common.datatypehelpers import * #+cimport
from awlsim.core.hardware_params import *
from awlsim.core.hardware import * #+cimport
from awlsim.core.operators import * #+cimport
from awlsim.core.offset import * #+cimport
from awlsim.core.cpu import * #+cimport
class SigBit(object):
def __init__(self, hal, halName, byteOffset, bitOffset):
self.hal = hal
self.halName = halName
self.byteOffset = byteOffset
self.bitOffset = bitOffset
self.setMask = 1 << bitOffset
self.clrMask = ~(1 << bitOffset)
def readInput(self, destBuf):
if self.hal[self.halName]:
destBuf[self.byteOffset] |= self.setMask
else:
destBuf[self.byteOffset] &= self.clrMask
def writeOutput(self, srcBuf):
self.hal[self.halName] = (srcBuf[self.byteOffset] >> self.bitOffset) & 1
class SigU8(object):
def __init__(self, hal, halName, offset):
self.hal = hal
self.halName = halName
self.offset = offset
def readInput(self, destBuf):
destBuf[self.offset] = self.hal[self.halName] & 0xFF
def writeOutput(self, srcBuf):
self.hal[self.halName] = srcBuf[self.offset] & 0xFF
class SigU16(object):
def __init__(self, hal, halName, offset):
self.hal = hal
self.halName = halName
self.offset = offset
def readInput(self, destBuf):
word = self.hal[self.halName] & 0xFFFF
destBuf[self.offset] = (word >> 8) & 0xFF
destBuf[self.offset + 1] = word & 0xFF
def writeOutput(self, srcBuf):
word = (srcBuf[self.offset] << 8) |\
srcBuf[self.offset + 1]
self.hal[self.halName] = word & 0xFFFF
class SigS16(object):
def __init__(self, hal, halName, offset):
self.hal = hal
self.halName = halName
self.offset = offset
def readInput(self, destBuf):
word = self.hal[self.halName] & 0xFFFF
destBuf[self.offset] = (word >> 8) & 0xFF
destBuf[self.offset + 1] = word & 0xFF
def writeOutput(self, srcBuf):
word = (srcBuf[self.offset] << 8) |\
srcBuf[self.offset + 1]
self.hal[self.halName] = wordToSignedPyInt(word)
class SigU31(object):
def __init__(self, hal, halName, offset):
self.hal = hal
self.halName = halName
self.offset = offset
def readInput(self, destBuf):
dword = self.hal[self.halName] & 0x7FFFFFFF
destBuf[self.offset] = (dword >> 24) & 0xFF
destBuf[self.offset + 1] = (dword >> 16) & 0xFF
destBuf[self.offset + 2] = (dword >> 8) & 0xFF
destBuf[self.offset + 3] = dword & 0xFF
def writeOutput(self, srcBuf):
dword = (srcBuf[self.offset] << 24) |\
(srcBuf[self.offset + 1] << 16) |\
(srcBuf[self.offset + 2] << 8) |\
srcBuf[self.offset + 3]
self.hal[self.halName] = dword & 0x7FFFFFFF
class SigS32(object):
def __init__(self, hal, halName, offset):
self.hal = hal
self.halName = halName
self.offset = offset
def readInput(self, destBuf):
dword = self.hal[self.halName] & 0xFFFFFFFF
destBuf[self.offset] = (dword >> 24) & 0xFF
destBuf[self.offset + 1] = (dword >> 16) & 0xFF
destBuf[self.offset + 2] = (dword >> 8) & 0xFF
destBuf[self.offset + 3] = dword & 0xFF
def writeOutput(self, srcBuf):
dword = (srcBuf[self.offset] << 24) |\
(srcBuf[self.offset + 1] << 16) |\
(srcBuf[self.offset + 2] << 8) |\
srcBuf[self.offset + 3]
self.hal[self.halName] = dwordToSignedPyInt(dword)
class SigFloat(object):
def __init__(self, hal, halName, offset):
self.hal = hal
self.halName = halName
self.offset = offset
def readInput(self, destBuf):
dword = pyFloatToDWord(self.hal[self.halName])
destBuf[self.offset] = (dword >> 24) & 0xFF
destBuf[self.offset + 1] = (dword >> 16) & 0xFF
destBuf[self.offset + 2] = (dword >> 8) & 0xFF
destBuf[self.offset + 3] = dword & 0xFF
def writeOutput(self, srcBuf):
dword = (srcBuf[self.offset] << 24) |\
(srcBuf[self.offset + 1] << 16) |\
(srcBuf[self.offset + 2] << 8) |\
srcBuf[self.offset + 3]
self.hal[self.halName] = dwordToPyFloat(dword)
class HardwareInterface_LinuxCNC(AbstractHardwareInterface): #+cdef
name = "LinuxCNC"
description = "LinuxCNC and MachineKit hardware support.\n"\
"http://linuxcnc.org/\n"\
"http://www.machinekit.io/"
paramDescs = [
HwParamDesc_pyobject("hal",
description = "LinuxCNC HAL instance object",
mandatory = True),
HwParamDesc_int("inputSize",
description = "Input area size",
defaultValue = 32,
mandatory = True),
HwParamDesc_int("outputSize",
description = "Output area size",
defaultValue = 32,
mandatory = True),
]
def __init__(self, sim, parameters={}):
AbstractHardwareInterface.__init__(self,
sim = sim,
parameters = parameters)
self.linuxCNC_initialized = False
def doStartup(self):
if not self.linuxCNC_initialized:
try:
import hal as LinuxCNC_HAL
self.LinuxCNC_HAL = LinuxCNC_HAL
except ImportError as e:
self.raiseException("Failed to import LinuxCNC HAL module"
":\n%s" % str(e))
# Get the LinuxCNC-HAL-component object
self.hal = self.getParamValueByName("hal")
# Get parameters
self.inputSize = self.getParamValueByName("inputSize")
self.outputSize = self.getParamValueByName("outputSize")
self.__configDone = False
# Signal LinuxCNC that we are ready.
self.hal.ready()
self.linuxCNC_initialized = True
def doShutdown(self):
pass
#TODO find overlappings
def __buildTable(self, baseName, addressBase, size):
tab = []
for address in range(addressBase, addressBase + size):
offset = address - addressBase
for bitNr in range(8):
if self.hal["%s.bit.%d.%d.active" % (baseName, address, bitNr)]:
tab.append(SigBit(self.hal,
"%s.bit.%d.%d" % (baseName, address, bitNr),
offset, bitNr))
if self.hal["%s.u8.%d.active" % (baseName, address)]:
tab.append(SigU8(self.hal,
"%s.u8.%d" % (baseName, address),
offset))
if address % 2:
continue
if size - offset < 2:
continue
if self.hal["%s.u16.%d.active" % (baseName, address)]:
tab.append(SigU16(self.hal,
"%s.u16.%d" % (baseName, address),
offset))
if self.hal["%s.s16.%d.active" % (baseName, address)]:
tab.append(SigS16(self.hal,
"%s.s16.%d" % (baseName, address),
offset))
if size - offset < 4:
continue
if self.hal["%s.u31.%d.active" % (baseName, address)]:
tab.append(SigU31(self.hal,
"%s.u31.%d" % (baseName, address),
offset))
if self.hal["%s.s32.%d.active" % (baseName, address)]:
tab.append(SigS32(self.hal,
"%s.s32.%d" % (baseName, address),
offset))
if self.hal["%s.float.%d.active" % (baseName, address)]:
tab.append(SigFloat(self.hal,
"%s.float.%d" % (baseName, address),
offset))
return tab
def __tryBuildConfig(self):
if not self.hal["config.ready"]:
return
self.__activeInputs = self.__buildTable("input",
self.inputAddressBase, self.inputSize)
#TODO dump the input table
self.__activeOutputs = self.__buildTable("output",
self.outputAddressBase, self.outputSize)
#TODO dump the input table
self.__configDone = True
printInfo("HAL configuration done")
def readInputs(self): #+cdef
if not self.__configDone:
self.__tryBuildConfig()
if not self.__configDone:
return
data = bytearray(self.inputSize)
for desc in self.__activeInputs:
desc.readInput(data)
self.sim.cpu.storeInputRange(self.inputAddressBase, data)
def writeOutputs(self): #+cdef
if not self.__configDone:
return
data = self.sim.cpu.fetchOutputRange(self.outputAddressBase,
self.outputSize)
for desc in self.__activeOutputs:
desc.writeOutput(data)
def directReadInput(self, accessWidth, accessOffset): #@nocy
#@cy cdef bytearray directReadInput(self, uint32_t accessWidth, uint32_t accessOffset):
pass#TODO
return bytearray()
def directWriteOutput(self, accessWidth, accessOffset, data): #@nocy
#@cy cdef ExBool_t directWriteOutput(self, uint32_t accessWidth, uint32_t accessOffset, bytearray data) except ExBool_val:
pass#TODO
return False
# Module entry point
HardwareInterface = HardwareInterface_LinuxCNC
|