Skip to content

Port Adafruit_TinyUSB_Arduino lib #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
May 19, 2021
Merged
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@
[submodule "libraries/Mouse"]
path = libraries/Mouse
url = https://github.com/earlephilhower/Mouse
[submodule "libraries/Adafruit_TinyUSB_Arduino"]
path = libraries/Adafruit_TinyUSB_Arduino
url = https://github.com/adafruit/Adafruit_TinyUSB_Arduino.git
83 changes: 78 additions & 5 deletions boards.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ menu.freq=CPU Speed
menu.dbgport=Debug Port
menu.dbglvl=Debug Level
menu.boot2=Boot Stage 2
menu.usbstack=USB Stack

# -----------------------------------
# Raspberry Pi Pico
# -----------------------------------
rpipico.name=Raspberry Pi Pico
rpipico.vid.0=0x2e8a
rpipico.pid.0=0x000a
Expand All @@ -24,6 +29,10 @@ rpipico.build.core=rp2040
rpipico.build.mcu=rp2040
rpipico.build.ldscript=memmap_default.ld
rpipico.build.boot2=boot2_w25q080_2_padded_checksum
rpipico.build.vid=0x2e8a
rpipico.build.pid=0x000a
rpipico.build.usb_manufacturer="Raspberry Pi"
rpipico.build.usb_product="Pico"
rpipico.menu.flash.2097152_0=2MB (no FS)
rpipico.menu.flash.2097152_0.upload.maximum_size=2093056
rpipico.menu.flash.2097152_0.build.flash_length=2093056
Expand Down Expand Up @@ -102,6 +111,14 @@ rpipico.menu.dbglvl.All=All
rpipico.menu.dbglvl.All.build.debug_level=-DDEBUG_RP2040_WIRE -DDEBUG_RP2040_SPI -DDEBUG_RP2040_CORE
rpipico.menu.dbglvl.NDEBUG=NDEBUG
rpipico.menu.dbglvl.NDEBUG.build.debug_level=-DNDEBUG
rpipico.menu.usbstack.picosdk=Pico SDK
rpipico.menu.usbstack.picosdk.build.usbstack_flags=
rpipico.menu.usbstack.tinyusb=Adafruit TinyUSB
rpipico.menu.usbstack.tinyusb.build.usbstack_flags=-DUSE_TINYUSB "-I{build.core.path}/TinyUSB" "-I{runtime.platform.path}/libraries/Adafruit_TinyUSB_Arduino/src/arduino"

# -----------------------------------
# Raspberry Pi Pico (Picoprobe)
# -----------------------------------
rpipicopicoprobe.name=Raspberry Pi Pico (Picoprobe)
rpipicopicoprobe.vid.0=0x2e8a
rpipicopicoprobe.pid.0=0x0004
Expand All @@ -122,6 +139,10 @@ rpipicopicoprobe.build.core=rp2040
rpipicopicoprobe.build.mcu=rp2040
rpipicopicoprobe.build.ldscript=memmap_default.ld
rpipicopicoprobe.build.boot2=boot2_w25q080_2_padded_checksum
rpipicopicoprobe.build.vid=0x2e8a
rpipicopicoprobe.build.pid=0x000a
rpipicopicoprobe.build.usb_manufacturer="Raspberry Pi"
rpipicopicoprobe.build.usb_product="Pico (Picoprobe)"
rpipicopicoprobe.menu.flash.2097152_0=2MB (no FS)
rpipicopicoprobe.menu.flash.2097152_0.upload.maximum_size=2093056
rpipicopicoprobe.menu.flash.2097152_0.build.flash_length=2093056
Expand Down Expand Up @@ -200,10 +221,18 @@ rpipicopicoprobe.menu.dbglvl.All=All
rpipicopicoprobe.menu.dbglvl.All.build.debug_level=-DDEBUG_RP2040_WIRE -DDEBUG_RP2040_SPI -DDEBUG_RP2040_CORE
rpipicopicoprobe.menu.dbglvl.NDEBUG=NDEBUG
rpipicopicoprobe.menu.dbglvl.NDEBUG.build.debug_level=-DNDEBUG
rpipicopicoprobe.menu.usbstack.picosdk=Pico SDK
rpipicopicoprobe.menu.usbstack.picosdk.build.usbstack_flags=
rpipicopicoprobe.menu.usbstack.tinyusb=Adafruit TinyUSB
rpipicopicoprobe.menu.usbstack.tinyusb.build.usbstack_flags=-DUSE_TINYUSB "-I{build.core.path}/TinyUSB" "-I{runtime.platform.path}/libraries/Adafruit_TinyUSB_Arduino/src/arduino"

# -----------------------------------
# Adafruit Feather RP2040
# -----------------------------------
adafruitfeather.name=Adafruit Feather RP2040
adafruitfeather.vid.0=0x2e8a
adafruitfeather.pid.0=0x000b
adafruitfeather.build.usbpid=-DSERIALUSB_PID=0x000b
adafruitfeather.vid.0=0x239a
adafruitfeather.pid.0=0x80f1
adafruitfeather.build.usbpid=-DSERIALUSB_PID=0x80f1
adafruitfeather.build.board=ADAFRUIT_FEATHER_RP2040
adafruitfeather.build.mcu=cortex-m0plus
adafruitfeather.build.variant=adafruitfeather
Expand All @@ -220,6 +249,10 @@ adafruitfeather.build.core=rp2040
adafruitfeather.build.mcu=rp2040
adafruitfeather.build.ldscript=memmap_default.ld
adafruitfeather.build.boot2=boot2_w25x10cl_4_padded_checksum
adafruitfeather.build.vid=0x239a
adafruitfeather.build.pid=0x80f1
adafruitfeather.build.usb_manufacturer="Adafruit"
adafruitfeather.build.usb_product="Feather RP2040"
adafruitfeather.menu.flash.8388608_0=8MB (no FS)
adafruitfeather.menu.flash.8388608_0.upload.maximum_size=8384512
adafruitfeather.menu.flash.8388608_0.build.flash_length=8384512
Expand Down Expand Up @@ -334,10 +367,18 @@ adafruitfeather.menu.dbglvl.All=All
adafruitfeather.menu.dbglvl.All.build.debug_level=-DDEBUG_RP2040_WIRE -DDEBUG_RP2040_SPI -DDEBUG_RP2040_CORE
adafruitfeather.menu.dbglvl.NDEBUG=NDEBUG
adafruitfeather.menu.dbglvl.NDEBUG.build.debug_level=-DNDEBUG
adafruitfeather.menu.usbstack.picosdk=Pico SDK
adafruitfeather.menu.usbstack.picosdk.build.usbstack_flags=
adafruitfeather.menu.usbstack.tinyusb=Adafruit TinyUSB
adafruitfeather.menu.usbstack.tinyusb.build.usbstack_flags=-DUSE_TINYUSB "-I{build.core.path}/TinyUSB" "-I{runtime.platform.path}/libraries/Adafruit_TinyUSB_Arduino/src/arduino"

# -----------------------------------
# Adafruit Feather RP2040 (Picoprobe)
# -----------------------------------
adafruitfeatherpicoprobe.name=Adafruit Feather RP2040 (Picoprobe)
adafruitfeatherpicoprobe.vid.0=0x2e8a
adafruitfeatherpicoprobe.vid.0=0x239a
adafruitfeatherpicoprobe.pid.0=0x0004
adafruitfeatherpicoprobe.build.usbpid=-DSERIALUSB_PID=0x000b
adafruitfeatherpicoprobe.build.usbpid=-DSERIALUSB_PID=0x80f1
adafruitfeatherpicoprobe.build.board=ADAFRUIT_FEATHER_RP2040
adafruitfeatherpicoprobe.build.mcu=cortex-m0plus
adafruitfeatherpicoprobe.build.variant=adafruitfeather
Expand All @@ -354,6 +395,10 @@ adafruitfeatherpicoprobe.build.core=rp2040
adafruitfeatherpicoprobe.build.mcu=rp2040
adafruitfeatherpicoprobe.build.ldscript=memmap_default.ld
adafruitfeatherpicoprobe.build.boot2=boot2_w25x10cl_4_padded_checksum
adafruitfeatherpicoprobe.build.vid=0x239a
adafruitfeatherpicoprobe.build.pid=0x80f1
adafruitfeatherpicoprobe.build.usb_manufacturer="Adafruit"
adafruitfeatherpicoprobe.build.usb_product="Feather RP2040 (Picoprobe)"
adafruitfeatherpicoprobe.menu.flash.8388608_0=8MB (no FS)
adafruitfeatherpicoprobe.menu.flash.8388608_0.upload.maximum_size=8384512
adafruitfeatherpicoprobe.menu.flash.8388608_0.build.flash_length=8384512
Expand Down Expand Up @@ -468,6 +513,14 @@ adafruitfeatherpicoprobe.menu.dbglvl.All=All
adafruitfeatherpicoprobe.menu.dbglvl.All.build.debug_level=-DDEBUG_RP2040_WIRE -DDEBUG_RP2040_SPI -DDEBUG_RP2040_CORE
adafruitfeatherpicoprobe.menu.dbglvl.NDEBUG=NDEBUG
adafruitfeatherpicoprobe.menu.dbglvl.NDEBUG.build.debug_level=-DNDEBUG
adafruitfeatherpicoprobe.menu.usbstack.picosdk=Pico SDK
adafruitfeatherpicoprobe.menu.usbstack.picosdk.build.usbstack_flags=
adafruitfeatherpicoprobe.menu.usbstack.tinyusb=Adafruit TinyUSB
adafruitfeatherpicoprobe.menu.usbstack.tinyusb.build.usbstack_flags=-DUSE_TINYUSB "-I{build.core.path}/TinyUSB" "-I{runtime.platform.path}/libraries/Adafruit_TinyUSB_Arduino/src/arduino"

# -----------------------------------
# Generic RP2040
# -----------------------------------
generic.name=Generic RP2040
generic.vid.0=0x2e8a
generic.pid.0=0xf00a
Expand All @@ -488,6 +541,10 @@ generic.build.core=rp2040
generic.build.mcu=rp2040
generic.build.ldscript=memmap_default.ld
generic.build.boot2=boot2_generic_03h_4_padded_checksum
generic.build.vid=0x2e8a
generic.build.pid=0xf00a
generic.build.usb_manufacturer="Generic"
generic.build.usb_product="RP2040"
generic.menu.flash.2097152_0=2MB (no FS)
generic.menu.flash.2097152_0.upload.maximum_size=2093056
generic.menu.flash.2097152_0.build.flash_length=2093056
Expand Down Expand Up @@ -578,6 +635,10 @@ generic.menu.dbglvl.All=All
generic.menu.dbglvl.All.build.debug_level=-DDEBUG_RP2040_WIRE -DDEBUG_RP2040_SPI -DDEBUG_RP2040_CORE
generic.menu.dbglvl.NDEBUG=NDEBUG
generic.menu.dbglvl.NDEBUG.build.debug_level=-DNDEBUG
generic.menu.usbstack.picosdk=Pico SDK
generic.menu.usbstack.picosdk.build.usbstack_flags=
generic.menu.usbstack.tinyusb=Adafruit TinyUSB
generic.menu.usbstack.tinyusb.build.usbstack_flags=-DUSE_TINYUSB "-I{build.core.path}/TinyUSB" "-I{runtime.platform.path}/libraries/Adafruit_TinyUSB_Arduino/src/arduino"
generic.menu.boot2.boot2_generic_03h_2_padded_checksum=Generic SPI /2
generic.menu.boot2.boot2_generic_03h_2_padded_checksum.build.boot2=boot2_generic_03h_2_padded_checksum
generic.menu.boot2.boot2_generic_03h_4_padded_checksum=Generic SPI /4
Expand All @@ -594,6 +655,10 @@ generic.menu.boot2.boot2_w25x10cl_2_padded_checksum=W25X10CL QSPI /2
generic.menu.boot2.boot2_w25x10cl_2_padded_checksum.build.boot2=boot2_w25x10cl_2_padded_checksum
generic.menu.boot2.boot2_w25x10cl_4_padded_checksum=W25X10CL QSPI /4
generic.menu.boot2.boot2_w25x10cl_4_padded_checksum.build.boot2=boot2_w25x10cl_4_padded_checksum

# -----------------------------------
# Generic RP2040 (Picoprobe)
# -----------------------------------
genericpicoprobe.name=Generic RP2040 (Picoprobe)
genericpicoprobe.vid.0=0x2e8a
genericpicoprobe.pid.0=0x0004
Expand All @@ -614,6 +679,10 @@ genericpicoprobe.build.core=rp2040
genericpicoprobe.build.mcu=rp2040
genericpicoprobe.build.ldscript=memmap_default.ld
genericpicoprobe.build.boot2=boot2_generic_03h_4_padded_checksum
genericpicoprobe.build.vid=0x2e8a
genericpicoprobe.build.pid=0xf00a
genericpicoprobe.build.usb_manufacturer="Generic"
genericpicoprobe.build.usb_product="RP2040 (Picoprobe)"
genericpicoprobe.menu.flash.2097152_0=2MB (no FS)
genericpicoprobe.menu.flash.2097152_0.upload.maximum_size=2093056
genericpicoprobe.menu.flash.2097152_0.build.flash_length=2093056
Expand Down Expand Up @@ -704,6 +773,10 @@ genericpicoprobe.menu.dbglvl.All=All
genericpicoprobe.menu.dbglvl.All.build.debug_level=-DDEBUG_RP2040_WIRE -DDEBUG_RP2040_SPI -DDEBUG_RP2040_CORE
genericpicoprobe.menu.dbglvl.NDEBUG=NDEBUG
genericpicoprobe.menu.dbglvl.NDEBUG.build.debug_level=-DNDEBUG
genericpicoprobe.menu.usbstack.picosdk=Pico SDK
genericpicoprobe.menu.usbstack.picosdk.build.usbstack_flags=
genericpicoprobe.menu.usbstack.tinyusb=Adafruit TinyUSB
genericpicoprobe.menu.usbstack.tinyusb.build.usbstack_flags=-DUSE_TINYUSB "-I{build.core.path}/TinyUSB" "-I{runtime.platform.path}/libraries/Adafruit_TinyUSB_Arduino/src/arduino"
genericpicoprobe.menu.boot2.boot2_generic_03h_2_padded_checksum=Generic SPI /2
genericpicoprobe.menu.boot2.boot2_generic_03h_2_padded_checksum.build.boot2=boot2_generic_03h_2_padded_checksum
genericpicoprobe.menu.boot2.boot2_generic_03h_4_padded_checksum=Generic SPI /4
Expand Down
9 changes: 8 additions & 1 deletion cores/rp2040/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,14 @@ unsigned long millis();
#define HAVE_HWSERIAL2

#ifdef __cplusplus
#include "SerialUSB.h"

#ifdef USE_TINYUSB
// Needed for declaring Serial
#include "Adafruit_USBD_CDC.h"
#else
#include "SerialUSB.h"
#endif

#include "SerialUART.h"
#include "RP2040.h"
#include "Bootsel.h"
Expand Down
4 changes: 4 additions & 0 deletions cores/rp2040/RP2040USB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef USE_TINYUSB

#include <Arduino.h>
#include "CoreMutex.h"
#include "RP2040USB.h"
Expand Down Expand Up @@ -317,3 +319,5 @@ void tud_hid_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uin
(void) bufsize;
}

#endif

4 changes: 4 additions & 0 deletions cores/rp2040/SerialUSB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifndef USE_TINYUSB

#include <Arduino.h>
#include "CoreMutex.h"

Expand Down Expand Up @@ -189,3 +191,5 @@ void arduino::serialEventRun(void)
serialEvent();
}
}

#endif
2 changes: 1 addition & 1 deletion cores/rp2040/Stream.h
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#include "api/String.h"
#include "api/Stream.h"
90 changes: 90 additions & 0 deletions cores/rp2040/TinyUSB/tusb_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2018, hathach for Adafruit
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifndef _TUSB_CONFIG_H_
#define _TUSB_CONFIG_H_

#ifdef __cplusplus
extern "C" {
#endif

//--------------------------------------------------------------------
// COMMON CONFIGURATION
//--------------------------------------------------------------------
#ifdef USE_TINYUSB
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
#else
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_NONE
#endif

//#ifndef CFG_TUSB_MCU
// #define CFG_TUSB_MCU OPT_MCU_RP2040
//#endif
#define CFG_TUSB_OS OPT_OS_PICO

#define CFG_TUSB_DEBUG 0
//#if CFG_TUSB_DEBUG
// #define tu_printf serial1_printf
// extern int serial1_printf(const char *__restrict __format, ...);
//#endif

#define CFG_TUSB_MEM_SECTION
#define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4)

//--------------------------------------------------------------------
// DEVICE CONFIGURATION
//--------------------------------------------------------------------

#define CFG_TUD_ENDOINT0_SIZE 64

//------------- CLASS -------------//
#define CFG_TUD_CDC 1
#define CFG_TUD_MSC 1
#define CFG_TUD_HID 1
#define CFG_TUD_MIDI 1
#define CFG_TUD_VENDOR 1

// CDC FIFO size of TX and RX
#define CFG_TUD_CDC_RX_BUFSIZE 256
#define CFG_TUD_CDC_TX_BUFSIZE 256

// MSC Buffer size of Device Mass storage
#define CFG_TUD_MSC_BUFSIZE 512

// HID buffer size Should be sufficient to hold ID (if any) + Data
#define CFG_TUD_HID_BUFSIZE 64

// MIDI FIFO size of TX and RX
#define CFG_TUD_MIDI_RX_BUFSIZE 128
#define CFG_TUD_MIDI_TX_BUFSIZE 128

// Vendor FIFO size of TX and RX
#define CFG_TUD_VENDOR_RX_BUFSIZE 64
#define CFG_TUD_VENDOR_TX_BUFSIZE 64

#ifdef __cplusplus
}
#endif

#endif /* _TUSB_CONFIG_H_ */
24 changes: 18 additions & 6 deletions cores/rp2040/delay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,42 @@
#include <pico.h>
#include <pico/time.h>

extern "C" void delay( unsigned long ms ) {
#ifdef USE_TINYUSB
#include "Adafruit_TinyUSB_API.h"
#endif

extern "C"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rp2040 port from the Arduino Lib is changed to call the tud_task() from irq context like "pico-sdk" stack. The TinyUSB_Device_Task() is also implemented in the way the require mutex to run. Therefore it no longer needs to use the task ready event API as previous commit.

The reason for not pursuing thread-context task, is that the sleep_ms() from pico-sdk call wfe() does not provide hook() to run background work in thread-context. In short CPU will wake in case of USB IRQ, run the handler and then sleep again without any chances to run tud_task() in thread-context.

PS: Let me know if you want to revert to have extern "C" for each function instead of block {}.

{

void delay( unsigned long ms ) {
if (!ms) {
return;
}

sleep_ms(ms);
}

extern "C" void delayMicroseconds( unsigned int usec ) {
void delayMicroseconds( unsigned int usec ) {
if (!usec) {
return;
}
sleep_us(usec);
}

extern "C" void yield() {
// NOOP
void yield() {
#ifdef USE_TINYUSB
TinyUSB_Device_Task();
TinyUSB_Device_FlushCDC();
#endif
}

extern "C" uint32_t millis() {

uint32_t millis() {
return to_ms_since_boot(get_absolute_time());
}

extern "C" uint32_t micros() {
uint32_t micros() {
return to_us_since_boot(get_absolute_time());
}

} // extern C
Loading