improve TinyUSB port

add yield to stream timeRead() timePeek() and Serial::bool()
This commit is contained in:
hathach 2019-05-22 00:48:41 +07:00
parent 0f22c475f5
commit 404c9e4068
5 changed files with 20 additions and 23 deletions

View file

@ -64,7 +64,13 @@ void Adafruit_USBD_CDC::end(void)
Adafruit_USBD_CDC::operator bool()
{
return tud_cdc_connected();
bool ret = tud_cdc_connected();
// Add an yield to run usb background in case sketch block wait as follows
// while(!Serial) {}
if ( !ret ) yield();
return ret;
}
int Adafruit_USBD_CDC::available(void)
@ -82,11 +88,6 @@ int Adafruit_USBD_CDC::read(void)
return (int) tud_cdc_read_char();
}
size_t Adafruit_USBD_CDC::readBytes(char *buffer, size_t length)
{
return tud_cdc_read(buffer, length);
}
void Adafruit_USBD_CDC::flush(void)
{
tud_cdc_write_flush();

View file

@ -51,9 +51,6 @@ public:
return write((const uint8_t *)buffer, size);
}
operator bool();
size_t readBytes(char *buffer, size_t length);
size_t readBytes(uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); }
};
extern Adafruit_USBD_CDC Serial;

View file

@ -35,6 +35,7 @@ int Stream::timedRead()
do {
c = read();
if (c >= 0) return c;
yield(); // running TinyUSB task
} while(millis() - _startMillis < _timeout);
return -1; // -1 indicates timeout
}
@ -47,6 +48,7 @@ int Stream::timedPeek()
do {
c = peek();
if (c >= 0) return c;
yield(); // running TinyUSB task
} while(millis() - _startMillis < _timeout);
return -1; // -1 indicates timeout
}

View file

@ -28,7 +28,17 @@
static void __empty() {
// Empty
}
#ifdef USE_TINYUSB
#include "tusb.h"
void yield(void)
{
tud_task();
tud_cdc_write_flush();
}
#else
void yield(void) __attribute__ ((weak, alias("__empty")));
#endif
/**
* SysTick hook

View file

@ -52,23 +52,10 @@ int main( void )
for (;;)
{
loop();
#ifdef USE_TINYUSB
tud_task();
tud_cdc_write_flush();
#endif
yield(); // yield run usb background task
if (serialEventRun) serialEventRun();
}
return 0;
}
#ifdef USE_TINYUSB
void yield(void)
{
tud_task();
tud_cdc_write_flush();
}
#endif