34
34
#include "py/ringbuf.h"
35
35
#include "extmod/misc.h"
36
36
#include "shared/runtime/interrupt_char.h"
37
+ #include "shared/tinyusb/mp_usbd_cdc.h"
37
38
#include "tusb.h"
38
39
#include "uart.h"
39
40
@@ -64,62 +65,16 @@ NORETURN void mp_hal_raise(HAL_StatusTypeDef status) {
64
65
mp_raise_OSError (mp_hal_status_to_errno_table [status ]);
65
66
}
66
67
67
- #if MICROPY_HW_USB_CDC
68
-
69
68
uint8_t cdc_itf_pending ; // keep track of cdc interfaces which need attention to poll
70
69
71
- void poll_cdc_interfaces (void ) {
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
70
uintptr_t mp_hal_stdio_poll (uintptr_t poll_flags ) {
107
71
uintptr_t ret = 0 ;
108
72
#if MICROPY_HW_USB_CDC
109
- poll_cdc_interfaces ( );
73
+ ret |= mp_usbd_cdc_poll_interfaces ( poll_flags );
110
74
#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
- }
75
+ #if MICROPY_HW_ENABLE_UART_REPL
115
76
if (poll_flags & MP_STREAM_POLL_WR ) {
116
- #if MICROPY_HW_ENABLE_UART_REPL
117
77
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
78
}
124
79
#endif
125
80
#if MICROPY_PY_OS_DUPTERM
@@ -132,7 +87,7 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
132
87
int mp_hal_stdin_rx_chr (void ) {
133
88
for (;;) {
134
89
#if MICROPY_HW_USB_CDC
135
- poll_cdc_interfaces ( );
90
+ mp_usbd_cdc_poll_interfaces ( 0 );
136
91
#endif
137
92
138
93
#if MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE
@@ -165,27 +120,10 @@ mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
165
120
#endif
166
121
167
122
#if MICROPY_HW_USB_CDC
168
- if (tud_cdc_connected ()) {
169
- size_t i = 0 ;
170
- while (i < len ) {
171
- uint32_t n = len - i ;
172
- if (n > CFG_TUD_CDC_EP_BUFSIZE ) {
173
- n = CFG_TUD_CDC_EP_BUFSIZE ;
174
- }
175
- int timeout = 0 ;
176
- // Wait with a max of USC_CDC_TIMEOUT ms
177
- while (n > tud_cdc_write_available () && timeout ++ < MICROPY_HW_USB_CDC_TX_TIMEOUT ) {
178
- MICROPY_EVENT_POLL_HOOK
179
- }
180
- if (timeout >= MICROPY_HW_USB_CDC_TX_TIMEOUT ) {
181
- break ;
182
- }
183
- uint32_t n2 = tud_cdc_write (str + i , n );
184
- tud_cdc_write_flush ();
185
- i += n2 ;
186
- }
187
- ret = MIN (i , ret );
123
+ mp_uint_t cdc_res = mp_usbd_cdc_tx_strn (str , len );
124
+ if (cdc_res > 0 ) {
188
125
did_write = true;
126
+ ret = MIN (cdc_res , ret );
189
127
}
190
128
#endif
191
129
0 commit comments