Skip to content

Commit f3e0ff7

Browse files
committed
add max3421_readRegister() max3421_readRegister(), max3421_writeIOPINS1(), max3421_writeIOPINS2() to usbhost with max3421
update examples to work with featherwing usb host max3421
1 parent 22f5dac commit f3e0ff7

File tree

16 files changed

+100
-24
lines changed

16 files changed

+100
-24
lines changed

examples/DualRole/CDC/serial_host_bridge/usbh_helper.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@
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_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
43+
// For Metro shape CS and INT are pin 10, 9 (host shield default)
4444
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
45+
#else
46+
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
47+
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
4548
#endif
4649
#else
4750
// Native USB Host such as rp2040

examples/DualRole/HID/hid_device_report/usbh_helper.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@
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_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
43+
// For Metro shape CS and INT are pin 10, 9 (host shield default)
4444
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
45+
#else
46+
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
47+
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
4548
#endif
4649
#else
4750
// 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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@
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_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
43+
// For Metro shape CS and INT are pin 10, 9 (host shield default)
4444
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
45+
#else
46+
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
47+
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
4548
#endif
4649
#else
4750
// 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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@
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_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
43+
// For Metro shape CS and INT are pin 10, 9 (host shield default)
4444
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
45+
#else
46+
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
47+
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
4548
#endif
4649
#else
4750
// 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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@
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_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
43+
// For Metro shape CS and INT are pin 10, 9 (host shield default)
4444
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
45+
#else
46+
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
47+
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
4548
#endif
4649
#else
4750
// 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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@
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_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
43+
// For Metro shape CS and INT are pin 10, 9 (host shield default)
4444
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
45+
#else
46+
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
47+
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
4548
#endif
4649
#else
4750
// 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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@
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_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
43+
// For Metro shape CS and INT are pin 10, 9 (host shield default)
4444
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
45+
#else
46+
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
47+
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
4548
#endif
4649
#else
4750
// 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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@
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_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
43+
// For Metro shape CS and INT are pin 10, 9 (host shield default)
4444
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
45+
#else
46+
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
47+
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
4548
#endif
4649
#else
4750
// Native USB Host such as rp2040

src/arduino/Adafruit_USBH_Host.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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,
@@ -118,6 +139,7 @@ bool Adafruit_USBH_Host::begin(uint8_t rhport) {
118139
attachInterrupt(_intr, max3421_isr, FALLING);
119140
#endif
120141

142+
_rhport = rhport;
121143
return tuh_init(rhport);
122144
}
123145

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);

0 commit comments

Comments
 (0)