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
/*
* 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 take their turn one by one: each task lights its LED exclusively
* for 1 s and then exits (returns from Run()). Once all tasks have exited, kernel.Start() returns and the
* sequence is repeated, finally all LEDs are switched on.
*/
#include <stk.h>
#include "example.h"
using namespace bsp;
static volatile uint8_t g_TaskSwitch = 0;
// 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()
{
uint8_t task_id = m_task_id;
while (true)
{
if (g_TaskSwitch != task_id)
{
stk::Sleep(100);
continue;
}
Led::SwitchOnExclusive(static_cast<LedId>(task_id));
stk::Sleep(1000);
g_TaskSwitch = (task_id + 1) % LED_MAX;
return;
}
}
};
// 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 RunOnce()
{
g_Kernel.AddTask(&g_Task1);
g_Kernel.AddTask(&g_Task2);
g_Kernel.AddTask(&g_Task3);
g_Kernel.AddTask(&g_Task4);
// note: kernel will exit from Start() once all tasks exit (complete their work)
g_Kernel.Start();
}
void RunExample()
{
using namespace stk;
Led::InitAll(false);
g_Kernel.Initialize();
// repeat 3 times
for (int32_t i = 0; i < 3; ++i)
{
}
// switched on all LEDs when execution ends
Led::InitAll(true);
while (true) {}
}
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() override
Entry point of the user task.
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
#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)