From 3af6dac99dd07673bd40e5795e48eb10dea37599 Mon Sep 17 00:00:00 2001 From: Sergey Pluzhnikov Date: Mon, 26 Jun 2023 12:59:14 +0200 Subject: [PATCH] STM32: SPI with DMA - more reliable detection of transfer completed --- .../platform/spi/stm32/spi_master_dma.hpp.in | 4 +-- .../spi/stm32/spi_master_dma_impl.hpp.in | 28 ++++++++----------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/modm/platform/spi/stm32/spi_master_dma.hpp.in b/src/modm/platform/spi/stm32/spi_master_dma.hpp.in index 1f53b2eb1c..e5d53a61d1 100644 --- a/src/modm/platform/spi/stm32/spi_master_dma.hpp.in +++ b/src/modm/platform/spi/stm32/spi_master_dma.hpp.in @@ -83,8 +83,8 @@ private: handleDmaTransmitComplete(); static inline bool dmaError { false }; - static inline bool dmaTransmitComplete { false }; - static inline bool dmaReceiveComplete { false }; + static inline bool dmaTransmitComplete { true }; + static inline bool dmaReceiveComplete { true }; // needed for transfers where no RX or TX buffers are given static inline uint8_t dmaDummy { 0 }; diff --git a/src/modm/platform/spi/stm32/spi_master_dma_impl.hpp.in b/src/modm/platform/spi/stm32/spi_master_dma_impl.hpp.in index da2587a585..3c0e949fbe 100644 --- a/src/modm/platform/spi/stm32/spi_master_dma_impl.hpp.in +++ b/src/modm/platform/spi/stm32/spi_master_dma_impl.hpp.in @@ -122,6 +122,9 @@ modm::platform::SpiMaster{{ id }}_Dma::transfer( if (tx) { Dma::TxChannel::setMemoryAddress(uint32_t(tx)); Dma::TxChannel::setMemoryIncrementMode(true); + Dma::TxChannel::setDataLength(length); + dmaTransmitComplete = false; + Dma::TxChannel::start(); } else { Dma::TxChannel::setMemoryAddress(uint32_t(&dmaDummy)); Dma::TxChannel::setMemoryIncrementMode(false); @@ -129,19 +132,14 @@ modm::platform::SpiMaster{{ id }}_Dma::transfer( if (rx) { Dma::RxChannel::setMemoryAddress(uint32_t(rx)); Dma::RxChannel::setMemoryIncrementMode(true); + Dma::RxChannel::setDataLength(length); + dmaReceiveComplete = false; + Dma::RxChannel::start(); } else { Dma::RxChannel::setMemoryAddress(uint32_t(&dmaDummy)); Dma::RxChannel::setMemoryIncrementMode(false); } - Dma::RxChannel::setDataLength(length); - dmaReceiveComplete = false; - Dma::RxChannel::start(); - - Dma::TxChannel::setDataLength(length); - dmaTransmitComplete = false; - Dma::TxChannel::start(); - while (true) { if (dmaError) break; @@ -180,6 +178,9 @@ modm::platform::SpiMaster{{ id }}_Dma::transfer( if (tx) { Dma::TxChannel::setMemoryAddress(uint32_t(tx)); Dma::TxChannel::setMemoryIncrementMode(true); + Dma::TxChannel::setDataLength(length); + dmaTransmitComplete = false; + Dma::TxChannel::start(); } else { Dma::TxChannel::setMemoryAddress(uint32_t(&dmaDummy)); Dma::TxChannel::setMemoryIncrementMode(false); @@ -187,19 +188,14 @@ modm::platform::SpiMaster{{ id }}_Dma::transfer( if (rx) { Dma::RxChannel::setMemoryAddress(uint32_t(rx)); Dma::RxChannel::setMemoryIncrementMode(true); + Dma::RxChannel::setDataLength(length); + dmaReceiveComplete = false; + Dma::RxChannel::start(); } else { Dma::RxChannel::setMemoryAddress(uint32_t(&dmaDummy)); Dma::RxChannel::setMemoryIncrementMode(false); } - Dma::RxChannel::setDataLength(length); - dmaReceiveComplete = false; - Dma::RxChannel::start(); - - Dma::TxChannel::setDataLength(length); - dmaTransmitComplete = false; - Dma::TxChannel::start(); - [[fallthrough]]; default: