cancel
Showing results for 
Search instead for 
Did you mean: 

USART in interrupt mode

PNaph.2
Associate II

Hi, I want to use USART in interrupt mode in stm32f429 evaluation board.

I saw the demo example UART_Hyperterminal_IT, but I didn't understood why they are calling HAL_UART_Receive_IT() function if we are using USART in interrupt mode not in polling mode ?

Calling this function, look like we are polling for coming message on USART.

Please correct me if I am wrong ?

And please tell how can I use USART in interrupt mode?

Thanks

Pratik

2 REPLIES 2

The Cube/HAL functions are relatively inflexible, as they expect a fixed number of data to arrive. Especially if you are experienced with programming on other mcus, you may be better off avoiding them and using direct register access.

JW

KnarfB
Principal III

No, HAL_UART_Receive_IT is just preparing the receive and non-blocking. Once the buffer is filled, the HAL_UART_RxCpltCallback will be called which you have to implement. A typical implementation will call HAL_UART_Receive_IT inside to enable further eceives. Like

char ch;
 
void main()
{
	...
	HAL_UART_Receive_IT(&huart1, &ch, 1); // kick-off receive
	...
}
 
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if(huart == &huart1) {
		// process the byte received
		HAL_UART_Receive_IT(&huart1, &ch, 1); // kick-off next receive
	}
}

The easiest use is with single char buffers unless you know the expected packet size precisely. Even then, you have to care about errors. See Tilen Majele's code http://stm32f4-discovery.net/2017/07/stm32-tutorial-efficiently-receive-uart-data-using-dma/