Skip to content

Commit 537be8c

Browse files
authored
Merge pull request #109 from adafruit/support-rp2040-mbed
Support rp2040 mbed
2 parents 940576a + d308130 commit 537be8c

File tree

27 files changed

+225
-78
lines changed

27 files changed

+225
-78
lines changed

README.md

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,55 @@
22

33
[![Build Status](https://github.com/adafruit/Adafruit_TinyUSB_Arduino/workflows/Build/badge.svg)](https://github.com/adafruit/Adafruit_TinyUSB_Arduino/actions) [![License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://opensource.org/licenses/MIT)
44

5-
This library is a Arduino-friendly version of [TinyUSB](https://github.com/hathach/tinyusb) stack. It is designed with structure and APIs that are easily integrated to existing or new Arduino Core. Supported platform including:
5+
This library is a Arduino-friendly version of [TinyUSB](https://github.com/hathach/tinyusb) stack.
6+
It is designed with structure and APIs that are easily integrated to an Arduino Core.
67

7-
- [Adafruit_nRF52_Arduino](https://github.com/adafruit/Adafruit_nRF52_Arduino)
8-
- [Adafruit/ArduinoCore-samd](https://github.com/adafruit/ArduinoCore-samd) selectable via menu`Tools->USB Stack->TinyUSB`
9-
- [earlephilhower/arduino-pico](https://github.com/earlephilhower/arduino-pico) selectable via menu `Tools->USB Stack->Adafruit TinyUSB`
8+
## Features
109

11-
Current class drivers supported are
10+
Currently only support device mode only, supported class drivers are:
1211

1312
- Communication (CDC): which is used to implement `Serial` monitor
1413
- Human Interface Device (HID): Generic (In & Out), Keyboard, Mouse, Gamepad etc ...
1514
- Mass Storage Class (MSC): with multiple LUNs
1615
- Musical Instrument Digital Interface (MIDI)
1716
- WebUSB with vendor specific class
1817

19-
For supported ArduinoCore, to use this library, you only need to have `<Adafruit_TinyUSB.h>` in your sketch. If your ArduinoCore does not support TinyUSB library yet, it is rather simple to port.
18+
## Supported Cores
19+
20+
There are 2 type of supported cores: with and without built-in support for TinyUSB. Built-in support provide seamless integration but requires extra code added to core's source code. Unfortunately it is not always easy or possible to make those modification.
21+
22+
### Cores with built-in support
23+
24+
Following core has Tinyusb as either the primary usb stack or selectable via menu `Tools->USB Stack`. You only need to include `<Adafruit_TinyUSB.h>` in your sketch to use.
25+
26+
- [Adafruit_nRF52_Arduino](https://github.com/adafruit/Adafruit_nRF52_Arduino)
27+
- [adafruit/ArduinoCore-samd](https://github.com/adafruit/ArduinoCore-samd)
28+
- [earlephilhower/arduino-pico](https://github.com/earlephilhower/arduino-pico)
29+
30+
### Cores without built-in support
31+
32+
Following is cores without built-in support
33+
34+
- **mbed_rp2040**
35+
36+
It is still possible to use TinyUSB but with some limits such as:
37+
38+
- `TinyUSB_Device_Init()` need to be manually called in setup()
39+
- `TinyUSB_Device_Task()` and/or `TinyUSB_Device_FlushCDC()` may (or not) need to be manually called in loop()
40+
- Use `SerialTinyUSB` name instead of Serial for serial monitor
41+
- And there could be more other issues, using on these cores should be considered as experimental
2042

2143
## Class Driver API
2244

2345
More document to write ...
2446

2547
## Porting Guide
2648

27-
It is rather easy if you want to integrate TinyUSB lib to your ArduinoCore.
49+
To integrate TinyUSB library to a Arduino core, you will need to make changes to the core for built-in support and library for porting the mcu/platform.
50+
51+
### Arduino Core Changes
2852

29-
### ArduinoCore Changes
53+
If possible, making changes to core will allow it to have built-in which make it almost transparent to user sketch
3054

3155
1. Add this repo as submodule (or have local copy) at your ArduioCore/libraries/Adafruit_TinyUSB_Arduino (much like SPI).
3256
2. Since Serial as CDC is considered as part of the core, we need to have `#include "Adafruit_USBD_CDC.h"` within your `Arduino.h`. For this to work, your `platform.txt` include path need to have `"-I{runtime.platform.path}/libraries/Adafruit_TinyUSB_Arduino/src/arduino"`.

examples/Composite/hid_generic_inout_ramdisk/hid_generic_inout_ramdisk.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ void setup()
6868
usb_hid.begin();
6969

7070
Serial.begin(115200);
71-
Serial.println("Waiting for USBDevice mount");
71+
Serial.println("Waiting for TinyUSBDevice mount");
7272
// wait until device mounted
73-
while( !USBDevice.mounted() ) delay(1);
73+
while( !TinyUSBDevice.mounted() ) delay(1);
7474

7575
Serial.println("Adafruit TinyUSB HID Generic In Out example");
7676
}

examples/Composite/mouse_external_flash/mouse_external_flash.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ void loop()
114114
uint32_t const btn = (digitalRead(pin) == activeState);
115115

116116
// Remote wakeup
117-
if ( USBDevice.suspended() && btn )
117+
if ( TinyUSBDevice.suspended() && btn )
118118
{
119119
// Wake up host if we are in suspend mode
120120
// and REMOTE_WAKEUP feature is enabled by host

examples/Composite/mouse_ramdisk/mouse_ramdisk.ino

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ Adafruit_USBD_MSC usb_msc;
4545
// the setup function runs once when you press reset or power the board
4646
void setup()
4747
{
48+
#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040)
49+
// Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040
50+
TinyUSB_Device_Init(0);
51+
#endif
52+
4853
// Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively
4954
usb_msc.setID("Adafruit", "Mass Storage", "1.0");
5055

@@ -66,7 +71,7 @@ void setup()
6671
usb_hid.begin();
6772

6873
Serial.begin(115200);
69-
while( !USBDevice.mounted() ) delay(1); // wait for native usb
74+
while( !TinyUSBDevice.mounted() ) delay(1); // wait for native usb
7075

7176
Serial.println("Adafruit TinyUSB Mouse + Mass Storage (ramdisk) example");
7277
}
@@ -80,7 +85,7 @@ void loop()
8085
uint32_t const btn = (digitalRead(pin) == activeState);
8186

8287
// Remote wakeup
83-
if ( USBDevice.suspended() && btn )
88+
if ( TinyUSBDevice.suspended() && btn )
8489
{
8590
// Wake up host if we are in suspend mode
8691
// and REMOTE_WAKEUP feature is enabled by host

examples/HID/hid_composite/hid_composite.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void setup()
6666
Serial.println("Adafruit TinyUSB HID Composite example");
6767

6868
// wait until device mounted
69-
while( !USBDevice.mounted() ) delay(1);
69+
while( !TinyUSBDevice.mounted() ) delay(1);
7070
}
7171

7272
void loop()
@@ -78,11 +78,11 @@ void loop()
7878
bool btn_pressed = (digitalRead(pin) == activeState);
7979

8080
// Remote wakeup
81-
if ( USBDevice.suspended() && btn_pressed )
81+
if ( TinyUSBDevice.suspended() && btn_pressed )
8282
{
8383
// Wake up host if we are in suspend mode
8484
// and REMOTE_WAKEUP feature is enabled by host
85-
USBDevice.remoteWakeup();
85+
TinyUSBDevice.remoteWakeup();
8686
}
8787

8888
/*------------- Mouse -------------*/

examples/HID/hid_composite_joy_featherwing/hid_composite_joy_featherwing.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void setup()
7777
last_x = ss.analogRead(3);
7878

7979
// wait until device mounted
80-
while( !USBDevice.mounted() ) delay(1);
80+
while( !TinyUSBDevice.mounted() ) delay(1);
8181
}
8282

8383
void loop()
@@ -145,10 +145,10 @@ void loop()
145145

146146
/*------------- Remote Wakeup -------------*/
147147
// Remote wakeup if PC is suspended and we has user interaction with joy feather wing
148-
if ( has_action && USBDevice.suspended() )
148+
if ( has_action && TinyUSBDevice.suspended() )
149149
{
150150
// Wake up only works if REMOTE_WAKEUP feature is enable by host
151151
// Usually this is the case with Mouse/Keyboard device
152-
USBDevice.remoteWakeup();
152+
TinyUSBDevice.remoteWakeup();
153153
}
154154
}

examples/HID/hid_gamepad/hid_gamepad.ino

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ hid_gamepad_report_t gp; // defined in hid.h from Adafruit_TinyUSB_Arduin
3636

3737
void setup()
3838
{
39+
#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040)
40+
// Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040
41+
TinyUSB_Device_Init(0);
42+
#endif
43+
3944
Serial.begin(115200);
4045

4146
usb_hid.setPollInterval(2);
@@ -44,19 +49,19 @@ void setup()
4449
usb_hid.begin();
4550

4651
// wait until device mounted
47-
while( !USBDevice.mounted() ) delay(1);
52+
while( !TinyUSBDevice.mounted() ) delay(1);
4853

4954
Serial.println("Adafruit TinyUSB HID Gamepad example");
5055
}
5156

5257
void loop()
5358
{
5459
// // Remote wakeup
55-
// if ( USBDevice.suspended() && btn )
60+
// if ( TinyUSBDevice.suspended() && btn )
5661
// {
5762
// // Wake up host if we are in suspend mode
5863
// // and REMOTE_WAKEUP feature is enabled by host
59-
// USBDevice.remoteWakeup();
64+
// TinyUSBDevice.remoteWakeup();
6065
// }
6166

6267
if ( !usb_hid.ready() ) return;

examples/HID/hid_generic_inout/hid_generic_inout.ino

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ Adafruit_USBD_HID usb_hid;
4141
// the setup function runs once when you press reset or power the board
4242
void setup()
4343
{
44+
#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040)
45+
// Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040
46+
TinyUSB_Device_Init(0);
47+
#endif
48+
4449
usb_hid.enableOutEndpoint(true);
4550
usb_hid.setPollInterval(2);
4651
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
@@ -52,7 +57,7 @@ void setup()
5257
Serial.begin(115200);
5358

5459
// wait until device mounted
55-
while( !USBDevice.mounted() ) delay(1);
60+
while( !TinyUSBDevice.mounted() ) delay(1);
5661

5762
Serial.println("Adafruit TinyUSB HID Generic In Out example");
5863
}

examples/HID/hid_keyboard/hid_keyboard.ino

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ uint8_t pincount = sizeof(pins)/sizeof(pins[0]);
4040
// the setup function runs once when you press reset or power the board
4141
void setup()
4242
{
43+
#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040)
44+
// Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040
45+
TinyUSB_Device_Init(0);
46+
#endif
47+
4348
usb_hid.setPollInterval(2);
4449
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
4550
usb_hid.setReportCallback(NULL, hid_report_callback);
@@ -58,7 +63,7 @@ void setup()
5863
}
5964

6065
// wait until device mounted
61-
while( !USBDevice.mounted() ) delay(1);
66+
while( !TinyUSBDevice.mounted() ) delay(1);
6267
}
6368

6469

@@ -68,11 +73,11 @@ void loop()
6873
delay(2);
6974

7075
// // Remote wakeup
71-
// if ( USBDevice.suspended() && btn )
76+
// if ( TinyUSBDevice.suspended() && btn )
7277
// {
7378
// // Wake up host if we are in suspend mode
7479
// // and REMOTE_WAKEUP feature is enabled by host
75-
// USBDevice.remoteWakeup();
80+
// TinyUSBDevice.remoteWakeup();
7681
// }
7782

7883
if ( !usb_hid.ready() ) return;

examples/HID/hid_mouse/hid_mouse.ino

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ Adafruit_USBD_HID usb_hid;
4343
// the setup function runs once when you press reset or power the board
4444
void setup()
4545
{
46+
#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040)
47+
// Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040
48+
TinyUSB_Device_Init(0);
49+
#endif
50+
4651
// Set up button, pullup opposite to active state
4752
pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);
4853

@@ -55,7 +60,7 @@ void setup()
5560
Serial.begin(115200);
5661

5762
// wait until device mounted
58-
while( !USBDevice.mounted() ) delay(1);
63+
while( !TinyUSBDevice.mounted() ) delay(1);
5964

6065
Serial.println("Adafruit TinyUSB HID Mouse example");
6166
}
@@ -72,11 +77,11 @@ void loop()
7277
if (!btn_pressed) return;
7378

7479
// Remote wakeup
75-
if ( USBDevice.suspended() )
80+
if ( TinyUSBDevice.suspended() )
7681
{
7782
// Wake up host if we are in suspend mode
7883
// and REMOTE_WAKEUP feature is enabled by host
79-
USBDevice.remoteWakeup();
84+
TinyUSBDevice.remoteWakeup();
8085
}
8186

8287
if ( usb_hid.ready() )

examples/MIDI/midi_pizza_box_dj/midi_pizza_box_dj.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ void setup() {
9494
for(uint8_t i=0; i<FRAMES; i++) lvl[i] = 256;
9595

9696
// Wait until device is enumerated properly before sending MIDI message
97-
while( !USBDevice.mounted() ) delay(1);
97+
while( !TinyUSBDevice.mounted() ) delay(1);
9898
}
9999

100100
///////////////////////////////////////////////////////////////////////////////

examples/MIDI/midi_test/midi_test.ino

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ byte note_sequence[] = {
3838

3939
void setup()
4040
{
41+
#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040)
42+
// Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040
43+
TinyUSB_Device_Init(0);
44+
#endif
45+
4146
pinMode(LED_BUILTIN, OUTPUT);
4247

4348
//usb_midi.setStringDescriptor("TinyUSB MIDI");
@@ -56,7 +61,7 @@ void setup()
5661
Serial.begin(115200);
5762

5863
// wait until device mounted
59-
while( !USBDevice.mounted() ) delay(1);
64+
while( !TinyUSBDevice.mounted() ) delay(1);
6065
}
6166

6267
void loop()
@@ -98,13 +103,25 @@ void loop()
98103
void handleNoteOn(byte channel, byte pitch, byte velocity)
99104
{
100105
// Log when a note is pressed.
101-
Serial.printf("Note on: channel = %d, pitch = %d, velocity - %d", channel, pitch, velocity);
102-
Serial.println();
106+
Serial.print("Note on: channel = ");
107+
Serial.print(channel);
108+
109+
Serial.print(" pitch = ");
110+
Serial.print(pitch);
111+
112+
Serial.print(" velocity = ");
113+
Serial.println(velocity);
103114
}
104115

105116
void handleNoteOff(byte channel, byte pitch, byte velocity)
106117
{
107118
// Log when a note is released.
108-
Serial.printf("Note off: channel = %d, pitch = %d, velocity - %d", channel, pitch, velocity);
109-
Serial.println();
119+
Serial.print("Note off: channel = ");
120+
Serial.print(channel);
121+
122+
Serial.print(" pitch = ");
123+
Serial.print(pitch);
124+
125+
Serial.print(" velocity = ");
126+
Serial.println(velocity);
110127
}

examples/MassStorage/msc_ramdisk/msc_ramdisk.ino

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ Adafruit_USBD_MSC usb_msc;
2121
// the setup function runs once when you press reset or power the board
2222
void setup()
2323
{
24+
#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040)
25+
// Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040
26+
TinyUSB_Device_Init(0);
27+
#endif
28+
2429
// Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively
2530
usb_msc.setID("Adafruit", "Mass Storage", "1.0");
26-
31+
2732
// Set disk size
2833
usb_msc.setCapacity(DISK_BLOCK_NUM, DISK_BLOCK_SIZE);
2934

@@ -32,7 +37,7 @@ void setup()
3237

3338
// Set Lun ready (RAM disk is always ready)
3439
usb_msc.setUnitReady(true);
35-
40+
3641
usb_msc.begin();
3742

3843
Serial.begin(115200);
@@ -47,8 +52,8 @@ void loop()
4752
}
4853

4954
// Callback invoked when received READ10 command.
50-
// Copy disk's data to buffer (up to bufsize) and
51-
// return number of copied bytes (must be multiple of block size)
55+
// Copy disk's data to buffer (up to bufsize) and
56+
// return number of copied bytes (must be multiple of block size)
5257
int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
5358
{
5459
uint8_t const* addr = msc_disk[lba];
@@ -58,7 +63,7 @@ int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
5863
}
5964

6065
// Callback invoked when received WRITE10 command.
61-
// Process data in buffer to disk's storage and
66+
// Process data in buffer to disk's storage and
6267
// return number of written bytes (must be multiple of block size)
6368
int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
6469
{

0 commit comments

Comments
 (0)