Skip to content

Commit e9a7631

Browse files
robert-hhdpgeorge
authored andcommitted
samd/mphalport: Fix USB endpoint handling ignoring Ctrl-C.
Porting PR micropython#8040 by @hoihu to SAMD, following the commit 5873390. One small addition: before executing keyboard interrupt, the input buffer is cleared.
1 parent b001730 commit e9a7631

File tree

3 files changed

+57
-20
lines changed

3 files changed

+57
-20
lines changed

ports/samd/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ SRC_C += \
131131
shared/libc/string0.c \
132132
shared/readline/readline.c \
133133
shared/runtime/gchelper_native.c \
134+
shared/runtime/interrupt_char.c \
134135
shared/runtime/pyexec.c \
135136
shared/runtime/softtimer.c \
136137
shared/runtime/stdout_helpers.c \

ports/samd/mphalport.c

+55-20
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,54 @@
3333
#include "samd_soc.h"
3434
#include "tusb.h"
3535

36-
#if MICROPY_KBD_EXCEPTION
37-
int mp_interrupt_char = -1;
38-
39-
void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) {
40-
(void)itf;
41-
(void)wanted_char;
42-
tud_cdc_read_char(); // discard interrupt char
43-
mp_sched_keyboard_interrupt();
44-
}
36+
#ifndef MICROPY_HW_STDIN_BUFFER_LEN
37+
#define MICROPY_HW_STDIN_BUFFER_LEN 128
38+
#endif
39+
40+
STATIC uint8_t stdin_ringbuf_array[MICROPY_HW_STDIN_BUFFER_LEN];
41+
ringbuf_t stdin_ringbuf = { stdin_ringbuf_array, sizeof(stdin_ringbuf_array), 0, 0 };
4542

46-
void mp_hal_set_interrupt_char(int c) {
47-
mp_interrupt_char = c;
48-
tud_cdc_set_wanted_char(c);
43+
uint8_t cdc_itf_pending; // keep track of cdc interfaces which need attention to poll
44+
45+
void poll_cdc_interfaces(void) {
46+
// any CDC interfaces left to poll?
47+
if (cdc_itf_pending && ringbuf_free(&stdin_ringbuf)) {
48+
for (uint8_t itf = 0; itf < 8; ++itf) {
49+
if (cdc_itf_pending & (1 << itf)) {
50+
tud_cdc_rx_cb(itf);
51+
if (!cdc_itf_pending) {
52+
break;
53+
}
54+
}
55+
}
56+
}
4957
}
5058

51-
#endif
59+
void tud_cdc_rx_cb(uint8_t itf) {
60+
// consume pending USB data immediately to free usb buffer and keep the endpoint from stalling.
61+
// in case the ringbuffer is full, mark the CDC interface that need attention later on for polling
62+
cdc_itf_pending &= ~(1 << itf);
63+
for (uint32_t bytes_avail = tud_cdc_n_available(itf); bytes_avail > 0; --bytes_avail) {
64+
if (ringbuf_free(&stdin_ringbuf)) {
65+
int data_char = tud_cdc_read_char();
66+
#if MICROPY_KBD_EXCEPTION
67+
if (data_char == mp_interrupt_char) {
68+
// Clear the ring buffer
69+
stdin_ringbuf.iget = stdin_ringbuf.iput = 0;
70+
// and stop
71+
mp_sched_keyboard_interrupt();
72+
} else {
73+
ringbuf_put(&stdin_ringbuf, data_char);
74+
}
75+
#else
76+
ringbuf_put(&stdin_ringbuf, data_char);
77+
#endif
78+
} else {
79+
cdc_itf_pending |= (1 << itf);
80+
return;
81+
}
82+
}
83+
}
5284

5385
void mp_hal_set_pin_mux(mp_hal_pin_obj_t pin, uint8_t mux) {
5486
int pin_grp = pin / 32;
@@ -94,9 +126,12 @@ void mp_hal_delay_us(mp_uint_t us) {
94126

95127
uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
96128
uintptr_t ret = 0;
97-
if (tud_cdc_connected() && tud_cdc_available()) {
129+
130+
poll_cdc_interfaces();
131+
if ((poll_flags & MP_STREAM_POLL_RD) && ringbuf_peek(&stdin_ringbuf) != -1) {
98132
ret |= MP_STREAM_POLL_RD;
99133
}
134+
100135
#if MICROPY_PY_OS_DUPTERM
101136
ret |= mp_uos_dupterm_poll(poll_flags);
102137
#endif
@@ -105,13 +140,13 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
105140

106141
int mp_hal_stdin_rx_chr(void) {
107142
for (;;) {
108-
if (tud_cdc_connected() && tud_cdc_available()) {
109-
uint8_t buf[1];
110-
uint32_t count = tud_cdc_read(buf, sizeof(buf));
111-
if (count) {
112-
return buf[0];
113-
}
143+
144+
poll_cdc_interfaces();
145+
int c = ringbuf_get(&stdin_ringbuf);
146+
if (c != -1) {
147+
return c;
114148
}
149+
115150
#if MICROPY_PY_OS_DUPTERM
116151
int dupterm_c = mp_uos_dupterm_rx_chr();
117152
if (dupterm_c >= 0) {

ports/samd/mphalport.h

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#ifndef MICROPY_INCLUDED_SAMD_MPHALPORT_H
2828
#define MICROPY_INCLUDED_SAMD_MPHALPORT_H
2929

30+
#include "py/ringbuf.h"
3031
#include "py/mpconfig.h"
3132

3233
// ASF4

0 commit comments

Comments
 (0)