Skip to content

Commit c6e527d

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 8bd5472 + 767234e commit c6e527d

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

ports/espressif/supervisor/usb_serial_jtag.c

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,29 @@ STATIC volatile bool connected;
4646
#error "CONFIG_ESP_PHY_ENABLE_USB must be enabled in sdkconfig"
4747
#endif
4848

49+
// Make sure the recv interrupt is disabled during this. Otherwise, it could reorder data if it
50+
// interrupts itself.
51+
static void _copy_out_of_fifo(void) {
52+
size_t req_len = ringbuf_num_empty(&ringbuf);
53+
if (req_len == 0) {
54+
// Disable the interrupt so that CircuitPython can run and process the ringbuf. It will
55+
// re-enable the interrupt once the ringbuf is empty.
56+
usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT);
57+
}
58+
if (req_len > USB_SERIAL_JTAG_BUF_SIZE) {
59+
req_len = USB_SERIAL_JTAG_BUF_SIZE;
60+
}
61+
uint8_t rx_buf[USB_SERIAL_JTAG_BUF_SIZE];
62+
size_t len = usb_serial_jtag_ll_read_rxfifo(rx_buf, req_len);
63+
for (size_t i = 0; i < len; ++i) {
64+
if (rx_buf[i] == mp_interrupt_char) {
65+
mp_sched_keyboard_interrupt();
66+
} else {
67+
ringbuf_put(&ringbuf, rx_buf[i]);
68+
}
69+
}
70+
}
71+
4972
static void usb_serial_jtag_isr_handler(void *arg) {
5073
uint32_t flags = usb_serial_jtag_ll_get_intsts_mask();
5174

@@ -60,25 +83,13 @@ static void usb_serial_jtag_isr_handler(void *arg) {
6083

6184
if (flags & USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT) {
6285
usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT);
63-
size_t req_len = ringbuf_num_empty(&ringbuf);
64-
if (req_len > USB_SERIAL_JTAG_BUF_SIZE) {
65-
req_len = USB_SERIAL_JTAG_BUF_SIZE;
66-
}
67-
uint8_t rx_buf[USB_SERIAL_JTAG_BUF_SIZE];
68-
size_t len = usb_serial_jtag_ll_read_rxfifo(rx_buf, req_len);
69-
for (size_t i = 0; i < len; ++i) {
70-
if (rx_buf[i] == mp_interrupt_char) {
71-
mp_sched_keyboard_interrupt();
72-
} else {
73-
ringbuf_put(&ringbuf, rx_buf[i]);
74-
}
75-
}
86+
_copy_out_of_fifo();
7687
port_wake_main_task_from_isr();
7788
}
7889
}
7990

8091
void usb_serial_jtag_init(void) {
81-
ringbuf_init(&ringbuf, buf, sizeof(buf));
92+
ringbuf_init(&ringbuf, buf, sizeof(buf) - 1);
8293
usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_INTR_SOF | USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT | USB_SERIAL_JTAG_INTR_TOKEN_REC_IN_EP1);
8394
usb_serial_jtag_ll_ena_intr_mask(USB_SERIAL_JTAG_INTR_SOF | USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT | USB_SERIAL_JTAG_INTR_TOKEN_REC_IN_EP1);
8495
ESP_ERROR_CHECK(esp_intr_alloc(ETS_USB_SERIAL_JTAG_INTR_SOURCE, ESP_INTR_FLAG_LEVEL1,
@@ -93,11 +104,18 @@ char usb_serial_jtag_read_char(void) {
93104
if (ringbuf_num_filled(&ringbuf) == 0) {
94105
return -1;
95106
}
96-
return ringbuf_get(&ringbuf);
107+
char c = ringbuf_get(&ringbuf);
108+
// Maybe re-enable the recv interrupt if we've emptied the ringbuf.
109+
if (ringbuf_num_filled(&ringbuf) == 0) {
110+
usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT);
111+
_copy_out_of_fifo();
112+
usb_serial_jtag_ll_ena_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT);
113+
}
114+
return c;
97115
}
98116

99117
bool usb_serial_jtag_bytes_available(void) {
100-
return ringbuf_num_filled(&ringbuf);
118+
return ringbuf_num_filled(&ringbuf) > 0;
101119
}
102120

103121
void usb_serial_jtag_write(const char *text, uint32_t length) {

0 commit comments

Comments
 (0)