Skip to content

Commit 2c8cb9e

Browse files
committed
improve compatibility with esp32
1 parent 4f81112 commit 2c8cb9e

File tree

17 files changed

+176
-71
lines changed

17 files changed

+176
-71
lines changed

src/Adafruit_TinyUSB.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
#error TinyUSB is not selected, please select it in "Tools->Menu->USB Stack"
3333
#endif
3434

35+
// ESP32 out-of-sync
36+
#ifdef ARDUINO_ARCH_ESP32
37+
#include "arduino/ports/esp32/tusb_config_esp32.h"
38+
#endif
39+
3540
#include "tusb_option.h"
3641

3742
// Device

src/arduino/Adafruit_USBH_Host.cpp

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
* THE SOFTWARE.
2323
*/
2424

25+
// ESP32 out-of-sync
26+
#ifdef ARDUINO_ARCH_ESP32
27+
#include "arduino/ports/esp32/tusb_config_esp32.h"
28+
#define MSBFIRST SPI_MSBFIRST
29+
#endif
30+
2531
#include "tusb_option.h"
2632

2733
#if CFG_TUH_ENABLED
@@ -118,7 +124,15 @@ TU_ATTR_WEAK void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance,
118124
//--------------------------------------------------------------------+
119125
#if CFG_TUH_ENABLED && defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
120126

121-
static void max3421_isr(void) { tuh_int_handler(1); }
127+
static void max3421_isr(void) {
128+
// ESP32 out-of-sync
129+
#if defined(ARDUINO_ARCH_ESP32)
130+
extern "C" void hcd_int_handler_esp32(uint8_t rhport, bool in_isr);
131+
hcd_int_handler_esp32(1, false);
132+
#else
133+
tuh_int_handler(1, true);
134+
#endif
135+
}
122136

123137
extern "C" {
124138

@@ -152,21 +166,20 @@ bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf,
152166
SPISettings config(max_clock, MSBFIRST, SPI_MODE0);
153167
host->_spi->beginTransaction(config);
154168

155-
size_t count = 0;
156-
while (count < tx_len || count < rx_len) {
169+
// TODO improve spi speed with burst transfer
170+
for (size_t count = 0; count < tx_len || count < rx_len; count++) {
157171
uint8_t data = 0x00;
158172

173+
// TX
159174
if (count < tx_len) {
160175
data = tx_buf[count];
161176
}
162-
163177
data = host->_spi->transfer(data);
164178

179+
// RX
165180
if (count < rx_len) {
166181
rx_buf[count] = data;
167182
}
168-
169-
count++;
170183
}
171184

172185
host->_spi->endTransaction();
@@ -179,10 +192,12 @@ void tuh_max3421_int_api(uint8_t rhport, bool enabled) {
179192
if (!Adafruit_USBH_Host::_instance) {
180193
return;
181194
}
195+
Adafruit_USBH_Host *host = Adafruit_USBH_Host::_instance;
196+
(void)host;
182197

183198
#ifdef ARDUINO_ARCH_SAMD
199+
//--- SAMD51 ---//
184200
#ifdef __SAMD51__
185-
Adafruit_USBH_Host *host = Adafruit_USBH_Host::_instance;
186201
const IRQn_Type irq =
187202
(IRQn_Type)(EIC_0_IRQn + g_APinDescription[host->_intr].ulExtInt);
188203

@@ -192,6 +207,7 @@ void tuh_max3421_int_api(uint8_t rhport, bool enabled) {
192207
NVIC_DisableIRQ(irq);
193208
}
194209
#else
210+
//--- SAMD21 ---//
195211
if (enabled) {
196212
NVIC_EnableIRQ(EIC_IRQn);
197213
} else {
@@ -200,12 +216,22 @@ void tuh_max3421_int_api(uint8_t rhport, bool enabled) {
200216
#endif
201217

202218
#elif defined(ARDUINO_NRF52_ADAFRUIT)
219+
//--- nRF52 ---//
203220
if (enabled) {
204221
NVIC_EnableIRQ(GPIOTE_IRQn);
205222
} else {
206223
NVIC_DisableIRQ(GPIOTE_IRQn);
207224
}
208225

226+
#elif defined(ARDUINO_ARCH_ESP32)
227+
//--- ESP32 ---//
228+
if (enabled) {
229+
gpio_intr_enable((gpio_num_t)host->_intr);
230+
} else {
231+
gpio_intr_disable((gpio_num_t)host->_intr);
232+
}
233+
#else
234+
#error "MAX3421e host is not Unsupported by this architecture"
209235
#endif
210236
}
211237
}

src/arduino/msc/Adafruit_USBH_MSC.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
* THE SOFTWARE.
2323
*/
2424

25+
// ESP32 out-of-sync
26+
#ifdef ARDUINO_ARCH_ESP32
27+
#include "arduino/ports/esp32/tusb_config_esp32.h"
28+
#endif
29+
2530
#include "tusb_option.h"
2631

2732
#if CFG_TUH_ENABLED && CFG_TUH_MSC

src/arduino/ports/esp32/tusb_config_esp32.h

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
* THE SOFTWARE.
2323
*/
2424

25-
#ifndef _TUSB_CONFIG_ESP32_H_
26-
#define _TUSB_CONFIG_ESP32_H_
25+
#ifndef TUSB_CONFIG_ESP32_H_
26+
#define TUSB_CONFIG_ESP32_H_
2727

2828
#ifdef __cplusplus
2929
extern "C" {
@@ -43,11 +43,14 @@ extern "C" {
4343
//--------------------------------------------------------------------+
4444
// ESP32 out-of-sync
4545
//--------------------------------------------------------------------+
46-
#ifndef tu_static
47-
#define tu_static static
46+
#include "esp_idf_version.h"
4847

48+
// IDF 4.4.4 and prior is using tinyusb 0.14.0
49+
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 4, 5)
4950
#include <stddef.h>
5051
#include <string.h>
52+
53+
#define tu_static static
5154
static inline int tu_memset_s(void *dest, size_t destsz, int ch, size_t count) {
5255
if (count > destsz) {
5356
return -1;
@@ -63,25 +66,37 @@ static inline int tu_memcpy_s(void *dest, size_t destsz, const void *src,
6366
memcpy(dest, src, count);
6467
return 0;
6568
}
69+
70+
enum {
71+
TUSB_EPSIZE_BULK_FS = 64,
72+
TUSB_EPSIZE_BULK_HS = 512,
73+
74+
TUSB_EPSIZE_ISO_FS_MAX = 1023,
75+
TUSB_EPSIZE_ISO_HS_MAX = 1024,
76+
};
77+
78+
enum { TUSB_INDEX_INVALID_8 = 0xFFu };
6679
#endif
6780

6881
#ifndef CFG_TUD_MEM_SECTION
6982
#define CFG_TUD_MEM_SECTION CFG_TUSB_MEM_SECTION
7083
#endif
7184

85+
#ifndef CFG_TUH_MEM_SECTION
86+
#define CFG_TUH_MEM_SECTION CFG_TUSB_MEM_SECTION
87+
#endif
88+
89+
#ifndef CFG_TUH_MEM_ALIGN
90+
#define CFG_TUH_MEM_ALIGN CFG_TUSB_MEM_ALIGN
91+
#endif
92+
7293
#ifndef CFG_TUD_LOG_LEVEL
7394
#define CFG_TUD_LOG_LEVEL 2
7495
#endif
7596

76-
#if 0
77-
//--------------------------------------------------------------------
78-
// COMMON CONFIGURATION
79-
//--------------------------------------------------------------------
80-
#define CFG_TUSB_MCU OPT_MCU_NRF5X
81-
82-
#define CFG_TUSB_OS OPT_OS_FREERTOS
83-
#define CFG_TUSB_MEM_SECTION
84-
#define CFG_TUSB_MEM_ALIGN __attribute__((aligned(4)))
97+
// #ifndef CFG_TUH_LOG_LEVEL
98+
// #define CFG_TUH_LOG_LEVEL 2
99+
// #endif
85100

86101
#ifndef CFG_TUSB_DEBUG
87102
#define CFG_TUSB_DEBUG 0
@@ -90,27 +105,30 @@ static inline int tu_memcpy_s(void *dest, size_t destsz, const void *src,
90105
// For selectively disable device log (when > CFG_TUSB_DEBUG)
91106
// #define CFG_TUD_LOG_LEVEL 3
92107

93-
#ifdef USE_TINYUSB
94-
// Enable device stack
95-
#define CFG_TUD_ENABLED 1
96-
97-
// Enable host stack with MAX3421E (host shield)
98-
#define CFG_TUH_ENABLED 1
99-
#define CFG_TUH_MAX3421 1
100-
101-
#else
102-
#define CFG_TUD_ENABLED 0
103-
#define CFG_TUH_ENABLED 0
104-
#endif
105-
106108
//--------------------------------------------------------------------
107109
// DEVICE CONFIGURATION
108110
//--------------------------------------------------------------------
109111

112+
// device configuration is configured in BSP
113+
// sdk/include/arduino_tinyusb/include/tusb_config.h
114+
110115
//--------------------------------------------------------------------
111116
// Host Configuration
112117
//--------------------------------------------------------------------
113118

119+
// Enable host stack with MAX3421E (host shield)
120+
#define CFG_TUH_ENABLED 0
121+
#define CFG_TUH_MAX_SPEED OPT_MODE_HIGH_SPEED
122+
#ifndef TUH_OPT_HIGH_SPEED
123+
#define TUH_OPT_HIGH_SPEED 0
124+
#endif
125+
126+
#define CFG_TUH_MAX3421 1
127+
128+
#ifndef CFG_TUH_MAX3421_ENDPOINT_TOTAL
129+
#define CFG_TUH_MAX3421_ENDPOINT_TOTAL (8 + 4 * (CFG_TUH_DEVICE_MAX - 1))
130+
#endif
131+
114132
// Size of buffer to hold descriptors and other data used for enumeration
115133
#define CFG_TUH_ENUMERATION_BUFSIZE 256
116134

@@ -150,8 +168,6 @@ static inline int tu_memcpy_s(void *dest, size_t destsz, const void *src,
150168
#define CFG_TUH_CDC_LINE_CODING_ON_ENUM \
151169
{ 115200, CDC_LINE_CONDING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 }
152170

153-
#endif
154-
155171
#ifdef __cplusplus
156172
}
157173
#endif

src/class/cdc/cdc_device.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424
* This file is part of the TinyUSB stack.
2525
*/
2626

27-
#include "tusb_option.h"
28-
29-
#if (CFG_TUD_ENABLED && CFG_TUD_CDC)
30-
3127
// ESP32 out-of-sync
3228
#ifdef ARDUINO_ARCH_ESP32
3329
#include "arduino/ports/esp32/tusb_config_esp32.h"
3430
#endif
3531

32+
#include "tusb_option.h"
33+
34+
#if (CFG_TUD_ENABLED && CFG_TUD_CDC)
35+
3636
#include "device/usbd.h"
3737
#include "device/usbd_pvt.h"
3838

src/class/dfu/dfu_rt_device.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424
* This file is part of the TinyUSB stack.
2525
*/
2626

27-
#include "tusb_option.h"
28-
29-
#if (CFG_TUD_ENABLED && CFG_TUD_DFU_RUNTIME)
30-
3127
// ESP32 out-of-sync
3228
#ifdef ARDUINO_ARCH_ESP32
3329
#include "arduino/ports/esp32/tusb_config_esp32.h"
3430
#endif
3531

32+
#include "tusb_option.h"
33+
34+
#if (CFG_TUD_ENABLED && CFG_TUD_DFU_RUNTIME)
35+
3636
#include "device/usbd.h"
3737
#include "device/usbd_pvt.h"
3838

src/class/hid/hid_device.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
* This file is part of the TinyUSB stack.
2525
*/
2626

27+
// ESP32 out-of-sync
28+
#ifdef ARDUINO_ARCH_ESP32
29+
#include "arduino/ports/esp32/tusb_config_esp32.h"
30+
#endif
31+
2732
#include "tusb_option.h"
2833

2934
#if (CFG_TUD_ENABLED && CFG_TUD_HID)
@@ -32,11 +37,6 @@
3237
// INCLUDE
3338
//--------------------------------------------------------------------+
3439

35-
// ESP32 out-of-sync
36-
#ifdef ARDUINO_ARCH_ESP32
37-
#include "arduino/ports/esp32/tusb_config_esp32.h"
38-
#endif
39-
4040
#include "device/usbd.h"
4141
#include "device/usbd_pvt.h"
4242

src/class/midi/midi_device.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
* This file is part of the TinyUSB stack.
2525
*/
2626

27+
// ESP32 out-of-sync
28+
#ifdef ARDUINO_ARCH_ESP32
29+
#include "arduino/ports/esp32/tusb_config_esp32.h"
30+
#endif
31+
2732
#include "tusb_option.h"
2833

2934
#if (CFG_TUD_ENABLED && CFG_TUD_MIDI)
@@ -32,11 +37,6 @@
3237
// INCLUDE
3338
//--------------------------------------------------------------------+
3439

35-
// ESP32 out-of-sync
36-
#ifdef ARDUINO_ARCH_ESP32
37-
#include "arduino/ports/esp32/tusb_config_esp32.h"
38-
#endif
39-
4040
#include "device/usbd.h"
4141
#include "device/usbd_pvt.h"
4242

src/class/msc/msc_device.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424
* This file is part of the TinyUSB stack.
2525
*/
2626

27-
#include "tusb_option.h"
28-
29-
#if (CFG_TUD_ENABLED && CFG_TUD_MSC)
30-
3127
// ESP32 out-of-sync
3228
#ifdef ARDUINO_ARCH_ESP32
3329
#include "arduino/ports/esp32/tusb_config_esp32.h"
3430
#endif
3531

32+
#include "tusb_option.h"
33+
34+
#if (CFG_TUD_ENABLED && CFG_TUD_MSC)
35+
3636
#include "device/dcd.h" // for faking dcd_event_xfer_complete
3737
#include "device/usbd.h"
3838
#include "device/usbd_pvt.h"

src/class/vendor/vendor_device.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@
2424
* This file is part of the TinyUSB stack.
2525
*/
2626

27-
#include "tusb_option.h"
28-
29-
#if (CFG_TUD_ENABLED && CFG_TUD_VENDOR)
30-
3127
// ESP32 out-of-sync
3228
// Somehow we have linking issue: multiple definition of vendor APIs with arduino-esp32 master
3329
// skip this driver entirely and used the pre-compiled libarduino_tinyusb.a instead
3430
#ifdef ARDUINO_ARCH_ESP32
3531
#include "arduino/ports/esp32/tusb_config_esp32.h"
3632
#else
3733

34+
#include "tusb_option.h"
35+
36+
#if (CFG_TUD_ENABLED && CFG_TUD_VENDOR)
37+
3838
#include "device/usbd.h"
3939
#include "device/usbd_pvt.h"
4040

0 commit comments

Comments
 (0)