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
|
# -*- coding: utf-8 -*-
#
# AWL simulator - LinuxCNC HAL interface
#
# Copyright 2013 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.core.compat import *
from awlsim.core.hardware import *
from awlsim.core.util import *
from awlsim.core.datatypehelpers import *
class HardwareInterface(AbstractHardwareInterface):
name = "LinuxCNC"
paramDescs = [
HwParamDesc_pyobject("hal", pyTypeDesc = "<class 'hal.component'>",
description = "LinuxCNC HAL instance object",
mandatory = True),
HwParamDesc_int("inputSize",
description = "Input area size",
mandatory = True),
HwParamDesc_int("outputSize",
description = "Output area size",
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.getParam("hal")
# Get parameters
self.inputSize = self.getParam("inputSize")
self.outputSize = self.getParam("outputSize")
# Signal LinuxCNC that we are ready.
self.hal.ready()
self.linuxCNC_initialized = True
def doShutdown(self):
pass
@staticmethod
def __storeDWord(bytearr, offset, dword):
bytearr[offset] = (dword >> 24) & 0xFF
bytearr[offset + 1] = (dword >> 16) & 0xFF
bytearr[offset + 2] = (dword >> 8) & 0xFF
bytearr[offset + 3] = dword & 0xFF
def readInputs(self):
hal, base, size = self.hal, self.inputAddressBase, self.inputSize
# Start with empty data
inData = bytearray(size)
# Get the data from the HAL pins
for offset in range(size):
address = base + offset
for bitNr in range(8):
if hal["input.bit.%d.%d.active" % (address, bitNr)]:
if hal["input.bit.%d.%d" % (address, bitNr)]:
inData[offset] |= 1 << bitNr
else:
inData[offset] &= ~(1 << bitNr)
if size - offset < 4:
continue
if hal["input.u32.%d.active" % address]:
self.__storeDWord(inData, offset,
hal["input.u32.%d" % address])
if hal["input.s32.%d.active" % address]:
self.__storeDWord(inData, offset,
hal["input.u32.%d" % address] & 0xFFFFFFFF)
if hal["input.float.%d.active" % address]:
self.__storeDWord(inData, offset,
pyFloatToDWord(hal["input.u32.%d" % address]))
self.sim.cpu.storeInputRange(base, inData)
def writeOutputs(self):
hal, base, size = self.hal, self.outputAddressBase, self.outputSize
# Get the output data from the CPU
outData = self.sim.cpu.fetchOutputRange(base, size)
# Copy the data to the HAL pins
for offset in range(size):
address = base + offset
byteData = outData[offset]
for bitNr in range(8):
hal["output.bit.%d.%d" % (address, bitNr)] = (byteData >> bitNr) & 1
if size - offset < 4:
continue
dwordData = (byteData << 24) | (outData[offset + 1] << 16) |\
(outData[offset + 2] << 8) | outData[offset + 3]
hal["output.u32.%d" % address] = dwordData & 0x7FFFFFFF
hal["output.s32.%d" % address] = dwordToSignedPyInt(dwordData)
hal["output.float.%d" % address] = dwordToPyFloat(dwordData)
def directReadInput(self, accessWidth, accessOffset):
pass#TODO
def directWriteOutput(self, accessWidth, accessOffset, data):
pass#TODO
|