Skip to content

Commit 32b2fa7

Browse files
authored
feat(hwcdc): fix delay
fixes delay when CDC is not connected. It was only considering when the USB cable was not plugged.
1 parent 52da230 commit 32b2fa7

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

Diff for: cores/esp32/HWCDC.cpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ static void hw_cdc_isr_handler(void *arg) {
138138

139139
static void ARDUINO_ISR_ATTR cdc0_write_char(char c) {
140140
uint32_t tx_timeout_ms = 0;
141-
if(usb_serial_jtag_is_connected()) {
141+
if(isCDC_Connected()) {
142142
tx_timeout_ms = requested_tx_timeout_ms;
143143
}
144144
if(xPortInIsrContext()){
@@ -157,9 +157,7 @@ HWCDC::~HWCDC(){
157157
end();
158158
}
159159

160-
161-
// It should return <true> just when USB is plugged and CDC is connected.
162-
HWCDC::operator bool() const
160+
bool HWCDC::isCDC_Connected()
163161
{
164162
static bool running = false;
165163

@@ -183,7 +181,13 @@ HWCDC::operator bool() const
183181
usb_serial_jtag_ll_write_txfifo(&c, sizeof(c));
184182
usb_serial_jtag_ll_txfifo_flush();
185183
running = true;
186-
return false;
184+
return false;
185+
}
186+
187+
// It should return <true> just when USB is plugged and CDC is connected.
188+
HWCDC::operator bool() const
189+
{
190+
return isCDC_Connected();
187191
}
188192

189193
void HWCDC::onEvent(esp_event_handler_t callback){
@@ -299,7 +303,7 @@ int HWCDC::availableForWrite(void)
299303
if(tx_ring_buf == NULL || tx_lock == NULL){
300304
return 0;
301305
}
302-
if(usb_serial_jtag_is_connected()) {
306+
if(isCDC_Connected()) {
303307
tx_timeout_ms = requested_tx_timeout_ms;
304308
}
305309
if(xSemaphoreTake(tx_lock, tx_timeout_ms / portTICK_PERIOD_MS) != pdPASS){
@@ -331,7 +335,7 @@ size_t HWCDC::write(const uint8_t *buffer, size_t size)
331335
if(buffer == NULL || size == 0 || tx_ring_buf == NULL || tx_lock == NULL){
332336
return 0;
333337
}
334-
if(usb_serial_jtag_is_connected()) {
338+
if(isCDC_Connected()) {
335339
tx_timeout_ms = requested_tx_timeout_ms;
336340
} else {
337341
isConnected = false;
@@ -392,7 +396,7 @@ void HWCDC::flush(void)
392396
if(tx_ring_buf == NULL || tx_lock == NULL){
393397
return;
394398
}
395-
if(usb_serial_jtag_is_connected()) {
399+
if(isCDC_Connected()) {
396400
tx_timeout_ms = requested_tx_timeout_ms;
397401
} else {
398402
isConnected = false;

0 commit comments

Comments
 (0)