30
30
eth_list = [] # 'PIN','name','ETH'
31
31
qspi_list = [] # 'PIN','name','QUADSPI'
32
32
syswkup_list = [] # 'PIN','name','SYSWKUP'
33
+ usb_list = [] # 'PIN','name','USB'
34
+ usb_otgfs_list = [] # 'PIN','name','USB'
35
+ usb_otghs_list = [] # 'PIN','name','USB'
33
36
34
37
35
38
def find_gpio_file ():
@@ -239,6 +242,16 @@ def store_sys(pin, name, signal):
239
242
syswkup_list .append ([pin , name , signal ])
240
243
241
244
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
+
242
255
def print_header ():
243
256
s = """/*
244
257
*******************************************************************************
@@ -287,88 +300,102 @@ def print_header():
287
300
288
301
289
302
def print_all_lists ():
290
- if print_list_header ("ADC" , "ADC" , adclist , "ADC" ):
303
+ if print_list_header ("ADC" , "ADC" , "ADC" , adclist ):
291
304
print_adc ()
292
- if print_list_header ("DAC" , "DAC" , daclist , "DAC" ):
305
+ if print_list_header ("DAC" , "DAC" , "DAC" , daclist ):
293
306
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 ):
295
308
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 ):
299
312
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
+ ):
301
322
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
+ ):
309
332
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 ):
317
340
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 ):
321
344
print_eth ()
322
- if print_list_header ("QUADSPI" , "QUADSPI" , qspi_list , "QSPI" ):
345
+ if print_list_header ("QUADSPI" , "QUADSPI" , "QSPI" , qspi_list ):
323
346
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 :
331
365
s = (
332
366
(
333
367
"""
334
368
//*** %s ***
335
369
"""
336
370
)
337
- % comment
371
+ % feature
338
372
)
339
373
else :
340
374
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 += (
353
378
(
354
379
"""
355
- //*** %s ***
380
+ #ifdef HAL_%s_MODULE_ENABLED
381
+ const PinMap PinMap_%s[] = {
356
382
"""
357
383
)
358
- % comment
384
+ % ( switch , lname )
359
385
)
360
- else :
361
- s = ""
362
- s + = (
386
+ else :
387
+ # No data for the feature or the list
388
+ s = (
363
389
(
364
390
"""
365
391
//*** No %s ***
366
392
"""
367
393
)
368
- % name
394
+ % ( feature if feature else lname )
369
395
)
396
+
370
397
out_c_file .write (s )
371
- return len ( l )
398
+ return lenlst
372
399
373
400
374
401
def print_adc ():
@@ -425,7 +452,7 @@ def print_dac():
425
452
)
426
453
out_c_file .write (s1 )
427
454
out_c_file .write (
428
- """ {NC, NP, 0}
455
+ """ {NC, NP, 0}
429
456
};
430
457
#endif
431
458
"""
@@ -476,7 +503,7 @@ def print_pwm():
476
503
s1 += "STM_PIN_DATA_EXT(STM_MODE_AF_PP, GPIO_PULLUP, "
477
504
r = result .split (" " )
478
505
for af in r :
479
- s2 = s1 + af + ", " + chan + neg + ")}, // " + p [2 ] + "\n "
506
+ s2 = s1 + af + ", " + chan + neg + ")}, // " + p [2 ] + "\n "
480
507
out_c_file .write (s2 )
481
508
out_c_file .write (
482
509
""" {NC, NP, 0}
@@ -570,7 +597,7 @@ def print_eth():
570
597
if len (prev_s ) > 0 :
571
598
out_c_file .write ("\n " )
572
599
prev_s = s1
573
- s1 += " // " + p [2 ]
600
+ s1 += " // " + p [2 ]
574
601
out_c_file .write (s1 )
575
602
out_c_file .write (
576
603
"""\n {NC, NP, 0}
@@ -595,7 +622,7 @@ def print_qspi():
595
622
if len (prev_s ) > 0 :
596
623
out_c_file .write ("\n " )
597
624
prev_s = s1
598
- s1 += " // " + p [2 ]
625
+ s1 += " // " + p [2 ]
599
626
out_c_file .write (s1 )
600
627
out_c_file .write (
601
628
"""\n {NC, NP, 0}
@@ -605,7 +632,7 @@ def print_qspi():
605
632
)
606
633
607
634
608
- def print_syswkup ():
635
+ def print_syswkup_h ():
609
636
out_h_file .write (" /* SYS_WKUP */\n " )
610
637
# H7xx and F446 start from 0, inc by 1
611
638
num = syswkup_list [0 ][2 ].replace ("SYS_WKUP" , "" )
@@ -640,6 +667,63 @@ def print_syswkup():
640
667
out_h_file .write (s1 )
641
668
642
669
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
+
643
727
tokenize = re .compile (r"(\d+)|(\D+)" ).findall
644
728
645
729
@@ -671,6 +755,9 @@ def sort_my_lists():
671
755
eth_list .sort (key = natural_sortkey )
672
756
qspi_list .sort (key = natural_sortkey )
673
757
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 )
674
761
675
762
676
763
def clean_all_lists ():
@@ -693,6 +780,9 @@ def clean_all_lists():
693
780
del eth_list [:]
694
781
del qspi_list [:]
695
782
del syswkup_list [:]
783
+ del usb_list [:]
784
+ del usb_otgfs_list [:]
785
+ del usb_otghs_list [:]
696
786
697
787
698
788
def parse_pins ():
@@ -733,6 +823,8 @@ def parse_pins():
733
823
store_qspi (pin , name , sig )
734
824
if "SYS_" in sig :
735
825
store_sys (pin , name , sig )
826
+ if "USB" in sig :
827
+ store_usb (pin , name , sig )
736
828
737
829
738
830
# main
@@ -825,9 +917,7 @@ def parse_pins():
825
917
if not (os .path .isfile (os .path .join (cubemxdir , args .mcu ))):
826
918
print ("\n " + args .mcu + " file not found" )
827
919
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" )
831
921
quit ()
832
922
mcu_list .append (args .mcu )
833
923
else :
0 commit comments