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
blinky/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 * Four tasks (RED, ORANGE, GREEN, BLUE) share one stk::sync::EventFlags object with one flag bit per
18 * task. A task blocks until its own flag is set (the flag is auto-cleared on wake-up), lights its LED
19 * exclusively, sleeps for 1 s and then sets the flag of the next task. Sleeping is done until an exact
20 * timestamp on a running timeline, which makes the LED switching drift-free.
21 */
22
23#include <stk.h>
25#include "example.h"
26
27using namespace bsp;
28
29// R2350 requires larger stack due to stack-memory heavy SDK API
30#ifdef _PICO_H
31enum { TASK_STACK_SIZE = 1024 };
32#else
33enum { TASK_STACK_SIZE = 256 };
34#endif
35
36// One flag bit per LED task; task 0 (RED) goes first
37static const uint32_t FLAGS_ALL[] = {
38 (1U << LED_RED),
39 (1U << LED_ORANGE),
40 (1U << LED_GREEN),
41 (1U << LED_BLUE)
42};
43
44// Start with the RED task's flag set so it runs first
46
47// Timeline for a precise LED switching
49
50// Task's core (thread)
51template <stk::EAccessMode _AccessMode>
52class MyTask : public stk::Task<TASK_STACK_SIZE, _AccessMode>
53{
54 uint8_t m_task_id;
55 uint32_t m_my_flag;
56 uint32_t m_next_flag;
57
58public:
59 MyTask(uint8_t task_id) : m_task_id(task_id), m_my_flag(FLAGS_ALL[task_id]),
60 m_next_flag(FLAGS_ALL[(task_id + 1) % LED_MAX])
61 {}
62
63private:
64 void Run() override
65 {
66 // we switch LEDs with 250ms period
67 const stk::Ticks period = stk::GetTicksFromMs(250);
68
69 // get a start of the timeline
71
72 while (true)
73 {
74 // block until this task's flag is set; auto-cleared on return
77 continue;
78
79 // change active LED
80 {
83 }
84
85 // sleep 1s drift-free and then delegate work to the next task
86 // we could use simple stk::Sleep() but due to other work around Sleep call we
87 // will get a time drift, STK allows to sleep until exact timestamp making it
88 // possible precise sleeping with 1 tick precision, you could also use
89 // time::TimerHost for timer-related tasks (see related 'timer' example)
90 stk::SleepUntil(g_Timeline += period);
91
92 // hand off to the next task
94 }
95 }
96};
97
99{
100 using namespace stk;
101
102 Led::InitAll(false);
103
104 // operating in Static + Sync mode (EventFlags requires KERNEL_SYNC) and optionally tickless
105 const uint8_t KernelMode = KERNEL_STATIC | KERNEL_SYNC | (STK_TICKLESS_IDLE ? KERNEL_TICKLESS : 0);
106
107 // allocate scheduling kernel for 3 threads (tasks) with Round-Robin scheduling strategy
109
110 // note: using ACCESS_PRIVILEGED as Cortex-M3+ may not allow writing to GPIO from a less secure user thread
115
116 // init scheduling kernel
117 kernel.Initialize();
118
119 // register threads (tasks)
120 kernel.AddTask(&task1);
121 kernel.AddTask(&task2);
122 kernel.AddTask(&task3);
123 kernel.AddTask(&task4);
124
125 // start scheduler (it will start threads added by AddTask), execution in main() will be blocked on this line
126 kernel.Start();
127
128 // shall not reach here after Start() was called
129 STK_ASSERT(false);
130}
LedId
Definition led.h:16
@ LED_ORANGE
Definition led.h:18
@ LED_GREEN
Definition led.h:17
@ LED_MAX
Definition led.h:22
@ LED_BLUE
Definition led.h:20
@ LED_RED
Definition led.h:19
Top-level STK include. Provides the Kernel class template and all built-in task-switching strategies.
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:532
Implementation of synchronization primitive: stk::sync::EventFlagsT and stk::sync::EventFlags.
Namespace of STK package.
int64_t Ticks
Ticks value.
Definition stk_common.h:221
static bool SleepUntil(Ticks timestamp)
Put calling process into a sleep state until the specified timestamp.
Definition stk_helper.h:523
static Ticks GetTicksFromMs(Time ms, uint32_t resolution)
Convert milliseconds to ticks.
Definition stk_helper.h:406
static Ticks GetTicks()
Get number of ticks elapsed since kernel start.
Definition stk_helper.h:448
@ 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_STATIC
All tasks are static and can not exit.
Definition stk_common.h:47
EventFlagsT< uint32_t > EventFlags
Default event flags group with a 32-bit flags word (31 usable flags, bit 31 reserved).
Definition cpu.h:17
Concrete implementation of IKernel.
Definition stk.h:102
void Start() override
Start the scheduler. This call does not return until all tasks have exited (KERNEL_DYNAMIC mode) or i...
Definition stk.h:1999
EAddResult AddTask(ITask *user_task) override
Register task for a soft real-time (SRT) scheduling.
Definition stk.h:1465
void Initialize(uint32_t resolution_us=PERIODICITY_DEFAULT) override
Initialize kernel.
Definition stk.h:1425
RAII instance that enters the critical section on construction and exits it on destruction.
Definition stk_arch.h:495
Partial implementation of the user task.
Definition stk_helper.h:98
static constexpr uint32_t OPT_WAIT_ANY
static bool IsError(uint32_t result)
MyTask(uint8_t task_id)
void Run() override
Entry point of the user task.
static void InitAll(bool init_state)
Definition led.h:40
static void SwitchOnExclusive(Id led)
Definition led.h:48
static stk::Ticks g_Timeline
static const uint32_t FLAGS_ALL[]
static stk::sync::EventFlags g_TaskFlags(FLAGS_ALL[LED_RED])
void RunExample()
static stk::sync::EventFlags g_TaskFlags(FLAGS_ALL[LED_RED])
#define TASK_STACK_SIZE
static stk::Ticks g_Timeline