@@ -197,11 +197,29 @@ extern "C" {
197197
198198void tuh_max3421_spi_cs_api (uint8_t rhport, bool active) {
199199 (void )rhport;
200+
200201 if (!Adafruit_USBH_Host::_instance) {
201202 return ;
202203 }
204+ Adafruit_USBH_Host *host = Adafruit_USBH_Host::_instance;
205+ SPIClass *spi = host->_spi ;
203206
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+ }
205223}
206224
207225bool 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,
212230 return false ;
213231 }
214232 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 ;
228234
229235#ifdef ARDUINO_ARCH_SAMD
230236 // 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,
236242 if (tx_buf) {
237243 data = tx_buf[count];
238244 }
239- data = host-> _spi ->transfer (data);
245+ data = spi ->transfer (data);
240246
241247 if (rx_buf) {
242248 rx_buf[count] = data;
243249 }
244250 }
245251#elif defined(ARDUINO_ARCH_ESP32)
246- host-> _spi ->transferBytes (tx_buf, rx_buf, xfer_bytes);
252+ spi ->transferBytes (tx_buf, rx_buf, xfer_bytes);
247253#else
248- host-> _spi ->transfer (tx_buf, rx_buf, xfer_bytes);
254+ spi ->transfer (tx_buf, rx_buf, xfer_bytes);
249255#endif
250256
251- host->_spi ->endTransaction ();
252257 return true ;
253258}
254259
0 commit comments