Skip to content

Commit c11efc7

Browse files
andrewleechdpgeorge
authored andcommitted
samd/mphalport: Refactor to use shared TinyUSB CDC functions.
Signed-off-by: Andrew Leech <andrew@alelec.net>
1 parent 2d33071 commit c11efc7

File tree

2 files changed

+7
-74
lines changed

2 files changed

+7
-74
lines changed

ports/samd/mphalport.c

Lines changed: 6 additions & 74 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_usbd_cdc.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 = 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;
@@ -165,16 +124,7 @@ uint64_t mp_hal_ticks_us_64(void) {
165124

166125
uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
167126
uintptr_t ret = 0;
168-
169-
poll_cdc_interfaces();
170-
if ((poll_flags & MP_STREAM_POLL_RD) && ringbuf_peek(&stdin_ringbuf) != -1) {
171-
ret |= MP_STREAM_POLL_RD;
172-
}
173-
174-
if ((poll_flags & MP_STREAM_POLL_WR) && tud_cdc_connected() && tud_cdc_write_available() > 0) {
175-
ret |= MP_STREAM_POLL_WR;
176-
}
177-
127+
ret |= mp_usbd_cdc_poll_interfaces(poll_flags);
178128
#if MICROPY_PY_OS_DUPTERM
179129
ret |= mp_os_dupterm_poll(poll_flags);
180130
#endif
@@ -184,7 +134,7 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
184134
int mp_hal_stdin_rx_chr(void) {
185135
for (;;) {
186136

187-
poll_cdc_interfaces();
137+
mp_usbd_cdc_poll_interfaces(0);
188138
int c = ringbuf_get(&stdin_ringbuf);
189139
if (c != -1) {
190140
return c;
@@ -203,28 +153,10 @@ int mp_hal_stdin_rx_chr(void) {
203153
mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
204154
mp_uint_t ret = len;
205155
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);
156+
mp_uint_t cdc_res = mp_usbd_cdc_tx_strn(str, len);
157+
if (cdc_res > 0) {
227158
did_write = true;
159+
ret = MIN(cdc_res, ret);
228160
}
229161
#if MICROPY_PY_OS_DUPTERM
230162
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy