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/example.cpp
Go to the documentation of this file.
1/*
2 * SuperTinyKernel(TM) RTOS: Lightweight High-Performance Deterministic C++ RTOS for Embedded Systems.
3 *
4 * Source: https://github.com/SuperTinyKernel-RTOS
5 *
6 * Copyright (c) 2022-2026 Neutron Code Limited <stk@neutroncode.com>. All Rights Reserved.
7 * License: MIT License, see LICENSE for a full text.
8 */
9
15
16/*
17 * A periodic timer toggles the green LED every 100 ms and a one-shot timer stops the timer host after
18 * 20 s. Timers are driven by the stk::time::TimerHost, which runs in its own internal task(s).
19 */
20
21//#define _STK_ASSERT_REDIRECT
22
23#include <stk.h>
24#include <time/stk_time.h>
25#include "example.h"
26
27#ifdef _STK_ASSERT_REDIRECT
28#include <stdint.h>
29extern void STK_ASSERT_HANDLER(const char *err, const char *source, int32_t line);
30#endif
31
32using namespace bsp;
33
34// R2350 requires larger stack due to stack-memory heavy SDK API
35#ifdef _PICO_H
36enum { TASK_STACK_SIZE = 1024 };
37#else
38enum { TASK_STACK_SIZE = 256 };
39#endif
40
41#ifdef _STK_ASSERT_REDIRECT
42void STK_ASSERT_HANDLER(const char *err, const char *source, int32_t line)
43{
45 while (true) {}
46}
47#endif
48
50
51// ---------------------------------------------------------------------------
52// LED timer callback — toggles green LED every 100 ms
53// ---------------------------------------------------------------------------
54
56{
57 uint8_t m_led;
58
60 {}
61
63 {
65 m_led = (m_led + 1) % LED_MAX;
66 }
67};
68
69// ---------------------------------------------------------------------------
70// Shutdown timer callback — stops the timer host after 20 s
71// ---------------------------------------------------------------------------
72
74{
76 {
77 Led::InitAll(true);
78
79 host->Shutdown();
80 }
81};
82
83// ---------------------------------------------------------------------------
84// RunExample
85// ---------------------------------------------------------------------------
86
88{
89 Led::InitAll(false);
90
91 static stk::Kernel<stk::KERNEL_DYNAMIC | stk::KERNEL_SYNC | (STK_TICKLESS_IDLE ? stk::KERNEL_TICKLESS : 0),
93
94 static LedTimer LedTimer;
96
97 g_Kernel.Initialize();
99 g_Timers.Start(LedTimer, 0, stk::GetTicksFromMs(100)); // periodic timer, triggered every 100ms
100 g_Timers.Start(ShutdownTimer, stk::GetTicksFromMs(20000), 0); // one-shot timer, triggered once in 20s
101 g_Kernel.Start();
102
103 Led::InitAll(false);
104
105 for (;;) {}
106}
@ LED_MAX
Definition led.h:22
Top-level STK include. Provides the Kernel class template and all built-in task-switching strategies.
static void __stk_debug_break()
Definition stk_defs.h:495
Collection of time-related primitives (stk::time namespace).
@ ACCESS_PRIVILEGED
Privileged access mode (access to hardware is fully unrestricted).
Definition stk_common.h:38
static Ticks GetTicksFromMs(Time ms, uint32_t resolution)
Convert milliseconds to ticks.
Definition stk_helper.h:406
PlatformArmCortexM PlatformDefault
Default platform implementation.
@ KERNEL_TICKLESS
Tickless mode. To use this mode STK_TICKLESS_IDLE must be defined to 1 in stk_config....
Definition stk_common.h:51
@ KERNEL_SYNC
Synchronization support (see Event).
Definition stk_common.h:50
@ KERNEL_DYNAMIC
Tasks can be added or removed and therefore exit when done.
Definition stk_common.h:48
SwitchStrategyRoundRobinT< false > SwitchStrategyRR
Shorthand alias for SwitchStrategyRoundRobin.
Definition cpu.h:17
Concrete implementation of IKernel.
Definition stk.h:102
Software timer multiplexer that manages multiple Timer instances on top of a small fixed set of kerne...
EResult Shutdown()
Shutdown host instance. All timers are stopped and removed from the host.
@ TASK_COUNT
total number of tasks serving this instance
::stk::time::Timer Timer
Alias of stk::time::Timer, keeps the former nested name TimerHost::Timer valid.
LedId Id
Definition led.h:32
static void InitAll(bool init_state)
Definition led.h:40
static void SwitchOnExclusive(Id led)
Definition led.h:48
uint8_t m_led
void OnExpired(stk::time::TimerHost *host)
Callback invoked when this timer expires.
void OnExpired(stk::time::TimerHost *host)
Callback invoked when this timer expires.
static stk::time::TimerHost g_Timers
#define TASK_STACK_SIZE
static stk_kernel_t * g_Kernel
void RunExample()