@@ -197,11 +197,28 @@ 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;
203
205
204
- digitalWrite (Adafruit_USBH_Host::_instance->_cs , active ? LOW : HIGH);
206
+ if (active) {
207
+ // MAX3421e max clock is 26MHz
208
+ // Depending on mcu ports, it may need to be clipped down
209
+ #ifdef ARDUINO_ARCH_SAMD
210
+ // SAMD 21/51 can only work reliably at 12MHz
211
+ uint32_t const max_clock = 12000000ul ;
212
+ #else
213
+ uint32_t const max_clock = 26000000ul ;
214
+ #endif
215
+
216
+ host->_spi ->beginTransaction (SPISettings (max_clock, MSBFIRST, SPI_MODE0));
217
+ digitalWrite (Adafruit_USBH_Host::_instance->_cs , LOW);
218
+ } else {
219
+ host->_spi ->endTransaction ();
220
+ digitalWrite (Adafruit_USBH_Host::_instance->_cs , HIGH);
221
+ }
205
222
}
206
223
207
224
bool tuh_max3421_spi_xfer_api (uint8_t rhport, uint8_t const *tx_buf,
@@ -213,19 +230,6 @@ bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf,
213
230
}
214
231
Adafruit_USBH_Host *host = Adafruit_USBH_Host::_instance;
215
232
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);
228
-
229
233
#ifdef ARDUINO_ARCH_SAMD
230
234
// SAMD cannot use transfer(tx_buf, rx_buf, len) API since it default to use
231
235
// DMA. However, since this can be invoked within EIC_Handler (ISR) which may
@@ -248,7 +252,6 @@ bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf,
248
252
host->_spi ->transfer (tx_buf, rx_buf, xfer_bytes);
249
253
#endif
250
254
251
- host->_spi ->endTransaction ();
252
255
return true ;
253
256
}
254
257
0 commit comments