cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_Delay() causes infinite loop in some cases

Rob Keck
Associate II
Posted on February 02, 2017 at 15:44

Hey there, I am relatively new to STM32 Development (coming from an AVR Background).

I am currently working on a LoRaWAN sample project using the STM32L053 Discovery board and the SX1272 Modem with IBM LMiC Library. According to the Docs i have implemented all needed functions.

So there is this function i have to implement: void hal_waitUntil(uint32_t time).

So i just call HAL_Delay(time) inside the function. But if I run the code on the controller the delay never exits.

Getting the Systick Value with GDB or PRINTF shows, that the systick isn't increasing anymore after entering the delay. But if I call HAL_Delay() somewhere in the main.c it works without problems (so the systick must work).

I attached my eclipse project. 

In the src folder there are main.c, hardware.c (where i init the peripherals) and hal.c (where the LMiC functions are declared).

Would be awesome if someone could help me I'm hangin' there for days...

#systick #hal_delay #stm32
1 ACCEPTED SOLUTION

Accepted Solutions
David SIORPAES
ST Employee
Posted on February 02, 2017 at 16:01

I had a rapid look to the code and it seems that in radio_init() you are disabling all interrupts (including Systick) so the Systick counter stops. Afterwards, you call hal_waitUntil() which never exists as time has stopped.

View solution in original post

9 REPLIES 9
David SIORPAES
ST Employee
Posted on February 02, 2017 at 16:01

I had a rapid look to the code and it seems that in radio_init() you are disabling all interrupts (including Systick) so the Systick counter stops. Afterwards, you call hal_waitUntil() which never exists as time has stopped.

Posted on February 02, 2017 at 16:14

Well, yeah, thats it... 

Thanks a lot man! U You saved my day.

Posted on February 03, 2017 at 13:34

So the next question would be, how can i stop the systick from interrupting?

The LMiC manual says i have to provide the delay in systicks. Should I use a timer instead?

Posted on February 03, 2017 at 18:33

If you really need to disable all interrupts you'd need to poll on a timer or on CPU cycle counter.

If however you can tolerate SysTick (and possibly other) interrupts you may want to arrange your interrupt priorities so that instead of disabling all interrupts with __disable_irq() you disable only the ones with a priority level lower than a given threshold with __set_BASEPRI().

RDois
Associate II

Hi,

I'm having the same problem.

There is no code included in the post. Could anyone tell me where these interrupts are being disabled / how to enable them?

Thanks in advance.

Check TICK_INT_PRIORITY in stm32fxyz_hal_conf.h

This needs to be a low number so the tick will keep ticking even when you are inside another interrupt (callbacK).

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

@Community member​ 

I have it set to (uint32_t)0U

Levels for other interrupts would need to be higher so the SysTick can preempt them so the tick increments, and HAL_Delay() functions.

You should avoid using HAL_Delay() in interrupt, and by inference callback, functions.

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

There is a straightforward way to use the SysTick timer in polling mode for delay, which works even if interrupts are disabled (as long as SysTick is still running), see in this post:

https://community.st.com/s/feed/0D50X00009XkW2MSAV