1111 'TIM' : ['CH1' , 'CH2' , 'CH3' , 'CH4' ,
1212 'CH1N' , 'CH2N' , 'CH3N' , 'CH1_ETR' , 'ETR' , 'BKIN' ],
1313 'I2C' : ['SDA' , 'SCL' ],
14+ 'I2S' : ['CK' , 'MCK' , 'SD' , 'WS' , 'EXTSD' ],
1415 'USART' : ['RX' , 'TX' , 'CTS' , 'RTS' , 'CK' ],
1516 'UART' : ['RX' , 'TX' , 'CTS' , 'RTS' ],
1617 'SPI' : ['NSS' , 'SCK' , 'MISO' , 'MOSI' ]
1718}
1819
20+ CONDITIONAL_VAR = {
21+ 'I2C' : 'MICROPY_HW_I2C{num}_SCL' ,
22+ 'I2S' : 'MICROPY_HW_ENABLE_I2S{num}' ,
23+ 'SPI' : 'MICROPY_HW_ENABLE_SPI{num}' ,
24+ 'UART' : 'MICROPY_HW_UART{num}_PORT' ,
25+ 'UART5' : 'MICROPY_HW_UART5_TX_PORT' ,
26+ 'USART' : 'MICROPY_HW_UART{num}_PORT' ,
27+ }
28+
1929def parse_port_pin (name_str ):
2030 """Parses a string and returns a (port-num, pin-num) tuple."""
2131 if len (name_str ) < 3 :
@@ -41,12 +51,39 @@ def split_name_num(name_num):
4151 break
4252 return name , num
4353
54+ def conditional_var (name_num ):
55+ # Try the specific instance first. For example, if name_num is UART4_RX
56+ # then try UART4 first, and then try UART second.
57+ name , num = split_name_num (name_num )
58+ var = None
59+ if name_num in CONDITIONAL_VAR :
60+ var = CONDITIONAL_VAR [name_num ]
61+ elif name in CONDITIONAL_VAR :
62+ var = CONDITIONAL_VAR [name ]
63+ if var :
64+ return var .format (num = num )
65+
66+ def print_conditional_if (cond_var , file = None ):
67+ if cond_var :
68+ if cond_var .find ('ENABLE' ) >= 0 :
69+ print ('#if defined({0}) && {0}' .format (cond_var ), file = file )
70+ else :
71+ print ('#if defined({0})' .format (cond_var ), file = file )
72+
73+ def print_conditional_endif (cond_var , file = None ):
74+ if cond_var :
75+ print ('#endif' , file = file )
76+
4477
4578class AlternateFunction (object ):
4679 """Holds the information associated with a pins alternate function."""
4780
4881 def __init__ (self , idx , af_str ):
4982 self .idx = idx
83+ # Special case. We change I2S2ext_SD into I2S2_EXTSD so that it parses
84+ # the same way the other peripherals do.
85+ af_str = af_str .replace ('ext_' , '_EXT' )
86+
5087 self .af_str = af_str
5188
5289 self .func = ''
@@ -77,7 +114,10 @@ def mux_name(self):
77114
78115 def print (self ):
79116 """Prints the C representation of this AF."""
117+ cond_var = None
80118 if self .supported :
119+ cond_var = conditional_var ('{}{}' .format (self .func , self .fn_num ))
120+ print_conditional_if (cond_var )
81121 print (' AF' , end = '' )
82122 else :
83123 print (' //' , end = '' )
@@ -86,6 +126,7 @@ def print(self):
86126 fn_num = 0
87127 print ('({:2d}, {:8s}, {:2d}, {:10s}, {:8s}), // {:s}' .format (self .idx ,
88128 self .func , fn_num , self .pin_type , self .ptr (), self .af_str ))
129+ print_conditional_endif (cond_var )
89130
90131 def qstr_list (self ):
91132 return [self .mux_name ()]
@@ -163,9 +204,9 @@ def print(self):
163204 print ("// " , end = '' )
164205 print ('};' )
165206 print ('' )
166- print ('const pin_obj_t pin_{:s} = PIN({:s}, {:d}, {:d}, {: s}, {:s}, {:d});' .format (
207+ print ('const pin_obj_t pin_{:s} = PIN({:s}, {:d}, {:s}, {:s}, {:d});' .format (
167208 self .cpu_pin_name (), self .port_letter (), self .pin ,
168- self .alt_fn_count , self . alt_fn_name (null_if_0 = True ),
209+ self .alt_fn_name (null_if_0 = True ),
169210 self .adc_num_str (), self .adc_channel ))
170211 print ('' )
171212
@@ -297,7 +338,13 @@ def print_qstr(self, qstr_filename):
297338 for named_pin in self .board_pins :
298339 qstr_set |= set ([named_pin .name ()])
299340 for qstr in sorted (qstr_set ):
341+ cond_var = None
342+ if qstr .startswith ('AF' ):
343+ af_words = qstr .split ('_' )
344+ cond_var = conditional_var (af_words [1 ])
345+ print_conditional_if (cond_var , file = qstr_file )
300346 print ('Q({})' .format (qstr ), file = qstr_file )
347+ print_conditional_endif (cond_var , file = qstr_file )
301348
302349 def print_af_hdr (self , af_const_filename ):
303350 with open (af_const_filename , 'wt' ) as af_const_file :
@@ -313,10 +360,14 @@ def print_af_hdr(self, af_const_filename):
313360 if len (mux_name ) > mux_name_width :
314361 mux_name_width = len (mux_name )
315362 for mux_name in sorted (af_hdr_set ):
363+ af_words = mux_name .split ('_' ) # ex mux_name: AF9_I2C2
364+ cond_var = conditional_var (af_words [1 ])
365+ print_conditional_if (cond_var , file = af_const_file )
316366 key = 'MP_OBJ_NEW_QSTR(MP_QSTR_{}),' .format (mux_name )
317367 val = 'MP_OBJ_NEW_SMALL_INT(GPIO_{})' .format (mux_name )
318368 print (' { %-*s %s },' % (mux_name_width + 26 , key , val ),
319369 file = af_const_file )
370+ print_conditional_endif (cond_var , file = af_const_file )
320371
321372 def print_af_py (self , af_py_filename ):
322373 with open (af_py_filename , 'wt' ) as af_py_file :
0 commit comments