AnsweredAssumed Answered

Delay function using __WFI()

Question asked by Ahmed Talaat on Apr 26, 2015
Latest reply on Apr 26, 2015 by Ahmed Talaat
Hello,
I have seen and implemented timer based delay function either based on TIM or systick, and they rely on a variable being inc/dec by the ISR till it satisfies a certain condition inside the Delay loop which allow it to return to the caller.
My concern with this technique is that it keeps the CPU running in an infinite loop till the expiry of the delay value which is consuming more power.

I thought of having the delay function configure the timer to generate an interrupt update once the duration specified by the user is achieved, and then go into sleep mode using __WFI().
My concern with this technique it that is not reliable because while the CPU is in Sleep waiting for the TIM interrupt to happen another interrupt would happen, then the delay call is returned to the called without waiting for the right time.

I thought of disabling all interrupts except for the delay timer before calling the __WFI() within the delay function, and then restoring back all interrupts state, but this is not good because it will not allow any other interrupt to be serviced till the delay expires.
In that case, the high power version based on infinite loop would be better despite its power consumption.

I read about Sleep on Exit. Not sure how to set it using the peripheral library but it will not help because I do a lot of activities in the main loop.

Read a lot about difference between WFI and WFE but I gave up to understand  but I am not sure that using WFE will not help in my case.

Is there a best of both worlds in generating delays?

Thank you.

Outcomes