cancel
Showing results for 
Search instead for 
Did you mean: 

Is HAL_Delay() implemented with Interrupts?

Karan 123
Senior

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​

15 REPLIES 15
berendi
Principal

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

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​

Nikita91
Lead II

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.

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

Pavel A.
Evangelist III

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

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

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().

Ok ..Thanks .. But I also want to use high priority interrupt with HAL_Delay() and don't want to disable that like Pavel A. (Community Member) said ..

--

Karan

berendi
Principal

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