Skip to content

Commit 7438327

Browse files
authored
Merge pull request #126 from adafruit/support-esp32-2.0
Add support for ESP32-S2 arduino core version 2.0.0
2 parents 82fafbc + 53e5625 commit 7438327

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+648
-268
lines changed

.github/workflows/githubci.yml

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ jobs:
99
fail-fast: false
1010
matrix:
1111
arduino-platform:
12+
# ESP32S2
13+
- 'funhouse'
14+
- 'magtag'
15+
- 'metroesp32s2'
1216
# nRF52
1317
- 'cpb'
1418
- 'nrf52840'

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ There are 2 type of supported cores: with and without built-in support for TinyU
2121

2222
### Cores with built-in support
2323

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

26-
- [Adafruit_nRF52_Arduino](https://github.com/adafruit/Adafruit_nRF52_Arduino)
26+
- [adafruit/Adafruit_nRF52_Arduino](https://github.com/adafruit/Adafruit_nRF52_Arduino)
2727
- [adafruit/ArduinoCore-samd](https://github.com/adafruit/ArduinoCore-samd)
2828
- [earlephilhower/arduino-pico](https://github.com/earlephilhower/arduino-pico)
29+
- [espressif/arduino-esp32](https://github.com/espressif/arduino-esp32)
30+
31+
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.
2932

3033
### Cores without built-in support
3134

changelog.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Adafruit TinyUSB Arduino Library Changelog
22

3+
## 1.5.0 - 2021.09.29
4+
5+
- Add support for ESP32-S2 core version 2.0.0
6+
- 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.
7+
8+
- Add new constructor for Adafruit_USBD_HID(desc_report, len, protocol, interval_ms, has_out_endpoint)
9+
310
## 1.4.4 - 2021.08.18
411

512
- Update tinyusb stack

examples/CDC/cdc_multi/.magtag.test.skip

Whitespace-only changes.

examples/CDC/cdc_multi/.metroesp32s2.test.skip

Whitespace-only changes.

examples/CDC/no_serial/.funhouse.test.skip

Whitespace-only changes.

examples/CDC/no_serial/.magtag.test.skip

Whitespace-only changes.

examples/CDC/no_serial/.metroesp32s2.test.skip

Whitespace-only changes.

examples/Composite/mouse_external_flash/mouse_external_flash.ino

+24-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#include "Adafruit_SPIFlash.h"
2020
#include "Adafruit_TinyUSB.h"
2121

22+
//--------------------------------------------------------------------+
23+
// MSC External Flash Config
24+
//--------------------------------------------------------------------+
25+
2226
// Uncomment to run example with FRAM
2327
// #define FRAM_CS A5
2428
// #define FRAM_SPI SPI
@@ -51,26 +55,39 @@ Adafruit_SPIFlash flash(&flashTransport);
5155

5256
Adafruit_USBD_MSC usb_msc;
5357

58+
//--------------------------------------------------------------------+
59+
// HID Config
60+
//--------------------------------------------------------------------+
61+
5462
// HID report descriptor using TinyUSB's template
5563
// Single Report (no ID) descriptor
5664
uint8_t const desc_hid_report[] =
5765
{
5866
TUD_HID_REPORT_DESC_MOUSE()
5967
};
6068

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

63-
#if defined ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS
73+
#if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY)
6474
const int pin = 4; // Left Button
6575
bool activeState = true;
66-
#elif defined ARDUINO_NRF52840_FEATHER
67-
const int pin = 7; // UserSw
76+
77+
#elif defined(ARDUINO_FUNHOUSE_ESP32S2)
78+
const int pin = BUTTON_DOWN;
79+
bool activeState = true;
80+
81+
#elif defined PIN_BUTTON1
82+
const int pin = PIN_BUTTON1;
6883
bool activeState = false;
84+
6985
#else
7086
const int pin = 12;
7187
bool activeState = false;
7288
#endif
7389

90+
7491
// the setup function runs once when you press reset or power the board
7592
void setup()
7693
{
@@ -92,11 +109,12 @@ void setup()
92109

93110
usb_msc.begin();
94111

95-
96112
// Set up button
97113
pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);
98114

99-
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
115+
// Notes: following commented-out functions has no affect on ESP32
116+
// usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
117+
100118
usb_hid.begin();
101119

102120
Serial.begin(115200);

examples/Composite/mouse_ramdisk/mouse_ramdisk.ino

+21-4
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,40 @@
1616

1717
#include "Adafruit_TinyUSB.h"
1818

19+
//--------------------------------------------------------------------+
20+
// MSC RAM Disk Config
21+
//--------------------------------------------------------------------+
22+
1923
// 8KB is the smallest size that windows allow to mount
2024
#define DISK_BLOCK_NUM 16
2125
#define DISK_BLOCK_SIZE 512
2226
#include "ramdisk.h"
2327

28+
Adafruit_USBD_MSC usb_msc;
29+
30+
//--------------------------------------------------------------------+
31+
// HID Config
32+
//--------------------------------------------------------------------+
33+
2434
// HID report descriptor using TinyUSB's template
2535
// Single Report (no ID) descriptor
2636
uint8_t const desc_hid_report[] =
2737
{
2838
TUD_HID_REPORT_DESC_MOUSE()
2939
};
3040

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

34-
#if defined ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS || defined ARDUINO_NRF52840_CIRCUITPLAY
45+
#if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY)
3546
const int pin = 4; // Left Button
3647
bool activeState = true;
3748

49+
#elif defined(ARDUINO_FUNHOUSE_ESP32S2)
50+
const int pin = BUTTON_DOWN;
51+
bool activeState = true;
52+
3853
#elif defined PIN_BUTTON1
3954
const int pin = PIN_BUTTON1;
4055
bool activeState = false;
@@ -69,7 +84,9 @@ void setup()
6984
// Set up button
7085
pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);
7186

72-
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
87+
// Notes: following commented-out functions has no affect on ESP32
88+
// usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
89+
7390
usb_hid.begin();
7491

7592
Serial.begin(115200);

examples/HID/hid_boot_keyboard/hid_boot_keyboard.ino

+79-15
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
*********************************************************************/
1111

1212
#include "Adafruit_TinyUSB.h"
13+
#include <Adafruit_NeoPixel.h>
1314

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

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

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

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

37-
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 };
38-
41+
// number of pins
3942
uint8_t pincount = sizeof(pins)/sizeof(pins[0]);
4043

44+
// For keycode definition check out https://github.com/hathach/tinyusb/blob/master/src/class/hid/hid.h
45+
uint8_t hidcode[] = { HID_KEY_ARROW_RIGHT, HID_KEY_ARROW_LEFT, HID_KEY_ARROW_DOWN, HID_KEY_ARROW_UP };
46+
47+
#if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY) || defined(ARDUINO_FUNHOUSE_ESP32S2)
48+
bool activeState = true;
49+
#else
50+
bool activeState = false;
51+
#endif
52+
53+
//------------- Neopixel -------------//
54+
// #define PIN_NEOPIXEL 8
55+
#ifdef PIN_NEOPIXEL
56+
57+
// How many NeoPixels are attached to the Arduino?
58+
// use on-board defined NEOPIXEL_NUM if existed
59+
#ifndef NEOPIXEL_NUM
60+
#define NEOPIXEL_NUM 10
61+
#endif
62+
63+
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NEOPIXEL_NUM, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
64+
65+
#endif
66+
67+
4168
// the setup function runs once when you press reset or power the board
4269
void setup()
4370
{
@@ -46,22 +73,53 @@ void setup()
4673
TinyUSB_Device_Init(0);
4774
#endif
4875

49-
usb_hid.setBootProtocol(HID_ITF_PROTOCOL_KEYBOARD);
50-
usb_hid.setPollInterval(2);
51-
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
76+
// Notes: following commented-out functions has no affect on ESP32
77+
// usb_hid.setBootProtocol(HID_ITF_PROTOCOL_KEYBOARD);
78+
// usb_hid.setPollInterval(2);
79+
// usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
80+
// usb_hid.setStringDescriptor("TinyUSB Keyboard");
81+
82+
// Set up output report (on control endpoint) for Capslock indicator
5283
usb_hid.setReportCallback(NULL, hid_report_callback);
53-
//usb_hid.setStringDescriptor("TinyUSB Keyboard");
5484

5585
usb_hid.begin();
5686

5787
// led pin
5888
pinMode(LED_BUILTIN, OUTPUT);
5989
digitalWrite(LED_BUILTIN, LOW);
6090

91+
// neopixel if existed
92+
#ifdef PIN_NEOPIXEL
93+
pixels.begin();
94+
pixels.setBrightness(50);
95+
96+
#ifdef NEOPIXEL_POWER
97+
pinMode(NEOPIXEL_POWER, OUTPUT);
98+
digitalWrite(NEOPIXEL_POWER, NEOPIXEL_POWER_ON);
99+
#endif
100+
#endif
101+
102+
// overwrite input pin with PIN_BUTTONx
103+
#ifdef PIN_BUTTON1
104+
pins[0] = PIN_BUTTON1;
105+
#endif
106+
107+
#ifdef PIN_BUTTON2
108+
pins[1] = PIN_BUTTON2;
109+
#endif
110+
111+
#ifdef PIN_BUTTON3
112+
pins[2] = PIN_BUTTON3;
113+
#endif
114+
115+
#ifdef PIN_BUTTON4
116+
pins[3] = PIN_BUTTON4;
117+
#endif
118+
61119
// Set up pin as input
62120
for (uint8_t i=0; i<pincount; i++)
63121
{
64-
pinMode(pins[i], INPUT_PULLUP);
122+
pinMode(pins[i], activeState ? INPUT_PULLDOWN : INPUT_PULLUP);
65123
}
66124

67125
// wait until device mounted
@@ -83,7 +141,7 @@ void loop()
83141
// scan normal key and send report
84142
for(uint8_t i=0; i < pincount; i++)
85143
{
86-
if ( 0 == digitalRead(pins[i]) )
144+
if ( activeState == digitalRead(pins[i]) )
87145
{
88146
// if pin is active (low), add its hid code to key report
89147
keycode[count++] = hidcode[i];
@@ -129,13 +187,19 @@ void hid_report_callback(uint8_t report_id, hid_report_type_t report_type, uint8
129187
{
130188
(void) report_id;
131189
(void) bufsize;
190+
132191
// LED indicator is output report with only 1 byte length
133192
if ( report_type != HID_REPORT_TYPE_OUTPUT ) return;
134193

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

139-
// turn on LED if caplock is set
198+
// turn on LED if capslock is set
140199
digitalWrite(LED_BUILTIN, ledIndicator & KEYBOARD_LED_CAPSLOCK);
200+
201+
#ifdef PIN_NEOPIXEL
202+
pixels.fill(ledIndicator & KEYBOARD_LED_CAPSLOCK ? 0xff0000 : 0x000000);
203+
pixels.show();
204+
#endif
141205
}

examples/HID/hid_boot_mouse/hid_boot_mouse.ino

+13-7
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818
* Depending on the board, the button pin
1919
* and its active state (when pressed) are different
2020
*/
21-
#if defined ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS || defined ARDUINO_NRF52840_CIRCUITPLAY
21+
#if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY)
2222
const int pin = 4; // Left Button
2323
bool activeState = true;
2424

25+
#elif defined(ARDUINO_FUNHOUSE_ESP32S2)
26+
const int pin = BUTTON_DOWN;
27+
bool activeState = true;
28+
2529
#elif defined PIN_BUTTON1
2630
const int pin = PIN_BUTTON1;
2731
bool activeState = false;
@@ -39,8 +43,9 @@ uint8_t const desc_hid_report[] =
3943
TUD_HID_REPORT_DESC_MOUSE()
4044
};
4145

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

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

56-
usb_hid.setBootProtocol(HID_ITF_PROTOCOL_MOUSE);
57-
usb_hid.setPollInterval(2);
58-
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
59-
//usb_hid.setStringDescriptor("TinyUSB Mouse");
61+
// Notes: following commented-out functions has no affect on ESP32
62+
// usb_hid.setBootProtocol(HID_ITF_PROTOCOL_MOUSE);
63+
// usb_hid.setPollInterval(2);
64+
// usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
65+
// usb_hid.setStringDescriptor("TinyUSB Mouse");
6066

6167
usb_hid.begin();
6268

0 commit comments

Comments
 (0)