Skip to content

Commit 8f7fe50

Browse files
committed
- add CDC/serial_echo example using SerialTinyUSB
- call CDC flush in task() for core without tinyusb builtin support
1 parent 2179aeb commit 8f7fe50

File tree

5 files changed

+57
-8
lines changed

5 files changed

+57
-8
lines changed

examples/CDC/serial_echo/.pico_rp2040_tinyusb_host.test.skip

Whitespace-only changes.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*********************************************************************
2+
Adafruit invests time and resources providing this open source code,
3+
please support Adafruit and open-source hardware by purchasing
4+
products from Adafruit!
5+
6+
MIT license, check LICENSE for more information
7+
Copyright (c) 2019 Ha Thach for Adafruit Industries
8+
All text above, and the splash screen below must be included in
9+
any redistribution
10+
*********************************************************************/
11+
12+
#include "Adafruit_TinyUSB.h"
13+
14+
/* This sketch demonstrates USB CDC Serial echo (convert to upper case) using SerialTinyUSB which
15+
* is available for both core with built-in USB support and without.
16+
*/
17+
18+
void setup() {
19+
// Manual begin() is required on core without built-in support e.g. mbed rp2040
20+
if (!TinyUSBDevice.isInitialized()) {
21+
TinyUSBDevice.begin(0);
22+
}
23+
}
24+
25+
void loop() {
26+
#ifdef TINYUSB_NEED_POLLING_TASK
27+
// Manual call tud_task since it isn't called by Core's background
28+
TinyUSBDevice.task();
29+
#endif
30+
31+
uint8_t buf[64];
32+
uint32_t count = 0;
33+
while (SerialTinyUSB.available()) {
34+
buf[count++] = (uint8_t) toupper(SerialTinyUSB.read());
35+
}
36+
37+
if (count) {
38+
SerialTinyUSB.write(buf, count);
39+
}
40+
}

src/Adafruit_TinyUSB.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@
2525
#ifndef ADAFRUIT_TINYUSB_H_
2626
#define ADAFRUIT_TINYUSB_H_
2727

28-
// Core that has built-in support: Adafruit SAMD, nRF, rp2040, esp32
29-
#if !(defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_NRF52_ADAFRUIT) || \
30-
defined(ARDUINO_ARCH_ESP32) || \
31-
(defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED)))
32-
#define TINYUSB_NEED_POLLING_TASK
33-
#endif
34-
3528
// Error message for Core that must select TinyUSB via menu
3629
#if !defined(USE_TINYUSB) && \
3730
(defined(ARDUINO_ARCH_SAMD) || \

src/arduino/Adafruit_TinyUSB_API.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
3232
// TinyUSB_API, USBD_CDC, USBD_Device, USBD_Interface,
3333
#define TINYUSB_API_VERSION 30000
3434

35+
// Core that has built-in support: Adafruit SAMD, Adafruit nRF, rp2040, esp32
36+
#if !(defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_NRF52_ADAFRUIT) || \
37+
defined(ARDUINO_ARCH_ESP32) || \
38+
(defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED)))
39+
#define TINYUSB_NEED_POLLING_TASK
40+
#endif
41+
3542
//--------------------------------------------------------------------+
3643
// Core API
3744
// Should be called by BSP Core to initialize, process task

src/arduino/Adafruit_USBD_Device.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,16 @@ uint8_t Adafruit_USBD_Device::addStringDescriptor(const char *s) {
150150
return index;
151151
}
152152

153-
void Adafruit_USBD_Device::task(void) { tud_task(); }
153+
void Adafruit_USBD_Device::task(void) {
154+
tud_task();
155+
156+
#ifdef TINYUSB_NEED_POLLING_TASK
157+
// can also be used with port with built-in support
158+
if (SerialTinyUSB) {
159+
SerialTinyUSB.flush();
160+
}
161+
#endif
162+
}
154163

155164
bool Adafruit_USBD_Device::mounted(void) { return tud_mounted(); }
156165

0 commit comments

Comments
 (0)