Skip to content

Commit 1708cff

Browse files
authored
Merge pull request #343 from adafruit/more-max3421-2
move SPI beginTransaction()/endTransaction() to tuh_max3421_spi_cs_api()
2 parents bf68b76 + 9d5e6a2 commit 1708cff

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

src/arduino/Adafruit_USBH_Host.cpp

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,29 @@ 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;
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

207225
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,
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

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)