Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import xml.etree.ElementTree as ET
- import os.path
- import sys
- cubemx_dir = 'D:\programs\STM32Cube\STM32CubeMX'
- if len(sys.argv) == 1:
- print 'Usage: ' + __file__ + ' STM32_MCU_NAME'
- print 'For example: ' + __file__ + ' STM32F103C8'
- exit(-1)
- mcu_name = sys.argv[1]
- ns = {'stm': 'http://mcd.rou.st.com/modules.php?name=mcu'}
- families_xml = os.path.join(cubemx_dir, 'db', 'mcu', 'families.xml')
- if not os.path.exists(families_xml):
- print 'Could not find CubeMX, please set CubeMX directory'
- exit(-1)
- families = ET.parse(families_xml).getroot()
- mcu_xml = families.find(".//Mcu[@RPN='" + mcu_name + "']").attrib['Name'] + '.xml'
- mcu = ET.parse(os.path.join(cubemx_dir, 'db', 'mcu', mcu_xml)).getroot()
- remap_xml = 'GPIO-' + mcu.find("stm:IP[@Name='GPIO']", ns).attrib['Version'] + '_Modes.xml'
- remap_root = ET.parse(os.path.join(cubemx_dir, 'db', 'mcu', 'IP', remap_xml )).getroot()
- mcu_pins = mcu.findall('stm:Pin', ns);
- remaps = {}
- defaultremaps = {}
- af_functions=[]
- def find_remaps():
- for pin in remap_root.findall('stm:GPIO_Pin', ns):
- pin_name = pin.attrib['Name']
- gpio_signals = pin.findall('stm:PinSignal', ns)
- for gpio_signal in gpio_signals:
- signal = gpio_signal.attrib['Name']
- periph = signal.split('_')[0]
- if signal not in remaps:
- remaps[signal] = {}
- if mcu_name.startswith('STM32F1'):
- remap_function = False
- remap_block = gpio_signal.find('stm:RemapBlock', ns)
- if remap_block is not None:
- if 'DefaultRemap' in remap_block.attrib:
- remap_function = 'AF__HAL_AFIO_REMAP_' + periph + '_DISABLE'
- defaultremaps[signal] = pin_name
- else:
- remap_function = 'AF' + remap_block.find('stm:SpecificParameter', ns).find('stm:PossibleValue', ns).text
- else:
- remap_function = 'AF__NO_REMAP'
- if signal not in defaultremaps:
- defaultremaps[signal] = pin_name
- remaps[signal][pin_name] = remap_function
- if remap_function not in af_functions:
- af_functions.append(remap_function)
- else:
- gpio_af = gpio_signal.find("stm:SpecificParameter[@Name='GPIO_AF']", ns)
- remaps[signal][pin_name] = gpio_af.find("stm:PossibleValue", ns).text
- pins = []
- peripherals = []
- def process_pins():
- for pin in mcu_pins:
- pin_name = pin.attrib['Name']
- pins.append(pin_name)
- if not pin.attrib['Type'] == 'I/O':
- continue
- signals = pin.findall('stm:Signal', ns)
- for signal_element in signals:
- signal = signal_element.attrib['Name']
- periph = signal.split('_')[0]
- if periph == 'GPIO':
- continue
- if periph not in peripherals:
- peripherals.append(periph)
- if signal not in defaultremaps:
- defaultremaps[signal] = pin_name
- if signal not in remaps:
- remaps[signal] = {}
- remaps[signal][pin_name] = 'AF_NO_REMAP'
- def generate_source_code():
- source_code = '//DO NOT MODIFY, THIS FILE WAS AUTOMATICALLY GENERATED\n'
- source_code += '//MCU name: ' + mcu_name+ '\n'
- source_code += '//MCU xml: ' + mcu_xml+ '\n'
- source_code += '//GPIO remap/alternate function xml: ' + remap_xml+ '\n'
- source_code += '\n'
- if len(af_functions) > 0:
- source_code += 'static void AF_NO_REMAP (void) { }\n'
- for af_function in af_functions:
- if 'SPI' in af_function:
- source_code += 'static void ' + af_function + '(void) { ' + af_function[2:] + '(); }\n'
- source_code += '\n'
- source_code += 'spi_init_info_t spi_init_info[NB_SPI_INSTANCES] = {\n'
- for periph in peripherals:
- if periph.startswith('SPI'):
- source_code += ' {\n'
- source_code += ' .spi_instance = ' + periph + ',\n'
- source_code += ' .spi_clock_init = ' + periph + '_CLK_ENABLE,\n'
- source_code += ' .miso_alternate = ' + remaps[periph+"_MISO"][defaultremaps[periph+"_MISO"]] + ',\n'
- source_code += ' .miso_port = GPIO' + defaultremaps[periph+"_MISO"][1:2] + ',\n'
- source_code += ' .miso_pin = GPIO_PIN_' + defaultremaps[periph+"_MISO"][2:] + ',\n'
- source_code += ' .miso_speed = GPIO_SPEED_FREQ_HIGH,\n'
- source_code += ' .miso_pull = GPIO_PULLDOWN,\n'
- source_code += ' .miso_mode = GPIO_MODE_AF_PP,\n'
- source_code += ' .mosi_alternate = ' + remaps[periph+"_MOSI"][defaultremaps[periph+"_MOSI"]] + ',\n'
- source_code += ' .mosi_port = GPIO' + defaultremaps[periph+"_MOSI"][1:2] + ',\n'
- source_code += ' .mosi_pin = GPIO_PIN_' + defaultremaps[periph+"_MOSI"][2:] + ',\n'
- source_code += ' .mosi_speed = GPIO_SPEED_FREQ_HIGH,\n'
- source_code += ' .mosi_pull = GPIO_PULLDOWN,\n'
- source_code += ' .mosi_mode = GPIO_MODE_AF_PP,\n'
- source_code += ' .sck_alternate = ' + remaps[periph+"_SCK"][defaultremaps[periph+"_SCK"]] + ',\n'
- source_code += ' .sck_port = GPIO' + defaultremaps[periph+"_SCK"][1:2] + ',\n'
- source_code += ' .sck_pin = GPIO_PIN_' + defaultremaps[periph+"_SCK"][2:] + ',\n'
- source_code += ' .sck_speed = GPIO_SPEED_FREQ_HIGH,\n'
- source_code += ' .sck_pull = GPIO_PULLDOWN,\n'
- source_code += ' .sck_mode = GPIO_MODE_AF_PP,\n'
- source_code += ' },\n'
- source_code += '};\n'
- source_code += '\n'
- source_code += 'remaps_t remaps {\n'
- for signal in sorted(remaps):
- if signal.startswith('SPI'):
- for pin in remaps[signal]:
- if pin in pins:
- remap = remaps[signal][pin]
- source_code += ' { ' + signal.ljust(9) + ', ' + pin.ljust(4) + ', ' + remap + '},\n'
- source_code += '};\n'
- return source_code
- find_remaps()
- process_pins()
- peripherals.sort()
- af_functions.sort()
- print generate_source_code()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement