Search code examples
cinterruptuartiarstm32f0

I am trying to use UART communication in interrupt mode, where should i put my function?


I am new to USART communication so forgive me if i am asking a silly question.

I am using STM32F0discovry board and code using IAR EWARM. My main function looks like this. I also have a HAL_UART_RxCpltCallback function after the main function.

int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
  NVIC_EnableIRQ(USART1_IRQn);
  //HAL_UART_Receive_IT(&huart1,buffer,1);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
   
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
    //HAL_UART_Receive(&huart1,buffer2,10); // unit: milli second
    HAL_UART_Receive_IT(&huart1,buffer,1); // unit: milli second
    HAL_Delay(100);
    if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0))
    {
      HAL_UART_Transmit(&huart1,buffera,1,100);
      HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_SET);
      HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_SET);
    }
  }
  
  /* USER CODE END 3 */
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
   if(buffer[0] == 'a' )
    {
      HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_9);
      HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_RESET);
    }
    else if(buffer[0] == 'b' )   
    {
      HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_8);
      HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET);
    }
    else if(buffer[0] == 'c' )   
    {
      HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET);
      HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_RESET);
    }
   //HAL_UART_Receive_IT(&huart1,buffer,1); // unit: milli second
}

I am reading some tutorials and wondering if I should put my function inside the USART1_IRQHandler function.

void USART1_IRQHandler(void)
{
  /* USER CODE BEGIN USART1_IRQn 0 */
    
  /* USER CODE END USART1_IRQn 0 */
  HAL_UART_IRQHandler(&huart1);
  /* USER CODE BEGIN USART1_IRQn 1 */

  /* USER CODE END USART1_IRQn 1 */
}

Or should I put my code inside the HAL_UART_RxCpltCallback function? I am confused, Thank you for your time reading this!


Solution

  • void USART1_IRQHandler(void) is the actual interrupt handler that gets executed by the CPU when a USART1 IRQ is triggered. It delegates the actual work to the STM32 HAL by calling HAL_UART_IRQHandler(&huart1);.

    That HAL_UART_IRQHandler is the generic IRQ handler for all UART interrupts, which is why it takes a UART handle so it knowns what UART triggered the interrupt.

    The HAL_UART_IRQHandler the determines what event has actually happened by querying the actual status bits of the UART that triggered the interrupt. Based on that it call the actual handler function.

    The handler function then performs the actual work. In case you started a receive and all requested bytes have been received it will call HAL_UART_RxCpltCallback.

    There you should check that the function is called for the UART you expected and then you can process the data.

    There are a number of such callback function, which can be found in the documentation of the HAL.