From 03bfda3196e7afc6eb024547fdb5b4cab3b012a6 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 2 Nov 2023 15:18:55 +0700 Subject: [PATCH] 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 --- src/arduino/Adafruit_USBH_Host.cpp | 33 ++++++++++++----------- src/portable/analog/max3421/hcd_max3421.c | 1 - 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/arduino/Adafruit_USBH_Host.cpp b/src/arduino/Adafruit_USBH_Host.cpp index 366f53c3..fefd2b30 100644 --- a/src/arduino/Adafruit_USBH_Host.cpp +++ b/src/arduino/Adafruit_USBH_Host.cpp @@ -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, @@ -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 @@ -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; } diff --git a/src/portable/analog/max3421/hcd_max3421.c b/src/portable/analog/max3421/hcd_max3421.c index e35de5d9..f920c630 100644 --- a/src/portable/analog/max3421/hcd_max3421.c +++ b/src/portable/analog/max3421/hcd_max3421.c @@ -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));