Skip to content

Commit 7b42a8f

Browse files
authored
Apply suggestions from code review
1 parent d4eca05 commit 7b42a8f

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

cores/esp32/HWCDC.cpp

+17-22
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,10 @@ static void hw_cdc_isr_handler(void *arg) {
7676
arduino_hw_cdc_event_data_t event = {0};
7777
usbjtag_intr_status = usb_serial_jtag_ll_get_intsts_mask();
7878

79-
// enabling USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY only happen when CDC is connected
8079
if (usbjtag_intr_status & USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY) {
8180
// Interrupt tells us the host picked up the data we sent.
8281
if(!usb_serial_jtag_is_connected()) {
83-
isConnected = false; // reset it when USB is unplugged
82+
isConnected = false;
8483
usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
8584
// USB is unplugged, nothing to be done here
8685
return;
@@ -123,13 +122,13 @@ static void hw_cdc_isr_handler(void *arg) {
123122
}
124123
event.rx.len = i;
125124
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;
127126
}
128127

129128
if (usbjtag_intr_status & USB_SERIAL_JTAG_INTR_BUS_RESET) {
130129
usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_INTR_BUS_RESET);
131130
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;
133132
}
134133

135134
if (xTaskWoken == pdTRUE) {
@@ -138,16 +137,16 @@ static void hw_cdc_isr_handler(void *arg) {
138137
}
139138

140139
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;
142141
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;
144143
}
145144
if(xPortInIsrContext()){
146145
xRingbufferSendFromISR(tx_ring_buf, (void*) (&c), 1, NULL);
147146
} else {
148147
xRingbufferSend(tx_ring_buf, (void*) (&c), 1, tx_timeout_ms / portTICK_PERIOD_MS);
149148
}
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();
151150
}
152151

153152
HWCDC::HWCDC() {
@@ -162,8 +161,6 @@ HWCDC::~HWCDC(){
162161
// It should return <true> just when USB is plugged and CDC is connected.
163162
HWCDC::operator bool() const
164163
{
165-
// <running> deals when this function is called many times
166-
// typically with something like `while(!Serial) delay(10);`
167164
static bool running = false;
168165

169166
// USB may be unplugged
@@ -181,7 +178,7 @@ HWCDC::operator bool() const
181178
if (running == false && !isConnected) { // enables it only once!
182179
usb_serial_jtag_ll_ena_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
183180
}
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
185182
uint8_t c = '\0';
186183
usb_serial_jtag_ll_write_txfifo(&c, sizeof(c));
187184
usb_serial_jtag_ll_txfifo_flush();
@@ -298,12 +295,12 @@ size_t HWCDC::setTxBufferSize(size_t tx_queue_len){
298295

299296
int HWCDC::availableForWrite(void)
300297
{
301-
uint32_t tx_timeout_ms = 0; // if not connected, no timeout
298+
uint32_t tx_timeout_ms = 0;
302299
if(tx_ring_buf == NULL || tx_lock == NULL){
303300
return 0;
304301
}
305302
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;
307304
}
308305
if(xSemaphoreTake(tx_lock, tx_timeout_ms / portTICK_PERIOD_MS) != pdPASS){
309306
return 0;
@@ -330,12 +327,12 @@ static void flushTXBuffer()
330327

331328
size_t HWCDC::write(const uint8_t *buffer, size_t size)
332329
{
333-
uint32_t tx_timeout_ms = 0; // if not connected, no timeout
330+
uint32_t tx_timeout_ms = 0;
334331
if(buffer == NULL || size == 0 || tx_ring_buf == NULL || tx_lock == NULL){
335332
return 0;
336333
}
337334
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;
339336
} else {
340337
isConnected = false;
341338
}
@@ -350,7 +347,6 @@ size_t HWCDC::write(const uint8_t *buffer, size_t size)
350347
space = size;
351348
}
352349
// 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
354350
if(xRingbufferSend(tx_ring_buf, (void*) (buffer), space, 0) != pdTRUE){
355351
size = 0;
356352
} else {
@@ -379,7 +375,7 @@ size_t HWCDC::write(const uint8_t *buffer, size_t size)
379375
// CDC is diconnected ==> flush all data from TX buffer
380376
if(to_send && !usb_serial_jtag_ll_txfifo_writable()) {
381377
isConnected = false;
382-
flushTXBuffer(); // flush TX Ringbuffer
378+
flushTXBuffer();
383379
}
384380
xSemaphoreGive(tx_lock);
385381
return size;
@@ -392,19 +388,18 @@ size_t HWCDC::write(uint8_t c)
392388

393389
void HWCDC::flush(void)
394390
{
395-
uint32_t tx_timeout_ms = 0; // if not connected, no timeout
391+
uint32_t tx_timeout_ms = 0;
396392
if(tx_ring_buf == NULL || tx_lock == NULL){
397393
return;
398394
}
399395
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;
401397
} else {
402398
isConnected = false;
403399
}
404400
if(xSemaphoreTake(tx_lock, tx_timeout_ms / portTICK_PERIOD_MS) != pdPASS){
405401
return;
406402
}
407-
// USB may be plugged, but CDC may be not connected ==> do not block and flush TX buffer, keeping just the lastest data buffered
408403
UBaseType_t uxItemsWaiting = 0;
409404
vRingbufferGetInfo(tx_ring_buf, NULL, NULL, NULL, NULL, &uxItemsWaiting);
410405
if(uxItemsWaiting){
@@ -415,13 +410,13 @@ void HWCDC::flush(void)
415410
uint8_t tries = 3;
416411
while(tries && uxItemsWaiting){
417412
delay(5);
418-
UBaseType_t lastUxItemsWaiting = uxItemsWaiting; // is it flushing CDC?
413+
UBaseType_t lastUxItemsWaiting = uxItemsWaiting;
419414
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--;
421416
}
422417
if (tries == 0) { // CDC isn't connected anymore...
423418
isConnected = false;
424-
flushTXBuffer(); // flush TX Ringbuffer
419+
flushTXBuffer();
425420
}
426421
xSemaphoreGive(tx_lock);
427422
}

0 commit comments

Comments
 (0)