Skip to main content
Visitor II
June 27, 2020
Question

Is HAL_Delay() implemented with Interrupts?

  • June 27, 2020
  • 5 replies
  • 7024 views

Hi,

Is HAL_Delay() implemented with SysTick() interrupt ?

Is ​it safe to use HAL_Delay() in while loop main.c

with other ( low priorities ) interrupts?

​Thanks

--

Karan​

    This topic has been closed for replies.

    5 replies

    Visitor II
    June 27, 2020

    Yes. By default it uses a counter which is incremented by the SysTick interrupt handler once every millisecond.

    Karan 123Author
    Visitor II
    June 27, 2020

    Thanks for update..

    Do I need to change ​(lower) SysTick priority if I have HAL_Delay() in while (1). ?

    ​Where other high priority interrupts? ​

    --

    Karan​

    Explorer II
    June 28, 2020

    The HAL_Delay() use the counter register of SysTick, but not the SyStick iterrupt.

    The HAL_Delay() is an active polling loop waiting for SysTick interrupts.

    This is why HAL_Delay() should not be used in an interrupt routine.

    Karan 123Author
    Visitor II
    June 28, 2020

    Hi,

    @Nikita91 (Community Member)

    Thanks for update .. How to use HAL_Delay() in main.c while(1) with other interrupts but not inside the interrupts ?

    I have gone through below link..

    https://stackoverflow.com/questions/53899882/hal-delay-stuck-in-a-infinite-loop/53903790#53903790

    https://community.st.com/s/question/0D50X00009XkYLP/haldelay-causes-infinite-loop-in-some-cases

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

    By Below Priorities setup , the code stuck in infinite loop and then hardfault

    0693W000001rehFQAQ.png

    Should I avoid HAL_Delay and use dummy delay as below ?

    void __delay_ms(int32_t k)
    {
    	int32_t i,j ;
    	for(i=0;i<k;i++)
    		for(j=0;j<3000;j++)
    		{
     
    		}
    }

    Please advice..

    --

    Karan

    Super User
    June 28, 2020

    As @berendi​ replied, in main just call HAL_Delay. Unless you need delay while interrupts are disabled. Or unless you want more precise delay (microseconds). Just try it. Do not change the systick priority.

    --pa

    Karan 123Author
    Visitor II
    June 30, 2020

    Hi,

    @berendi (Community Member) and Pavel A. (Community Member)

    But Why below type of delay can't produce constant delay every time ?

    void __delay_ms(int32_t k)
    {
    	int32_t i,j ;
    	for(i=0;i<k;i++)
    		for(j=0;j<3000;j++)
    		{
     
    		}
    }

    --

    Karan

    Visitor II
    June 30, 2020

    Execution time depends on lots of factors, e.g. interrupt activity, code alignment in flash. It can never be relied upon. Use a timer or a counter derived from a timer, like HAL_Delay().

    Visitor II
    June 30, 2020

    Do not use HAL_Delay() or any other kind of delay in interrupts.

    Karan 123Author
    Visitor II
    June 30, 2020

    I do not use HAL_Delay() in interrupts . I want to use HAL_Delay() in main with interrupts

    Visitor II
    June 30, 2020

    What is it that still prevents you from using it?

    Visitor II
    June 30, 2020

    Take a step back and ask yourself: where are the time critical actions (that needs to kick in within 1 msec)

    Interrupts should remain very brief. If you do things that take time within an interrupt, something is wrong in the implementation.

    HAL_Delay has "1 msec jitter" to be very brief

    Timer Output Compare can be used to generate any period measurement. The finer the time granularity, the higer frequency the timer runs.

    If you need to drive LED Segment, why not use TIMER to generate a burst of clock pulses, and use another output compare channel(s) (interrupts?) to flip the data GPIOs if the LED clock frequency is few kHz ? The implementation depends on the timing constrains of the system and the timing/latency constrains within the microcontroller. This is what SW tools won't help much. It's called SW profiling.