@@ -76,11 +76,10 @@ static void hw_cdc_isr_handler(void *arg) {
76
76
arduino_hw_cdc_event_data_t event = {0 };
77
77
usbjtag_intr_status = usb_serial_jtag_ll_get_intsts_mask ();
78
78
79
- // enabling USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY only happen when CDC is connected
80
79
if (usbjtag_intr_status & USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY) {
81
80
// Interrupt tells us the host picked up the data we sent.
82
81
if (!usb_serial_jtag_is_connected ()) {
83
- isConnected = false ; // reset it when USB is unplugged
82
+ isConnected = false ;
84
83
usb_serial_jtag_ll_clr_intsts_mask (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
85
84
// USB is unplugged, nothing to be done here
86
85
return ;
@@ -123,13 +122,13 @@ static void hw_cdc_isr_handler(void *arg) {
123
122
}
124
123
event.rx .len = i;
125
124
arduino_hw_cdc_event_post (ARDUINO_HW_CDC_EVENTS, ARDUINO_HW_CDC_RX_EVENT, &event, sizeof (arduino_hw_cdc_event_data_t ), &xTaskWoken);
126
- isConnected = true ; // receiving data also means that CDC is connected!
125
+ isConnected = true ;
127
126
}
128
127
129
128
if (usbjtag_intr_status & USB_SERIAL_JTAG_INTR_BUS_RESET) {
130
129
usb_serial_jtag_ll_clr_intsts_mask (USB_SERIAL_JTAG_INTR_BUS_RESET);
131
130
arduino_hw_cdc_event_post (ARDUINO_HW_CDC_EVENTS, ARDUINO_HW_CDC_BUS_RESET_EVENT, &event, sizeof (arduino_hw_cdc_event_data_t ), &xTaskWoken);
132
- isConnected = false ; // TX/RX only takes place after USB BUS resets (it was just plugged)
131
+ isConnected = false ;
133
132
}
134
133
135
134
if (xTaskWoken == pdTRUE) {
@@ -138,16 +137,16 @@ static void hw_cdc_isr_handler(void *arg) {
138
137
}
139
138
140
139
static void ARDUINO_ISR_ATTR cdc0_write_char (char c) {
141
- uint32_t tx_timeout_ms = 0 ; // if not connected, no timeout
140
+ uint32_t tx_timeout_ms = 0 ;
142
141
if (usb_serial_jtag_is_connected ()) {
143
- tx_timeout_ms = requested_tx_timeout_ms; // once connected, restores the TX timeout
142
+ tx_timeout_ms = requested_tx_timeout_ms;
144
143
}
145
144
if (xPortInIsrContext ()){
146
145
xRingbufferSendFromISR (tx_ring_buf, (void *) (&c), 1 , NULL );
147
146
} else {
148
147
xRingbufferSend (tx_ring_buf, (void *) (&c), 1 , tx_timeout_ms / portTICK_PERIOD_MS);
149
148
}
150
- usb_serial_jtag_ll_txfifo_flush (); // flushes HW Serial CDC and sets IN_EMPTY when Host reads data
149
+ usb_serial_jtag_ll_txfifo_flush ();
151
150
}
152
151
153
152
HWCDC::HWCDC () {
@@ -162,8 +161,6 @@ HWCDC::~HWCDC(){
162
161
// It should return <true> just when USB is plugged and CDC is connected.
163
162
HWCDC::operator bool () const
164
163
{
165
- // <running> deals when this function is called many times
166
- // typically with something like `while(!Serial) delay(10);`
167
164
static bool running = false ;
168
165
169
166
// USB may be unplugged
@@ -181,7 +178,7 @@ HWCDC::operator bool() const
181
178
if (running == false && !isConnected) { // enables it only once!
182
179
usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
183
180
}
184
- // this will feed CDC TX FIFO and then trigger when CDC is connected
181
+ // this will feed CDC TX FIFO to trigger IN_EMPTY
185
182
uint8_t c = ' \0 ' ;
186
183
usb_serial_jtag_ll_write_txfifo (&c, sizeof (c));
187
184
usb_serial_jtag_ll_txfifo_flush ();
@@ -298,12 +295,12 @@ size_t HWCDC::setTxBufferSize(size_t tx_queue_len){
298
295
299
296
int HWCDC::availableForWrite (void )
300
297
{
301
- uint32_t tx_timeout_ms = 0 ; // if not connected, no timeout
298
+ uint32_t tx_timeout_ms = 0 ;
302
299
if (tx_ring_buf == NULL || tx_lock == NULL ){
303
300
return 0 ;
304
301
}
305
302
if (usb_serial_jtag_is_connected ()) {
306
- tx_timeout_ms = requested_tx_timeout_ms; // once connected, restores the TX timeout
303
+ tx_timeout_ms = requested_tx_timeout_ms;
307
304
}
308
305
if (xSemaphoreTake (tx_lock, tx_timeout_ms / portTICK_PERIOD_MS) != pdPASS){
309
306
return 0 ;
@@ -330,12 +327,12 @@ static void flushTXBuffer()
330
327
331
328
size_t HWCDC::write (const uint8_t *buffer, size_t size)
332
329
{
333
- uint32_t tx_timeout_ms = 0 ; // if not connected, no timeout
330
+ uint32_t tx_timeout_ms = 0 ;
334
331
if (buffer == NULL || size == 0 || tx_ring_buf == NULL || tx_lock == NULL ){
335
332
return 0 ;
336
333
}
337
334
if (usb_serial_jtag_is_connected ()) {
338
- tx_timeout_ms = requested_tx_timeout_ms; // once connected, restores the TX timeout
335
+ tx_timeout_ms = requested_tx_timeout_ms;
339
336
} else {
340
337
isConnected = false ;
341
338
}
@@ -350,7 +347,6 @@ size_t HWCDC::write(const uint8_t *buffer, size_t size)
350
347
space = size;
351
348
}
352
349
// Non-Blocking method, Sending data to ringbuffer, and handle the data in ISR.
353
- // USB may be plugged, but CDC may be not connected ==> do not block and flush TX buffer, keeping just the lastest data buffered
354
350
if (xRingbufferSend (tx_ring_buf, (void *) (buffer), space, 0 ) != pdTRUE){
355
351
size = 0 ;
356
352
} else {
@@ -379,7 +375,7 @@ size_t HWCDC::write(const uint8_t *buffer, size_t size)
379
375
// CDC is diconnected ==> flush all data from TX buffer
380
376
if (to_send && !usb_serial_jtag_ll_txfifo_writable ()) {
381
377
isConnected = false ;
382
- flushTXBuffer (); // flush TX Ringbuffer
378
+ flushTXBuffer ();
383
379
}
384
380
xSemaphoreGive (tx_lock);
385
381
return size;
@@ -392,19 +388,18 @@ size_t HWCDC::write(uint8_t c)
392
388
393
389
void HWCDC::flush (void )
394
390
{
395
- uint32_t tx_timeout_ms = 0 ; // if not connected, no timeout
391
+ uint32_t tx_timeout_ms = 0 ;
396
392
if (tx_ring_buf == NULL || tx_lock == NULL ){
397
393
return ;
398
394
}
399
395
if (usb_serial_jtag_is_connected ()) {
400
- tx_timeout_ms = requested_tx_timeout_ms; // once connected, restores the TX timeout
396
+ tx_timeout_ms = requested_tx_timeout_ms;
401
397
} else {
402
398
isConnected = false ;
403
399
}
404
400
if (xSemaphoreTake (tx_lock, tx_timeout_ms / portTICK_PERIOD_MS) != pdPASS){
405
401
return ;
406
402
}
407
- // USB may be plugged, but CDC may be not connected ==> do not block and flush TX buffer, keeping just the lastest data buffered
408
403
UBaseType_t uxItemsWaiting = 0 ;
409
404
vRingbufferGetInfo (tx_ring_buf, NULL , NULL , NULL , NULL , &uxItemsWaiting);
410
405
if (uxItemsWaiting){
@@ -415,13 +410,13 @@ void HWCDC::flush(void)
415
410
uint8_t tries = 3 ;
416
411
while (tries && uxItemsWaiting){
417
412
delay (5 );
418
- UBaseType_t lastUxItemsWaiting = uxItemsWaiting; // is it flushing CDC?
413
+ UBaseType_t lastUxItemsWaiting = uxItemsWaiting;
419
414
vRingbufferGetInfo (tx_ring_buf, NULL , NULL , NULL , NULL , &uxItemsWaiting);
420
- if (lastUxItemsWaiting == uxItemsWaiting) tries--; // avoids locking when USB is plugged, but CDC is not connected
415
+ if (lastUxItemsWaiting == uxItemsWaiting) tries--;
421
416
}
422
417
if (tries == 0 ) { // CDC isn't connected anymore...
423
418
isConnected = false ;
424
- flushTXBuffer (); // flush TX Ringbuffer
419
+ flushTXBuffer ();
425
420
}
426
421
xSemaphoreGive (tx_lock);
427
422
}
0 commit comments