MaJerle / stm32-usart-uart-dma-rx-tx

STM32 examples for USART using DMA for efficient RX and TX transmission
MIT License
1.3k stars 322 forks source link

problem in using multiple DMAs for RX #29

Closed ymkim92 closed 1 year ago

ymkim92 commented 1 year ago

I love this repo. It has nice documentation as well. Thank you.

I found a problem when I try to work with 2 UARTs by following the instruction here. The code uses RX DMA. It works fine when I test each UART port one by one. But the problem happens when I play with 2 ports at the same time:

The problem I found is that "LL_DMA_GetDataLength()" returns a wrong value sometimes very often in the case above. I use different streams:

void DMA1_Stream1_IRQHandler(void)
{
  /* USER CODE BEGIN DMA1_Stream1_IRQn 0 */
    // USART3 RX
    if (LL_DMA_IsActiveFlag_HT1(DMA1))
    {
      LL_DMA_ClearFlag_HT1(DMA1);
      /* Call function Reception complete Callback */
      USART3_Rx_Callback();
    }
    else if (LL_DMA_IsActiveFlag_TC1(DMA1))
    {
      LL_DMA_ClearFlag_TC1(DMA1);
      /* Call function Reception complete Callback */
      USART3_Rx_Callback();
    }
    else if(LL_DMA_IsActiveFlag_TE1(DMA1))
    {
      /* Call Error function */
      USART3_Error_Callback();
    }
...
void DMA1_Stream6_IRQHandler(void)
{
  /* USER CODE BEGIN DMA1_Stream6_IRQn 0 */
    // UART8 RX
    if (LL_DMA_IsActiveFlag_HT6(DMA1))
    {
      LL_DMA_ClearFlag_HT6(DMA1);
      /* Call function Reception complete Callback */
      UART8_Rx_Callback();
    }
    else if (LL_DMA_IsActiveFlag_TC6(DMA1))
    {
      LL_DMA_ClearFlag_TC6(DMA1);
      /* Call function Reception complete Callback */
      UART8_Rx_Callback();
    }
    else if(LL_DMA_IsActiveFlag_TE6(DMA1))
    {
      /* Call Error function */
      UART8_Error_Callback();
    }
...
MaJerle commented 1 year ago

Please double check that all parameters are properly used for both uarts.

ymkim92 commented 1 year ago

It is very hard to find the cause of the problem. I have decided to use the interrupt instead of DMA.

MaJerle commented 1 year ago

Can you paste the full source of all uart rx callbacl functions here?

ymkim92 commented 1 year ago

I am sorry for this late response. I can't find the code at the moment. But I guess the cause of the problem was the different priority between interrupt and dma stream. I overlooked the note in README.md.