|
| 1 | +import sys |
| 2 | +import re |
| 3 | +import os |
| 4 | +from xml.dom import minidom |
| 5 | +from xml.dom.minidom import parse, Node |
| 6 | +io_list = [] #'PIN','name' |
| 7 | +adclist = [] #'PIN','name','ADCSignal' |
| 8 | +daclist = [] #'PIN','name','DACSignal' |
| 9 | +i2cscl_list = [] #'PIN','name','I2CSCLSignal' |
| 10 | +i2csda_list = [] #'PIN','name','I2CSDASignal' |
| 11 | +pwm_list = [] #'PIN','name','PWM' |
| 12 | +uarttx_list = [] #'PIN','name','UARTtx' |
| 13 | +uartrx_list = [] #'PIN','name','UARTrx' |
| 14 | +uartcts_list = [] #'PIN','name','UARTcts' |
| 15 | +uartrts_list = [] #'PIN','name','UARTrts' |
| 16 | +spimosi_list = [] #'PIN','name','SPIMOSI' |
| 17 | +spimiso_list = [] #'PIN','name','SPIMISO' |
| 18 | +spissel_list = [] #'PIN','name','SPISSEL' |
| 19 | +spisclk_list = [] #'PIN','name','SPISCLK' |
| 20 | +cantd_list = [] #'PIN','name','CANTD' |
| 21 | +canrd_list = [] #'PIN','name','CANRD' |
| 22 | +eth_list = [] #'PIN','name','ETH' |
| 23 | + |
| 24 | + |
| 25 | +def find_gpio_file(xmldoc): |
| 26 | + res = 'ERROR' |
| 27 | + itemlist = xmldoc.getElementsByTagName('IP') |
| 28 | + for s in itemlist: |
| 29 | + a = s.attributes['Name'].value |
| 30 | + if "GPIO" in a: |
| 31 | + res = s.attributes['Version'].value |
| 32 | + return res |
| 33 | + |
| 34 | +def get_gpio_af_num(xml, pintofind, iptofind): |
| 35 | +# xml = parse('GPIO-STM32L051_gpio_v1_0_Modes.xml') |
| 36 | + #xml = parse(gpiofile) |
| 37 | + #DBG print ('pin to find ' + pintofind) |
| 38 | + i=0 |
| 39 | + mygpioaf = 'NOTFOUND' |
| 40 | + for n in xml.documentElement.childNodes: |
| 41 | + i += 1 |
| 42 | + j = 0 |
| 43 | + if n.nodeType == Node.ELEMENT_NODE: |
| 44 | + for premlevel in n.attributes.items(): |
| 45 | +# if 'PB7' in premlevel: |
| 46 | + if pintofind == premlevel[1]: |
| 47 | + #DBG print (i , premlevel) |
| 48 | + #n = noeud de la pin recherchee |
| 49 | + for m in n.childNodes: |
| 50 | + j += 1 |
| 51 | + k = 0 |
| 52 | + if m.nodeType == Node.ELEMENT_NODE: |
| 53 | + for deuzlevel in m.attributes.items(): |
| 54 | + k += 1 |
| 55 | +# if 'I2C1_SDA' in deuzlevel: |
| 56 | + if iptofind in deuzlevel: |
| 57 | + #DBG print (i, j, m.attributes.items()) |
| 58 | + # m = noeud de l'IP recherchee |
| 59 | + for p in m.childNodes: |
| 60 | + if p.nodeType == Node.ELEMENT_NODE: |
| 61 | + #p noeud du 'Specific parameter' |
| 62 | + #DBG print (i,j,k,p.attributes.items()) |
| 63 | + for myc in p.childNodes: |
| 64 | + #DBG print (myc) |
| 65 | + if myc.nodeType == Node.ELEMENT_NODE: |
| 66 | + #myc = noeud du ALTERNATE !!! ENFIN !!! |
| 67 | + for mygpioaflist in myc.childNodes: |
| 68 | + mygpioaf = mygpioaflist.data |
| 69 | + #print (mygpioaf) |
| 70 | + if mygpioaf == 'NOTFOUND': |
| 71 | + print ('GPIO AF not found in ' + gpiofile + ' for ' + pintofind + ' and the IP ' + iptofind) |
| 72 | + #quit() |
| 73 | + return mygpioaf |
| 74 | + |
| 75 | +def store_pin (pin, name): |
| 76 | + #store pin I/O |
| 77 | + p = [pin, name] |
| 78 | + io_list.append(p) |
| 79 | + |
| 80 | +#function to store ADC list |
| 81 | +def store_adc (pin, name, signal): |
| 82 | + adclist.append([pin,name,signal]) |
| 83 | + |
| 84 | +#function to store DAC list |
| 85 | +def store_dac (pin, name, signal): |
| 86 | + daclist.append([pin,name,signal]) |
| 87 | + |
| 88 | +#function to store I2C list |
| 89 | +def store_i2c (pin, name, signal): |
| 90 | + #is it SDA or SCL ? |
| 91 | + if "_SCL" in signal: |
| 92 | + i2cscl_list.append([pin,name,signal]) |
| 93 | + if "_SDA" in signal: |
| 94 | + i2csda_list.append([pin,name,signal]) |
| 95 | + |
| 96 | +#function to store timers |
| 97 | +def store_pwm(pin, name, signal): |
| 98 | + if "_CH" in signal: |
| 99 | + pwm_list.append([pin,name,signal]) |
| 100 | + |
| 101 | +#function to store Uart pins |
| 102 | +def store_uart(pin, name, signal): |
| 103 | + if "_TX" in signal: |
| 104 | + uarttx_list.append([pin,name,signal]) |
| 105 | + if "_RX" in signal: |
| 106 | + uartrx_list.append([pin,name,signal]) |
| 107 | + if "_CTS" in signal: |
| 108 | + uartcts_list.append([pin,name,signal]) |
| 109 | + if "_RTS" in signal: |
| 110 | + uartrts_list.append([pin,name,signal]) |
| 111 | + |
| 112 | +#function to store SPI pins |
| 113 | +def store_spi(pin, name, signal): |
| 114 | + if "_MISO" in signal: |
| 115 | + spimiso_list.append([pin,name,signal]) |
| 116 | + if "_MOSI" in signal: |
| 117 | + spimosi_list.append([pin,name,signal]) |
| 118 | + if "_SCK" in signal: |
| 119 | + spisclk_list.append([pin,name,signal]) |
| 120 | + if "_NSS" in signal: |
| 121 | + spissel_list.append([pin,name,signal]) |
| 122 | + |
| 123 | +#function to store CAN pins |
| 124 | +def store_can(pin, name, signal): |
| 125 | + if "_RX" in signal: |
| 126 | + canrd_list.append([pin,name,signal]) |
| 127 | + if "_TX" in signal: |
| 128 | + cantd_list.append([pin,name,signal]) |
| 129 | + |
| 130 | +#function to store ETH list |
| 131 | +def store_eth (pin, name, signal): |
| 132 | + eth_list.append([pin,name,signal]) |
| 133 | + |
| 134 | +def print_header(): |
| 135 | + s = ("""/* |
| 136 | + ******************************************************************************* |
| 137 | + * Copyright (c) 2016, STMicroelectronics |
| 138 | + * All rights reserved. |
| 139 | + * |
| 140 | + * Redistribution and use in source and binary forms, with or without |
| 141 | + * modification, are permitted provided that the following conditions are met: |
| 142 | + * |
| 143 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 144 | + * this list of conditions and the following disclaimer. |
| 145 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 146 | + * this list of conditions and the following disclaimer in the documentation |
| 147 | + * and/or other materials provided with the distribution. |
| 148 | + * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| 149 | + * may be used to endorse or promote products derived from this software |
| 150 | + * without specific prior written permission. |
| 151 | + * |
| 152 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 153 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 154 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 155 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 156 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 157 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 158 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 159 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 160 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 161 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 162 | + ******************************************************************************* |
| 163 | + */ |
| 164 | +#include "Arduino.h" |
| 165 | +#include "%s.h" |
| 166 | +
|
| 167 | +// ===== |
| 168 | +// Note: Commented lines are alternative possibilities which are not used per default. |
| 169 | +// If you change them, you will have to know what you do |
| 170 | +// ===== |
| 171 | +
|
| 172 | +""" % re.sub('\.c$', '', out_filename)) |
| 173 | + out_file.write( s) |
| 174 | + |
| 175 | +def print_all_lists(): |
| 176 | + if print_list_header("ADC", "ADC", adclist, "ADC"): |
| 177 | + print_adc() |
| 178 | + if print_list_header("DAC", "DAC", daclist, "DAC"): |
| 179 | + print_dac() |
| 180 | + if print_list_header("I2C", "I2C_SDA", i2csda_list, "I2C"): |
| 181 | + print_i2c(xml, i2csda_list) |
| 182 | + if print_list_header("", "I2C_SCL", i2cscl_list, "I2C"): |
| 183 | + print_i2c(xml, i2cscl_list) |
| 184 | + if print_list_header("PWM", "PWM", pwm_list, "TIM"): |
| 185 | + print_pwm(xml) |
| 186 | + if print_list_header("SERIAL", "UART_TX", uarttx_list, "UART"): |
| 187 | + print_uart(xml, uarttx_list) |
| 188 | + if print_list_header("", "UART_RX", uartrx_list, "UART"): |
| 189 | + print_uart(xml, uartrx_list) |
| 190 | + if print_list_header("", "UART_RTS", uartrts_list, "UART"): |
| 191 | + print_uart(xml, uartrts_list) |
| 192 | + if print_list_header("", "UART_CTS", uartcts_list, "UART"): |
| 193 | + print_uart(xml, uartcts_list) |
| 194 | + if print_list_header("SPI", "SPI_MOSI", spimosi_list, "SPI"): |
| 195 | + print_spi(xml, spimosi_list) |
| 196 | + if print_list_header("", "SPI_MISO", spimiso_list, "SPI"): |
| 197 | + print_spi(xml, spimiso_list) |
| 198 | + if print_list_header("", "SPI_SCLK", spisclk_list, "SPI"): |
| 199 | + print_spi(xml, spisclk_list) |
| 200 | + if print_list_header("", "SPI_SSEL", spissel_list, "SPI"): |
| 201 | + print_spi(xml, spissel_list) |
| 202 | + if print_list_header("CAN", "CAN_RD", canrd_list, "CAN"): |
| 203 | + print_can(xml, canrd_list) |
| 204 | + if print_list_header("", "CAN_TD", cantd_list, "CAN"): |
| 205 | + print_can(xml, cantd_list) |
| 206 | + if print_list_header("ETHERNET", "Ethernet", eth_list, "ETH"): |
| 207 | + print_eth(xml, eth_list) |
| 208 | + |
| 209 | +def print_list_header(comment, name, l, switch): |
| 210 | + if len(l)>0: |
| 211 | + if comment: |
| 212 | + s = (""" |
| 213 | +//*** %s *** |
| 214 | +""") % comment |
| 215 | + else: |
| 216 | + s = "" |
| 217 | + s += (""" |
| 218 | +#ifdef HAL_%s_MODULE_ENABLED |
| 219 | +const PinMap PinMap_%s[] = { |
| 220 | +""") % (switch, name) |
| 221 | + else: |
| 222 | + if comment: |
| 223 | + s = (""" |
| 224 | +//*** %s *** |
| 225 | +""") % comment |
| 226 | + else: |
| 227 | + s = "" |
| 228 | + s+=(""" |
| 229 | +//*** No %s *** |
| 230 | +""") % name |
| 231 | + out_file.write(s) |
| 232 | + return len(l) |
| 233 | + |
| 234 | +def print_adc(): |
| 235 | + i = 0 |
| 236 | + if len(adclist)>0: |
| 237 | + # Check GPIO version (alternate or not) |
| 238 | + s_pin_data = 'STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, ' |
| 239 | + while i < len(adclist): |
| 240 | + p=adclist[i] |
| 241 | + if "IN" in p[2]: |
| 242 | + s1 = "%-12s" % (" {" + p[0] + ',') |
| 243 | + a = p[2].split('_') |
| 244 | + inst = a[0].replace("ADC", "") |
| 245 | + if len(inst) == 0: |
| 246 | + inst = '1' #single ADC for this product |
| 247 | + s1 += "%-7s" % ('ADC' + inst + ',') |
| 248 | + chan = a[1].replace("IN", "") |
| 249 | + s1 += s_pin_data + chan |
| 250 | + s1 += ', 0)}, // ' + p[2] + '\n' |
| 251 | + out_file.write(s1) |
| 252 | + i += 1 |
| 253 | + |
| 254 | + out_file.write( """ {NC, NP, 0} |
| 255 | +}; |
| 256 | +#endif |
| 257 | +""") |
| 258 | + |
| 259 | +def print_dac(): |
| 260 | + i = 0 |
| 261 | + if len(daclist)>0: |
| 262 | + while i < len(daclist): |
| 263 | + p=daclist[i] |
| 264 | + b=p[2] |
| 265 | + s1 = "%-12s" % (" {" + p[0] + ',') |
| 266 | + #2nd element is the DAC signal |
| 267 | + if b[3] == '_': # 1 DAC in this chip |
| 268 | + s1 += 'DAC1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, ' + b[7] + ', 0)}, // ' + b + '\n' |
| 269 | + else: |
| 270 | + s1 += 'DAC' + b[3] + ', STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, ' + b[8] + ', 0)}, // ' + b + '\n' |
| 271 | + out_file.write(s1) |
| 272 | + i += 1 |
| 273 | + out_file.write( """ {NC, NP, 0} |
| 274 | +}; |
| 275 | +#endif |
| 276 | +""") |
| 277 | + |
| 278 | +def print_i2c(xml, l): |
| 279 | + i = 0 |
| 280 | + if len(l)>0: |
| 281 | + while i < len(l): |
| 282 | + p=l[i] |
| 283 | + result = get_gpio_af_num(xml, p[1], p[2]) |
| 284 | + if result != 'NOTFOUND': |
| 285 | + s1 = "%-12s" % (" {" + p[0] + ',') |
| 286 | + #2nd element is the I2C XXX signal |
| 287 | + b = p[2].split('_')[0] |
| 288 | + s1 += b[:len(b)-1] + b[len(b)-1] + ', STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, ' |
| 289 | + s1 += result + ')},\n' |
| 290 | + out_file.write(s1) |
| 291 | + i += 1 |
| 292 | + out_file.write( """ {NC, NP, 0} |
| 293 | +}; |
| 294 | +#endif |
| 295 | +""") |
| 296 | + |
| 297 | +def print_pwm(xml): |
| 298 | + i=0 |
| 299 | + if len(pwm_list)>0: |
| 300 | + while i < len(pwm_list): |
| 301 | + p=pwm_list[i] |
| 302 | + result = get_gpio_af_num(xml, p[1], p[2]) |
| 303 | + if result != 'NOTFOUND': |
| 304 | + s1 = "%-12s" % (" {" + p[0] + ',') |
| 305 | + #2nd element is the PWM signal |
| 306 | + a = p[2].split('_') |
| 307 | + inst = a[0] |
| 308 | + if len(inst) == 3: |
| 309 | + inst += '1' |
| 310 | + s1 += "%-8s" % (inst + ',') |
| 311 | + chan = a[1].replace("CH", "") |
| 312 | + if chan.endswith('N'): |
| 313 | + neg = ', 1' |
| 314 | + chan = chan.strip('N') |
| 315 | + else: |
| 316 | + neg = ', 0' |
| 317 | + s1 += 'STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, ' + result + ', ' + chan + neg |
| 318 | + s1 += ')}, // ' + p[2] + '\n' |
| 319 | + out_file.write(s1) |
| 320 | + i += 1 |
| 321 | + out_file.write( """ {NC, NP, 0} |
| 322 | +}; |
| 323 | +#endif |
| 324 | +""") |
| 325 | + |
| 326 | +def print_uart(xml, l): |
| 327 | + i=0 |
| 328 | + if len(l)>0: |
| 329 | + while i < len(l): |
| 330 | + p=l[i] |
| 331 | + result = get_gpio_af_num(xml, p[1], p[2]) |
| 332 | + if result != 'NOTFOUND': |
| 333 | + s1 = "%-12s" % (" {" + p[0] + ',') |
| 334 | + #2nd element is the UART_XX signal |
| 335 | + b=p[2].split('_')[0] |
| 336 | + s1 += "%-9s" % (b[:len(b)-1] + b[len(b)-1:] + ',') |
| 337 | + s1 += 'STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, ' + result +')},\n' |
| 338 | + out_file.write(s1) |
| 339 | + i += 1 |
| 340 | + |
| 341 | + out_file.write( """ {NC, NP, 0} |
| 342 | +}; |
| 343 | +#endif |
| 344 | +""") |
| 345 | + |
| 346 | +def print_spi(xml, l): |
| 347 | + i=0 |
| 348 | + if len(l)>0: |
| 349 | + while i < len(l): |
| 350 | + p=l[i] |
| 351 | + result = get_gpio_af_num(xml, p[1], p[2]) |
| 352 | + if result != 'NOTFOUND': |
| 353 | + s1 = "%-12s" % (" {" + p[0] + ',') |
| 354 | + #2nd element is the SPI_XXXX signal |
| 355 | + instance=p[2].split('_')[0].replace("SPI", "") |
| 356 | + s1 += 'SPI' + instance + ', STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, ' |
| 357 | + s1 += result +')},\n' |
| 358 | + out_file.write(s1) |
| 359 | + i += 1 |
| 360 | + |
| 361 | + out_file.write( """ {NC, NP, 0} |
| 362 | +}; |
| 363 | +#endif |
| 364 | +""") |
| 365 | + |
| 366 | +def print_can(xml, l): |
| 367 | + i=0 |
| 368 | + if len(l)>0: |
| 369 | + while i < len(l): |
| 370 | + p=l[i] |
| 371 | + b=p[2] |
| 372 | + result = get_gpio_af_num(xml, p[1], p[2]) |
| 373 | + if result != 'NOTFOUND': |
| 374 | + s1 = "%-12s" % (" {" + p[0] + ',') |
| 375 | + #2nd element is the CAN_XX signal |
| 376 | + instance = p[2].split('_')[0].replace("CAN", "") |
| 377 | + #if len(instance) == 0: |
| 378 | + # instance = '1' |
| 379 | + s1 += 'CAN' + instance + ', STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, ' |
| 380 | + s1 += result +')},\n' |
| 381 | + out_file.write( s1) |
| 382 | + i += 1 |
| 383 | + out_file.write( """ {NC, NP, 0} |
| 384 | +}; |
| 385 | +#endif |
| 386 | +""") |
| 387 | + |
| 388 | +def print_eth(xml, l): |
| 389 | + i=0 |
| 390 | + if len(l)>0: |
| 391 | + prev_s = '' |
| 392 | + while i < len(l): |
| 393 | + p=l[i] |
| 394 | + result = get_gpio_af_num(xml, p[1], p[2]) |
| 395 | + if result != 'NOTFOUND': |
| 396 | + s1 = "%-12s" % (" {" + p[0] + ',') |
| 397 | + #2nd element is the ETH_XXXX signal |
| 398 | + s1 += 'ETH, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, ' + result +')},' |
| 399 | + #check duplicated lines, only signal differs |
| 400 | + if (prev_s == s1): |
| 401 | + s1 = '|' + p[2] |
| 402 | + else: |
| 403 | + if len(prev_s)>0: |
| 404 | + out_file.write('\n') |
| 405 | + prev_s = s1 |
| 406 | + s1 += ' // ' + p[2] |
| 407 | + out_file.write(s1) |
| 408 | + i += 1 |
| 409 | + |
| 410 | + out_file.write( """\n {NC, NP, 0} |
| 411 | +}; |
| 412 | +#endif |
| 413 | +""") |
| 414 | + |
| 415 | +tokenize = re.compile(r'(\d+)|(\D+)').findall |
| 416 | +def natural_sortkey(list_2_elem): |
| 417 | + |
| 418 | + return tuple(int(num) if num else alpha for num, alpha in tokenize(list_2_elem[0])) |
| 419 | + |
| 420 | +def sort_my_lists(): |
| 421 | + adclist.sort(key=natural_sortkey) |
| 422 | + daclist.sort(key=natural_sortkey) |
| 423 | + i2cscl_list.sort(key=natural_sortkey) |
| 424 | + i2csda_list.sort(key=natural_sortkey) |
| 425 | + pwm_list.sort(key=natural_sortkey) |
| 426 | + uarttx_list.sort(key=natural_sortkey) |
| 427 | + uartrx_list.sort(key=natural_sortkey) |
| 428 | + uartcts_list.sort(key=natural_sortkey) |
| 429 | + uartrts_list.sort(key=natural_sortkey) |
| 430 | + spimosi_list.sort(key=natural_sortkey) |
| 431 | + spimiso_list.sort(key=natural_sortkey) |
| 432 | + spissel_list.sort(key=natural_sortkey) |
| 433 | + spisclk_list.sort(key=natural_sortkey) |
| 434 | + cantd_list.sort(key=natural_sortkey) |
| 435 | + canrd_list.sort(key=natural_sortkey) |
| 436 | + eth_list.sort(key=natural_sortkey) |
| 437 | + |
| 438 | + return |
| 439 | + |
| 440 | +# START MAIN PROGRAM |
| 441 | +#xmldoc = minidom.parse('STM32L051K(6-8)Tx.xml') |
| 442 | +cur_dir = os.getcwd() |
| 443 | +out_filename = 'PeripheralPins.c' |
| 444 | + |
| 445 | +if len(sys.argv) < 3: |
| 446 | + print("Usage: " + sys.argv[0] + " <BOARD_NAME> <product xml file name>") |
| 447 | + print(" - <BOARD_NAME> is the name of the board as it will be named in mbed") |
| 448 | + print(" - <product xml file name> is the STM32 file description in Cube MX") |
| 449 | + print(" !!This xml file contains non alpha characters in its name, you should call it with quotes") |
| 450 | + print("") |
| 451 | + print(" This script is able to generate the %s for a specific board" % out_filename ) |
| 452 | + print(" After file generation, review it carefully and ") |
| 453 | + print(" please report any issue to github:") |
| 454 | + print(" https://github.com/fpistm/stm32_tools/issues") |
| 455 | + print("") |
| 456 | + print(" Once generated, you should comment a line if the pin is generated ") |
| 457 | + print(" several times for the same IP") |
| 458 | + print(" or if the pin should not be used (overlaid with some HW on the board, ") |
| 459 | + print(" for instance)") |
| 460 | + quit() |
| 461 | + |
| 462 | +if sys.platform.startswith('win32'): |
| 463 | + #print ("Windows env") |
| 464 | + cubemxdir = 'C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeMX\db\mcu' |
| 465 | + cubemxdirIP = cubemxdir+"\\IP\\" |
| 466 | + input_file_name = cubemxdir+"\\" + sys.argv[2] |
| 467 | + out_path = cur_dir+'\\Arduino\\_'+sys.argv[1] |
| 468 | + output_filename = out_path+"\\"+out_filename |
| 469 | +else: |
| 470 | + #print ("Linux env") |
| 471 | + if sys.platform.startswith('linux'): |
| 472 | + cubemxdir = os.getenv("HOME")+'/STM32CubeMX/db/mcu' |
| 473 | + cubemxdirIP = cubemxdir+"/IP/" |
| 474 | + input_file_name = cubemxdir+'/'+ sys.argv[2] |
| 475 | + out_path = cur_dir+'/Arduino/'+sys.argv[1] |
| 476 | + output_filename = out_path+'/'+out_filename |
| 477 | + else: |
| 478 | + #print ("Darwin env") |
| 479 | + if sys.platform.startswith('darwin'): |
| 480 | + print("Platform is Mac OSX") |
| 481 | + cubemxdir = '/Applications/STMicroelectronics/STM32CubeMX.app/Contents/Resources/db/mcu' |
| 482 | + cubemxdirIP = cubemxdir+"/IP/" |
| 483 | + input_file_name = cubemxdir+'/'+ sys.argv[2] |
| 484 | + out_path = cur_dir+'/Arduino/'+sys.argv[1] |
| 485 | + output_filename = out_path+'/'+out_filename |
| 486 | + else: |
| 487 | + print ("Unsupported OS") |
| 488 | + quit() |
| 489 | + |
| 490 | +#open input file |
| 491 | +#check input file exists |
| 492 | +if not(os.path.isdir(cubemxdir)): |
| 493 | + print ("\n ! ! ! Cube Mx seems not to be installed or not at the requested location") |
| 494 | + print ("\n ! ! ! please check the value you set for cubemxdir variable at the top of " + sys.argv[0] + " file") |
| 495 | + quit() |
| 496 | +if not(os.path.isfile(input_file_name)): |
| 497 | + print ('\n ! ! ! '+sys.argv[2] + ' file not found') |
| 498 | + print ("\n ! ! ! Check in " + cubemxdir + " the correct name of this file") |
| 499 | + print ("\n ! ! ! You may use double quotes for this file if it contains special characters") |
| 500 | + quit() |
| 501 | + |
| 502 | + |
| 503 | +print (" * * * Opening input file...") |
| 504 | +if not(os.path.isdir(out_path)): |
| 505 | + os.makedirs(out_path) |
| 506 | +xmldoc = minidom.parse(input_file_name) |
| 507 | +itemlist = xmldoc.getElementsByTagName('Pin') |
| 508 | +#open output file |
| 509 | +if (os.path.isfile(output_filename)): |
| 510 | + print (" * * * * Requested %s file already exists and will be overwritten" % out_filename) |
| 511 | + os.remove(output_filename) |
| 512 | + |
| 513 | +out_file = open(output_filename, 'w') |
| 514 | + |
| 515 | +# Check if Alternate functions in GPIO or old GPIO (no alternate) |
| 516 | +if 'STM32F10' in sys.argv[2]: |
| 517 | + print ("STM32F10xx MCU not supported due to older GPIO IP version\n") |
| 518 | + quit() |
| 519 | + |
| 520 | +gpiofile = find_gpio_file(xmldoc) |
| 521 | +if gpiofile == 'ERROR': |
| 522 | + quit() |
| 523 | +xml = parse(cubemxdirIP + 'GPIO-' + gpiofile + '_Modes.xml') |
| 524 | +print (" * * * Getting pins and Ips for the xml file...") |
| 525 | +pinregex=r'^(P[A-Z][0-9][0-5]?)' |
| 526 | +for s in itemlist: |
| 527 | + m = re.match(pinregex, s.attributes['Name'].value) |
| 528 | + if m: |
| 529 | + pin = m.group(0) # pin formatted P<port><number>: PFO |
| 530 | + name = s.attributes['Name'].value.strip() # full name: "PF0 / OSC_IN" |
| 531 | + if s.attributes['Type'].value == "I/O": |
| 532 | + store_pin(pin, name) |
| 533 | + else: |
| 534 | + continue |
| 535 | + siglist = s.getElementsByTagName('Signal') |
| 536 | + for a in siglist: |
| 537 | + sig = a.attributes['Name'].value.strip() |
| 538 | + if "ADC" in sig: |
| 539 | + #store ADC pin |
| 540 | + store_adc( pin, name, sig) |
| 541 | + if all(["DAC" in sig, "_OUT" in sig]): |
| 542 | + #store DAC |
| 543 | + store_dac( pin, name, sig) |
| 544 | + if "I2C" in sig: |
| 545 | + #store DAC |
| 546 | + store_i2c( pin, name, sig) |
| 547 | + if re.match('^TIM', sig) is not None: #ignore HRTIM |
| 548 | + #store PWM |
| 549 | + store_pwm( pin, name, sig) |
| 550 | + if re.match('^(LPU|US|U)ART', sig) is not None: |
| 551 | + store_uart( pin, name, sig) |
| 552 | + if "SPI" in sig: |
| 553 | + store_spi( pin, name, sig) |
| 554 | + if "CAN" in sig: |
| 555 | + store_can( pin, name, sig) |
| 556 | + if "ETH" in sig: |
| 557 | + store_eth( pin, name, sig) |
| 558 | + |
| 559 | +print (" * * * Sorting lists...") |
| 560 | +sort_my_lists() |
| 561 | + |
| 562 | +print (" * * * Printing lists...") |
| 563 | +print_header() |
| 564 | +print_all_lists() |
| 565 | + |
| 566 | +nb_pin = (len(io_list)) |
| 567 | +print ("nb of I/O pins: %i" % nb_pin) |
| 568 | +print ('\n * * * ' + sys.argv[1]+' OK') |
0 commit comments