From c7dc5f2507fbcca8215823744607008ccc3759cc Mon Sep 17 00:00:00 2001 From: Bernhard Boser Date: Thu, 7 Jan 2021 14:13:19 -0800 Subject: [PATCH 1/7] support RTS/CTS hardware flow control --- ports/nrf/common-hal/busio/UART.c | 59 ++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/ports/nrf/common-hal/busio/UART.c b/ports/nrf/common-hal/busio/UART.c index 012ebc3b5e..625807d866 100644 --- a/ports/nrf/common-hal/busio/UART.c +++ b/ports/nrf/common-hal/busio/UART.c @@ -36,6 +36,7 @@ #include "supervisor/shared/translate.h" #include "nrfx_uarte.h" +#include "nrf_gpio.h" #include // expression to examine, and return value in case of failing @@ -98,10 +99,16 @@ static void uart_callback_irq (const nrfx_uarte_event_t * event, void * context) switch ( event->type ) { case NRFX_UARTE_EVT_RX_DONE: - ringbuf_put_n(&self->ringbuf, event->data.rxtx.p_data, event->data.rxtx.bytes); + if (ringbuf_num_empty(&self->ringbuf) >= event->data.rxtx.bytes) { + ringbuf_put_n(&self->ringbuf, event->data.rxtx.p_data, event->data.rxtx.bytes); + // keep receiving + (void) nrfx_uarte_rx(self->uarte, &self->rx_char, 1); + } else { + // receive buffer full, suspend + self->rx_paused = true; + nrf_gpio_pin_write(self->rts_pin_number, true); + } - // keep receiving - (void) nrfx_uarte_rx(self->uarte, &self->rx_char, 1); break; case NRFX_UARTE_EVT_TX_DONE: @@ -137,8 +144,8 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self, mp_float_t timeout, uint16_t receiver_buffer_size, byte* receiver_buffer, bool sigint_enabled) { - if ((rts != NULL) || (cts != NULL) || (rs485_dir != NULL) || (rs485_invert)) { - mp_raise_ValueError(translate("RTS/CTS/RS485 Not yet supported on this device")); + if ((rs485_dir != NULL) || (rs485_invert)) { + mp_raise_ValueError(translate("RS485 Not yet supported on this device")); } // Find a free UART peripheral. @@ -166,16 +173,18 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self, mp_raise_ValueError(translate("Odd parity is not supported")); } + bool hwfc = rts != NULL || cts != NULL; + nrfx_uarte_config_t config = { .pseltxd = (tx == NULL) ? NRF_UARTE_PSEL_DISCONNECTED : tx->number, .pselrxd = (rx == NULL) ? NRF_UARTE_PSEL_DISCONNECTED : rx->number, - .pselcts = NRF_UARTE_PSEL_DISCONNECTED, - .pselrts = NRF_UARTE_PSEL_DISCONNECTED, + .pselcts = (cts == NULL) ? NRF_UARTE_PSEL_DISCONNECTED : cts->number, + .pselrts = (rts == NULL) ? NRF_UARTE_PSEL_DISCONNECTED : rts->number, .p_context = self, .baudrate = get_nrf_baud(baudrate), .interrupt_priority = 7, .hal_cfg = { - .hwfc = NRF_UARTE_HWFC_DISABLED, + .hwfc = hwfc ? NRF_UARTE_HWFC_ENABLED : NRF_UARTE_HWFC_DISABLED, .parity = (parity == BUSIO_UART_PARITY_NONE) ? NRF_UARTE_PARITY_EXCLUDED : NRF_UARTE_PARITY_INCLUDED } }; @@ -207,9 +216,25 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self, self->tx_pin_number = NO_PIN; } + if ( rts != NULL ) { + self->rts_pin_number = rts->number; + claim_pin(rts); + } else { + self->rts_pin_number = NO_PIN; + } + + if ( cts != NULL ) { + self->cts_pin_number = cts->number; + claim_pin(cts); + } else { + self->cts_pin_number = NO_PIN; + } + self->baudrate = baudrate; self->timeout_ms = timeout * 1000; + self->rx_paused = false; + // Initial wait for incoming byte _VERIFY_ERR(nrfx_uarte_rx(self->uarte, &self->rx_char, 1)); } @@ -223,8 +248,12 @@ void common_hal_busio_uart_deinit(busio_uart_obj_t *self) { nrfx_uarte_uninit(self->uarte); reset_pin_number(self->tx_pin_number); reset_pin_number(self->rx_pin_number); + reset_pin_number(self->rts_pin_number); + reset_pin_number(self->cts_pin_number); self->tx_pin_number = NO_PIN; self->rx_pin_number = NO_PIN; + self->rts_pin_number = NO_PIN; + self->cts_pin_number = NO_PIN; ringbuf_free(&self->ringbuf); } } @@ -237,6 +266,10 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t uint64_t start_ticks = supervisor_ticks_ms64(); + if (len > ringbuf_capacity(&self->ringbuf)) { + mp_raise_ValueError(translate("Reading >receiver_buffer_size bytes is not supported")); + } + // Wait for all bytes received or timeout while ( (ringbuf_num_filled(&self->ringbuf) < len) && (supervisor_ticks_ms64() - start_ticks < self->timeout_ms) ) { RUN_BACKGROUND_TASKS; @@ -252,6 +285,16 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t // Copy as much received data as available, up to len bytes. size_t rx_bytes = ringbuf_get_n(&self->ringbuf, data, len); + // restart reader, if stopped + if (self->rx_paused) { + // the character that did not fit in ringbuf is in rx_char + ringbuf_put_n(&self->ringbuf, &self->rx_char, 1); + // keep receiving + (void) nrfx_uarte_rx(self->uarte, &self->rx_char, 1); + nrf_gpio_pin_write(self->rts_pin_number, false); + self->rx_paused = false; + } + NVIC_EnableIRQ(nrfx_get_irq_number(self->uarte->p_reg)); return rx_bytes; From e867d1915e1ebde11518a4cb6617a8eb2b374a46 Mon Sep 17 00:00:00 2001 From: Bernhard Boser Date: Sun, 17 Jan 2021 19:30:41 -0800 Subject: [PATCH 2/7] make translate --- locale/circuitpython.pot | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index f319b82f8b..d07bf6caf4 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2021-01-04 12:55-0600\n" +"POT-Creation-Date: 2021-01-17 19:26-0800\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -1606,6 +1606,10 @@ msgstr "" msgid "RNG Init Error" msgstr "" +#: ports/nrf/common-hal/busio/UART.c +msgid "RS485 Not yet supported on this device" +msgstr "" + #: ports/esp32s2/common-hal/busio/UART.c #: ports/mimxrt10xx/common-hal/busio/UART.c msgid "RS485 inversion specified when not in RS485 mode" @@ -1621,7 +1625,7 @@ msgid "RTC is not supported on this board" msgstr "" #: ports/atmel-samd/common-hal/busio/UART.c ports/cxd56/common-hal/busio/UART.c -#: ports/nrf/common-hal/busio/UART.c ports/stm/common-hal/busio/UART.c +#: ports/stm/common-hal/busio/UART.c msgid "RTS/CTS/RS485 Not yet supported on this device" msgstr "" @@ -1642,6 +1646,10 @@ msgstr "" msgid "Read-only object" msgstr "" +#: ports/nrf/common-hal/busio/UART.c +msgid "Reading >receiver_buffer_size bytes is not supported" +msgstr "" + #: shared-bindings/displayio/EPaperDisplay.c msgid "Refresh too soon" msgstr "" @@ -2217,10 +2225,18 @@ msgstr "" msgid "branch not in range" msgstr "" +#: extmod/ulab/code/ulab_create.c +msgid "buffer is smaller than requested size" +msgstr "" + #: shared-bindings/audiocore/RawSample.c msgid "buffer must be a bytes-like object" msgstr "" +#: extmod/ulab/code/ulab_create.c +msgid "buffer size must be a multiple of element size" +msgstr "" + #: shared-module/struct/__init__.c msgid "buffer size must match format" msgstr "" @@ -3262,6 +3278,10 @@ msgstr "" msgid "offset must be >= 0" msgstr "" +#: extmod/ulab/code/ulab_create.c +msgid "offset must be non-negative and no greater than buffer length" +msgstr "" + #: py/objstr.c py/objstrunicode.c msgid "offset out of bounds" msgstr "" From 07a9593abcb86c676f2f60f487e1bad4805fb0bb Mon Sep 17 00:00:00 2001 From: Bernhard Boser Date: Sun, 17 Jan 2021 19:33:32 -0800 Subject: [PATCH 3/7] add flow control info to busio_uart_obj_t --- ports/nrf/common-hal/busio/UART.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ports/nrf/common-hal/busio/UART.h b/ports/nrf/common-hal/busio/UART.h index a251162910..2eaf584403 100644 --- a/ports/nrf/common-hal/busio/UART.h +++ b/ports/nrf/common-hal/busio/UART.h @@ -42,10 +42,13 @@ typedef struct { uint32_t timeout_ms; ringbuf_t ringbuf; - uint8_t rx_char; // EasyDMA buf + uint8_t rx_char; // EasyDMA buf + bool rx_paused; // set by irq if no space in rbuf uint8_t tx_pin_number; uint8_t rx_pin_number; + uint8_t cts_pin_number; + uint8_t rts_pin_number; } busio_uart_obj_t; void uart_reset(void); From 1e88b9411d54e5c15a27d4a9c2748c264cb9522f Mon Sep 17 00:00:00 2001 From: Bernhard Boser Date: Mon, 18 Jan 2021 10:21:27 -0800 Subject: [PATCH 4/7] rebase (main) & make translate --- locale/circuitpython.pot | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index d07bf6caf4..398a2b15da 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2021-01-17 19:26-0800\n" +"POT-Creation-Date: 2021-01-18 10:20-0800\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" From 16d54586c1b5cbf839ee8fbb78cf56dd105096f3 Mon Sep 17 00:00:00 2001 From: Bernhard Boser Date: Wed, 27 Jan 2021 09:20:45 -0800 Subject: [PATCH 5/7] remove long read error message --- ports/nrf/common-hal/busio/UART.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ports/nrf/common-hal/busio/UART.c b/ports/nrf/common-hal/busio/UART.c index 625807d866..6ecf7e0ba3 100644 --- a/ports/nrf/common-hal/busio/UART.c +++ b/ports/nrf/common-hal/busio/UART.c @@ -266,9 +266,12 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t uint64_t start_ticks = supervisor_ticks_ms64(); + // check removed to reduce code size + /* if (len > ringbuf_capacity(&self->ringbuf)) { mp_raise_ValueError(translate("Reading >receiver_buffer_size bytes is not supported")); } + */ // Wait for all bytes received or timeout while ( (ringbuf_num_filled(&self->ringbuf) < len) && (supervisor_ticks_ms64() - start_ticks < self->timeout_ms) ) { From 0d3771ece3ada68d043c41691d9776bedc4de64b Mon Sep 17 00:00:00 2001 From: Bernhard Boser Date: Wed, 27 Jan 2021 09:24:03 -0800 Subject: [PATCH 6/7] make translate --- locale/circuitpython.pot | 4 ---- 1 file changed, 4 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index ad605a2dbf..4209e6e9ac 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -1719,10 +1719,6 @@ msgstr "" msgid "Read-only object" msgstr "" -#: ports/nrf/common-hal/busio/UART.c -msgid "Reading >receiver_buffer_size bytes is not supported" -msgstr "" - #: ports/esp32s2/bindings/espidf/__init__.c ports/esp32s2/esp_error.c msgid "Received response was invalid" msgstr "" From 745ba01768e4f72fcd47c39a8a719f982772ca61 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Mon, 1 Feb 2021 12:22:35 -0500 Subject: [PATCH 7/7] shrink simmel --- ports/nrf/boards/simmel/mpconfigboard.mk | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ports/nrf/boards/simmel/mpconfigboard.mk b/ports/nrf/boards/simmel/mpconfigboard.mk index 032caac408..6bc05a7f08 100644 --- a/ports/nrf/boards/simmel/mpconfigboard.mk +++ b/ports/nrf/boards/simmel/mpconfigboard.mk @@ -3,6 +3,8 @@ USB_PID = 0xc051 USB_PRODUCT = "Simmel" USB_MANUFACTURER = "Betrusted" +CIRCUITPY_DEVICES="CDC,MSC,HID" + MCU_CHIP = nrf52833 # SPI_FLASH_FILESYSTEM = 1 @@ -29,6 +31,7 @@ CIRCUITPY_RTC = 1 CIRCUITPY_SDCARDIO = 0 CIRCUITPY_TOUCHIO = 0 CIRCUITPY_ULAB = 0 +CIRCUITPY_USB_MIDI = 0 CIRCUITPY_WATCHDOG = 1 # Enable micropython.native