From 1f0216d9d141917b7c612b14f7948f5fa09aa87f Mon Sep 17 00:00:00 2001 From: Fabrizio Monti <fabrizio.monti@skywarder.eu> Date: Tue, 4 Mar 2025 08:45:08 +0100 Subject: [PATCH] [DMA] Added documentation and small code refactoring (removed unnecessary member variable and made DMAStreamId constant inside DMAStream). --- src/shared/drivers/dma/DMA.cpp | 5 ++--- src/shared/drivers/dma/DMA.h | 39 ++++++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/shared/drivers/dma/DMA.cpp b/src/shared/drivers/dma/DMA.cpp index ecd258978..c23edfe84 100644 --- a/src/shared/drivers/dma/DMA.cpp +++ b/src/shared/drivers/dma/DMA.cpp @@ -507,6 +507,8 @@ void DMAStream::setup(DMATransaction& transaction) enableInterrupt = true; } + // Select the interrupt number + IRQn_Type irqNumber = DMADefs::irqNumberMapping[static_cast<uint8_t>(id)]; if (enableInterrupt) { NVIC_SetPriority(irqNumber, 8); @@ -674,9 +676,6 @@ DMAStream::DMAStream(DMADefs::DMAStreamId id, DMADefs::Channel channel) // Refer to reference manual for the register bits structure int offset = static_cast<uint8_t>(id) % 4; IFindex = (offset % 2) * 6 + (offset / 2) * 16; - - // Select the interrupt - irqNumber = DMADefs::irqNumberMapping[static_cast<uint8_t>(id)]; } DMAStream* DMAStreamGuard::operator->() diff --git a/src/shared/drivers/dma/DMA.h b/src/shared/drivers/dma/DMA.h index 1d78927b7..4234aa342 100644 --- a/src/shared/drivers/dma/DMA.h +++ b/src/shared/drivers/dma/DMA.h @@ -89,6 +89,9 @@ public: static DMADriver& instance(); + /** + * @return True if the stream is not already in use. + */ bool tryChannel(DMADefs::DMAStreamId id); /** @@ -123,6 +126,9 @@ public: private: DMADriver(); + /** + * @brief Wakeup the sleeping thread associated to the stream. + */ void IRQwakeupThread(DMAStream& stream); miosix::FastMutex mutex; @@ -146,18 +152,44 @@ public: /** * @brief Activate the stream. As soon as the stream is enabled, it - * can serve any DMA request from the peripheral connected to the stream. + * serves any DMA request from/to the peripheral connected to the stream. */ void enable(); void disable(); + /** + * @brief Wait for the half transfer complete signal. + * The caller waits for the corresponding interrupt, if enabled. + * Otherwise it goes to polling mode on the flag. + */ void waitForHalfTransfer(); + /** + * @brief Wait for the transfer complete signal. + * The caller waits for the corresponding interrupt, if enabled. + * Otherwise it goes to polling mode on the flag. + */ void waitForTransferComplete(); + /** + * @brief Wait for the half transfer complete signal. + * The caller waits for the corresponding interrupt, if enabled. + * Otherwise it goes to polling mode on the flag. + * @param timeout_ns The maximum time that will be waited. + * @return True if the event is reached, false if the + * timeout expired. + */ bool timedWaitForHalfTransfer(std::chrono::nanoseconds timeout_ns); + /** + * @brief Wait for the transfer complete signal. + * The caller waits for the corresponding interrupt, if enabled. + * Otherwise it goes to polling mode on the flag. + * @param timeout_ns The maximum time that will be waited. + * @return True if the event is reached, false if the + * timeout expired. + */ bool timedWaitForTransferComplete(std::chrono::nanoseconds timeout_ns); void setHalfTransferCallback(std::function<void()> callback); @@ -278,7 +310,7 @@ private: /** * @brief Used to determine if the user thread is - * waiting to be awaked by an interrupt. + * waiting to be awakened by an interrupt. */ miosix::Thread* waitingThread = nullptr; @@ -294,9 +326,8 @@ private: std::function<void()> transferCompleteCallback; std::function<void()> errorCallback; - DMADefs::DMAStreamId id; + const DMADefs::DMAStreamId id; DMADefs::Channel currentChannel; - IRQn_Type irqNumber; DMA_Stream_TypeDef* registers; volatile uint32_t* ISR; ///< Interrupt status register -- GitLab