cancel
Showing results for 
Search instead for 
Did you mean: 

UART receive in interrupt mode (HAL_UART_Receive_IT)

Valentin Gros
Associate III

Hello,

Target : NUCLEO-L432KC board

IDE : STM32Cube IDE

OS : Windows

I want to get information from a GPS chip with an UART communication (TX/RX). It works when I use non-interrupt mode with HAL_UART_Receive function form HAL library.

However I want to optimize the code using the function HAL_UART_Receive_IT from HAL library. For now it is not working.

My UART configuration is here :

0690X000009jzH4QAI.png

I only added this code to the generated main.c :

/* USER CODE BEGIN 0 */

uint8_t rx_buff[20];

/* USER CODE END 0 */

and

 /* USER CODE BEGIN 2 */

 HAL_UART_Receive_IT (&huart1, rx_buff, 20);

 /* USER CODE END 2 */

and

/* USER CODE BEGIN 4 */

void HAL_UART_RxCplt_Callback(UART_HandleTypeDef * huart)

{

__NOP();

}

void HAL_UART_TxCplt_Callback(UART_HandleTypeDef * huart)

{

__NOP();

}

/* USER CODE END 4 */

Then I compiled the code, and I expected to see parts of the data form the GPS in my rx_buff variable. But there is no data in this variable according to the "live expression" debugging tool.

0690X000009jzJFQAY.png

Do you have any idea why it doesn't work ?

Best regards,

Valentin Gros

10 REPLIES 10
parth kothiya
Senior

__HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE);

 HAL_UART_Receive_IT(&huart1, (uint8_t *)RX_Buffer, sizeof(RX_Buffer));

while debugging your code check uart data register you get any thing or not.

also check priority of uart interrupt.

retry after changing optimization setting(no optimize code) or use volatile.

clean whole project and rebuild project.

Valentin Gros
Associate III

Hello Parth Kothiya,

First of all, thanks for you help.

I updated my program, so now I have this code (before the while(1){} loop) :

 /* USER CODE BEGIN 2 */

 __HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE);

 HAL_UART_Receive_IT(&huart1, (uint8_t *)rx_buff, sizeof(rx_buff));

 /* USER CODE END 2 */

  • I have no error, no warning when compiling, however I still don't have any data in rx_buff (despite that it works with  HAL_UART_Receive without interrupt).
  • Priority of UART interrupt is set to 0,0 (see screenshot)

0690X000009k00cQAA.png

  • I'm using STM32Cube IDE, I didn't find the window where I can change optimization settings
  • I cleaned the whole projet and rebuilt it but I still don't have any data
Bob S
Principal

When are you looking at the receive buffer? Are you looking immediately after calling HAL_UART_Receive_IT(), say by setting a breakpoint or single-stepping in the debugger? If so, there won't be anything there YET. You should add code to your HAL_UART_RxCplt_Callback() to set a global (and volatile) flag saying that you have received 20 bytes. Then in your main loop, wait for that flag to be set and THEN look at the receive buffer.

Another question - does the GPS send data automatically (like once per second or something like that)? Or do you need to send something to the GPS first?

Pavel A.
Evangelist III

> __HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE);

No, you don't have to enable the U(s)ART interrupt this way before calling HAL_UART_Receive_IT.

HAL_UART_Receive_IT will do this itself.

> Priority of UART interrupt is set to 0,0 (see screenshot)

This should instruct Cube to generate NVIC enable interrupt call for the UART . Have you checked that it is generated and called?

-- pa

code optimization setting found in your compiler settings

KEIL IDE:

http://www.keil.com/support/man/docs/armclang_intro/armclang_intro_fnb1472741490155.htm

TRUE STUDIO:

You can change that setting under build configuration (hammer with a blue icon) -> C/C++ Build -> Settings -> Tool settings -> C Compiler -> Optimization.

try this if it not work then backup your project and make new project then 1st try uart receive data in interrupt mode it successfully receive data then change code according to your application.

NOTE: check TX RX PINS and your gsm module send any data better to use any serial port software(cool term) to send data to mcu debug code and you can find what is going wrong.

umesh_patil
Associate II

receive your rx_buff in the receive it callback function. It will work

Hello Parth Kothiya,

Thanks for you help.

  • I tried different build configuration (fyi with STM32Cube IDE, the path is : File - > Properties -> C/C++ Build -> Settings - > Tool Settings - > Optimization). Default config is optimization level = none (-O0))
  • Your second idea is to try an example called "uart receive data in interrupt mode" ?
  • My GPS is sending data continuously (and I receive the data in no-interrupts mode)

Hello Bob,

Thanks for you help.

  • I'm looking at the receive buffer every time. The "live expression debug function" of STM32Cube IDE makes it possible to watch continuously variables. So I should see this buffer changing very fast. Instead it stays at 0.
  • Yes my GPS is sending data continuously. I receive data when I use no-interrupt function like " HAL_UART_Receive(&huart1, rx_buff, 20);"

Hello Pavel,

Thanks for your help.

  • I don't see generated NVIC interrupt call for the UART in the main.c. Should it be generated in main.c ? What does it looks like ?