@@ -71,7 +71,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
71
71
if (!have_tx && !have_rx ) {
72
72
mp_raise_ValueError ("tx and rx cannot both be None" );
73
73
}
74
-
74
+
75
75
self -> baudrate = baudrate ;
76
76
self -> character_bits = bits ;
77
77
self -> timeout_ms = timeout ;
@@ -82,10 +82,12 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
82
82
for (int i = 0 ; i < NUM_SERCOMS_PER_PIN ; i ++ ) {
83
83
Sercom * potential_sercom = NULL ;
84
84
if (have_tx ) {
85
- potential_sercom = tx -> sercom [i ].sercom ;
86
85
sercom_index = tx -> sercom [i ].index ;
87
- if (potential_sercom == NULL ||
88
- potential_sercom -> USART .CTRLA .bit .ENABLE != 0 ||
86
+ if (sercom_index >= SERCOM_INST_NUM ) {
87
+ continue ;
88
+ }
89
+ potential_sercom = sercom_insts [sercom_index ];
90
+ if (potential_sercom -> USART .CTRLA .bit .ENABLE != 0 ||
89
91
!(tx -> sercom [i ].pad == 0 ||
90
92
tx -> sercom [i ].pad == 2 )) {
91
93
continue ;
@@ -98,12 +100,13 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
98
100
}
99
101
}
100
102
for (int j = 0 ; j < NUM_SERCOMS_PER_PIN ; j ++ ) {
101
- if (((!have_tx && rx -> sercom [j ].sercom -> USART .CTRLA .bit .ENABLE == 0 ) ||
102
- potential_sercom == rx -> sercom [j ].sercom ) &&
103
+ if (((!have_tx && rx -> sercom [j ].index < SERCOM_INST_NUM &&
104
+ sercom_insts [rx -> sercom [j ].index ]-> USART .CTRLA .bit .ENABLE == 0 ) ||
105
+ sercom_index == rx -> sercom [j ].index ) &&
103
106
rx -> sercom [j ].pad != tx_pad ) {
104
107
rx_pinmux = PINMUX (rx -> pin , (j == 0 ) ? MUX_C : MUX_D );
105
108
rx_pad = rx -> sercom [j ].pad ;
106
- sercom = rx -> sercom [j ].sercom ;
109
+ sercom = sercom_insts [ rx -> sercom [j ].index ] ;
107
110
sercom_index = rx -> sercom [j ].index ;
108
111
break ;
109
112
}
@@ -147,7 +150,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
147
150
// usart_async_init() sets a number of defaults based on a prototypical SERCOM
148
151
// which don't necessarily match what we need. After calling it, set the values
149
152
// specific to this instantiation of UART.
150
-
153
+
151
154
// Set pads computed for this SERCOM.
152
155
// TXPO:
153
156
// 0x0: TX pad 0; no RTS/CTS
@@ -182,7 +185,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
182
185
// http://start.atmel.com/static/help/index.html?GUID-79201A5A-226F-4FBB-B0B8-AB0BE0554836
183
186
// Look at the ASFv4 code example for async USART.
184
187
usart_async_register_callback (usart_desc_p , USART_ASYNC_RXC_CB , usart_async_rxc_callback );
185
-
188
+
186
189
187
190
if (have_tx ) {
188
191
gpio_set_pin_direction (tx -> pin , GPIO_DIRECTION_OUT );
@@ -193,7 +196,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
193
196
} else {
194
197
self -> tx_pin = NO_PIN ;
195
198
}
196
-
199
+
197
200
if (have_rx ) {
198
201
gpio_set_pin_direction (rx -> pin , GPIO_DIRECTION_IN );
199
202
gpio_set_pin_pull_mode (rx -> pin , GPIO_PULL_OFF );
@@ -238,7 +241,7 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
238
241
// Nothing to read.
239
242
return 0 ;
240
243
}
241
-
244
+
242
245
struct io_descriptor * io ;
243
246
usart_async_get_io_descriptor (usart_desc_p , & io );
244
247
@@ -266,7 +269,7 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
266
269
MICROPY_VM_HOOK_LOOP
267
270
#endif
268
271
}
269
-
272
+
270
273
return total_read ;
271
274
}
272
275
@@ -305,7 +308,7 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
305
308
* errcode = MP_EAGAIN ;
306
309
return MP_STREAM_ERROR ;
307
310
}
308
-
311
+
309
312
struct usart_async_status async_status ;
310
313
// Could return ERR_BUSY, but if that's true there's already a problem.
311
314
usart_async_get_status (usart_desc_p , & async_status );
0 commit comments