Skip to main content
Visitor II
June 28, 2020
Question

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

  • June 28, 2020
  • 4 replies
  • 2072 views

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.

    This topic has been closed for replies.

    4 replies

    Visitor II
    June 28, 2020

    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

    Graduate II
    June 28, 2020

    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

    Explorer II
    June 28, 2020

    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

    LMI2Author
    Visitor II
    June 28, 2020

    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