@@ -144,22 +144,21 @@ static void hw_cdc_isr_handler(void *arg) {
144
144
}
145
145
}
146
146
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 ();
150
149
}
151
150
152
151
bool HWCDC::isCDC_Connected () {
153
152
static bool running = false ;
154
153
155
154
// USB may be unplugged
156
- if (!isPlugged ()) {
155
+ if (!isPlugged ()) {
157
156
connected = false ;
158
157
running = false ;
159
- SOF_TIMEOUT = 5 ; // SOF timeout when unplugged
158
+ SOF_TIMEOUT = 5 ; // SOF timeout when unplugged
160
159
return false ;
161
160
} else {
162
- SOF_TIMEOUT = 50 ; // SOF timeout when plugged
161
+ SOF_TIMEOUT = 50 ; // SOF timeout when plugged
163
162
}
164
163
165
164
if (connected) {
@@ -177,15 +176,16 @@ bool HWCDC::isCDC_Connected() {
177
176
return false ;
178
177
}
179
178
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 ;
184
184
vRingbufferGetInfo (tx_ring_buf, NULL , NULL , NULL , NULL , &uxItemsWaiting);
185
185
size_t freeSpace = xRingbufferGetCurFreeSize (tx_ring_buf);
186
186
size_t ringbufferLength = freeSpace + uxItemsWaiting;
187
187
188
- if (buffer == NULL ) {
188
+ if (buffer == NULL ) {
189
189
// just flush the whole ring buffer and exit - used by HWCDC::flush()
190
190
size_t queued_size = 0 ;
191
191
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)
194
194
}
195
195
return ;
196
196
}
197
- if (size == 0 ) {
198
- return ; // nothing to do
197
+ if (size == 0 ) {
198
+ return ; // nothing to do
199
199
}
200
- if (freeSpace >= size){
200
+ if (freeSpace >= size) {
201
201
// 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) {
203
203
return ;
204
204
}
205
205
} else {
206
206
// how many byte should be flushed to make space for the new data
207
207
size_t to_flush = size - freeSpace;
208
- if (to_flush > ringbufferLength) {
208
+ if (to_flush > ringbufferLength) {
209
209
to_flush = ringbufferLength;
210
210
}
211
211
size_t queued_size = 0 ;
@@ -216,10 +216,10 @@ static void flushTXBuffer(const uint8_t *buffer, size_t size)
216
216
// now add the new data that fits to the ring buffer
217
217
uint8_t *bptr = (uint8_t *)buffer;
218
218
if (size >= ringbufferLength) {
219
- size = ringbufferLength;
220
- bptr = (uint8_t *)buffer + (size - ringbufferLength);
219
+ size = ringbufferLength;
220
+ bptr = (uint8_t *)buffer + (size - ringbufferLength);
221
221
}
222
- if (xRingbufferSend (tx_ring_buf, (void *)bptr, size, 0 ) != pdTRUE){
222
+ if (xRingbufferSend (tx_ring_buf, (void *)bptr, size, 0 ) != pdTRUE) {
223
223
return ;
224
224
}
225
225
}
@@ -233,7 +233,7 @@ static void ARDUINO_ISR_ATTR cdc0_write_char(char c) {
233
233
}
234
234
if (!HWCDC::isConnected ()) {
235
235
// just pop/push RingBuffer and apply FIFO policy
236
- flushTXBuffer ((const uint8_t *)&c, 1 );
236
+ flushTXBuffer ((const uint8_t *)&c, 1 );
237
237
return ;
238
238
}
239
239
if (xPortInIsrContext ()) {
@@ -332,7 +332,9 @@ void HWCDC::begin(unsigned long baud) {
332
332
// Enable USB pad function
333
333
USB_SERIAL_JTAG.conf0 .usb_pad_enable = 1 ;
334
334
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
+ );
336
338
if (!intr_handle && esp_intr_alloc (ETS_USB_SERIAL_JTAG_INTR_SOURCE, 0 , hw_cdc_isr_handler, NULL , &intr_handle) != ESP_OK) {
337
339
isr_log_e (" HW USB CDC failed to init interrupts" );
338
340
end ();
@@ -415,11 +417,11 @@ size_t HWCDC::write(const uint8_t *buffer, size_t size) {
415
417
size_t space = xRingbufferGetCurFreeSize (tx_ring_buf);
416
418
size_t to_send = size, so_far = 0 ;
417
419
418
- if (space > size){
420
+ if (space > size) {
419
421
space = size;
420
422
}
421
423
// 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) {
423
425
size = 0 ;
424
426
} else {
425
427
to_send -= space;
@@ -430,15 +432,15 @@ size_t HWCDC::write(const uint8_t *buffer, size_t size) {
430
432
usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
431
433
}
432
434
// 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
435
437
while (connected && to_send) {
436
438
space = xRingbufferGetCurFreeSize (tx_ring_buf);
437
- if (space > to_send){
439
+ if (space > to_send) {
438
440
space = to_send;
439
441
}
440
442
// 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) {
442
444
size = so_far;
443
445
log_w (" write failed due to ring buffer full - timeout" );
444
446
break ;
@@ -456,7 +458,7 @@ size_t HWCDC::write(const uint8_t *buffer, size_t size) {
456
458
delay (1 );
457
459
} else {
458
460
last_toSend = to_send;
459
- tries = tx_timeout_ms; // reset the timeout
461
+ tries = tx_timeout_ms; // reset the timeout
460
462
}
461
463
if (tries == 0 ) { // CDC isn't connected anymore...
462
464
size = so_far;
@@ -466,7 +468,7 @@ size_t HWCDC::write(const uint8_t *buffer, size_t size) {
466
468
}
467
469
}
468
470
// 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 ()) {
470
472
connected = false ;
471
473
flushTXBuffer (buffer + so_far, to_send);
472
474
}
@@ -486,33 +488,33 @@ void HWCDC::flush(void) {
486
488
if (xSemaphoreTake (tx_lock, tx_timeout_ms / portTICK_PERIOD_MS) != pdPASS) {
487
489
return ;
488
490
}
489
- if (!isCDC_Connected ()) {
490
- flushTXBuffer (NULL , 0 );
491
+ if (!isCDC_Connected ()) {
492
+ flushTXBuffer (NULL , 0 );
491
493
} else {
492
494
UBaseType_t uxItemsWaiting = 0 ;
493
495
vRingbufferGetInfo (tx_ring_buf, NULL , NULL , NULL , NULL , &uxItemsWaiting);
494
- if (uxItemsWaiting){
496
+ if (uxItemsWaiting) {
495
497
// Now trigger the ISR to read data from the ring buffer.
496
498
usb_serial_jtag_ll_txfifo_flush ();
497
- if (connected) {
499
+ if (connected) {
498
500
usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
499
501
}
500
502
}
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) {
503
505
delay (1 );
504
506
UBaseType_t lastUxItemsWaiting = uxItemsWaiting;
505
507
vRingbufferGetInfo (tx_ring_buf, NULL , NULL , NULL , NULL , &uxItemsWaiting);
506
508
if (lastUxItemsWaiting == uxItemsWaiting) {
507
509
tries--;
508
510
}
509
- if (connected) {
511
+ if (connected) {
510
512
usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
511
513
}
512
514
}
513
515
if (tries == 0 ) { // CDC isn't connected anymore...
514
516
connected = false ;
515
- flushTXBuffer (NULL , 0 ); // flushes all TX Buffer
517
+ flushTXBuffer (NULL , 0 ); // flushes all TX Buffer
516
518
}
517
519
}
518
520
xSemaphoreGive (tx_lock);
0 commit comments