What is a Timer?
Timer provides one-shot or periodic timeouts with millisecond resolution. You create a timer with callback, start it, and the framework invokes your callback when the deadline expires. Caller provides storage; no allocation.
Quick Start
1. Create Timer
rte_timer_storage_t storage;
.period_ms = 100,
.callback = my_timer_callback,
.user_ctx = my_context
};
struct rte_timer_impl_s * rte_timer_handle_t
rte_status_t rte_timer_create(rte_timer_storage_t *storage, const rte_timer_config_t *config, rte_timer_handle_t *out_handle)
Creates a timer bound to caller-owned storage. Does not start it.
@ RTE_TIMER_MODE_PERIODIC
Configuration for rte_timer_create().
2. Start Timer
rte_status_t rte_timer_start(rte_timer_handle_t handle)
Starts (or restarts) a created timer.
3. Implement Callback
app_t *app = (app_t *)user_ctx;
app->timer_fired_count++;
}
Timer Modes
ONE_SHOT - Fire once after delay, then stop
PERIODIC - Fire repeatedly at interval
Practical Examples
Example 1: Periodic Heartbeat
app_t *app = (app_t *)ctx;
send_heartbeat_message();
app->heartbeat_count++;
}
void app_init(app_t *app) {
.period_ms = 1000,
.callback = heartbeat_callback,
.user_ctx = app
};
}
Example 2: One-Shot Timeout
log_warning("Operation timeout!");
}
void operation_with_timeout(void) {
.period_ms = 5000,
.callback = timeout_callback,
.user_ctx = NULL
};
if (operation_completed) {
}
}
rte_status_t rte_timer_stop(rte_timer_handle_t handle)
Stops a running timer; safe to call on an already-stopped timer.
@ RTE_TIMER_MODE_ONE_SHOT
Timer Operations
rte_timer_now_ms(out_now_ms)
rte_status_t rte_timer_destroy(rte_timer_handle_t handle)
Destroys a timer, releasing any OSAdapter resources bound to it.
Best Practices
- Create at startup, before use
- Keep callbacks short
- Runs in callback context (may be ISR)
- Minimize time in callback
- Do real work in main loop
- Use appropriate period for mode
- PERIODIC: main loop cycle time (10-100ms typical)
- ONE_SHOT: operation deadline
- Stop before destroying
See Also