30
30
#include "py/stream.h"
31
31
#include "shared/runtime/interrupt_char.h"
32
32
#include "shared/tinyusb/mp_usbd.h"
33
+ #include "shared/tinyusb/mp_cdc_common.h"
33
34
#include "extmod/misc.h"
34
35
#include "samd_soc.h"
35
36
#include "tusb.h"
@@ -51,48 +52,6 @@ ringbuf_t stdin_ringbuf = { stdin_ringbuf_array, sizeof(stdin_ringbuf_array), 0,
51
52
mp_usbd_task(); \
52
53
} while (0)
53
54
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
-
96
55
void mp_hal_set_pin_mux (mp_hal_pin_obj_t pin , uint8_t mux ) {
97
56
int pin_grp = pin / 32 ;
98
57
int port_grp = (pin % 32 ) / 2 ;
@@ -166,7 +125,7 @@ uint64_t mp_hal_ticks_us_64(void) {
166
125
uintptr_t mp_hal_stdio_poll (uintptr_t poll_flags ) {
167
126
uintptr_t ret = 0 ;
168
127
169
- poll_cdc_interfaces ();
128
+ tud_cdc_poll_interfaces ();
170
129
if ((poll_flags & MP_STREAM_POLL_RD ) && ringbuf_peek (& stdin_ringbuf ) != -1 ) {
171
130
ret |= MP_STREAM_POLL_RD ;
172
131
}
@@ -184,7 +143,7 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
184
143
int mp_hal_stdin_rx_chr (void ) {
185
144
for (;;) {
186
145
187
- poll_cdc_interfaces ();
146
+ tud_cdc_poll_interfaces ();
188
147
int c = ringbuf_get (& stdin_ringbuf );
189
148
if (c != -1 ) {
190
149
return c ;
@@ -203,28 +162,10 @@ int mp_hal_stdin_rx_chr(void) {
203
162
mp_uint_t mp_hal_stdout_tx_strn (const char * str , mp_uint_t len ) {
204
163
mp_uint_t ret = len ;
205
164
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 ) {
227
167
did_write = true;
168
+ ret = MIN (cdc_res , ret );
228
169
}
229 170
#if MICROPY_PY_OS_DUPTERM
230
171
int dupterm_res = mp_os_dupterm_tx_strn (str , len );
0 commit comments