@@ -37,7 +37,7 @@ static QueueHandle_t rx_queue = NULL;
37
37
static uint8_t rx_data_buf[64 ] = {0 };
38
38
static intr_handle_t intr_handle = NULL ;
39
39
static SemaphoreHandle_t tx_lock = NULL ;
40
- static volatile bool isConnected = false ;
40
+ static volatile bool connected = false ;
41
41
42
42
// timeout has no effect when USB CDC is unplugged
43
43
static uint32_t requested_tx_timeout_ms = 100 ;
@@ -79,12 +79,12 @@ static void hw_cdc_isr_handler(void *arg) {
79
79
if (usbjtag_intr_status & USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY) {
80
80
// Interrupt tells us the host picked up the data we sent.
81
81
if (!usb_serial_jtag_is_connected ()) {
82
- isConnected = false ;
82
+ connected = false ;
83
83
usb_serial_jtag_ll_clr_intsts_mask (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
84
84
// USB is unplugged, nothing to be done here
85
85
return ;
86
86
} else {
87
- isConnected = true ;
87
+ connected = true ;
88
88
}
89
89
if (usb_serial_jtag_ll_txfifo_writable () == 1 ) {
90
90
// We disable the interrupt here so that the interrupt won't be triggered if there is no data to send.
@@ -98,7 +98,7 @@ static void hw_cdc_isr_handler(void *arg) {
98
98
usb_serial_jtag_ll_write_txfifo (queued_buff, queued_size);
99
99
usb_serial_jtag_ll_txfifo_flush ();
100
100
vRingbufferReturnItemFromISR (tx_ring_buf, queued_buff, &xTaskWoken);
101
- if (isConnected ) usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
101
+ if (connected ) usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
102
102
// send event?
103
103
// ets_printf("TX:%u\n", queued_size);
104
104
event.tx .len = queued_size;
@@ -122,23 +122,50 @@ static void hw_cdc_isr_handler(void *arg) {
122
122
}
123
123
event.rx .len = i;
124
124
arduino_hw_cdc_event_post (ARDUINO_HW_CDC_EVENTS, ARDUINO_HW_CDC_RX_EVENT, &event, sizeof (arduino_hw_cdc_event_data_t ), &xTaskWoken);
125
- isConnected = true ;
125
+ connected = true ;
126
126
}
127
127
128
128
if (usbjtag_intr_status & USB_SERIAL_JTAG_INTR_BUS_RESET) {
129
129
usb_serial_jtag_ll_clr_intsts_mask (USB_SERIAL_JTAG_INTR_BUS_RESET);
130
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);
131
- isConnected = false ;
131
+ connected = false ;
132
132
}
133
133
134
134
if (xTaskWoken == pdTRUE) {
135
135
portYIELD_FROM_ISR ();
136
136
}
137
137
}
138
138
139
+ bool HWCDC::isCDC_Connected ()
140
+ {
141
+ static bool running = false ;
142
+
143
+ // USB may be unplugged
144
+ if (usb_serial_jtag_is_connected () == false ) {
145
+ connected = false ;
146
+ running = false ;
147
+ return false ;
148
+ }
149
+
150
+ if (connected) {
151
+ running = false ;
152
+ return true ;
153
+ }
154
+
155
+ if (running == false && !connected) { // enables it only once!
156
+ usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
157
+ }
158
+ // this will feed CDC TX FIFO to trigger IN_EMPTY
159
+ uint8_t c = ' \0 ' ;
160
+ usb_serial_jtag_ll_write_txfifo (&c, sizeof (c));
161
+ usb_serial_jtag_ll_txfifo_flush ();
162
+ running = true ;
163
+ return false ;
164
+ }
165
+
139
166
static void ARDUINO_ISR_ATTR cdc0_write_char (char c) {
140
167
uint32_t tx_timeout_ms = 0 ;
141
- if (usb_serial_jtag_is_connected ()) {
168
+ if (HWCDC::isConnected ()) {
142
169
tx_timeout_ms = requested_tx_timeout_ms;
143
170
}
144
171
if (xPortInIsrContext ()){
@@ -157,33 +184,10 @@ HWCDC::~HWCDC(){
157
184
end ();
158
185
}
159
186
160
-
161
187
// It should return <true> just when USB is plugged and CDC is connected.
162
188
HWCDC::operator bool () const
163
189
{
164
- static bool running = false ;
165
-
166
- // USB may be unplugged
167
- if (usb_serial_jtag_is_connected () == false ) {
168
- isConnected = false ;
169
- running = false ;
170
- return false ;
171
- }
172
-
173
- if (isConnected) {
174
- running = false ;
175
- return true ;
176
- }
177
-
178
- if (running == false && !isConnected) { // enables it only once!
179
- usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
180
- }
181
- // this will feed CDC TX FIFO to trigger IN_EMPTY
182
- uint8_t c = ' \0 ' ;
183
- usb_serial_jtag_ll_write_txfifo (&c, sizeof (c));
184
- usb_serial_jtag_ll_txfifo_flush ();
185
- running = true ;
186
- return false ;
190
+ return HWCDC::isCDC_Connected ();
187
191
}
188
192
189
193
void HWCDC::onEvent (esp_event_handler_t callback){
@@ -267,7 +271,7 @@ void HWCDC::end()
267
271
arduino_hw_cdc_event_loop_handle = NULL ;
268
272
}
269
273
HWCDC::deinit (this );
270
- isConnected = false ;
274
+ connected = false ;
271
275
}
272
276
273
277
void HWCDC::setTxTimeoutMs (uint32_t timeout){
@@ -299,7 +303,7 @@ int HWCDC::availableForWrite(void)
299
303
if (tx_ring_buf == NULL || tx_lock == NULL ){
300
304
return 0 ;
301
305
}
302
- if (usb_serial_jtag_is_connected ()) {
306
+ if (HWCDC::isCDC_Connected ()) {
303
307
tx_timeout_ms = requested_tx_timeout_ms;
304
308
}
305
309
if (xSemaphoreTake (tx_lock, tx_timeout_ms / portTICK_PERIOD_MS) != pdPASS){
@@ -331,10 +335,10 @@ size_t HWCDC::write(const uint8_t *buffer, size_t size)
331
335
if (buffer == NULL || size == 0 || tx_ring_buf == NULL || tx_lock == NULL ){
332
336
return 0 ;
333
337
}
334
- if (usb_serial_jtag_is_connected ()) {
338
+ if (HWCDC::isCDC_Connected ()) {
335
339
tx_timeout_ms = requested_tx_timeout_ms;
336
340
} else {
337
- isConnected = false ;
341
+ connected = false ;
338
342
}
339
343
if (xSemaphoreTake (tx_lock, tx_timeout_ms / portTICK_PERIOD_MS) != pdPASS){
340
344
return 0 ;
@@ -354,7 +358,7 @@ size_t HWCDC::write(const uint8_t *buffer, size_t size)
354
358
so_far += space;
355
359
// Now trigger the ISR to read data from the ring buffer.
356
360
usb_serial_jtag_ll_txfifo_flush ();
357
- if (isConnected ) usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
361
+ if (connected ) usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
358
362
359
363
while (to_send){
360
364
if (max_size > to_send){
@@ -369,12 +373,12 @@ size_t HWCDC::write(const uint8_t *buffer, size_t size)
369
373
to_send -= max_size;
370
374
// Now trigger the ISR to read data from the ring buffer.
371
375
usb_serial_jtag_ll_txfifo_flush ();
372
- if (isConnected ) usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
376
+ if (connected ) usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
373
377
}
374
378
}
375
379
// CDC is diconnected ==> flush all data from TX buffer
376
380
if (to_send && !usb_serial_jtag_ll_txfifo_writable ()) {
377
- isConnected = false ;
381
+ connected = false ;
378
382
flushTXBuffer ();
379
383
}
380
384
xSemaphoreGive (tx_lock);
@@ -392,10 +396,10 @@ void HWCDC::flush(void)
392
396
if (tx_ring_buf == NULL || tx_lock == NULL ){
393
397
return ;
394
398
}
395
- if (usb_serial_jtag_is_connected ()) {
399
+ if (HWCDC::isCDC_Connected ()) {
396
400
tx_timeout_ms = requested_tx_timeout_ms;
397
401
} else {
398
- isConnected = false ;
402
+ connected = false ;
399
403
}
400
404
if (xSemaphoreTake (tx_lock, tx_timeout_ms / portTICK_PERIOD_MS) != pdPASS){
401
405
return ;
@@ -405,7 +409,7 @@ void HWCDC::flush(void)
405
409
if (uxItemsWaiting){
406
410
// Now trigger the ISR to read data from the ring buffer.
407
411
usb_serial_jtag_ll_txfifo_flush ();
408
- if (isConnected ) usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
412
+ if (connected ) usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
409
413
}
410
414
uint8_t tries = 3 ;
411
415
while (tries && uxItemsWaiting){
@@ -415,7 +419,7 @@ void HWCDC::flush(void)
415
419
if (lastUxItemsWaiting == uxItemsWaiting) tries--;
416
420
}
417
421
if (tries == 0 ) { // CDC isn't connected anymore...
418
- isConnected = false ;
422
+ connected = false ;
419
423
flushTXBuffer ();
420
424
}
421
425
xSemaphoreGive (tx_lock);
0 commit comments