0

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!

1 Answers1

1

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.

koder
  • 2,038
  • 7
  • 10
  • Thank you a lot koder! I am also wondering that will `USART1_IRQHandler ` be called without `HAL_UART_Receive_IT ` function ? – Yuechen Jiang Jan 08 '21 at 01:53
  • @YuechenJiang, yes it will. `USART1_IRQHandler` is set up in the vector table as _the_ handler for USART1 interrupts. So whenever there is a hardware interrupt for USART1, it _will_ be called. But without a cal to `HAL_UART_Receive_IT` (or `HAL_UART_Reveive_DMA`) the HAL will not know what to do and so will never call the `HAL_UART_RxCpltCallback` (nor any other callback) function. – koder Jan 08 '21 at 08:27
  • Thank you so much! But I am having another question : it seems that when i put my `HAL_UART_Receive_IT` inside `USART1_IRQHandler` , then `USART1_IRQHandler` is never called. is this correct way to do so? – Yuechen Jiang Jan 11 '21 at 01:42
  • @YuechenJiang, if my answer helped, please accept it. And no calling `HAL_UART_Receive_IT` from within the handler is probably not the best idea. But I don't know why it fails. – koder Jan 11 '21 at 08:00
  • Thank you so much koder! Your help is really useful to me! Hope you have a great day! – Yuechen Jiang Jan 12 '21 at 06:10