Skip to content

Commit e15bb06

Browse files
authored
Merge pull request #2 from imwhocodes/usb-unconnected-stall-fix
fix stall on usb write: try to write only if usb is actually connecte…
2 parents 28371fa + e392b55 commit e15bb06

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

Diff for: cores/esp32/HWCDC.cpp

+9-7
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,14 @@ static void hw_cdc_isr_handler(void *arg) {
139139
}
140140

141141
static void ARDUINO_ISR_ATTR cdc0_write_char(char c) {
142-
if(xPortInIsrContext()){
143-
xRingbufferSendFromISR(tx_ring_buf, (void*) (&c), 1, NULL);
144-
} else {
145-
xRingbufferSend(tx_ring_buf, (void*) (&c), 1, tx_timeout_ms / portTICK_PERIOD_MS);
142+
if(initial_empty){
143+
if(xPortInIsrContext()){
144+
xRingbufferSendFromISR(tx_ring_buf, (void*) (&c), 1, NULL);
145+
} else {
146+
xRingbufferSend(tx_ring_buf, (void*) (&c), 1, tx_timeout_ms / portTICK_PERIOD_MS);
147+
}
148+
usb_serial_jtag_ll_ena_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
146149
}
147-
usb_serial_jtag_ll_ena_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
148150
}
149151

150152
HWCDC::HWCDC() {
@@ -245,7 +247,7 @@ int HWCDC::availableForWrite(void)
245247

246248
size_t HWCDC::write(const uint8_t *buffer, size_t size)
247249
{
248-
if(buffer == NULL || size == 0 || tx_ring_buf == NULL || tx_lock == NULL){
250+
if(buffer == NULL || size == 0 || tx_ring_buf == NULL || tx_lock == NULL || initial_empty == false){
249251
return 0;
250252
}
251253
if(xSemaphoreTake(tx_lock, tx_timeout_ms / portTICK_PERIOD_MS) != pdPASS){
@@ -293,7 +295,7 @@ size_t HWCDC::write(uint8_t c)
293295

294296
void HWCDC::flush(void)
295297
{
296-
if(tx_ring_buf == NULL || tx_lock == NULL){
298+
if(tx_ring_buf == NULL || tx_lock == NULL || initial_empty == false){
297299
return;
298300
}
299301
if(xSemaphoreTake(tx_lock, tx_timeout_ms / portTICK_PERIOD_MS) != pdPASS){

0 commit comments

Comments
 (0)