From 1b277fbef5406471e65d0b8ceea6a740c3dd3385 Mon Sep 17 00:00:00 2001 From: Fabrizio Monti <fabrizio.monti@skywarder.eu> Date: Mon, 24 Feb 2025 21:07:37 +0100 Subject: [PATCH] [DMA] Code refactoring. --- src/shared/drivers/dma/DMA.cpp | 22 ++++++------- src/shared/drivers/dma/DMA.h | 39 ++++++++--------------- src/shared/drivers/dma/DMADefs.h | 14 ++++++++ src/shared/utils/TimedPollingFlag.h | 4 --- src/tests/drivers/test-dma-mem-to-mem.cpp | 14 ++------ 5 files changed, 41 insertions(+), 52 deletions(-) diff --git a/src/shared/drivers/dma/DMA.cpp b/src/shared/drivers/dma/DMA.cpp index a38af66d3..ecd258978 100644 --- a/src/shared/drivers/dma/DMA.cpp +++ b/src/shared/drivers/dma/DMA.cpp @@ -304,9 +304,9 @@ bool DMADriver::tryChannel(DMADefs::DMAStreamId id) return streams.count(id) == 0; } -DMAStreamGuard DMADriver::acquireStreamBlocking( - DMADefs::DMAStreamId id, DMADefs::Channel channel, - const std::chrono::nanoseconds timeout) +DMAStreamGuard DMADriver::acquireStream(DMADefs::DMAStreamId id, + DMADefs::Channel channel, + std::chrono::nanoseconds timeout) { Lock<FastMutex> l(mutex); @@ -339,8 +339,8 @@ DMAStreamGuard DMADriver::acquireStreamBlocking( return DMAStreamGuard(&(streams.at(id))); } -DMAStreamGuard DMADriver::automaticAcquireStreamBlocking( - DMADefs::Peripherals peripheral, const std::chrono::nanoseconds timeout) +DMAStreamGuard DMADriver::acquireStreamForPeripheral( + DMADefs::Peripherals peripheral, std::chrono::nanoseconds timeout) { const auto availableStreams = DMADefs::mapPeripherals.equal_range(peripheral); @@ -405,14 +405,14 @@ DMADriver::DMADriver() ClockUtils::enablePeripheralClock(DMA2); // Reset interrupts flags by setting the clear bits to 1 - // TODO: Change this magic number - DMA1->HIFCR = 0x0f7d0f7d; // = 0b 0000 1111 0111 1101 0000 1111 0111 1101 - DMA1->LIFCR = 0x0f7d0f7d; - DMA2->HIFCR = 0x0f7d0f7d; - DMA2->LIFCR = 0x0f7d0f7d; + constexpr int resetValue = 0x0f7d0f7d; + DMA1->HIFCR = resetValue; + DMA1->LIFCR = resetValue; + DMA2->HIFCR = resetValue; + DMA2->LIFCR = resetValue; } -void DMAStream::setup(DMATransaction transaction) +void DMAStream::setup(DMATransaction& transaction) { currentSetup = transaction; diff --git a/src/shared/drivers/dma/DMA.h b/src/shared/drivers/dma/DMA.h index 63ede2588..1d78927b7 100644 --- a/src/shared/drivers/dma/DMA.h +++ b/src/shared/drivers/dma/DMA.h @@ -35,15 +35,6 @@ namespace Boardcore { -// TODO: remove and find a better solution for stm32f407 -#ifndef DMA_SxCR_MSIZE_Pos -#define DMA_SxCR_MSIZE_Pos (13U) -#endif -// TODO: remove and find a better solution for stm32f407 -#ifndef DMA_SxCR_PSIZE_Pos -#define DMA_SxCR_PSIZE_Pos (11U) -#endif - struct DMATransaction { enum class Direction : uint16_t @@ -105,29 +96,27 @@ public: * correct channel. * @param id The id of the stream to be acquired. * @param channel The channel used to initialize the stream. - * @param timeout The maximum time that will be waited when in blocking - * mode, 0 to disable the timeout and wait forever. - * @return The pointer to the allocated stream if successful, nullptr if the - * timeout expired. + * @param timeout The maximum time that will be waited, defaults to waiting + * forever. + * @return A stream guard that might be valid or not, depending on the + * outcome of the request. */ - DMAStreamGuard acquireStreamBlocking( + DMAStreamGuard acquireStream( DMADefs::DMAStreamId id, DMADefs::Channel channel, - const std::chrono::nanoseconds timeout); + std::chrono::nanoseconds timeout = std::chrono::nanoseconds::zero()); /** * @brief Try to acquire a stream that is connected to the specified * peripheral. * @param peripheral The wanted peripheral. - * @param timeout The maximum time that will be waited when in blocking - * mode, 0 to disable the timeout and wait forever. - * @return The pointer to the allocated stream if successful, nullptr if the - * timeout expired. - * - * TODO: change name + * @param timeout The maximum time that will be waited, defaults to waiting + * forever. + * @return A stream guard that might be valid or not, depending on the + * outcome of the request. */ - DMAStreamGuard automaticAcquireStreamBlocking( + DMAStreamGuard acquireStreamForPeripheral( DMADefs::Peripherals peripheral, - const std::chrono::nanoseconds timeout); + std::chrono::nanoseconds timeout = std::chrono::nanoseconds::zero()); void releaseStream(DMADefs::DMAStreamId id); @@ -153,7 +142,7 @@ public: /** * @brief Setup the stream with the given configuration. */ - void setup(DMATransaction transaction); + void setup(DMATransaction& transaction); /** * @brief Activate the stream. As soon as the stream is enabled, it @@ -447,7 +436,7 @@ public: inline bool isValid() { return pStream != nullptr; } private: - DMAStream* pStream = nullptr; + DMAStream* const pStream; }; } // namespace Boardcore diff --git a/src/shared/drivers/dma/DMADefs.h b/src/shared/drivers/dma/DMADefs.h index 88797e8ab..91426006b 100644 --- a/src/shared/drivers/dma/DMADefs.h +++ b/src/shared/drivers/dma/DMADefs.h @@ -30,6 +30,20 @@ namespace Boardcore { +// For some reason these defines are missing +// in the CMSIS STM32F4xx file +#ifdef STM32F407xx + +#ifndef DMA_SxCR_MSIZE_Pos +#define DMA_SxCR_MSIZE_Pos (13U) +#endif + +#ifndef DMA_SxCR_PSIZE_Pos +#define DMA_SxCR_PSIZE_Pos (11U) +#endif + +#endif // STM32F407xx + namespace DMADefs { diff --git a/src/shared/utils/TimedPollingFlag.h b/src/shared/utils/TimedPollingFlag.h index 4e2dd42d0..44f139767 100644 --- a/src/shared/utils/TimedPollingFlag.h +++ b/src/shared/utils/TimedPollingFlag.h @@ -40,12 +40,8 @@ inline bool timedPollingFlag(std::function<bool()> readFlag, uint64_t start = miosix::getTime(); while (miosix::getTime() - start < timeout_ns) - { if (readFlag()) - { return true; - } - } return false; } diff --git a/src/tests/drivers/test-dma-mem-to-mem.cpp b/src/tests/drivers/test-dma-mem-to-mem.cpp index 7807b8810..6eef0b9f2 100644 --- a/src/tests/drivers/test-dma-mem-to-mem.cpp +++ b/src/tests/drivers/test-dma-mem-to-mem.cpp @@ -24,8 +24,6 @@ #include <miosix.h> #include <util/util.h> -#include <chrono> - using namespace miosix; using namespace Boardcore; @@ -33,9 +31,8 @@ void printBuffer(uint8_t* buffer, size_t size); int main() { - DMAStreamGuard stream = - DMADriver::instance().automaticAcquireStreamBlocking( - DMADefs::Peripherals::PE_MEM_ONLY, std::chrono::seconds::zero()); + DMAStreamGuard stream = DMADriver::instance().acquireStreamForPeripheral( + DMADefs::Peripherals::PE_MEM_ONLY); if (!stream.isValid()) { @@ -70,9 +67,7 @@ int main() stream->setup(trn); stream->enable(); - auto begin = std::chrono::steady_clock::now(); stream->waitForTransferComplete(); - auto end = std::chrono::steady_clock::now(); printf("After:\n"); printf("Buffer 1:\n"); @@ -80,11 +75,6 @@ int main() printf("Buffer 2:\n"); printBuffer(buffer2, sizeof(buffer2)); - const auto elapsedTime = - std::chrono::duration_cast<std::chrono::microseconds>(end - begin) - .count(); - printf("Elapsed time: %lld [us]\n", elapsedTime); - return 0; } -- GitLab