cancel
Showing results for 
Search instead for 
Did you mean: 

How to use usart + dma ?

lldr.1
Associate

hi, Dear, how can I use usart + dma? I make the project by stm32cubeMx and add some code in main function as below: but it run fail and show a dma transfer error(Show as addition files: RunHere.png). Looking for your reply , Thank you!

 

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 uint32_t ticko = 0;

 uint32_t t = 0;

 char buf[256];

 while (1)

 {

    if (HAL_GetTick() - ticko > 1000)   {

       ticko = HAL_GetTick();

       sprintf(buf, "test uart and dma: time:%d\n", t++);

       HAL_UART_Transmit_DMA(&huart1, buf, strlen(buf));

 

       //HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size);

       //HAL_USART_Transmit_DMA(&huart1, buf, strlen(buf));

    }

   /* USER CODE END WHILE */

 

   /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

3 REPLIES 3

You'd presumably need to use a callback to flag if the transfer completed. And then wait before sending again.​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Are you meaning add a callback function like below:

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)

{

  if (huart == &huart1)  {

     ctlUart.f_TxBusy = FALSE;

#ifdef freeRtos_Enable

     if (pdPASS != xSemaphoreGive(ctlUart.sema))  {

        while (1);

     }

#endif

  }

}

Miles1
Associate III

I had a similar issue where only the first DMA transmission would work, and found the fix was to enable HAL_UART_IRQHandler()

See this thread for more details:

https://community.st.com/s/question/0D50X00009XkgtY/dma-uart-with-hal-remain-busy-bug