diff options
| author | Damien George <damien@micropython.org> | 2025-06-23 12:56:35 +1000 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2025-07-08 10:10:16 +1000 |
| commit | 6a53319336ce234757e04223cfe8560d6bea2de6 (patch) | |
| tree | add20b9a9b38c4365ce6e529c3a64dc55f4a1532 | |
| parent | 4951a06bbb5b3987a8ac922c06b8764c083d168c (diff) | |
zephyr/src: Increase UART input buffer to 512 bytes and reduce latency.
There are two changes here:
1. Increase the UART input bufffer to 512 bytes. That's necessary to get
basic REPL reliability tests working, and helps improve `mpremote`
usage, eg copying large files.
2. Remove `uart_sem` semaphore. This is no longer needed because
`zephyr_getchar()` should be fully non-blocking and have as low a
latency as possible. `mp_hal_stdin_rx_chr()` (which calls
`zephyr_getchar`) already uses `MICROPY_EVENT_POLL_HOOK` to get
an efficient wait, and doing an extra wait and check for the
semaphore in `zephyr_getchar()` just introduces unnecessary latency and
can lead to slower input, and potentially overflowing the UART input
buffer.
Signed-off-by: Damien George <damien@micropython.org>
| -rw-r--r-- | ports/zephyr/src/zephyr_getchar.c | 12 |
1 files changed, 3 insertions, 9 deletions
diff --git a/ports/zephyr/src/zephyr_getchar.c b/ports/zephyr/src/zephyr_getchar.c index 7660e3cc1..bf504a97c 100644 --- a/ports/zephyr/src/zephyr_getchar.c +++ b/ports/zephyr/src/zephyr_getchar.c @@ -23,12 +23,10 @@ extern int mp_interrupt_char; void mp_sched_keyboard_interrupt(void); void mp_hal_signal_event(void); -void mp_hal_wait_sem(struct k_sem *sem, uint32_t timeout_ms); -static struct k_sem uart_sem; -#define UART_BUFSIZE 256 +#define UART_BUFSIZE (512) static uint8_t uart_ringbuf[UART_BUFSIZE]; -static uint8_t i_get, i_put; +static uint16_t i_get, i_put; static int console_irq_input_hook(uint8_t ch) { int i_next = (i_put + 1) & (UART_BUFSIZE - 1); @@ -44,8 +42,6 @@ static int console_irq_input_hook(uint8_t ch) { uart_ringbuf[i_put] = ch; i_put = i_next; } - // printk("%x\n", ch); - k_sem_give(&uart_sem); return 1; } @@ -55,8 +51,7 @@ int zephyr_getchar_check(void) { } int zephyr_getchar(void) { - mp_hal_wait_sem(&uart_sem, 0); - if (k_sem_take(&uart_sem, K_MSEC(0)) == 0) { + if (i_get != i_put) { unsigned int key = irq_lock(); int c = (int)uart_ringbuf[i_get++]; i_get &= UART_BUFSIZE - 1; @@ -67,7 +62,6 @@ int zephyr_getchar(void) { } void zephyr_getchar_init(void) { - k_sem_init(&uart_sem, 0, UINT_MAX); uart_console_in_debug_hook_install(console_irq_input_hook); // All NULLs because we're interested only in the callback above uart_register_input(NULL, NULL, NULL); |
