32
32
#include "shared/runtime/softtimer.h"
33
33
#include "shared/timeutils/timeutils.h"
34
34
#include "shared/tinyusb/mp_usbd.h"
35
+ #include "shared/tinyusb/mp_usbd_cdc.h"
35
36
#include "pendsv.h"
36
37
#include "tusb.h"
37
38
#include "uart.h"
@@ -58,68 +59,14 @@ ringbuf_t stdin_ringbuf = { stdin_ringbuf_array, sizeof(stdin_ringbuf_array) };
58
59
59
60
#endif
60
61
61
- #if MICROPY_HW_USB_CDC
62
-
63
- uint8_t cdc_itf_pending ; // keep track of cdc interfaces which need attention to poll
64
-
65
- void poll_cdc_interfaces (void ) {
66
- if (!cdc_itf_pending ) {
67
- // Explicitly run the USB stack as the scheduler may be locked (eg we are in
68
- // an interrupt handler) while there is data pending.
69
- mp_usbd_task ();
70
- }
71
-
72
- // any CDC interfaces left to poll?
73
- if (cdc_itf_pending && ringbuf_free (& stdin_ringbuf )) {
74
- for (uint8_t itf = 0 ; itf < 8 ; ++ itf ) {
75
- if (cdc_itf_pending & (1 << itf )) {
76
- tud_cdc_rx_cb (itf );
77
- if (!cdc_itf_pending ) {
78
- break ;
79
- }
80
- }
81
- }
82
- }
83
- }
84
-
85
- void tud_cdc_rx_cb (uint8_t itf ) {
86
- // consume pending USB data immediately to free usb buffer and keep the endpoint from stalling.
87
- // in case the ringbuffer is full, mark the CDC interface that need attention later on for polling
88
- cdc_itf_pending &= ~(1 << itf );
89
- for (uint32_t bytes_avail = tud_cdc_n_available (itf ); bytes_avail > 0 ; -- bytes_avail ) {
90
- if (ringbuf_free (& stdin_ringbuf )) {
91
- int data_char = tud_cdc_read_char ();
92
- if (data_char == mp_interrupt_char ) {
93
- mp_sched_keyboard_interrupt ();
94
- } else {
95
- ringbuf_put (& stdin_ringbuf , data_char );
96
- }
97
- } else {
98
- cdc_itf_pending |= (1 << itf );
99
- return ;
100
- }
101
- }
102
- }
103
-
104
- #endif
105
-
106
62
uintptr_t mp_hal_stdio_poll (uintptr_t poll_flags ) {
107
63
uintptr_t ret = 0 ;
108
64
#if MICROPY_HW_USB_CDC
109
- poll_cdc_interfaces ( );
65
+ ret |= mp_usbd_cdc_poll_interfaces ( poll_flags );
110
66
#endif
111
- #if MICROPY_HW_ENABLE_UART_REPL || MICROPY_HW_USB_CDC
112
- if ((poll_flags & MP_STREAM_POLL_RD ) && ringbuf_peek (& stdin_ringbuf ) != -1 ) {
113
- ret |= MP_STREAM_POLL_RD ;
114
- }
67
+ #if MICROPY_HW_ENABLE_UART_REPL
115
68
if (poll_flags & MP_STREAM_POLL_WR ) {
116
- #if MICROPY_HW_ENABLE_UART_REPL
117
69
ret |= MP_STREAM_POLL_WR ;
118
- #else
119
- if (tud_cdc_connected () && tud_cdc_write_available () > 0 ) {
120
- ret |= MP_STREAM_POLL_WR ;
121
- }
122
- #endif
123
70
}
124
71
#endif
125
72
#if MICROPY_PY_OS_DUPTERM
@@ -132,7 +79,7 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
132
79
int mp_hal_stdin_rx_chr (void ) {
133
80
for (;;) {
134
81
#if MICROPY_HW_USB_CDC
135
- poll_cdc_interfaces ( );
82
+ mp_usbd_cdc_poll_interfaces ( 0 );
136
83
#endif
137
84
138
85
int c = ringbuf_get (& stdin_ringbuf );
@@ -159,32 +106,10 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
159
106
#endif
160
107
161
108
#if MICROPY_HW_USB_CDC
162
- if (tud_cdc_connected ()) {
163
- size_t i = 0 ;
164
- while (i < len ) {
165
- uint32_t n = len - i ;
166
- if (n > CFG_TUD_CDC_EP_BUFSIZE ) {
167
- n = CFG_TUD_CDC_EP_BUFSIZE ;
168
- }
169
- int timeout = 0 ;
170
- // Wait with a max of USC_CDC_TIMEOUT ms
171
- while (n > tud_cdc_write_available () && timeout ++ < MICROPY_HW_USB_CDC_TX_TIMEOUT ) {
172
- mp_event_wait_ms (1 );
173
-
174
- // Explicitly run the USB stack as the scheduler may be locked (eg we
175
- // are in an interrupt handler), while there is data pending.
176
- mp_usbd_task ();
177
- }
178
- if (timeout >= MICROPY_HW_USB_CDC_TX_TIMEOUT ) {
179
- ret = i ;
180
- break ;
181
- }
182
- uint32_t n2 = tud_cdc_write (str + i , n );
183
- tud_cdc_write_flush ();
184
- i += n2 ;
185
- }
186
- ret = MIN (i , ret );
109
+ mp_uint_t cdc_res = mp_usbd_cdc_tx_strn (str , len );
110
+ if (cdc_res > 0 ) {
187
111
did_write = true;
112
+ ret = MIN (cdc_res , ret );
188
113
}
189
114
#endif
190
115
0 commit comments