cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 waiting for UART interrupt while doing other tasks

VYoun
Associate III

Hello,

I am using STM32H7 and Stm32CubeMX. In my current application, I perform a user interface (using UART), and then the system performs a measurement task.

However, I need to change the system to perform the user interface and measurement task in the following manner:

The measurement is performed anyways. The UART interface is activated as soon as the user enters something in the Serial Terminal.

I am really confused as to how to do this. I really appreciate any help.

Best regards,

Vouria

5 REPLIES 5
Curtis B.
Senior

Hi,

you could either operate the UART in Interrupt or in DMA mode. In Interrupt mode you can call the function HAL_UART_Receive_IT or in DMA mode you can call HAL_UART_Receive_DMA. You need to specify the number of characters you want to receive. If all characters are received HAL_UART_RxCpltCallback is invoked. You need to define this function and process the received data. If you do not expect messages with fixed length, it is more complicated. You can use interrupt mode to receive only one character. In the callback function you process the character an check for e.g. CR ore LF. Another possibility is to use DMA mode with a length greater ore equal to the maximum message length you expect. You can use the idle line interrupt to abort the DMA transfer and process the message in the correspondingcallback.

Regards,

Daniel

turboscrew
Senior III

What does "The UART interface is activated" mean?

I'd not use the HAL but write a "terminal" I/O. It's hard to do using HAL, but fairly easy with plain C.

HAL can still be used to set up the serial, but interrupt handling and send/receive functions you have to write yourself.

Check https://github.com/turboscrew/blue_pill_init/blob/master/FirstTry.c

search for "init_usart1" and read on.

Hi,

Thank you for your reply. I also believe that I should wait for UART interrupt. The problem is that the UART interrupt only works when I execute the line:

HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)

After this line is executed, the interrupt does not work. However, I need that my USART starts whenever the interrupt is received.

In general for my application, I want that the system abort other tasks and execute the UART Interrupt service routine whenever the any character is entered in the serial terminal.

Your help is greatly appreciated.

Thank you and best regards,

Vouria

Hello,

Thank you for your reply.

sorry if my explanation was not clear.

In my application I need to take few values from the user, so I used printf and usart receive to design an interface. The interface works as the following: some text is shown in the Serial Terminal ( such as "Enter the current time and date") and then the user input is received with usart receive functions.

My problem is the following: I want my application to run its tasks ( these are measurement tasks generally), but I want the system to stop its routines whenever an input is received from UART. I want the system to run the interface whenever a specific input is received in the UART (or scanf).

I will read the page that you have provided. Thank you again.

Best regards,

Vouria

Curtis B.
Senior

Hi Vouria,

If you have received the number of characters that you set in uint16_t Size the function HAL_UART_RxCpltCallback is invoked. If you want to receive further characters, you need to call HAL_UART_Receive_IT again.

Regards,

Daniel