35
35
#include " Adafruit_TinyUSB_API.h"
36
36
#include " Adafruit_USBH_Host.h"
37
37
38
- #if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
39
- static void max3421_isr (void );
40
- #endif
41
-
42
38
Adafruit_USBH_Host *Adafruit_USBH_Host::_instance = NULL ;
43
39
44
40
Adafruit_USBH_Host::Adafruit_USBH_Host (void ) {
45
41
Adafruit_USBH_Host::_instance = this ;
46
42
_spi = NULL ;
47
- _cs = _intr = -1 ;
43
+ _cs = _intr = _sck = _mosi = _miso = -1 ;
48
44
}
49
45
46
+ #if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
47
+ static void max3421_isr (void );
48
+
49
+ #if defined(ARDUINO_ARCH_ESP32)
50
+ SemaphoreHandle_t max3421_intr_sem;
51
+ static void max3421_intr_task (void *param);
52
+ #endif
53
+
50
54
Adafruit_USBH_Host::Adafruit_USBH_Host (SPIClass *spi, int8_t cs, int8_t intr) {
51
55
Adafruit_USBH_Host::_instance = this ;
52
56
_spi = spi;
53
57
_cs = cs;
54
58
_intr = intr;
59
+ _sck = _mosi = _miso = -1 ;
60
+ }
61
+
62
+ Adafruit_USBH_Host::Adafruit_USBH_Host (int8_t sck, int8_t mosi, int8_t miso,
63
+ int8_t cs, int8_t intr) {
64
+ Adafruit_USBH_Host::_instance = this ;
65
+ _spi = NULL ;
66
+ _cs = cs;
67
+ _intr = intr;
68
+ _sck = sck;
69
+ _mosi = mosi;
70
+ _miso = miso;
55
71
}
72
+ #endif
56
73
57
74
bool Adafruit_USBH_Host::configure (uint8_t rhport, uint32_t cfg_id,
58
75
const void *cfg_param) {
@@ -68,7 +85,7 @@ bool Adafruit_USBH_Host::configure_pio_usb(uint8_t rhport,
68
85
69
86
bool Adafruit_USBH_Host::begin (uint8_t rhport) {
70
87
#if defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
71
- if (_spi == NULL || _intr < 0 || _cs < 0 ) {
88
+ if (_intr < 0 || _cs < 0 ) {
72
89
return false ;
73
90
}
74
91
@@ -77,7 +94,25 @@ bool Adafruit_USBH_Host::begin(uint8_t rhport) {
77
94
digitalWrite (_cs, HIGH);
78
95
79
96
// SPI init
80
- SPI.begin ();
97
+ if (_spi) {
98
+ _spi->begin ();
99
+ } else {
100
+ #ifdef ARDUINO_ARCH_ESP32
101
+ // ESP32 SPI assign pins when init instead of declaration as standard API
102
+ _spi = new SPIClass (HSPI);
103
+ _spi->begin (_sck, _miso, _mosi, -1 );
104
+ #else
105
+ // Software SPI is not supported yet
106
+ return false ;
107
+ #endif
108
+ }
109
+
110
+ #ifdef ARDUINO_ARCH_ESP32
111
+ // Create an task for executing interrupt handler in thread mode
112
+ max3421_intr_sem = xSemaphoreCreateBinary ();
113
+ xTaskCreateUniversal (max3421_intr_task, " max3421 intr" , 2048 , NULL , 2 , NULL ,
114
+ ARDUINO_RUNNING_CORE);
115
+ #endif
81
116
82
117
// Interrupt pin
83
118
pinMode (_intr, INPUT_PULLUP);
@@ -124,23 +159,38 @@ TU_ATTR_WEAK void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance,
124
159
// --------------------------------------------------------------------+
125
160
#if CFG_TUH_ENABLED && defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421
126
161
127
- #if defined(ARDUINO_ARCH_ESP32) && !defined(PLATFORMIO)
162
+ #if defined(ARDUINO_ARCH_ESP32)
163
+
164
+ #ifdef PLATFORMIO
165
+ #define tuh_int_handler_esp32 tuh_int_handler
166
+ #else
128
167
extern " C" void hcd_int_handler_esp32 (uint8_t rhport, bool in_isr);
168
+ #define tuh_int_handler_esp32 hcd_int_handler_esp32
129
169
#endif
130
170
171
+ static void max3421_intr_task (void *param) {
172
+ (void )param;
173
+
174
+ while (1 ) {
175
+ xSemaphoreTake (max3421_intr_sem, portMAX_DELAY);
176
+ tuh_int_handler_esp32 (1 , false );
177
+ }
178
+ }
179
+
131
180
static void max3421_isr (void ) {
132
- // ESP32 out-of-sync
133
- #if defined(ARDUINO_ARCH_ESP32)
134
- #if defined(PLATFORMIO)
135
- tuh_int_handler (1 , false );
136
- #else
137
- hcd_int_handler_esp32 (1 , false );
138
- #endif
139
- #else
140
- tuh_int_handler (1 , true );
141
- #endif
181
+ BaseType_t xHigherPriorityTaskWoken = pdFALSE;
182
+ xSemaphoreGiveFromISR (max3421_intr_sem, &xHigherPriorityTaskWoken);
183
+ if (xHigherPriorityTaskWoken) {
184
+ portYIELD_FROM_ISR ();
185
+ }
142
186
}
143
187
188
+ #else
189
+
190
+ static void max3421_isr (void ) { tuh_int_handler (1 , true ); }
191
+
192
+ #endif // ESP32
193
+
144
194
extern " C" {
145
195
146
196
void tuh_max3421_spi_cs_api (uint8_t rhport, bool active) {
@@ -167,8 +217,8 @@ bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf,
167
217
// SAMD 21/51 can only work reliably at 12MHz
168
218
uint32_t const max_clock = 12000000ul ;
169
219
#else
170
- // uint32_t const max_clock = 26000000ul;
171
- uint32_t const max_clock = 4000000ul ;
220
+ uint32_t const max_clock = 26000000ul ;
221
+ // uint32_t const max_clock = 4000000ul;
172
222
#endif
173
223
174
224
SPISettings config (max_clock, MSBFIRST, SPI_MODE0);
@@ -190,6 +240,8 @@ bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf,
190
240
rx_buf[count] = data;
191
241
}
192
242
}
243
+ #elif defined(ARDUINO_ARCH_ESP32)
244
+ host->_spi ->transferBytes (tx_buf, rx_buf, xfer_bytes);
193
245
#else
194
246
host->_spi ->transfer (tx_buf, rx_buf, xfer_bytes);
195
247
#endif
0 commit comments