cancel
Showing results for 
Search instead for 
Did you mean: 

Redirect Printf and Scanf to interrrupt driven UART

David_
Senior

This example work perfectly using non interrupt driven UART HAL library, HAL_UART_Receive and HAL_UART_Transmit

Any idea on changing to HAL_UART_Receive_IT and HAL_UART_Transmit_IT?

Many thanks

 

https://forum.digikey.com/t/easily-use-scanf-on-stm32/21103

2 REPLIES 2

Use ring buffers to hold the data and service the interrupts. 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Mostafa_Elrafei
Associate

Hello ,

You have to do the following ,

1- you have to create Queue, note I use here Queue handler from FreeRTOS for ease but you can implement your own circular buffer

QueueHandle_t xQueueSerial;

char serial_input;

xQueueSerial = xQueueCreate(20,sizeof(serial_input));

 

2- this queue shall be filled from HAL_UART_RxCpltCallback

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
 HAL_UART_Receive_IT(&huart2, (uint8_t*)&serial_input, 1);
 xQueueSendFromISR(xQueueSerial,&serial_input,NULL);
}

 

3- In task you shall check if queue is filled with data, if it is filled then you print the character recieved as following

Screenshot 2026-02-21 010250.png

 4- In Task if you press enter '\r' then flag_serial_finished is set to true and now you can parse data using sscanf:

2.png

Edited to apply source code formatting; please see How to insert source code for future reference.
Please don't post code as images.