# STM32F411RE UART IRQ

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);
}


## 1 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.

• I located the function, when I ran it I don't enter the irq-routine when sending a character from PuTTY. I haven't touched anything in main. Mar 26 '16 at 14:52
• When I send a character in the terminal, I get "OK" different amount of times. Why is that happening? I tried to disable interrupt when entering, and, enable it again when leaving the interrupt handler, the result was that it was alternating between printing non-stop and pausing ?!?! Mar 26 '16 at 17:18
• HAL_UART_Receive_IT(&huart2,&data1,sizeof(data1)); have u read in HAL manual what does this function do exactly, becuase i takes the size of the buffer . instead of this condition if(data1 > 0) and sending "OK" let the the function at each interrupt send you the character it recieves , in this way it is easier to debug Mar 28 '16 at 7:48