Electrical – STM32F411RE UART IRQ

freertosstm32cubemxstm32f4

I want to do RS-232 communication with a PC terminal using interrupt on the receiving for the STM32F11RE.
I've done polling using

HAL_UART_Receive(&huart2,&data1,sizeof(data1),HAL_MAX_DELAY);

in a while loop, echoing back upon completion lie this:

HAL_UART_Transmit(&huart2,"OK",sizeof("OK"),HAL_MAX_DELAY);

Now I want to do the same but without polling, but I don't know how to proceed.
The manual says "Enable the NVIC USART IRQ handle.", but there is no reference to NVIC in STM324fxx_hal_uart.c
There is a function

void HAL_UART_IRQHandler(UART_HandleTypeDef *huart);

Since it has an input argument, I guess this is not called by an interrupt vector somewhere. It must be the case that I will have to call it from main.
Should I somehow create a highpriority task and call the IRQ-handler?
Currently I have disbaled FreeRTOS.
There is a global interrupt flag that I checked in the CubMx.


I've add this now

void USART2_IRQHandler(void)
{

HAL_UART_IRQHandler(&huart2);

uint8_t data1;

 HAL_UART_Receive_IT(&huart2,&data1,sizeof(data1));
      if(data1 > 0)
      {
          HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin,GPIO_PIN_SET);
          HAL_UART_Transmit_IT(&huart2,"OK",sizeof("OK"));
      }
}

It works with this in main:

void MX_USART2_UART_Init(void)
{

 huart2.Instance = USART2;
 huart2.Init.BaudRate = 115200;
 huart2.Init.WordLength = UART_WORDLENGTH_8B;
 huart2.Init.StopBits = UART_STOPBITS_1;
 huart2.Init.Parity = UART_PARITY_NONE;
 huart2.Init.Mode = UART_MODE_TX_RX;
 huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 huart2.Init.OverSampling = UART_OVERSAMPLING_16;
 HAL_UART_Init(&huart2);
 HAL_NVIC_SetPriority(USART2_IRQn,14,0);
__HAL_UART_ENABLE_IT(&huart2,UART_IT_RXNE);
}

Best Answer

The interrupt handler will be located in stm32f4xx_it.c

You will find UART interrupt handler and callback function, you can add your code instead.

for example if you would like to store the recieved data in a special array or transimt the characters somewhere , just make sure to clear the flags if you are not using the callback function.

Related Topic