3030eth_list = [] #'PIN','name','ETH'
3131qspi_list = [] #'PIN','name','QUADSPI'
3232
33- def find_gpio_file (xmldoc ):
33+ def find_gpio_file ():
3434 res = 'ERROR'
35- itemlist = xmldoc .getElementsByTagName ('IP' )
35+ itemlist = xml_mcu .getElementsByTagName ('IP' )
3636 for s in itemlist :
3737 a = s .attributes ['Name' ].value
3838 if "GPIO" in a :
3939 res = s .attributes ['Version' ].value
4040 return res
4141
42- def get_gpio_af_num (xml , pintofind , iptofind ):
42+ def get_gpio_af_num (pintofind , iptofind ):
4343 if 'STM32F10' in mcu_file :
44- return get_gpio_af_numF1 (xml , pintofind , iptofind )
45- # xml = parse('GPIO-STM32L051_gpio_v1_0_Modes.xml')
46- #xml = parse(gpiofile)
44+ return get_gpio_af_numF1 (pintofind , iptofind )
4745 #DBG print ('pin to find ' + pintofind)
4846 i = 0
4947 mygpioaf = 'NOTFOUND'
50- for n in xml .documentElement .childNodes :
48+ for n in xml_gpio .documentElement .childNodes :
5149 i += 1
5250 j = 0
5351 if n .nodeType == Node .ELEMENT_NODE :
@@ -82,11 +80,11 @@ def get_gpio_af_num(xml, pintofind, iptofind):
8280 #quit()
8381 return mygpioaf .replace ('NOTFOUND ' , '' )
8482
85- def get_gpio_af_numF1 (xml , pintofind , iptofind ):
83+ def get_gpio_af_numF1 (pintofind , iptofind ):
8684 #print ('pin to find ' + pintofind + ' ip to find ' + iptofind)
8785 i = 0
8886 mygpioaf = 'NOTFOUND'
89- for n in xml .documentElement .childNodes :
87+ for n in xml_gpio .documentElement .childNodes :
9088 i += 1
9189 j = 0
9290 if n .nodeType == Node .ELEMENT_NODE :
@@ -240,35 +238,35 @@ def print_all_lists():
240238 if print_list_header ("DAC" , "DAC" , daclist , "DAC" ):
241239 print_dac ()
242240 if print_list_header ("I2C" , "I2C_SDA" , i2csda_list , "I2C" ):
243- print_i2c (xml , i2csda_list )
241+ print_i2c (i2csda_list )
244242 if print_list_header ("" , "I2C_SCL" , i2cscl_list , "I2C" ):
245- print_i2c (xml , i2cscl_list )
243+ print_i2c (i2cscl_list )
246244 if print_list_header ("PWM" , "PWM" , pwm_list , "TIM" ):
247- print_pwm (xml )
245+ print_pwm ()
248246 if print_list_header ("SERIAL" , "UART_TX" , uarttx_list , "UART" ):
249- print_uart (xml , uarttx_list )
247+ print_uart (uarttx_list )
250248 if print_list_header ("" , "UART_RX" , uartrx_list , "UART" ):
251- print_uart (xml , uartrx_list )
249+ print_uart (uartrx_list )
252250 if print_list_header ("" , "UART_RTS" , uartrts_list , "UART" ):
253- print_uart (xml , uartrts_list )
251+ print_uart (uartrts_list )
254252 if print_list_header ("" , "UART_CTS" , uartcts_list , "UART" ):
255- print_uart (xml , uartcts_list )
253+ print_uart (uartcts_list )
256254 if print_list_header ("SPI" , "SPI_MOSI" , spimosi_list , "SPI" ):
257- print_spi (xml , spimosi_list )
255+ print_spi (spimosi_list )
258256 if print_list_header ("" , "SPI_MISO" , spimiso_list , "SPI" ):
259- print_spi (xml , spimiso_list )
257+ print_spi (spimiso_list )
260258 if print_list_header ("" , "SPI_SCLK" , spisclk_list , "SPI" ):
261- print_spi (xml , spisclk_list )
259+ print_spi (spisclk_list )
262260 if print_list_header ("" , "SPI_SSEL" , spissel_list , "SPI" ):
263- print_spi (xml , spissel_list )
261+ print_spi (spissel_list )
264262 if print_list_header ("CAN" , "CAN_RD" , canrd_list , "CAN" ):
265- print_can (xml , canrd_list )
263+ print_can (canrd_list )
266264 if print_list_header ("" , "CAN_TD" , cantd_list , "CAN" ):
267- print_can (xml , cantd_list )
265+ print_can (cantd_list )
268266 if print_list_header ("ETHERNET" , "Ethernet" , eth_list , "ETH" ):
269- print_eth (xml , eth_list )
267+ print_eth ()
270268 if print_list_header ("QUADSPI" , "QUADSPI" , qspi_list , "QSPI" ):
271- print_qspi (xml , qspi_list )
269+ print_qspi ()
272270
273271def print_list_header (comment , name , l , switch ):
274272 if len (l )> 0 :
@@ -330,9 +328,9 @@ def print_dac():
330328#endif
331329""" )
332330
333- def print_i2c (xml , l ):
331+ def print_i2c (l ):
334332 for p in l :
335- result = get_gpio_af_num (xml , p [1 ], p [2 ])
333+ result = get_gpio_af_num (p [1 ], p [2 ])
336334 if result != 'NOTFOUND' :
337335 s1 = "%-12s" % (" {" + p [0 ] + ',' )
338336 #2nd element is the I2C XXX signal
@@ -347,9 +345,9 @@ def print_i2c(xml, l):
347345#endif
348346""" )
349347
350- def print_pwm (xml ):
348+ def print_pwm ():
351349 for p in pwm_list :
352- result = get_gpio_af_num (xml , p [1 ], p [2 ])
350+ result = get_gpio_af_num (p [1 ], p [2 ])
353351 if result != 'NOTFOUND' :
354352 s1 = "%-12s" % (" {" + p [0 ] + ',' )
355353 #2nd element is the PWM signal
@@ -374,9 +372,9 @@ def print_pwm(xml):
374372#endif
375373""" )
376374
377- def print_uart (xml , l ):
375+ def print_uart (l ):
378376 for p in l :
379- result = get_gpio_af_num (xml , p [1 ], p [2 ])
377+ result = get_gpio_af_num (p [1 ], p [2 ])
380378 if result != 'NOTFOUND' :
381379 s1 = "%-12s" % (" {" + p [0 ] + ',' )
382380 #2nd element is the UART_XX signal
@@ -395,9 +393,9 @@ def print_uart(xml, l):
395393#endif
396394""" )
397395
398- def print_spi (xml , l ):
396+ def print_spi (l ):
399397 for p in l :
400- result = get_gpio_af_num (xml , p [1 ], p [2 ])
398+ result = get_gpio_af_num (p [1 ], p [2 ])
401399 if result != 'NOTFOUND' :
402400 s1 = "%-12s" % (" {" + p [0 ] + ',' )
403401 #2nd element is the SPI_XXXX signal
@@ -412,10 +410,10 @@ def print_spi(xml, l):
412410#endif
413411""" )
414412
415- def print_can (xml , l ):
413+ def print_can (l ):
416414 for p in l :
417415 b = p [2 ]
418- result = get_gpio_af_num (xml , p [1 ], p [2 ])
416+ result = get_gpio_af_num (p [1 ], p [2 ])
419417 if result != 'NOTFOUND' :
420418 s1 = "%-12s" % (" {" + p [0 ] + ',' )
421419 #2nd element is the CAN_XX signal
@@ -435,10 +433,10 @@ def print_can(xml, l):
435433#endif
436434""" )
437435
438- def print_eth (xml , l ):
436+ def print_eth ():
439437 prev_s = ''
440- for p in l :
441- result = get_gpio_af_num (xml , p [1 ], p [2 ])
438+ for p in eth_list :
439+ result = get_gpio_af_num (p [1 ], p [2 ])
442440 if result != 'NOTFOUND' :
443441 s1 = "%-12s" % (" {" + p [0 ] + ',' )
444442 #2nd element is the ETH_XXXX signal
@@ -457,10 +455,10 @@ def print_eth(xml, l):
457455#endif
458456""" )
459457
460- def print_qspi (xml , l ):
458+ def print_qspi ():
461459 prev_s = ''
462- for p in l :
463- result = get_gpio_af_num (xml , p [1 ], p [2 ])
460+ for p in qspi_list :
461+ result = get_gpio_af_num (p [1 ], p [2 ])
464462 if result != 'NOTFOUND' :
465463 s1 = "%-12s" % (" {" + p [0 ] + ',' )
466464 #2nd element is the QUADSPI_XXXX signal
@@ -503,8 +501,6 @@ def sort_my_lists():
503501 eth_list .sort (key = natural_sortkey )
504502 qspi_list .sort (key = natural_sortkey )
505503
506- return
507-
508504def clean_all_lists ():
509505 del io_list [:]
510506 del adclist [:]
@@ -524,7 +520,42 @@ def clean_all_lists():
524520 del canrd_list [:]
525521 del eth_list [:]
526522 del qspi_list [:]
527-
523+
524+ def parse_pins ():
525+ print (" * Getting pins per Ips..." )
526+ pinregex = r'^(P[A-Z][0-9][0-5]?)'
527+ itemlist = xml_mcu .getElementsByTagName ('Pin' )
528+ for s in itemlist :
529+ m = re .match (pinregex , s .attributes ['Name' ].value )
530+ if m :
531+ pin = m .group (0 )[:2 ] + '_' + m .group (0 )[2 :] # pin formatted P<port>_<number>: PF_O
532+ name = s .attributes ['Name' ].value .strip () # full name: "PF0 / OSC_IN"
533+ if s .attributes ['Type' ].value == "I/O" :
534+ store_pin (pin , name )
535+ else :
536+ continue
537+ siglist = s .getElementsByTagName ('Signal' )
538+ for a in siglist :
539+ sig = a .attributes ['Name' ].value .strip ()
540+ if "ADC" in sig :
541+ store_adc (pin , name , sig )
542+ if all (["DAC" in sig , "_OUT" in sig ]):
543+ store_dac (pin , name , sig )
544+ if "I2C" in sig :
545+ store_i2c (pin , name , sig )
546+ if re .match ('^TIM' , sig ) is not None : #ignore HRTIM
547+ store_pwm (pin , name , sig )
548+ if re .match ('^(LPU|US|U)ART' , sig ) is not None :
549+ store_uart (pin , name , sig )
550+ if "SPI" in sig :
551+ store_spi (pin , name , sig )
552+ if "CAN" in sig :
553+ store_can (pin , name , sig )
554+ if "ETH" in sig :
555+ store_eth (pin , name , sig )
556+ if "QUADSPI" in sig :
557+ store_qspi (pin , name , sig )
558+
528559# main
529560cur_dir = os .getcwd ()
530561out_filename = 'PeripheralPins.c'
@@ -601,73 +632,35 @@ def clean_all_lists():
601632 quit ()
602633
603634for mcu_file in mcu_list :
604- print ("Generate %s for '%s'" % (out_filename , mcu_file ))
635+ print ("Generating %s for '%s'... " % (out_filename , mcu_file ))
605636 input_file_name = os .path .join (cubemxdir , mcu_file )
606637 out_path = os .path .join (cur_dir , 'Arduino' , os .path .splitext (mcu_file )[0 ])
607638 output_filename = os .path .join (out_path , out_filename )
608639 if not (os .path .isdir (out_path )):
609640 os .makedirs (out_path )
610641
611- #open input file
612- #print (" * * * Opening input file...")
613- xmldoc = minidom .parse (input_file_name )
614- itemlist = xmldoc .getElementsByTagName ('Pin' )
615642 #open output file
616643 if (os .path .isfile (output_filename )):
617- #print (" * * * * Requested %s file already exists and will be overwritten" % out_filename)
644+ #print (" * Requested %s file already exists and will be overwritten" % out_filename)
618645 os .remove (output_filename )
619-
620646 out_file = open (output_filename , 'w' )
621647
622- gpiofile = find_gpio_file (xmldoc )
648+ #open input file
649+ xml_mcu = parse (input_file_name )
650+ gpiofile = find_gpio_file ()
623651 if gpiofile == 'ERROR' :
624652 print ("Could not find GPIO file" )
625653 quit ()
654+ xml_gpio = parse (os .path .join (cubemxdirIP , 'GPIO-' + gpiofile + '_Modes.xml' ))
626655
627- xml = parse (os .path .join (cubemxdirIP , 'GPIO-' + gpiofile + '_Modes.xml' ))
628- print (" * * * Getting pins and Ips for the xml file..." )
629- pinregex = r'^(P[A-Z][0-9][0-5]?)'
630- for s in itemlist :
631- m = re .match (pinregex , s .attributes ['Name' ].value )
632- if m :
633- pin = m .group (0 )[:2 ] + '_' + m .group (0 )[2 :] # pin formatted P<port>_<number>: PF_O
634- name = s .attributes ['Name' ].value .strip () # full name: "PF0 / OSC_IN"
635- if s .attributes ['Type' ].value == "I/O" :
636- store_pin (pin , name )
637- else :
638- continue
639- siglist = s .getElementsByTagName ('Signal' )
640- for a in siglist :
641- sig = a .attributes ['Name' ].value .strip ()
642- if "ADC" in sig :
643- store_adc (pin , name , sig )
644- if all (["DAC" in sig , "_OUT" in sig ]):
645- store_dac (pin , name , sig )
646- if "I2C" in sig :
647- store_i2c (pin , name , sig )
648- if re .match ('^TIM' , sig ) is not None : #ignore HRTIM
649- store_pwm (pin , name , sig )
650- if re .match ('^(LPU|US|U)ART' , sig ) is not None :
651- store_uart (pin , name , sig )
652- if "SPI" in sig :
653- store_spi (pin , name , sig )
654- if "CAN" in sig :
655- store_can (pin , name , sig )
656- if "ETH" in sig :
657- store_eth (pin , name , sig )
658- if "QUADSPI" in sig :
659- store_qspi (pin , name , sig )
660-
661- #print (" * * * Sorting lists...")
656+ parse_pins ()
662657 sort_my_lists ()
663-
664- #print (" * * * Printing lists...")
665658 print_header ()
666659 print_all_lists ()
667660
668661 nb_pin = (len (io_list ))
669- print ("nb of I/O pins: %i" % nb_pin )
670- print (' \n * * * ' + mcu_file + ' OK' )
662+ print (" * I/O pins found : %i" % nb_pin )
663+ print ("done \n " )
671664 clean_all_lists ()
672665
673666 out_file .close ()
0 commit comments