From eda4945efac219a7b4363bf3946d9e77d7b33bb8 Mon Sep 17 00:00:00 2001
From: Fabrizio Monti <fabrizio.monti@skywarder.eu>
Date: Thu, 6 Feb 2025 20:13:40 +0100
Subject: [PATCH] [DMA] Added method to change the transaction size without the
 need to rerun the entire setup.

---
 src/shared/drivers/dma/DMA.cpp | 10 +++++++++-
 src/shared/drivers/dma/DMA.h   |  7 +++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/src/shared/drivers/dma/DMA.cpp b/src/shared/drivers/dma/DMA.cpp
index 5ee1dc131..07679e4f9 100644
--- a/src/shared/drivers/dma/DMA.cpp
+++ b/src/shared/drivers/dma/DMA.cpp
@@ -370,7 +370,8 @@ void DMAStream::setup(DMATransaction transaction)
     registers->CR |= static_cast<uint32_t>(transaction.priority);
     if (transaction.circularMode)
         registers->CR |= DMA_SxCR_CIRC;
-    registers->NDTR = transaction.numberOfDataItems;
+
+    setNumberOfDataItems(transaction.numberOfDataItems);
 
     if (transaction.direction == DMATransaction::Direction::MEM_TO_PER)
     {
@@ -551,6 +552,13 @@ void DMAStream::readFlags()
     directModeErrorFlag  = flags & DMA_LISR_DMEIF0;
 }
 
+void DMAStream::setNumberOfDataItems(const uint16_t nBytes)
+{
+    // TODO: assert that the stream is disabled while doing it
+    currentSetup.numberOfDataItems = nBytes;
+    registers->NDTR                = nBytes;
+}
+
 int DMAStream::getCurrentBufferNumber()
 {
     return (registers->CR & DMA_SxCR_CT) != 0 ? 2 : 1;
diff --git a/src/shared/drivers/dma/DMA.h b/src/shared/drivers/dma/DMA.h
index e36a70d90..8fda416db 100644
--- a/src/shared/drivers/dma/DMA.h
+++ b/src/shared/drivers/dma/DMA.h
@@ -210,6 +210,13 @@ public:
      */
     void readFlags();
 
+    /**
+     * @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.
+     */
+    void setNumberOfDataItems(const uint16_t nBytes);
+
     /**
      * @brief Returns the last read status of the half transfer flag.
      *
-- 
GitLab