Skip to content

Add support for ESP32-S2 arduino core version 2.0.0 #126

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 24 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
29c1687
slightly modify stack to compile with esp32
hathach Sep 24, 2021
4044e9a
update lib to compile with esp 2.0.0
hathach Sep 24, 2021
611608f
esp32s2 running well with lib API
hathach Sep 27, 2021
0385061
hid works with esp32 by moving arguments into constructor
hathach Sep 27, 2021
a6da6f2
minor clean up
hathach Sep 28, 2021
015735a
fine tune esp32 port
hathach Sep 28, 2021
fe9e880
change helper function signature a bit
hathach Sep 28, 2021
7499bc3
more clean up
hathach Sep 28, 2021
028058d
midi work with esp32
hathach Sep 28, 2021
7c20752
esp32 work with webusb
hathach Sep 28, 2021
1b22237
update webusb example to support NEOPIXEL_POWER pin
hathach Sep 28, 2021
335e90e
update hid_boot_keyboard to support neopixel with power pin, button a…
hathach Sep 28, 2021
7bd22e6
enable metro esp32s2 for ci
hathach Sep 28, 2021
c63bf0e
clang format
hathach Sep 28, 2021
41d45dd
update hid composite to work with funhouse
hathach Sep 28, 2021
4a3013d
skip cdc_multi and no_serial for esp32s2
hathach Sep 28, 2021
5e40ffd
update hid examples to work esp32s2
hathach Sep 28, 2021
f5b5f05
use ci-arduino with increased timeout
hathach Sep 28, 2021
0c5a29b
all hid examples work with esp32s2
hathach Sep 28, 2021
012db86
all msc examples work with esp32s2, skip a couple of incompatible exa…
hathach Sep 28, 2021
c00c628
update note for esp32s2 port
hathach Sep 29, 2021
6eb5a28
update hid generic inout exmaple to work with esp32s2, update changelog
hathach Sep 29, 2021
3c612b5
correct ci branch
hathach Sep 29, 2021
53e5625
add funhouse and magtag to ci build
hathach Sep 30, 2021
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
4 changes: 4 additions & 0 deletions .github/workflows/githubci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ jobs:
fail-fast: false
matrix:
arduino-platform:
# ESP32S2
- 'funhouse'
- 'magtag'
- 'metroesp32s2'
# nRF52
- 'cpb'
- 'nrf52840'
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ There are 2 type of supported cores: with and without built-in support for TinyU

### Cores with built-in support

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.
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.

- [Adafruit_nRF52_Arduino](https://github.com/adafruit/Adafruit_nRF52_Arduino)
- [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)

ESP32 port relies on Espressif's [esp32-hal-tinyusb.c](https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-tinyusb.c) for building usb descriptors which requires all descriptors must be specified in usb objects declaration i.e constructors. Therefore all descriptor-related fields must be part of object declaration and descriptor-related API have no effect afterwards for this port.

### Cores without built-in support

Expand Down
7 changes: 7 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Adafruit TinyUSB Arduino Library Changelog

## 1.5.0 - 2021.09.29

- Add support for ESP32-S2 core version 2.0.0
- ESP32 port relies on Espressif's [esp32-hal-tinyusb.c](https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-tinyusb.c) for building usb descriptors which requires all descriptors must be specified in usb objects declaration i.e constructors. Therefore all descriptor-related fields must be part of object declaration and descriptor-related API have no effect afterwards for this port.

- Add new constructor for Adafruit_USBD_HID(desc_report, len, protocol, interval_ms, has_out_endpoint)

## 1.4.4 - 2021.08.18

- Update tinyusb stack
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
30 changes: 24 additions & 6 deletions examples/Composite/mouse_external_flash/mouse_external_flash.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#include "Adafruit_SPIFlash.h"
#include "Adafruit_TinyUSB.h"

//--------------------------------------------------------------------+
// MSC External Flash Config
//--------------------------------------------------------------------+

// Uncomment to run example with FRAM
// #define FRAM_CS A5
// #define FRAM_SPI SPI
Expand Down Expand Up @@ -51,26 +55,39 @@ Adafruit_SPIFlash flash(&flashTransport);

Adafruit_USBD_MSC usb_msc;

//--------------------------------------------------------------------+
// HID Config
//--------------------------------------------------------------------+

// HID report descriptor using TinyUSB's template
// Single Report (no ID) descriptor
uint8_t const desc_hid_report[] =
{
TUD_HID_REPORT_DESC_MOUSE()
};

Adafruit_USBD_HID usb_hid;
// USB HID object. For ESP32 these values cannot be changed after this declaration
// desc report, desc len, protocol, interval, use out endpoint
Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_NONE, 2, false);

#if defined ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS
#if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY)
const int pin = 4; // Left Button
bool activeState = true;
#elif defined ARDUINO_NRF52840_FEATHER
const int pin = 7; // UserSw

#elif defined(ARDUINO_FUNHOUSE_ESP32S2)
const int pin = BUTTON_DOWN;
bool activeState = true;

#elif defined PIN_BUTTON1
const int pin = PIN_BUTTON1;
bool activeState = false;

#else
const int pin = 12;
bool activeState = false;
#endif


// the setup function runs once when you press reset or power the board
void setup()
{
Expand All @@ -92,11 +109,12 @@ void setup()

usb_msc.begin();


// Set up button
pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);

usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
// Notes: following commented-out functions has no affect on ESP32
// usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));

usb_hid.begin();

Serial.begin(115200);
Expand Down
25 changes: 21 additions & 4 deletions examples/Composite/mouse_ramdisk/mouse_ramdisk.ino
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,40 @@

#include "Adafruit_TinyUSB.h"

//--------------------------------------------------------------------+
// MSC RAM Disk Config
//--------------------------------------------------------------------+

// 8KB is the smallest size that windows allow to mount
#define DISK_BLOCK_NUM 16
#define DISK_BLOCK_SIZE 512
#include "ramdisk.h"

Adafruit_USBD_MSC usb_msc;

//--------------------------------------------------------------------+
// HID Config
//--------------------------------------------------------------------+

// HID report descriptor using TinyUSB's template
// Single Report (no ID) descriptor
uint8_t const desc_hid_report[] =
{
TUD_HID_REPORT_DESC_MOUSE()
};

Adafruit_USBD_HID usb_hid;
Adafruit_USBD_MSC usb_msc;
// USB HID object. For ESP32 these values cannot be changed after this declaration
// desc report, desc len, protocol, interval, use out endpoint
Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_NONE, 2, false);

#if defined ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS || defined ARDUINO_NRF52840_CIRCUITPLAY
#if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY)
const int pin = 4; // Left Button
bool activeState = true;

#elif defined(ARDUINO_FUNHOUSE_ESP32S2)
const int pin = BUTTON_DOWN;
bool activeState = true;

#elif defined PIN_BUTTON1
const int pin = PIN_BUTTON1;
bool activeState = false;
Expand Down Expand Up @@ -69,7 +84,9 @@ void setup()
// Set up button
pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);

usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
// Notes: following commented-out functions has no affect on ESP32
// usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));

usb_hid.begin();

Serial.begin(115200);
Expand Down
94 changes: 79 additions & 15 deletions examples/HID/hid_boot_keyboard/hid_boot_keyboard.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
*********************************************************************/

#include "Adafruit_TinyUSB.h"
#include <Adafruit_NeoPixel.h>

/* This sketch demonstrates USB HID keyboard.
* - PIN A0-A5 is used to send digit '0' to '5' respectively
* (On the RP2040, pins D0-D5 used)
* - LED will be used as Caplock indicator
* - LED and/or Neopixels will be used as Capslock indicator
*/

// HID report descriptor using TinyUSB's template
Expand All @@ -24,20 +25,46 @@ uint8_t const desc_hid_report[] =
TUD_HID_REPORT_DESC_KEYBOARD()
};

Adafruit_USBD_HID usb_hid;
// USB HID object. For ESP32 these values cannot be changed after this declaration
// desc report, desc len, protocol, interval, use out endpoint
Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_KEYBOARD, 2, false);

// Array of pins and its keycode
// For keycode definition see BLEHidGeneric.h
//------------- Input Pins -------------//
// Array of pins and its keycode.
// Notes: these pins can be replaced by PIN_BUTTONn if defined in setup()
#ifdef ARDUINO_ARCH_RP2040
uint8_t pins[] = { D0, D1, D2, D3, D4, D5 };
uint8_t pins[] = { D0, D1, D2, D3 };
#else
uint8_t pins[] = { A0, A1, A2, A3, A4, A5 };
uint8_t pins[] = { A0, A1, A2, A3 };
#endif

uint8_t hidcode[] = { HID_KEY_ARROW_RIGHT, HID_KEY_ARROW_LEFT, HID_KEY_ARROW_DOWN, HID_KEY_ARROW_UP , HID_KEY_4, HID_KEY_5 };

// number of pins
uint8_t pincount = sizeof(pins)/sizeof(pins[0]);

// For keycode definition check out https://github.com/hathach/tinyusb/blob/master/src/class/hid/hid.h
uint8_t hidcode[] = { HID_KEY_ARROW_RIGHT, HID_KEY_ARROW_LEFT, HID_KEY_ARROW_DOWN, HID_KEY_ARROW_UP };

#if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY) || defined(ARDUINO_FUNHOUSE_ESP32S2)
bool activeState = true;
#else
bool activeState = false;
#endif

//------------- Neopixel -------------//
// #define PIN_NEOPIXEL 8
#ifdef PIN_NEOPIXEL

// How many NeoPixels are attached to the Arduino?
// use on-board defined NEOPIXEL_NUM if existed
#ifndef NEOPIXEL_NUM
#define NEOPIXEL_NUM 10
#endif

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NEOPIXEL_NUM, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);

#endif


// the setup function runs once when you press reset or power the board
void setup()
{
Expand All @@ -46,22 +73,53 @@ void setup()
TinyUSB_Device_Init(0);
#endif

usb_hid.setBootProtocol(HID_ITF_PROTOCOL_KEYBOARD);
usb_hid.setPollInterval(2);
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
// Notes: following commented-out functions has no affect on ESP32
// usb_hid.setBootProtocol(HID_ITF_PROTOCOL_KEYBOARD);
// usb_hid.setPollInterval(2);
// usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
// usb_hid.setStringDescriptor("TinyUSB Keyboard");

// Set up output report (on control endpoint) for Capslock indicator
usb_hid.setReportCallback(NULL, hid_report_callback);
//usb_hid.setStringDescriptor("TinyUSB Keyboard");

usb_hid.begin();

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

// neopixel if existed
#ifdef PIN_NEOPIXEL
pixels.begin();
pixels.setBrightness(50);

#ifdef NEOPIXEL_POWER
pinMode(NEOPIXEL_POWER, OUTPUT);
digitalWrite(NEOPIXEL_POWER, NEOPIXEL_POWER_ON);
#endif
#endif

// overwrite input pin with PIN_BUTTONx
#ifdef PIN_BUTTON1
pins[0] = PIN_BUTTON1;
#endif

#ifdef PIN_BUTTON2
pins[1] = PIN_BUTTON2;
#endif

#ifdef PIN_BUTTON3
pins[2] = PIN_BUTTON3;
#endif

#ifdef PIN_BUTTON4
pins[3] = PIN_BUTTON4;
#endif

// Set up pin as input
for (uint8_t i=0; i<pincount; i++)
{
pinMode(pins[i], INPUT_PULLUP);
pinMode(pins[i], activeState ? INPUT_PULLDOWN : INPUT_PULLUP);
}

// wait until device mounted
Expand All @@ -83,7 +141,7 @@ void loop()
// scan normal key and send report
for(uint8_t i=0; i < pincount; i++)
{
if ( 0 == digitalRead(pins[i]) )
if ( activeState == digitalRead(pins[i]) )
{
// if pin is active (low), add its hid code to key report
keycode[count++] = hidcode[i];
Expand Down Expand Up @@ -129,13 +187,19 @@ void hid_report_callback(uint8_t report_id, hid_report_type_t report_type, uint8
{
(void) report_id;
(void) bufsize;

// LED indicator is output report with only 1 byte length
if ( report_type != HID_REPORT_TYPE_OUTPUT ) return;

// The LED bit map is as follows: (also defined by KEYBOARD_LED_* )
// Kana (4) | Compose (3) | ScrollLock (2) | CapsLock (1) | Numlock (0)
uint8_t ledIndicator = buffer[0];

// turn on LED if caplock is set
// turn on LED if capslock is set
digitalWrite(LED_BUILTIN, ledIndicator & KEYBOARD_LED_CAPSLOCK);

#ifdef PIN_NEOPIXEL
pixels.fill(ledIndicator & KEYBOARD_LED_CAPSLOCK ? 0xff0000 : 0x000000);
pixels.show();
#endif
}
20 changes: 13 additions & 7 deletions examples/HID/hid_boot_mouse/hid_boot_mouse.ino
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
* Depending on the board, the button pin
* and its active state (when pressed) are different
*/
#if defined ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS || defined ARDUINO_NRF52840_CIRCUITPLAY
#if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY)
const int pin = 4; // Left Button
bool activeState = true;

#elif defined(ARDUINO_FUNHOUSE_ESP32S2)
const int pin = BUTTON_DOWN;
bool activeState = true;

#elif defined PIN_BUTTON1
const int pin = PIN_BUTTON1;
bool activeState = false;
Expand All @@ -39,8 +43,9 @@ uint8_t const desc_hid_report[] =
TUD_HID_REPORT_DESC_MOUSE()
};

// USB HID object
Adafruit_USBD_HID usb_hid;
// USB HID object. For ESP32 these values cannot be changed after this declaration
// desc report, desc len, protocol, interval, use out endpoint
Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_MOUSE, 2, false);

// the setup function runs once when you press reset or power the board
void setup()
Expand All @@ -53,10 +58,11 @@ void setup()
// Set up button, pullup opposite to active state
pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);

usb_hid.setBootProtocol(HID_ITF_PROTOCOL_MOUSE);
usb_hid.setPollInterval(2);
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
//usb_hid.setStringDescriptor("TinyUSB Mouse");
// Notes: following commented-out functions has no affect on ESP32
// usb_hid.setBootProtocol(HID_ITF_PROTOCOL_MOUSE);
// usb_hid.setPollInterval(2);
// usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
// usb_hid.setStringDescriptor("TinyUSB Mouse");

usb_hid.begin();

Expand Down
Loading