2020-06-28 09:29 AM
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.
2020-06-28 10:05 AM
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
2020-06-28 10:13 AM
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
2020-06-28 11:40 AM
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
2020-06-28 02:21 PM
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