NetBurner 3.5.6
PDF Version |
Functions | |
int | IntervalOSSem (OS_SEM *p_toSem, int num_per_sec, int timer=FIRST_UNUSED_TIMER) |
Create a periodic timer that posts to an RTOS semaphore. | |
int | IntervalOSFlag (OS_FLAGS *p_toFlag, uint32_t flag_value, int num_per_sec, int timer=FIRST_UNUSED_TIMER) |
Create a periodic timer that sets RTOS flags. | |
int | IntervalInterruptCallback (void(*p_toCallbackFunc)(), int num_per_sec, int timer=FIRST_UNUSED_TIMER) |
Create a periodic timer that calls a function from interrupt context. | |
void | IntervalStop (int timer_number) |
Stop and release an interval timer. | |
#include< IntervalTimer.h>
The Interval Timer API provides hardware-based periodic interrupts for real-time applications. Unlike software timers, these use dedicated hardware timer peripherals to generate precise, low-jitter periodic events.
Callback functions execute in interrupt context with the following restrictions:
Frequency | Use Case | Callback Execution Time |
---|---|---|
20-100 Hz | Display updates, slow sensors | < 100 us |
100-1000 Hz | Control loops, fast sampling | < 10 us |
1000+ Hz | High-speed I/O, signal generation | < 1 us |
int IntervalInterruptCallback | ( | void(* | p_toCallbackFunc )(), |
int | num_per_sec, | ||
int | timer = FIRST_UNUSED_TIMER ) |
#include <IntervalTimer.h>
Create a periodic timer that calls a function from interrupt context.
Creates a hardware interval timer that calls the specified callback function at the requested frequency. The callback executes in interrupt context, providing the lowest possible latency and jitter for time-critical operations.
Use with extreme caution. Interrupt callbacks have strict requirements and restrictions. They are appropriate for simple, fast operations like toggling GPIO pins, reading sensors, or updating counters. For most periodic tasks, IntervalOSSem() or IntervalOSFlag() are safer and more appropriate choices.
Your callback function MUST NOT:
Your callback function SHOULD:
p_toCallbackFunc | Pointer to the callback function. Must have signature: void MyCallback(void). The function will be called from interrupt context at the specified frequency. Must remain valid (not be deleted) while timer is active. |
num_per_sec | Callback frequency in Hz (calls per second). Valid range is 20 to platform-dependent maximum. Consider:
|
timer | Hardware timer number to use. Options:
|
Example - Simple counter increment (safe):
Example - GPIO toggling for timing signal (safe):
Example - ADC sampling with semaphore post (safe):
Example - WRONG - Do not do this (unsafe):
Example - Measuring callback overhead:
Example - Proper synchronization with tasks:
int IntervalOSFlag | ( | OS_FLAGS * | p_toFlag, |
uint32_t | flag_value, | ||
int | num_per_sec, | ||
int | timer = FIRST_UNUSED_TIMER ) |
#include <IntervalTimer.h>
Create a periodic timer that sets RTOS flags.
Creates a hardware interval timer that sets the specified flag bit(s) in an OS_FLAGS object at the requested frequency. This is ideal for event notification where multiple event sources need to signal a single task, or when you need to distinguish between different types of periodic events.
Unlike semaphores which simply count posts, flags maintain distinct bit states allowing a task to wait on multiple different events simultaneously and determine which event(s) occurred.
p_toFlag | Pointer to an OS_FLAGS object. The flags object need not be initialized before use (has no Init() method). Must remain valid for the lifetime of the interval timer. |
flag_value | The flag bit(s) to set on each timer period. Can be a single bit (e.g., 0x0001) or multiple bits (e.g., 0x0003). Use bitwise OR to combine multiple flags. Bits are set, not toggled. Common patterns:
|
num_per_sec | Flag setting frequency in Hz (sets per second). Valid range is 20 to platform-dependent maximum (typically 10,000+). Higher frequencies increase CPU overhead. Common values:
|
timer | Hardware timer number to use. Options:
|
Example - Basic flag-based periodic event:
Example - Multiple periodic events with different rates:
Example - Combining timer events with other events:
Example - Watchdog pattern:
int IntervalOSSem | ( | OS_SEM * | p_toSem, |
int | num_per_sec, | ||
int | timer = FIRST_UNUSED_TIMER ) |
#include <IntervalTimer.h>
Create a periodic timer that posts to an RTOS semaphore.
Creates a hardware interval timer that posts to the specified semaphore at the requested frequency. This is the preferred method for periodic task synchronization, as it allows tasks to block efficiently on the semaphore while the hardware timer ensures precise timing.
The timer uses a hardware peripheral to generate periodic interrupts. Each interrupt posts to the semaphore, waking any task waiting on it. This provides low-jitter, precise periodic events for time-critical operations.
p_toSem | Pointer to an initialized OS_SEM object. The semaphore must be initialized with Init() before passing to this function. Must remain valid for the lifetime of the interval timer. |
num_per_sec | Posting frequency in Hz (posts per second). Valid range is 20 to platform-dependent maximum (typically 10,000+). Higher frequencies increase CPU overhead. Common values:
|
timer | Hardware timer number to use. Options:
|
Example - Basic periodic task:
Example - Multiple tasks synchronized to one timer:
Example - Error handling:
Example - Timeout with periodic check:
void IntervalStop | ( | int | timer_number | ) |
#include <IntervalTimer.h>
Stop and release an interval timer.
Stops the specified interval timer and frees the associated hardware timer resource, making it available for reuse. After calling this function, the timer will no longer generate interrupts, post to semaphores, set flags, or call callbacks.
Always call this function when you no longer need an interval timer. Failing to stop timers causes resource leaks, eventually exhausting the limited number of hardware timers available on the platform.
This function is safe to call from any context (task or interrupt) and is safe to call multiple times on the same timer number (subsequent calls have no effect).
timer_number | The timer number returned by IntervalOSSem(), IntervalOSFlag(), or IntervalInterruptCallback(). Valid timer numbers are non-negative integers (0, 1, 2, etc.). Negative values are ignored (function returns immediately). |
Example - Basic cleanup:
Example - RAII-style resource management:
Example - Multiple timers management:
Example - Mode switching:
Example - Safe shutdown:
Example - Error recovery: