add max3421_readRegister() max3421_readRegister(), max3421_writeIOPINS1(), max3421_writeIOPINS2() to usbhost with max3421

update examples to work with featherwing usb host max3421
This commit is contained in:
hathach 2023-11-21 13:28:44 +07:00
parent 22f5dacfde
commit f3e0ff74cc
16 changed files with 100 additions and 24 deletions

View file

@ -39,9 +39,12 @@
#if defined(ARDUINO_METRO_ESP32S2)
Adafruit_USBH_Host USBHost(&SPI, 15, 14);
#else
// Default use SPI and pin 10, 9 for CS and INT
#elif defined(ARDUINO_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
// For Metro shape CS and INT are pin 10, 9 (host shield default)
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
#else
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
#endif
#else
// Native USB Host such as rp2040

View file

@ -39,9 +39,12 @@
#if defined(ARDUINO_METRO_ESP32S2)
Adafruit_USBH_Host USBHost(&SPI, 15, 14);
#else
// Default use SPI and pin 10, 9 for CS and INT
#elif defined(ARDUINO_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
// For Metro shape CS and INT are pin 10, 9 (host shield default)
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
#else
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
#endif
#else
// Native USB Host such as rp2040

View file

@ -57,10 +57,13 @@ void setup() {
Serial.begin(115200);
usb_hid.begin();
#ifndef ARDUINO_ARCH_RP2040
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
// init host stack on controller (rhport) 1
// For rp2040: this is called in core1's setup1()
USBHost.begin(1);
// FeatherWing USB Host use MAX3421E's GPIO0 as VBUS enable (active high)
USBHost.max3421_writeIOPINS1(0x01, false);
#endif
//while ( !Serial ) delay(10); // wait for native usb

View file

@ -39,9 +39,12 @@
#if defined(ARDUINO_METRO_ESP32S2)
Adafruit_USBH_Host USBHost(&SPI, 15, 14);
#else
// Default use SPI and pin 10, 9 for CS and INT
#elif defined(ARDUINO_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
// For Metro shape CS and INT are pin 10, 9 (host shield default)
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
#else
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
#endif
#else
// Native USB Host such as rp2040

View file

@ -68,10 +68,13 @@ void setup() {
coeffs[0] = butterworth_lowpass(CUTOFF_FREQUENCY, SAMPLING_FREQUENCY);
coeffs[1] = butterworth_lowpass(CUTOFF_FREQUENCY, SAMPLING_FREQUENCY);
#ifndef ARDUINO_ARCH_RP2040
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
// init host stack on controller (rhport) 1
// For rp2040: this is called in core1's setup1()
USBHost.begin(1);
// FeatherWing USB Host use MAX3421E's GPIO0 as VBUS enable (active high)
USBHost.max3421_writeIOPINS1(0x01, false);
#endif
//while ( !Serial ) delay(10); // wait for native usb

View file

@ -39,9 +39,12 @@
#if defined(ARDUINO_METRO_ESP32S2)
Adafruit_USBH_Host USBHost(&SPI, 15, 14);
#else
// Default use SPI and pin 10, 9 for CS and INT
#elif defined(ARDUINO_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
// For Metro shape CS and INT are pin 10, 9 (host shield default)
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
#else
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
#endif
#else
// Native USB Host such as rp2040

View file

@ -51,10 +51,13 @@ void setup() {
Serial.begin(115200);
usb_hid.begin();
#ifndef ARDUINO_ARCH_RP2040
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
// init host stack on controller (rhport) 1
// For rp2040: this is called in core1's setup1()
USBHost.begin(1);
// FeatherWing USB Host use MAX3421E's GPIO0 as VBUS enable (active high)
USBHost.max3421_writeIOPINS1(0x01, false);
#endif
//while ( !Serial ) delay(10); // wait for native usb

View file

@ -39,9 +39,12 @@
#if defined(ARDUINO_METRO_ESP32S2)
Adafruit_USBH_Host USBHost(&SPI, 15, 14);
#else
// Default use SPI and pin 10, 9 for CS and INT
#elif defined(ARDUINO_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
// For Metro shape CS and INT are pin 10, 9 (host shield default)
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
#else
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
#endif
#else
// Native USB Host such as rp2040

View file

@ -114,10 +114,13 @@ void setup() {
pinMode(LED_BUILTIN, OUTPUT);
#ifndef ARDUINO_ARCH_RP2040
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
// init host stack on controller (rhport) 1
// For rp2040: this is called in core1's setup1()
USBHost.begin(1);
// FeatherWing USB Host use MAX3421E's GPIO0 as VBUS enable (active high)
USBHost.max3421_writeIOPINS1(0x01, false);
#endif
#ifdef USE_FREERTOS

View file

@ -39,9 +39,12 @@
#if defined(ARDUINO_METRO_ESP32S2)
Adafruit_USBH_Host USBHost(&SPI, 15, 14);
#else
// Default use SPI and pin 10, 9 for CS and INT
#elif defined(ARDUINO_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
// For Metro shape CS and INT are pin 10, 9 (host shield default)
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
#else
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
#endif
#else
// Native USB Host such as rp2040

View file

@ -44,10 +44,13 @@ bool is_mounted = false;
void setup() {
Serial.begin(115200);
#ifndef ARDUINO_ARCH_RP2040
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
// init host stack on controller (rhport) 1
// For rp2040: this is called in core1's setup1()
USBHost.begin(1);
// FeatherWing USB Host use MAX3421E's GPIO0 as VBUS enable (active high)
USBHost.max3421_writeIOPINS1(0x01, false);
#endif
// while ( !Serial ) delay(10); // wait for native usb

View file

@ -39,9 +39,12 @@
#if defined(ARDUINO_METRO_ESP32S2)
Adafruit_USBH_Host USBHost(&SPI, 15, 14);
#else
// Default use SPI and pin 10, 9 for CS and INT
#elif defined(ARDUINO_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
// For Metro shape CS and INT are pin 10, 9 (host shield default)
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
#else
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
#endif
#else
// Native USB Host such as rp2040

View file

@ -68,10 +68,13 @@ dev_info_t dev_info[CFG_TUH_DEVICE_MAX] = { 0 };
void setup() {
Serial.begin(115200);
#ifndef ARDUINO_ARCH_RP2040
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
// init host stack on controller (rhport) 1
// For rp2040: this is called in core1's setup1()
USBHost.begin(1);
// FeatherWing USB Host use MAX3421E's GPIO0 as VBUS enable (active high)
USBHost.max3421_writeIOPINS1(0x01, false);
#endif
// while ( !Serial ) delay(10); // wait for native usb
@ -94,7 +97,6 @@ void loop() {
//------------- Core0 -------------//
void loop() {
}
//------------- Core1 -------------//

View file

@ -39,9 +39,12 @@
#if defined(ARDUINO_METRO_ESP32S2)
Adafruit_USBH_Host USBHost(&SPI, 15, 14);
#else
// Default use SPI and pin 10, 9 for CS and INT
#elif defined(ARDUINO_METRO_ESP32S3) || defined(ADAFRUIT_METRO_M4_EXPRESS) || defined(ADAFRUIT_METRO_M0_EXPRESS)
// For Metro shape CS and INT are pin 10, 9 (host shield default)
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
#else
// default to FeatherWing USB Host MAX3421E: CS and INT are pin D11, D10
Adafruit_USBH_Host USBHost(&SPI, 11, 10);
#endif
#else
// Native USB Host such as rp2040

View file

@ -40,7 +40,7 @@ Adafruit_USBH_Host *Adafruit_USBH_Host::_instance = NULL;
Adafruit_USBH_Host::Adafruit_USBH_Host(void) {
Adafruit_USBH_Host::_instance = this;
_rhport = 0;
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
_spi = NULL;
_cs = _intr = _sck = _mosi = _miso = -1;
@ -48,6 +48,15 @@ Adafruit_USBH_Host::Adafruit_USBH_Host(void) {
}
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
// API to read MAX3421's register. Implemented by TinyUSB
extern "C" uint8_t tuh_max3421_reg_read(uint8_t rhport, uint8_t reg,
bool in_isr);
// API to write MAX3421's register. Implemented by TinyUSB
extern "C" bool tuh_max3421_reg_write(uint8_t rhport, uint8_t reg, uint8_t data,
bool in_isr);
static void max3421_isr(void);
#if defined(ARDUINO_ARCH_ESP32)
@ -57,6 +66,7 @@ static void max3421_intr_task(void *param);
Adafruit_USBH_Host::Adafruit_USBH_Host(SPIClass *spi, int8_t cs, int8_t intr) {
Adafruit_USBH_Host::_instance = this;
_rhport = 0;
_spi = spi;
_cs = cs;
_intr = intr;
@ -66,6 +76,7 @@ Adafruit_USBH_Host::Adafruit_USBH_Host(SPIClass *spi, int8_t cs, int8_t intr) {
Adafruit_USBH_Host::Adafruit_USBH_Host(SPIClass *spi, int8_t sck, int8_t mosi,
int8_t miso, int8_t cs, int8_t intr) {
Adafruit_USBH_Host::_instance = this;
_rhport = 0;
_spi = spi;
_cs = cs;
_intr = intr;
@ -73,6 +84,16 @@ Adafruit_USBH_Host::Adafruit_USBH_Host(SPIClass *spi, int8_t sck, int8_t mosi,
_mosi = mosi;
_miso = miso;
}
uint8_t Adafruit_USBH_Host::max3421_readRegister(uint8_t reg, bool in_isr) {
return tuh_max3421_reg_read(_rhport, reg, in_isr);
}
bool Adafruit_USBH_Host::max3421_writeRegister(uint8_t reg, uint8_t data,
bool in_isr) {
return tuh_max3421_reg_write(_rhport, reg, data, in_isr);
}
#endif
bool Adafruit_USBH_Host::configure(uint8_t rhport, uint32_t cfg_id,
@ -118,6 +139,7 @@ bool Adafruit_USBH_Host::begin(uint8_t rhport) {
attachInterrupt(_intr, max3421_isr, FALLING);
#endif
_rhport = rhport;
return tuh_init(rhport);
}

View file

@ -56,6 +56,17 @@ public:
Adafruit_USBH_Host(SPIClass *spi, int8_t cs, int8_t intr);
Adafruit_USBH_Host(SPIClass *spi, int8_t sck, int8_t mosi, int8_t miso,
int8_t cs, int8_t intr);
uint8_t max3421_readRegister(uint8_t reg, bool in_isr);
bool max3421_writeRegister(uint8_t reg, uint8_t data, bool in_isr);
bool max3421_writeIOPINS1(uint8_t data, bool in_isr) {
enum { IOPINS1_ADDR = 20u << 3 }; // 0xA0
return max3421_writeRegister(IOPINS1_ADDR, data, in_isr);
}
bool max3421_writeIOPINS2(uint8_t data, bool in_isr) {
enum { IOPINS2_ADDR = 21u << 3 }; // 0xA8
return max3421_writeRegister(IOPINS2_ADDR, data, in_isr);
}
#endif
public:
@ -75,6 +86,8 @@ public:
static Adafruit_USBH_Host *_instance;
private:
uint8_t _rhport;
friend void tuh_max3421_spi_cs_api(uint8_t rhport, bool active);
friend bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf,
uint8_t *rx_buf, size_t xfer_bytes);