Skip to content

Commit 03bfda3

Browse files
committed
call SPI beginTransaction()/endTransaction() in tuh_max3421_spi_cs_api()
this is correct implementation to share SPI with other peripherals. Therefore hcd_init() should not call this, only when start/end a transfer
1 parent 2c80ef1 commit 03bfda3

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

src/arduino/Adafruit_USBH_Host.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,28 @@ extern "C" {
197197

198198
void 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;
203205

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+
}
205222
}
206223

207224
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,
213230
}
214231
Adafruit_USBH_Host *host = Adafruit_USBH_Host::_instance;
215232

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-
229233
#ifdef ARDUINO_ARCH_SAMD
230234
// SAMD cannot use transfer(tx_buf, rx_buf, len) API since it default to use
231235
// 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,
248252
host->_spi->transfer(tx_buf, rx_buf, xfer_bytes);
249253
#endif
250254

251-
host->_spi->endTransaction();
252255
return true;
253256
}
254257

src/portable/analog/max3421/hcd_max3421.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,6 @@ bool hcd_init(uint8_t rhport) {
422422
(void) rhport;
423423

424424
tuh_max3421_int_api(rhport, false);
425-
tuh_max3421_spi_cs_api(rhport, false);
426425

427426
TU_LOG2_INT(sizeof(max3421_ep_t));
428427
TU_LOG2_INT(sizeof(max3421_data_t));

0 commit comments

Comments
 (0)