Skip to content

Commit 73d1072

Browse files
committed
enhance Serial.available()/write() to prevent blocking wait without yield/delay
1 parent b928d9b commit 73d1072

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

cores/nRF5/Adafruit_TinyUSB_Core/Adafruit_USBD_CDC.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,24 @@ void Adafruit_USBD_CDC::end(void)
6464

6565
Adafruit_USBD_CDC::operator bool()
6666
{
67-
return tud_cdc_connected();
67+
bool ret = tud_cdc_connected();
68+
69+
// Add an yield to run usb background in case sketch block wait as follows
70+
// while( !Serial ) {}
71+
if ( !ret ) yield();
72+
73+
return ret;
6874
}
6975

7076
int Adafruit_USBD_CDC::available(void)
7177
{
72-
return tud_cdc_available();
78+
uint32_t count = tud_cdc_available();
79+
80+
// Add an yield to run usb background in case sketch block wait as follows
81+
// while( !Serial.available() ) {}
82+
if (!count) yield();
83+
84+
return count;
7385
}
7486

7587
int Adafruit_USBD_CDC::peek(void)

0 commit comments

Comments
 (0)