|
NetBurner 3.5.7
PDF Version |
High-precision microsecond delay functionality. More...
Classes | |
| class | DelayObject |
| High-resolution microsecond delay timer class. More... | |
High-precision microsecond delay functionality.
#include< HiResDelay.h>
The High Resolution Delay Timer provides microsecond-precision timing by utilizing hardware system timers. This is ideal for applications requiring precise timing control such as:
When a DelayObject is created, it automatically allocates an available hardware system timer from the processor's timer pool. The timer remains allocated for the lifetime of the object and is automatically freed when the object goes out of scope or is destroyed.
OSTimeDly() and DelayObject::DelayUsec() both block the calling task, but they are built for fundamentally different timing regimes. Choosing the wrong one either wastes a scarce hardware timer or silently misses the timing you thought you asked for.
OSTimeDly() counts in RTOS ticks. With the default TICKS_PER_SECOND = 20, one tick is 50 ms. OSTimeDly(1) does not mean "sleep 50 ms" — it means "wake on the next tick boundary," which can be anywhere from near-zero up to a full 50 ms away depending on where the current time falls in the tick window. Only OSTimeDly(N) for reasonably large N approaches N * tick_period.DelayObject::DelayUsec() programs a hardware timer directly, so the delay is measured in microseconds against the timer's own clock — no dependency on the RTOS tick phase.OSTimeDly() uses the single shared system tick. Every task in the system can use it concurrently at no additional hardware cost.DelayObject consumes one channel of a hardware timer peripheral (a small pool — e.g., four DMA timers on MCF5441X). Every active DelayObject reserves a channel for its lifetime, even when not actively delaying. Create them sparingly and prefer static/long-lived instances to avoid exhausting the pool.OSTimeDly() is effectively free: it inserts the task on the scheduler's delay queue and yields. No peripheral setup, no ISR wiring.DelayObject::DelayUsec() arms a hardware timer, takes an interrupt on expiry, runs an ISR, and posts a semaphore. For delays shorter than ~20 us this setup cost dominates, which is why the implementation falls back to a calibrated busy-wait in that range.OSTimeDly() waits allow the scheduler to idle the CPU between ticks.DelayObject keeps a peripheral clocked and will generate interrupts regardless of system load.| If you need... | Use |
|---|---|
| Sub-tick precision (<50 ms, or any µs value) | DelayObject |
| Protocol or bit-bang timing (I2C, 1-Wire, pulses) | DelayObject |
| General task pacing (>= several ticks) | OSTimeDly() |
| Periodic loops that tolerate tick-granular phase | OSTimeDly() |
| Drift-free periodic schedule | OSTimeWaitUntil() |
OSTimeDly() and leave the hardware timers for code that actually needs microsecond precision.| Method | Resolution | Blocking | Use Case |
|---|---|---|---|
| OSTimeDly() | 1 RTOS tick (50 ms default) | Yes | General delays |
| DelayObject | Microseconds | Yes | Precise hardware timing |
| IntervalTimer | Microseconds | No | Periodic callbacks |
| Stopwatch | Microseconds | No | Time measurement |