-
Notifications
You must be signed in to change notification settings - Fork 2
/
cube.py
381 lines (322 loc) · 11.7 KB
/
cube.py
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/env python
# Copyright (C) Cezar Burlacu
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
All CubeMX parsing and the "models: mcu, pin"
"""
import sys
from utils import *
from collections import defaultdict
MCU_PATH = '/db/mcu/'
IP_PATH = '/db/mcu/IP/'
MCU_FAMILIES_PATH = '/db/mcu/families.xml'
CUBE_PATH = None
_resistor = {
'GPIO_PULLUP': 'PullUp',
'GPIO_PULLDOWN': 'PullDown',
'GPIO_NOPULL': 'Floating'
}
_speed = {
'GPIO_SPEED_FREQ_LOW': 'Minimum',
'GPIO_SPEED_FREQ_MEDIUM': 'Low',
'GPIO_SPEED_FREQ_HIGH': 'High',
'GPIO_SPEED_FREQ_VERY_HIGH': 'Maximum',
}
# by default CubeMX is setting the GPIO as low
_level = {
'GPIO_PIN_SET': 'High',
'GPIO_PIN_RESET': 'Low',
}
_signal = {
r'GPIO_Input': 'Input',
r'GPIO_Output': 'Output',
r'ADC[0-9x]+_IN[0-9]+': 'Analog',
r'IN[0-9]{1,2}': 'Analog',
# r'.*JTMS-SWDIO.*': 'Alternate',
# r'.*JTCK-SWCLK.*': 'Alternate',
# r'.*UART.*': 'Alternate',
# r'.*USART.*': 'Alternate',
}
_type = {
'GPIO_MODE_OUTPUT_OD': 'OpenDrain',
'GPIO_MODE_OUTPUT_PP': 'PushPull'
}
_defaultValues = {}
def getChibiValue(prop, cube2ch, cube):
if cube in cube2ch:
return cube2ch[cube]
elif prop in _defaultValues:
if _defaultValues[prop] in cube2ch:
return cube2ch[_defaultValues[prop]]
return None
class Pin:
pinNo = -1
CName = "N/A" # cube mx name
Pin = "N/A"
SignalName = None
AnalogSwitch = None
PinLocked = None
ID = None
Type = None
Level = None
Speed = None
Resistor = None
Mode = None
ModeCube = None
Alternate = None
_gpioDesc = None
_gpioNs = ''
parent = None
def __init__(self):
pass
def getModeFromSignal(self, signal):
mode = "Input"
for key in _signal:
if re.match(key, signal):
mode = _signal[key]
return mode
# find alternate function
if self._gpioDesc is not None:
pinSignal = getElem(self._gpioDesc, "PinSignal[@Name='%s']//{0}PossibleValue" % signal, self._gpioNs)
if pinSignal is not None:
# print ("Success for %s" % pinSignal.text)
expr = r'GPIO_AF([0-9]{1,2}).*'
m = re.match(expr, pinSignal.text)
if m and len(m.groups()) >= 1:
mode = self.Mode = 'Alternate'
self.Alternate = m.group(1)
# print(signal, self.Mode, self.Alternate)
else:
print("Failed to read %s" % signal)
else:
print("Failed: %s" % signal, self.CName, self.Pin)
else:
print("Invalid description - %s / %s " % (self.Pin, signal))
return mode
def update(self, prop, value):
updated = False
if prop == 'GPIO_Label':
self.ID = value
updated = True
elif prop == 'GPIO_PuPd':
self.Resistor = getChibiValue(prop, _resistor, value)
updated = True
elif prop == 'GPIO_Speed':
self.Speed = getChibiValue(prop, _speed, value)
updated = True
elif prop == 'Signal':
self.SignalName = value
self.Mode = self.getModeFromSignal(value)
updated = True
elif prop == 'Mode':
self.ModeCube = value # the mode will be updated when signal is set
updated = True
elif prop == 'PinState':
self.Level = getChibiValue(prop, _level, value)
updated = True
elif prop == 'GPIO_ModeDefaultOutputPP':
self.Type = getChibiValue(prop, _type, value)
updated = True
else:
print("{0}: property {1} is not used? {2}".format(self.CName, prop, value))
# load all default values for a Pin that is defined in Cube project
# in this way, all default Chibi properties should be overridden
if updated:
self.loadDefaults()
# Load default value ONLY if is not loaded yet
# these default values are default in CubeMX
def loadDefaults(self):
emptyDict = {}
self.ID = '' if self.ID is None else self.ID
self.Resistor = getChibiValue('GPIO_PuPd', _resistor, 'N/A') if self.Resistor is None else self.Resistor
self.Speed = getChibiValue('GPIO_Speed', _speed, 'N/A') if self.Speed is None else self.Speed
self.Mode = 'Input' if self.Mode is None else self.Mode
self.Level = getChibiValue('PinState', _level, 'N/A') if self.Level is None else self.Level
self.Type = getChibiValue('GPIO_ModeDefaultOutputPP', _type, 'N/A') if self.Type is None else self.Type
self.Alternate = '0' if self.Alternate is None else self.Alternate
def __str__(self):
return"Pin: %s, %s, %s, %s, %s" % (self.Pin, self.SignalName, self.Mode, self.Speed, self.Resistor)
class MCU:
partNumber = None
name = ""
pins = {}
ports = defaultdict(list)
gpiosDesc = None
HSEClock = None
LSEClock = None
VDD = None
Family = None
CubeFile = ""
def __init__(self, partNo):
self.partNumber = partNo
self.name = ""
self.pins = {}
self.ports = defaultdict(list)
self.gpiosDesc = None
self.HSEClock = None
self.LSEClock = None
self.VDD = None
self.Family = None
self.CubeFile = ""
def updateProperties(self, props):
count = 0
dummy = {}
for key in props:
processed = False
m = re.match(r'(P[A-K][0-9]{1,2})\.(.*)', key, flags=re.IGNORECASE)
if m and len(m.groups()) == 2:
gpioName = m.group(1)
gpioProp = m.group(2)
port, pin = getPortInfo(gpioName)
pinName = getPinName(port, pin)
pin = self.pins[pinName] if pinName in self.pins else None
if pin is not None:
dummy[pin.Pin] = "dummy"
pin.update(gpioProp, props[key])
count += 1
else:
print("%s was not found in pins??" % gpioName)
# else:
# print("%s is not a GPIO" % key)
if not processed: # try RCC - clocks
if key == 'RCC.HSE_VALUE':
self.HSEClock = props[key]
elif key == 'PCC.Vdd':
try:
vdd = float(props[key]) * 100
self.VDD = str(int(vdd))
except:
pass
elif key == 'RCC.LSE_VALUE':
self.LSEClock = props[key]
# elif key == '':
# elif key == '':
print("%s properties of %d GPIOs were updated from CubeMX project" %
(count, len(dummy)))
def __str__(self):
return "MCU: %s\nName: %s" % (self.partNumber, self.name)
def getDefaultValue(parent, ns, refName):
retVal = None
elem = getElem(parent, "RefParameter[@Name='%s']" % refName, ns)
if elem is not None and 'DefaultValue' in elem.attrib:
retVal = elem.attrib['DefaultValue']
return retVal
def loadDefaultValues(gpio, ns):
retVal = {}
val = getDefaultValue(gpio, ns, 'PinState')
if val is not None:
retVal['PinState'] = val
val = getDefaultValue(gpio, ns, 'GPIO_ModeDefaultOutputPP')
if val is not None:
retVal['GPIO_ModeDefaultOutputPP'] = val
val = getDefaultValue(gpio, ns, 'GPIO_Speed')
if val is not None:
retVal['GPIO_Speed'] = val
val = getDefaultValue(gpio, ns, 'GPIO_PuPd')
if val is not None:
retVal['GPIO_PuPd'] = val
val = getDefaultValue(gpio, ns, 'GPIO_ModeDefaultPP')
if val is not None:
retVal['GPIO_ModeDefaultPP'] = val
val = getDefaultValue(gpio, ns, 'GPIO_ModeDefaultOutputPP')
if val is not None:
retVal['GPIO_ModeDefaultOutputPP'] = val
val = getDefaultValue(gpio, ns, 'GPIO_ModeDefaultEXTI')
if val is not None:
retVal['GPIO_ModeDefaultEXTI'] = val
val = getDefaultValue(gpio, ns, 'GPIO_AF')
if val is not None:
retVal['GPIO_AF'] = val
val = getDefaultValue(gpio, ns, 'GPIO_Speed_High_Default')
if val is not None:
retVal['GPIO_Speed_High_Default'] = val
return retVal
def _load_(partNo):
mcu = MCU(partNo)
print("Loading %s" % mcu.partNumber)
try:
root, ns = getRoot(CUBE_PATH + MCU_FAMILIES_PATH)
if root is None or ns is None:
return None
elems = getElems(root, "Mcu[@RefName='%s']" % mcu.partNumber, ns)
if elems is None or len(elems) != 1:
print("Cannot identify the MCU (%s)" % mcu.partNumber)
sys.exit(-4)
mcuDesc = elems[0]
try:
mcu.Family = mcuDesc.getparent().getparent().attrib['Name'] + 'xx'
except:
pass
mcu.name = mcuDesc.attrib['Name']
mcuDesc, ns = getRoot(CUBE_PATH + MCU_PATH + mcu.name + ".xml")
gpioIp = getElems(mcuDesc, "IP[@Name='GPIO']", ns)
if len(gpioIp) == 1:
gpioVersion = gpioIp[0].attrib['Version']
print("GPIO version is '%s'" % gpioVersion)
gpioDesc, gns = getRoot(CUBE_PATH + IP_PATH + ("GPIO-%s_Modes.xml" % gpioVersion))
global _defaultValues
_defaultValues = loadDefaultValues(gpioDesc, gns)
mcu.gpiosDesc = getElems(gpioDesc, "GPIO_Pin", gns)
# print(self.gpiosDesc)
else:
print ("Invalid GPIO description")
pinsDesc = getElems(mcuDesc, "Pin", ns)
print("%s has %d pins" % (partNo, len(pinsDesc)))
count = 0
alreadyExists = 0
duplicates = {}
for pinDesc in pinsDesc:
pin = Pin()
pin.parent = mcu
pin.pinNo = pinDesc.attrib['Position']
pin.CName = pinDesc.attrib['Name']
port, pinNo = getPortInfo(pin.CName)
pinName = getPinName(port, pinNo)
pin.Pin = pinName if not isEmpty(pinName) else pin.CName
# find gpio description from gpio-xxx_modes.xml
gpio_desc = [i for i in mcu.gpiosDesc if
i.attrib['Name'] == pin.CName]
pin._gpioDesc = gpio_desc[0] if len(gpio_desc) >= 1 else None
pin._gpioNs = gns
if pin.Pin in mcu.pins:
alreadyExists += 1
duplicates[pin.Pin] = "dummy"
mcu.pins[pin.Pin] = pin
count += 1
if port is not None and pinNo is not None:
mcu.ports[port].append(pinNo)
print("%s has %d pins (%d) (%d duplicates - %s)" % (
mcu.partNumber, len(mcu.pins), count, alreadyExists, str(duplicates.keys())))
except KeyError as xxx:
mcu = None
print("Failed to load %s because of %s" % (partNo, xxx))
return mcu
def getMCU(partNo):
mcu = _load_(partNo)
return mcu
def loadIOC(filename):
lines = {}
with open(filename) as f:
while True:
line = f.readline().strip()
if not line:
break
if line[0] == '#':
continue
# print(line)
vals = line.split('=', 2)
key = vals[0]
value = vals[1]
lines[key] = value
return lines