1
\$\begingroup\$

I am working with the stm32f4xx for the using UART DMA Tx-Rx operations.

I have this line firstly:

HAL_UART_Receive_DMA(&huart2,(uint8_t*)dma_rx_buf,DMA_BUF_SIZE);

Than I check the NDTR register with below code: (I stop the DMA before I get to these lines)

valueNDTR = __HAL_DMA_GET_COUNTER(huart->hdmarx);

Sometimes currentValueCDTR returns 0.

And I tried below lines, I expected value NDTR = DMA_BUF_SIZE but I get 0.

HAL_UART_DMAStop(&huart2);
valueNDTR = __HAL_DMA_GET_COUNTER(huart->hdmarx);

What is the reason for the NDTR value returning 0, DMA is full or another reason?

\$\endgroup\$
0
1
\$\begingroup\$

NDTR == 0 means that DMA transfer is completed. I don't know how you process USART reception buffer, but NDTR == 0 case probably indicates an error. This may happen if more bytes than you expected end up in your USART receiver before you process them. In this case, DMA stops (which is actually a good thing because it prevents DMA to overflow data to somewhere else), and the bytes in RX buffer are incomplete or corrupted.

NDTR == DMA_BUF_SIZE (assuming 1-byte transfers) means that no bytes have been received since you enable DMA.

Normally, one configures an RX buffer and DMA NDTR according to the max expected packet/frame size. For example, for implementing Modbus protocol, a 256-byte should be enough.

Then you configure an idle line detection interrupt or Modbus timeout interrupt to detect the end of the frame. In the USART ISR, you disable DMA and DMA_BUF_SIZE - NDTR gives you the number of bytes received. Then you process the RX buffer and re-enable RX DMA.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.