Skip to content

Commit

Permalink
call SPI beginTransaction()/endTransaction() in tuh_max3421_spi_cs_api()
Browse files Browse the repository at this point in the history
this is correct implementation to share SPI with other peripherals.
Therefore hcd_init() should not call this, only when start/end a
transfer
  • Loading branch information
hathach committed Nov 2, 2023
1 parent 2c80ef1 commit 03bfda3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
33 changes: 18 additions & 15 deletions src/arduino/Adafruit_USBH_Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,28 @@ extern "C" {

void tuh_max3421_spi_cs_api(uint8_t rhport, bool active) {
(void)rhport;

if (!Adafruit_USBH_Host::_instance) {
return;
}
Adafruit_USBH_Host *host = Adafruit_USBH_Host::_instance;

digitalWrite(Adafruit_USBH_Host::_instance->_cs, active ? LOW : HIGH);
if (active) {
// MAX3421e max clock is 26MHz
// Depending on mcu ports, it may need to be clipped down
#ifdef ARDUINO_ARCH_SAMD
// SAMD 21/51 can only work reliably at 12MHz
uint32_t const max_clock = 12000000ul;
#else
uint32_t const max_clock = 26000000ul;
#endif

host->_spi->beginTransaction(SPISettings(max_clock, MSBFIRST, SPI_MODE0));
digitalWrite(Adafruit_USBH_Host::_instance->_cs, LOW);
} else {
host->_spi->endTransaction();
digitalWrite(Adafruit_USBH_Host::_instance->_cs, HIGH);
}
}

bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf,
Expand All @@ -213,19 +230,6 @@ bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf,
}
Adafruit_USBH_Host *host = Adafruit_USBH_Host::_instance;

// MAX3421e max clock is 26MHz
// Depending on mcu ports, it may need to be clipped down
#ifdef ARDUINO_ARCH_SAMD
// SAMD 21/51 can only work reliably at 12MHz
uint32_t const max_clock = 12000000ul;
#else
uint32_t const max_clock = 26000000ul;
// uint32_t const max_clock = 4000000ul;
#endif

SPISettings config(max_clock, MSBFIRST, SPI_MODE0);
host->_spi->beginTransaction(config);

#ifdef ARDUINO_ARCH_SAMD
// SAMD cannot use transfer(tx_buf, rx_buf, len) API since it default to use
// DMA. However, since this can be invoked within EIC_Handler (ISR) which may
Expand All @@ -248,7 +252,6 @@ bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf,
host->_spi->transfer(tx_buf, rx_buf, xfer_bytes);
#endif

host->_spi->endTransaction();
return true;
}

Expand Down
1 change: 0 additions & 1 deletion src/portable/analog/max3421/hcd_max3421.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,6 @@ bool hcd_init(uint8_t rhport) {
(void) rhport;

tuh_max3421_int_api(rhport, false);
tuh_max3421_spi_cs_api(rhport, false);

TU_LOG2_INT(sizeof(max3421_ep_t));
TU_LOG2_INT(sizeof(max3421_data_t));
Expand Down

0 comments on commit 03bfda3

Please sign in to comment.