Skip to content

Commit 11115e4

Browse files
dhylandsdpgeorge
authored andcommitted
stmhal: Add I2S support to make-pins.py
1 parent c91727b commit 11115e4

3 files changed

Lines changed: 81 additions & 6 deletions

File tree

stmhal/boards/make-pins.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,21 @@
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+
1929
def 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

4578
class 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:

stmhal/boards/stm32f4xx_prefix.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
.af_fn = (af_ptr) \
1818
}
1919

20-
#define PIN(p_port, p_pin, p_num_af, p_af, p_adc_num, p_adc_channel) \
20+
#define PIN(p_port, p_pin, p_af, p_adc_num, p_adc_channel) \
2121
{ \
2222
{ &pin_type }, \
2323
.name = MP_QSTR_ ## p_port ## p_pin, \
2424
.port = PORT_ ## p_port, \
2525
.pin = (p_pin), \
26-
.num_af = (p_num_af), \
26+
.num_af = (sizeof(p_af) / sizeof(pin_obj_t)), \
2727
.pin_mask = (1 << ((p_pin) & 0x0f)), \
2828
.gpio = GPIO ## p_port, \
2929
.af = p_af, \

stmhal/pin_defs_stmhal.h

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ enum {
4545
AF_FN_I2C,
4646
AF_FN_USART,
4747
AF_FN_UART = AF_FN_USART,
48-
AF_FN_SPI
48+
AF_FN_SPI,
49+
AF_FN_I2S,
4950
};
5051

5152
enum {
@@ -77,20 +78,43 @@ enum {
7778
AF_PIN_TYPE_SPI_MISO,
7879
AF_PIN_TYPE_SPI_SCK,
7980
AF_PIN_TYPE_SPI_NSS,
81+
82+
AF_PIN_TYPE_I2S_CK = 0,
83+
AF_PIN_TYPE_I2S_MCK,
84+
AF_PIN_TYPE_I2S_SD,
85+
AF_PIN_TYPE_I2S_WS,
86+
AF_PIN_TYPE_I2S_EXTSD,
8087
};
8188

89+
// The HAL uses a slightly different naming than we chose, so we provide
90+
// some #defines to massage things. Also I2S and SPI share the same
91+
// peripheral.
92+
93+
#define GPIO_AF5_I2S2 GPIO_AF5_SPI2
94+
#define GPIO_AF5_I2S3 GPIO_AF5_I2S3ext
95+
#define GPIO_AF6_I2S2 GPIO_AF6_I2S2ext
96+
#define GPIO_AF6_I2S3 GPIO_AF6_SPI3
97+
#define GPIO_AF7_I2S2 GPIO_AF7_SPI2
98+
#define GPIO_AF7_I2S3 GPIO_AF7_I2S3ext
99+
100+
#define I2S2 SPI2
101+
#define I2S3 SPI3
102+
82103
enum {
83104
PIN_ADC1 = (1 << 0),
84105
PIN_ADC2 = (1 << 1),
85106
PIN_ADC3 = (1 << 2),
86107
};
87108

109+
// Note that SPI and I2S are really the same peripheral as far as the HAL
110+
// is concerned, so there is no I2S_TypeDef.
88111
#define PIN_DEFS_PORT_AF_UNION \
89112
TIM_TypeDef *TIM; \
90113
I2C_TypeDef *I2C; \
91114
USART_TypeDef *USART; \
92115
USART_TypeDef *UART; \
93-
SPI_TypeDef *SPI;
116+
SPI_TypeDef *SPI;\
117+
SPI_TypeDef *I2S;
94118

95119
typedef GPIO_TypeDef pin_gpio_t;
96120

0 commit comments

Comments
 (0)