1515#include "py/stream.h"
1616
1717#include "nrfx_uarte.h"
18- #include "nrf_gpio.h"
18+ #include <stdatomic.h>
1919#include <string.h>
2020
2121// expression to examine, and return value in case of failing
2222#define _VERIFY_ERR (_exp ) \
2323 do { \
2424 uint32_t _err = (_exp); \
25- if (NRFX_SUCCESS != _err) { \
25+ while (NRFX_SUCCESS != _err) { \
2626 mp_raise_msg_varg(&mp_type_RuntimeError, MP_ERROR_TEXT("error = 0x%08lX"), _err); \
2727 } \
2828 } while (0)
@@ -36,6 +36,17 @@ static nrfx_uarte_t nrfx_uartes[] = {
3636 #endif
3737};
3838
39+ static void * irq_handlers [] = {
40+ #if NRFX_CHECK (NRFX_UARTE0_ENABLED )
41+ NRFX_UARTE_INST_HANDLER_GET (0 ),
42+ #endif
43+ #if NRFX_CHECK (NRFX_UARTE1_ENABLED )
44+ NRFX_UARTE_INST_HANDLER_GET (1 ),
45+ #endif
46+ };
47+
48+
49+
3950static bool never_reset [NRFX_UARTE0_ENABLED + NRFX_UARTE1_ENABLED ];
4051
4152static uint32_t get_nrf_baud (uint32_t baudrate ) {
@@ -80,8 +91,8 @@ static void uart_callback_irq(const nrfx_uarte_event_t *event, void *context) {
8091
8192 switch (event -> type ) {
8293 case NRFX_UARTE_EVT_RX_DONE :
83- if (ringbuf_num_empty (& self -> ringbuf ) >= event -> data .rxtx . bytes ) {
84- ringbuf_put_n (& self -> ringbuf , event -> data .rxtx . p_data , event -> data .rxtx . bytes );
94+ if (ringbuf_num_empty (& self -> ringbuf ) >= event -> data .rx . length ) {
95+ ringbuf_put_n (& self -> ringbuf , event -> data .rx . p_buffer , event -> data .rx . length );
8596 // keep receiving
8697 (void )nrfx_uarte_rx (self -> uarte , & self -> rx_char , 1 );
8798 } else {
@@ -100,7 +111,7 @@ static void uart_callback_irq(const nrfx_uarte_event_t *event, void *context) {
100111 // Possible Error source is Overrun, Parity, Framing, Break
101112 // uint32_t errsrc = event->data.error.error_mask;
102113
103- ringbuf_put_n (& self -> ringbuf , event -> data .error .rxtx . p_data , event -> data .error .rxtx . bytes );
114+ ringbuf_put_n (& self -> ringbuf , event -> data .error .rx . p_buffer , event -> data .error .rx . length );
104115
105116 // Keep receiving
106117 (void )nrfx_uarte_rx (self -> uarte , & self -> rx_char , 1 );
@@ -151,9 +162,11 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
151162
152163 // Find a free UART peripheral.
153164 self -> uarte = NULL ;
165+ void * handler = NULL ;
154166 for (size_t i = 0 ; i < MP_ARRAY_SIZE (nrfx_uartes ); i ++ ) {
155167 if ((nrfx_uartes [i ].p_reg -> ENABLE & UARTE_ENABLE_ENABLE_Msk ) == 0 ) {
156168 self -> uarte = & nrfx_uartes [i ];
169+ handler = irq_handlers [i ];
157170 break ;
158171 }
159172 }
@@ -173,19 +186,25 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
173186 bool hwfc = rts != NULL || cts != NULL ;
174187
175188 nrfx_uarte_config_t config = {
176- .pseltxd = (tx == NULL ) ? NRF_UARTE_PSEL_DISCONNECTED : tx -> number ,
177- .pselrxd = (rx == NULL ) ? NRF_UARTE_PSEL_DISCONNECTED : rx -> number ,
178- .pselcts = (cts == NULL ) ? NRF_UARTE_PSEL_DISCONNECTED : cts -> number ,
179- .pselrts = (rts == NULL ) ? NRF_UARTE_PSEL_DISCONNECTED : rts -> number ,
189+ .txd_pin = (tx == NULL ) ? NRF_UARTE_PSEL_DISCONNECTED : tx -> number ,
190+ .rxd_pin = (rx == NULL ) ? NRF_UARTE_PSEL_DISCONNECTED : rx -> number ,
191+ .cts_pin = (cts == NULL ) ? NRF_UARTE_PSEL_DISCONNECTED : cts -> number ,
192+ .rts_pin = (rts == NULL ) ? NRF_UARTE_PSEL_DISCONNECTED : rts -> number ,
193+ .skip_gpio_cfg = false,
194+ .skip_psel_cfg = false,
180195 .p_context = self ,
181196 .baudrate = get_nrf_baud (baudrate ),
182197 .interrupt_priority = NRFX_UARTE_DEFAULT_CONFIG_IRQ_PRIORITY ,
183- .hal_cfg = {
198+ .config = {
184199 .hwfc = hwfc ? NRF_UARTE_HWFC_ENABLED : NRF_UARTE_HWFC_DISABLED ,
185200 .parity = (parity == BUSIO_UART_PARITY_NONE ) ? NRF_UARTE_PARITY_EXCLUDED : NRF_UARTE_PARITY_INCLUDED
186201 }
187202 };
188203
204+ // Manually connect the UART IRQ through Zephyr.
205+ irq_connect_dynamic (nrfx_get_irq_number (self -> uarte -> p_reg ), NRFX_UARTE_DEFAULT_CONFIG_IRQ_PRIORITY ,
206+ nrfx_isr , handler , 0 );
207+
189208 _VERIFY_ERR (nrfx_uarte_init (self -> uarte , & config , uart_callback_irq ));
190209
191210 // Init buffer for rx
@@ -337,7 +356,7 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
337356 RUN_BACKGROUND_TASKS ;
338357 }
339358
340- (* errcode ) = nrfx_uarte_tx (self -> uarte , tx_buf , len );
359+ (* errcode ) = nrfx_uarte_tx (self -> uarte , tx_buf , len , 0 );
341360 _VERIFY_ERR (* errcode );
342361 (* errcode ) = 0 ;
343362
0 commit comments