title: STM32F4 DMA seems to be overflowing
body: <p>I'm having a problem in with STM32F4 DMA.
  I have set up my DMA config as follows in circular mode:</p>
  
  <pre><code>hdma_usart1_rx.Instance = DMA2_Stream2;
  hdma_usart1_rx.Init.Channel = DMA_CHANNEL_4;
  hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;
  hdma_usart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  hdma_usart1_rx.Init.Mode = DMA_CIRCULAR;
  hdma_usart1_rx.Init.Priority = DMA_PRIORITY_HIGH;
  hdma_usart1_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  HAL_DMA_Init(&amp;hdma_usart1_rx);
  
  __HAL_LINKDMA(huart,hdmarx,hdma_usart1_rx);
  </code></pre>
  
  <p>This is the UART DMA call:</p>
  
  <pre><code>HAL_UART_Receive_DMA(&amp;huart1,uartRxMsgBuffer, 40);
  </code></pre>
  
  <p>Every program cycle I pool the NDTR value to check if It has changed, if it does I then know that some bytes were received and are available for processing.
  For some reason as I developed my code I noticed that the DMA Rx buffer <code>uartRxMsgBuffer</code> was being corrupted when I send more than 40 bytes of data to it.</p>
  
  <p>I got kind of lost don't know where to go from this point on because I checked my program and I'm not writing directly to this buffer and somewhat it's getting overwritten. I also noticed that when I remove the following line from my code the DMA works:</p>
  
  <pre><code>uartm-&gt;msgProcessingBuffer[uartm-&gt;currentMsgProcessingBufferPointer][uartm-&gt;msgProcessingBufferCharPointer] = uartm-&gt;uartRxMsgBuffer[uartm-&gt;RxMsgPointerStart];
  </code></pre>
  
  <p>I tried replacing the code above for the equivalent one bellow and it's still not working:</p>
  
  <pre><code>strncpy(&amp;uartm-&gt;msgProcessingBuffer[uartm-&gt;currentMsgProcessingBufferPointer][uartm-&gt;msgProcessingBufferCharPointer],&amp;uartm-&gt;uartRxMsgBuffer[uartm-&gt;RxMsgPointerStart],1);
  </code></pre>
  
  <p>There are 2 pictures of the DMA buffer before and after corruption (see the <code>pRxBuffPtr</code> array). I'm sending streams of <code>123456789\r\n</code> to it, when I send for the 4th time the buffer gets corrupted.</p>
  
  <p><a href="https://i.stack.imgur.com/RxC7c.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RxC7c.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/cHlbc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cHlbc.png" alt="enter image description here"></a></p>