From f21c262659e0428def1175dfb6b39abb8ff7558d Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Sun, 11 Oct 2026 17:56:44 -0700 Subject: [PATCH 1/2] zephyr-cp: raise the UART console receive buffer to 1 KB At 115200 baud the REPL echo cannot keep up with a long line or paste, and the 64-byte queue overflowed. With 1 KB, 1000-byte lines and pastes of up to about that size go through on SiWx917-DK2605A and ESP32-S31-Korvo-1. Costs 960 bytes of RAM. Co-Authored-By: Claude Opus 5.5 (1M context) --- ports/zephyr-cp/supervisor/serial.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/zephyr-cp/supervisor/serial.c b/ports/zephyr-cp/supervisor/serial.c index 3ae3e73fa10..6107c967973 100644 --- a/ports/zephyr-cp/supervisor/serial.c +++ b/ports/zephyr-cp/supervisor/serial.c @@ -14,7 +14,7 @@ usb_cdc_serial_obj_t *usb_console; #else #include "shared-bindings/busio/UART.h" static busio_uart_obj_t uart_console; -static uint8_t buffer[64]; +static uint8_t buffer[1024]; #endif void port_serial_early_init(void) { From 0a332d8e812346b13dc90ab684b565788264f4d2 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Sun, 11 Oct 2026 17:58:48 -0700 Subject: [PATCH 2/2] zephyr-cp: Ctrl-C interrupts only from the console serial_cb() raised KeyboardInterrupt on 0x03 from every UART, including busio.UART, board UARTs and usb_cdc.data, so a sensor sending 0x03 would stop code.py. Pass sigint_enabled into construct_from_device, as the stm and silabs ports store it, and set it only for the UART and USB CDC consoles. TinyUSB ports likewise watch only the console CDC. Co-Authored-By: Claude Opus 5.5 (1M context) --- ports/zephyr-cp/common-hal/busio/UART.c | 7 ++++--- ports/zephyr-cp/common-hal/busio/UART.h | 4 +++- ports/zephyr-cp/common-hal/usb_cdc/Serial.c | 4 ++-- ports/zephyr-cp/common-hal/usb_cdc/Serial.h | 2 +- ports/zephyr-cp/cptools/zephyr2cp.py | 2 +- ports/zephyr-cp/supervisor/serial.c | 2 +- ports/zephyr-cp/supervisor/usb.c | 4 ++-- 7 files changed, 14 insertions(+), 11 deletions(-) diff --git a/ports/zephyr-cp/common-hal/busio/UART.c b/ports/zephyr-cp/common-hal/busio/UART.c index 97ab44bb8c1..1c97555955b 100644 --- a/ports/zephyr-cp/common-hal/busio/UART.c +++ b/ports/zephyr-cp/common-hal/busio/UART.c @@ -70,7 +70,7 @@ static void serial_cb(const struct device *dev, void *user_data) { if (uart_fifo_read(dev, &c, 1) != 1) { break; } - if (mp_interrupt_char == c) { + if (self->sigint_enabled && mp_interrupt_char == c) { common_hal_busio_uart_clear_rx_buffer(self); mp_sched_keyboard_interrupt(); port_wake_main_task_from_isr(); @@ -83,7 +83,7 @@ static void serial_cb(const struct device *dev, void *user_data) { } // Helper function for Zephyr-specific initialization from device tree -mp_obj_t common_hal_busio_uart_construct_from_device(busio_uart_obj_t *self, const struct device *uart_device, uint16_t receiver_buffer_size, byte *receiver_buffer) { +mp_obj_t common_hal_busio_uart_construct_from_device(busio_uart_obj_t *self, const struct device *uart_device, uint16_t receiver_buffer_size, byte *receiver_buffer, bool sigint_enabled) { self->base.type = &busio_uart_type; self->uart_device = uart_device; self->dynamic = false; @@ -104,6 +104,7 @@ mp_obj_t common_hal_busio_uart_construct_from_device(busio_uart_obj_t *self, con self->write_timeout = K_FOREVER; self->rx_paused = false; self->flow_control = false; + self->sigint_enabled = sigint_enabled; uart_irq_rx_enable(uart_device); return MP_OBJ_FROM_PTR(self); @@ -148,7 +149,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self, allocated_buffer = true; } - common_hal_busio_uart_construct_from_device(self, dev, receiver_buffer_size, receiver_buffer); + common_hal_busio_uart_construct_from_device(self, dev, receiver_buffer_size, receiver_buffer, sigint_enabled); self->dynamic = true; self->receiver_buffer = allocated_buffer ? receiver_buffer : NULL; self->tx = tx; diff --git a/ports/zephyr-cp/common-hal/busio/UART.h b/ports/zephyr-cp/common-hal/busio/UART.h index d98a0d2c929..3be26d2feec 100644 --- a/ports/zephyr-cp/common-hal/busio/UART.h +++ b/ports/zephyr-cp/common-hal/busio/UART.h @@ -30,6 +30,8 @@ typedef struct { // Set when the sender waits for unread data (USB CDC), so RX can // pause on a full msgq instead of dropping bytes. bool flow_control; + // Raise KeyboardInterrupt on mp_interrupt_char. + bool sigint_enabled; // True when the underlying Zephyr device was dynamically routed to the // pins above at construction time. Such objects own their receiver @@ -38,7 +40,7 @@ typedef struct { } busio_uart_obj_t; // Helper function for Zephyr-specific initialization from device tree -mp_obj_t common_hal_busio_uart_construct_from_device(busio_uart_obj_t *self, const struct device *uart_device, uint16_t receiver_buffer_size, byte *receiver_buffer); +mp_obj_t common_hal_busio_uart_construct_from_device(busio_uart_obj_t *self, const struct device *uart_device, uint16_t receiver_buffer_size, byte *receiver_buffer, bool sigint_enabled); // Internal helper for clearing buffer void common_hal_busio_uart_clear_rx_buffer(busio_uart_obj_t *self); diff --git a/ports/zephyr-cp/common-hal/usb_cdc/Serial.c b/ports/zephyr-cp/common-hal/usb_cdc/Serial.c index 6384dff22b1..b93e188b0a0 100644 --- a/ports/zephyr-cp/common-hal/usb_cdc/Serial.c +++ b/ports/zephyr-cp/common-hal/usb_cdc/Serial.c @@ -10,8 +10,8 @@ #include "common-hal/busio/UART.h" #include "supervisor/shared/tick.h" -mp_obj_t common_hal_usb_cdc_serial_construct_from_device(usb_cdc_serial_obj_t *self, const struct device *uart_device, uint16_t receiver_buffer_size, byte *receiver_buffer) { - common_hal_busio_uart_construct_from_device(self, uart_device, receiver_buffer_size, receiver_buffer); +mp_obj_t common_hal_usb_cdc_serial_construct_from_device(usb_cdc_serial_obj_t *self, const struct device *uart_device, uint16_t receiver_buffer_size, byte *receiver_buffer, bool sigint_enabled) { + common_hal_busio_uart_construct_from_device(self, uart_device, receiver_buffer_size, receiver_buffer, sigint_enabled); self->flow_control = true; self->base.type = &usb_cdc_serial_type; return MP_OBJ_FROM_PTR(self); diff --git a/ports/zephyr-cp/common-hal/usb_cdc/Serial.h b/ports/zephyr-cp/common-hal/usb_cdc/Serial.h index 9a110545a37..fdad46bec31 100644 --- a/ports/zephyr-cp/common-hal/usb_cdc/Serial.h +++ b/ports/zephyr-cp/common-hal/usb_cdc/Serial.h @@ -11,4 +11,4 @@ typedef busio_uart_obj_t usb_cdc_serial_obj_t; // Helper function for Zephyr-specific initialization from device tree -mp_obj_t common_hal_usb_cdc_serial_construct_from_device(usb_cdc_serial_obj_t *self, const struct device *uart_device, uint16_t receiver_buffer_size, byte *receiver_buffer); +mp_obj_t common_hal_usb_cdc_serial_construct_from_device(usb_cdc_serial_obj_t *self, const struct device *uart_device, uint16_t receiver_buffer_size, byte *receiver_buffer, bool sigint_enabled); diff --git a/ports/zephyr-cp/cptools/zephyr2cp.py b/ports/zephyr-cp/cptools/zephyr2cp.py index 660b263262f..0125ee658ab 100644 --- a/ports/zephyr-cp/cptools/zephyr2cp.py +++ b/ports/zephyr-cp/cptools/zephyr2cp.py @@ -1358,7 +1358,7 @@ def zephyr_dts_to_cp_board(board_id, portdir, builddir, zephyrbuilddir, mpconfig if driver == "serial": # UART needs a receiver buffer buffer_decl = f"static byte {instance_name}_buffer[128];" - construct_call = f"common_hal_busio_uart_construct_from_device(&{instance_name}_obj, DEVICE_DT_GET(DT_NODELABEL({labels[0]})), 128, {instance_name}_buffer)" + construct_call = f"common_hal_busio_uart_construct_from_device(&{instance_name}_obj, DEVICE_DT_GET(DT_NODELABEL({labels[0]})), 128, {instance_name}_buffer, false)" else: # Default case (I2C, SPI, I2S) buffer_decl = "" diff --git a/ports/zephyr-cp/supervisor/serial.c b/ports/zephyr-cp/supervisor/serial.c index 6107c967973..8e801de9f2e 100644 --- a/ports/zephyr-cp/supervisor/serial.c +++ b/ports/zephyr-cp/supervisor/serial.c @@ -20,7 +20,7 @@ static uint8_t buffer[1024]; void port_serial_early_init(void) { #if CIRCUITPY_USB_DEVICE == 0 uart_console.base.type = &busio_uart_type; - common_hal_busio_uart_construct_from_device(&uart_console, DEVICE_DT_GET(DT_CHOSEN(zephyr_console)), sizeof(buffer), buffer); + common_hal_busio_uart_construct_from_device(&uart_console, DEVICE_DT_GET(DT_CHOSEN(zephyr_console)), sizeof(buffer), buffer, true); #endif } diff --git a/ports/zephyr-cp/supervisor/usb.c b/ports/zephyr-cp/supervisor/usb.c index f269a38ea59..63dc28054fd 100644 --- a/ports/zephyr-cp/supervisor/usb.c +++ b/ports/zephyr-cp/supervisor/usb.c @@ -272,7 +272,7 @@ void usb_init(void) { if (console) { uint8_t *receiver_buffer = port_malloc(1024, true); if (receiver_buffer != NULL) { - common_hal_usb_cdc_serial_construct_from_device(&usb_cdc_console_obj, DEVICE_DT_GET(DT_NODELABEL(cdc_acm_console)), 1024, receiver_buffer); + common_hal_usb_cdc_serial_construct_from_device(&usb_cdc_console_obj, DEVICE_DT_GET(DT_NODELABEL(cdc_acm_console)), 1024, receiver_buffer, true); } else { console = false; } @@ -283,7 +283,7 @@ void usb_init(void) { if (data) { uint8_t *receiver_buffer = port_malloc(1024, true); if (receiver_buffer != NULL) { - common_hal_usb_cdc_serial_construct_from_device(&usb_cdc_data_obj, DEVICE_DT_GET(DT_NODELABEL(cdc_acm_data)), 1024, receiver_buffer); + common_hal_usb_cdc_serial_construct_from_device(&usb_cdc_data_obj, DEVICE_DT_GET(DT_NODELABEL(cdc_acm_data)), 1024, receiver_buffer, false); } else { data = false; }