0
\$\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\$

closed as unclear what you're asking by PlasmaHH, Scott Seidman, m.Alin, Wesley Lee, Voltage Spike Feb 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.

  • 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 Feb 16 '17 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\$ – shi Feb 16 '17 at 14:04
  • \$\begingroup\$ Be careful with interrupts \$\endgroup\$ – Voltage Spike Feb 16 '17 at 16:54
5
\$\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\$
  • \$\begingroup\$ sorry where i can find stm32f4xx_it.c i can't find it in the project. \$\endgroup\$ – shi Feb 16 '17 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\$ – tilz0R Feb 16 '17 at 13:53
  • \$\begingroup\$ thx a lot bro it works \$\endgroup\$ – shi Feb 16 '17 at 14:01
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\$
  • \$\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\$ – tilz0R Feb 17 '17 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\$ – Marko Buršič Feb 17 '17 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\$ – tilz0R Feb 17 '17 at 8:50

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