TikuOS: Simple. Ubiquitous. Intelligence, Everywhere.

An open-source operating system for microwatt computers.
Deploy-and-forget embedded systems that run for years on a coin cell or indefinitely on harvested energy.

API Reference / Timers & Clock

Module · Timers & Clock

Timers & Clock

TikuOS provides three timing primitives: the system clock (the free-running tick source), software timers (managed by a process, either event-posting or callback-driven), and the hardware timer (a single-shot, ISR-dispatched timer used when you need precise control and cannot tolerate process-dispatch latency).

Headers: kernel/timers/tiku_timer.h, kernel/timers/tiku_clock.h, kernel/timers/tiku_htimer.h.

Software Timers

Header: kernel/timers/tiku_timer.h

Unified timer supporting two modes: event (posts TIKU_EVENT_TIMER to a process) and callback (invokes a function directly). Any number of timers can be active simultaneously; they're managed as a linked list and checked each scheduler iteration.

Types

typedef void (*tiku_timer_callback_t)(void *ptr);

struct tiku_timer {
    struct tiku_timer *next;        /* Linked list pointer (internal)       */
    tiku_clock_time_t start;        /* When the timer was set               */
    tiku_clock_time_t interval;     /* Duration in clock ticks              */
    uint8_t mode;                   /* TIKU_TIMER_MODE_EVENT or _CALLBACK   */
    uint8_t active;                 /* Non-zero if in the active list       */
    struct tiku_process *p;         /* Event target or callback context     */
    tiku_timer_callback_t func;     /* Callback function (CALLBACK mode)    */
    void *ptr;                      /* User data for callback               */
};

Constants

ConstantDescription
TIKU_TIMER_SECONDOne second in timer ticks
TIKU_TIMER_MINUTEOne minute in timer ticks

Functions

FunctionDescription
tiku_timer_init()Initialize the timer subsystem. Call once at startup, after the process system is up.
tiku_timer_set_callback(t, ticks, func, ptr)Set a callback timer. Re-arms if already active.
tiku_timer_set_event(t, ticks)Set an event timer that posts TIKU_EVENT_TIMER to the calling process on expiry.
tiku_timer_reset(t)Reset for drift-free periodic operation: start = old_start + interval.
tiku_timer_restart(t)Restart anchored to current time. Use when drift doesn't matter.
tiku_timer_stop(t)Stop a timer. Safe to call even if not active.
tiku_timer_expired(t)Non-zero if expired (not in active list).
tiku_timer_remaining(t)Ticks remaining until expiration. Returns 0 if expired.
tiku_timer_expiration_time(t)Absolute tick at which this timer fires.
tiku_timer_any_pending()Non-zero if at least one timer is active.
tiku_timer_next_expiration()Nearest expiration across all timers. Useful for the scheduler to know how long it can sleep.
tiku_timer_request_poll()Request the timer process to poll. Safe to call from a clock ISR.

Examples

Event timer (most common)

static struct tiku_timer my_timer;

TIKU_PROCESS_THREAD(my_proc, ev, data)
{
    TIKU_PROCESS_BEGIN();

    tiku_timer_set_event(&my_timer, TIKU_CLOCK_SECOND);

    while (1) {
        TIKU_PROCESS_WAIT_EVENT_UNTIL(ev == TIKU_EVENT_TIMER);
        /* periodic work */
        tiku_timer_reset(&my_timer);  /* drift-free */
    }

    TIKU_PROCESS_END();
}

Callback timer

static struct tiku_timer cb_timer;

static void on_timeout(void *ptr)
{
    /* runs on next scheduler pass that notices the expired timer */
}

tiku_timer_set_callback(&cb_timer, TIKU_CLOCK_SECOND * 2, on_timeout, NULL);

System Clock

Header: kernel/timers/tiku_clock.h

Free-running tick source driven by a low-frequency clock (typically ACLK from the 32 kHz crystal). Provides the time base used by all software timers.

Types

typedef unsigned short tiku_clock_time_t;

Constants and Macros

MacroDescription
TIKU_CLOCK_SECONDNumber of clock ticks per second
TIKU_CLOCK_LT(a, b)Wraparound-safe less-than comparison
TIKU_CLOCK_DIFF(a, b)Wraparound-safe difference (a - b)
TIKU_CLOCK_MS_TO_TICKS(ms)Convert milliseconds to clock ticks

Functions

FunctionDescription
tiku_clock_init()Initialize the system clock. Call once during system boot.
tiku_clock_time()Current clock time in ticks.
tiku_clock_seconds()Current time in seconds since system start.
tiku_clock_wait(t)Busy-wait for the specified number of clock ticks.
tiku_clock_delay_usec(dt)CPU delay in microsecond-scale units. Calibration is platform-specific.

Hardware Timer

Header: kernel/timers/tiku_htimer.h

Single-shot hardware timer with ISR-context callbacks. Only one htimer can be pending at a time — setting a new one replaces any previously scheduled timer. Use this when you need microsecond-level precision or cannot tolerate the process-dispatch latency of software timers.

Types

typedef unsigned short tiku_htimer_clock_t;
typedef void (*tiku_htimer_callback_t)(struct tiku_htimer *t, void *ptr);

struct tiku_htimer {
    tiku_htimer_clock_t time;       /* Scheduled firing time (absolute)     */
    tiku_htimer_callback_t func;    /* ISR callback                         */
    void *ptr;                      /* User data for callback               */
};

Return Codes

ConstantValueDescription
TIKU_HTIMER_OK0Success
TIKU_HTIMER_ERR_TIME-1Time too close or in the past
TIKU_HTIMER_ERR_INVALID-2NULL timer or callback
TIKU_HTIMER_ERR_NONE-3No timer to cancel

Macros

MacroDescription
TIKU_HTIMER_NOW()Read current hardware timer value
TIKU_HTIMER_TIME(ht)Get the scheduled time of an htimer
TIKU_HTIMER_SECONDHardware timer ticks per second
TIKU_HTIMER_GUARD_TIMEMinimum ticks between now and scheduled time
TIKU_HTIMER_CLOCK_DIFF(a, b)Signed difference with wraparound
TIKU_HTIMER_CLOCK_LT(a, b)True if a is before b

Functions

FunctionDescription
tiku_htimer_init()Initialize the hardware timer subsystem. Call once at startup.
tiku_htimer_set(ht, time, func, ptr)Schedule a hardware timer. Only one can be active. Time must be at least TIKU_HTIMER_GUARD_TIME ticks in the future. Returns TIKU_HTIMER_OK on success.
tiku_htimer_cancel()Cancel the pending timer. Returns TIKU_HTIMER_OK if cancelled, TIKU_HTIMER_ERR_NONE if nothing was pending.
tiku_htimer_is_scheduled()Non-zero if a timer is currently scheduled.
tiku_htimer_run_next()Run the pending callback. Only call from the hardware timer ISR.
ISR context: Hardware timer callbacks run from an interrupt service routine. Keep them short — do not call blocking APIs, do not allocate, and prefer posting an event to a process for anything non-trivial.