Antonio, in my opinion: when you need a delay of more that a ms, you could have the task sleep by calling e.g.
When you want to wait for 50 uS, you can go either of two ways:
- Set-up a timer-counter that you can poll and wait 50 uS in a loop.
- Write a loop that has a predictable speed of execution.
Take care with the latter, when you change the optimisation level of the compiler, a loop could be implemented differently, or even not be compiled at all.
One solution to this is to write the loop as assembler code.
Here is an example from Atmel:
cycles(unsigned long n)
/* n is passed in the register r0 */
asm volatile (
“loop: DMB n”
“SUBS R0, R0, #1 n”
“BNE.N loop ”