From 60539ea162a15d38d974ee58f7c5430c565cf0eb Mon Sep 17 00:00:00 2001 From: robert-hh Date: Thu, 7 Jul 2022 21:19:28 +0200 Subject: [PATCH] nrf/drivers/usb: Probe for interrupt char in USB CDC task. And ensure that the input queue is empty when scheduling the interrupt. --- ports/nrf/drivers/bluetooth/ble_uart.c | 2 +- ports/nrf/drivers/usb/usb_cdc.c | 10 +++++++++- ports/nrf/modules/machine/uart.c | 2 ++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/ports/nrf/drivers/bluetooth/ble_uart.c b/ports/nrf/drivers/bluetooth/ble_uart.c index c3712fe8c9..3020f1af62 100644 --- a/ports/nrf/drivers/bluetooth/ble_uart.c +++ b/ports/nrf/drivers/bluetooth/ble_uart.c @@ -193,9 +193,9 @@ STATIC void gatts_event_handler(mp_obj_t self_in, uint16_t event_id, uint16_t at for (uint16_t i = 0; i < length; i++) { #if MICROPY_KBD_EXCEPTION if (data[i] == mp_interrupt_char) { - mp_sched_keyboard_interrupt(); m_rx_ring_buffer.start = 0; m_rx_ring_buffer.end = 0; + mp_sched_keyboard_interrupt(); } else #endif { diff --git a/ports/nrf/drivers/usb/usb_cdc.c b/ports/nrf/drivers/usb/usb_cdc.c index a1d411a256..eda933b89f 100644 --- a/ports/nrf/drivers/usb/usb_cdc.c +++ b/ports/nrf/drivers/usb/usb_cdc.c @@ -35,6 +35,8 @@ #include "nrfx_uart.h" #include "py/ringbuf.h" #include "py/stream.h" +#include "py/runtime.h" +#include "shared/runtime/interrupt_char.h" #ifdef BLUETOOTH_SD #include "nrf_sdm.h" @@ -127,7 +129,13 @@ static void cdc_task(void) int c; uint32_t count = tud_cdc_read(&c, 1); (void)count; - ringbuf_put((ringbuf_t*)&rx_ringbuf, c); + if (c == mp_interrupt_char) { + rx_ringbuf.iget = 0; + rx_ringbuf.iput = 0; + mp_sched_keyboard_interrupt(); + } else { + ringbuf_put((ringbuf_t*)&rx_ringbuf, c); + } } int chars = 0; diff --git a/ports/nrf/modules/machine/uart.c b/ports/nrf/modules/machine/uart.c index 2cc421aa1f..e8c82e57dd 100644 --- a/ports/nrf/modules/machine/uart.c +++ b/ports/nrf/modules/machine/uart.c @@ -128,6 +128,8 @@ STATIC void uart_event_handler(nrfx_uart_event_t const *p_event, void *p_context nrfx_uart_rx(self->p_uart, &self->buf->rx_buf[0], 1); #if !MICROPY_PY_BLE_NUS && MICROPY_KBD_EXCEPTION if (chr == mp_interrupt_char) { + self->buf->rx_ringbuf.iget = 0; + self->buf->rx_ringbuf.iput = 0; mp_sched_keyboard_interrupt(); } else #endif