From 7ea8dff8db4891c84bb0ef23c47ecd1c54a6a493 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Thu, 19 Mar 2020 16:06:38 +0100 Subject: [PATCH] spi: do not deselect other chip-selects And make sure on reset & init everything is deselected. Reduces CPU load on a pixhawk cube by almost 1%. --- boards/nxp/fmuk66-v3/src/spi.cpp | 15 ++------------- boards/nxp/fmurt1062-v1/src/spi.cpp | 15 ++------------- .../nuttx/src/px4/stm/stm32_common/spi/spi.cpp | 15 ++------------- 3 files changed, 6 insertions(+), 39 deletions(-) diff --git a/boards/nxp/fmuk66-v3/src/spi.cpp b/boards/nxp/fmuk66-v3/src/spi.cpp index bb4594a996..5a53664ce8 100644 --- a/boards/nxp/fmuk66-v3/src/spi.cpp +++ b/boards/nxp/fmuk66-v3/src/spi.cpp @@ -300,27 +300,16 @@ __EXPORT int fmuk66_spi_bus_initialize(void) static inline void kinetis_spixselect(const px4_spi_bus_t *bus, struct spi_dev_s *dev, uint32_t devid, bool selected) { - int matched_dev_idx = -1; - for (int i = 0; i < SPI_BUS_MAX_DEVICES; ++i) { if (bus->devices[i].cs_gpio == 0) { break; } if (devid == bus->devices[i].devid) { - matched_dev_idx = i; - - } else { - // Making sure the other peripherals are not selected - kinetis_gpiowrite(bus->devices[i].cs_gpio, 1); + // SPI select is active low, so write !selected to select the device + kinetis_gpiowrite(bus->devices[i].cs_gpio, !selected); } } - - // different devices might use the same CS, so make sure to configure the one we want last - if (matched_dev_idx != -1) { - // SPI select is active low, so write !selected to select the device - kinetis_gpiowrite(bus->devices[matched_dev_idx].cs_gpio, !selected); - } } void kinetis_spi0select(FAR struct spi_dev_s *dev, uint32_t devid, bool selected) diff --git a/boards/nxp/fmurt1062-v1/src/spi.cpp b/boards/nxp/fmurt1062-v1/src/spi.cpp index 3914a584d7..ae85253f52 100644 --- a/boards/nxp/fmurt1062-v1/src/spi.cpp +++ b/boards/nxp/fmurt1062-v1/src/spi.cpp @@ -241,27 +241,16 @@ __EXPORT int imxrt1062_spi_bus_initialize(void) static inline void imxrt_spixselect(const px4_spi_bus_t *bus, struct spi_dev_s *dev, uint32_t devid, bool selected) { - int matched_dev_idx = -1; - for (int i = 0; i < SPI_BUS_MAX_DEVICES; ++i) { if (bus->devices[i].cs_gpio == 0) { break; } if (devid == bus->devices[i].devid) { - matched_dev_idx = i; - - } else { - // Making sure the other peripherals are not selected - imxrt_gpio_write(bus->devices[i].cs_gpio, 1); + // SPI select is active low, so write !selected to select the device + imxrt_gpio_write(bus->devices[i].cs_gpio, !selected); } } - - // different devices might use the same CS, so make sure to configure the one we want last - if (matched_dev_idx != -1) { - // SPI select is active low, so write !selected to select the device - imxrt_gpio_write(bus->devices[matched_dev_idx].cs_gpio, !selected); - } } #if defined(CONFIG_IMXRT_LPSPI1) diff --git a/platforms/nuttx/src/px4/stm/stm32_common/spi/spi.cpp b/platforms/nuttx/src/px4/stm/stm32_common/spi/spi.cpp index 6e512ab5ec..fbe37133f8 100644 --- a/platforms/nuttx/src/px4/stm/stm32_common/spi/spi.cpp +++ b/platforms/nuttx/src/px4/stm/stm32_common/spi/spi.cpp @@ -184,27 +184,16 @@ __EXPORT void stm32_spiinitialize() static inline void stm32_spixselect(const px4_spi_bus_t *bus, struct spi_dev_s *dev, uint32_t devid, bool selected) { - int matched_dev_idx = -1; - for (int i = 0; i < SPI_BUS_MAX_DEVICES; ++i) { if (bus->devices[i].cs_gpio == 0) { break; } if (devid == bus->devices[i].devid) { - matched_dev_idx = i; - - } else { - // Making sure the other peripherals are not selected - stm32_gpiowrite(bus->devices[i].cs_gpio, 1); + // SPI select is active low, so write !selected to select the device + stm32_gpiowrite(bus->devices[i].cs_gpio, !selected); } } - - // different devices might use the same CS, so make sure to configure the one we want last - if (matched_dev_idx != -1) { - // SPI select is active low, so write !selected to select the device - stm32_gpiowrite(bus->devices[matched_dev_idx].cs_gpio, !selected); - } }