Skip to content
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

support esp32p4 #503

Merged
merged 3 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/githubci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ jobs:
# ESP32 ci use dev json
- 'feather_esp32s2'
- 'feather_esp32s3'
- 'esp32p4'
# nRF52
- 'cpb'
- 'nrf52840'
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Following core has TinyUSB as either the primary usb stack or selectable via men
- [adafruit/Adafruit_nRF52_Arduino](https://github.com/adafruit/Adafruit_nRF52_Arduino)
- [adafruit/ArduinoCore-samd](https://github.com/adafruit/ArduinoCore-samd)
- [earlephilhower/arduino-pico](https://github.com/earlephilhower/arduino-pico)
- [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32) additional Tools menu is needed
- [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32) Host mode using MAX3421E controller should work with all chips. Device mode only support S2/S3/P4 and additional Tools menu are needed
- `USB Mode=USB-OTG (TinyUSB)` for S3 and P4
- `USB CDC On Boot=Enabled`, `USB Firmware MSC On Boot=Disabled`, `USB DFU On Boot=Disabled`
- [openwch/arduino_core_ch32](https://github.com/openwch/arduino_core_ch32)
Expand Down
1 change: 1 addition & 0 deletions examples/CDC/cdc_multi/.skip.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ feather_esp32s3
funhouse
magtag
metroesp32s2
esp32p4
pico_rp2040_tinyusb_host
16 changes: 10 additions & 6 deletions examples/CDC/cdc_multi/cdc_multi.ino
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,27 @@

#include <Adafruit_TinyUSB.h>

#define LED LED_BUILTIN

// Create 2nd instance of CDC Ports.
#ifdef ARDUINO_ARCH_ESP32
#error "Currently multiple CDCs on ESP32-Sx is not yet supported. An PR to update core/esp32/USBCDC and/or pre-built libusb are needed."
#error "Currently multiple CDCs on ESP32 is not yet supported"
// for ESP32, we need to specify instance number when declaring object
Adafruit_USBD_CDC USBSer1(1);
#else
Adafruit_USBD_CDC USBSer1;
#endif

void setup() {
pinMode(LED, OUTPUT);
#ifdef LED_BUILTIN
pinMode(LED_BUILTIN, OUTPUT);
#endif

Serial.begin(115200);

// check to see if multiple CDCs are enabled
if ( CFG_TUD_CDC < 2 ) {
digitalWrite(LED, HIGH); // LED on for error indicator
#ifdef LED_BUILTIN
digitalWrite(LED_BUILTIN, HIGH); // LED on for error indicator
#endif

while(1) {
Serial.printf("CFG_TUD_CDC must be at least 2, current value is %u\n", CFG_TUD_CDC);
Expand Down Expand Up @@ -96,7 +98,9 @@ void loop() {

if (delay_without_delaying(500)) {
LEDstate = !LEDstate;
digitalWrite(LED, LEDstate);
#ifdef LED_BUILTIN
digitalWrite(LED_BUILTIN, LEDstate);
#endif
}
}

Expand Down
15 changes: 7 additions & 8 deletions examples/CDC/no_serial/no_serial.ino
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
* Note: this will cause device to loose the touch1200 and require
* user manual interaction to put device into bootloader/DFU mode.
*/

int led = LED_BUILTIN;

void setup()
{
void setup() {
// Manual begin() is required on core without built-in support e.g. mbed rp2040
if (!TinyUSBDevice.isInitialized()) {
TinyUSBDevice.begin(0);
Expand All @@ -38,11 +34,12 @@ void setup()
TinyUSBDevice.attach();
}

pinMode(led, OUTPUT);
#ifdef LED_BUILTIN
pinMode(LED_BUILTIN, OUTPUT);
#endif
}

void loop()
{
void loop() {
#ifdef TINYUSB_NEED_POLLING_TASK
// Manual call tud_task since it isn't called by Core's background
TinyUSBDevice.task();
Expand All @@ -53,6 +50,8 @@ void loop()
static uint8_t led_state = 0;
if (millis() - ms > 1000) {
ms = millis();
#ifdef LED_BUILTIN
digitalWrite(LED_BUILTIN, 1-led_state);
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ void data_log(void) {
}

// Turn on LED when start writing
#ifdef LED_BUILTIN
digitalWrite(LED_BUILTIN, HIGH);
#endif

f_log = fatfs.open(LOG_FILE, O_WRITE | O_APPEND | O_CREAT);

Expand Down Expand Up @@ -112,7 +114,9 @@ void usbhost_rtos_task(void *param) {
void setup() {
Serial.begin(115200);

#ifdef LED_BUILTIN
pinMode(LED_BUILTIN, OUTPUT);
#endif

#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
// init host stack on controller (rhport) 1
Expand Down Expand Up @@ -172,10 +176,12 @@ bool write_complete_callback(uint8_t dev_addr, tuh_msc_complete_data_t const *cb
(void) dev_addr;
(void) cb_data;

#ifdef LED_BUILTIN
// turn off LED after write is complete
// Note this only marks the usb transfer is complete, device can take longer to actual
// write data to physical flash
digitalWrite(LED_BUILTIN, LOW);
#endif

return true;
}
Expand Down
4 changes: 4 additions & 0 deletions examples/HID/hid_boot_keyboard/hid_boot_keyboard.ino
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ void setup() {
}

// led pin
#ifdef LED_BUILTIN
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
#endif

// overwrite input pin with PIN_BUTTONx
#ifdef PIN_BUTTON1
Expand Down Expand Up @@ -176,6 +178,8 @@ void hid_report_callback(uint8_t report_id, hid_report_type_t report_type, uint8
// Kana (4) | Compose (3) | ScrollLock (2) | CapsLock (1) | Numlock (0)
uint8_t ledIndicator = buffer[0];

#ifdef LED_BUILTIN
// turn on LED if capslock is set
digitalWrite(LED_BUILTIN, ledIndicator & KEYBOARD_LED_CAPSLOCK);
#endif
}
4 changes: 4 additions & 0 deletions examples/MIDI/midi_multi_ports/midi_multi_ports.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
Adafruit_USBD_MIDI usb_midi(3);

void setup() {
#ifdef LED_BUILTIN
pinMode(LED_BUILTIN, OUTPUT);
#endif

// Manual begin() is required on core without built-in support e.g. mbed rp2040
if (!TinyUSBDevice.isInitialized()) {
Expand Down Expand Up @@ -52,6 +54,8 @@ void loop() {
static uint8_t led_state = 0;
if (millis() - ms > 1000) {
ms = millis();
#ifdef LED_BUILTIN
digitalWrite(LED_BUILTIN, 1-led_state);
#endif
}
}
2 changes: 0 additions & 2 deletions examples/MIDI/midi_test/midi_test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ void setup() {

Serial.begin(115200);

pinMode(LED_BUILTIN, OUTPUT);

usb_midi.setStringDescriptor("TinyUSB MIDI");

// Initialize MIDI, and listen to all MIDI channels
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ bool fs_formatted = false;
bool fs_changed = true;;

// the setup function runs once when you press reset or power the board
void setup()
{
void setup() {
#ifdef LED_BUILTIN
pinMode(LED_BUILTIN, OUTPUT);
#endif

Serial.begin(115200);

Expand Down Expand Up @@ -150,7 +151,9 @@ int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize) {
// Process data in buffer to disk's storage and
// return number of written bytes (must be multiple of block size)
int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize) {
#ifdef LED_BUILTIN
digitalWrite(LED_BUILTIN, HIGH);
#endif

// Note: SPIFLash Block API: readBlocks/writeBlocks/syncBlocks
// already include 4K sector caching internally. We don't need to cache it, yahhhh!!
Expand All @@ -168,5 +171,7 @@ void msc_flush_cb (void) {

fs_changed = true;

#ifdef LED_BUILTIN
digitalWrite(LED_BUILTIN, LOW);
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ bool flash_changed = false;

// the setup function runs once when you press reset or power the board
void setup() {
#ifdef LED_BUILTIN
pinMode(LED_BUILTIN, OUTPUT);
#endif
Serial.begin(115200);

// MSC with 2 Logical Units: LUN0: External Flash, LUN1: SDCard
Expand Down Expand Up @@ -234,11 +236,12 @@ int32_t sdcard_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
// Callback invoked when received WRITE10 command.
// Process data in buffer to disk's storage and
// return number of written bytes (must be multiple of block size)
int32_t sdcard_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
{
int32_t sdcard_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize) {
bool rc;

#ifdef LED_BUILTIN
digitalWrite(LED_BUILTIN, HIGH);
#endif

#if SD_FAT_VERSION >= 20000
rc = sd.card()->writeSectors(lba, buffer, bufsize/512);
Expand All @@ -264,7 +267,9 @@ void sdcard_flush_cb (void)

sd_changed = true;

#ifdef LED_BUILTIN
digitalWrite(LED_BUILTIN, LOW);
#endif
}

#ifdef SDCARD_DETECT
Expand Down Expand Up @@ -299,8 +304,7 @@ bool sdcard_ready_callback(void)
// Callback invoked when received READ10 command.
// Copy disk's data to buffer (up to bufsize) and
// return number of copied bytes (must be multiple of block size)
int32_t external_flash_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
{
int32_t external_flash_read_cb (uint32_t lba, void* buffer, uint32_t bufsize) {
// Note: SPIFLash Bock API: readBlocks/writeBlocks/syncBlocks
// already include 4K sector caching internally. We don't need to cache it, yahhhh!!
return flash.readBlocks(lba, (uint8_t*) buffer, bufsize/512) ? bufsize : -1;
Expand All @@ -309,9 +313,10 @@ int32_t external_flash_read_cb (uint32_t lba, void* buffer, uint32_t bufsize)
// Callback invoked when received WRITE10 command.
// Process data in buffer to disk's storage and
// return number of written bytes (must be multiple of block size)
int32_t external_flash_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize)
{
int32_t external_flash_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize) {
#ifdef LED_BUILTIN
digitalWrite(LED_BUILTIN, HIGH);
#endif

// Note: SPIFLash Bock API: readBlocks/writeBlocks/syncBlocks
// already include 4K sector caching internally. We don't need to cache it, yahhhh!!
Expand All @@ -320,14 +325,15 @@ int32_t external_flash_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize

// Callback invoked when WRITE10 command is completed (status received and accepted by host).
// used to flush any pending cache.
void external_flash_flush_cb (void)
{
void external_flash_flush_cb (void) {
flash.syncBlocks();

// clear file system's cache to force refresh
fatfs.cacheClear();

flash_changed = true;

#ifdef LED_BUILTIN
digitalWrite(LED_BUILTIN, LOW);
#endif
}
1 change: 1 addition & 0 deletions examples/MassStorage/msc_sd/.skip.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ feather_esp32s3
funhouse
magtag
metroesp32s2
esp32p4
feather_rp2040_tinyusb
pico_rp2040_tinyusb_host
CH32V20x_EVT
9 changes: 7 additions & 2 deletions examples/MassStorage/msc_sdfat/msc_sdfat.ino
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ Adafruit_USBD_MSC usb_msc;
bool fs_changed;

// the setup function runs once when you press reset or power the board
void setup()
{
void setup() {
Serial.begin(115200);

#ifdef LED_BUILTIN
pinMode(LED_BUILTIN, OUTPUT);
#endif

// Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively
usb_msc.setID("Adafruit", "SD Card", "1.0");
Expand Down Expand Up @@ -138,7 +139,9 @@ int32_t msc_read_cb (uint32_t lba, void* buffer, uint32_t bufsize) {
int32_t msc_write_cb (uint32_t lba, uint8_t* buffer, uint32_t bufsize) {
bool rc;

#ifdef LED_BUILTIN
digitalWrite(LED_BUILTIN, HIGH);
#endif

#if SD_FAT_VERSION >= 20000
rc = sd.card()->writeSectors(lba, buffer, bufsize/512);
Expand All @@ -163,5 +166,7 @@ void msc_flush_cb (void) {

fs_changed = true;

#ifdef LED_BUILTIN
digitalWrite(LED_BUILTIN, LOW);
#endif
}
12 changes: 7 additions & 5 deletions examples/WebUSB/webusb_serial/webusb_serial.ino
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ Adafruit_USBD_WebUSB usb_web;
// Page source can be found at https://github.com/hathach/tinyusb-webusb-page/tree/main/webusb-serial
WEBUSB_URL_DEF(landingPage, 1 /*https*/, "example.tinyusb.org/webusb-serial/index.html");

int led_pin = LED_BUILTIN;

// the setup function runs once when you press reset or power the board
void setup() {
// Manual begin() is required on core without built-in support e.g. mbed rp2040
Expand All @@ -44,8 +42,10 @@ void setup() {
}
Serial.begin(115200);

pinMode(led_pin, OUTPUT);
digitalWrite(led_pin, LOW);
#ifdef LED_BUILTIN
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
#endif

usb_web.setLandingPage(&landingPage);
usb_web.setLineStateCallback(line_state_callback);
Expand Down Expand Up @@ -104,7 +104,9 @@ void loop() {
}

void line_state_callback(bool connected) {
digitalWrite(led_pin, connected);
#ifdef LED_BUILTIN
digitalWrite(LED_BUILTIN, connected);
#endif

if (connected) {
usb_web.println("WebUSB interface connected !!");
Expand Down
8 changes: 4 additions & 4 deletions src/arduino/Adafruit_USBD_CDC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
#define TINYUSB_API_VERSION 0
#endif

#define BULK_PACKET_SIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)

// SerialTinyUSB can be macro expanding to "Serial" on supported cores
Adafruit_USBD_CDC SerialTinyUSB;

Expand Down Expand Up @@ -71,8 +69,10 @@ uint16_t Adafruit_USBD_CDC::getInterfaceDescriptor(uint8_t itfnum_deprecated,
uint8_t _strid = 0;
#endif

uint8_t const desc[] = {TUD_CDC_DESCRIPTOR(itfnum, _strid, ep_notif, 8,
ep_out, ep_in, BULK_PACKET_SIZE)};
uint16_t const mps =
(TUD_OPT_HIGH_SPEED ? 512 : 64); // TODO actual link speed
uint8_t const desc[] = {
TUD_CDC_DESCRIPTOR(itfnum, _strid, ep_notif, 8, ep_out, ep_in, mps)};

uint16_t const len = sizeof(desc);

Expand Down
Loading