From 04937f36930dfd51fe4c00c62dc036e99f203d4b Mon Sep 17 00:00:00 2001 From: Fabrizio Monti <fabrizio.monti@skywarder.eu> Date: Fri, 7 Mar 2025 10:23:04 +0100 Subject: [PATCH] [DMA] Added safety check to setNumberOfDataItems(). --- src/shared/drivers/dma/DMA.cpp | 11 +++++++++-- src/shared/drivers/dma/DMA.h | 6 ++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/shared/drivers/dma/DMA.cpp b/src/shared/drivers/dma/DMA.cpp index c23edfe84..6a1903e6c 100644 --- a/src/shared/drivers/dma/DMA.cpp +++ b/src/shared/drivers/dma/DMA.cpp @@ -613,11 +613,18 @@ void DMAStream::readFlags() directModeErrorFlag = flags & DMA_LISR_DMEIF0; } -void DMAStream::setNumberOfDataItems(const uint16_t nBytes) +bool DMAStream::setNumberOfDataItems(const uint16_t nBytes) { - // TODO: assert that the stream is disabled while doing it + // Verify that the stream is disabled while doing it + if (registers->CR & DMA_SxCR_EN != 0) + { + // Cannot proceed + return false; + } + currentSetup.numberOfDataItems = nBytes; registers->NDTR = nBytes; + return true; } void DMAStream::setChannel(const DMADefs::Channel channel) diff --git a/src/shared/drivers/dma/DMA.h b/src/shared/drivers/dma/DMA.h index b1276864a..cabb4de6f 100644 --- a/src/shared/drivers/dma/DMA.h +++ b/src/shared/drivers/dma/DMA.h @@ -213,9 +213,11 @@ public: /** * @brief Set the number of bytes to be exchanged during a * dma transaction. Useful in case you don't want to change - * the entire configuration. + * the entire configuration. Use while the stream is not + * enabled. + * @return True if the operation succeeded, false otherwise. */ - void setNumberOfDataItems(const uint16_t nBytes); + bool setNumberOfDataItems(const uint16_t nBytes); /** * @brief Select the channel to be used by the stream during -- GitLab