This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new e3080a73371 arch/arm/stm32h7: Fail a serial open when no DMA stream is 
free.
e3080a73371 is described below

commit e3080a73371ecafd48056ef5c68f93405bfe1091
Author: Max Kriegleder <[email protected]>
AuthorDate: Wed Nov 18 18:54:47 2026 +0100

    arch/arm/stm32h7: Fail a serial open when no DMA stream is free.
    
    up_dma_setup() passed the handle from stm32_dmachannel() straight to
    stm32_dmasetup().  On the H7, stm32_dmachannel() does not wait for a
    stream: it returns NULL when every stream of the controller is taken.
    The open() then asserted in stm32_dmasetup(), or with assertions off
    dereferenced NULL.
    
    Return -EBUSY instead.  If the RX stream is the one that is missing,
    release the TX stream that was already taken, and undo up_setup() for
    a port that is not the console, so a later open() can succeed once a
    stream is free.
    
    Assisted-by: Claude:claude-opus-5-5
    Signed-off-by: Max Kriegleder <[email protected]>
---
 arch/arm/src/stm32h7/stm32_serial.c | 36 ++++++++++++++++++++++++++++++++++--
 1 file changed, 34 insertions(+), 2 deletions(-)

diff --git a/arch/arm/src/stm32h7/stm32_serial.c 
b/arch/arm/src/stm32h7/stm32_serial.c
index 4355c404540..f1054fd807a 100644
--- a/arch/arm/src/stm32h7/stm32_serial.c
+++ b/arch/arm/src/stm32h7/stm32_serial.c
@@ -2225,11 +2225,18 @@ static int up_dma_setup(struct uart_dev_s *dev)
     }
 
 #if defined(SERIAL_HAVE_TXDMA)
-  /* Acquire the Tx DMA channel.  This should always succeed. */
+  /* Acquire the Tx DMA channel.  This fails if all streams of the DMA
+   * controller are taken.
+   */
 
   if (priv->txdma_channel != INVALID_SERIAL_DMA_CHANNEL)
     {
       priv->txdma = stm32_dmachannel(priv->txdma_channel);
+      if (priv->txdma == NULL)
+        {
+          result = -EBUSY;
+          goto errout;
+        }
 
       /* Enable receive Tx DMA for the UART */
 
@@ -2239,11 +2246,18 @@ static int up_dma_setup(struct uart_dev_s *dev)
 #endif
 
 #if defined(SERIAL_HAVE_RXDMA)
-  /* Acquire the Rx DMA channel.  This should always succeed. */
+  /* Acquire the Rx DMA channel.  This fails if all streams of the DMA
+   * controller are taken.
+   */
 
   if (priv->rxdma_channel != INVALID_SERIAL_DMA_CHANNEL)
     {
       priv->rxdma = stm32_dmachannel(priv->rxdma_channel);
+      if (priv->rxdma == NULL)
+        {
+          result = -EBUSY;
+          goto errout;
+        }
 
       /* Configure for circular DMA reception into the RX FIFO */
 
@@ -2278,6 +2292,24 @@ static int up_dma_setup(struct uart_dev_s *dev)
 #endif
 
   return OK;
+
+errout:
+#if defined(SERIAL_HAVE_TXDMA)
+  if (priv->txdma != NULL)
+    {
+      modifyreg32(priv->usartbase + STM32_USART_CR3_OFFSET,
+                  USART_CR3_DMAT, 0);
+      stm32_dmafree(priv->txdma);
+      priv->txdma = NULL;
+    }
+#endif
+
+  if (!dev->isconsole)
+    {
+      up_shutdown(dev);
+    }
+
+  return result;
 }
 #endif
 

Reply via email to