Skip to content

Commit 2b0b814

Browse files
authored
Merge pull request #346 from adafruit/support-featherwing-max3421
Support featherwing max3421
2 parents 22f5dac + 2ca4b90 commit 2b0b814

File tree

18 files changed

+136
-30
lines changed

18 files changed

+136
-30
lines changed

examples/DualRole/CDC/serial_host_bridge/usbh_helper.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@
3939

4040
#if defined(ARDUINO_METRO_ESP32S2)
4141
Adafruit_USBH_Host USBHost(&SPI, 15, 14);
42-
#else
43-
// Default use SPI and pin 10, 9 for CS and INT
42+
#elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2)
43+
Adafruit_USBH_Host USBHost(&SPI, 27, 33);
44+
#elif defined(ARDUINO_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
45+
// For Metro shape CS and INT are pin 10, 9 (host shield default)
4446
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
47+
#else
48+
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
49+
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
4550
#endif
4651
#else
4752
// Native USB Host such as rp2040

examples/DualRole/HID/hid_device_report/usbh_helper.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@
3939

4040
#if defined(ARDUINO_METRO_ESP32S2)
4141
Adafruit_USBH_Host USBHost(&SPI, 15, 14);
42-
#else
43-
// Default use SPI and pin 10, 9 for CS and INT
42+
#elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2)
43+
Adafruit_USBH_Host USBHost(&SPI, 27, 33);
44+
#elif defined(ARDUINO_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
45+
// For Metro shape CS and INT are pin 10, 9 (host shield default)
4446
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
47+
#else
48+
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
49+
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
4550
#endif
4651
#else
4752
// Native USB Host such as rp2040

examples/DualRole/HID/hid_mouse_log_filter/hid_mouse_log_filter.ino

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,13 @@ void setup() {
5757
Serial.begin(115200);
5858
usb_hid.begin();
5959

60-
#ifndef ARDUINO_ARCH_RP2040
60+
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
6161
// init host stack on controller (rhport) 1
6262
// For rp2040: this is called in core1's setup1()
6363
USBHost.begin(1);
64+
65+
// FeatherWing USB Host use MAX3421E's GPIO0 as VBUS enable (active high)
66+
USBHost.max3421_writeIOPINS1(0x01, false);
6467
#endif
6568

6669
//while ( !Serial ) delay(10); // wait for native usb

examples/DualRole/HID/hid_mouse_log_filter/usbh_helper.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@
3939

4040
#if defined(ARDUINO_METRO_ESP32S2)
4141
Adafruit_USBH_Host USBHost(&SPI, 15, 14);
42-
#else
43-
// Default use SPI and pin 10, 9 for CS and INT
42+
#elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2)
43+
Adafruit_USBH_Host USBHost(&SPI, 27, 33);
44+
#elif defined(ARDUINO_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
45+
// For Metro shape CS and INT are pin 10, 9 (host shield default)
4446
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
47+
#else
48+
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
49+
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
4550
#endif
4651
#else
4752
// Native USB Host such as rp2040

examples/DualRole/HID/hid_mouse_tremor_filter/hid_mouse_tremor_filter.ino

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,13 @@ void setup() {
6868
coeffs[0] = butterworth_lowpass(CUTOFF_FREQUENCY, SAMPLING_FREQUENCY);
6969
coeffs[1] = butterworth_lowpass(CUTOFF_FREQUENCY, SAMPLING_FREQUENCY);
7070

71-
#ifndef ARDUINO_ARCH_RP2040
71+
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
7272
// init host stack on controller (rhport) 1
7373
// For rp2040: this is called in core1's setup1()
7474
USBHost.begin(1);
75+
76+
// FeatherWing USB Host use MAX3421E's GPIO0 as VBUS enable (active high)
77+
USBHost.max3421_writeIOPINS1(0x01, false);
7578
#endif
7679

7780
//while ( !Serial ) delay(10); // wait for native usb

examples/DualRole/HID/hid_mouse_tremor_filter/usbh_helper.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@
3939

4040
#if defined(ARDUINO_METRO_ESP32S2)
4141
Adafruit_USBH_Host USBHost(&SPI, 15, 14);
42-
#else
43-
// Default use SPI and pin 10, 9 for CS and INT
42+
#elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2)
43+
Adafruit_USBH_Host USBHost(&SPI, 27, 33);
44+
#elif defined(ARDUINO_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
45+
// For Metro shape CS and INT are pin 10, 9 (host shield default)
4446
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
47+
#else
48+
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
49+
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
4550
#endif
4651
#else
4752
// Native USB Host such as rp2040

examples/DualRole/HID/hid_remapper/hid_remapper.ino

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,13 @@ void setup() {
5151
Serial.begin(115200);
5252
usb_hid.begin();
5353

54-
#ifndef ARDUINO_ARCH_RP2040
54+
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
5555
// init host stack on controller (rhport) 1
5656
// For rp2040: this is called in core1's setup1()
5757
USBHost.begin(1);
58+
59+
// FeatherWing USB Host use MAX3421E's GPIO0 as VBUS enable (active high)
60+
USBHost.max3421_writeIOPINS1(0x01, false);
5861
#endif
5962

6063
//while ( !Serial ) delay(10); // wait for native usb

examples/DualRole/HID/hid_remapper/usbh_helper.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@
3939

4040
#if defined(ARDUINO_METRO_ESP32S2)
4141
Adafruit_USBH_Host USBHost(&SPI, 15, 14);
42-
#else
43-
// Default use SPI and pin 10, 9 for CS and INT
42+
#elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2)
43+
Adafruit_USBH_Host USBHost(&SPI, 27, 33);
44+
#elif defined(ARDUINO_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
45+
// For Metro shape CS and INT are pin 10, 9 (host shield default)
4446
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
47+
#else
48+
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
49+
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
4550
#endif
4651
#else
4752
// Native USB Host such as rp2040

examples/DualRole/MassStorage/msc_data_logger/msc_data_logger.ino

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,13 @@ void setup() {
114114

115115
pinMode(LED_BUILTIN, OUTPUT);
116116

117-
#ifndef ARDUINO_ARCH_RP2040
117+
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
118118
// init host stack on controller (rhport) 1
119119
// For rp2040: this is called in core1's setup1()
120120
USBHost.begin(1);
121+
122+
// FeatherWing USB Host use MAX3421E's GPIO0 as VBUS enable (active high)
123+
USBHost.max3421_writeIOPINS1(0x01, false);
121124
#endif
122125

123126
#ifdef USE_FREERTOS

examples/DualRole/MassStorage/msc_data_logger/usbh_helper.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@
3939

4040
#if defined(ARDUINO_METRO_ESP32S2)
4141
Adafruit_USBH_Host USBHost(&SPI, 15, 14);
42-
#else
43-
// Default use SPI and pin 10, 9 for CS and INT
42+
#elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2)
43+
Adafruit_USBH_Host USBHost(&SPI, 27, 33);
44+
#elif defined(ARDUINO_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
45+
// For Metro shape CS and INT are pin 10, 9 (host shield default)
4446
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
47+
#else
48+
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
49+
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
4550
#endif
4651
#else
4752
// Native USB Host such as rp2040

examples/DualRole/MassStorage/msc_file_explorer/msc_file_explorer.ino

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,13 @@ bool is_mounted = false;
4444
void setup() {
4545
Serial.begin(115200);
4646

47-
#ifndef ARDUINO_ARCH_RP2040
47+
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
4848
// init host stack on controller (rhport) 1
4949
// For rp2040: this is called in core1's setup1()
5050
USBHost.begin(1);
51+
52+
// FeatherWing USB Host use MAX3421E's GPIO0 as VBUS enable (active high)
53+
USBHost.max3421_writeIOPINS1(0x01, false);
5154
#endif
5255

5356
// while ( !Serial ) delay(10); // wait for native usb

examples/DualRole/MassStorage/msc_file_explorer/usbh_helper.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@
3939

4040
#if defined(ARDUINO_METRO_ESP32S2)
4141
Adafruit_USBH_Host USBHost(&SPI, 15, 14);
42-
#else
43-
// Default use SPI and pin 10, 9 for CS and INT
42+
#elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2)
43+
Adafruit_USBH_Host USBHost(&SPI, 27, 33);
44+
#elif defined(ARDUINO_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
45+
// For Metro shape CS and INT are pin 10, 9 (host shield default)
4446
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
47+
#else
48+
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
49+
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
4550
#endif
4651
#else
4752
// Native USB Host such as rp2040

examples/DualRole/Simple/device_info/device_info.ino

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,13 @@ dev_info_t dev_info[CFG_TUH_DEVICE_MAX] = { 0 };
6868
void setup() {
6969
Serial.begin(115200);
7070

71-
#ifndef ARDUINO_ARCH_RP2040
71+
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
7272
// init host stack on controller (rhport) 1
7373
// For rp2040: this is called in core1's setup1()
7474
USBHost.begin(1);
75+
76+
// FeatherWing USB Host use MAX3421E's GPIO0 as VBUS enable (active high)
77+
USBHost.max3421_writeIOPINS1(0x01, false);
7578
#endif
7679

7780
// while ( !Serial ) delay(10); // wait for native usb
@@ -94,7 +97,6 @@ void loop() {
9497

9598
//------------- Core0 -------------//
9699
void loop() {
97-
98100
}
99101

100102
//------------- Core1 -------------//

examples/DualRole/Simple/device_info/usbh_helper.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@
3939

4040
#if defined(ARDUINO_METRO_ESP32S2)
4141
Adafruit_USBH_Host USBHost(&SPI, 15, 14);
42-
#else
43-
// Default use SPI and pin 10, 9 for CS and INT
42+
#elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32_V2)
43+
Adafruit_USBH_Host USBHost(&SPI, 27, 33);
44+
#elif defined(ARDUINO_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
45+
// For Metro shape CS and INT are pin 10, 9 (host shield default)
4446
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
47+
#else
48+
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
49+
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
4550
#endif
4651
#else
4752
// Native USB Host such as rp2040

src/arduino/Adafruit_USBH_Host.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#ifdef ARDUINO_ARCH_ESP32
2727
#include "arduino/ports/esp32/tusb_config_esp32.h"
2828
#include "driver/gpio.h"
29-
#define MSBFIRST SPI_MSBFIRST
29+
#include <Arduino.h>
3030
#endif
3131

3232
#include "tusb_option.h"
@@ -40,14 +40,23 @@ Adafruit_USBH_Host *Adafruit_USBH_Host::_instance = NULL;
4040

4141
Adafruit_USBH_Host::Adafruit_USBH_Host(void) {
4242
Adafruit_USBH_Host::_instance = this;
43-
43+
_rhport = 0;
4444
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
4545
_spi = NULL;
4646
_cs = _intr = _sck = _mosi = _miso = -1;
4747
#endif
4848
}
4949

5050
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
51+
52+
// API to read MAX3421's register. Implemented by TinyUSB
53+
extern "C" uint8_t tuh_max3421_reg_read(uint8_t rhport, uint8_t reg,
54+
bool in_isr);
55+
56+
// API to write MAX3421's register. Implemented by TinyUSB
57+
extern "C" bool tuh_max3421_reg_write(uint8_t rhport, uint8_t reg, uint8_t data,
58+
bool in_isr);
59+
5160
static void max3421_isr(void);
5261

5362
#if defined(ARDUINO_ARCH_ESP32)
@@ -57,6 +66,7 @@ static void max3421_intr_task(void *param);
5766

5867
Adafruit_USBH_Host::Adafruit_USBH_Host(SPIClass *spi, int8_t cs, int8_t intr) {
5968
Adafruit_USBH_Host::_instance = this;
69+
_rhport = 0;
6070
_spi = spi;
6171
_cs = cs;
6272
_intr = intr;
@@ -66,13 +76,24 @@ Adafruit_USBH_Host::Adafruit_USBH_Host(SPIClass *spi, int8_t cs, int8_t intr) {
6676
Adafruit_USBH_Host::Adafruit_USBH_Host(SPIClass *spi, int8_t sck, int8_t mosi,
6777
int8_t miso, int8_t cs, int8_t intr) {
6878
Adafruit_USBH_Host::_instance = this;
79+
_rhport = 0;
6980
_spi = spi;
7081
_cs = cs;
7182
_intr = intr;
7283
_sck = sck;
7384
_mosi = mosi;
7485
_miso = miso;
7586
}
87+
88+
uint8_t Adafruit_USBH_Host::max3421_readRegister(uint8_t reg, bool in_isr) {
89+
return tuh_max3421_reg_read(_rhport, reg, in_isr);
90+
}
91+
92+
bool Adafruit_USBH_Host::max3421_writeRegister(uint8_t reg, uint8_t data,
93+
bool in_isr) {
94+
return tuh_max3421_reg_write(_rhport, reg, data, in_isr);
95+
}
96+
7697
#endif
7798

7899
bool Adafruit_USBH_Host::configure(uint8_t rhport, uint32_t cfg_id,
@@ -102,22 +123,21 @@ bool Adafruit_USBH_Host::begin(uint8_t rhport) {
102123
#ifdef ARDUINO_ARCH_ESP32
103124
// ESP32 SPI assign pins when begin() of declaration as standard API
104125
_spi->begin(_sck, _miso, _mosi, -1);
105-
#else
106-
_spi->begin();
107-
#endif
108126

109-
#ifdef ARDUINO_ARCH_ESP32
110127
// Create an task for executing interrupt handler in thread mode
111128
max3421_intr_sem = xSemaphoreCreateBinary();
112129
xTaskCreateUniversal(max3421_intr_task, "max3421 intr", 2048, NULL, 5, NULL,
113130
ARDUINO_RUNNING_CORE);
131+
#else
132+
_spi->begin();
114133
#endif
115134

116135
// Interrupt pin
117136
pinMode(_intr, INPUT_PULLUP);
118137
attachInterrupt(_intr, max3421_isr, FALLING);
119138
#endif
120139

140+
_rhport = rhport;
121141
return tuh_init(rhport);
122142
}
123143

src/arduino/Adafruit_USBH_Host.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ class Adafruit_USBH_Host {
5656
Adafruit_USBH_Host(SPIClass *spi, int8_t cs, int8_t intr);
5757
Adafruit_USBH_Host(SPIClass *spi, int8_t sck, int8_t mosi, int8_t miso,
5858
int8_t cs, int8_t intr);
59+
60+
uint8_t max3421_readRegister(uint8_t reg, bool in_isr);
61+
bool max3421_writeRegister(uint8_t reg, uint8_t data, bool in_isr);
62+
bool max3421_writeIOPINS1(uint8_t data, bool in_isr) {
63+
enum { IOPINS1_ADDR = 20u << 3 }; // 0xA0
64+
return max3421_writeRegister(IOPINS1_ADDR, data, in_isr);
65+
}
66+
bool max3421_writeIOPINS2(uint8_t data, bool in_isr) {
67+
enum { IOPINS2_ADDR = 21u << 3 }; // 0xA8
68+
return max3421_writeRegister(IOPINS2_ADDR, data, in_isr);
69+
}
5970
#endif
6071

6172
public:
@@ -75,6 +86,8 @@ class Adafruit_USBH_Host {
7586
static Adafruit_USBH_Host *_instance;
7687

7788
private:
89+
uint8_t _rhport;
90+
7891
friend void tuh_max3421_spi_cs_api(uint8_t rhport, bool active);
7992
friend bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf,
8093
uint8_t *rx_buf, size_t xfer_bytes);

src/arduino/ports/esp32/tusb_config_esp32.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ extern "C" {
5757
#define CFG_TUH_LOG_LEVEL 2
5858
#endif
5959

60+
//--------------------------------------------------------------------
61+
// COMMON CONFIGURATION
62+
// Note: it is possible to use tinyusb + max3421e as host controller
63+
// with no OTG USB MCU such as eps32, c3 etc...
64+
//--------------------------------------------------------------------
65+
6066
// #ifndef CFG_TUSB_DEBUG
6167
// #define CFG_TUSB_DEBUG 0
6268
// #endif

src/tusb_config.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,17 @@
4242
// Note: when compiling core Arduino IDEs will include tusb_config.h in the BSP
4343
// sdk/include/arduino_tinyusb/include. While compiling .c file in this library this
4444
// file will be used instead. For consistency: include the one in BSP here as well
45-
#include "../../arduino_tinyusb/include/tusb_config.h"
45+
#include "sdkconfig.h"
46+
#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
47+
#include "../../arduino_tinyusb/include/tusb_config.h"
48+
#else
49+
#define CFG_TUSB_OS OPT_OS_FREERTOS
50+
// clang-format off
51+
#define CFG_TUSB_OS_INC_PATH freertos/
52+
// clang-format on
53+
54+
#include "arduino/ports/esp32/tusb_config_esp32.h"
55+
#endif
4656

4757
// Note: For platformio prioritize this file over the one in BSP in all cases
4858

0 commit comments

Comments
 (0)