Skip to content

Commit 404c9e4

Browse files
committed
improve TinyUSB port
add yield to stream timeRead() timePeek() and Serial::bool()
1 parent 0f22c47 commit 404c9e4

File tree

5 files changed

+20
-23
lines changed

5 files changed

+20
-23
lines changed

cores/arduino/Adafruit_TinyUSB_Core/Adafruit_USBD_CDC.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ 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)
@@ -82,11 +88,6 @@ int Adafruit_USBD_CDC::read(void)
8288
return (int) tud_cdc_read_char();
8389
}
8490

85-
size_t Adafruit_USBD_CDC::readBytes(char *buffer, size_t length)
86-
{
87-
return tud_cdc_read(buffer, length);
88-
}
89-
9091
void Adafruit_USBD_CDC::flush(void)
9192
{
9293
tud_cdc_write_flush();

cores/arduino/Adafruit_TinyUSB_Core/Adafruit_USBD_CDC.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ class Adafruit_USBD_CDC : public Stream, Adafruit_USBD_Interface
5151
return write((const uint8_t *)buffer, size);
5252
}
5353
operator bool();
54-
55-
size_t readBytes(char *buffer, size_t length);
56-
size_t readBytes(uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); }
5754
};
5855

5956
extern Adafruit_USBD_CDC Serial;

cores/arduino/Stream.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ int Stream::timedRead()
3535
do {
3636
c = read();
3737
if (c >= 0) return c;
38+
yield(); // running TinyUSB task
3839
} while(millis() - _startMillis < _timeout);
3940
return -1; // -1 indicates timeout
4041
}
@@ -47,6 +48,7 @@ int Stream::timedPeek()
4748
do {
4849
c = peek();
4950
if (c >= 0) return c;
51+
yield(); // running TinyUSB task
5052
} while(millis() - _startMillis < _timeout);
5153
return -1; // -1 indicates timeout
5254
}

cores/arduino/hooks.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,17 @@
2828
static void __empty() {
2929
// Empty
3030
}
31+
32+
#ifdef USE_TINYUSB
33+
#include "tusb.h"
34+
void yield(void)
35+
{
36+
tud_task();
37+
tud_cdc_write_flush();
38+
}
39+
#else
3140
void yield(void) __attribute__ ((weak, alias("__empty")));
41+
#endif
3242

3343
/**
3444
* SysTick hook

cores/arduino/main.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,10 @@ int main( void )
5252
for (;;)
5353
{
5454
loop();
55-
56-
#ifdef USE_TINYUSB
57-
tud_task();
58-
tud_cdc_write_flush();
59-
#endif
55+
yield(); // yield run usb background task
6056

6157
if (serialEventRun) serialEventRun();
6258
}
6359

6460
return 0;
6561
}
66-
67-
#ifdef USE_TINYUSB
68-
void yield(void)
69-
{
70-
tud_task();
71-
tud_cdc_write_flush();
72-
}
73-
#endif
74-

0 commit comments

Comments
 (0)