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_swrr/example.cpp
/*
* SuperTinyKernel™ (STK): 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 CPU-loaded tasks blink an LED each and have different weights (BLUE is the highest, RED is the
* lowest), so the larger the weight the more CPU time the task gets and the more often its LED blinks.
* Unlike the fixed-priority strategy (see \ref blinky_fp/example.cpp), SWRR guarantees a CPU time slice
* to every task including the one with the lowest weight and does not require tasks to cooperate by
* calling stk::Sleep() or stk::Yield().
*/
#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
// Generic LED-blink task
template <int32_t _Weight, stk::EAccessMode _AccessMode>
class LedTask : public stk::TaskW<_Weight, TASK_STACK_SIZE, _AccessMode>
{
uint8_t m_led_id;
public:
LedTask(uint8_t id) : m_led_id(id)
{}
private:
void Run() override
{
bool led_state = false;
while (true)
{
// do some busy work to create CPU load, this ensures tasks are always ready to run (not sleeping)
// due to load higher priority tasks will preempt lower priority ones
for (volatile uint32_t i = 0; i < 300000; i++)
{}
// toggle LED for this task
{
// protect from preemption during hardware IO
stk::hw::CriticalSection::ScopedLock __cs;
Led::Set(static_cast<LedId>(m_led_id), led_state);
}
// SWRR, unlike with fixed-priority sched::SwitchStrategyFP32, does not require tasks cooperation with
// Sleep() or Yield(), all tasks will get their CPU time slice, even tasks with lowest priority
//stk::Sleep(10);
}
}
};
void RunExample()
{
using namespace stk;
Led::InitAll(false);
// 3 tasks kernel with Smooth Weighted Round-Robin scheduling strategy
static Kernel<KERNEL_STATIC | (STK_TICKLESS_IDLE ? KERNEL_TICKLESS : 0), 4,
// RED is lowest priority (1), and BLUE is highest (69):
// - BLUE gets more CPU time and will blink very often
// - GRREN blinks less often than BLUE
// - ORANGE is blinking less often than GREEN
// - RED is least blinking as it gets gets the least CPU time
// note: if you set the same priority for tasks LEDs of these tasks will blink equally
kernel.Initialize();
kernel.AddTask(&task_red);
kernel.AddTask(&task_org);
kernel.AddTask(&task_green);
kernel.AddTask(&task_blue);
// start scheduling (blocks forever)
kernel.Start();
STK_ASSERT(false);
}
static bool led_state[LED_MAX]
LedId
Definition led.h:16
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
Namespace of STK package.
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_STATIC
All tasks are static and can not exit.
Definition stk_common.h:47
SwitchStrategySmoothWeightedRoundRobinT< false > SwitchStrategySWRR
Shorthand alias for SwitchStrategySmoothWeightedRoundRobin.
Definition cpu.h:17
Concrete implementation of IKernel.
Definition stk.h:102
Partial implementation of the user task with a compile-time scheduling weight. Use when the kernel is...
Definition stk_helper.h:151
void Run()
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
LedTaskT< stk::ACCESS_PRIVILEGED > LedTask
#define TASK_STACK_SIZE