From d092722ae83f03f0da7a77b525037cf279465112 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 28 Dec 2018 00:39:33 +0700 Subject: [PATCH 1/2] fix #1407 keep receiving in case of error --- ports/nrf/common-hal/busio/UART.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ports/nrf/common-hal/busio/UART.c b/ports/nrf/common-hal/busio/UART.c index a291af5788..c200b6e6f8 100644 --- a/ports/nrf/common-hal/busio/UART.c +++ b/ports/nrf/common-hal/busio/UART.c @@ -81,7 +81,7 @@ static void uart_callback_irq (const nrfx_uarte_event_t * event, void * context) } // keep receiving - _VERIFY_ERR(nrfx_uarte_rx(&self->uarte, &self->rx_char, 1)); + (void) nrfx_uarte_rx(&self->uarte, &self->rx_char, 1); break; case NRFX_UARTE_EVT_TX_DONE: @@ -89,7 +89,11 @@ static void uart_callback_irq (const nrfx_uarte_event_t * event, void * context) break; case NRFX_UARTE_EVT_ERROR: - // Handle error + // Possible Error source is Overrun, Parity, Framing, Break + // uint32_t errsrc = event->data.error.error_mask; + + // Keep receiving + (void) nrfx_uarte_rx(&self->uarte, &self->rx_char, 1); break; default: From 3ee766bc013aa3b3bb03606f6a0df03d8cf260e5 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 28 Dec 2018 01:05:30 +0700 Subject: [PATCH 2/2] put received bytes to fifo when error --- ports/nrf/common-hal/busio/UART.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/ports/nrf/common-hal/busio/UART.c b/ports/nrf/common-hal/busio/UART.c index c200b6e6f8..912956369c 100644 --- a/ports/nrf/common-hal/busio/UART.c +++ b/ports/nrf/common-hal/busio/UART.c @@ -67,18 +67,24 @@ static void ringbuf_clear(ringbuf_t *r) r->iput = r->iget = 0; } +// will overwrite old data +static void ringbuf_put_n(ringbuf_t* r, uint8_t* buf, uint8_t bufsize) +{ + for(uint8_t i=0; i < bufsize; i++) { + if ( ringbuf_put(r, buf[i]) < 0 ) { + // if full overwrite old data + (void) ringbuf_get(r); + ringbuf_put(r, buf[i]); + } + } +} + static void uart_callback_irq (const nrfx_uarte_event_t * event, void * context) { busio_uart_obj_t* self = (busio_uart_obj_t*) context; switch ( event->type ) { case NRFX_UARTE_EVT_RX_DONE: - for(uint8_t i=0; i < event->data.rxtx.bytes; i++) { - if ( ringbuf_put(&self->rbuf, event->data.rxtx.p_data[i]) < 0 ) { - // if full overwrite old data - (void) ringbuf_get(&self->rbuf); - ringbuf_put(&self->rbuf, event->data.rxtx.p_data[i]); - } - } + ringbuf_put_n(&self->rbuf, event->data.rxtx.p_data, event->data.rxtx.bytes); // keep receiving (void) nrfx_uarte_rx(&self->uarte, &self->rx_char, 1); @@ -92,6 +98,8 @@ static void uart_callback_irq (const nrfx_uarte_event_t * event, void * context) // Possible Error source is Overrun, Parity, Framing, Break // uint32_t errsrc = event->data.error.error_mask; + ringbuf_put_n(&self->rbuf, event->data.error.rxtx.p_data, event->data.error.rxtx.bytes); + // Keep receiving (void) nrfx_uarte_rx(&self->uarte, &self->rx_char, 1); break;