Skip to content

Commit 581fff6

Browse files
committed
add support for esp32s2
1 parent 3fae073 commit 581fff6

File tree

11 files changed

+324
-14
lines changed

11 files changed

+324
-14
lines changed

examples/Composite/hid_generic_inout_ramdisk/ramdisk.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#define RAMDISK_H_
2727

2828
#define README_CONTENTS \
29-
"This is Adafruit TinyUSB MassStorage device demo on RAM disk."
29+
"This is TinyUSB MassStorage device demo for Arduino on RAM disk."
3030

3131
uint8_t msc_disk[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] = {
3232
//------------- Block0: Boot Sector -------------//

examples/Composite/mouse_ramdisk/ramdisk.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#define RAMDISK_H_
2727

2828
#define README_CONTENTS \
29-
"This is Adafruit TinyUSB MassStorage device demo on RAM disk."
29+
"This is TinyUSB MassStorage device demo for Arduino on RAM disk."
3030

3131
uint8_t msc_disk[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] = {
3232
//------------- Block0: Boot Sector -------------//

examples/MassStorage/msc_ramdisk/ramdisk.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#define RAMDISK_H_
2727

2828
#define README_CONTENTS \
29-
"This is Adafruit TinyUSB MassStorage device demo on RAM disk."
29+
"This is TinyUSB MassStorage device demo for Arduino on RAM disk."
3030

3131
uint8_t msc_disk[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] = {
3232
//------------- Block0: Boot Sector -------------//

examples/MassStorage/msc_ramdisk_dual/ramdisk.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@
2929
// LUN 0
3030
//--------------------------------------------------------------------+
3131
#define README0_CONTENTS \
32-
"LUN0: This is tinyusb's MassStorage Class demo.\r\n\r\n\
33-
If you find any bugs or get any questions, feel free to file an\r\n\
34-
issue at github.com/hathach/tinyusb"
32+
"LUN0: This is TinyUSB MassStorage device demo for Arduino on RAM disk."
3533

3634
uint8_t msc_disk0[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] = {
3735
//------------- Block0: Boot Sector -------------//
@@ -119,9 +117,7 @@ uint8_t msc_disk0[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] = {
119117
// LUN 1
120118
//--------------------------------------------------------------------+
121119
#define README1_CONTENTS \
122-
"LUN1: This is tinyusb's MassStorage Class demo.\r\n\r\n\
123-
If you find any bugs or get any questions, feel free to file an\r\n\
124-
issue at github.com/hathach/tinyusb"
120+
"LUN1: This is TinyUSB MassStorage device demo for Arduino on RAM disk."
125121

126122
uint8_t msc_disk1[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] = {
127123
//------------- Block0: Boot Sector -------------//

src/Adafruit_TinyUSB.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
#define ADAFRUIT_TINYUSB_H_
2727

2828
// Give warning for Core that must select TinyUSB via menu
29-
#if (defined ARDUINO_ARCH_SAMD || defined ARDUINO_ARCH_RP2040) && !defined USE_TINYUSB
30-
#error TinyUSB is not selected, please select it in Tools->Menu->USB Stack
29+
#if !defined(USE_TINYUSB) && ( defined(ARDUINO_ARCH_SAMD) || \
30+
(defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED)) )
31+
#error TinyUSB is not selected, please select it in "Tools->Menu->USB Stack"
3132
#endif
3233

3334
#include "tusb_option.h"

src/arduino/Adafruit_USBD_CDC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
uint8_t _cdc_instance_count = 0;
4141

42-
Adafruit_USBD_CDC Serial;
42+
Adafruit_USBD_CDC SerialTinyUSB;
4343

4444
Adafruit_USBD_CDC::Adafruit_USBD_CDC(void) {
4545
_begun = false;

src/arduino/Adafruit_USBD_CDC.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,16 @@ class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface {
8383
*/
8484
};
8585

86-
extern Adafruit_USBD_CDC Serial;
86+
// "Serial" is used with TinyUSB CDC
87+
#if defined(USE_TINYUSB) && !(defined(ARDUINO_ARCH_ESP32) && ARDUINO_SERIAL_PORT==0)
88+
extern Adafruit_USBD_CDC Serial;
89+
#define SerialTinyUSB Serial
90+
#endif
91+
92+
// Serial is probably used with HW Uart
93+
#ifndef SerialTinyUSB
94+
extern Adafruit_USBD_CDC SerialTinyUSB;
95+
#endif
8796

8897
#endif // __cplusplus
8998
#endif

src/arduino/Adafruit_USBD_Device.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
#include "Adafruit_USBD_CDC.h"
3232
#include "Adafruit_USBD_Device.h"
3333

34+
#ifndef USB_VID
35+
#define USB_VID 0xcafe
36+
#define USB_PID 0xcafe
37+
#endif
38+
3439
#ifndef USB_MANUFACTURER
3540
#define USB_MANUFACTURER "Unknown"
3641
#endif
@@ -47,6 +52,7 @@
4752
#define USB_CONFIG_POWER 100
4853
#endif
4954

55+
5056
enum { STRID_LANGUAGE = 0, STRID_MANUFACTURER, STRID_PRODUCT, STRID_SERIAL };
5157

5258
Adafruit_USBD_Device USBDevice;
@@ -217,7 +223,7 @@ bool Adafruit_USBD_Device::begin(uint8_t rhport) {
217223
_desc_device.bDeviceSubClass = MISC_SUBCLASS_COMMON;
218224
_desc_device.bDeviceProtocol = MISC_PROTOCOL_IAD;
219225

220-
Serial.begin(115200);
226+
SerialTinyUSB.begin(115200);
221227

222228
// Init device hardware and call tusb_init()
223229
TinyUSB_Port_InitDevice(rhport);
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2019, 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+
#include "tusb_option.h"
26+
27+
#if defined(ARDUINO_ARCH_ESP32) && TUSB_OPT_DEVICE_ENABLED
28+
29+
#include "sdkconfig.h"
30+
31+
#include "soc/soc.h"
32+
#include "soc/efuse_reg.h"
33+
#include "soc/rtc_cntl_reg.h"
34+
#include "soc/usb_struct.h"
35+
#include "soc/usb_reg.h"
36+
#include "soc/usb_wrap_reg.h"
37+
#include "soc/usb_wrap_struct.h"
38+
#include "soc/usb_periph.h"
39+
#include "soc/periph_defs.h"
40+
#include "soc/timer_group_struct.h"
41+
#include "soc/system_reg.h"
42+
43+
#include "hal/usb_hal.h"
44+
45+
// compiler error with gpio_ll_get_drive_capability()
46+
// invalid conversion from 'uint32_t' {aka 'unsigned int'} to 'gpio_drive_cap_t' [-fpermissive]
47+
// #include "hal/gpio_ll.h"
48+
49+
#include "freertos/FreeRTOS.h"
50+
#include "freertos/task.h"
51+
52+
#include "driver/gpio.h"
53+
#include "driver/periph_ctrl.h"
54+
55+
#include "esp_efuse.h"
56+
#include "esp_efuse_table.h"
57+
#include "esp_rom_gpio.h"
58+
59+
#include "esp32-hal.h"
60+
61+
#include "esp32s2/rom/usb/usb_persist.h"
62+
#include "esp32s2/rom/usb/usb_dc.h"
63+
#include "esp32s2/rom/usb/chip_usb_dw_wrapper.h"
64+
65+
#include "arduino/Adafruit_TinyUSB_API.h"
66+
#include "arduino/Adafruit_USBD_Device.h"
67+
68+
//--------------------------------------------------------------------+
69+
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
70+
//--------------------------------------------------------------------+
71+
72+
#define USBD_STACK_SZ (4096)
73+
74+
//--------------------------------------------------------------------+
75+
// Forward USB interrupt events to TinyUSB IRQ Handler
76+
//--------------------------------------------------------------------+
77+
// extern "C" void USBD_IRQHandler(void) {
78+
// tud_int_handler(0);
79+
//}
80+
81+
//--------------------------------------------------------------------+
82+
// Porting API
83+
//--------------------------------------------------------------------+
84+
85+
// USB Device Driver task
86+
// This top level thread processes all usb events and invokes callbacks
87+
static void usb_device_task(void *param) {
88+
(void)param;
89+
while(1) tud_task(); // RTOS forever loop
90+
}
91+
92+
typedef enum {
93+
RESTART_NO_PERSIST,
94+
RESTART_PERSIST,
95+
RESTART_BOOTLOADER,
96+
RESTART_BOOTLOADER_DFU,
97+
RESTART_TYPE_MAX
98+
} restart_type_t;
99+
100+
static bool usb_persist_enabled = false;
101+
static restart_type_t usb_persist_mode = RESTART_NO_PERSIST;
102+
103+
static void IRAM_ATTR usb_persist_shutdown_handler(void)
104+
{
105+
if(usb_persist_mode != RESTART_NO_PERSIST){
106+
if (usb_persist_enabled) {
107+
usb_dc_prepare_persist();
108+
}
109+
if (usb_persist_mode == RESTART_BOOTLOADER) {
110+
//USB CDC Download
111+
if (usb_persist_enabled) {
112+
chip_usb_set_persist_flags(USBDC_PERSIST_ENA);
113+
} else {
114+
periph_module_reset(PERIPH_USB_MODULE);
115+
periph_module_enable(PERIPH_USB_MODULE);
116+
}
117+
REG_WRITE(RTC_CNTL_OPTION1_REG, RTC_CNTL_FORCE_DOWNLOAD_BOOT);
118+
} else if (usb_persist_mode == RESTART_BOOTLOADER_DFU) {
119+
//DFU Download
120+
chip_usb_set_persist_flags(USBDC_BOOT_DFU);
121+
REG_WRITE(RTC_CNTL_OPTION1_REG, RTC_CNTL_FORCE_DOWNLOAD_BOOT);
122+
} else if (usb_persist_enabled) {
123+
//USB Persist reboot
124+
chip_usb_set_persist_flags(USBDC_PERSIST_ENA);
125+
}
126+
}
127+
}
128+
129+
static void configure_pins(usb_hal_context_t *usb)
130+
{
131+
for (const usb_iopin_dsc_t *iopin = usb_periph_iopins; iopin->pin != -1; ++iopin) {
132+
if ((usb->use_external_phy) || (iopin->ext_phy_only == 0)) {
133+
esp_rom_gpio_pad_select_gpio(iopin->pin);
134+
if (iopin->is_output) {
135+
esp_rom_gpio_connect_out_signal(iopin->pin, iopin->func, false, false);
136+
} else {
137+
esp_rom_gpio_connect_in_signal(iopin->pin, iopin->func, false);
138+
if ((iopin->pin != GPIO_FUNC_IN_LOW) && (iopin->pin != GPIO_FUNC_IN_HIGH)) {
139+
// workaround for compiler error with including "hal/gpio_ll.h"
140+
// gpio_ll_input_enable(&GPIO, (gpio_num_t) iopin->pin);
141+
PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[iopin->pin]);
142+
}
143+
}
144+
esp_rom_gpio_pad_unhold(iopin->pin);
145+
}
146+
}
147+
if (!usb->use_external_phy) {
148+
gpio_set_drive_capability((gpio_num_t) USBPHY_DM_NUM, GPIO_DRIVE_CAP_3);
149+
gpio_set_drive_capability((gpio_num_t) USBPHY_DP_NUM, GPIO_DRIVE_CAP_3);
150+
}
151+
}
152+
153+
void TinyUSB_Port_InitDevice(uint8_t rhport) {
154+
(void)rhport;
155+
156+
// from esp32-hal_tinyusb
157+
bool usb_did_persist = (USB_WRAP.date.val == USBDC_PERSIST_ENA);
158+
159+
//if(usb_did_persist && usb_persist_enabled){
160+
// Enable USB/IO_MUX peripheral reset, if coming from persistent reboot
161+
REG_CLR_BIT(RTC_CNTL_USB_CONF_REG, RTC_CNTL_IO_MUX_RESET_DISABLE);
162+
REG_CLR_BIT(RTC_CNTL_USB_CONF_REG, RTC_CNTL_USB_RESET_DISABLE);
163+
//} else
164+
if(!usb_did_persist || !usb_persist_enabled){
165+
// Reset USB module
166+
periph_module_reset(PERIPH_USB_MODULE);
167+
periph_module_enable(PERIPH_USB_MODULE);
168+
}
169+
170+
esp_register_shutdown_handler(usb_persist_shutdown_handler);
171+
172+
usb_hal_context_t hal = {
173+
.use_external_phy = false
174+
};
175+
usb_hal_init(&hal);
176+
configure_pins(&hal);
177+
178+
tusb_init();
179+
180+
// Create a task for tinyusb device stack
181+
xTaskCreate(usb_device_task, "usbd", USBD_STACK_SZ, NULL, configMAX_PRIORITIES - 1, NULL);
182+
}
183+
184+
void TinyUSB_Port_EnterDFU(void) {
185+
// Reset to Bootloader
186+
usb_persist_mode = RESTART_BOOTLOADER;
187+
esp_restart();
188+
}
189+
190+
uint8_t TinyUSB_Port_GetSerialNumber(uint8_t serial_id[16]) {
191+
uint32_t *serial_32 = (uint32_t *)serial_id;
192+
193+
/* Get the MAC address */
194+
const uint32_t mac0 = __builtin_bswap32(REG_GET_FIELD(EFUSE_RD_MAC_SPI_SYS_0_REG, EFUSE_MAC_0));
195+
const uint16_t mac1 = __builtin_bswap16((uint16_t) REG_GET_FIELD(EFUSE_RD_MAC_SPI_SYS_1_REG, EFUSE_MAC_1));
196+
197+
memcpy(serial_id, &mac1, 2);
198+
memcpy(serial_id+2, &mac0, 4);
199+
200+
return 6;
201+
}
202+
203+
extern "C" void yield(void) {
204+
TinyUSB_Device_FlushCDC();
205+
vPortYield();
206+
}
207+
208+
#endif // USE_TINYUSB

0 commit comments

Comments
 (0)