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
once/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 of a KERNEL_DYNAMIC kernel take their turn one by one: each task lights its LED exclusively
18 * for 1 s and then exits (returns from Run()). Once all tasks have exited, kernel.Start() returns and the
19 * sequence is repeated, finally all LEDs are switched on.
20 */
21
22#include <stk.h>
23#include "example.h"
24
25using namespace bsp;
26
27static volatile uint8_t g_TaskSwitch = 0;
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
36template <stk::EAccessMode _AccessMode>
37class MyTask : public stk::Task<TASK_STACK_SIZE, _AccessMode>
38{
39 uint8_t m_task_id;
40
41public:
42 MyTask(uint8_t task_id) : m_task_id(task_id)
43 {}
44
45private:
46 void Run()
47 {
48 uint8_t task_id = m_task_id;
49
50 while (true)
51 {
52 if (g_TaskSwitch != task_id)
53 {
54 stk::Sleep(100);
55 continue;
56 }
57
58 Led::SwitchOnExclusive(static_cast<LedId>(task_id));
59
60 stk::Sleep(1000);
61
62 g_TaskSwitch = (task_id + 1) % LED_MAX;
63 return;
64 }
65 }
66};
67
69
70// note: using ACCESS_PRIVILEGED as some MCUs may not allow writing to GPIO from a user thread, such as i.MX RT1050 (Arm Cortex-M7)
76
77static void RunOnce()
78{
79 g_Kernel.AddTask(&g_Task1);
80 g_Kernel.AddTask(&g_Task2);
81 g_Kernel.AddTask(&g_Task3);
82 g_Kernel.AddTask(&g_Task4);
83
84 g_TaskSwitch = 0;
85
86 // note: kernel will exit from Start() once all tasks exit (complete their work)
87 g_Kernel.Start();
88}
89
91{
92 using namespace stk;
93
94 Led::InitAll(false);
95
96 g_Kernel.Initialize();
97
98 // repeat 3 times
99 for (int32_t i = 0; i < 3; ++i)
100 {
101 RunOnce();
102 }
103
104 // switched on all LEDs when execution ends
105 Led::InitAll(true);
106
107 while (true) {}
108}
LedId
Definition led.h:16
@ LED_MAX
Definition led.h:22
Top-level STK include. Provides the Kernel class template and all built-in task-switching strategies.
Namespace of STK package.
static bool Sleep(Timeout tick_count)
Put calling process into a sleep state.
Definition stk_helper.h:495
Definition cpu.h:17
Concrete implementation of IKernel.
Definition stk.h:102
Partial implementation of the user task.
Definition stk_helper.h:98
void Run()
Entry point of the user task.
MyTask(uint8_t task_id)
static constexpr Id GREEN
Definition led.h:34
static constexpr Id RED
Definition led.h:36
static constexpr Id ORANGE
Definition led.h:35
static constexpr Id BLUE
Definition led.h:37
static void InitAll(bool init_state)
Definition led.h:40
static void SwitchOnExclusive(Id led)
Definition led.h:48
#define TASK_STACK_SIZE
static stk_kernel_t * g_Kernel
static volatile uint8_t g_TaskSwitch
static void RunOnce()
static MyTask< stk::ACCESS_PRIVILEGED > g_Task3(Led::GREEN)
static MyTask< stk::ACCESS_PRIVILEGED > g_Task1(Led::RED)
static MyTask< stk::ACCESS_PRIVILEGED > g_Task4(Led::BLUE)
static MyTask< stk::ACCESS_PRIVILEGED > g_Task2(Led::ORANGE)
void RunExample()