3030eth_list = [] # 'PIN','name','ETH'
3131qspi_list = [] # 'PIN','name','QUADSPI'
3232syswkup_list = [] # 'PIN','name','SYSWKUP'
33+ usb_list = [] # 'PIN','name','USB'
34+ usb_otgfs_list = [] # 'PIN','name','USB'
35+ usb_otghs_list = [] # 'PIN','name','USB'
3336
3437
3538def find_gpio_file ():
@@ -239,6 +242,16 @@ def store_sys(pin, name, signal):
239242 syswkup_list .append ([pin , name , signal ])
240243
241244
245+ # function to store USB pins
246+ def store_usb (pin , name , signal ):
247+ if "OTG" not in signal :
248+ usb_list .append ([pin , name , signal ])
249+ if signal .startswith ("USB_OTG_FS" ):
250+ usb_otgfs_list .append ([pin , name , signal ])
251+ if signal .startswith ("USB_OTG_HS" ):
252+ usb_otghs_list .append ([pin , name , signal ])
253+
254+
242255def print_header ():
243256 s = """/*
244257 *******************************************************************************
@@ -287,88 +300,102 @@ def print_header():
287300
288301
289302def print_all_lists ():
290- if print_list_header ("ADC" , "ADC" , adclist , "ADC" ):
303+ if print_list_header ("ADC" , "ADC" , "ADC" , adclist ):
291304 print_adc ()
292- if print_list_header ("DAC" , "DAC" , daclist , "DAC" ):
305+ if print_list_header ("DAC" , "DAC" , "DAC" , daclist ):
293306 print_dac ()
294- if print_list_header ("I2C" , "I2C_SDA" , i2csda_list , "I2C" ):
307+ if print_list_header ("I2C" , "I2C_SDA" , "I2C" , i2csda_list , i2cscl_list ):
295308 print_i2c (i2csda_list )
296- if print_list_header ("" , "I2C_SCL" , i2cscl_list , "I2C" ):
297- print_i2c (i2cscl_list )
298- if print_list_header ("PWM" , "PWM" , pwm_list , "TIM" ):
309+ if print_list_header ("" , "I2C_SCL" , "I2C" , i2cscl_list ):
310+ print_i2c (i2cscl_list )
311+ if print_list_header ("PWM" , "PWM" , "TIM" , pwm_list ):
299312 print_pwm ()
300- if print_list_header ("SERIAL" , "UART_TX" , uarttx_list , "UART" ):
313+ if print_list_header (
314+ "SERIAL" ,
315+ "UART_TX" ,
316+ "UART" ,
317+ uarttx_list ,
318+ uartrx_list ,
319+ uartrts_list ,
320+ uartcts_list ,
321+ ):
301322 print_uart (uarttx_list )
302- if print_list_header ("" , "UART_RX" , uartrx_list , "UART" ):
303- print_uart (uartrx_list )
304- if print_list_header ("" , "UART_RTS" , uartrts_list , "UART" ):
305- print_uart (uartrts_list )
306- if print_list_header ("" , "UART_CTS" , uartcts_list , "UART" ):
307- print_uart (uartcts_list )
308- if print_list_header ("SPI" , "SPI_MOSI" , spimosi_list , "SPI" ):
323+ if print_list_header ("" , "UART_RX" , "UART" , uartrx_list ):
324+ print_uart (uartrx_list )
325+ if print_list_header ("" , "UART_RTS" , "UART" , uartrts_list ):
326+ print_uart (uartrts_list )
327+ if print_list_header ("" , "UART_CTS" , "UART" , uartcts_list ):
328+ print_uart (uartcts_list )
329+ if print_list_header (
330+ "SPI" , "SPI_MOSI" , "SPI" , spimosi_list , spimiso_list , spisclk_list , spissel_list
331+ ):
309332 print_spi (spimosi_list )
310- if print_list_header ("" , "SPI_MISO" , spimiso_list , "SPI" ):
311- print_spi (spimiso_list )
312- if print_list_header ("" , "SPI_SCLK" , spisclk_list , "SPI" ):
313- print_spi (spisclk_list )
314- if print_list_header ("" , "SPI_SSEL" , spissel_list , "SPI" ):
315- print_spi (spissel_list )
316- if print_list_header ("CAN" , "CAN_RD" , canrd_list , "CAN" ):
333+ if print_list_header ("" , "SPI_MISO" , "SPI" , spimiso_list ):
334+ print_spi (spimiso_list )
335+ if print_list_header ("" , "SPI_SCLK" , "SPI" , spisclk_list ):
336+ print_spi (spisclk_list )
337+ if print_list_header ("" , "SPI_SSEL" , "SPI" , spissel_list ):
338+ print_spi (spissel_list )
339+ if print_list_header ("CAN" , "CAN_RD" , "CAN" , canrd_list , cantd_list ):
317340 print_can (canrd_list )
318- if print_list_header ("" , "CAN_TD" , cantd_list , "CAN" ):
319- print_can (cantd_list )
320- if print_list_header ("ETHERNET" , "Ethernet" , eth_list , "ETH" ):
341+ if print_list_header ("" , "CAN_TD" , "CAN" , cantd_list ):
342+ print_can (cantd_list )
343+ if print_list_header ("ETHERNET" , "Ethernet" , "ETH" , eth_list ):
321344 print_eth ()
322- if print_list_header ("QUADSPI" , "QUADSPI" , qspi_list , "QSPI" ):
345+ if print_list_header ("QUADSPI" , "QUADSPI" , "QSPI" , qspi_list ):
323346 print_qspi ()
324- # Print specific PinNames
325- print_syswkup ()
326-
327-
328- def print_list_header (comment , name , l , switch ):
329- if len (l ) > 0 :
330- if comment :
347+ if print_list_header ("USB" , "USB" , "PCD" , usb_list , usb_otgfs_list , usb_otghs_list ):
348+ print_usb (usb_list )
349+ if print_list_header ("" , "USB_OTG_FS" , "PCD" , usb_otgfs_list ):
350+ print_usb (usb_otgfs_list )
351+ if print_list_header ("" , "USB_OTG_HS" , "PCD" , usb_otghs_list ):
352+ print_usb (usb_otghs_list )
353+ # Print specific PinNames in header file
354+ print_syswkup_h ()
355+ print_usb_h ()
356+
357+
358+ def print_list_header (feature , lname , switch , * argslst ):
359+ lenlst = 0
360+ for lst in argslst :
361+ lenlst += len (lst )
362+ if lenlst > 0 :
363+ # There is data for the feature
364+ if feature :
331365 s = (
332366 (
333367 """
334368//*** %s ***
335369"""
336370 )
337- % comment
371+ % feature
338372 )
339373 else :
340374 s = ""
341- s += (
342- (
343- """
344- #ifdef HAL_%s_MODULE_ENABLED
345- const PinMap PinMap_%s[] = {
346- """
347- )
348- % (switch , name )
349- )
350- else :
351- if comment :
352- s = (
375+ # Only for the first list
376+ if argslst [0 ]:
377+ s += (
353378 (
354379 """
355- //*** %s ***
380+ #ifdef HAL_%s_MODULE_ENABLED
381+ const PinMap PinMap_%s[] = {
356382"""
357383 )
358- % comment
384+ % ( switch , lname )
359385 )
360- else :
361- s = ""
362- s + = (
386+ else :
387+ # No data for the feature or the list
388+ s = (
363389 (
364390 """
365391//*** No %s ***
366392"""
367393 )
368- % name
394+ % ( feature if feature else lname )
369395 )
396+
370397 out_c_file .write (s )
371- return len ( l )
398+ return lenlst
372399
373400
374401def print_adc ():
@@ -425,7 +452,7 @@ def print_dac():
425452 )
426453 out_c_file .write (s1 )
427454 out_c_file .write (
428- """ {NC, NP, 0}
455+ """ {NC, NP, 0}
429456};
430457#endif
431458"""
@@ -476,7 +503,7 @@ def print_pwm():
476503 s1 += "STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, "
477504 r = result .split (" " )
478505 for af in r :
479- s2 = s1 + af + ", " + chan + neg + ")}, // " + p [2 ] + "\n "
506+ s2 = s1 + af + ", " + chan + neg + ")}, // " + p [2 ] + "\n "
480507 out_c_file .write (s2 )
481508 out_c_file .write (
482509 """ {NC, NP, 0}
@@ -570,7 +597,7 @@ def print_eth():
570597 if len (prev_s ) > 0 :
571598 out_c_file .write ("\n " )
572599 prev_s = s1
573- s1 += " // " + p [2 ]
600+ s1 += " // " + p [2 ]
574601 out_c_file .write (s1 )
575602 out_c_file .write (
576603 """\n {NC, NP, 0}
@@ -595,7 +622,7 @@ def print_qspi():
595622 if len (prev_s ) > 0 :
596623 out_c_file .write ("\n " )
597624 prev_s = s1
598- s1 += " // " + p [2 ]
625+ s1 += " // " + p [2 ]
599626 out_c_file .write (s1 )
600627 out_c_file .write (
601628 """\n {NC, NP, 0}
@@ -605,7 +632,7 @@ def print_qspi():
605632 )
606633
607634
608- def print_syswkup ():
635+ def print_syswkup_h ():
609636 out_h_file .write (" /* SYS_WKUP */\n " )
610637 # H7xx and F446 start from 0, inc by 1
611638 num = syswkup_list [0 ][2 ].replace ("SYS_WKUP" , "" )
@@ -640,6 +667,63 @@ def print_syswkup():
640667 out_h_file .write (s1 )
641668
642669
670+ def print_usb (lst ):
671+ use_hs_in_fs = False
672+ inst = "USB"
673+ if lst == usb_otgfs_list :
674+ inst = "USB_OTG_FS"
675+ elif lst == usb_otghs_list :
676+ inst = "USB_OTG_HS"
677+
678+ for p in lst :
679+ result = get_gpio_af_num (p [1 ], p [2 ])
680+ s1 = "%-10s" % (" {" + p [0 ] + "," )
681+ if lst == usb_otghs_list :
682+ if "ULPI" not in p [2 ] and not use_hs_in_fs :
683+ out_c_file .write ("#ifdef USE_USB_HS_IN_FS\n " )
684+ use_hs_in_fs = True
685+ if "ULPI" in p [2 ] and use_hs_in_fs :
686+ out_c_file .write ("#endif /* USE_USB_HS_IN_FS */\n " )
687+ use_hs_in_fs = False
688+
689+ # 2nd element is the USB_XXXX signal
690+ if not p [2 ].startswith ("USB_D" ) and "VBUS" not in p [2 ]:
691+ if "ID" not in p [2 ]:
692+ s1 += inst + ", STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, "
693+ else :
694+ # ID pin: AF_PP + PULLUP
695+ s1 += inst + ", STM_PIN_DATA(STM_MODE_AF_OD, GPIO_PULLUP, "
696+ else :
697+ # USB_DM/DP and VBUS: INPUT + NOPULL
698+ s1 += inst + ", STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, "
699+ if result == "NOTFOUND" :
700+ s1 += "0)},"
701+ else :
702+ r = result .split (" " )
703+ for af in r :
704+ s1 += af + ")},"
705+ s1 += " // " + p [2 ] + "\n "
706+ out_c_file .write (s1 )
707+ if lst :
708+ if use_hs_in_fs :
709+ out_c_file .write ("#endif /* USE_USB_HS_IN_FS */\n " )
710+ out_c_file .write (
711+ """ {NC, NP, 0}
712+ };
713+ #endif
714+ """
715+ )
716+
717+
718+ def print_usb_h ():
719+ if usb_list or usb_otgfs_list or usb_otghs_list :
720+ out_h_file .write (" /* USB */\n " )
721+ out_h_file .write ("#ifdef USBCON\n " )
722+ for p in usb_list + usb_otgfs_list + usb_otghs_list :
723+ out_h_file .write (" " + p [2 ] + " = " + p [0 ] + ",\n " )
724+ out_h_file .write ("#endif\n " )
725+
726+
643727tokenize = re .compile (r"(\d+)|(\D+)" ).findall
644728
645729
@@ -671,6 +755,9 @@ def sort_my_lists():
671755 eth_list .sort (key = natural_sortkey )
672756 qspi_list .sort (key = natural_sortkey )
673757 syswkup_list .sort (key = natural_sortkey2 )
758+ usb_list .sort (key = natural_sortkey )
759+ usb_otgfs_list .sort (key = natural_sortkey )
760+ usb_otghs_list .sort (key = natural_sortkey )
674761
675762
676763def clean_all_lists ():
@@ -693,6 +780,9 @@ def clean_all_lists():
693780 del eth_list [:]
694781 del qspi_list [:]
695782 del syswkup_list [:]
783+ del usb_list [:]
784+ del usb_otgfs_list [:]
785+ del usb_otghs_list [:]
696786
697787
698788def parse_pins ():
@@ -733,6 +823,8 @@ def parse_pins():
733823 store_qspi (pin , name , sig )
734824 if "SYS_" in sig :
735825 store_sys (pin , name , sig )
826+ if "USB" in sig :
827+ store_usb (pin , name , sig )
736828
737829
738830# main
@@ -825,9 +917,7 @@ def parse_pins():
825917 if not (os .path .isfile (os .path .join (cubemxdir , args .mcu ))):
826918 print ("\n " + args .mcu + " file not found" )
827919 print ("\n Check in " + cubemxdir + " the correct name of this file" )
828- print (
829- "\n You may use double quotes for file containing special characters"
830- )
920+ print ("\n You may use double quotes for file containing special characters" )
831921 quit ()
832922 mcu_list .append (args .mcu )
833923else :
0 commit comments