@@ -144,22 +144,21 @@ static void hw_cdc_isr_handler(void *arg) {
144144 }
145145}
146146
147- inline bool HWCDC::isPlugged (void )
148- {
149- return (lastSOF_ms + SOF_TIMEOUT) >= millis ();
147+ inline bool HWCDC::isPlugged (void ) {
148+ return (lastSOF_ms + SOF_TIMEOUT) >= millis ();
150149}
151150
152151bool HWCDC::isCDC_Connected () {
153152 static bool running = false ;
154153
155154 // USB may be unplugged
156- if (!isPlugged ()) {
155+ if (!isPlugged ()) {
157156 connected = false ;
158157 running = false ;
159- SOF_TIMEOUT = 5 ; // SOF timeout when unplugged
158+ SOF_TIMEOUT = 5 ; // SOF timeout when unplugged
160159 return false ;
161160 } else {
162- SOF_TIMEOUT = 50 ; // SOF timeout when plugged
161+ SOF_TIMEOUT = 50 ; // SOF timeout when plugged
163162 }
164163
165164 if (connected) {
@@ -177,15 +176,16 @@ bool HWCDC::isCDC_Connected() {
177176 return false ;
178177}
179178
180- static void flushTXBuffer (const uint8_t *buffer, size_t size)
181- {
182- if (!tx_ring_buf) return ;
183- UBaseType_t uxItemsWaiting= 0 ;
179+ static void flushTXBuffer (const uint8_t *buffer, size_t size) {
180+ if (!tx_ring_buf) {
181+ return ;
182+ }
183+ UBaseType_t uxItemsWaiting = 0 ;
184184 vRingbufferGetInfo (tx_ring_buf, NULL , NULL , NULL , NULL , &uxItemsWaiting);
185185 size_t freeSpace = xRingbufferGetCurFreeSize (tx_ring_buf);
186186 size_t ringbufferLength = freeSpace + uxItemsWaiting;
187187
188- if (buffer == NULL ) {
188+ if (buffer == NULL ) {
189189 // just flush the whole ring buffer and exit - used by HWCDC::flush()
190190 size_t queued_size = 0 ;
191191 uint8_t *queued_buff = (uint8_t *)xRingbufferReceiveUpTo (tx_ring_buf, &queued_size, 0 , ringbufferLength);
@@ -194,18 +194,18 @@ static void flushTXBuffer(const uint8_t *buffer, size_t size)
194194 }
195195 return ;
196196 }
197- if (size == 0 ) {
198- return ; // nothing to do
197+ if (size == 0 ) {
198+ return ; // nothing to do
199199 }
200- if (freeSpace >= size){
200+ if (freeSpace >= size) {
201201 // there is enough space, just add the data to the ring buffer
202- if (xRingbufferSend (tx_ring_buf, (void *)buffer, size, 0 ) != pdTRUE){
202+ if (xRingbufferSend (tx_ring_buf, (void *)buffer, size, 0 ) != pdTRUE) {
203203 return ;
204204 }
205205 } else {
206206 // how many byte should be flushed to make space for the new data
207207 size_t to_flush = size - freeSpace;
208- if (to_flush > ringbufferLength) {
208+ if (to_flush > ringbufferLength) {
209209 to_flush = ringbufferLength;
210210 }
211211 size_t queued_size = 0 ;
@@ -216,10 +216,10 @@ static void flushTXBuffer(const uint8_t *buffer, size_t size)
216216 // now add the new data that fits to the ring buffer
217217 uint8_t *bptr = (uint8_t *)buffer;
218218 if (size >= ringbufferLength) {
219- size = ringbufferLength;
220- bptr = (uint8_t *)buffer + (size - ringbufferLength);
219+ size = ringbufferLength;
220+ bptr = (uint8_t *)buffer + (size - ringbufferLength);
221221 }
222- if (xRingbufferSend (tx_ring_buf, (void *)bptr, size, 0 ) != pdTRUE){
222+ if (xRingbufferSend (tx_ring_buf, (void *)bptr, size, 0 ) != pdTRUE) {
223223 return ;
224224 }
225225 }
@@ -233,7 +233,7 @@ static void ARDUINO_ISR_ATTR cdc0_write_char(char c) {
233233 }
234234 if (!HWCDC::isConnected ()) {
235235 // just pop/push RingBuffer and apply FIFO policy
236- flushTXBuffer ((const uint8_t *)&c, 1 );
236+ flushTXBuffer ((const uint8_t *)&c, 1 );
237237 return ;
238238 }
239239 if (xPortInIsrContext ()) {
@@ -332,7 +332,9 @@ void HWCDC::begin(unsigned long baud) {
332332 // Enable USB pad function
333333 USB_SERIAL_JTAG.conf0 .usb_pad_enable = 1 ;
334334 usb_serial_jtag_ll_disable_intr_mask (USB_SERIAL_JTAG_LL_INTR_MASK);
335- usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY | USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT | USB_SERIAL_JTAG_INTR_BUS_RESET | USB_SERIAL_JTAG_INTR_SOF);
335+ usb_serial_jtag_ll_ena_intr_mask (
336+ USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY | USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT | USB_SERIAL_JTAG_INTR_BUS_RESET | USB_SERIAL_JTAG_INTR_SOF
337+ );
336338 if (!intr_handle && esp_intr_alloc (ETS_USB_SERIAL_JTAG_INTR_SOURCE, 0 , hw_cdc_isr_handler, NULL , &intr_handle) != ESP_OK) {
337339 isr_log_e (" HW USB CDC failed to init interrupts" );
338340 end ();
@@ -415,11 +417,11 @@ size_t HWCDC::write(const uint8_t *buffer, size_t size) {
415417 size_t space = xRingbufferGetCurFreeSize (tx_ring_buf);
416418 size_t to_send = size, so_far = 0 ;
417419
418- if (space > size){
420+ if (space > size) {
419421 space = size;
420422 }
421423 // Non-Blocking method, Sending data to ringbuffer, and handle the data in ISR.
422- if (space > 0 && xRingbufferSend (tx_ring_buf, (void *) (buffer), space, 0 ) != pdTRUE){
424+ if (space > 0 && xRingbufferSend (tx_ring_buf, (void *) (buffer), space, 0 ) != pdTRUE) {
423425 size = 0 ;
424426 } else {
425427 to_send -= space;
@@ -430,15 +432,15 @@ size_t HWCDC::write(const uint8_t *buffer, size_t size) {
430432 usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
431433 }
432434 // tracks CDC trasmission progress to avoid hanging if CDC is unplugged while still sending data
433- size_t last_toSend = to_send;
434- uint32_t tries = tx_timeout_ms; // waits 1ms per sending data attempt, in case CDC is unplugged
435+ size_t last_toSend = to_send;
436+ uint32_t tries = tx_timeout_ms; // waits 1ms per sending data attempt, in case CDC is unplugged
435437 while (connected && to_send) {
436438 space = xRingbufferGetCurFreeSize (tx_ring_buf);
437- if (space > to_send){
439+ if (space > to_send) {
438440 space = to_send;
439441 }
440442 // Blocking method, Sending data to ringbuffer, and handle the data in ISR.
441- if (xRingbufferSend (tx_ring_buf, (void *) (buffer+ so_far), space, tx_timeout_ms / portTICK_PERIOD_MS) != pdTRUE) {
443+ if (xRingbufferSend (tx_ring_buf, (void *) (buffer + so_far), space, tx_timeout_ms / portTICK_PERIOD_MS) != pdTRUE) {
442444 size = so_far;
443445 log_w (" write failed due to ring buffer full - timeout" );
444446 break ;
@@ -456,7 +458,7 @@ size_t HWCDC::write(const uint8_t *buffer, size_t size) {
456458 delay (1 );
457459 } else {
458460 last_toSend = to_send;
459- tries = tx_timeout_ms; // reset the timeout
461+ tries = tx_timeout_ms; // reset the timeout
460462 }
461463 if (tries == 0 ) { // CDC isn't connected anymore...
462464 size = so_far;
@@ -466,7 +468,7 @@ size_t HWCDC::write(const uint8_t *buffer, size_t size) {
466468 }
467469 }
468470 // CDC was diconnected while sending data ==> flush the TX buffer keeping the last data
469- if (to_send && !usb_serial_jtag_ll_txfifo_writable ()) {
471+ if (to_send && !usb_serial_jtag_ll_txfifo_writable ()) {
470472 connected = false ;
471473 flushTXBuffer (buffer + so_far, to_send);
472474 }
@@ -486,33 +488,33 @@ void HWCDC::flush(void) {
486488 if (xSemaphoreTake (tx_lock, tx_timeout_ms / portTICK_PERIOD_MS) != pdPASS) {
487489 return ;
488490 }
489- if (!isCDC_Connected ()) {
490- flushTXBuffer (NULL , 0 );
491+ if (!isCDC_Connected ()) {
492+ flushTXBuffer (NULL , 0 );
491493 } else {
492494 UBaseType_t uxItemsWaiting = 0 ;
493495 vRingbufferGetInfo (tx_ring_buf, NULL , NULL , NULL , NULL , &uxItemsWaiting);
494- if (uxItemsWaiting){
496+ if (uxItemsWaiting) {
495497 // Now trigger the ISR to read data from the ring buffer.
496498 usb_serial_jtag_ll_txfifo_flush ();
497- if (connected) {
499+ if (connected) {
498500 usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
499501 }
500502 }
501- uint32_t tries = tx_timeout_ms; // waits 1ms per ISR sending data attempt, in case CDC is unplugged
502- while (connected && tries && uxItemsWaiting){
503+ uint32_t tries = tx_timeout_ms; // waits 1ms per ISR sending data attempt, in case CDC is unplugged
504+ while (connected && tries && uxItemsWaiting) {
503505 delay (1 );
504506 UBaseType_t lastUxItemsWaiting = uxItemsWaiting;
505507 vRingbufferGetInfo (tx_ring_buf, NULL , NULL , NULL , NULL , &uxItemsWaiting);
506508 if (lastUxItemsWaiting == uxItemsWaiting) {
507509 tries--;
508510 }
509- if (connected) {
511+ if (connected) {
510512 usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
511513 }
512514 }
513515 if (tries == 0 ) { // CDC isn't connected anymore...
514516 connected = false ;
515- flushTXBuffer (NULL , 0 ); // flushes all TX Buffer
517+ flushTXBuffer (NULL , 0 ); // flushes all TX Buffer
516518 }
517519 }
518520 xSemaphoreGive (tx_lock);
0 commit comments