SuperTinyKernel™ RTOS 1.08.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
timer_c/example.c
/*
* SuperTinyKernel™ (STK): Lightweight High-Performance Deterministic C++ RTOS for Embedded Systems.
*
* Source: https://github.com/SuperTinyKernel-RTOS
*
* Copyright (c) 2022-2026 Neutron Code Limited <stk@neutroncode.com>. All Rights Reserved.
* License: MIT License, see LICENSE for a full text.
*/
/*
* C version of the \ref timer/example.cpp example using stk_c_time.h: a periodic timer toggles the green
* LED every 1 s and a one-shot timer shuts the timer host down.
*/
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <stk_c.h>
#include <stk_c_time.h>
#include "example.h"
// ---------------------------------------------------------------------------
// LED timer callback — toggles green LED every 1 s
// ---------------------------------------------------------------------------
static bool s_LedToggle = false;
// Memory of the timers (must outlive the timers)
stk_timer_t *timer,
void *user_data)
{
(void)host;
(void)timer;
(void)user_data;
}
// ---------------------------------------------------------------------------
// Shutdown timer callback — stops the timer host after 20 s
// ---------------------------------------------------------------------------
stk_timer_t *timer,
void *user_data)
{
(void)timer;
(void)user_data;
Led_InitAll(true);
}
// ---------------------------------------------------------------------------
// RunExample
// ---------------------------------------------------------------------------
void RunExample(void)
{
Led_InitAll(false);
// Create kernel for core 0
// Obtain timer host for core 0 and initialize it
stk_timerhost_init(timers, kernel, /*privileged=*/true);
// Helper: convert milliseconds to ticks using current tick resolution
int32_t res = stk_tick_resolution(); /* microseconds per tick */
// Create and start LED timer — periodic, fires every 1 s
stk_timer_start(timers, led_timer,
0, /* no initial delay */
(uint32_t)stk_ticks_from_ms_r(1000, res)); /* 1000 ms -> ticks */
// Create and start shutdown timer — one-shot, fires once after 20 s
stk_timer_start(timers, shutdown_timer,
(uint32_t)stk_ticks_from_ms_r(20000, res), /* 20000 ms -> ticks */
0); /* one-shot */
// Start the kernel — never returns
Led_InitAll(false);
for (;;) {}
}
void Led_InitAll(bool state)
Definition c_wrapper.cpp:27
void Led_Set(LedId led, bool state)
Definition c_wrapper.cpp:22
@ LED_GREEN
Definition led.h:17
C language binding/interface for SuperTinyKernel RTOS.
C language binding for stk::time::TimerHost and stk::time::Timer.
struct stk_kernel_t stk_kernel_t
Opaque handle to a kernel instance.
Definition stk_c.h:188
void stk_kernel_start(stk_kernel_t *k)
Start the scheduler - never returns.
Definition stk_c.cpp:478
static stk_tick_t stk_ticks_from_ms_r(stk_time_t msec, uint32_t resolution)
Get ticks from milliseconds using an explicit tick resolution.
Definition stk_c.h:869
uint32_t stk_tick_resolution(void)
Returns how many microseconds correspond to one kernel tick.
Definition stk_c.cpp:790
#define STK_PERIODICITY_DEFAULT
Default tick period (1 ms).
Definition stk_c.h:196
void stk_kernel_init(stk_kernel_t *k, uint32_t tick_period_us)
Initialize kernel with given tick period.
Definition stk_c.cpp:471
stk_kernel_t * stk_kernel_create(uint8_t core_nr)
Create kernel.
Definition stk_c.cpp:403
stk_timer_result_t stk_timer_start(stk_timerhost_t *host, stk_timer_t *tmr, uint32_t delay, uint32_t period)
Start a timer.
stk_timer_result_t stk_timerhost_shutdown(stk_timerhost_t *host)
Gracefully shut down the TimerHost.
void stk_timerhost_init(stk_timerhost_t *host, stk_kernel_t *kernel, bool privileged)
Initialize the TimerHost and register its internal tasks with the kernel.
stk_timer_t * stk_timer_create(stk_timer_mem_t *const membuf, stk_timer_callback_t callback, void *user_data)
Construct a timer in the supplied memory buffer.
stk_timerhost_t * stk_timerhost_get(uint8_t core_nr)
Obtain the pre-allocated TimerHost for the given CPU core.
Opaque memory container for a stk_timer_t instance.
Definition stk_c_time.h:130
static bool s_LedToggle
static void on_led_timer_expired(stk_timerhost_t *host, stk_timer_t *timer, void *user_data)
static stk_timer_mem_t s_LedTimerMem
static void on_shutdown_timer_expired(stk_timerhost_t *host, stk_timer_t *timer, void *user_data)
static stk_timer_mem_t s_ShutdownTimerMem