@@ -64,13 +64,47 @@ void tud_cdc_rx_cb(uint8_t itf)
6464 }
6565}
6666
67+
68+ static size_t tinyusb_cdc_write (uint8_t itf, const uint8_t *buffer, size_t size){
69+ if (itf >= MAX_USB_CDC_DEVICES){
70+ return 0 ;
71+ }
72+ if (!tud_cdc_n_connected (itf)){
73+ return 0 ;
74+ }
75+ size_t tosend = size, sofar = 0 ;
76+ while (tosend){
77+ uint32_t space = tud_cdc_n_write_available (itf);
78+ if (!space){
79+ delay (1 );
80+ continue ;
81+ }
82+ if (tosend < space){
83+ space = tosend;
84+ }
85+ uint32_t sent = tud_cdc_n_write (itf, buffer + sofar, space);
86+ if (!sent){
87+ return sofar;
88+ }
89+ sofar += sent;
90+ tosend -= sent;
91+ tud_cdc_n_write_flush (itf);
92+ }
93+ return sofar;
94+ }
95+
96+ static void ARDUINO_ISR_ATTR cdc0_write_char (char c)
97+ {
98+ tinyusb_cdc_write (0 , (const uint8_t *)&c, 1 );
99+ }
100+
67101// void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char);
68102
69103static void usb_unplugged_cb (void * arg, esp_event_base_t event_base, int32_t event_id, void * event_data){
70104 ((USBCDC*)arg)->_onUnplugged ();
71105}
72106
73- USBCDC::USBCDC (uint8_t itfn) : itf(itfn), bit_rate(0 ), stop_bits(0 ), parity(0 ), data_bits(0 ), dtr(false ), rts(false ), connected(usb_persist_this_boot() ), reboot_enable(true ), rx_queue(NULL ) {
107+ USBCDC::USBCDC (uint8_t itfn) : itf(itfn), bit_rate(0 ), stop_bits(0 ), parity(0 ), data_bits(0 ), dtr(false ), rts(false ), connected(false ), reboot_enable(true ), rx_queue(NULL ) {
74108 tinyusb_enable_interface (USB_INTERFACE_CDC, TUD_CDC_DESC_LEN, load_cdc_descriptor);
75109 if (itf < MAX_USB_CDC_DEVICES){
76110 devices[itf] = this ;
@@ -94,15 +128,6 @@ void USBCDC::begin(size_t rx_queue_len)
94128 if (!rx_queue){
95129 return ;
96130 }
97- if (connected){
98- arduino_usb_cdc_event_data_t p = {0 };
99- arduino_usb_event_post (ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_CONNECTED_EVENT, &p, sizeof (arduino_usb_cdc_event_data_t ), portMAX_DELAY);
100-
101- arduino_usb_cdc_event_data_t l = {0 };
102- l.line_state .dtr = true ;
103- l.line_state .rts = true ;
104- arduino_usb_event_post (ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_LINE_STATE_EVENT, &l, sizeof (arduino_usb_cdc_event_data_t ), portMAX_DELAY);
105- }
106131}
107132
108133void USBCDC::end ()
@@ -167,8 +192,6 @@ void USBCDC::_onLineState(bool _dtr, bool _rts){
167192 l.line_state .dtr = dtr;
168193 l.line_state .rts = rts;
169194 arduino_usb_event_post (ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_LINE_STATE_EVENT, &l, sizeof (arduino_usb_cdc_event_data_t ), portMAX_DELAY);
170- } else {
171- log_d (" CDC RESET: itf: %u, dtr: %u, rts: %u, state: %u" , itf, dtr, rts, lineState);
172195 }
173196
174197}
@@ -193,7 +216,6 @@ void USBCDC::_onRX(){
193216 uint32_t count = tud_cdc_n_read (itf, buf, CONFIG_USB_CDC_RX_BUFSIZE);
194217 for (uint32_t i=0 ; i<count; i++){
195218 if (rx_queue == NULL || !xQueueSend (rx_queue, buf+i, 0 )){
196- log_e (" read failed" );
197219 return ;
198220 }
199221 }
@@ -271,29 +293,7 @@ int USBCDC::availableForWrite(void)
271293}
272294
273295size_t USBCDC::write (const uint8_t *buffer, size_t size){
274- if (itf >= MAX_USB_CDC_DEVICES){
275- return 0 ;
276- }
277- size_t tosend = size, sofar = 0 ;
278- while (tosend){
279- uint32_t space = tud_cdc_n_write_available (itf);
280- if (!space){
281- delay (1 );
282- continue ;
283- }
284- if (tosend < space){
285- space = tosend;
286- }
287- uint32_t sent = tud_cdc_n_write (itf, buffer + sofar, space);
288- if (!sent){
289- log_e (" tud_cdc_n_write failed!" );
290- return sofar;
291- }
292- sofar += sent;
293- tosend -= sent;
294- tud_cdc_n_write_flush (itf);
295- }
296- return sofar;
296+ return tinyusb_cdc_write (itf, buffer, size);
297297}
298298
299299size_t USBCDC::write (uint8_t c)
@@ -309,6 +309,15 @@ uint32_t USBCDC::baudRate()
309309 return bit_rate;
310310}
311311
312+ void USBCDC::setDebugOutput (bool en)
313+ {
314+ if (en) {
315+ ets_install_putc1 ((void (*)(char )) &cdc0_write_char);
316+ } else {
317+ ets_install_putc1 (NULL );
318+ }
319+ }
320+
312321USBCDC::operator bool () const
313322{
314323 if (itf >= MAX_USB_CDC_DEVICES){
0 commit comments