@@ -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,58 +122,37 @@ 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
- static void ARDUINO_ISR_ATTR cdc0_write_char (char c) {
140
- uint32_t tx_timeout_ms = 0 ;
141
- if (isCDC_Connected ()) {
142
- tx_timeout_ms = requested_tx_timeout_ms;
143
- }
144
- if (xPortInIsrContext ()){
145
- xRingbufferSendFromISR (tx_ring_buf, (void *) (&c), 1 , NULL );
146
- } else {
147
- xRingbufferSend (tx_ring_buf, (void *) (&c), 1 , tx_timeout_ms / portTICK_PERIOD_MS);
148
- }
149
- usb_serial_jtag_ll_txfifo_flush ();
150
- }
151
-
152
- HWCDC::HWCDC () {
153
-
154
- }
155
-
156
- HWCDC::~HWCDC (){
157
- end ();
158
- }
159
-
160
139
bool HWCDC::isCDC_Connected ()
161
140
{
162
141
static bool running = false ;
163
142
164
143
// USB may be unplugged
165
144
if (usb_serial_jtag_is_connected () == false ) {
166
- isConnected = false ;
145
+ connected = false ;
167
146
running = false ;
168
147
return false ;
169
148
}
170
149
171
- if (isConnected ) {
150
+ if (connected ) {
172
151
running = false ;
173
152
return true ;
174
153
}
175
154
176
- if (running == false && !isConnected ) { // enables it only once!
155
+ if (running == false && !connected ) { // enables it only once!
177
156
usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
178
157
}
179
158
// this will feed CDC TX FIFO to trigger IN_EMPTY
@@ -184,10 +163,31 @@ bool HWCDC::isCDC_Connected()
184
163
return false ;
185
164
}
186
165
166
+ static void ARDUINO_ISR_ATTR cdc0_write_char (char c) {
167
+ uint32_t tx_timeout_ms = 0 ;
168
+ if (HWCDC::isConnected ()) {
169
+ tx_timeout_ms = requested_tx_timeout_ms;
170
+ }
171
+ if (xPortInIsrContext ()){
172
+ xRingbufferSendFromISR (tx_ring_buf, (void *) (&c), 1 , NULL );
173
+ } else {
174
+ xRingbufferSend (tx_ring_buf, (void *) (&c), 1 , tx_timeout_ms / portTICK_PERIOD_MS);
175
+ }
176
+ usb_serial_jtag_ll_txfifo_flush ();
177
+ }
178
+
179
+ HWCDC::HWCDC () {
180
+
181
+ }
182
+
183
+ HWCDC::~HWCDC (){
184
+ end ();
185
+ }
186
+
187
187
// It should return <true> just when USB is plugged and CDC is connected.
188
188
HWCDC::operator bool () const
189
189
{
190
- return isCDC_Connected ();
190
+ return HWCDC:: isCDC_Connected ();
191
191
}
192
192
193
193
void HWCDC::onEvent (esp_event_handler_t callback){
@@ -271,7 +271,7 @@ void HWCDC::end()
271
271
arduino_hw_cdc_event_loop_handle = NULL ;
272
272
}
273
273
HWCDC::deinit (this );
274
- isConnected = false ;
274
+ connected = false ;
275
275
}
276
276
277
277
void HWCDC::setTxTimeoutMs (uint32_t timeout){
@@ -303,7 +303,7 @@ int HWCDC::availableForWrite(void)
303
303
if (tx_ring_buf == NULL || tx_lock == NULL ){
304
304
return 0 ;
305
305
}
306
- if (isCDC_Connected ()) {
306
+ if (HWCDC:: isCDC_Connected ()) {
307
307
tx_timeout_ms = requested_tx_timeout_ms;
308
308
}
309
309
if (xSemaphoreTake (tx_lock, tx_timeout_ms / portTICK_PERIOD_MS) != pdPASS){
@@ -335,10 +335,10 @@ size_t HWCDC::write(const uint8_t *buffer, size_t size)
335
335
if (buffer == NULL || size == 0 || tx_ring_buf == NULL || tx_lock == NULL ){
336
336
return 0 ;
337
337
}
338
- if (isCDC_Connected ()) {
338
+ if (HWCDC:: isCDC_Connected ()) {
339
339
tx_timeout_ms = requested_tx_timeout_ms;
340
340
} else {
341
- isConnected = false ;
341
+ connected = false ;
342
342
}
343
343
if (xSemaphoreTake (tx_lock, tx_timeout_ms / portTICK_PERIOD_MS) != pdPASS){
344
344
return 0 ;
@@ -358,7 +358,7 @@ size_t HWCDC::write(const uint8_t *buffer, size_t size)
358
358
so_far += space;
359
359
// Now trigger the ISR to read data from the ring buffer.
360
360
usb_serial_jtag_ll_txfifo_flush ();
361
- 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);
362
362
363
363
while (to_send){
364
364
if (max_size > to_send){
@@ -373,12 +373,12 @@ size_t HWCDC::write(const uint8_t *buffer, size_t size)
373
373
to_send -= max_size;
374
374
// Now trigger the ISR to read data from the ring buffer.
375
375
usb_serial_jtag_ll_txfifo_flush ();
376
- 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);
377
377
}
378
378
}
379
379
// CDC is diconnected ==> flush all data from TX buffer
380
380
if (to_send && !usb_serial_jtag_ll_txfifo_writable ()) {
381
- isConnected = false ;
381
+ connected = false ;
382
382
flushTXBuffer ();
383
383
}
384
384
xSemaphoreGive (tx_lock);
@@ -396,10 +396,10 @@ void HWCDC::flush(void)
396
396
if (tx_ring_buf == NULL || tx_lock == NULL ){
397
397
return ;
398
398
}
399
- if (isCDC_Connected ()) {
399
+ if (HWCDC:: isCDC_Connected ()) {
400
400
tx_timeout_ms = requested_tx_timeout_ms;
401
401
} else {
402
- isConnected = false ;
402
+ connected = false ;
403
403
}
404
404
if (xSemaphoreTake (tx_lock, tx_timeout_ms / portTICK_PERIOD_MS) != pdPASS){
405
405
return ;
@@ -409,7 +409,7 @@ void HWCDC::flush(void)
409
409
if (uxItemsWaiting){
410
410
// Now trigger the ISR to read data from the ring buffer.
411
411
usb_serial_jtag_ll_txfifo_flush ();
412
- 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);
413
413
}
414
414
uint8_t tries = 3 ;
415
415
while (tries && uxItemsWaiting){
@@ -419,7 +419,7 @@ void HWCDC::flush(void)
419
419
if (lastUxItemsWaiting == uxItemsWaiting) tries--;
420
420
}
421
421
if (tries == 0 ) { // CDC isn't connected anymore...
422
- isConnected = false ;
422
+ connected = false ;
423
423
flushTXBuffer ();
424
424
}
425
425
xSemaphoreGive (tx_lock);
0 commit comments