Search code examples
callbackstm32uartfreertos

how to register separate callback function for each UART port in stm32


I am using Freertos to program in STM32. is it possible to register separate callback function for each UART port in stm32? Whenever data received in that port that particular callback function has to be invoked after receiving data...

Thanks in advance...


Solution

  • In your HAL_UART_RxCpltCallback() function, you can act differently depending upon the UART peripheral.

    void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
      if (huart->Instance == USART1) {
        // USART1
      } else if (huart->Instance == USART2) {
        // USART2
      } else if (huart->Instance == USART3) {
        // USART3
      } else if (huart->Instance == UART4) {
        // UART4
      }
    }
    
    

    Same is possible with HAL_UART_TxCpltCallback as well.