Skip to content

Commit 33c2a08

Browse files
committed
work rather well with esp32s2
1 parent ea80c18 commit 33c2a08

File tree

3 files changed

+90
-23
lines changed

3 files changed

+90
-23
lines changed

examples/DualRole/Simple/device_info/usbh_helper.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,14 @@
5050

5151
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
5252
#include "SPI.h"
53+
54+
#ifdef ARDUINO_ARCH_ESP32
55+
// ESP32 specify SCK, MOSI, MISO, CS, INT
56+
Adafruit_USBH_Host USBHost(36, 35, 37, 15, 14);
57+
#else
5358
// USB Host using MAX3421E: SPI, CS, INT
5459
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
60+
#endif
5561
#else
5662
Adafruit_USBH_Host USBHost;
5763
#endif

src/arduino/Adafruit_USBH_Host.cpp

Lines changed: 72 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,41 @@
3535
#include "Adafruit_TinyUSB_API.h"
3636
#include "Adafruit_USBH_Host.h"
3737

38-
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
39-
static void max3421_isr(void);
40-
#endif
41-
4238
Adafruit_USBH_Host *Adafruit_USBH_Host::_instance = NULL;
4339

4440
Adafruit_USBH_Host::Adafruit_USBH_Host(void) {
4541
Adafruit_USBH_Host::_instance = this;
4642
_spi = NULL;
47-
_cs = _intr = -1;
43+
_cs = _intr = _sck = _mosi = _miso = -1;
4844
}
4945

46+
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
47+
static void max3421_isr(void);
48+
49+
#if defined(ARDUINO_ARCH_ESP32)
50+
SemaphoreHandle_t max3421_intr_sem;
51+
static void max3421_intr_task(void *param);
52+
#endif
53+
5054
Adafruit_USBH_Host::Adafruit_USBH_Host(SPIClass *spi, int8_t cs, int8_t intr) {
5155
Adafruit_USBH_Host::_instance = this;
5256
_spi = spi;
5357
_cs = cs;
5458
_intr = intr;
59+
_sck = _mosi = _miso = -1;
60+
}
61+
62+
Adafruit_USBH_Host::Adafruit_USBH_Host(int8_t sck, int8_t mosi, int8_t miso,
63+
int8_t cs, int8_t intr) {
64+
Adafruit_USBH_Host::_instance = this;
65+
_spi = NULL;
66+
_cs = cs;
67+
_intr = intr;
68+
_sck = sck;
69+
_mosi = mosi;
70+
_miso = miso;
5571
}
72+
#endif
5673

5774
bool Adafruit_USBH_Host::configure(uint8_t rhport, uint32_t cfg_id,
5875
const void *cfg_param) {
@@ -68,7 +85,7 @@ bool Adafruit_USBH_Host::configure_pio_usb(uint8_t rhport,
6885

6986
bool Adafruit_USBH_Host::begin(uint8_t rhport) {
7087
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
71-
if (_spi == NULL || _intr < 0 || _cs < 0) {
88+
if (_intr < 0 || _cs < 0) {
7289
return false;
7390
}
7491

@@ -77,7 +94,25 @@ bool Adafruit_USBH_Host::begin(uint8_t rhport) {
7794
digitalWrite(_cs, HIGH);
7895

7996
// SPI init
80-
SPI.begin();
97+
if (_spi) {
98+
_spi->begin();
99+
} else {
100+
#ifdef ARDUINO_ARCH_ESP32
101+
// ESP32 SPI assign pins when init instead of declaration as standard API
102+
_spi = new SPIClass(HSPI);
103+
_spi->begin(_sck, _miso, _mosi, -1);
104+
#else
105+
// Software SPI is not supported yet
106+
return false;
107+
#endif
108+
}
109+
110+
#ifdef ARDUINO_ARCH_ESP32
111+
// Create an task for executing interrupt handler in thread mode
112+
max3421_intr_sem = xSemaphoreCreateBinary();
113+
xTaskCreateUniversal(max3421_intr_task, "max3421 intr", 2048, NULL, 2, NULL,
114+
ARDUINO_RUNNING_CORE);
115+
#endif
81116

82117
// Interrupt pin
83118
pinMode(_intr, INPUT_PULLUP);
@@ -124,23 +159,38 @@ TU_ATTR_WEAK void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance,
124159
//--------------------------------------------------------------------+
125160
#if CFG_TUH_ENABLED && defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
126161

127-
#if defined(ARDUINO_ARCH_ESP32) && !defined(PLATFORMIO)
162+
#if defined(ARDUINO_ARCH_ESP32)
163+
164+
#ifdef PLATFORMIO
165+
#define tuh_int_handler_esp32 tuh_int_handler
166+
#else
128167
extern "C" void hcd_int_handler_esp32(uint8_t rhport, bool in_isr);
168+
#define tuh_int_handler_esp32 hcd_int_handler_esp32
129169
#endif
130170

171+
static void max3421_intr_task(void *param) {
172+
(void)param;
173+
174+
while (1) {
175+
xSemaphoreTake(max3421_intr_sem, portMAX_DELAY);
176+
tuh_int_handler_esp32(1, false);
177+
}
178+
}
179+
131180
static void max3421_isr(void) {
132-
// ESP32 out-of-sync
133-
#if defined(ARDUINO_ARCH_ESP32)
134-
#if defined(PLATFORMIO)
135-
tuh_int_handler(1, false);
136-
#else
137-
hcd_int_handler_esp32(1, false);
138-
#endif
139-
#else
140-
tuh_int_handler(1, true);
141-
#endif
181+
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
182+
xSemaphoreGiveFromISR(max3421_intr_sem, &xHigherPriorityTaskWoken);
183+
if (xHigherPriorityTaskWoken) {
184+
portYIELD_FROM_ISR();
185+
}
142186
}
143187

188+
#else
189+
190+
static void max3421_isr(void) { tuh_int_handler(1, true); }
191+
192+
#endif // ESP32
193+
144194
extern "C" {
145195

146196
void tuh_max3421_spi_cs_api(uint8_t rhport, bool active) {
@@ -167,8 +217,8 @@ bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf,
167217
// SAMD 21/51 can only work reliably at 12MHz
168218
uint32_t const max_clock = 12000000ul;
169219
#else
170-
// uint32_t const max_clock = 26000000ul;
171-
uint32_t const max_clock = 4000000ul;
220+
uint32_t const max_clock = 26000000ul;
221+
// uint32_t const max_clock = 4000000ul;
172222
#endif
173223

174224
SPISettings config(max_clock, MSBFIRST, SPI_MODE0);
@@ -190,6 +240,8 @@ bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf,
190240
rx_buf[count] = data;
191241
}
192242
}
243+
#elif defined(ARDUINO_ARCH_ESP32)
244+
host->_spi->transferBytes(tx_buf, rx_buf, xfer_bytes);
193245
#else
194246
host->_spi->transfer(tx_buf, rx_buf, xfer_bytes);
195247
#endif

src/arduino/Adafruit_USBH_Host.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,26 @@ void tuh_max3421_int_api(uint8_t rhport, bool enabled);
4141
}
4242

4343
class Adafruit_USBH_Host {
44+
45+
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
4446
private:
4547
SPIClass *_spi;
4648
int8_t _cs;
4749
int8_t _intr;
4850

49-
public:
50-
// default constructor
51-
Adafruit_USBH_Host(void);
51+
// for esp32 or using softwareSPI
52+
int8_t _sck, _mosi, _miso;
5253

54+
public:
5355
// constructor for using MAX3421E (host shield)
5456
Adafruit_USBH_Host(SPIClass *spi, int8_t cs, int8_t intr);
57+
Adafruit_USBH_Host(int8_t sck, int8_t mosi, int8_t miso, int8_t cs,
58+
int8_t intr);
59+
#endif
60+
61+
public:
62+
// default constructor
63+
Adafruit_USBH_Host(void);
5564

5665
bool configure(uint8_t rhport, uint32_t cfg_id, const void *cfg_param);
5766

0 commit comments

Comments
 (0)