cancel
Showing results for 
Search instead for 
Did you mean: 

Is HAL_Delay(1) quaranteed to be close to 1ms?

LMI2
Lead

Is it absolutely impossible that HAL_Delay(1); is sometimes zero delay due for instance when it called. Minimum values can sometimes be tricky.

I need a short delay around 1ms. 0.8ms or 1.2ms is allright but 0 zero is not ok.

4 REPLIES 4
Dvorak.Peter
Senior II

Just search this forum for "HAL_Delay clive"

Since HAL_delay is broken by design, you will need to roll your own as per Clive's instructions

Generally it should give at least 1ms but may be significantly longer, and frequently 2ms depending on when you hit the clock edges.

It can get delayed by other blocking code, or interrupts/callbacks.

What some level of tighter precision, perhaps use a 1 MHz / 1us TIM, and delta the TIM->CNT

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Nikita91
Lead II

You can use the SysTick counter value if your delay is less than the systick overflov time (that saves a timer)

#if defined(__CC_ARM)
	#pragma push
	#pragma Otime
#else
	#pragma GCC push_options
	#pragma GCC optimize ("O3")
#endif
 
void bspDelayUs (uint32_t us)
{
	volatile uint32_t start = DWT->CYCCNT ;
	volatile uint32_t cycles = (bspTsMhzDiv * us) - 42u ;	// Remove some cycles for instructions
 
	while ((DWT->CYCCNT - start) < cycles) ;
}
 
#if defined(__CC_ARM)
	#pragma pop
#else
	#pragma GCC pop_options
#endif

LMI2
Lead

Thank you all of you.

It looks like I can't print or bookmark search results. I had to take a screencapture. Reinventing the wheel and getting square wheels.

For your information, I have a board with 100pin STM32F746/STM32H750 CPU.

Solutions from Nikita and Clive look interesting. I was thinking myself of using DWT->CYCCNT ; or whatever to count clock cycles for 10ms and divide that by 1000 to get a 10us delay. But if you have a ready solution I may not bother. This 1ms delay was for testing my board and SW with low speed first.

Regards

Leif M