From e28cf4323bf4eb9dd9cbdea04d40a09c0cce0478 Mon Sep 17 00:00:00 2001 From: Michael Keller Date: Mon, 25 Nov 2019 18:47:02 +1300 Subject: [PATCH] Fixed handling of SPI parameters for Unified Targets. (#9218) Fixed handling of SPI parameters for Unified Targets. --- src/main/drivers/bus_spi.c | 14 ++++---- src/main/drivers/bus_spi.h | 2 +- src/main/drivers/bus_spi_config.c | 2 +- src/main/drivers/bus_spi_hal.c | 13 ++------ src/main/drivers/bus_spi_impl.h | 2 +- src/main/drivers/bus_spi_ll.c | 15 +++------ src/main/drivers/bus_spi_stdperiph.c | 15 +++------ src/main/drivers/rx/rx_a7105.c | 1 - src/main/drivers/rx/rx_cyrf6936.c | 1 - src/main/fc/init.c | 50 ++++++++++++++++++++++++---- 10 files changed, 63 insertions(+), 52 deletions(-) diff --git a/src/main/drivers/bus_spi.c b/src/main/drivers/bus_spi.c index 5f869b37f0..1ddbb288a8 100644 --- a/src/main/drivers/bus_spi.c +++ b/src/main/drivers/bus_spi.c @@ -69,7 +69,7 @@ SPI_TypeDef *spiInstanceByDevice(SPIDevice device) return spiDevice[device].dev; } -bool spiInit(SPIDevice device) +bool spiInit(SPIDevice device, bool leadingEdge) { switch (device) { case SPIINVALID: @@ -77,7 +77,7 @@ bool spiInit(SPIDevice device) case SPIDEV_1: #ifdef USE_SPI_DEVICE_1 - spiInitDevice(device); + spiInitDevice(device, leadingEdge); return true; #else break; @@ -85,7 +85,7 @@ bool spiInit(SPIDevice device) case SPIDEV_2: #ifdef USE_SPI_DEVICE_2 - spiInitDevice(device); + spiInitDevice(device, leadingEdge); return true; #else break; @@ -93,7 +93,7 @@ bool spiInit(SPIDevice device) case SPIDEV_3: #if defined(USE_SPI_DEVICE_3) && !defined(STM32F1) - spiInitDevice(device); + spiInitDevice(device, leadingEdge); return true; #else break; @@ -101,7 +101,7 @@ bool spiInit(SPIDevice device) case SPIDEV_4: #if defined(USE_SPI_DEVICE_4) - spiInitDevice(device); + spiInitDevice(device, leadingEdge); return true; #else break; @@ -109,7 +109,7 @@ bool spiInit(SPIDevice device) case SPIDEV_5: #if defined(USE_SPI_DEVICE_5) - spiInitDevice(device); + spiInitDevice(device, leadingEdge); return true; #else break; @@ -117,7 +117,7 @@ bool spiInit(SPIDevice device) case SPIDEV_6: #if defined(USE_SPI_DEVICE_6) - spiInitDevice(device); + spiInitDevice(device, leadingEdge); return true; #else break; diff --git a/src/main/drivers/bus_spi.h b/src/main/drivers/bus_spi.h index d815cb3a05..fef3c961cd 100644 --- a/src/main/drivers/bus_spi.h +++ b/src/main/drivers/bus_spi.h @@ -122,7 +122,7 @@ void spiPreinitRegister(ioTag_t iotag, uint8_t iocfg, uint8_t init); void spiPreinitByIO(IO_t io); void spiPreinitByTag(ioTag_t tag); -bool spiInit(SPIDevice device); +bool spiInit(SPIDevice device, bool leadingEdge); void spiSetDivisor(SPI_TypeDef *instance, uint16_t divisor); uint8_t spiTransferByte(SPI_TypeDef *instance, uint8_t data); bool spiIsBusBusy(SPI_TypeDef *instance); diff --git a/src/main/drivers/bus_spi_config.c b/src/main/drivers/bus_spi_config.c index 05968c7dd6..72c9c16e3b 100644 --- a/src/main/drivers/bus_spi_config.c +++ b/src/main/drivers/bus_spi_config.c @@ -91,7 +91,7 @@ void spiPreinit(void) flashPreInit(flashConfig()); #endif -#if defined(USE_RX_SPI) && !defined(USE_RX_SOFTSPI) +#if defined(USE_RX_SPI) rxSpiDevicePreInit(rxSpiConfig()); #endif diff --git a/src/main/drivers/bus_spi_hal.c b/src/main/drivers/bus_spi_hal.c index c4594d48c1..8ba2ab3abe 100644 --- a/src/main/drivers/bus_spi_hal.c +++ b/src/main/drivers/bus_spi_hal.c @@ -36,7 +36,7 @@ #include "nvic.h" #include "rcc.h" -void spiInitDevice(SPIDevice device) +void spiInitDevice(SPIDevice device, bool leadingEdge) { spiDevice_t *spi = &(spiDevice[device]); @@ -44,16 +44,7 @@ void spiInitDevice(SPIDevice device) return; } -#ifdef SDCARD_SPI_INSTANCE - if (spi->dev == SDCARD_SPI_INSTANCE) { - spi->leadingEdge = true; - } -#endif -#ifdef RX_SPI_INSTANCE - if (spi->dev == RX_SPI_INSTANCE) { - spi->leadingEdge = true; - } -#endif + spi->leadingEdge = leadingEdge; // Enable SPI clock RCC_ClockCmd(spi->rcc, ENABLE); diff --git a/src/main/drivers/bus_spi_impl.h b/src/main/drivers/bus_spi_impl.h index a988d09974..a06de02db7 100644 --- a/src/main/drivers/bus_spi_impl.h +++ b/src/main/drivers/bus_spi_impl.h @@ -83,5 +83,5 @@ typedef struct SPIDevice_s { extern spiDevice_t spiDevice[SPIDEV_COUNT]; -void spiInitDevice(SPIDevice device); +void spiInitDevice(SPIDevice device, bool leadingEdge); uint32_t spiTimeoutUserCallback(SPI_TypeDef *instance); diff --git a/src/main/drivers/bus_spi_ll.c b/src/main/drivers/bus_spi_ll.c index 3c4698b397..6a22c7e2ba 100644 --- a/src/main/drivers/bus_spi_ll.c +++ b/src/main/drivers/bus_spi_ll.c @@ -85,7 +85,7 @@ static LL_SPI_InitTypeDef defaultInit = .CRCCalculation = SPI_CRCCALCULATION_DISABLE, }; -void spiInitDevice(SPIDevice device) +void spiInitDevice(SPIDevice device, bool leadingEdge) { spiDevice_t *spi = &(spiDevice[device]); @@ -94,16 +94,9 @@ void spiInitDevice(SPIDevice device) } #ifndef USE_SPI_TRANSACTION -#ifdef SDCARD_SPI_INSTANCE - if (spi->dev == SDCARD_SPI_INSTANCE) { - spi->leadingEdge = true; - } -#endif -#ifdef RX_SPI_INSTANCE - if (spi->dev == RX_SPI_INSTANCE) { - spi->leadingEdge = true; - } -#endif + spi->leadingEdge = leadingEdge; +#else + UNUSED(leadingEdge); #endif // Enable SPI clock diff --git a/src/main/drivers/bus_spi_stdperiph.c b/src/main/drivers/bus_spi_stdperiph.c index 76f636b16a..d95a378cce 100644 --- a/src/main/drivers/bus_spi_stdperiph.c +++ b/src/main/drivers/bus_spi_stdperiph.c @@ -44,7 +44,7 @@ static SPI_InitTypeDef defaultInit = { .SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8, }; -void spiInitDevice(SPIDevice device) +void spiInitDevice(SPIDevice device, bool leadingEdge) { spiDevice_t *spi = &(spiDevice[device]); @@ -53,16 +53,9 @@ void spiInitDevice(SPIDevice device) } #ifndef USE_SPI_TRANSACTION -#ifdef SDCARD_SPI_INSTANCE - if (spi->dev == SDCARD_SPI_INSTANCE) { - spi->leadingEdge = true; - } -#endif -#ifdef RX_SPI_INSTANCE - if (spi->dev == RX_SPI_INSTANCE) { - spi->leadingEdge = true; - } -#endif + spi->leadingEdge = leadingEdge; +#else + UNUSED(leadingEdge); #endif // Enable SPI clock diff --git a/src/main/drivers/rx/rx_a7105.c b/src/main/drivers/rx/rx_a7105.c index b67d850106..899edb3f42 100644 --- a/src/main/drivers/rx/rx_a7105.c +++ b/src/main/drivers/rx/rx_a7105.c @@ -54,7 +54,6 @@ void a7105extiHandler(extiCallbackRec_t* cb) void A7105Init(uint32_t id, IO_t extiPin, IO_t txEnPin) { - spiDeviceByInstance(RX_SPI_INSTANCE); rxIntIO = extiPin; /* config receiver IRQ pin */ IOInit(rxIntIO, OWNER_RX_SPI_EXTI, 0); EXTIHandlerInit(&a7105extiCallbackRec, a7105extiHandler); diff --git a/src/main/drivers/rx/rx_cyrf6936.c b/src/main/drivers/rx/rx_cyrf6936.c index 92ea0f9f3c..06abe318a4 100644 --- a/src/main/drivers/rx/rx_cyrf6936.c +++ b/src/main/drivers/rx/rx_cyrf6936.c @@ -71,7 +71,6 @@ bool cyrf6936RxFinished(uint32_t *timeStamp) bool cyrf6936Init(IO_t extiPin) { - spiDeviceByInstance(RX_SPI_INSTANCE); rxIntIO = extiPin; IOInit(rxIntIO, OWNER_RX_SPI_EXTI, 0); EXTIHandlerInit(&cyrf6936extiCallbackRec, cyrf6936ExtiHandler); diff --git a/src/main/fc/init.c b/src/main/fc/init.c index da76b7cbb3..246faa4c01 100644 --- a/src/main/fc/init.c +++ b/src/main/fc/init.c @@ -208,6 +208,41 @@ static IO_t busSwitchResetPin = IO_NONE; } #endif +bool requiresSpiLeadingEdge(SPIDevice device) +{ +#if defined(CONFIG_IN_SDCARD) || defined(CONFIG_IN_EXTERNAL_FLASH) +#if !defined(SDCARD_SPI_INSTANCE) && !defined(RX_SPI_INSTANCE) + UNUSED(device); +#endif +#if defined(SDCARD_SPI_INSTANCE) + if (device == spiDeviceByInstance(SDCARD_SPI_INSTANCE)) { + return true; + } +#endif +#if defined(RX_SPI_INSTANCE) + if (device == spiDeviceByInstance(RX_SPI_INSTANCE)) { + return true; + } +#endif +#else +#if !defined(USE_SDCARD) && !defined(USE_RX_SPI) + UNUSED(device); +#endif +#if defined(USE_SDCARD) + if (device == SPI_CFG_TO_DEV(sdcardConfig()->device)) { + return true; + } +#endif +#if defined(USE_RX_SPI) + if (device == SPI_CFG_TO_DEV(rxSpiConfig()->spibus)) { + return true; + } +#endif +#endif // CONFIG_IN_SDCARD || CONFIG_IN_EXTERNAL_FLASH + + return false; +} + static void configureSPIAndQuadSPI(void) { #ifdef USE_SPI @@ -220,22 +255,22 @@ static void configureSPIAndQuadSPI(void) spiPreinit(); #ifdef USE_SPI_DEVICE_1 - spiInit(SPIDEV_1); + spiInit(SPIDEV_1, requiresSpiLeadingEdge(SPIDEV_1)); #endif #ifdef USE_SPI_DEVICE_2 - spiInit(SPIDEV_2); + spiInit(SPIDEV_2, requiresSpiLeadingEdge(SPIDEV_2)); #endif #ifdef USE_SPI_DEVICE_3 - spiInit(SPIDEV_3); + spiInit(SPIDEV_3, requiresSpiLeadingEdge(SPIDEV_3)); #endif #ifdef USE_SPI_DEVICE_4 - spiInit(SPIDEV_4); + spiInit(SPIDEV_4, requiresSpiLeadingEdge(SPIDEV_4)); #endif #ifdef USE_SPI_DEVICE_5 - spiInit(SPIDEV_5); + spiInit(SPIDEV_5, requiresSpiLeadingEdge(SPIDEV_5)); #endif #ifdef USE_SPI_DEVICE_6 - spiInit(SPIDEV_6); + spiInit(SPIDEV_6, requiresSpiLeadingEdge(SPIDEV_6)); #endif #endif // USE_SPI @@ -248,12 +283,13 @@ static void configureSPIAndQuadSPI(void) #endif // USE_QUAD_SPI } +#ifdef USE_SDCARD void sdCardAndFSInit() { sdcard_init(sdcardConfig()); afatfs_init(); } - +#endif void init(void) {