diff --git a/targets/TARGET_STM/README.md b/targets/TARGET_STM/README.md index 4f0aa31f8d..bd8f4c40eb 100644 --- a/targets/TARGET_STM/README.md +++ b/targets/TARGET_STM/README.md @@ -163,13 +163,15 @@ STM32CubeMX has to be installed first. Path has to be specified in the `cube_pat ``` $ python tools/targets/STM32_gen_PeripheralPins.py -h -Script version 1.9 -usage: STM32_gen_PeripheralPins.py [-h] [-l | -b | -m xml | -t HW | -c CUSTOM] +Script version 1.17 +usage: STM32_gen_PeripheralPins.py [-h] (-l | -b | -m xml | -t HW | -c CUSTOM) Script will generate PeripheralPins.c thanks to the xml files description available in STM32CubeMX directory defined in 'cube_path.json': C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeMX +More information in targets/TARGET_STM/README.md + optional arguments: -h, --help show this help message and exit -l, --list list available mcu xml files description in STM32CubeMX @@ -182,6 +184,7 @@ optional arguments: specify a custom board .ioc file description to use (use double quotes). Once generated, you have to check and comment pins that can not be used (specific HW, internal ADC channels, remove PWM using us ticker timer, ...) + ``` How to generate files for a custom boards based on a STM32F427 MCU: @@ -195,13 +198,19 @@ STM32F427Z(G-I)Tx.xml $ python tools/targets/STM32_gen_PeripheralPins.py -m "STM32F427V(G-I)Tx.xml" -Script version 1.9 -CubeMX DB version DB.5.0.60 +Script version 1.17 +CubeMX DB version DB.6.0.0 - * Output directory: C:\github\mbed\STM32F427V(G-I)Tx - * Generating PeripheralPins.c and PinNames.h with 'STM32F427V(G-I)Tx.xml' + * Output directory: targets_custom\TARGET_STM\TARGET_STM32F4\TARGET_STM32F427xG\TARGET_STM32F427VGT + * Generating PeripheralPins.c and PinNames.h with 'C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeMX\db\mcu\STM32F427V(G-I)Tx.xml' * GPIO file: C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeMX\db\mcu\IP\GPIO-STM32F427_gpio_v1_0_Modes.xml - * I/O pins found: 82 connected: 0 + * I/O pins found: 135 connected: 0 + + * Output directory: targets_custom\TARGET_STM\TARGET_STM32F4\TARGET_STM32F427xI\TARGET_STM32F427VIT + * Generating PeripheralPins.c and PinNames.h with 'C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeMX\db\mcu\STM32F427V(G-I)Tx.xml' + * GPIO file: C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeMX\db\mcu\IP\GPIO-STM32F427_gpio_v1_0_Modes.xml + * I/O pins found: 135 connected: 0 + ``` ### Use of custom_targets.json @@ -213,9 +222,9 @@ Example with a board based on STM32F103C8 (like BluePill): ``` $ python tools/targets/STM32_gen_PeripheralPins.py -m "STM32F103C(8-B)Tx.xml" -// PeripheralPins.c and PinNames.h template files are created in STM32H745ZITx directory +// PeripheralPins.c and PinNames.h template files are created in targets_custom/TARGET_STM/TARGET_STM32F1/TARGET_STM32F103x8/TARGET_STM32F103C8T directory -$ mv STM32F103C\(8-B\)Tx/ TARGET_BLUEPILL_F103C8 +$ mv TARGET_STM32F103C8T TARGET_BLUEPILL_F103C8 // Edit PeripheralPins.c and PinNames.h to match your board configuration // Create a custom_targets.json with: @@ -241,9 +250,9 @@ Example with a board based on STM32H745ZI ``` $ python tools/targets/STM32_gen_PeripheralPins.py -m "STM32H745ZITx.xml" -// PeripheralPins.c and PinNames.h template files are created in STM32H745ZITx directory +// PeripheralPins.c and PinNames.h template files are created in targets_custom/TARGET_STM/TARGET_STM32H7/TARGET_STM32H745xI/TARGET_STM32H745ZIT directory -$ mv STM32H745ZITx TARGET_H745ZI_BOARD +$ mv TARGET_STM32H745ZIT TARGET_H745ZI_BOARD // Edit PeripheralPins.c and PinNames.h to match your board configuration // Create a custom_targets.json with: diff --git a/tools/targets/STM32_gen_PeripheralPins.py b/tools/targets/STM32_gen_PeripheralPins.py index d077c0b733..9fa38c440d 100644 --- a/tools/targets/STM32_gen_PeripheralPins.py +++ b/tools/targets/STM32_gen_PeripheralPins.py @@ -26,10 +26,9 @@ import textwrap from xml.dom.minidom import parse, Node from argparse import RawTextHelpFormatter -GENPINMAP_VERSION = "1.12" +GENPINMAP_VERSION = "1.17" ADD_DEVICE_IF = 0 -ADD_QSPI_FEATURE = 1 DEBUG_PRINT = 0 mcu_file="" @@ -51,46 +50,35 @@ spisclk_list = [] #'PIN','name','SPISCLK' cantd_list = [] #'PIN','name','CANTD' canrd_list = [] #'PIN','name','CANRD' eth_list = [] #'PIN','name','ETH' -quadspidata0_list = [] #'PIN','name','QUADSPIDATA0' -quadspidata1_list = [] #'PIN','name','QUADSPIDATA1' -quadspidata2_list = [] #'PIN','name','QUADSPIDATA2' -quadspidata3_list = [] #'PIN','name','QUADSPIDATA3' +quadspidata0_list = [] #'PIN','name','QUADSPIDATA0' +quadspidata1_list = [] #'PIN','name','QUADSPIDATA1' +quadspidata2_list = [] #'PIN','name','QUADSPIDATA2' +quadspidata3_list = [] #'PIN','name','QUADSPIDATA3' quadspisclk_list = [] #'PIN','name','QUADSPISCLK' quadspissel_list = [] #'PIN','name','QUADSPISSEL' +octospidata0_list = [] #'PIN','name','OCTOSPIDATA0' +octospidata1_list = [] #'PIN','name','OCTOSPIDATA1' +octospidata2_list = [] #'PIN','name','OCTOSPIDATA2' +octospidata3_list = [] #'PIN','name','OCTOSPIDATA3' +octospidata4_list = [] #'PIN','name','OCTOSPIDATA4' +octospidata5_list = [] #'PIN','name','OCTOSPIDATA5' +octospidata6_list = [] #'PIN','name','OCTOSPIDATA6' +octospidata7_list = [] #'PIN','name','OCTOSPIDATA7' +octospidqs_list = [] #'PIN','name','OCTOSPIDQS' +octospisclk_list = [] #'PIN','name','OCTOSPISCLK' +octospissel_list = [] #'PIN','name','OCTOSPISSEL' usb_list = [] # 'PIN','name','USB' usb_otgfs_list = [] # 'PIN','name','USB' usb_otghs_list = [] # 'PIN','name','USB' osc_list = [] #'PIN','name','OSC' sys_list = [] #'PIN','name','SYS' -STDIO_list = ["", ""] # TX , RX +STDIO_list = ["Pxx", "Pxx"] # TX , RX LED_list = [] BUTTON_list = [] - -TIM_MST_LIST = { -# Timer used for us ticker is hardcoded in this script -# Default is TIM5 -"F0":"TIM2", -"F030":"TIM1", # overwrite previous default F0 -"F05":"TIM1", # overwrite previous default F0 -"F070":"TIM1", # overwrite previous default F0 -"F1":"TIM4", -"F3":"TIM2", -"L0":"TIM21", -"G0":"TIM2", -"G070":"TIM3", -"G43":"TIM2", -"G471":"TIM2", -"G49":"TIM2", -"G4A":"TIM2", -"L10":"TIM2", -"L152R(6-8-B)":"TIM2", -"L41":"TIM2", -"L43":"TIM2", -"L45":"TIM2", - -"WB":"TIM16" -} +DUAL_PAD = False +MCU_USERNAME= "" +TIM_MST = "" TIM_DUALCORE_LIST = { # Timer used for us ticker is hardcoded in this script "H745":"TIM2", @@ -119,9 +107,53 @@ def find_gpio_file(): return res +def find_tim_mst(): + global TIM_MST + + # Let's list first the available timers + tim_list = [] + itemlist = xml_mcu.getElementsByTagName("IP") + for s in itemlist: + a = s.attributes["Name"].value + if "TIM" in a: + tim_list.append(s.attributes["InstanceName"].value) + + # Then choose 1 timer for us ticker + TIM_MST = "" + if TARGET_FAMILY == "STM32F0": + search_order = ["TIM5", "TIM2", "TIM1"] + elif TARGET_FAMILY == "STM32F1": + search_order = ["TIM5", "TIM4", "TIM2"] + elif TARGET_FAMILY == "STM32F3": + search_order = ["TIM5", "TIM2"] + elif TARGET_FAMILY == "STM32G0": + search_order = ["TIM5", "TIM2", "TIM3"] + elif TARGET_FAMILY == "STM32G4": + search_order = ["TIM5", "TIM2"] + elif TARGET_FAMILY == "STM32L0": + search_order = ["TIM5", "TIM21"] + elif TARGET_FAMILY == "STM32L1": + search_order = ["TIM5", "TIM2"] + elif TARGET_FAMILY == "STM32L4": + search_order = ["TIM5", "TIM2"] + elif TARGET_FAMILY == "STM32WB": + search_order = ["TIM5", "TIM16"] + else: + search_order = ["TIM5"] + for EachTimer in search_order: + if EachTimer in tim_list: + TIM_MST = EachTimer + break + if TIM_MST == "": + print("!!! error TIM_MST not found") + else: + print_debug("TIM_MST=%s" % TIM_MST) + def get_gpio_af_num(pintofind, iptofind): + pintofind = pintofind.split("-")[0].split(" ")[0] # to avoid for ex "PC14-OSC32_IN", "PB4 (NJTRST)" + if "STM32F10" in mcu_file: - return get_gpio_af_numF1(pintofind, iptofind) + return get_gpio_af_num_stm32f1(pintofind, iptofind) i = 0 mygpioaf = "" for n in xml_gpio.documentElement.childNodes: @@ -130,18 +162,18 @@ def get_gpio_af_num(pintofind, iptofind): if n.nodeType == Node.ELEMENT_NODE: for firstlevel in n.attributes.items(): # if 'PB7' in firstlevel: - if pintofind == firstlevel[1]: + if pintofind == firstlevel[1].split("-")[0].split(" ")[0]: # to avoid for ex "PC14-OSC32_IN", "PB4 (NJTRST)" # n = pin node found - for m in n.childNodes: + for each_child_node in n.childNodes: j += 1 k = 0 - if m.nodeType == Node.ELEMENT_NODE: - for secondlevel in m.attributes.items(): + if each_child_node.nodeType == Node.ELEMENT_NODE: + for secondlevel in each_child_node.attributes.items(): k += 1 # if 'I2C1_SDA' in secondlevel: - if iptofind in secondlevel: + if iptofind in secondlevel[1].replace("_CTS_NSS", "_CTS"): # to avoid "USART2_CTS_NSS" # m = IP node found - for p in m.childNodes: + for p in each_child_node.childNodes: if p.nodeType == Node.ELEMENT_NODE: # p node of 'Specific parameter' for myc in p.childNodes: @@ -157,27 +189,27 @@ def get_gpio_af_num(pintofind, iptofind): return mygpioaf -def get_gpio_af_numF1(pintofind, iptofind): +def get_gpio_af_num_stm32f1(pintofind, iptofind): print_debug('pin to find ' + pintofind + ' ip to find ' + iptofind) i = 0 mygpioaf = "" - for n in xml_gpio.documentElement.childNodes: + for gpio_child_node in xml_gpio.documentElement.childNodes: i += 1 j = 0 - if n.nodeType == Node.ELEMENT_NODE: - for firstlevel in n.attributes.items(): + if gpio_child_node.nodeType == Node.ELEMENT_NODE: + for firstlevel in gpio_child_node.attributes.items(): if pintofind == firstlevel[1]: - # n = pin node found - for m in n.childNodes: + # gpio_child_node = pin node found + for each_child_node in gpio_child_node.childNodes: j += 1 k = 0 - if m.nodeType == Node.ELEMENT_NODE: - for secondlevel in m.attributes.items(): + if each_child_node.nodeType == Node.ELEMENT_NODE: + for secondlevel in each_child_node.attributes.items(): k += 1 # if 'I2C1_SDA' in secondlevel: if iptofind in secondlevel: # m = IP node found - for p in m.childNodes: + for p in each_child_node.childNodes: # p node 'RemapBlock' if ( p.nodeType == Node.ELEMENT_NODE @@ -223,8 +255,7 @@ def get_gpio_af_numF1(pintofind, iptofind): def store_pin(pin, name): # store pin I/O - p = [pin, name] - io_list.append(p) + io_list.append([pin, name]) # function to store ADC list @@ -305,6 +336,32 @@ def store_qspi(pin, name, signal): quadspissel_list.append([pin, name, signal]) +# function to store OSPI pins +def store_ospi(pin, name, signal): + if "_IO0" in signal: + octospidata0_list.append([pin, name, signal]) + if "_IO1" in signal: + octospidata1_list.append([pin, name, signal]) + if "_IO2" in signal: + octospidata2_list.append([pin, name, signal]) + if "_IO3" in signal: + octospidata3_list.append([pin, name, signal]) + if "_IO4" in signal: + octospidata4_list.append([pin, name, signal]) + if "_IO5" in signal: + octospidata5_list.append([pin, name, signal]) + if "_IO6" in signal: + octospidata6_list.append([pin, name, signal]) + if "_IO7" in signal: + octospidata7_list.append([pin, name, signal]) + if "_CLK" in signal: + octospisclk_list.append([pin, name, signal]) + if "_NCS" in signal: + octospissel_list.append([pin, name, signal]) + if "_DQS" in signal: + octospidqs_list.append([pin, name, signal]) + + # function to store USB pins def store_usb(pin, name, signal): if "OTG" not in signal: @@ -326,9 +383,9 @@ def store_sys(pin, name, signal): def print_header(): - DATE_YEAR = datetime.datetime.now().year + date_year = datetime.datetime.now().year - s = ("""/* mbed Microcontroller Library + line_to_write = ("""/* mbed Microcontroller Library * SPDX-License-Identifier: BSD-3-Clause ****************************************************************************** * @@ -366,10 +423,10 @@ def print_header(): // //============================================================================== -""" % (DATE_YEAR, os.path.basename(input_file_name))) - out_c_file.write( s ) +""" % (date_year, os.path.basename(input_file_name))) + out_c_file.write(line_to_write) - s = ("""/* mbed Microcontroller Library + line_to_write = ("""/* mbed Microcontroller Library * SPDX-License-Identifier: BSD-3-Clause ****************************************************************************** * @@ -395,22 +452,32 @@ def print_header(): #ifdef __cplusplus extern "C" { #endif +""" % (date_year, os.path.basename(input_file_name))) + out_h_file.write(line_to_write) + if DUAL_PAD: + line_to_write = (""" +#define ALTC 0xF00 +""") + out_h_file.write(line_to_write) + + line_to_write = (""" typedef enum { ALT0 = 0x100, ALT1 = 0x200, ALT2 = 0x300, - ALT3 = 0x400 + ALT3 = 0x400, + ALT4 = 0x500 } ALTx; typedef enum { -""" % (DATE_YEAR, os.path.basename(input_file_name))) - out_h_file.write( s ) +""") + out_h_file.write(line_to_write) def print_footer(): - s = (""" + line_to_write = (""" // Not connected NC = (int)0xFFFFFFFF } PinName; @@ -421,7 +488,7 @@ def print_footer(): #endif """) - out_h_file.write(s) + out_h_file.write(line_to_write) def print_all_lists(): @@ -455,26 +522,47 @@ def print_all_lists(): print_can(canrd_list) if print_list_header("", "CAN_TD", cantd_list, "CAN"): print_can(cantd_list) - if ADD_QSPI_FEATURE: - if print_list_header("QUADSPI", "QSPI_DATA0", quadspidata0_list, "QSPI"): - print_qspi(quadspidata0_list) - if print_list_header("", "QSPI_DATA1", quadspidata1_list, "QSPI"): - print_qspi(quadspidata1_list) - if print_list_header("", "QSPI_DATA2", quadspidata2_list, "QSPI"): - print_qspi(quadspidata2_list) - if print_list_header("", "QSPI_DATA3", quadspidata3_list, "QSPI"): - print_qspi(quadspidata3_list) - if print_list_header("", "QSPI_SCLK", quadspisclk_list, "QSPI"): - print_qspi(quadspisclk_list) - if print_list_header("", "QSPI_SSEL", quadspissel_list, "QSPI"): - print_qspi(quadspissel_list) + if print_list_header("QUADSPI", "QSPI_DATA0", quadspidata0_list, "QSPI"): + print_qspi(quadspidata0_list) + if print_list_header("", "QSPI_DATA1", quadspidata1_list, "QSPI"): + print_qspi(quadspidata1_list) + if print_list_header("", "QSPI_DATA2", quadspidata2_list, "QSPI"): + print_qspi(quadspidata2_list) + if print_list_header("", "QSPI_DATA3", quadspidata3_list, "QSPI"): + print_qspi(quadspidata3_list) + if print_list_header("", "QSPI_SCLK", quadspisclk_list, "QSPI"): + print_qspi(quadspisclk_list) + if print_list_header("", "QSPI_SSEL", quadspissel_list, "QSPI"): + print_qspi(quadspissel_list) + if print_list_header("OCTOSPI", "OSPI_DATA0", octospidata0_list, "OCTO"): + print_ospi(octospidata0_list) + if print_list_header("", "OSPI_DATA1", octospidata1_list, "OCTO"): + print_ospi(octospidata1_list) + if print_list_header("", "OSPI_DATA2", octospidata2_list, "OCTO"): + print_ospi(octospidata2_list) + if print_list_header("", "OSPI_DATA3", octospidata3_list, "OCTO"): + print_ospi(octospidata3_list) + if print_list_header("", "OSPI_DATA4", octospidata2_list, "OCTO"): + print_ospi(octospidata4_list) + if print_list_header("", "OSPI_DATA5", octospidata3_list, "OCTO"): + print_ospi(octospidata5_list) + if print_list_header("", "OSPI_DATA6", octospidata2_list, "OCTO"): + print_ospi(octospidata6_list) + if print_list_header("", "OSPI_DATA7", octospidata3_list, "OCTO"): + print_ospi(octospidata7_list) + if print_list_header("", "OSPI_DQS", octospidqs_list, "OCTO"): + print_ospi(octospidqs_list) + if print_list_header("", "OSPI_SCLK", octospisclk_list, "OCTO"): + print_ospi(octospisclk_list) + if print_list_header("", "OSPI_SSEL", octospissel_list, "OCTO"): + print_ospi(octospissel_list) if print_list_header("USBDEVICE", "USB_FS", usb_list, "USBDEVICE"): print_usb(usb_list) if print_list_header("USBDEVICE", "USB_FS", usb_otgfs_list, "USBDEVICE"): print_usb(usb_otgfs_list) if print_list_header("USBDEVICE", "USB_HS", usb_otghs_list, "USBDEVICE"): print_usb(usb_otghs_list) - print_PinList(io_list) + print_pin_list(io_list) print_h_file(usb_list, "USB") print_h_file(usb_otgfs_list, "USB FS") print_h_file(usb_otghs_list, "USB HS") @@ -484,37 +572,30 @@ def print_all_lists(): def print_list_header(comment, name, l, switch): - s = "" + line_to_write = "" if len(l)>0: if comment: - s += "\n//*** %s ***\n" % comment + line_to_write += "\n//*** %s ***\n" % comment - s += "\n" + line_to_write += "\n" if name == "PWM": - DefaultTimerCore1 = "TIM5" - for EachTarget in TIM_MST_LIST: - if EachTarget in mcu_file: - DefaultTimerCore1 = TIM_MST_LIST[EachTarget] - s += "// %s cannot be used because already used by the us_ticker\n" % DefaultTimerCore1 + line_to_write += "// %s cannot be used because already used by the us_ticker\n" % TIM_MST + line_to_write += "// (update us_ticker_data.h file if another timer is chosen)\n" - DefaultTimerCore2 = "" - for EachTarget in TIM_DUALCORE_LIST: - if EachTarget in mcu_file: - DefaultTimerCore2 = TIM_DUALCORE_LIST[EachTarget] - if DefaultTimerCore2 != "": - s += "// %s cannot be used because already used by the us_ticker (DUAL_CORE)\n" % DefaultTimerCore2 + default_timer_core2 = "" + for each_target in TIM_DUALCORE_LIST: + if each_target in mcu_file: + default_timer_core2 = TIM_DUALCORE_LIST[each_target] + if default_timer_core2 != "": + line_to_write += "// %s cannot be used because already used by the us_ticker (DUAL_CORE)\n" % default_timer_core2 if ADD_DEVICE_IF: - s += "#if DEVICE_%s\n" % switch + line_to_write += "#if DEVICE_%s\n" % switch - s += "MBED_WEAK const PinMap PinMap_%s[] = {\n" % name + line_to_write += "MBED_WEAK const PinMap PinMap_%s[] = {\n" % name - # else: - # if comment: - # s += "\n//*** No %s ***\n" % comment - - out_c_file.write(s) + out_c_file.write(line_to_write) return len(l) @@ -529,73 +610,73 @@ def print_adc(): prev_p = '' alt_index = 0 - for p in adclist: - if "IN" in p[2]: - CommentedLine = " " - if p[1] in PinLabel.keys(): - if "STDIO_UART" in PinLabel[p[1]]: - CommentedLine = "//" - if "RCC_OSC" in PinLabel[p[1]]: - CommentedLine = "//" - if CommentedLine != "//": - if p[0] == prev_p: + for parsed_pin in adclist: + if "IN" in parsed_pin[2]: + commented_line = " " + if parsed_pin[1] in PinLabel: + if "STDIO_UART" in PinLabel[parsed_pin[1]]: + commented_line = "//" + if "RCC_OSC" in PinLabel[parsed_pin[1]]: + commented_line = "//" + if commented_line != "//": + if parsed_pin[0] == prev_p: if "STM32F1" in mcu_file: continue else: - prev_p = p[0] - p[0] += '_ALT%d' % alt_index - store_pin(p[0], p[0]) + prev_p = parsed_pin[0] + parsed_pin[0] += '_ALT%d' % alt_index + store_pin(parsed_pin[0], parsed_pin[0]) alt_index += 1 else: - prev_p = p[0] + prev_p = parsed_pin[0] alt_index = 0 - s1 = "%-17s" % (CommentedLine + " {" + p[0] + ',') - a = p[2].split('_') + line_to_write = "%-17s" % (commented_line + " {" + parsed_pin[0] + ',') + a = parsed_pin[2].split('_') inst = a[0].replace("ADC", "") if len(inst) == 0: inst = '1' #single ADC for this product - s1 += "%-7s" % ('ADC_' + inst + ',') + line_to_write += "%-7s" % ('ADC_' + inst + ',') chan = re.sub('IN[N|P]?', '', a[1]) - s1 += s_pin_data + chan - s1 += ', 0)}, // ' + p[2] - if p[1] in PinLabel.keys(): - s1 += ' // Connected to ' + PinLabel[p[1]] - s1 += '\n' - out_c_file.write(s1) + line_to_write += s_pin_data + chan + line_to_write += ', 0)}, // ' + parsed_pin[2] + if parsed_pin[1] in PinLabel: + line_to_write += ' // Connected to ' + PinLabel[parsed_pin[1]] + line_to_write += '\n' + out_c_file.write(line_to_write) out_c_file.write( """ {NC, NC, 0} }; // !!! SECTION TO BE CHECKED WITH DEVICE REFERENCE MANUAL -// MBED_WEAK const PinMap PinMap_ADC_Internal[] = { +MBED_WEAK const PinMap PinMap_ADC_Internal[] = { // {ADC_TEMP, ADC_1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 16, 0)}, // {ADC_VREF, ADC_1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 17, 0)}, // {ADC_VBAT, ADC_1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 18, 0)}, -// {NC, NC, 0} -// }; + {NC, NC, 0} +}; """) if ADD_DEVICE_IF: out_c_file.write( "#endif\n" ) def print_dac(): - for p in daclist: - CommentedLine = " " - if p[1] in PinLabel.keys(): - if "STDIO_UART" in PinLabel[p[1]]: - CommentedLine = "//" - if "RCC_OSC" in PinLabel[p[1]]: - CommentedLine = "//" - s1 = "%-17s" % (CommentedLine + " {" + p[0] + ',') - #p[2] : DAC_OUT1 / DAC1_OUT1 - a = p[2].split('_') + for parsed_pin in daclist: + commented_line = " " + if parsed_pin[1] in PinLabel: + if "STDIO_UART" in PinLabel[parsed_pin[1]]: + commented_line = "//" + if "RCC_OSC" in PinLabel[parsed_pin[1]]: + commented_line = "//" + s1 = "%-17s" % (commented_line + " {" + parsed_pin[0] + ',') + #parsed_pin[2] : DAC_OUT1 / DAC1_OUT1 + a = parsed_pin[2].split('_') inst = a[0].replace("DAC", "") b = a[1].replace("OUT", "") if len(inst) == 0: inst = '1' # single DAC for this product s1 += "%-7s" % ('DAC_' + inst + ',') - s1 += 'STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, ' + b + ', 0)}, // ' + p[2] - if p[1] in PinLabel.keys(): - s1 += ' // Connected to ' + PinLabel[p[1]] + s1 += 'STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, ' + b + ', 0)}, // ' + parsed_pin[2] + if parsed_pin[1] in PinLabel: + s1 += ' // Connected to ' + PinLabel[parsed_pin[1]] s1 += '\n' out_c_file.write(s1) out_c_file.write( """ {NC, NC, 0} @@ -608,37 +689,37 @@ def print_dac(): def print_i2c(l): prev_p = '' alt_index = 0 - for p in l: - result = get_gpio_af_num(p[1], p[2]) - CommentedLine = " " - if p[1] in PinLabel.keys(): - if "STDIO_UART" in PinLabel[p[1]]: - CommentedLine = "//" - if "RCC_OSC" in PinLabel[p[1]]: - CommentedLine = "//" - if CommentedLine != "//": - if p[0] == prev_p: - prev_p = p[0] - p[0] += '_ALT%d' % alt_index - store_pin(p[0], p[0]) + for parsed_pin in l: + result = get_gpio_af_num(parsed_pin[1], parsed_pin[2]) + commented_line = " " + if parsed_pin[1] in PinLabel: + if "STDIO_UART" in PinLabel[parsed_pin[1]]: + commented_line = "//" + if "RCC_OSC" in PinLabel[parsed_pin[1]]: + commented_line = "//" + if commented_line != "//": + if parsed_pin[0] == prev_p: + prev_p = parsed_pin[0] + parsed_pin[0] += '_ALT%d' % alt_index + store_pin(parsed_pin[0], parsed_pin[0]) alt_index += 1 else: - prev_p = p[0] + prev_p = parsed_pin[0] alt_index = 0 - s1 = "%-17s" % (CommentedLine + " {" + p[0] + ',') - # p[2] : I2C1_SDA / FMPI2C1_SDA - if "FMP" in p[2]: - inst = p[2].split('_')[0].replace("FMPI2C", "") + s1 = "%-17s" % (commented_line + " {" + parsed_pin[0] + ',') + # parsed_pin[2] : I2C1_SDA / FMPI2C1_SDA + if "FMP" in parsed_pin[2]: + inst = parsed_pin[2].split('_')[0].replace("FMPI2C", "") s1 += "%-10s" % ('FMPI2C_' + inst + ',') else: - inst = p[2].split('_')[0].replace("I2C", "") + inst = parsed_pin[2].split('_')[0].replace("I2C", "") s1 += "%-7s" % ('I2C_' + inst + ',') s1 += 'STM_PIN_DATA(STM_MODE_AF_OD, GPIO_NOPULL, ' r = result.split(' ') for af in r: s2 = s1 + af + ')},' - if p[1] in PinLabel.keys(): - s2 += ' // Connected to ' + PinLabel[p[1]] + if parsed_pin[1] in PinLabel: + s2 += ' // Connected to ' + PinLabel[parsed_pin[1]] s2 += '\n' out_c_file.write(s2) out_c_file.write( """ {NC, NC, 0} @@ -652,42 +733,35 @@ def print_pwm(): prev_p = '' alt_index = 0 - TIM_MST = "TIM5" - OneLineCommented = False - for EachTarget in TIM_MST_LIST: - if EachTarget in mcu_file: - TIM_MST = TIM_MST_LIST[EachTarget] - - TIM_DUALCORE = "NOT_KNOWN" + tim_dualcore = "NOT_KNOWN" for EachTarget in TIM_DUALCORE_LIST: if EachTarget in mcu_file: - TIM_DUALCORE = TIM_DUALCORE_LIST[EachTarget] + tim_dualcore = TIM_DUALCORE_LIST[EachTarget] - for p in pwm_list: - result = get_gpio_af_num(p[1], p[2]) - CommentedLine = " " - if p[1] in PinLabel.keys(): - if "STDIO_UART" in PinLabel[p[1]]: - CommentedLine = "//" - if "RCC_OSC" in PinLabel[p[1]]: - CommentedLine = "//" - if "%s_" % TIM_MST in p[2]: - CommentedLine = "//" - OneLineCommented = True - if "%s_" % TIM_DUALCORE in p[2]: - CommentedLine = "//" - if CommentedLine != "//": - if p[0] == prev_p: - prev_p = p[0] - p[0] += '_ALT%d' % alt_index - store_pin(p[0], p[0]) + for parsed_pin in pwm_list: + result = get_gpio_af_num(parsed_pin[1], parsed_pin[2]) + commented_line = " " + if parsed_pin[1] in PinLabel: + if "STDIO_UART" in PinLabel[parsed_pin[1]]: + commented_line = "//" + if "RCC_OSC" in PinLabel[parsed_pin[1]]: + commented_line = "//" + if "%s_" % TIM_MST in parsed_pin[2]: + commented_line = "//" + if "%s_" % tim_dualcore in parsed_pin[2]: + commented_line = "//" + if commented_line != "//": + if parsed_pin[0] == prev_p: + prev_p = parsed_pin[0] + parsed_pin[0] += '_ALT%d' % alt_index + store_pin(parsed_pin[0], parsed_pin[0]) alt_index += 1 else: - prev_p = p[0] + prev_p = parsed_pin[0] alt_index = 0 - s1 = "%-17s" % (CommentedLine + " {" + p[0] + ',') - # p[2] : TIM2_CH1 / TIM15_CH1N - a = p[2].split('_') + s1 = "%-17s" % (commented_line + " {" + parsed_pin[0] + ',') + # parsed_pin[2] : TIM2_CH1 / TIM15_CH1N + a = parsed_pin[2].split('_') inst = a[0].replace("TIM", "PWM_") # if len(inst) == 3: # inst += '1' @@ -706,16 +780,14 @@ def print_pwm(): continue else: prev_s1 = s1 - s2 = s1 + af + ', ' + chan + neg + ')}, // ' + p[2] - if p[1] in PinLabel.keys(): - s2 += ' // Connected to ' + PinLabel[p[1]] + s2 = s1 + af + ', ' + chan + neg + ')}, // ' + parsed_pin[2] + if parsed_pin[1] in PinLabel: + s2 += ' // Connected to ' + PinLabel[parsed_pin[1]] s2 += '\n' out_c_file.write(s2) out_c_file.write( """ {NC, NC, 0} }; """) - if OneLineCommented == False: - print("error: TIMER %s NOT FOUND" % TIM_MST) if ADD_DEVICE_IF: out_c_file.write( "#endif\n" ) @@ -723,27 +795,27 @@ def print_pwm(): def print_uart(l): prev_p = '' alt_index = 0 - for p in l: - result = get_gpio_af_num(p[1], p[2]) - CommentedLine = " " - if p[1] in PinLabel.keys(): - if "RCC_OSC" in PinLabel[p[1]]: - CommentedLine = "//" - if CommentedLine != "//": - if p[0] == prev_p: - prev_p = p[0] - p[0] += '_ALT%d' % alt_index - store_pin(p[0], p[0]) + for parsed_pin in l: + result = get_gpio_af_num(parsed_pin[1], parsed_pin[2]) + commented_line = " " + if parsed_pin[1] in PinLabel: + if "RCC_OSC" in PinLabel[parsed_pin[1]]: + commented_line = "//" + if commented_line != "//": + if parsed_pin[0] == prev_p: + prev_p = parsed_pin[0] + parsed_pin[0] += '_ALT%d' % alt_index + store_pin(parsed_pin[0], parsed_pin[0]) alt_index += 1 else: - prev_p = p[0] + prev_p = parsed_pin[0] alt_index = 0 - s1 = "%-17s" % (CommentedLine + " {" + p[0] + ',') - # p[2] : USART2_RX - b=p[2].split('_')[0] + s1 = "%-17s" % (commented_line + " {" + parsed_pin[0] + ',') + # parsed_pin[2] : USART2_RX + b=parsed_pin[2].split('_')[0] b = b.replace("UART", "UART_") b = b.replace("USART", "UART_") - s1 += "%-9s" % (b[:len(b)-1] + b[len(b)-1:] + ',') + s1 += "%-10s" % (b[:len(b)-1] + b[len(b)-1:] + ',') if 'STM32F10' in mcu_file and l == uartrx_list: s1 += 'STM_PIN_DATA(STM_MODE_INPUT, GPIO_PULLUP, ' else: @@ -751,8 +823,8 @@ def print_uart(l): r = result.split(' ') for af in r: s2 = s1 + af + ')},' - if p[1] in PinLabel.keys(): - s2 += ' // Connected to ' + PinLabel[p[1]] + if parsed_pin[1] in PinLabel: + s2 += ' // Connected to ' + PinLabel[parsed_pin[1]] s2 += '\n' out_c_file.write(s2) out_c_file.write( """ {NC, NC, 0} @@ -765,33 +837,33 @@ def print_uart(l): def print_spi(l): prev_p = '' alt_index = 0 - for p in l: - result = get_gpio_af_num(p[1], p[2]) - CommentedLine = " " - if p[1] in PinLabel.keys(): - if "STDIO_UART" in PinLabel[p[1]]: - CommentedLine = "//" - if "RCC_OSC" in PinLabel[p[1]]: - CommentedLine = "//" - if CommentedLine != "//": - if p[0] == prev_p: - prev_p = p[0] - p[0] += '_ALT%d' % alt_index - store_pin(p[0], p[0]) + for parsed_pin in l: + result = get_gpio_af_num(parsed_pin[1], parsed_pin[2]) + commented_line = " " + if parsed_pin[1] in PinLabel: + if "STDIO_UART" in PinLabel[parsed_pin[1]]: + commented_line = "//" + if "RCC_OSC" in PinLabel[parsed_pin[1]]: + commented_line = "//" + if commented_line != "//": + if parsed_pin[0] == prev_p: + prev_p = parsed_pin[0] + parsed_pin[0] += '_ALT%d' % alt_index + store_pin(parsed_pin[0], parsed_pin[0]) alt_index += 1 else: - prev_p = p[0] + prev_p = parsed_pin[0] alt_index = 0 - s1 = "%-17s" % (CommentedLine + " {" + p[0] + ',') - # p[2] : SPI1_MISO - instance=p[2].split('_')[0].replace("SPI", "") + s1 = "%-17s" % (commented_line + " {" + parsed_pin[0] + ',') + # parsed_pin[2] : SPI1_MISO + instance=parsed_pin[2].split('_')[0].replace("SPI", "") s1 += "%-7s" % ('SPI_' + instance + ',') s1 += 'STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, ' r = result.split(' ') for af in r: s2 = s1 + af + ')},' - if p[1] in PinLabel.keys(): - s2 += ' // Connected to ' + PinLabel[p[1]] + if parsed_pin[1] in PinLabel: + s2 += ' // Connected to ' + PinLabel[parsed_pin[1]] s2 += '\n' out_c_file.write(s2) out_c_file.write( """ {NC, NC, 0} @@ -802,18 +874,18 @@ def print_spi(l): def print_can(l): - for p in l: - result = get_gpio_af_num(p[1], p[2]) - CommentedLine = " " - if p[1] in PinLabel.keys(): - if "STDIO_UART" in PinLabel[p[1]]: - CommentedLine = "//" - if "RCC_OSC" in PinLabel[p[1]]: - CommentedLine = "//" - s1 = "%-17s" % (CommentedLine + " {" + p[0] + ',') - # p[2] : CAN_RX / CAN1_RX - p[2] = p[2].replace("FD", "") - instance = p[2].split('_')[0].replace("CAN", "") + for parsed_pin in l: + result = get_gpio_af_num(parsed_pin[1], parsed_pin[2]) + commented_line = " " + if parsed_pin[1] in PinLabel: + if "STDIO_UART" in PinLabel[parsed_pin[1]]: + commented_line = "//" + if "RCC_OSC" in PinLabel[parsed_pin[1]]: + commented_line = "//" + s1 = "%-17s" % (commented_line + " {" + parsed_pin[0] + ',') + # parsed_pin[2] : CAN_RX / CAN1_RX + parsed_pin[2] = parsed_pin[2].replace("FD", "") + instance = parsed_pin[2].split('_')[0].replace("CAN", "") if len(instance) == 0: instance = '1' s1 += "%-7s" % ('CAN_' + instance + ',') @@ -824,8 +896,8 @@ def print_can(l): r = result.split(' ') for af in r: s2 = s1 + af + ')},' - if p[1] in PinLabel.keys(): - s2 += ' // Connected to ' + PinLabel[p[1]] + if parsed_pin[1] in PinLabel: + s2 += ' // Connected to ' + PinLabel[parsed_pin[1]] s2 += '\n' out_c_file.write(s2) out_c_file.write( """ {NC, NC, 0} @@ -836,27 +908,27 @@ def print_can(l): def print_qspi(l): - for p in l: - result = get_gpio_af_num(p[1], p[2]) - if "BK2" in p[2]: # QSPI Bank 2 is not supported + for parsed_pin in l: + result = get_gpio_af_num(parsed_pin[1], parsed_pin[2]) + if "BK2" in parsed_pin[2]: # QSPI Bank 2 is not supported continue - CommentedLine = " " - if p[1] in PinLabel.keys(): - if "STDIO_UART" in PinLabel[p[1]]: - CommentedLine = "//" - if "RCC_OSC" in PinLabel[p[1]]: - CommentedLine = "//" - s1 = "%-16s" % (CommentedLine + " {" + p[0] + ',') - # p[2] : QUADSPI_BK1_IO3 / QUADSPI_CLK / QUADSPI_NCS - if "OCTOSPIM_P2" in p[2]: - s1 += "%-8s" % ('QSPI_2,') + commented_line = " " + if parsed_pin[1] in PinLabel: + if "STDIO_UART" in PinLabel[parsed_pin[1]]: + commented_line = "//" + if "RCC_OSC" in PinLabel[parsed_pin[1]]: + commented_line = "//" + s1 = "%-16s" % (commented_line + " {" + parsed_pin[0] + ',') + # parsed_pin[2] : QUADSPI_BK1_IO3 / QUADSPI_CLK / QUADSPI_NCS + if "OCTOSPIM_P2" in parsed_pin[2]: + s1 += "%-8s" % 'QSPI_2,' else: - s1 += "%-8s" % ('QSPI_1,') + s1 += "%-8s" % 'QSPI_1,' result = result.replace("GPIO_AF10_OTG_FS", "GPIO_AF10_QSPI") s1 += 'STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, ' + result +')},' - s1 += ' // ' + p[2] - if p[1] in PinLabel.keys(): - s1 += ' // Connected to ' + PinLabel[p[1]] + s1 += ' // ' + parsed_pin[2] + if parsed_pin[1] in PinLabel: + s1 += ' // Connected to ' + PinLabel[parsed_pin[1]] s1 += '\n' out_c_file.write(s1) out_c_file.write( """ {NC, NC, 0} @@ -865,6 +937,36 @@ def print_qspi(l): if ADD_DEVICE_IF: out_c_file.write( "#endif\n" ) + +def print_ospi(l): + for parsed_pin in l: + result = get_gpio_af_num(parsed_pin[1], parsed_pin[2]) + commented_line = " " + if parsed_pin[1] in PinLabel: + if "STDIO_UART" in PinLabel[parsed_pin[1]]: + commented_line = "//" + if "RCC_OSC" in PinLabel[parsed_pin[1]]: + commented_line = "//" + s1 = "%-16s" % (commented_line + " {" + parsed_pin[0] + ',') + # parsed_pin[2] : QUADSPI_BK1_IO3 / QUADSPI_CLK / QUADSPI_NCS + if "OCTOSPIM_P2" in parsed_pin[2]: + s1 += "%-8s" % 'OSPI_2,' + else: + s1 += "%-8s" % 'OSPI_1,' + # result = result.replace("GPIO_AF10_OTG_FS", "GPIO_AF10_QSPI") + s1 += 'STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, ' + result +')},' + s1 += ' // ' + parsed_pin[2] + if parsed_pin[1] in PinLabel: + s1 += ' // Connected to ' + PinLabel[parsed_pin[1]] + s1 += '\n' + out_c_file.write(s1) + out_c_file.write( """ {NC, NC, 0} +}; +""") + if ADD_DEVICE_IF: + out_c_file.write( "#endif\n" ) + + def print_usb(lst): use_hs_in_fs = False nb_loop = 1 @@ -876,39 +978,39 @@ def print_usb(lst): nb_loop = 2 for nb in range(nb_loop): - for p in lst: - result = get_gpio_af_num(p[1], p[2]) + for parsed_pin in lst: + result = get_gpio_af_num(parsed_pin[1], parsed_pin[2]) - CommentedLine = " " + commented_line = " " - if p[1] in PinLabel.keys(): - if "STDIO_UART" in PinLabel[p[1]]: - CommentedLine = "//" - if "RCC_OSC" in PinLabel[p[1]]: - CommentedLine = "//" + if parsed_pin[1] in PinLabel: + if "STDIO_UART" in PinLabel[parsed_pin[1]]: + commented_line = "//" + if "RCC_OSC" in PinLabel[parsed_pin[1]]: + commented_line = "//" - if "_SOF" in p[2] or "_NOE" in p[2]: - CommentedLine = "//" + if "_SOF" in parsed_pin[2] or "_NOE" in parsed_pin[2]: + commented_line = "//" if lst is usb_otghs_list: if nb == 0: - if "ULPI" in p[2]: + if "ULPI" in parsed_pin[2]: continue elif not use_hs_in_fs: out_c_file.write("#if (MBED_CONF_TARGET_USB_SPEED == USE_USB_HS_IN_FS)\n") use_hs_in_fs = True else: - if "ULPI" not in p[2]: + if "ULPI" not in parsed_pin[2]: continue elif use_hs_in_fs: out_c_file.write("#else /* MBED_CONF_TARGET_USB_SPEED */\n") use_hs_in_fs = False - s1 = "%-16s" % (CommentedLine + " {" + p[0] + ',') + s1 = "%-16s" % (commented_line + " {" + parsed_pin[0] + ',') # 2nd element is the USB_XXXX signal - if not p[2].startswith("USB_D") and "VBUS" not in p[2]: - if "ID" not in p[2]: + if not parsed_pin[2].startswith("USB_D") and "VBUS" not in parsed_pin[2]: + if "ID" not in parsed_pin[2]: s1 += inst + ", STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, " else: # ID pin: AF_PP + PULLUP @@ -922,9 +1024,9 @@ def print_usb(lst): r = result.split(" ") for af in r: s1 += af + ")}," - s1 += " // " + p[2] - if p[1] in PinLabel.keys(): - s1 += ' // Connected to ' + PinLabel[p[1]] + s1 += " // " + parsed_pin[2] + if parsed_pin[1] in PinLabel: + s1 += ' // Connected to ' + PinLabel[parsed_pin[1]] s1 += "\n" out_c_file.write(s1) if lst: @@ -937,46 +1039,49 @@ def print_usb(lst): out_c_file.write( "#endif\n" ) -def print_PinList(l): - l.sort(key=natural_sortkey) +def print_pin_list(pin_list): + pin_list.sort(key=natural_sortkey) previous_pin = "" - for p in l: - print_debug("pin %s => %s" % (p, p[0])) - if p[0] == previous_pin: + for parsed_pin in pin_list: + print_debug("pin %s => %s" % (parsed_pin, parsed_pin[0])) + if parsed_pin[0] == previous_pin: continue - previous_pin = p[0] + previous_pin = parsed_pin[0] - if "_ALT" in p[0]: - s1 = " %-10s = %s | %s, // same pin used for alternate HW\n" % (p[0], p[0].split('_A')[0], p[0].split('_')[2]) + if "_ALT" in parsed_pin[0]: + s1 = " %-10s = %-5s | %s, // same pin used for alternate HW\n" % (parsed_pin[0], parsed_pin[0].split('_A')[0], parsed_pin[0].split('_')[2]) + elif len(parsed_pin[0]) > 4 and "C" == parsed_pin[0][4]: + s1 = " %-10s = %-5s | ALTC, // dual pad\n" % (parsed_pin[0], parsed_pin[0].split('_A')[0].replace("PC", "PP").replace("C", "").replace("PP", "PC")) else: - if "PA" in p[0]: - PinValue = 0 - elif "PB" in p[0]: - PinValue = 0x10 - elif "PC" in p[0]: - PinValue = 0x20 - elif "PD" in p[0]: - PinValue = 0x30 - elif "PE" in p[0]: - PinValue = 0x40 - elif "PF" in p[0]: - PinValue = 0x50 - elif "PG" in p[0]: - PinValue = 0x60 - elif "PH" in p[0]: - PinValue = 0x70 - elif "PI" in p[0]: - PinValue = 0x80 - elif "PJ" in p[0]: - PinValue = 0x90 - elif "PK" in p[0]: - PinValue = 0xA0 - elif "PZ" in p[0]: - PinValue = 0x0 # to update + pin_value = 0 + if "PA" in parsed_pin[0]: + pin_value = 0 + elif "PB" in parsed_pin[0]: + pin_value = 0x10 + elif "PC" in parsed_pin[0]: + pin_value = 0x20 + elif "PD" in parsed_pin[0]: + pin_value = 0x30 + elif "PE" in parsed_pin[0]: + pin_value = 0x40 + elif "PF" in parsed_pin[0]: + pin_value = 0x50 + elif "PG" in parsed_pin[0]: + pin_value = 0x60 + elif "PH" in parsed_pin[0]: + pin_value = 0x70 + elif "PI" in parsed_pin[0]: + pin_value = 0x80 + elif "PJ" in parsed_pin[0]: + pin_value = 0x90 + elif "PK" in parsed_pin[0]: + pin_value = 0xA0 + elif "PZ" in parsed_pin[0]: + pin_value = 0x0 # to update else: - print("error in print_PinList with pin %s" % p[0]) - PinValue += int(p[0].split('_')[1]) - s1 = " %-10s = 0x%02X,\n" % (p[0], PinValue) + print("error in print_pin_list with pin %s" % parsed_pin[0]) + pin_value += int(parsed_pin[0].split('_')[1]) + s1 = " %-10s = 0x%02X,\n" % (parsed_pin[0], pin_value) out_h_file.write(s1) @@ -1009,7 +1114,7 @@ def print_PinList(l): D13= Px_x, D14= Px_x, D15= Px_x, - """) +""") s = (""" // STDIO for console print @@ -1043,57 +1148,52 @@ def print_PinList(l): // Standardized LED and button names """) - NameCounter = 1 - if len(LED_list) == 0: + name_counter = 1 + if not LED_list: LED_list.append("Pxx") for EachLED in LED_list: - try: - LED_LABEL = " // %s" % PinLabel[EachLED] - except: - LED_LABEL = "" - out_h_file.write(" LED%i = %s,%s\n" % (NameCounter, re.sub(r'(P.)', r'\1_', EachLED), LED_LABEL)) - NameCounter += 1 + led_label = "" + if EachLED in PinLabel: + led_label = " // %s" % PinLabel[EachLED] + out_h_file.write(" LED%i = %s,%s\n" % (name_counter, re.sub(r'(P.)', r'\1_', EachLED), led_label)) + name_counter += 1 - NameCounter = 1 - if len(BUTTON_list) == 0: - BUTTON_list.append("Pxx") + name_counter = 1 + if not BUTTON_list: + BUTTON_list.append("Pxx") for EachBUTTON in BUTTON_list: - try: - BUTTON_LABEL = " // %s" % PinLabel[EachBUTTON] - except: - BUTTON_LABEL = "" - out_h_file.write(" BUTTON%i = %s,%s\n" % (NameCounter, re.sub(r'(P.)', r'\1_', EachBUTTON).split('/')[0].split('-')[0], BUTTON_LABEL)) - NameCounter += 1 + button_label = "" + if EachBUTTON in PinLabel: + button_label = " // %s" % PinLabel[EachBUTTON] + out_h_file.write(" BUTTON%i = %s,%s\n" % (name_counter, re.sub(r'(P.)', r'\1_', EachBUTTON).split('/')[0].split('-')[0], button_label)) + name_counter += 1 out_h_file.write(""" // Backward legacy names USER_BUTTON = BUTTON1, PWM_OUT = D3, - """) +""") -def print_h_file(l, comment): - l.sort(key=natural_sortkey2) - if len(l) > 0: - s = ("\n /**** %s pins ****/\n" % comment) - out_h_file.write(s) +def print_h_file(pin_list, comment): + pin_list.sort(key=natural_sortkey2) + if len(pin_list) > 0: + line_to_write = ("\n /**** %s pins ****/\n" % comment) + out_h_file.write(line_to_write) prev_s = '' alt_index = 0 - for p in l: - if p[2] == prev_s: - prev_s = p[2] - p[2] += '_ALT%d' % alt_index - store_pin(p[0], p[0]) + for parsed_pin in pin_list: + if parsed_pin[2] == prev_s: + prev_s = parsed_pin[2] + parsed_pin[2] += '_ALT%d' % alt_index + store_pin(parsed_pin[0], parsed_pin[0]) alt_index += 1 else: - prev_s = p[2] + prev_s = parsed_pin[2] alt_index = 0 - s1 = " %s = %s,\n" % (p[2].replace("-", "_"), p[0]) - out_h_file.write(s1) - # else: - # s = ("\n/**** No %s pins ***/\n" % comment) - # out_h_file.write(s) + line_to_write = " %s = %s,\n" % (parsed_pin[2].replace("-", "_"), parsed_pin[0]) + out_h_file.write(line_to_write) tokenize = re.compile(r"(\d+)|(\D+)").findall @@ -1140,6 +1240,17 @@ def sort_my_lists(): quadspidata3_list.sort(key=natural_sortkey) quadspisclk_list.sort(key=natural_sortkey) quadspissel_list.sort(key=natural_sortkey) + octospidata0_list.sort(key=natural_sortkey) + octospidata1_list.sort(key=natural_sortkey) + octospidata2_list.sort(key=natural_sortkey) + octospidata3_list.sort(key=natural_sortkey) + octospidata4_list.sort(key=natural_sortkey) + octospidata5_list.sort(key=natural_sortkey) + octospidata6_list.sort(key=natural_sortkey) + octospidata7_list.sort(key=natural_sortkey) + octospidqs_list.sort(key=natural_sortkey) + octospisclk_list.sort(key=natural_sortkey) + octospissel_list.sort(key=natural_sortkey) usb_list.sort(key=natural_sortkey) usb_otgfs_list.sort(key=natural_sortkey) usb_otghs_list.sort(key=natural_sortkey) @@ -1168,23 +1279,49 @@ def clean_all_lists(): del quadspidata3_list[:] del quadspisclk_list[:] del quadspissel_list[:] + del octospidata0_list[:] + del octospidata1_list[:] + del octospidata2_list[:] + del octospidata3_list[:] + del octospidata4_list[:] + del octospidata5_list[:] + del octospidata6_list[:] + del octospidata7_list[:] + del octospidqs_list[:] + del octospisclk_list[:] + del octospissel_list[:] del usb_list[:] + del usb_otgfs_list[:] + del usb_otghs_list[:] del osc_list[:] del sys_list[:] def parse_pins(): - pinregex = r"^(P[A-Z][0-9][0-5]?)" + global DUAL_PAD + pinregex = r"^(P[A-Z][0-9][0-5]?[_]?[C]?)" itemlist = xml_mcu.getElementsByTagName("Pin") for s in itemlist: - m = re.match(pinregex, s.attributes["Name"].value) - if m: + if "Variant" in s.attributes: + continue + pinregex_match = re.match(pinregex, s.attributes["Name"].value) + if pinregex_match: pin = ( - m.group(0)[:2] + "_" + m.group(0)[2:] + pinregex_match.group(0)[:2] + "_" + pinregex_match.group(0)[2:].replace("_", "") ) # pin formatted P_: PF_O name = s.attributes["Name"].value.strip() # full name: "PF0 / OSC_IN" + if "_C" in name: + DUAL_PAD = True + store_pin("PA_0C", "") + store_pin("PA_1C", "") + store_pin("PC_2C", "") + store_pin("PC_3C", "") + if s.attributes["Type"].value == "I/O": store_pin(pin, name) + if DUAL_PAD: + if "_C" in name: + store_pin(pin.replace("2C", "2").replace("3C", "3"), name) else: continue siglist = s.getElementsByTagName("Signal") @@ -1208,6 +1345,8 @@ def parse_pins(): store_eth(pin, name, sig) if "QUADSPI" in sig or "OCTOSPI" in sig: store_qspi(pin, name, sig) + if "OCTOSPI" in sig: + store_ospi(pin, name, sig) if "USB" in sig: store_usb(pin, name, sig) if "RCC_OSC" in sig: @@ -1220,31 +1359,33 @@ PinData = {} PinLabel = {} -def parse_BoardFile(fileName): - print(" * Board file: '%s'" % (fileName)) - board_file = open(fileName, "r") - # IOC_PIN_pattern = re.compile(r'(P[A-I][\d]*).*\.([\w]*)=(.*)') - IOC_PIN_pattern = re.compile(r'(.*)\.([\w]*)=(.*)') +def parse_board_file(file_name): + global MCU_USERNAME + print(" * Board file: '%s'" % file_name) + board_file = open(file_name, "r") + ioc_pin_pattern = re.compile(r'(.*)\.([\w]*)=(.*)') for line in board_file.readlines(): - IOC_PIN = re.match(IOC_PIN_pattern, line) - if IOC_PIN: - if IOC_PIN.groups()[0] in PinData.keys(): - PinData[IOC_PIN.groups()[0]][IOC_PIN.groups()[1]] = IOC_PIN.groups()[2] + ioc_pin_match = re.match(ioc_pin_pattern, line) + if ioc_pin_match: + if ioc_pin_match.groups()[0] in PinData: + PinData[ioc_pin_match.groups()[0]][ioc_pin_match.groups()[1]] = ioc_pin_match.groups()[2] else: - PinData[IOC_PIN.groups()[0]] = {} - PinData[IOC_PIN.groups()[0]][IOC_PIN.groups()[1]] = IOC_PIN.groups()[2] - # IOC_MCU = re.match(r'Mcu\.UserName=(.*)', line) - IOC_MCU = re.match(r'Mcu\.Name=(.*)', line) - if IOC_MCU: - mcu_list.append("%s.xml" % IOC_MCU.groups()[0]) + PinData[ioc_pin_match.groups()[0]] = {} + PinData[ioc_pin_match.groups()[0]][ioc_pin_match.groups()[1]] = ioc_pin_match.groups()[2] + ioc_mcu_match = re.match(r'Mcu\.Name=(.*)', line) + if ioc_mcu_match: + mcu_list.append("%s.xml" % ioc_mcu_match.groups()[0]) + ioc_mcu_match = re.match(r'Mcu\.UserName=(.*)', line) + if ioc_mcu_match: + MCU_USERNAME = ioc_mcu_match.groups()[0] board_file.close() for EachPin in PinData: PinLabel[EachPin] = "" - if "Signal" in PinData[EachPin].keys(): + if "Signal" in PinData[EachPin]: PinLabel[EachPin] = PinData[EachPin]["Signal"] - if "GPIO_Label" in PinData[EachPin].keys(): + if "GPIO_Label" in PinData[EachPin]: PinLabel[EachPin] = PinData[EachPin]["GPIO_Label"] if any(led in PinLabel[EachPin].upper() for led in ["LED", "LD1", "LD2", "LD3", "LD4", "LD5", "LD6", "LD7", "LD8", "LD9"]): @@ -1252,10 +1393,10 @@ def parse_BoardFile(fileName): elif any(button in PinLabel[EachPin].upper() for button in ["BUTTON", "B_USER", "BTN"]): BUTTON_list.append(EachPin) - UART_HW_OPTION = "NO_NEED" - for EachTarget in VCP_UART_LIST: - if EachTarget in fileName: - UART_HW_OPTION = VCP_UART_LIST[EachTarget] + uart_hw_option = "NO_NEED" + for each_target in VCP_UART_LIST: + if each_target in file_name: + uart_hw_option = VCP_UART_LIST[each_target] try: if "STLK_RX" in PinLabel[EachPin] or "STLK_TX" in PinLabel[EachPin]: @@ -1284,6 +1425,12 @@ def parse_BoardFile(fileName): elif "ST_LINK_UART1_TX" in PinLabel[EachPin]: PinLabel[EachPin] = "STDIO_UART_TX" STDIO_list[0] = EachPin + elif "ST-LINK-UART1_RX" in PinLabel[EachPin]: + PinLabel[EachPin] = "STDIO_UART_RX" + STDIO_list[1] = EachPin + elif "ST-LINK-UART1_TX" in PinLabel[EachPin]: + PinLabel[EachPin] = "STDIO_UART_TX" + STDIO_list[0] = EachPin elif "STLINK_RX" in PinLabel[EachPin] or "STLINK_TX" in PinLabel[EachPin]: # Patch waiting for CubeMX correction if "RX" in PinData[EachPin]["Signal"]: @@ -1292,10 +1439,10 @@ def parse_BoardFile(fileName): else: PinLabel[EachPin] = "STDIO_UART_TX" STDIO_list[0] = EachPin - elif "%s_RX" % UART_HW_OPTION in PinLabel[EachPin]: + elif "%s_RX" % uart_hw_option in PinLabel[EachPin]: PinLabel[EachPin] = "STDIO_UART_RX" STDIO_list[1] = EachPin - elif "%s_TX" % UART_HW_OPTION in PinLabel[EachPin]: + elif "%s_TX" % uart_hw_option in PinLabel[EachPin]: STDIO_list[0] = EachPin PinLabel[EachPin] = "STDIO_UART_TX" elif "_RESERVED" in PinLabel[EachPin]: @@ -1349,7 +1496,7 @@ Once generated, you have to check and comment pins that can not be used (specifi '''), formatter_class=RawTextHelpFormatter) -group = parser.add_mutually_exclusive_group(required = True) +group = parser.add_mutually_exclusive_group(required=True) group.add_argument("-l", "--list", help="list available mcu xml files description in STM32CubeMX", action="store_true") @@ -1381,22 +1528,20 @@ cubemxdirIP = os.path.join(cubemxdir, "db", "mcu", "IP") cubemxdirBOARDS = os.path.join(cubemxdir, "db", "plugins", "boardmanager", "boards") version_file = os.path.join(cubemxdir, "db", "package.xml") -try: - xml_file = parse(version_file) - PackDescription_item = xml_file.getElementsByTagName("PackDescription") - for item in PackDescription_item: - CUBEMX_DB_VERSION = item.attributes["Release"].value -except: - CUBEMX_DB_VERSION = "NOT_FOUND" -print ("CubeMX DB version %s\n" % CUBEMX_DB_VERSION) +cubemx_db_version = "NOT_FOUND" +xml_file = parse(version_file) +PackDescription_item = xml_file.getElementsByTagName("PackDescription") +for item in PackDescription_item: + cubemx_db_version = item.attributes["Release"].value +print ("CubeMX DB version %s\n" % cubemx_db_version) if args.list: - FileCount = 0 + file_count = 0 for f in fnmatch.filter(os.listdir(cubemxdirMCU), "STM32*.xml"): print(f) - FileCount += 1 + file_count += 1 print() - print("%i available xml files description" % FileCount) + print("%i available xml files description" % file_count) sys.exit(0) if args.boards: @@ -1458,7 +1603,7 @@ if args.target: print("TrustZoneEnabled boards not parsed") sys.exit(0) - parse_BoardFile(board_file_name) + parse_board_file(board_file_name) TargetName = "" if "Nucleo" in board_file_name: TargetName += "NUCLEO_" @@ -1478,6 +1623,7 @@ if args.target: target_rename = { # manual renaming for some boards "DISCO_L072C": "DISCO_L072CZ_LRWAN1", "DISCO_L475V": "DISCO_L475VG_IOT01A", + "DISCO_L4S5V": "B_L4S5I_IOT01A", "DISCO_G071RBT": "DISCO_G071RB", "DISCO_L4R9A": "DISCO_L4R9I", "NUCLEO_WB55R": "NUCLEO_WB55RG", @@ -1492,7 +1638,7 @@ if args.target: "DISCO_H747XIH": "DISCO_H747I" } - if TargetName in target_rename.keys(): + if TargetName in target_rename: TargetName = target_rename[TargetName] if "DISC1" in board_file_name: @@ -1503,75 +1649,166 @@ if args.target: # Parse the user's custom board .ioc file if args.custom: - parse_BoardFile(args.custom) + parse_board_file(args.custom) TargetName = "" for mcu_file in mcu_list: - if args.mcu: - TargetName = os.path.splitext(mcu_file)[0] - out_path = os.path.join(cur_dir, 'targets_custom', 'TARGET_STM', 'TARGET_%s' % TargetName) - print(" * Output directory: %s" % (out_path)) - input_file_name = os.path.join(cubemxdirMCU, mcu_file) - print(" * Generating %s and %s with '%s'" % (PeripheralPins_c_filename, PinNames_h_filename, input_file_name)) - output_cfilename = os.path.join(out_path, PeripheralPins_c_filename) - output_hfilename = os.path.join(out_path, PinNames_h_filename) - if not(os.path.isdir(out_path)): - os.makedirs(out_path) + TargetNameList = [] - if (os.path.isfile(output_cfilename)): - print_debug(" * Requested %s file already exists and will be overwritten" % PeripheralPins_c_filename) - os.remove(output_cfilename) - out_c_file = open(output_cfilename, 'w') - out_h_file = open(output_hfilename, 'w') + # print("--- mcu_file %s ---" %(mcu_file)) + m2 = re.match("(.*)\(([\w])\-([\w])\)(.*)", mcu_file) + m3 = re.match("(.*)\(([\w])\-([\w])\-([\w])\)(.*)", mcu_file) + m4 = re.match("(.*)\(([\w])\-([\w])\-([\w])\-([\w])\)(.*)", mcu_file) + m5 = re.match("(.*)\(([\w])\-([\w])\-([\w])\-([\w])\-([\w])\)(.*)", mcu_file) + if m2: + new_mcu_file = m2.group(1) + m2.group(2) + m2.group(4) + TargetNameList.append(os.path.splitext(new_mcu_file)[0]) + new_mcu_file = m2.group(1) + m2.group(3) + m2.group(4) + TargetNameList.append(os.path.splitext(new_mcu_file)[0]) + elif m3: + new_mcu_file = m3.group(1) + m3.group(2) + m3.group(5) + TargetNameList.append(os.path.splitext(new_mcu_file)[0]) + new_mcu_file = m3.group(1) + m3.group(3) + m3.group(5) + TargetNameList.append(os.path.splitext(new_mcu_file)[0]) + new_mcu_file = m3.group(1) + m3.group(4) + m3.group(5) + TargetNameList.append(os.path.splitext(new_mcu_file)[0]) + elif m4: + new_mcu_file = m4.group(1) + m4.group(2) + m4.group(6) + TargetNameList.append(os.path.splitext(new_mcu_file)[0]) + new_mcu_file = m4.group(1) + m4.group(3) + m4.group(6) + TargetNameList.append(os.path.splitext(new_mcu_file)[0]) + new_mcu_file = m4.group(1) + m4.group(4) + m4.group(6) + TargetNameList.append(os.path.splitext(new_mcu_file)[0]) + new_mcu_file = m4.group(1) + m4.group(5) + m4.group(6) + TargetNameList.append(os.path.splitext(new_mcu_file)[0]) + elif m5: + new_mcu_file = m5.group(1) + m5.group(2) + m5.group(7) + TargetNameList.append(os.path.splitext(new_mcu_file)[0]) + new_mcu_file = m5.group(1) + m5.group(3) + m5.group(7) + TargetNameList.append(os.path.splitext(new_mcu_file)[0]) + new_mcu_file = m5.group(1) + m5.group(4) + m5.group(7) + TargetNameList.append(os.path.splitext(new_mcu_file)[0]) + new_mcu_file = m5.group(1) + m5.group(5) + m5.group(7) + TargetNameList.append(os.path.splitext(new_mcu_file)[0]) + new_mcu_file = m5.group(1) + m5.group(6) + m5.group(7) + TargetNameList.append(os.path.splitext(new_mcu_file)[0]) + elif "(" in mcu_file: + print("!!! error SCRIPT ISSUE with file %s" % mcu_file) + sys.exit(4) + else: + TargetNameList.append(os.path.splitext(mcu_file)[0]) - #open input file - try: - xml_mcu = parse(input_file_name) - except: - # Patch waiting for CubeMX correction - if "STM32F042K6Tx" in input_file_name: - input_file_name = os.path.join(cubemxdirMCU, "STM32F042K(4-6)Tx.xml") - xml_mcu = parse(input_file_name) - elif "STM32F429Z" in input_file_name: - input_file_name = os.path.join(cubemxdirMCU, "STM32F429ZITx.xml") - xml_mcu = parse(input_file_name) - elif "STM32F746Z" in input_file_name: - input_file_name = os.path.join(cubemxdirMCU, "STM32F746ZGTx.xml") - xml_mcu = parse(input_file_name) - elif "STM32F767Z" in input_file_name: - input_file_name = os.path.join(cubemxdirMCU, "STM32F767ZGTx.xml") - xml_mcu = parse(input_file_name) - - elif "STM32L011K4Tx" in input_file_name: - input_file_name = os.path.join(cubemxdirMCU, "STM32L011K(3-4)Tx.xml") - xml_mcu = parse(input_file_name) - elif "STM32L432KCUx" in input_file_name: - input_file_name = os.path.join(cubemxdirMCU, "STM32L432K(B-C)Ux.xml") - xml_mcu = parse(input_file_name) - elif "STM32F746N" in input_file_name: - input_file_name = os.path.join(cubemxdirMCU, "STM32F746NGHx.xml") - xml_mcu = parse(input_file_name) + for EachTargetName in TargetNameList: + # print("EachTargetName %s" % EachTargetName) + m = re.match("(STM32[\w]{2})", EachTargetName) + if m: + TARGET_FAMILY = m.group(0) else: - print ("\n ! ! ! Error in CubeMX file. File " + input_file_name + " doesn't exist") - print (" ! ! ! Check in " + cubemxdirMCU) + print("no TARGET_FAMILY") + sys.exit(2) + + SearchSubFamily = EachTargetName[:9] + 'x' + EachTargetName[10:] + m = re.match("(STM32[\w]{6})", SearchSubFamily) + if m: + TARGET_SUBFAMILY = m.group(0) + else: + print("no TARGET_SUBFAMILY") + sys.exit(2) + + if args.mcu: + m = re.match("(STM32[\w]{7})", EachTargetName) + if m: + out_path = os.path.join(cur_dir, 'targets_custom', 'TARGET_STM', 'TARGET_%s' %TARGET_FAMILY, 'TARGET_%s' %TARGET_SUBFAMILY, 'TARGET_%s' % m.group(0)) + if EachTargetName.endswith('A'): + out_path += "_A" + elif EachTargetName.endswith('P'): + out_path += "_P" + elif EachTargetName.endswith('Q'): + out_path += "_Q" + elif EachTargetName.endswith('N'): + out_path += "_N" + elif EachTargetName.endswith('S'): + out_path += "_S" + elif EachTargetName.endswith('X'): + out_path += "_X" + else: + print("!!! Warning output directory not found") + sys.exit(8) + else: + if EachTargetName == MCU_USERNAME: + out_path = os.path.join(cur_dir, 'targets_custom', 'TARGET_STM', 'TARGET_%s' % TARGET_FAMILY, 'TARGET_%s' % TARGET_SUBFAMILY, 'TARGET_%s' % TargetName) + else: + continue + + print(" * Output directory: %s" % out_path) + if not (os.path.isdir(out_path)): + os.makedirs(out_path) + else: + print("!!! %s already exist" % out_path) + + input_file_name = os.path.join(cubemxdirMCU, mcu_file) + print(" * Generating %s and %s with '%s'" % (PeripheralPins_c_filename, PinNames_h_filename, input_file_name)) + output_cfilename = os.path.join(out_path, PeripheralPins_c_filename) + output_hfilename = os.path.join(out_path, PinNames_h_filename) + + if os.path.isfile(output_cfilename): + print_debug(" * Requested %s file already exists and will be overwritten" % PeripheralPins_c_filename) + os.remove(output_cfilename) + out_c_file = open(output_cfilename, 'w') + out_h_file = open(output_hfilename, 'w') + + #open input file + try: + xml_mcu = parse(input_file_name) + except: + # Patch waiting for CubeMX correction + if "STM32F042K6Tx" in input_file_name: + input_file_name = os.path.join(cubemxdirMCU, "STM32F042K(4-6)Tx.xml") + xml_mcu = parse(input_file_name) + elif "STM32F429Z" in input_file_name: + input_file_name = os.path.join(cubemxdirMCU, "STM32F429ZITx.xml") + xml_mcu = parse(input_file_name) + elif "STM32F746Z" in input_file_name: + input_file_name = os.path.join(cubemxdirMCU, "STM32F746ZGTx.xml") + xml_mcu = parse(input_file_name) + elif "STM32F767Z" in input_file_name: + input_file_name = os.path.join(cubemxdirMCU, "STM32F767ZGTx.xml") + xml_mcu = parse(input_file_name) + + elif "STM32L011K4Tx" in input_file_name: + input_file_name = os.path.join(cubemxdirMCU, "STM32L011K(3-4)Tx.xml") + xml_mcu = parse(input_file_name) + elif "STM32L432KCUx" in input_file_name: + input_file_name = os.path.join(cubemxdirMCU, "STM32L432K(B-C)Ux.xml") + xml_mcu = parse(input_file_name) + elif "STM32F746N" in input_file_name: + input_file_name = os.path.join(cubemxdirMCU, "STM32F746NGHx.xml") + xml_mcu = parse(input_file_name) + else: + print ("\n ! ! ! Error in CubeMX file. File " + input_file_name + " doesn't exist") + print (" ! ! ! Check in " + cubemxdirMCU) + sys.exit(1) + gpiofile = find_gpio_file() + if gpiofile == "ERROR": + print("error: Could not find GPIO file") sys.exit(1) - gpiofile = find_gpio_file() - if gpiofile == "ERROR": - print("error: Could not find GPIO file") - sys.exit(1) - xml_gpio = parse(os.path.join(cubemxdirIP, "GPIO-" + gpiofile + "_Modes.xml")) - print (" * GPIO file: " + os.path.join(cubemxdirIP, "GPIO-" + gpiofile + "_Modes.xml")) + if gpiofile == "STM32H747_gpio_v1_0" and "H743" in input_file_name: # Workaround for DB issue + gpiofile = "STM32H743_gpio_v1_0" + xml_gpio = parse(os.path.join(cubemxdirIP, "GPIO-" + gpiofile + "_Modes.xml")) + print (" * GPIO file: " + os.path.join(cubemxdirIP, "GPIO-" + gpiofile + "_Modes.xml")) - parse_pins() - sort_my_lists() - print_header() - print_all_lists() - print_footer() + find_tim_mst() - nb_pin = (len(io_list)) - nb_connected_pin = len(PinLabel) - print (" * I/O pins found: %i connected: %i\n" % (nb_pin, nb_connected_pin)) - clean_all_lists() + parse_pins() + sort_my_lists() + print_header() + print_all_lists() + print_footer() - out_c_file.close() - out_h_file.close() + nb_pin = (len(io_list)) + nb_connected_pin = len(PinLabel) + print (" * I/O pins found: %i connected: %i\n" % (nb_pin, nb_connected_pin)) + clean_all_lists() + + out_c_file.close() + out_h_file.close()