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
/*
* SuperTinyKernel(TM) RTOS: 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.
*/
/*
* Four tasks of a KERNEL_DYNAMIC kernel sleep with stk::Sleep() for 1, 2, 3 and 4 s respectively, then
* light up their LED and exit. Once all tasks have completed, the kernel returns from Start() and all LEDs
* are switched on. Also shows the tickless idle mode when STK_TICKLESS_IDLE is enabled.
*/
#include <stk.h>
#include "example.h"
using namespace bsp;
// R2350 requires larger stack due to stack-memory heavy SDK API
#ifdef _PICO_H
enum { TASK_STACK_SIZE = 1024 };
#else
enum { TASK_STACK_SIZE = 256 };
#endif
template <stk::EAccessMode _AccessMode>
class MyTask : public stk::Task<TASK_STACK_SIZE, _AccessMode>
{
uint8_t m_task_id;
public:
MyTask(uint8_t task_id) : m_task_id(task_id)
{}
private:
void Run() override
{
// task 0: sleep 1000 ms
// task 1: sleep 2000 ms
// task 2: sleep 3000 ms
// task 3: sleep 4000 ms
stk::Sleep(1000 * (m_task_id + 1));
bsp::Led::SwitchOnExclusive(static_cast<Led::Id>(m_task_id));
}
};
static stk::Kernel<stk::KERNEL_DYNAMIC | (STK_TICKLESS_IDLE ? stk::KERNEL_TICKLESS : 0), 4,
// 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)
static void RunCycle()
{
g_Kernel.AddTask(&g_Task1);
g_Kernel.AddTask(&g_Task2);
g_Kernel.AddTask(&g_Task3);
g_Kernel.AddTask(&g_Task4);
g_Kernel.Start();
}
void RunExample()
{
Led::InitAll(false);
g_Kernel.Initialize();
for (int i = 0; i < 4; ++i)
{
}
// switch on all leds
Led::InitAll(true);
while (true) {}
}
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
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
#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 void RunCycle()