API Reference

struct Timer
#include <timer.h>

Timer structure containing timer state.

Public Members

uint32_t start

Start timestamp when timer was set

uint32_t interval

Timer interval in clock ticks

file timer.h
#include <stdbool.h>
#include <stdint.h>

Simple timer module for embedded systems.

This module provides basic timer functionality for measuring time intervals and detecting timeout conditions in embedded applications.

Features:

  • One-shot timers that automatically deactivate after expiration

  • External clock source abstraction via callback

  • Overflow-safe arithmetic using unsigned integer operations

  • Lightweight implementation suitable for resource-constrained systems

Usage example:

Timer myTimer;
Timer_Init(System_Millis);
Timer_Set(&myTimer, 1000); // 1 second timeout

// Later in main loop
if (Timer_IsExpired(&myTimer)) {
    // Handle timeout
}

// Or check remaining time
uint32_t remaining = Timer_Remaining(&myTimer);
if (remaining > 0) {
    // Timer still running
}

Typedefs

typedef uint32_t (*Timer_ClockTime)(void)

Function pointer type for clock time provider.

Return:

Current time in system ticks (typically milliseconds)

Functions

void Timer_Init(Timer_ClockTime clock)

Initializes the timer module with clock source.

This function must be called before using any timer functionality. The clock source function will be called to obtain current time.

Stores the clock function pointer for later use by timer operations. This function must be called before any timer functionality is used.

Parameters:
  • clock – Function pointer to clock time provider

  • clock – Function pointer to system clock provider

void Timer_Set(Timer *timer, uint32_t interval)

Sets and starts a timer with specified interval.

Configures the timer to expire after the specified interval. The timer becomes active and starts counting from current time.

Sets and starts a timer with specified interval.

Captures the current time as start reference and configures the timer to expire after the specified interval. The timer will be monitored by Timer_IsExpired().

Parameters:
  • timer – Pointer to timer structure

  • intervalTimer interval in clock ticks

  • timer – Pointer to timer structure to configure

  • interval – Timeout interval in clock ticks

bool Timer_IsExpired(Timer *timer)

Checks if timer has expired.

Tests whether the timer interval has elapsed since it was set. If the timer has expired, it is automatically deactivated. Inactive timers always return false.

Checks if timer has expired.

Compares elapsed time since timer start with the configured interval. Uses unsigned arithmetic which is overflow-safe for 32-bit counters. When timer expires, it is automatically deactivated to prevent multiple expiration notifications.

Note

This function uses overflow-safe arithmetic, so it works correctly even when the system clock wraps around.

Parameters:
  • timer – Pointer to timer structure

  • timer – Pointer to timer structure to check

Returns:

true if timer has expired, false otherwise

Returns:

true if timer was active and has expired, false otherwise

uint32_t Timer_Remaining(Timer *timer)

Returns remaining time until timer expiration.

Calculates how many clock ticks remain until the timer expires. If the timer has already expired, returns 0.

Calculates how many clock ticks remain until the timer expires. Uses unsigned arithmetic which is overflow-safe for 32-bit counters. If the timer has already expired, returns 0.

Note

This function uses overflow-safe arithmetic, so it works correctly even when the system clock wraps around.

Parameters:
  • timer – Pointer to timer structure

  • timer – Pointer to timer structure to check

Returns:

Number of clock ticks remaining, or 0 if expired

Returns:

Number of clock ticks remaining, or 0 if expired

file timer.c
#include “timer.h

Timer module implementation.

Implementation of simple timer functionality for embedded systems. Uses external clock source for time measurement and provides overflow-safe timer operations.

Functions

void Timer_Init(Timer_ClockTime clock)

Initializes the timer module with clock source.

Stores the clock function pointer for later use by timer operations. This function must be called before any timer functionality is used.

Parameters:

clock – Function pointer to system clock provider

void Timer_Set(Timer *timer, uint32_t interval)

Sets and activates a timer with specified interval.

Sets and starts a timer with specified interval.

Captures the current time as start reference and configures the timer to expire after the specified interval. The timer will be monitored by Timer_IsExpired().

Parameters:
  • timer – Pointer to timer structure to configure

  • interval – Timeout interval in clock ticks

bool Timer_IsExpired(Timer *timer)

Checks if timer has expired and deactivates it.

Checks if timer has expired.

Compares elapsed time since timer start with the configured interval. Uses unsigned arithmetic which is overflow-safe for 32-bit counters. When timer expires, it is automatically deactivated to prevent multiple expiration notifications.

Parameters:

timer – Pointer to timer structure to check

Returns:

true if timer was active and has expired, false otherwise

uint32_t Timer_Remaining(Timer *timer)

Returns remaining time until timer expiration.

Calculates how many clock ticks remain until the timer expires. Uses unsigned arithmetic which is overflow-safe for 32-bit counters. If the timer has already expired, returns 0.

Parameters:

timer – Pointer to timer structure to check

Returns:

Number of clock ticks remaining, or 0 if expired

Variables

static Timer_ClockTime clockTime

Static clock time provider function pointer

dir /home/runner/work/software-timer/software-timer/include
dir /home/runner/work/software-timer/software-timer/src