cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_UART_Receive_IT

SGian.1
Senior

Hi there!

I hope someone can help me. Afther a couple of years i start again to program the STM32 microcontroller (STM32F051). I need the use a UART for trasmit and receive data. For the trasmission no problem work weel ( i use the interrup HAL functions). My problem is when i want receive a data where i do to write the HAL_UART_Receive_IT function ? before a while or inside the while ? and why ? i'm confused. Someone know a good book or tutorial can hel me to understand the hal library ?

thank you soo much

Sergio

5 REPLIES 5
S.Ma
Principal

Well until someone has something better, try Tilen's https://github.com/MaJerle

KnarfB
Principal III

HAL_UART_Receive_IT prepares the hardware for receiving a buffer of fixed length. After the buffer is filled, the interrupt handler calls HAL_UART_RxCpltCallback, you implement that callback. At the end of the HAL_UART_RxCpltCallback callback, you will probably call HAL_UART_Receive_IT again to prepare for the next buffer.

This simple implementation takes only one char per interrupt:

char ch;
 
int main(void)
{
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_USART2_UART_Init();
 
  /* USER CODE BEGIN 2 */
  HAL_UART_Receive_IT( &huart2, &ch, 1 );
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  ...

and

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	// do something with ch 
	HAL_UART_Receive_IT( &huart2, &ch, 1 );
}

Note, that the USART2 global interrupt must be enabled in your project.

Todo: add error checking and handling.

hth

KnarfB

SGian.1
Senior

Thank you soo much. If i understand this stuff work for I2c bus and Spi

Sergio

SGian.1
Senior

:grinning_face: :grinning_face: wonderful finally work just one question inside a HAL_UART_RxCpltCallback is necessary write HAL_UART_Receive_IT( &huart2, &ch, 1 ); or i can just reset some flags ? and the answert is yes just reset some flags how ?

Thank you

Sergio

KnarfB
Principal III

Don't know, sorry. You may set a breakpoint in HAL_UART_RxCpltCallback, step into HAL_UART_Receive_IT and find out.

hth

KnarfB