Skip to content

File syn_timer.h

FileList > sched > syn_timer.h

Go to the source code of this file

Software timer service — one-shot and periodic timers. More...

  • #include "../common/syn_defs.h"
  • #include <stdbool.h>

Classes

Type Name
struct SYN_Timer
Software timer descriptor.

Public Types

Type Name
typedef void(* SYN_TimerCallback
Timer expiry callback.

Public Functions

Type Name
bool syn_timer_expired (SYN_Timer * timer)
Poll whether the timer has expired.
void syn_timer_init (SYN_Timer * timer, uint32_t period_ms, bool periodic, SYN_TimerCallback callback, void * user_data)
Initialize a software timer.
uint32_t syn_timer_next_expiry (const SYN_Timer * timers, size_t count)
Get the earliest expiry tick among a set of active timers.
uint32_t syn_timer_remaining (const SYN_Timer * timer)
Get the remaining time until the next expiry.
void syn_timer_service (SYN_Timer * timers, size_t count)
Service an array of timers.
void syn_timer_set_period (SYN_Timer * timer, uint32_t period_ms)
Change the timer's period. Takes effect on next start or next periodic reload.
void syn_timer_start (SYN_Timer * timer)
Start (or restart) the timer.
void syn_timer_stop (SYN_Timer * timer)
Stop the timer. It will not fire until started again.

Public Static Functions

Type Name
bool syn_timer_is_active (const SYN_Timer * timer)
Check if a timer is currently active (running).

Detailed Description

Timers are caller-owned structs with a callback. They can be used with or without the scheduler. In a bare main loop, call syn_timer_service() each iteration. With the scheduler, run it from a high-priority task.

Timers are driven by syn_port_get_tick_ms() — resolution depends on how often you call syn_timer_service().

** **

static SYN_Timer heartbeat;

void on_heartbeat(SYN_Timer *t, void *ctx) {
    syn_gpio_toggle(LED_PIN);
}

syn_timer_init(&heartbeat, 1000, true, on_heartbeat, NULL);
syn_timer_start(&heartbeat);

while (1) {
    syn_timer_service(&heartbeat, 1);
}

Public Types Documentation

typedef SYN_TimerCallback

Timer expiry callback.

typedef void(* SYN_TimerCallback) (struct SYN_Timer *timer, void *user_data);

Parameters:

  • timer The timer that expired.
  • user_data User-provided context pointer.

Public Functions Documentation

function syn_timer_expired

Poll whether the timer has expired.

bool syn_timer_expired (
    SYN_Timer * timer
) 

If expired and periodic, the timer is automatically re-armed. If expired and one-shot, the timer is stopped.

This can be used instead of (or in addition to) the callback.

Parameters:

  • timer Timer to check.

Returns:

true if the timer has expired since the last check.


function syn_timer_init

Initialize a software timer.

void syn_timer_init (
    SYN_Timer * timer,
    uint32_t period_ms,
    bool periodic,
    SYN_TimerCallback callback,
    void * user_data
) 

The timer is created in the stopped state. Call syn_timer_start() to begin counting.

Parameters:

  • timer Timer to initialize.
  • period_ms Period in milliseconds.
  • periodic true for repeating timer, false for one-shot.
  • callback Function called on expiry (may be NULL if using polling).
  • user_data User context pointer passed to callback.

function syn_timer_next_expiry

Get the earliest expiry tick among a set of active timers.

uint32_t syn_timer_next_expiry (
    const SYN_Timer * timers,
    size_t count
) 

Scans the timer array and returns the minimum target_tick of any active timer whose deadline is in the future. Used by the tickless scheduler to account for software timer wakeups.

Parameters:

  • timers Array of timers to scan.
  • count Number of timers in the array.

Returns:

Earliest expiry tick, or UINT32_MAX if no timers are active or all have already expired.


function syn_timer_remaining

Get the remaining time until the next expiry.

uint32_t syn_timer_remaining (
    const SYN_Timer * timer
) 

Parameters:

  • timer Timer to query.

Returns:

Remaining milliseconds, or 0 if expired/stopped.


function syn_timer_service

Service an array of timers.

void syn_timer_service (
    SYN_Timer * timers,
    size_t count
) 

Checks each timer for expiry and calls its callback if set. Call this from your main loop, a scheduler task, or a periodic ISR.

Parameters:

  • timers Array of timers.
  • count Number of timers in the array.

function syn_timer_set_period

Change the timer's period. Takes effect on next start or next periodic reload.

void syn_timer_set_period (
    SYN_Timer * timer,
    uint32_t period_ms
) 

Parameters:

  • timer Timer.
  • period_ms New period in milliseconds.

function syn_timer_start

Start (or restart) the timer.

void syn_timer_start (
    SYN_Timer * timer
) 

Sets the target tick to now + period_ms and marks the timer active.

Parameters:

  • timer Timer to start.

function syn_timer_stop

Stop the timer. It will not fire until started again.

void syn_timer_stop (
    SYN_Timer * timer
) 

Parameters:

  • timer Timer to stop.

Public Static Functions Documentation

function syn_timer_is_active

Check if a timer is currently active (running).

static inline bool syn_timer_is_active (
    const SYN_Timer * timer
) 

Parameters:

  • timer Timer to query.

Returns:

true if active.



The documentation for this class was generated from the following file src/syntropic/sched/syn_timer.h