Skip to content

Commit 70605b7

Browse files
committed
starting max3421e support
1 parent 336f237 commit 70605b7

File tree

4 files changed

+155
-31
lines changed

4 files changed

+155
-31
lines changed

examples/DualRole/Simple/device_info/device_info.ino

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
*
4242
*/
4343

44+
#ifdef ARDUINO_ARCH_RP2040
4445
// pio-usb is required for rp2040 host
4546
#include "pio_usb.h"
46-
#include "Adafruit_TinyUSB.h"
4747

4848
// Pin D+ for host, D- = D+ + 1
4949
#ifndef PIN_USB_HOST_DP
@@ -58,12 +58,21 @@
5858
#ifndef PIN_5V_EN_STATE
5959
#define PIN_5V_EN_STATE 1
6060
#endif
61+
#endif
6162

62-
// Language ID: English
63-
#define LANGUAGE_ID 0x0409
63+
#include "Adafruit_TinyUSB.h"
64+
65+
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
66+
#include "SPI.h"
6467

65-
// USB Host object
68+
// USB Host object using MAX3421E: SPI, CS, INT
69+
Adafruit_USBH_Host USBHost(&SPI, 10, 9);
70+
#else
6671
Adafruit_USBH_Host USBHost;
72+
#endif
73+
74+
// Language ID: English
75+
#define LANGUAGE_ID 0x0409
6776

6877
typedef struct {
6978
tusb_desc_device_t desc_device;
@@ -76,27 +85,39 @@ typedef struct {
7685
// CFG_TUH_DEVICE_MAX is defined by tusb_config header
7786
dev_info_t dev_info[CFG_TUH_DEVICE_MAX] = { 0 };
7887

79-
//--------------------------------------------------------------------+
80-
// Setup and Loop on Core0
81-
//--------------------------------------------------------------------+
82-
83-
// the setup function runs once when you press reset or power the board
88+
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
8489
void setup() {
85-
Serial1.begin(115200);
86-
8790
Serial.begin(115200);
8891
//while ( !Serial ) delay(10); // wait for native usb
8992

9093
Serial.println("TinyUSB Dual Device Info Example");
94+
95+
// run host stack on controller (rhport) 1
96+
USBHost.begin(1);
9197
}
9298

9399
void loop() {
100+
USBHost.task();
94101
}
95102

103+
#elif defined(ARDUINO_ARCH_RP2040)
96104
//--------------------------------------------------------------------+
97-
// Setup and Loop on Core1
105+
// For RP2040 use both core0 for device stack, core1 for host stack
98106
//--------------------------------------------------------------------+
99107

108+
//------------- Core0 -------------//
109+
void setup() {
110+
Serial.begin(115200);
111+
//while ( !Serial ) delay(10); // wait for native usb
112+
113+
Serial.println("TinyUSB Dual Device Info Example");
114+
}
115+
116+
void loop() {
117+
118+
}
119+
120+
//------------- Core1 -------------//
100121
void setup1() {
101122
//while ( !Serial ) delay(10); // wait for native usb
102123
Serial.println("Core1 setup to run TinyUSB host with pio-usb");
@@ -142,10 +163,10 @@ void setup1() {
142163
USBHost.begin(1);
143164
}
144165

145-
void loop1()
146-
{
166+
void loop1() {
147167
USBHost.task();
148168
}
169+
#endif
149170

150171
//--------------------------------------------------------------------+
151172
// TinyUSB Host callbacks

src/arduino/Adafruit_USBH_Host.cpp

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,24 @@
2929
#include "Adafruit_TinyUSB_API.h"
3030
#include "Adafruit_USBH_Host.h"
3131

32-
Adafruit_USBH_Host::Adafruit_USBH_Host(void) {}
32+
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
33+
static void max3421_isr(void);
34+
#endif
35+
36+
Adafruit_USBH_Host *Adafruit_USBH_Host::_instance = NULL;
37+
38+
Adafruit_USBH_Host::Adafruit_USBH_Host(void) {
39+
Adafruit_USBH_Host::_instance = this;
40+
_spi = NULL;
41+
_cs = _intr = -1;
42+
}
43+
44+
Adafruit_USBH_Host::Adafruit_USBH_Host(SPIClass *spi, int8_t cs, int8_t intr) {
45+
Adafruit_USBH_Host::_instance = this;
46+
_spi = spi;
47+
_cs = cs;
48+
_intr = intr;
49+
}
3350

3451
bool Adafruit_USBH_Host::configure(uint8_t rhport, uint32_t cfg_id,
3552
const void *cfg_param) {
@@ -43,7 +60,26 @@ bool Adafruit_USBH_Host::configure_pio_usb(uint8_t rhport,
4360
}
4461
#endif
4562

46-
bool Adafruit_USBH_Host::begin(uint8_t rhport) { return tuh_init(rhport); }
63+
bool Adafruit_USBH_Host::begin(uint8_t rhport) {
64+
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
65+
if (_spi == NULL || _intr < 0 || _cs < 0) {
66+
return false;
67+
}
68+
69+
// CS pin
70+
pinMode(_cs, OUTPUT);
71+
digitalWrite(_cs, HIGH);
72+
73+
// SPI init
74+
SPI.begin();
75+
76+
// Interrupt pin
77+
pinMode(_intr, INPUT_PULLUP);
78+
attachInterrupt(_intr, max3421_isr, FALLING);
79+
#endif
80+
81+
return tuh_init(rhport);
82+
}
4783

4884
void Adafruit_USBH_Host::task(void) { tuh_task(); }
4985

@@ -76,4 +112,53 @@ TU_ATTR_WEAK void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance,
76112
(void)report;
77113
(void)len;
78114
}
115+
116+
//--------------------------------------------------------------------+
117+
// USB Host using MAX3421E
118+
//--------------------------------------------------------------------+
119+
#if CFG_TUH_ENABLED && defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
120+
121+
static void max3421_isr(void) { tuh_int_handler(1); }
122+
123+
extern "C" {
124+
125+
void tuh_max3421_spi_cs_api(uint8_t rhport, bool active) {
126+
(void)rhport;
127+
if (!Adafruit_USBH_Host::_instance) {
128+
return;
129+
}
130+
131+
digitalWrite(Adafruit_USBH_Host::_instance->_cs, active ? LOW : HIGH);
132+
}
133+
134+
bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf,
135+
size_t tx_len, uint8_t *rx_buf, size_t rx_len) {
136+
(void)rhport;
137+
if (!Adafruit_USBH_Host::_instance) {
138+
return false;
139+
}
140+
141+
return true;
142+
}
143+
144+
void tuh_max3421_int_api(uint8_t rhport, bool enabled) {
145+
(void)rhport;
146+
if (!Adafruit_USBH_Host::_instance) {
147+
return;
148+
}
149+
150+
#ifdef ARDUINO_ARCH_SAMD
151+
#ifdef __SAMD51
152+
#else
153+
if (enabled) {
154+
NVIC_EnableIRQ(EIC_IRQn);
155+
} else {
156+
NVIC_DisableIRQ(EIC_IRQn);
157+
}
158+
#endif
159+
#endif
160+
}
161+
}
162+
#endif
163+
79164
#endif

src/arduino/Adafruit_USBH_Host.h

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,33 @@
2626
#define ADAFRUIT_USBH_HOST_H_
2727

2828
#include "Adafruit_USBD_Interface.h"
29+
#include "SPI.h"
2930
#include "tusb.h"
3031

3132
#ifdef ARDUINO_ARCH_ESP32
3233
#include "esp32-hal-tinyusb.h"
3334
#endif
3435

36+
extern "C" {
37+
void tuh_max3421_spi_cs_api(uint8_t rhport, bool active);
38+
bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf,
39+
size_t tx_len, uint8_t *rx_buf, size_t rx_len);
40+
void tuh_max3421_int_api(uint8_t rhport, bool enabled);
41+
}
42+
3543
class Adafruit_USBH_Host {
3644
private:
45+
SPIClass *_spi;
46+
int8_t _cs;
47+
int8_t _intr;
48+
3749
public:
50+
// default constructor
3851
Adafruit_USBH_Host(void);
3952

53+
// constructor for using MAX3421E (host shield)
54+
Adafruit_USBH_Host(SPIClass *spi, int8_t cs, int8_t intr);
55+
4056
bool configure(uint8_t rhport, uint32_t cfg_id, const void *cfg_param);
4157

4258
#ifdef ARDUINO_ARCH_RP2040
@@ -46,21 +62,22 @@ class Adafruit_USBH_Host {
4662
bool begin(uint8_t rhport);
4763
void task(void);
4864

65+
//------------- internal usage -------------//
66+
static Adafruit_USBH_Host *_instance;
67+
4968
private:
5069
// uint16_t const *descrip`tor_string_cb(uint8_t index, uint16_t langid);
5170
//
5271
// friend uint8_t const *tud_descriptor_device_cb(void);
5372
// friend uint8_t const *tud_descriptor_configuration_cb(uint8_t index);
5473
// friend uint16_t const *tud_descriptor_string_cb(uint8_t index,
5574
// uint16_t langid);
56-
};
5775

58-
// extern Adafruit_USBH_Host TinyUSBHost;
59-
//
60-
//// USBHost has a high chance to conflict with other usb stack
61-
//// only define if supported BSP
62-
// #ifdef USE_TINYUSB
63-
// #define USBHost TinyUSBHost
64-
// #endif
76+
friend void tuh_max3421_spi_cs_api(uint8_t rhport, bool active);
77+
friend bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf,
78+
size_t tx_len, uint8_t *rx_buf,
79+
size_t rx_len);
80+
friend void tuh_max3421_int_api(uint8_t rhport, bool enabled);
81+
};
6582

66-
#endif /* ADAFRUIT_USBH_HOST_H_ */
83+
#endif

src/arduino/ports/samd/tusb_config_samd.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@ extern "C" {
3838
#define CFG_TUSB_MCU OPT_MCU_SAMD21
3939
#endif
4040

41-
#ifdef USE_TINYUSB
42-
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
43-
#else
44-
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_NONE
45-
#endif
46-
4741
#define CFG_TUSB_OS OPT_OS_NONE
4842

4943
#ifndef CFG_TUSB_DEBUG
@@ -53,6 +47,13 @@ extern "C" {
5347
#define CFG_TUSB_MEM_SECTION
5448
#define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4)
5549

50+
// Enable device stack
51+
#define CFG_TUD_ENABLED 1
52+
53+
// Enable host stack with MAX3421E (host shield)
54+
#define CFG_TUH_ENABLED 1
55+
#define CFG_TUH_MAX3421 1
56+
5657
//--------------------------------------------------------------------
5758
// DEVICE CONFIGURATION
5859
//--------------------------------------------------------------------

0 commit comments

Comments
 (0)