Safe API Framework 0.1.0 build 384e6ae
Layered API framework for safety-related applications (ERTMS RBC reference targeting CENELEC EN 50128 SIL 4)
Loading...
Searching...
No Matches
Timer Module - User Guide

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; // Caller-owned storage
.period_ms = 100, // Fire every 100ms
.callback = my_timer_callback,
.user_ctx = my_context
};
rte_timer_create(&storage, &config, &timer);
struct rte_timer_impl_s * rte_timer_handle_t
Definition rte_timer.h:31
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.
Definition rte_timer.c:44
@ RTE_TIMER_MODE_PERIODIC
Definition rte_timer.h:37
Configuration for rte_timer_create().
Definition rte_timer.h:53

2. Start Timer

// Timer now running, will fire every 100ms
rte_status_t rte_timer_start(rte_timer_handle_t handle)
Starts (or restarts) a created timer.
Definition rte_timer.c:79

3. Implement Callback

void my_timer_callback(rte_timer_handle_t handle, void *user_ctx) {
app_t *app = (app_t *)user_ctx;
app->timer_fired_count++;
// Do work - keep it short, runs in callback context
}

Timer Modes

ONE_SHOT - Fire once after delay, then stop
PERIODIC - Fire repeatedly at interval

Practical Examples

Example 1: Periodic Heartbeat

void heartbeat_callback(rte_timer_handle_t handle, void *ctx) {
app_t *app = (app_t *)ctx;
send_heartbeat_message();
app->heartbeat_count++;
}
void app_init(app_t *app) {
rte_timer_config_t config = {
.period_ms = 1000, // Every second
.callback = heartbeat_callback,
.user_ctx = app
};
rte_timer_create(&app->heartbeat_storage, &config, &app->heartbeat_timer);
rte_timer_start(app->heartbeat_timer);
}

Example 2: One-Shot Timeout

void timeout_callback(rte_timer_handle_t handle, void *ctx) {
log_warning("Operation timeout!");
// Timer fired once and stopped
}
void operation_with_timeout(void) {
rte_timer_config_t config = {
.period_ms = 5000, // 5 second timeout
.callback = timeout_callback,
.user_ctx = NULL
};
rte_timer_create(&timeout_storage, &config, &timer);
// Do operation...
if (operation_completed) {
rte_timer_stop(timer); // Cancel timeout
}
// If operation takes >5s, timeout fires
}
rte_status_t rte_timer_stop(rte_timer_handle_t handle)
Stops a running timer; safe to call on an already-stopped timer.
Definition rte_timer.c:105
@ RTE_TIMER_MODE_ONE_SHOT
Definition rte_timer.h:36

Timer Operations

rte_timer_create(storage, config, out_handle) // Create
rte_timer_start(handle) // Start countdown
rte_timer_stop(handle) // Stop (idempotent)
rte_timer_destroy(handle) // Release resources
rte_timer_now_ms(out_now_ms) // Get current time
rte_status_t rte_timer_destroy(rte_timer_handle_t handle)
Destroys a timer, releasing any OSAdapter resources bound to it.
Definition rte_timer.c:122

Best Practices

  1. Create at startup, before use
  2. Keep callbacks short
    • Runs in callback context (may be ISR)
    • Minimize time in callback
    • Do real work in main loop
  3. Use appropriate period for mode
    • PERIODIC: main loop cycle time (10-100ms typical)
    • ONE_SHOT: operation deadline
  4. Stop before destroying

See Also