NetBurner 3.5.6
PDF Version
DelayObject Class Reference

High-resolution microsecond delay timer class. More...

#include <HiResDelay.h>

Public Member Functions

 DelayObject (int Timer=FIRST_UNUSED_TIMER)
 Construct a high-resolution delay timer object.
 
 ~DelayObject ()
 Destructor - releases the allocated hardware timer.
 
void DelayUsec (uint32_t usec)
 Perform a blocking delay with microsecond precision.
 
bool valid ()
 Verify the delay object is valid and ready to use.
 
void _wake ()
 Internal ISR callback to wake blocked task (internal use only)
 

Detailed Description

High-resolution microsecond delay timer class.

Provides blocking delays with microsecond precision by allocating and using a hardware system timer. The timer is automatically managed - allocated on construction and freed on destruction.

This class uses a hardware timer from the processor's timer pool to provide precise microsecond delays. The timer is allocated when the object is created and remains dedicated to this object until it is destroyed. An internal semaphore is used to block the calling task during the delay period.

Note
Hardware timers are a limited resource. Always verify successful allocation by checking valid() after construction.
Thread Safety
Each DelayObject should be used by a single task. For multi-task use, create separate DelayObject instances or protect with a mutex.
Performance Considerations
  • Object creation allocates a timer (overhead on first use)
  • Use static or member variables to avoid repeated allocation
  • Very short delays (<10us) have relatively higher overhead
  • Delay accuracy affected by interrupt latency and system load
Example: Basic Usage
if (delay.valid()) {
delay.DelayUsec(500); // 500 microsecond delay
}
High-resolution microsecond delay timer class.
Definition HiResDelay.h:164
Example: Pulse Generation
static DelayObject pulseDelay;
void generatePulse(int pin, uint32_t pulseWidthUs) {
Pins[pin].set(); // Set pin high
pulseDelay.DelayUsec(pulseWidthUs); // Precise pulse width
Pins[pin].clr(); // Set pin low
}
void DelayUsec(uint32_t usec)
Perform a blocking delay with microsecond precision.
Example: Protocol Timing
static DelayObject protocolDelay;
void sendI2CStart() {
SDA.set();
protocolDelay.DelayUsec(5); // Setup time
SCL.set();
protocolDelay.DelayUsec(5); // High time
SDA.clr();
protocolDelay.DelayUsec(5); // Hold time
}
See also
Interval Timer For non-blocking periodic timers
Stopwatch Timer For measuring elapsed time
OSTimeDly() For standard RTOS delays

Constructor & Destructor Documentation

◆ DelayObject()

DelayObject::DelayObject ( int Timer = FIRST_UNUSED_TIMER)

Construct a high-resolution delay timer object.

Allocates a hardware system timer from the processor's timer pool. The timer remains allocated for the lifetime of this object.

Parameters
TimerTimer number to use. Default is FIRST_UNUSED_TIMER which automatically selects the first available timer. Specific timer numbers can be used for advanced applications but this is not recommended for general use.
Note
Always check valid() after construction to ensure a timer was successfully allocated.
Warning
If no timers are available, the object will be invalid and DelayUsec() should not be called. Check valid() first.
Example
DelayObject delay; // Allocates first free timer
if (!delay.valid()) {
iprintf("ERROR: No hardware timers available\n");
return;
}
See also
valid() To verify successful timer allocation
FIRST_UNUSED_TIMER Default timer selection constant

◆ ~DelayObject()

DelayObject::~DelayObject ( )

Destructor - releases the allocated hardware timer.

Automatically frees the hardware timer back to the system pool when the object is destroyed or goes out of scope.

Note
The timer is freed even if a delay is in progress, though this should be avoided in normal operation.

Member Function Documentation

◆ _wake()

void DelayObject::_wake ( )
inline

Internal ISR callback to wake blocked task (internal use only)

This function is called by the timer interrupt service routine when the delay period expires. It posts the internal semaphore to unblock the task waiting in DelayUsec().

Warning
This function is for internal use only and should never be called by user code. It is public only for ISR access.
Note
The leading underscore indicates internal/private usage

◆ DelayUsec()

void DelayObject::DelayUsec ( uint32_t usec)

Perform a blocking delay with microsecond precision.

Blocks the calling task for the specified number of microseconds using a hardware timer. The task is suspended until the timer expires.

Parameters
usecNumber of microseconds to delay (range: 0 to 4,294,967,295)
Precondition
The DelayObject must be valid (check with valid())
Postcondition
The calling task resumes after approximately usec microseconds
Warning
Do not call this function if valid() returns false
Very short delays (<10us) may have reduced accuracy due to function call and setup overhead
Note
The actual delay may be slightly longer than requested due to:
  • Interrupt latency
  • Task scheduling overhead
  • System load
Delay Range Guidelines
  • 1-10 us: Use only if hardware timing is critical
  • 10-999 us: Ideal range for high-resolution delays
  • 1-20 ms: Consider using OSTimeDly() instead for efficiency
  • >20 ms: Strongly recommend OSTimeDly() to free hardware timer
Example: Short Delays
static DelayObject delay;
delay.DelayUsec(50); // 50 microsecond delay
delay.DelayUsec(100); // 100 microsecond delay
Example: Millisecond Delays
static DelayObject delay;
delay.DelayUsec(1000); // 1 millisecond = 1000 microseconds
delay.DelayUsec(2500); // 2.5 milliseconds
See also
OSTimeDly() For delays >= 20ms (more efficient)
valid() Check before calling this function

◆ valid()

bool DelayObject::valid ( )
inline

Verify the delay object is valid and ready to use.

Checks whether a hardware timer was successfully allocated during construction. Must be called before using DelayUsec() to avoid undefined behavior.

Returns
true if a hardware timer was successfully allocated
false if no timer was available or allocation failed
Postcondition
If false is returned, DelayUsec() must not be called
Note
A return value of false indicates all hardware timers are currently allocated. Consider:
  • Freeing unused DelayObject instances
  • Using OSTimeDly() for less critical timing
  • Redesigning to share timers between operations
Example: Proper Error Checking
if (delay.valid()) {
delay.DelayUsec(100); // Safe to use
} else {
iprintf("WARNING: No hardware timers available\n");
OSTimeDly(1); // Fallback to standard delay
}
Example: Critical Operation Guard
static DelayObject criticalDelay;
void performCriticalTiming() {
if (!criticalDelay.valid()) {
iprintf("FATAL: Cannot perform critical timing\n");
return; // Abort operation
}
// Proceed with timing-critical code
criticalDelay.DelayUsec(50);
}
bool valid()
Verify the delay object is valid and ready to use.
Definition HiResDelay.h:318
See also
DelayObject() Constructor that allocates the timer

The documentation for this class was generated from the following file: