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
sleep/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 sleep with stk::Sleep() for 1, 2, 3 and 4 s respectively, then
18 * light up their LED and exit. Once all tasks have completed, the kernel returns from Start() and all LEDs
19 * are switched on. Also shows the tickless idle mode when STK_TICKLESS_IDLE is enabled.
20 */
21
22#include <stk.h>
23#include "example.h"
24
25using namespace bsp;
26
27// R2350 requires larger stack due to stack-memory heavy SDK API
28#ifdef _PICO_H
29enum { TASK_STACK_SIZE = 1024 };
30#else
31enum { TASK_STACK_SIZE = 256 };
32#endif
33
34template <stk::EAccessMode _AccessMode>
35class MyTask : public stk::Task<TASK_STACK_SIZE, _AccessMode>
36{
37 uint8_t m_task_id;
38
39public:
40 MyTask(uint8_t task_id) : m_task_id(task_id)
41 {}
42
43private:
44 void Run() override
45 {
46 // task 0: sleep 1000 ms
47 // task 1: sleep 2000 ms
48 // task 2: sleep 3000 ms
49 // task 3: sleep 4000 ms
50 stk::Sleep(1000 * (m_task_id + 1));
51
53 }
54};
55
56static stk::Kernel<stk::KERNEL_DYNAMIC | (STK_TICKLESS_IDLE ? stk::KERNEL_TICKLESS : 0), 4,
58
59// 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)
61
62static void RunCycle()
63{
64 g_Kernel.AddTask(&g_Task1);
65 g_Kernel.AddTask(&g_Task2);
66 g_Kernel.AddTask(&g_Task3);
67 g_Kernel.AddTask(&g_Task4);
68
69 g_Kernel.Start();
70}
71
73{
74 Led::InitAll(false);
75
76 g_Kernel.Initialize();
77
78 for (int i = 0; i < 4; ++i)
79 {
80 RunCycle();
81 }
82
83 // switch on all leds
84 Led::InitAll(true);
85
86 while (true) {}
87}
Top-level STK include. Provides the Kernel class template and all built-in task-switching strategies.
static bool Sleep(Timeout tick_count)
Put calling process into a sleep state.
Definition stk_helper.h:495
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_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
Partial implementation of the user task.
Definition stk_helper.h:98
MyTask(uint8_t task_id)
void Run() override
Entry point of the user task.
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
#define TASK_STACK_SIZE
static stk_kernel_t * g_Kernel
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)
static MyTask< stk::ACCESS_PRIVILEGED > g_Task1(0)
static void RunCycle()
static MyTask< stk::ACCESS_PRIVILEGED > g_Task2(1)
static MyTask< stk::ACCESS_PRIVILEGED > g_Task3(2)
static MyTask< stk::ACCESS_PRIVILEGED > g_Task4(3)
void RunExample()