30
30
#include "py/mphal.h"
31
31
#include "shared/timeutils/timeutils.h"
32
32
#include "shared/runtime/interrupt_char.h"
33
+ #include "shared/tinyusb/mp_usbd_cdc.h"
33
34
#include "extmod/misc.h"
34
35
#include "ticks.h"
35
36
#include "tusb.h"
44
45
static uint8_t stdin_ringbuf_array [MICROPY_HW_STDIN_BUFFER_LEN ];
45
46
ringbuf_t stdin_ringbuf = {stdin_ringbuf_array , sizeof (stdin_ringbuf_array ), 0 , 0 };
46
47
47
- uint8_t cdc_itf_pending ; // keep track of cdc interfaces which need attention to poll
48
-
49
- void poll_cdc_interfaces (void ) {
50
- // any CDC interfaces left to poll?
51
- if (cdc_itf_pending && ringbuf_free (& stdin_ringbuf )) {
52
- for (uint8_t itf = 0 ; itf < 8 ; ++ itf ) {
53
- if (cdc_itf_pending & (1 << itf )) {
54
- tud_cdc_rx_cb (itf );
55
- if (!cdc_itf_pending ) {
56
- break ;
57
- }
58
- }
59
- }
60
- }
61
- }
62
-
63
-
64
- void tud_cdc_rx_cb (uint8_t itf ) {
65
- // consume pending USB data immediately to free usb buffer and keep the endpoint from stalling.
66
- // in case the ringbuffer is full, mark the CDC interface that need attention later on for polling
67
- cdc_itf_pending &= ~(1 << itf );
68
- for (uint32_t bytes_avail = tud_cdc_n_available (itf ); bytes_avail > 0 ; -- bytes_avail ) {
69
- if (ringbuf_free (& stdin_ringbuf )) {
70
- int data_char = tud_cdc_read_char ();
71
- if (data_char == mp_interrupt_char ) {
72
- mp_sched_keyboard_interrupt ();
73
- } else {
74
- ringbuf_put (& stdin_ringbuf , data_char );
75
- }
76
- } else {
77
- cdc_itf_pending |= (1 << itf );
78
- return ;
79
- }
80
- }
81
- }
82
-
83
48
uintptr_t mp_hal_stdio_poll (uintptr_t poll_flags ) {
84
49
uintptr_t ret = 0 ;
85
- poll_cdc_interfaces ();
86
- if ((poll_flags & MP_STREAM_POLL_RD ) && ringbuf_peek (& stdin_ringbuf ) != -1 ) {
87
- ret |= MP_STREAM_POLL_RD ;
88
- }
89
- if ((poll_flags & MP_STREAM_POLL_WR ) && tud_cdc_connected () && tud_cdc_write_available () > 0 ) {
90
- ret |= MP_STREAM_POLL_WR ;
91
- }
50
+ ret |= mp_usbd_cdc_poll_interfaces (poll_flags );
92
51
#if MICROPY_PY_OS_DUPTERM
93
52
ret |= mp_os_dupterm_poll (poll_flags );
94
53
#endif
@@ -97,7 +56,7 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
97
56
98
57
int mp_hal_stdin_rx_chr (void ) {
99
58
for (;;) {
100
- poll_cdc_interfaces ( );
59
+ mp_usbd_cdc_poll_interfaces ( 0 );
101
60
int c = ringbuf_get (& stdin_ringbuf );
102
61
if (c != -1 ) {
103
62
return c ;
@@ -115,29 +74,10 @@ int mp_hal_stdin_rx_chr(void) {
115
74
mp_uint_t mp_hal_stdout_tx_strn (const char * str , mp_uint_t len ) {
116
75
mp_uint_t ret = len ;
117
76
bool did_write = false;
118
- if (tud_cdc_connected ()) {
119
- size_t i = 0 ;
120
- while (i < len ) {
121
- uint32_t n = len - i ;
122
- if (n > CFG_TUD_CDC_EP_BUFSIZE ) {
123
- n = CFG_TUD_CDC_EP_BUFSIZE ;
124
- }
125
- uint64_t timeout = ticks_us64 () + (uint64_t )(MICROPY_HW_USB_CDC_TX_TIMEOUT * 1000 );
126
- // Wait with a max of USC_CDC_TIMEOUT ms
127
- while (n > tud_cdc_write_available () && ticks_us64 () < timeout ) {
128
- MICROPY_EVENT_POLL_HOOK
129
- }
130
- if (ticks_us64 () >= timeout ) {
131
- ret = i ;
132
- break ;
133
- }
134
-
135
- uint32_t n2 = tud_cdc_write (str + i , n );
136
- tud_cdc_write_flush ();
137
- i += n2 ;
138
- }
77
+ mp_uint_t cdc_res = mp_usbd_cdc_tx_strn (str , len );
78
+ if (cdc_res > 0 ) {
139
79
did_write = true;
140
- ret = MIN (i , ret );
80
+ ret = MIN (cdc_res , ret );
141
81
}
142
82
#if MICROPY_PY_OS_DUPTERM
143
83
int dupterm_res = mp_os_dupterm_tx_strn (str , len );
0 commit comments