@@ -197,11 +197,29 @@ extern "C" {
197
197
198
198
void tuh_max3421_spi_cs_api (uint8_t rhport, bool active) {
199
199
(void )rhport;
200
+
200
201
if (!Adafruit_USBH_Host::_instance) {
201
202
return ;
202
203
}
204
+ Adafruit_USBH_Host *host = Adafruit_USBH_Host::_instance;
205
+ SPIClass *spi = host->_spi ;
203
206
204
- digitalWrite (Adafruit_USBH_Host::_instance->_cs , active ? LOW : HIGH);
207
+ if (active) {
208
+ // MAX3421e max clock is 26MHz
209
+ // Depending on mcu ports, it may need to be clipped down
210
+ #ifdef ARDUINO_ARCH_SAMD
211
+ // SAMD 21/51 can only work reliably at 12MHz
212
+ uint32_t const max_clock = 12000000ul ;
213
+ #else
214
+ uint32_t const max_clock = 26000000ul ;
215
+ #endif
216
+
217
+ spi->beginTransaction (SPISettings (max_clock, MSBFIRST, SPI_MODE0));
218
+ digitalWrite (Adafruit_USBH_Host::_instance->_cs , LOW);
219
+ } else {
220
+ spi->endTransaction ();
221
+ digitalWrite (Adafruit_USBH_Host::_instance->_cs , HIGH);
222
+ }
205
223
}
206
224
207
225
bool tuh_max3421_spi_xfer_api (uint8_t rhport, uint8_t const *tx_buf,
@@ -212,19 +230,7 @@ bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf,
212
230
return false ;
213
231
}
214
232
Adafruit_USBH_Host *host = Adafruit_USBH_Host::_instance;
215
-
216
- // MAX3421e max clock is 26MHz
217
- // Depending on mcu ports, it may need to be clipped down
218
- #ifdef ARDUINO_ARCH_SAMD
219
- // SAMD 21/51 can only work reliably at 12MHz
220
- uint32_t const max_clock = 12000000ul ;
221
- #else
222
- uint32_t const max_clock = 26000000ul ;
223
- // uint32_t const max_clock = 4000000ul;
224
- #endif
225
-
226
- SPISettings config (max_clock, MSBFIRST, SPI_MODE0);
227
- host->_spi ->beginTransaction (config);
233
+ SPIClass *spi = host->_spi ;
228
234
229
235
#ifdef ARDUINO_ARCH_SAMD
230
236
// SAMD cannot use transfer(tx_buf, rx_buf, len) API since it default to use
@@ -236,19 +242,18 @@ bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf,
236
242
if (tx_buf) {
237
243
data = tx_buf[count];
238
244
}
239
- data = host-> _spi ->transfer (data);
245
+ data = spi ->transfer (data);
240
246
241
247
if (rx_buf) {
242
248
rx_buf[count] = data;
243
249
}
244
250
}
245
251
#elif defined(ARDUINO_ARCH_ESP32)
246
- host-> _spi ->transferBytes (tx_buf, rx_buf, xfer_bytes);
252
+ spi ->transferBytes (tx_buf, rx_buf, xfer_bytes);
247
253
#else
248
- host-> _spi ->transfer (tx_buf, rx_buf, xfer_bytes);
254
+ spi ->transfer (tx_buf, rx_buf, xfer_bytes);
249
255
#endif
250
256
251
- host->_spi ->endTransaction ();
252
257
return true ;
253
258
}
254
259
0 commit comments