diff options
Diffstat (limited to 'stmhal/boards/make-pins.py')
-rwxr-xr-x | stmhal/boards/make-pins.py | 55 |
1 files changed, 53 insertions, 2 deletions
diff --git a/stmhal/boards/make-pins.py b/stmhal/boards/make-pins.py index afb594e412..e7f16108bf 100755 --- a/stmhal/boards/make-pins.py +++ b/stmhal/boards/make-pins.py @@ -11,11 +11,21 @@ SUPPORTED_FN = { 'TIM' : ['CH1', 'CH2', 'CH3', 'CH4', 'CH1N', 'CH2N', 'CH3N', 'CH1_ETR', 'ETR', 'BKIN'], 'I2C' : ['SDA', 'SCL'], + 'I2S' : ['CK', 'MCK', 'SD', 'WS', 'EXTSD'], 'USART' : ['RX', 'TX', 'CTS', 'RTS', 'CK'], 'UART' : ['RX', 'TX', 'CTS', 'RTS'], 'SPI' : ['NSS', 'SCK', 'MISO', 'MOSI'] } +CONDITIONAL_VAR = { + 'I2C' : 'MICROPY_HW_I2C{num}_SCL', + 'I2S' : 'MICROPY_HW_ENABLE_I2S{num}', + 'SPI' : 'MICROPY_HW_ENABLE_SPI{num}', + 'UART' : 'MICROPY_HW_UART{num}_PORT', + 'UART5' : 'MICROPY_HW_UART5_TX_PORT', + 'USART' : 'MICROPY_HW_UART{num}_PORT', +} + def parse_port_pin(name_str): """Parses a string and returns a (port-num, pin-num) tuple.""" if len(name_str) < 3: @@ -41,12 +51,39 @@ def split_name_num(name_num): break return name, num +def conditional_var(name_num): + # Try the specific instance first. For example, if name_num is UART4_RX + # then try UART4 first, and then try UART second. + name, num = split_name_num(name_num) + var = None + if name_num in CONDITIONAL_VAR: + var = CONDITIONAL_VAR[name_num] + elif name in CONDITIONAL_VAR: + var = CONDITIONAL_VAR[name] + if var: + return var.format(num=num) + +def print_conditional_if(cond_var, file=None): + if cond_var: + if cond_var.find('ENABLE') >= 0: + print('#if defined({0}) && {0}'.format(cond_var), file=file) + else: + print('#if defined({0})'.format(cond_var), file=file) + +def print_conditional_endif(cond_var, file=None): + if cond_var: + print('#endif', file=file) + class AlternateFunction(object): """Holds the information associated with a pins alternate function.""" def __init__(self, idx, af_str): self.idx = idx + # Special case. We change I2S2ext_SD into I2S2_EXTSD so that it parses + # the same way the other peripherals do. + af_str = af_str.replace('ext_', '_EXT') + self.af_str = af_str self.func = '' @@ -77,7 +114,10 @@ class AlternateFunction(object): def print(self): """Prints the C representation of this AF.""" + cond_var = None if self.supported: + cond_var = conditional_var('{}{}'.format(self.func, self.fn_num)) + print_conditional_if(cond_var) print(' AF', end='') else: print(' //', end='') @@ -86,6 +126,7 @@ class AlternateFunction(object): fn_num = 0 print('({:2d}, {:8s}, {:2d}, {:10s}, {:8s}), // {:s}'.format(self.idx, self.func, fn_num, self.pin_type, self.ptr(), self.af_str)) + print_conditional_endif(cond_var) def qstr_list(self): return [self.mux_name()] @@ -163,9 +204,9 @@ class Pin(object): print("// ", end='') print('};') print('') - print('const pin_obj_t pin_{:s} = PIN({:s}, {:d}, {:d}, {:s}, {:s}, {:d});'.format( + print('const pin_obj_t pin_{:s} = PIN({:s}, {:d}, {:s}, {:s}, {:d});'.format( self.cpu_pin_name(), self.port_letter(), self.pin, - self.alt_fn_count, self.alt_fn_name(null_if_0=True), + self.alt_fn_name(null_if_0=True), self.adc_num_str(), self.adc_channel)) print('') @@ -297,7 +338,13 @@ class Pins(object): for named_pin in self.board_pins: qstr_set |= set([named_pin.name()]) for qstr in sorted(qstr_set): + cond_var = None + if qstr.startswith('AF'): + af_words = qstr.split('_') + cond_var = conditional_var(af_words[1]) + print_conditional_if(cond_var, file=qstr_file) print('Q({})'.format(qstr), file=qstr_file) + print_conditional_endif(cond_var, file=qstr_file) def print_af_hdr(self, af_const_filename): with open(af_const_filename, 'wt') as af_const_file: @@ -313,10 +360,14 @@ class Pins(object): if len(mux_name) > mux_name_width: mux_name_width = len(mux_name) for mux_name in sorted(af_hdr_set): + af_words = mux_name.split('_') # ex mux_name: AF9_I2C2 + cond_var = conditional_var(af_words[1]) + print_conditional_if(cond_var, file=af_const_file) key = 'MP_OBJ_NEW_QSTR(MP_QSTR_{}),'.format(mux_name) val = 'MP_OBJ_NEW_SMALL_INT(GPIO_{})'.format(mux_name) print(' { %-*s %s },' % (mux_name_width + 26, key, val), file=af_const_file) + print_conditional_endif(cond_var, file=af_const_file) def print_af_py(self, af_py_filename): with open(af_py_filename, 'wt') as af_py_file: |