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.

我尝试以下行,期望值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 Answer1

    1
    \$\begingroup\$

    NDTR == 0means that DMA transfer is completed. I don't know how you process USART reception buffer, butNDTR == 0case 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 implementingModbusprotocol, a 256-byte should be enough.

    Then you configure an idle line detection interruptorModbus timeout interrupt to detect the end of the frame. In the USART ISR, you disable DMA andDMA_BUF_SIZE - NDTRgives 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 ourterms of service,privacy policyandcookie policy

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