# STM32 HAL_DELAY hangs with HAL_GetTick=0 [closed]

Environment: GNU_ARM, STM32F407

Every time I use hal_delay() the program hangs just before the call to hal_delay().

printf("\n%d==>%d,",tickstart,HAL_GetTick());

in the loop of HAL_DELAY. The result is always: 0==>0

HAL_GetTick() doesn't work as expected even after I added:

HAL_NVIC_SetPriority(SysTick_IRQn,0,0);

How can I solve this?

## closed as unclear what you're asking by PlasmaHH, Scott Seidman, m.Alin, Wesley Lee, Voltage SpikeFeb 16 '17 at 16:54

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• the question is not clear, if possible modify the question with some line of code where the fault occur, also give more details of the problem you face – Raj Feb 16 '17 at 13:26
• the problem was that the interrupt is not handled so i added the file stm32f4xx_it.c/h generated by cubemx to my project wich contain a call to hal_inctick() – shi Feb 16 '17 at 14:04
• Be careful with interrupts – Voltage Spike Feb 16 '17 at 16:54

As an advanced STM32 user I can say that you did not handle the SysTick interrupt.

The HAL library enables the SysTick timer and interrupts for it.

In stm32f4xx_it.c file add this function call:

void SysTick_Handler(void) {
HAL_IncTick();
}


Then it should work.

• sorry where i can find stm32f4xx_it.c i can't find it in the project. – shi Feb 16 '17 at 13:47
• I assume you use CMSIS lib. If so, this file should be provided by ST. Use CubeMX to generate project for you. Then start from there. – tilz0R Feb 16 '17 at 13:53
• thx a lot bro it works – shi Feb 16 '17 at 14:01

When you use a HAL_DELAY you have to keep in mind that the counting is done in the interrupt routine SysTick. If you call the HAL_DELAY in the interrupt that has greater priority, then systick counting is stopped. So the HAL_DELAY gets stucked in the endless loop do...while, because the systick isn't counting. The remedy would be to set Systick interrupt at higher priority.

• github.com/MaJerle/stm32fxxx_hal_libraries/blob/master/… Delay can be easily done in interrupt with higher priority. Systick should always have lowest priority possible. – tilz0R Feb 17 '17 at 7:29
• @tilz0R I am using the MxCube, is it possible to make a simple patch to the autogenerated code? I see you are using your custom library. – Marko Buršič Feb 17 '17 at 7:52
• Cube will always generate this but you can override HAL_Delay I suppose. It has __weak parameter. Since SysTick has flag which indicates that reload happens but you did not read yet status, you can determine number of reloads (number of systicks). – tilz0R Feb 17 '17 at 8:50