Skip to main content
xmart
Associate II
August 14, 2020
Question

HAL Delay () function does not work with HAL_UART_Receive()

  • August 14, 2020
  • 11 replies
  • 2629 views

Hi everyone

I'm having this problem while using stm32L073RBT:

When I use HAL_UART_Receive() only it seems normal, but when I use HAL Delay () after or before HAL_UART_Receive() the program freezes,Knowing that I did not enable any interrupt!!

Thanks

This topic has been closed for replies.

11 replies

prain
Visitor II
August 15, 2020

Actually you have to enable SYSTICK interrupt. without this interrupt, HAL Delay () will not work .

xmart
xmartAuthor
Associate II
August 15, 2020

Thanks for your answer

interrupt for SYSTICK is enabled by default because I am generating the code with stm32cubmx, and I did a simple test like this:

int i=0;

while(1)

{

i++;

HAL Delay (500) ;

}

The delay function works!!

but when using HAL_UART_Receive() function it does not work

maybe there is interference or something

KnarfB
Super User
August 15, 2020

never heard about such an interference. Things may become clearer if you show more code.

LMI2
Senior III
August 15, 2020

Stupid question perhaps, but where and what exactly does get stuck. Do you for example have a very long timeout.

xmart
xmartAuthor
Associate II
August 15, 2020

What do you mean stupid ?! I think it's not a good word, I am describing exactly what is happening to me

LMI2
Senior III
August 15, 2020

I ment my own question. What exactly gets stuck?

xmart
xmartAuthor
Associate II
August 15, 2020

At first I thought that the program was stuck because no data was received from uart, but I later found out that it was not stuck because the counter is increasing, there is something that makes uart not working, then I noticed when I canceled the delay, uart received

Tesla DeLorean
Guru
August 15, 2020

The interference of UART vs SYSTICK is not something I can explain

I'd recommend clearing the local/auto variables

RCC_OscInitTypeDef RCC_OscInitStruct = {0};

 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

Check what interrupt handlers you have.

Have the loop transmit a character out of the UART so you can observe the looping independently of the debugger

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
TFranke
Associate
August 16, 2020

Hmm, what exactly do you expect from your main loop code?

 while (1) { 

HAL_UART_Receive(&huart1,buf,200,500);

HAL_Delay(500);

    i++;

 }

From my point of view your code puts the UART into receive mode for 500ms, then sleeps for another 500ms without being able to receive data. This may work or not, it's a question of chance if you are in receive mode when you transmit your test data. By removing the delay function you just increase the chance for successful reception.

Take a look at HAL_UART_Receive_DMA() or HAL_UART_Receive_IT() functions instead to receive data in the background of your main program execution.

HTH,

Tobias

xmart
xmartAuthor
Associate II
August 17, 2020

Right, but I know when the data comes in