Skip to main content
Graduate
August 29, 2024
Solved

HAL_UART_RxCpltCallback not called in DMA

  • August 29, 2024
  • 3 replies
  • 1684 views

Hi Everybody, 

  • USART interrupt enabled
  • DMA interrupt enabled
  • USART2_RX circular 
  • USART2_TX normal

Simplifying code to the essential part here, code is receiving the bytes in the circular buffer but it does not jump to HAL_UART_RxCpltCallback. Could someone please give some hints, what am I missing here?

 

#define UART_BUFFER_SIZE 4
__IO uint8_t uartBuffer[UART_BUFFER_SIZE];

void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_USART2_UART_Init(void);

 

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  HAL_Delay(1000);
  HAL_UARTEx_ReceiveToIdle_DMA(&huart2,(uint8_t *) &uartBuffer, UART_BUFFER_SIZE);
}

 

int main(void)
{
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART2_UART_Init();
 
  memset((uint8_t *) &uartBuffer, 0, sizeof(uartBuffer)); // Clear buffer
  HAL_UARTEx_ReceiveToIdle_DMA(&huart2,(uint8_t *) &uartBuffer, UART_BUFFER_SIZE);
 
  while (1)
  {
    HAL_Delay(1000);
  }
}

 

 

    This topic has been closed for replies.
    Best answer by TDK

    The HAL_UARTEx_ReceiveToIdle_DMA function will call the HAL_UARTEx_RxEventCallback callback, not HAL_UART_RxCpltCallback.

    See example (non-funcitonal) weak implementation here:

    /**
     * @brief Reception Event Callback (Rx event notification called after use of advanced reception service).
     * @PAram huart UART handle
     * @PAram Size Number of data available in application reception buffer (indicates a position in
     * reception buffer until which, data are available)
     * @retval None
     */
    __weak void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
    {
     /* Prevent unused argument(s) compilation warning */
     UNUSED(huart);
     UNUSED(Size);
    
     /* NOTE : This function should not be modified, when the callback is needed,
     the HAL_UARTEx_RxEventCallback can be implemented in the user file.
     */
    }

     

    3 replies

    TDKAnswer
    Super User
    August 29, 2024

    The HAL_UARTEx_ReceiveToIdle_DMA function will call the HAL_UARTEx_RxEventCallback callback, not HAL_UART_RxCpltCallback.

    See example (non-funcitonal) weak implementation here:

    /**
     * @brief Reception Event Callback (Rx event notification called after use of advanced reception service).
     * @PAram huart UART handle
     * @PAram Size Number of data available in application reception buffer (indicates a position in
     * reception buffer until which, data are available)
     * @retval None
     */
    __weak void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
    {
     /* Prevent unused argument(s) compilation warning */
     UNUSED(huart);
     UNUSED(Size);
    
     /* NOTE : This function should not be modified, when the callback is needed,
     the HAL_UARTEx_RxEventCallback can be implemented in the user file.
     */
    }

     

    Graduate
    August 29, 2024

    okay thanks.

    When I instead use the HAL_UART_Receive_DMA, no callback is triggered at the end, isnt it?

    Super User
    August 29, 2024

    When you use HAL_UART_Receive_DMA, then HAL_UART_RxCpltCallback will be called when the reception is complete. It will not be called until you receive all and exactly the number of characters you ask for.