Skip to content

Commit f8a2f38

Browse files
authored
Port Adafruit_TinyUSB_Arduino lib (earlephilhower#127)
* initial tinyusb lib port * add Adafruit_TinyUSB_Arduino as submodules * add yield() to main loop * sync with tinyusb lib latest * add USB manufacturer and product * fix typo in tinyusb lib * sync with master updating implementation * Rationalize link stage command line Make the build process less insane. * clean up delay * clean up platform and board * update makeboards.py for generating usbstack menu * update tinyusb lib to 1.0.0
1 parent 39d1a28 commit f8a2f38

12 files changed

+246
-23
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@
2222
[submodule "libraries/Mouse"]
2323
path = libraries/Mouse
2424
url = https://github.com/earlephilhower/Mouse
25+
[submodule "libraries/Adafruit_TinyUSB_Arduino"]
26+
path = libraries/Adafruit_TinyUSB_Arduino
27+
url = https://github.com/adafruit/Adafruit_TinyUSB_Arduino.git

boards.txt

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ menu.freq=CPU Speed
44
menu.dbgport=Debug Port
55
menu.dbglvl=Debug Level
66
menu.boot2=Boot Stage 2
7+
menu.usbstack=USB Stack
8+
9+
# -----------------------------------
10+
# Raspberry Pi Pico
11+
# -----------------------------------
712
rpipico.name=Raspberry Pi Pico
813
rpipico.vid.0=0x2e8a
914
rpipico.pid.0=0x000a
@@ -24,6 +29,10 @@ rpipico.build.core=rp2040
2429
rpipico.build.mcu=rp2040
2530
rpipico.build.ldscript=memmap_default.ld
2631
rpipico.build.boot2=boot2_w25q080_2_padded_checksum
32+
rpipico.build.vid=0x2e8a
33+
rpipico.build.pid=0x000a
34+
rpipico.build.usb_manufacturer="Raspberry Pi"
35+
rpipico.build.usb_product="Pico"
2736
rpipico.menu.flash.2097152_0=2MB (no FS)
2837
rpipico.menu.flash.2097152_0.upload.maximum_size=2093056
2938
rpipico.menu.flash.2097152_0.build.flash_length=2093056
@@ -102,6 +111,14 @@ rpipico.menu.dbglvl.All=All
102111
rpipico.menu.dbglvl.All.build.debug_level=-DDEBUG_RP2040_WIRE -DDEBUG_RP2040_SPI -DDEBUG_RP2040_CORE
103112
rpipico.menu.dbglvl.NDEBUG=NDEBUG
104113
rpipico.menu.dbglvl.NDEBUG.build.debug_level=-DNDEBUG
114+
rpipico.menu.usbstack.picosdk=Pico SDK
115+
rpipico.menu.usbstack.picosdk.build.usbstack_flags=
116+
rpipico.menu.usbstack.tinyusb=Adafruit TinyUSB
117+
rpipico.menu.usbstack.tinyusb.build.usbstack_flags=-DUSE_TINYUSB "-I{build.core.path}/TinyUSB" "-I{runtime.platform.path}/libraries/Adafruit_TinyUSB_Arduino/src/arduino"
118+
119+
# -----------------------------------
120+
# Raspberry Pi Pico (Picoprobe)
121+
# -----------------------------------
105122
rpipicopicoprobe.name=Raspberry Pi Pico (Picoprobe)
106123
rpipicopicoprobe.vid.0=0x2e8a
107124
rpipicopicoprobe.pid.0=0x0004
@@ -122,6 +139,10 @@ rpipicopicoprobe.build.core=rp2040
122139
rpipicopicoprobe.build.mcu=rp2040
123140
rpipicopicoprobe.build.ldscript=memmap_default.ld
124141
rpipicopicoprobe.build.boot2=boot2_w25q080_2_padded_checksum
142+
rpipicopicoprobe.build.vid=0x2e8a
143+
rpipicopicoprobe.build.pid=0x000a
144+
rpipicopicoprobe.build.usb_manufacturer="Raspberry Pi"
145+
rpipicopicoprobe.build.usb_product="Pico (Picoprobe)"
125146
rpipicopicoprobe.menu.flash.2097152_0=2MB (no FS)
126147
rpipicopicoprobe.menu.flash.2097152_0.upload.maximum_size=2093056
127148
rpipicopicoprobe.menu.flash.2097152_0.build.flash_length=2093056
@@ -200,10 +221,18 @@ rpipicopicoprobe.menu.dbglvl.All=All
200221
rpipicopicoprobe.menu.dbglvl.All.build.debug_level=-DDEBUG_RP2040_WIRE -DDEBUG_RP2040_SPI -DDEBUG_RP2040_CORE
201222
rpipicopicoprobe.menu.dbglvl.NDEBUG=NDEBUG
202223
rpipicopicoprobe.menu.dbglvl.NDEBUG.build.debug_level=-DNDEBUG
224+
rpipicopicoprobe.menu.usbstack.picosdk=Pico SDK
225+
rpipicopicoprobe.menu.usbstack.picosdk.build.usbstack_flags=
226+
rpipicopicoprobe.menu.usbstack.tinyusb=Adafruit TinyUSB
227+
rpipicopicoprobe.menu.usbstack.tinyusb.build.usbstack_flags=-DUSE_TINYUSB "-I{build.core.path}/TinyUSB" "-I{runtime.platform.path}/libraries/Adafruit_TinyUSB_Arduino/src/arduino"
228+
229+
# -----------------------------------
230+
# Adafruit Feather RP2040
231+
# -----------------------------------
203232
adafruitfeather.name=Adafruit Feather RP2040
204-
adafruitfeather.vid.0=0x2e8a
205-
adafruitfeather.pid.0=0x000b
206-
adafruitfeather.build.usbpid=-DSERIALUSB_PID=0x000b
233+
adafruitfeather.vid.0=0x239a
234+
adafruitfeather.pid.0=0x80f1
235+
adafruitfeather.build.usbpid=-DSERIALUSB_PID=0x80f1
207236
adafruitfeather.build.board=ADAFRUIT_FEATHER_RP2040
208237
adafruitfeather.build.mcu=cortex-m0plus
209238
adafruitfeather.build.variant=adafruitfeather
@@ -220,6 +249,10 @@ adafruitfeather.build.core=rp2040
220249
adafruitfeather.build.mcu=rp2040
221250
adafruitfeather.build.ldscript=memmap_default.ld
222251
adafruitfeather.build.boot2=boot2_w25x10cl_4_padded_checksum
252+
adafruitfeather.build.vid=0x239a
253+
adafruitfeather.build.pid=0x80f1
254+
adafruitfeather.build.usb_manufacturer="Adafruit"
255+
adafruitfeather.build.usb_product="Feather RP2040"
223256
adafruitfeather.menu.flash.8388608_0=8MB (no FS)
224257
adafruitfeather.menu.flash.8388608_0.upload.maximum_size=8384512
225258
adafruitfeather.menu.flash.8388608_0.build.flash_length=8384512
@@ -334,10 +367,18 @@ adafruitfeather.menu.dbglvl.All=All
334367
adafruitfeather.menu.dbglvl.All.build.debug_level=-DDEBUG_RP2040_WIRE -DDEBUG_RP2040_SPI -DDEBUG_RP2040_CORE
335368
adafruitfeather.menu.dbglvl.NDEBUG=NDEBUG
336369
adafruitfeather.menu.dbglvl.NDEBUG.build.debug_level=-DNDEBUG
370+
adafruitfeather.menu.usbstack.picosdk=Pico SDK
371+
adafruitfeather.menu.usbstack.picosdk.build.usbstack_flags=
372+
adafruitfeather.menu.usbstack.tinyusb=Adafruit TinyUSB
373+
adafruitfeather.menu.usbstack.tinyusb.build.usbstack_flags=-DUSE_TINYUSB "-I{build.core.path}/TinyUSB" "-I{runtime.platform.path}/libraries/Adafruit_TinyUSB_Arduino/src/arduino"
374+
375+
# -----------------------------------
376+
# Adafruit Feather RP2040 (Picoprobe)
377+
# -----------------------------------
337378
adafruitfeatherpicoprobe.name=Adafruit Feather RP2040 (Picoprobe)
338-
adafruitfeatherpicoprobe.vid.0=0x2e8a
379+
adafruitfeatherpicoprobe.vid.0=0x239a
339380
adafruitfeatherpicoprobe.pid.0=0x0004
340-
adafruitfeatherpicoprobe.build.usbpid=-DSERIALUSB_PID=0x000b
381+
adafruitfeatherpicoprobe.build.usbpid=-DSERIALUSB_PID=0x80f1
341382
adafruitfeatherpicoprobe.build.board=ADAFRUIT_FEATHER_RP2040
342383
adafruitfeatherpicoprobe.build.mcu=cortex-m0plus
343384
adafruitfeatherpicoprobe.build.variant=adafruitfeather
@@ -354,6 +395,10 @@ adafruitfeatherpicoprobe.build.core=rp2040
354395
adafruitfeatherpicoprobe.build.mcu=rp2040
355396
adafruitfeatherpicoprobe.build.ldscript=memmap_default.ld
356397
adafruitfeatherpicoprobe.build.boot2=boot2_w25x10cl_4_padded_checksum
398+
adafruitfeatherpicoprobe.build.vid=0x239a
399+
adafruitfeatherpicoprobe.build.pid=0x80f1
400+
adafruitfeatherpicoprobe.build.usb_manufacturer="Adafruit"
401+
adafruitfeatherpicoprobe.build.usb_product="Feather RP2040 (Picoprobe)"
357402
adafruitfeatherpicoprobe.menu.flash.8388608_0=8MB (no FS)
358403
adafruitfeatherpicoprobe.menu.flash.8388608_0.upload.maximum_size=8384512
359404
adafruitfeatherpicoprobe.menu.flash.8388608_0.build.flash_length=8384512
@@ -468,6 +513,14 @@ adafruitfeatherpicoprobe.menu.dbglvl.All=All
468513
adafruitfeatherpicoprobe.menu.dbglvl.All.build.debug_level=-DDEBUG_RP2040_WIRE -DDEBUG_RP2040_SPI -DDEBUG_RP2040_CORE
469514
adafruitfeatherpicoprobe.menu.dbglvl.NDEBUG=NDEBUG
470515
adafruitfeatherpicoprobe.menu.dbglvl.NDEBUG.build.debug_level=-DNDEBUG
516+
adafruitfeatherpicoprobe.menu.usbstack.picosdk=Pico SDK
517+
adafruitfeatherpicoprobe.menu.usbstack.picosdk.build.usbstack_flags=
518+
adafruitfeatherpicoprobe.menu.usbstack.tinyusb=Adafruit TinyUSB
519+
adafruitfeatherpicoprobe.menu.usbstack.tinyusb.build.usbstack_flags=-DUSE_TINYUSB "-I{build.core.path}/TinyUSB" "-I{runtime.platform.path}/libraries/Adafruit_TinyUSB_Arduino/src/arduino"
520+
521+
# -----------------------------------
522+
# Generic RP2040
523+
# -----------------------------------
471524
generic.name=Generic RP2040
472525
generic.vid.0=0x2e8a
473526
generic.pid.0=0xf00a
@@ -488,6 +541,10 @@ generic.build.core=rp2040
488541
generic.build.mcu=rp2040
489542
generic.build.ldscript=memmap_default.ld
490543
generic.build.boot2=boot2_generic_03h_4_padded_checksum
544+
generic.build.vid=0x2e8a
545+
generic.build.pid=0xf00a
546+
generic.build.usb_manufacturer="Generic"
547+
generic.build.usb_product="RP2040"
491548
generic.menu.flash.2097152_0=2MB (no FS)
492549
generic.menu.flash.2097152_0.upload.maximum_size=2093056
493550
generic.menu.flash.2097152_0.build.flash_length=2093056
@@ -578,6 +635,10 @@ generic.menu.dbglvl.All=All
578635
generic.menu.dbglvl.All.build.debug_level=-DDEBUG_RP2040_WIRE -DDEBUG_RP2040_SPI -DDEBUG_RP2040_CORE
579636
generic.menu.dbglvl.NDEBUG=NDEBUG
580637
generic.menu.dbglvl.NDEBUG.build.debug_level=-DNDEBUG
638+
generic.menu.usbstack.picosdk=Pico SDK
639+
generic.menu.usbstack.picosdk.build.usbstack_flags=
640+
generic.menu.usbstack.tinyusb=Adafruit TinyUSB
641+
generic.menu.usbstack.tinyusb.build.usbstack_flags=-DUSE_TINYUSB "-I{build.core.path}/TinyUSB" "-I{runtime.platform.path}/libraries/Adafruit_TinyUSB_Arduino/src/arduino"
581642
generic.menu.boot2.boot2_generic_03h_2_padded_checksum=Generic SPI /2
582643
generic.menu.boot2.boot2_generic_03h_2_padded_checksum.build.boot2=boot2_generic_03h_2_padded_checksum
583644
generic.menu.boot2.boot2_generic_03h_4_padded_checksum=Generic SPI /4
@@ -594,6 +655,10 @@ generic.menu.boot2.boot2_w25x10cl_2_padded_checksum=W25X10CL QSPI /2
594655
generic.menu.boot2.boot2_w25x10cl_2_padded_checksum.build.boot2=boot2_w25x10cl_2_padded_checksum
595656
generic.menu.boot2.boot2_w25x10cl_4_padded_checksum=W25X10CL QSPI /4
596657
generic.menu.boot2.boot2_w25x10cl_4_padded_checksum.build.boot2=boot2_w25x10cl_4_padded_checksum
658+
659+
# -----------------------------------
660+
# Generic RP2040 (Picoprobe)
661+
# -----------------------------------
597662
genericpicoprobe.name=Generic RP2040 (Picoprobe)
598663
genericpicoprobe.vid.0=0x2e8a
599664
genericpicoprobe.pid.0=0x0004
@@ -614,6 +679,10 @@ genericpicoprobe.build.core=rp2040
614679
genericpicoprobe.build.mcu=rp2040
615680
genericpicoprobe.build.ldscript=memmap_default.ld
616681
genericpicoprobe.build.boot2=boot2_generic_03h_4_padded_checksum
682+
genericpicoprobe.build.vid=0x2e8a
683+
genericpicoprobe.build.pid=0xf00a
684+
genericpicoprobe.build.usb_manufacturer="Generic"
685+
genericpicoprobe.build.usb_product="RP2040 (Picoprobe)"
617686
genericpicoprobe.menu.flash.2097152_0=2MB (no FS)
618687
genericpicoprobe.menu.flash.2097152_0.upload.maximum_size=2093056
619688
genericpicoprobe.menu.flash.2097152_0.build.flash_length=2093056
@@ -704,6 +773,10 @@ genericpicoprobe.menu.dbglvl.All=All
704773
genericpicoprobe.menu.dbglvl.All.build.debug_level=-DDEBUG_RP2040_WIRE -DDEBUG_RP2040_SPI -DDEBUG_RP2040_CORE
705774
genericpicoprobe.menu.dbglvl.NDEBUG=NDEBUG
706775
genericpicoprobe.menu.dbglvl.NDEBUG.build.debug_level=-DNDEBUG
776+
genericpicoprobe.menu.usbstack.picosdk=Pico SDK
777+
genericpicoprobe.menu.usbstack.picosdk.build.usbstack_flags=
778+
genericpicoprobe.menu.usbstack.tinyusb=Adafruit TinyUSB
779+
genericpicoprobe.menu.usbstack.tinyusb.build.usbstack_flags=-DUSE_TINYUSB "-I{build.core.path}/TinyUSB" "-I{runtime.platform.path}/libraries/Adafruit_TinyUSB_Arduino/src/arduino"
707780
genericpicoprobe.menu.boot2.boot2_generic_03h_2_padded_checksum=Generic SPI /2
708781
genericpicoprobe.menu.boot2.boot2_generic_03h_2_padded_checksum.build.boot2=boot2_generic_03h_2_padded_checksum
709782
genericpicoprobe.menu.boot2.boot2_generic_03h_4_padded_checksum=Generic SPI /4

cores/rp2040/Arduino.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,14 @@ unsigned long millis();
9595
#define HAVE_HWSERIAL2
9696

9797
#ifdef __cplusplus
98-
#include "SerialUSB.h"
98+
99+
#ifdef USE_TINYUSB
100+
// Needed for declaring Serial
101+
#include "Adafruit_USBD_CDC.h"
102+
#else
103+
#include "SerialUSB.h"
104+
#endif
105+
99106
#include "SerialUART.h"
100107
#include "RP2040.h"
101108
#include "Bootsel.h"

cores/rp2040/RP2040USB.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2020
*/
2121

22+
#ifndef USE_TINYUSB
23+
2224
#include <Arduino.h>
2325
#include "CoreMutex.h"
2426
#include "RP2040USB.h"
@@ -317,3 +319,5 @@ void tud_hid_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uin
317319
(void) bufsize;
318320
}
319321

322+
#endif
323+

cores/rp2040/SerialUSB.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2121
*/
2222

23+
#ifndef USE_TINYUSB
24+
2325
#include <Arduino.h>
2426
#include "CoreMutex.h"
2527

@@ -189,3 +191,5 @@ void arduino::serialEventRun(void)
189191
serialEvent();
190192
}
191193
}
194+
195+
#endif

cores/rp2040/Stream.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#include "api/String.h"
1+
#include "api/Stream.h"

cores/rp2040/TinyUSB/tusb_config.h

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2018, hathach for Adafruit
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
#ifndef _TUSB_CONFIG_H_
26+
#define _TUSB_CONFIG_H_
27+
28+
#ifdef __cplusplus
29+
extern "C" {
30+
#endif
31+
32+
//--------------------------------------------------------------------
33+
// COMMON CONFIGURATION
34+
//--------------------------------------------------------------------
35+
#ifdef USE_TINYUSB
36+
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
37+
#else
38+
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_NONE
39+
#endif
40+
41+
//#ifndef CFG_TUSB_MCU
42+
// #define CFG_TUSB_MCU OPT_MCU_RP2040
43+
//#endif
44+
#define CFG_TUSB_OS OPT_OS_PICO
45+
46+
#define CFG_TUSB_DEBUG 0
47+
//#if CFG_TUSB_DEBUG
48+
// #define tu_printf serial1_printf
49+
// extern int serial1_printf(const char *__restrict __format, ...);
50+
//#endif
51+
52+
#define CFG_TUSB_MEM_SECTION
53+
#define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4)
54+
55+
//--------------------------------------------------------------------
56+
// DEVICE CONFIGURATION
57+
//--------------------------------------------------------------------
58+
59+
#define CFG_TUD_ENDOINT0_SIZE 64
60+
61+
//------------- CLASS -------------//
62+
#define CFG_TUD_CDC 1
63+
#define CFG_TUD_MSC 1
64+
#define CFG_TUD_HID 1
65+
#define CFG_TUD_MIDI 1
66+
#define CFG_TUD_VENDOR 1
67+
68+
// CDC FIFO size of TX and RX
69+
#define CFG_TUD_CDC_RX_BUFSIZE 256
70+
#define CFG_TUD_CDC_TX_BUFSIZE 256
71+
72+
// MSC Buffer size of Device Mass storage
73+
#define CFG_TUD_MSC_BUFSIZE 512
74+
75+
// HID buffer size Should be sufficient to hold ID (if any) + Data
76+
#define CFG_TUD_HID_BUFSIZE 64
77+
78+
// MIDI FIFO size of TX and RX
79+
#define CFG_TUD_MIDI_RX_BUFSIZE 128
80+
#define CFG_TUD_MIDI_TX_BUFSIZE 128
81+
82+
// Vendor FIFO size of TX and RX
83+
#define CFG_TUD_VENDOR_RX_BUFSIZE 64
84+
#define CFG_TUD_VENDOR_TX_BUFSIZE 64
85+
86+
#ifdef __cplusplus
87+
}
88+
#endif
89+
90+
#endif /* _TUSB_CONFIG_H_ */

cores/rp2040/delay.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,42 @@
2121
#include <pico.h>
2222
#include <pico/time.h>
2323

24-
extern "C" void delay( unsigned long ms ) {
24+
#ifdef USE_TINYUSB
25+
#include "Adafruit_TinyUSB_API.h"
26+
#endif
27+
28+
extern "C"
29+
{
30+
31+
void delay( unsigned long ms ) {
2532
if (!ms) {
2633
return;
2734
}
2835

2936
sleep_ms(ms);
3037
}
3138

32-
extern "C" void delayMicroseconds( unsigned int usec ) {
39+
void delayMicroseconds( unsigned int usec ) {
3340
if (!usec) {
3441
return;
3542
}
3643
sleep_us(usec);
3744
}
3845

39-
extern "C" void yield() {
40-
// NOOP
46+
void yield() {
47+
#ifdef USE_TINYUSB
48+
TinyUSB_Device_Task();
49+
TinyUSB_Device_FlushCDC();
50+
#endif
4151
}
4252

43-
extern "C" uint32_t millis() {
53+
54+
uint32_t millis() {
4455
return to_ms_since_boot(get_absolute_time());
4556
}
4657

47-
extern "C" uint32_t micros() {
58+
uint32_t micros() {
4859
return to_us_since_boot(get_absolute_time());
4960
}
5061

62+
} // extern C

0 commit comments

Comments
 (0)