8000 samd/mphalport: Use shared/tinyusb cdc functions. · micropython/micropython@d606676 · GitHub
[go: up one dir, main page]

Skip to content

Commit d606676

Browse files
committed
samd/mphalport: Use shared/tinyusb cdc functions.
Signed-off-by: Andrew Leech <andrew@alelec.net>
1 parent dc7b9c1 commit d606676

File tree

2 files changed

+7
-65
lines changed

2 files changed

+7
-65
lines changed

ports/samd/mphalport.c

Lines changed: 6 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "py/stream.h"
3131
#include "shared/runtime/interrupt_char.h"
3232
#include "shared/tinyusb/mp_usbd.h"
33+
#include "shared/tinyusb/mp_cdc_common.h"
3334
#include "extmod/misc.h"
3435
#include "samd_soc.h"
3536
#include "tusb.h"
@@ -51,48 +52,6 @@ ringbuf_t stdin_ringbuf = { stdin_ringbuf_array, sizeof(stdin_ringbuf_array), 0,
5152
mp_usbd_task(); \
5253
} while (0)
5354

54-
uint8_t cdc_itf_pending; // keep track of cdc interfaces which need attention to poll
55-
56-
void poll_cdc_interfaces(void) {
57-
// any CDC interfaces left to poll?
58-
if (cdc_itf_pending && ringbuf_free(&stdin_ringbuf)) {
59-
for (uint8_t itf = 0; itf < 8; ++itf) {
60-
if (cdc_itf_pending & (1 << itf)) {
61-
tud_cdc_rx_cb(itf);
62-
if (!cdc_itf_pending) {
63-
break;
64-
}
65-
}
66-
}
67-
}
68-
}
69-
70-
void tud_cdc_rx_cb(uint8_t itf) {
71-
// consume pending USB data immediately to free usb buffer and keep the endpoint from stalling.
72-
// in case the ringbuffer is full, mark the CDC interface that need attention later on for polling
73-
cdc_itf_pending &= ~(1 << itf);
74-
for (uint32_t bytes_avail = tud_cdc_n_available(itf); bytes_avail > 0; --bytes_avail) {
75-
if (ringbuf_free(&stdin_ringbuf)) {
76-
int data_char < 8000 span class=pl-c1>= tud_cdc_read_char();
77-
#if MICROPY_KBD_EXCEPTION
78-
if (data_char == mp_interrupt_char) {
79-
// Clear the ring buffer
80-
stdin_ringbuf.iget = stdin_ringbuf.iput = 0;
81-
// and stop
82-
mp_sched_keyboard_interrupt();
83-
} else {
84-
ringbuf_put(&stdin_ringbuf, data_char);
85-
}
86-
#else
87-
ringbuf_put(&stdin_ringbuf, data_char);
88-
#endif
89-
} else {
90-
cdc_itf_pending |= (1 << itf);
91-
return;
92-
}
93-
}
94-
}
95-
9655
void mp_hal_set_pin_mux(mp_hal_pin_obj_t pin, uint8_t mux) {
9756
int pin_grp = pin / 32;
9857
int port_grp = (pin % 32) / 2;
@@ -166,7 +125,7 @@ uint64_t mp_hal_ticks_us_64(void) {
166125
uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
167126
uintptr_t ret = 0;
168127

169-
poll_cdc_interfaces();
128+
tud_cdc_poll_interfaces();
170129
if ((poll_flags & MP_STREAM_POLL_RD) && ringbuf_peek(&stdin_ringbuf) != -1) {
171130
ret |= MP_STREAM_POLL_RD;
172131
}
@@ -184,7 +143,7 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
184143
int mp_hal_stdin_rx_chr(void) {
185144
for (;;) {
186145

187-
poll_cdc_interfaces();
146+
tud_cdc_poll_interfaces();
188147
int c = ringbuf_get(&stdin_ringbuf);
189148
if (c != -1) {
190149
return c;
@@ -203,28 +162,10 @@ int mp_hal_stdin_rx_chr(void) {
203162
mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
204163
mp_uint_t ret = len;
205164
bool did_write = false;
206-
if (tud_cdc_connected()) {
207-
size_t i = 0;
208-
while (i < len) {
209-
uint32_t n = len - i;
210-
if (n > CFG_TUD_CDC_EP_BUFSIZE) {
211-
n = CFG_TUD_CDC_EP_BUFSIZE;
212-
}
213-
int timeout = 0;
214-
// Wait with a max of USC_CDC_TIMEOUT ms
215-
while (n > tud_cdc_write_available() && timeout++ < MICROPY_HW_USB_CDC_TX_TIMEOUT) {
216-
MICROPY_EVENT_POLL_HOOK_WITH_USB;
217-
}
218-
if (timeout >= MICROPY_HW_USB_CDC_TX_TIMEOUT) {
219-
ret = i;
220-
break;
221-
}
222-
uint32_t n2 = tud_cdc_write(str + i, n);
223-
tud_cdc_write_flush();
224-
i += n2;
225-
}
226-
ret = MIN(i, ret);
165+
mp_uint_t cdc_res = tud_cdc_tx_strn(str, len);
166+
if (cdc_res > 0) {
227167
did_write = true;
168+
ret = MIN(cdc_res, ret);
228169
}
229170
#if MICROPY_PY_OS_DUPTERM
230171
int dupterm_res = mp_os_dupterm_tx_strn(str, len);

ports/samd/mphalport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#define MICROPY_HW_USB_CDC_TX_TIMEOUT (500)
4545

4646
extern int mp_interrupt_char;
47+
extern ringbuf_t stdin_ringbuf;
4748
extern volatile uint32_t systick_ms;
4849
uint64_t mp_hal_ticks_us_64(void);
4950

0 commit comments

Comments
 (0)
0