Skip to content

Commit

Permalink
add max3421_readRegister() max3421_readRegister(), max3421_writeIOPIN…
Browse files Browse the repository at this point in the history
…S1(), max3421_writeIOPINS2() to usbhost with max3421

update examples to work with featherwing usb host max3421
  • Loading branch information
hathach committed Nov 21, 2023
1 parent 22f5dac commit f3e0ff7
Show file tree
Hide file tree
Showing 16 changed files with 100 additions and 24 deletions.
7 changes: 5 additions & 2 deletions examples/DualRole/CDC/serial_host_bridge/usbh_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions examples/DualRole/HID/hid_device_report/usbh_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions examples/DualRole/HID/hid_mouse_log_filter/usbh_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions examples/DualRole/HID/hid_mouse_tremor_filter/usbh_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion examples/DualRole/HID/hid_remapper/hid_remapper.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions examples/DualRole/HID/hid_remapper/usbh_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions examples/DualRole/MassStorage/msc_data_logger/usbh_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions examples/DualRole/MassStorage/msc_file_explorer/usbh_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions examples/DualRole/Simple/device_info/device_info.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -94,7 +97,6 @@ void loop() {

//------------- Core0 -------------//
void loop() {

}

//------------- Core1 -------------//
Expand Down
7 changes: 5 additions & 2 deletions examples/DualRole/Simple/device_info/usbh_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 23 additions & 1 deletion src/arduino/Adafruit_USBH_Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,23 @@ 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;
#endif
}

#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)
Expand All @@ -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;
Expand All @@ -66,13 +76,24 @@ 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;
_sck = sck;
_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,
Expand Down Expand Up @@ -118,6 +139,7 @@ bool Adafruit_USBH_Host::begin(uint8_t rhport) {
attachInterrupt(_intr, max3421_isr, FALLING);
#endif

_rhport = rhport;
return tuh_init(rhport);
}

Expand Down
13 changes: 13 additions & 0 deletions src/arduino/Adafruit_USBH_Host.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ class Adafruit_USBH_Host {
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:
Expand All @@ -75,6 +86,8 @@ class Adafruit_USBH_Host {
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);
Expand Down

0 comments on commit f3e0ff7

Please sign in to comment.