2017-04-09 21:53:07 -07:00
|
|
|
# This file is a mess, but does the job
|
|
|
|
import sys
|
2017-07-15 09:09:28 -07:00
|
|
|
sys.dont_write_bytecode = True
|
|
|
|
|
|
|
|
import config
|
|
|
|
import chip
|
2017-04-09 21:53:07 -07:00
|
|
|
import re
|
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
def natural_sort_key(s, _nsre=re.compile('([0-9]+)')):
|
|
|
|
return [int(text) if text.isdigit() else text.lower()
|
|
|
|
for text in re.split(_nsre, s)]
|
2017-04-09 21:53:07 -07:00
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
peripheral_counters = {}
|
|
|
|
peripheral_source_code_cache = {}
|
2017-04-09 21:53:07 -07:00
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
def get_cached_code_include(mcu, periph, source_code):
|
|
|
|
series = mcu.mcu_name[5:7]
|
|
|
|
key = series + source_code
|
2017-04-10 21:30:57 -07:00
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
if key in peripheral_source_code_cache:
|
|
|
|
return peripheral_source_code_cache[key]
|
2017-04-09 21:53:07 -07:00
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
counter_key = series + periph
|
|
|
|
peripheral_counters.setdefault(counter_key, 1)
|
2017-04-09 21:53:07 -07:00
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
counter = peripheral_counters[counter_key]
|
|
|
|
peripheral_counters[counter_key] = peripheral_counters[counter_key] + 1
|
2017-04-09 21:53:07 -07:00
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
filename = "stm32_" + periph + '_variant_' + str(counter) + '.h'
|
|
|
|
|
|
|
|
peripheral_source_code_cache[key] = filename
|
|
|
|
|
|
|
|
with open(config.system_dir + 'STM32' + series + '/stm32_chip/' + filename, "w") as f:
|
|
|
|
f.write(source_code)
|
2017-04-09 21:53:07 -07:00
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
return filename
|
2017-04-09 21:53:07 -07:00
|
|
|
|
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
def generate_source_code(mcu):
|
|
|
|
source_code = '';
|
|
|
|
source_code = '//Autogenerated file\n'
|
|
|
|
source_code += '//MCU name: ' + mcu.mcu_name+ '\n'
|
|
|
|
source_code += '//MCU xml: ' + mcu.mcu_xml_filename+ '\n'
|
|
|
|
source_code += '//GPIO remap/alternate function xml: ' + mcu.remap_xml_filename+ '\n'
|
|
|
|
|
|
|
|
source_code += '\n'
|
|
|
|
|
|
|
|
gpio_pins = [pin for pin in mcu.pins if pin.startswith('P') and pin != 'PDR_ON']
|
|
|
|
|
|
|
|
source_code += '\n'
|
|
|
|
gpio_source_code = '// If you want to change the default pin order\n'
|
|
|
|
gpio_source_code = 'copy the pin list from below to the variant.h file\n'
|
|
|
|
gpio_source_code = '\n'
|
|
|
|
gpio_source_code += '#define VARIANT_PIN_LIST_DEFAULT \\\n'
|
|
|
|
|
|
|
|
mcu.default_pin_list = '#define CHIP_PIN_LIST';
|
|
|
|
for pin in sorted(gpio_pins, key=lambda x: (x[0:2], int(x[2:]))):
|
|
|
|
gpio_source_code += ' PIN(' + pin[1] + ',' + pin[2:] + '), \\\n'
|
|
|
|
mcu.default_pin_list += ' PIN(' + pin[1] + ',' + pin[2:] + '),'
|
|
|
|
mcu.default_pin_list += '\n'
|
|
|
|
|
|
|
|
gpio_source_code += '\n'
|
|
|
|
|
|
|
|
include = get_cached_code_include(mcu, 'GPIO', gpio_source_code)
|
|
|
|
source_code += '#include "' + include + '"\n'
|
|
|
|
source_code += '\n'
|
|
|
|
|
|
|
|
include_source_code = ''
|
|
|
|
|
|
|
|
for periph in sorted(mcu.peripheral_to_signals):
|
2017-05-30 08:31:24 -07:00
|
|
|
periph_source_code = "";
|
2017-07-15 09:09:28 -07:00
|
|
|
if periph == 'UART':
|
|
|
|
continue
|
2017-04-15 01:25:11 -07:00
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
if 'SDIO' in periph:
|
|
|
|
periph_source_code += '#define STM32_CHIP_HAS_SDIO'
|
2017-05-30 08:31:24 -07:00
|
|
|
periph_source_code += '\n'
|
2017-07-15 09:09:28 -07:00
|
|
|
if 'I2S' in periph:
|
|
|
|
periph_source_code += '#define STM32_CHIP_HAS_I2S'
|
2017-05-30 08:31:24 -07:00
|
|
|
periph_source_code += '\n'
|
2017-07-15 09:49:08 -07:00
|
|
|
|
|
|
|
if periph == 'TIM':
|
|
|
|
old = False
|
|
|
|
|
|
|
|
all_timers_source_code = ''
|
|
|
|
|
|
|
|
for i in range(1, 19):
|
|
|
|
instance = 'TIM' + str(i)
|
|
|
|
|
|
|
|
periph_source_code += '\n'
|
|
|
|
periph_source_code += 'const stm32_tim_pin_list_type chip_' + instance.lower() + ' [] = {\n'
|
|
|
|
|
|
|
|
for periph_signal in sorted(set(mcu.peripheral_signal_to_instance_signals)):
|
|
|
|
|
|
|
|
for signal in sorted(set(mcu.peripheral_signal_to_instance_signals[periph_signal])):
|
|
|
|
|
|
|
|
for pin in sorted(set(mcu.instance_signal_to_pin_to_af[signal]), key = natural_sort_key):
|
|
|
|
|
|
|
|
p = signal.split('_')[0]
|
|
|
|
sig = signal.split('_')[1]
|
|
|
|
|
|
|
|
if p != instance:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if sig not in ('BKIN', 'BKIN2', 'CH1', 'CH1N', 'CH2', 'CH2N', 'CH3', 'CH3N', 'CH4', 'CH4N', 'ETR'):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if pin in mcu.pins:
|
|
|
|
remap = mcu.instance_signal_to_pin_to_af[signal][pin]
|
|
|
|
mcu.af_function_used.append(remap)
|
|
|
|
|
|
|
|
split = signal.split('_')
|
|
|
|
p = split[0]
|
|
|
|
|
|
|
|
p = p.replace('I2S', 'SPI');
|
|
|
|
|
|
|
|
timer_line = ' { ' + p.ljust(6) + ',GPIO' + pin[1:2] + ', GPIO_PIN_' + pin[2:].ljust(3) + ',TIM_' + sig.ljust(7) + ', ' + remap.ljust(15) + '}, \n'
|
|
|
|
|
|
|
|
periph_source_code += timer_line
|
|
|
|
|
|
|
|
all_timers_source_code += timer_line
|
|
|
|
|
|
|
|
periph_source_code += '}; \n'
|
|
|
|
|
|
|
|
periph_source_code += '\n'
|
|
|
|
periph_source_code += 'const stm32_tim_pin_list_type chip_tim [] = {\n'
|
|
|
|
periph_source_code += all_timers_source_code
|
|
|
|
periph_source_code += '}; \n'
|
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
for sig in sorted(set(mcu.peripheral_to_signals[periph])):
|
2017-04-15 01:25:11 -07:00
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
if 'SDIO' not in periph and 'I2S' not in periph and sig not in ['SDA', 'SCL', 'TX', 'RX', 'MISO', 'MOSI', 'SCK', 'NSS']:
|
2017-04-15 01:25:11 -07:00
|
|
|
continue
|
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
if 'I2S' == periph and sig == 'CKIN':
|
2017-04-21 22:54:01 -07:00
|
|
|
continue
|
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
if 'CAN' == periph:
|
2017-04-11 21:15:09 -07:00
|
|
|
continue
|
2017-07-15 09:09:28 -07:00
|
|
|
|
|
|
|
periph_source_code += '\n'
|
|
|
|
periph_source_code += 'const stm32_af_pin_list_type chip_af_'+(periph + '_' + sig).lower()+' [] = {\n'
|
|
|
|
#print mcu.group_signals
|
|
|
|
old = False
|
|
|
|
|
|
|
|
signals = mcu.peripheral_signal_to_instance_signals[periph + '_' +sig]
|
|
|
|
if periph == 'USART' and 'UART' in mcu.peripheral_to_signals:
|
|
|
|
signals.extend(mcu.peripheral_signal_to_instance_signals['UART_' +sig])
|
|
|
|
|
|
|
|
for signal in sorted(set(signals)):
|
|
|
|
if old != signal.split('_')[0]:
|
|
|
|
periph_source_code += '//' + signal.split('_')[0] + '\n'
|
|
|
|
p = signal.split('_')[0]
|
|
|
|
for pin in sorted(set(mcu.instance_signal_to_pin_to_af[signal]), key = natural_sort_key):
|
|
|
|
if pin in mcu.pins:
|
|
|
|
remap = mcu.instance_signal_to_pin_to_af[signal][pin]
|
|
|
|
mcu.af_function_used.append(remap)
|
|
|
|
|
|
|
|
split = signal.split('_')
|
|
|
|
p = split[0]
|
|
|
|
|
|
|
|
p = p.replace('I2S', 'SPI');
|
|
|
|
|
|
|
|
periph_source_code += ' { ' + p.ljust(6) + ', GPIO' + pin[1:2] + ', GPIO_PIN_' + pin[2:].ljust(3) + ', ' + remap.ljust(15) + '}, \n'
|
|
|
|
|
|
|
|
periph_source_code += '}; \n'
|
|
|
|
|
|
|
|
if periph_source_code:
|
|
|
|
include = get_cached_code_include(mcu, periph, periph_source_code)
|
|
|
|
include_source_code += '#include "' + include + '"\n'
|
|
|
|
include_source_code += '\n'
|
|
|
|
|
|
|
|
remap_source_code = ''
|
|
|
|
|
|
|
|
if len(mcu.af_functions) > 0:
|
|
|
|
remap_source_code += 'static void AF_NO_REMAP (void) { }\n'
|
|
|
|
|
|
|
|
for af_function in mcu.af_functions:
|
|
|
|
if af_function in mcu.af_function_used:
|
|
|
|
remap_source_code += '#ifdef ' + af_function[2:] + '\n'
|
|
|
|
remap_source_code += 'static void ' + af_function + '(void) { ' + af_function[2:] + '(); }\n'
|
|
|
|
remap_source_code += '#else\n'
|
|
|
|
remap_source_code += 'static void ' + af_function + '(void) { AF_NO_REMAP(); }\n'
|
|
|
|
remap_source_code += '#endif\n'
|
|
|
|
remap_source_code += '\n'
|
|
|
|
|
|
|
|
include = get_cached_code_include(mcu, 'REMAP', remap_source_code)
|
|
|
|
source_code += '#include "' + include + '"\n'
|
|
|
|
source_code += '\n'
|
|
|
|
|
|
|
|
source_code += include_source_code
|
|
|
|
|
|
|
|
adc_source_code = 'const stm32_chip_adc1_channel_type chip_adc1_channel[] = {\n'
|
|
|
|
|
|
|
|
for instance_signal in sorted(mcu.instance_signal_to_default_pin.keys(), key = natural_sort_key):
|
2017-07-16 09:17:13 -07:00
|
|
|
if 'ADC' not in instance_signal or '_IN' not in instance_signal:
|
2017-07-15 09:09:28 -07:00
|
|
|
continue
|
2017-04-11 21:15:09 -07:00
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
(instance, channel) = instance_signal.split('_', 1)
|
|
|
|
if not channel[2:].isdigit():
|
|
|
|
continue
|
2017-04-11 21:15:09 -07:00
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
pin = mcu.instance_signal_to_default_pin[instance_signal]
|
|
|
|
adc_source_code += ' { ' + instance + ', GPIO' + pin[1:2] + ', GPIO_PIN_' + pin[2:].ljust(3) + ', ADC_CHANNEL_' + channel[2:].ljust(3) + '}, \n'
|
|
|
|
|
|
|
|
adc_source_code += '};\n'
|
|
|
|
|
|
|
|
include = get_cached_code_include(mcu, 'ADC', adc_source_code)
|
|
|
|
source_code += '#include "' + include + '"\n'
|
|
|
|
source_code += '\n'
|
|
|
|
|
|
|
|
|
|
|
|
clock_source_code = 'const stm32_clock_freq_list_type chip_clock_freq_list[] = {\n'
|
|
|
|
for periph in sorted(mcu.PCLK1_PERIPHERALS):
|
|
|
|
if not periph.startswith('USART') and not periph.startswith('SPI') and not periph.startswith('I2C'):
|
|
|
|
continue
|
2017-04-09 21:53:07 -07:00
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
if periph not in mcu.instances:
|
|
|
|
continue
|
2017-04-09 21:53:07 -07:00
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
clock_source_code += ' {' + periph.ljust(6) + ', HAL_RCC_GetPCLK1Freq }, \n'
|
|
|
|
|
|
|
|
clock_source_code += '\n'
|
|
|
|
|
|
|
|
for periph in sorted(mcu.PCLK2_PERIPHERALS):
|
|
|
|
if not periph.startswith('USART') and not periph.startswith('SPI') and not periph.startswith('I2C'):
|
|
|
|
continue
|
2017-04-09 21:53:07 -07:00
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
if periph not in mcu.instances:
|
|
|
|
continue
|
|
|
|
|
|
|
|
clock_source_code += ' {' + periph.ljust(6) + ', HAL_RCC_GetPCLK2Freq }, \n'
|
|
|
|
|
|
|
|
clock_source_code += '};\n'
|
|
|
|
|
|
|
|
include = get_cached_code_include(mcu, 'FREQ', clock_source_code)
|
|
|
|
source_code += '#include "' + include + '"\n'
|
|
|
|
|
|
|
|
source_code += '\n'
|
|
|
|
|
|
|
|
dir = config.system_dir + name[:7] + "/stm32_chip/"
|
|
|
|
with open(dir + 'stm32_' + mcu.mcu_name + '.h', 'w') as file:
|
|
|
|
file.write(source_code)
|
|
|
|
|
2017-04-09 21:53:07 -07:00
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
with open(config.stm32_dir + 'stm32_build_defines.h', 'w') as file:
|
2017-04-09 21:53:07 -07:00
|
|
|
file.write('//Autogenerated file\n')
|
|
|
|
file.write('\n')
|
|
|
|
file.write('#ifndef STM32_BUILD_DEFINES_H\n')
|
|
|
|
file.write('#define STM32_BUILD_DEFINES_H\n')
|
|
|
|
file.write('\n')
|
|
|
|
|
|
|
|
name_processed = []
|
2017-04-10 21:30:57 -07:00
|
|
|
|
|
|
|
file.write('#if __IGNORE\n')
|
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
for mcu in chip.mcus:
|
2017-04-09 21:53:07 -07:00
|
|
|
name = mcu.attrib['RefName']
|
|
|
|
name = name[:11]
|
|
|
|
|
|
|
|
if name in name_processed:
|
|
|
|
continue
|
2017-07-15 09:09:28 -07:00
|
|
|
|
|
|
|
print name
|
|
|
|
|
2017-04-09 21:53:07 -07:00
|
|
|
name_processed.append(name)
|
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
dir = config.system_dir + name[:7] + "/CMSIS_Inc/"
|
2017-04-09 21:53:07 -07:00
|
|
|
#filename = name.lower()
|
2017-07-15 09:09:28 -07:00
|
|
|
define = chip.get_productline_for_include(name)
|
2017-05-30 08:11:26 -07:00
|
|
|
if not define:
|
|
|
|
continue
|
2017-04-09 21:53:07 -07:00
|
|
|
|
|
|
|
file.write('\n');
|
2017-04-10 21:30:57 -07:00
|
|
|
file.write('#elif defined(' + name + ')\n')
|
|
|
|
file.write(' #define ' + define + ' 1\n')
|
2017-04-09 21:53:07 -07:00
|
|
|
file.write(' #define CMSIS_STARTUP_ASSEMBLY "startup_' + define.lower() + '.s"\n')
|
|
|
|
file.write(' #define CHIP_PERIPHERAL_INCLUDE "stm32_' + name + '.h"\n')
|
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
mcu = chip.load_mcu(name)
|
|
|
|
generate_source_code(mcu)
|
2017-05-30 20:51:21 -07:00
|
|
|
|
|
|
|
file.write(' ' + mcu.default_pin_list)
|
|
|
|
|
2017-04-10 21:30:57 -07:00
|
|
|
file.write('#else \n')
|
|
|
|
file.write('#error UNKNOWN CHIP \n')
|
|
|
|
file.write('#endif\n')
|
|
|
|
|
2017-04-09 21:53:07 -07:00
|
|
|
file.write('\n')
|
|
|
|
file.write('#endif')
|
|
|
|
|
|
|
|
|
2017-07-15 09:09:28 -07:00
|
|
|
|