Hi all,
I am just wondering if there is a delay function in the MCUXpresso IDE. For example, I am wanting to create a 1 second delay in my program, and I am wondering if there is a nice function to use instead of creating nested for loops.
For example, in Arduino code, I would just use the function:
delay(1000);
Thanks in advance.
Solved! Go to Solution.
To be honest, Arduino is not the best template for professional SW development, to say the least.
Delay functions (busy idling) are one of the worst choices, better use a timer peripheral.
I used to use the SysTick timer available in all Cortex M devices. Set it up at a proper cycle (e.g. 10ms), and handle your delay functionality in the respective interrupt handler.
This way, you can run multiple different delay in parallel without interference and very little performance impact.
To be honest, Arduino is not the best template for professional SW development, to say the least.
Delay functions (busy idling) are one of the worst choices, better use a timer peripheral.
I used to use the SysTick timer available in all Cortex M devices. Set it up at a proper cycle (e.g. 10ms), and handle your delay functionality in the respective interrupt handler.
This way, you can run multiple different delay in parallel without interference and very little performance impact.
To be honest, a knife may NOT always be the best option to split an object in two parts, for this reason humans introduced scissors, hacksaws, etc.
And for the exact same reason, busy idling may be the worst solution in some circumstances while may be the BEST solution in other circumstances. Your point is not a good reason for NXP to not provide sleep functions like Arduino does, nor just to discredit other platforms.
> Your point is not a good reason for NXP to not provide sleep functions like Arduino does, nor just to discredit other platforms.
All my project managers up to now are disagreeing with you.
Higher BOM costs for a more performant MCU just to safe an hour or two of development time is a no-go in commercial projects.
This might be different in private projects.
Arduino is a library that abstracts the hardware level to a certain degree, and lowers the entry barrier for beginners and hobbyists. Which is not a bad thing - unless you have a commercial project with strict BOM cost constraints.
By the way, one certain way to recognize Arduino users in MCU fora used to be the question "How fast can that MCU toggle a GPIO ?".
If you are looking for 'busy wait' routines, then you might have a look at the McuWait (.c and .h) in https://github.com/ErichStyger/McuOnEclipseLibrary/tree/master/lib/src
This is what I use, and it provides busy waiting with cycles, milliseconds and microseconds. It uses 'burning nops' or hardware cycle counters if available.
So you can use things like McuWait_Waitms(100) or McuWait_Waitus(10) or even McuWait_Waitns(50).
Just keep in mind that the waiting time (of course) does not consider time spent in interrupts.
I hope this helps,
Erich
Hi Erich,
thanks for your suggestion, I just meant to emphasize the fact that often, if not always, there is no one-size-fits-all way to tell what is the best or worst option for doing something.
And that actively checking against systick timer current count IS busy waiting; and that NXP SDK is not providing a built-in function for this, while others do... YAWN!
can someone explain the function principle of the SysTick from the Demo LED Blink example code?
What happen if I want to have a smaller delay, like us?
It works that way:
typically the SysTick is configured for 1 ms. You set a counter value, and the SysTick counts that value down. You wait until that counter is down to zero.
Of course that might depend on the exact example, but this is how it is usually done.
If you want in the us range, you have to increase the speed of the SysTick (say to 100 us) and count it down the same way.