@@ -71,7 +71,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
7171 if (!have_tx && !have_rx ) {
7272 mp_raise_ValueError ("tx and rx cannot both be None" );
7373 }
74-
74+
7575 self -> baudrate = baudrate ;
7676 self -> character_bits = bits ;
7777 self -> timeout_ms = timeout ;
@@ -82,10 +82,12 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
8282 for (int i = 0 ; i < NUM_SERCOMS_PER_PIN ; i ++ ) {
8383 Sercom * potential_sercom = NULL ;
8484 if (have_tx ) {
85- potential_sercom = tx -> sercom [i ].sercom ;
8685 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 ||
8991 !(tx -> sercom [i ].pad == 0 ||
9092 tx -> sercom [i ].pad == 2 )) {
9193 continue ;
@@ -98,12 +100,13 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
98100 }
99101 }
100102 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 ) &&
103106 rx -> sercom [j ].pad != tx_pad ) {
104107 rx_pinmux = PINMUX (rx -> pin , (j == 0 ) ? MUX_C : MUX_D );
105108 rx_pad = rx -> sercom [j ].pad ;
106- sercom = rx -> sercom [j ].sercom ;
109+ sercom = sercom_insts [ rx -> sercom [j ].index ] ;
107110 sercom_index = rx -> sercom [j ].index ;
108111 break ;
109112 }
@@ -147,7 +150,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
147150 // usart_async_init() sets a number of defaults based on a prototypical SERCOM
148151 // which don't necessarily match what we need. After calling it, set the values
149152 // specific to this instantiation of UART.
150-
153+
151154 // Set pads computed for this SERCOM.
152155 // TXPO:
153156 // 0x0: TX pad 0; no RTS/CTS
@@ -182,7 +185,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
182185 // http://start.atmel.com/static/help/index.html?GUID-79201A5A-226F-4FBB-B0B8-AB0BE0554836
183186 // Look at the ASFv4 code example for async USART.
184187 usart_async_register_callback (usart_desc_p , USART_ASYNC_RXC_CB , usart_async_rxc_callback );
185-
188+
186189
187190 if (have_tx ) {
188191 gpio_set_pin_direction (tx -> pin , GPIO_DIRECTION_OUT );
@@ -193,7 +196,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
193196 } else {
194197 self -> tx_pin = NO_PIN ;
195198 }
196-
199+
197200 if (have_rx ) {
198201 gpio_set_pin_direction (rx -> pin , GPIO_DIRECTION_IN );
199202 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
238241 // Nothing to read.
239242 return 0 ;
240243 }
241-
244+
242245 struct io_descriptor * io ;
243246 usart_async_get_io_descriptor (usart_desc_p , & io );
244247
@@ -266,7 +269,7 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
266269 MICROPY_VM_HOOK_LOOP
267270#endif
268271 }
269-
272+
270273 return total_read ;
271274}
272275
@@ -305,7 +308,7 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
305308 * errcode = MP_EAGAIN ;
306309 return MP_STREAM_ERROR ;
307310 }
308-
311+
309312 struct usart_async_status async_status ;
310313 // Could return ERR_BUSY, but if that's true there's already a problem.
311314 usart_async_get_status (usart_desc_p , & async_status );
0 commit comments