3
\$\begingroup\$

Environment: GNU_ARM, STM32F407

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

I added:

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?

\$\endgroup\$
4
  • 2
    \$\begingroup\$ 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 \$\endgroup\$
    – Raj
    Commented Feb 16, 2017 at 13:26
  • \$\begingroup\$ 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() \$\endgroup\$
    – Ramo
    Commented Feb 16, 2017 at 14:04
  • \$\begingroup\$ Be careful with interrupts \$\endgroup\$
    – Voltage Spike
    Commented Feb 16, 2017 at 16:54
  • \$\begingroup\$ Not sure what's unclear about the question. The question and answers helped me to fix this issue for me. I had the HAL_GetTick() overwritten and forgot to increment uwTick in my function. \$\endgroup\$
    – user233646
    Commented Dec 20, 2019 at 10:38

2 Answers 2

9
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ sorry where i can find stm32f4xx_it.c i can't find it in the project. \$\endgroup\$
    – Ramo
    Commented Feb 16, 2017 at 13:47
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Feb 16, 2017 at 13:53
  • \$\begingroup\$ thx a lot bro it works \$\endgroup\$
    – Ramo
    Commented Feb 16, 2017 at 14:01
  • \$\begingroup\$ You will not believe how long it took me... to find your (correct) answer. Thank you! \$\endgroup\$ Commented Jul 29, 2020 at 14:35
1
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Feb 17, 2017 at 7:29
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Feb 17, 2017 at 7:52
  • \$\begingroup\$ 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). \$\endgroup\$ Commented Feb 17, 2017 at 8:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.