Advertisement
danieleff

CubeMX data export

Oct 23rd, 2016
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.69 KB | None | 0 0
  1. import xml.etree.ElementTree as ET
  2. import os.path
  3. import sys
  4.  
  5. cubemx_dir = 'D:\programs\STM32Cube\STM32CubeMX'
  6.  
  7. if len(sys.argv) == 1:
  8.     print 'Usage: ' + __file__ + ' STM32_MCU_NAME'
  9.     print 'For example: ' + __file__ + ' STM32F103C8'
  10.     exit(-1)
  11.  
  12. mcu_name = sys.argv[1]
  13.  
  14. ns = {'stm': 'http://mcd.rou.st.com/modules.php?name=mcu'}
  15.  
  16. families_xml = os.path.join(cubemx_dir, 'db', 'mcu', 'families.xml')
  17. if not os.path.exists(families_xml):
  18.     print 'Could not find CubeMX, please set CubeMX directory'
  19.     exit(-1)
  20.  
  21. families = ET.parse(families_xml).getroot()
  22. mcu_xml = families.find(".//Mcu[@RPN='" + mcu_name + "']").attrib['Name'] + '.xml'
  23.  
  24. mcu = ET.parse(os.path.join(cubemx_dir, 'db', 'mcu', mcu_xml)).getroot()
  25. remap_xml = 'GPIO-' + mcu.find("stm:IP[@Name='GPIO']", ns).attrib['Version'] + '_Modes.xml'
  26.  
  27. remap_root = ET.parse(os.path.join(cubemx_dir, 'db', 'mcu', 'IP', remap_xml )).getroot()
  28.  
  29.  
  30. mcu_pins = mcu.findall('stm:Pin',  ns);
  31.  
  32. remaps = {}
  33. defaultremaps = {}
  34. af_functions=[]
  35.  
  36. def find_remaps():
  37.     for pin in remap_root.findall('stm:GPIO_Pin',  ns):
  38.         pin_name = pin.attrib['Name']
  39.  
  40.         gpio_signals = pin.findall('stm:PinSignal',  ns)
  41.         for gpio_signal in gpio_signals:
  42.             signal = gpio_signal.attrib['Name']
  43.             periph = signal.split('_')[0]
  44.             if signal not in remaps:
  45.                 remaps[signal] = {}
  46.            
  47.             if mcu_name.startswith('STM32F1'):
  48.                 remap_function = False
  49.                
  50.                 remap_block = gpio_signal.find('stm:RemapBlock', ns)
  51.                 if remap_block is not None:
  52.                     if 'DefaultRemap' in remap_block.attrib:
  53.                         remap_function = 'AF__HAL_AFIO_REMAP_' + periph + '_DISABLE'
  54.                         defaultremaps[signal] = pin_name
  55.                     else:
  56.                         remap_function = 'AF' + remap_block.find('stm:SpecificParameter', ns).find('stm:PossibleValue', ns).text
  57.                 else:
  58.                     remap_function = 'AF__NO_REMAP'
  59.                     if signal not in defaultremaps:
  60.                         defaultremaps[signal] = pin_name
  61.                        
  62.                 remaps[signal][pin_name] = remap_function
  63.                 if remap_function not in af_functions:
  64.                     af_functions.append(remap_function)
  65.                
  66.             else:
  67.                 gpio_af = gpio_signal.find("stm:SpecificParameter[@Name='GPIO_AF']", ns)
  68.                 remaps[signal][pin_name] = gpio_af.find("stm:PossibleValue", ns).text
  69.            
  70. pins = []
  71. peripherals = []
  72.  
  73. def process_pins():
  74.     for pin in mcu_pins:
  75.         pin_name = pin.attrib['Name']
  76.         pins.append(pin_name)
  77.    
  78.         if not pin.attrib['Type'] == 'I/O':
  79.             continue
  80.        
  81.         signals = pin.findall('stm:Signal',  ns)
  82.         for signal_element in signals:
  83.             signal = signal_element.attrib['Name']
  84.             periph = signal.split('_')[0]
  85.             if periph == 'GPIO':
  86.                 continue
  87.            
  88.             if periph not in peripherals:
  89.                 peripherals.append(periph)
  90.                
  91.             if signal not in defaultremaps:
  92.                 defaultremaps[signal] = pin_name
  93.  
  94.             if signal not in remaps:
  95.                 remaps[signal] = {}
  96.                 remaps[signal][pin_name] = 'AF_NO_REMAP'
  97.  
  98. def generate_source_code():
  99.     source_code = '//DO NOT MODIFY, THIS FILE WAS AUTOMATICALLY GENERATED\n'
  100.     source_code += '//MCU name: ' + mcu_name+ '\n'
  101.     source_code += '//MCU  xml: ' + mcu_xml+ '\n'
  102.     source_code += '//GPIO remap/alternate function xml: ' + remap_xml+ '\n'
  103.    
  104.     source_code += '\n'
  105.    
  106.     if len(af_functions) > 0:
  107.         source_code += 'static void AF_NO_REMAP (void) { }\n'
  108.        
  109.     for af_function in af_functions:
  110.         if 'SPI' in af_function:
  111.             source_code += 'static void ' + af_function + '(void) { ' + af_function[2:] + '(); }\n'
  112.  
  113.     source_code += '\n'
  114.     source_code += 'spi_init_info_t spi_init_info[NB_SPI_INSTANCES] = {\n'
  115.    
  116.     for periph in peripherals:
  117.         if periph.startswith('SPI'):
  118.             source_code += '    {\n'
  119.            
  120.             source_code +=  '        .spi_instance   = ' + periph + ',\n'
  121.             source_code +=  '        .spi_clock_init = ' + periph + '_CLK_ENABLE,\n'
  122.            
  123.             source_code +=  '        .miso_alternate = ' + remaps[periph+"_MISO"][defaultremaps[periph+"_MISO"]] + ',\n'
  124.             source_code +=  '        .miso_port      = GPIO' + defaultremaps[periph+"_MISO"][1:2] + ',\n'
  125.             source_code +=  '        .miso_pin       = GPIO_PIN_' + defaultremaps[periph+"_MISO"][2:] + ',\n'
  126.             source_code +=  '        .miso_speed     = GPIO_SPEED_FREQ_HIGH,\n'
  127.             source_code +=  '        .miso_pull      = GPIO_PULLDOWN,\n'
  128.             source_code +=  '        .miso_mode      = GPIO_MODE_AF_PP,\n'
  129.            
  130.             source_code +=  '        .mosi_alternate = ' + remaps[periph+"_MOSI"][defaultremaps[periph+"_MOSI"]] + ',\n'
  131.             source_code +=  '        .mosi_port      = GPIO' + defaultremaps[periph+"_MOSI"][1:2] + ',\n'
  132.             source_code +=  '        .mosi_pin       = GPIO_PIN_' + defaultremaps[periph+"_MOSI"][2:] + ',\n'
  133.             source_code +=  '        .mosi_speed     = GPIO_SPEED_FREQ_HIGH,\n'
  134.             source_code +=  '        .mosi_pull      = GPIO_PULLDOWN,\n'
  135.             source_code +=  '        .mosi_mode      = GPIO_MODE_AF_PP,\n'
  136.            
  137.             source_code +=  '        .sck_alternate  = ' + remaps[periph+"_SCK"][defaultremaps[periph+"_SCK"]] + ',\n'
  138.             source_code +=  '        .sck_port       = GPIO' + defaultremaps[periph+"_SCK"][1:2] + ',\n'
  139.             source_code +=  '        .sck_pin        = GPIO_PIN_' + defaultremaps[periph+"_SCK"][2:] + ',\n'
  140.             source_code +=  '        .sck_speed      = GPIO_SPEED_FREQ_HIGH,\n'
  141.             source_code +=  '        .sck_pull       = GPIO_PULLDOWN,\n'
  142.             source_code +=  '        .sck_mode       = GPIO_MODE_AF_PP,\n'
  143.            
  144.             source_code += '    },\n'
  145.     source_code += '};\n'
  146.    
  147.     source_code += '\n'
  148.     source_code += 'remaps_t remaps {\n'
  149.    
  150.     for signal in sorted(remaps):
  151.         if signal.startswith('SPI'):
  152.             for pin in remaps[signal]:
  153.                 if pin in pins:
  154.                     remap = remaps[signal][pin]
  155.                     source_code += '    { ' + signal.ljust(9) + ', ' + pin.ljust(4) + ', ' + remap + '},\n'
  156.     source_code += '};\n'
  157.    
  158.     return source_code
  159.                
  160. find_remaps()
  161.  
  162. process_pins()
  163.  
  164. peripherals.sort()
  165.  
  166. af_functions.sort()
  167.  
  168. print generate_source_code()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement