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-trustzone-ns/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.
*/
/*
* Non-Secure binary of the STK TrustZone example. Four LED tasks pass a baton to each other with
* stk::sync::EventFlags just like in the \ref blinky/example.cpp example. The kernel is initialized on the Secure
* side, the Non-Secure side only adds its tasks and starts the scheduler. Must be used together with the
* Secure side of the example, see \ref blinky-trustzone-sc/example.cpp. See README.md in the example folder for
* the details.
*/
#include <stk.h>
#include <sync/stk_sync.h>
#include <time/stk_time.h>
#include <arch/arm/cortex-m/stk_arch_arm-tz.h> // for Secure/Non-Secure-specific TrustZone API
#include "example.h"
#if (__ARM_FEATURE_CMSE & 1) == 0
#error "Need ARMv8-M security extensions"
#elif (__ARM_FEATURE_CMSE & 2) != 0
#error "Compile without --mcmse"
#endif
#include "pico/unique_id.h"
using namespace bsp;
void NSC_OnExitNs(void);
uint32_t NSC_GetKey(uint8_t key[], uint32_t size);
void NSC_GetBoardUID(pico_unique_board_id_t *id_out);
extern "C" void runtime_init_clocks(void)
{
// clocks are initialized on Secure side of the binary
}
void pico_get_unique_board_id(pico_unique_board_id_t *id_out)
{
// you can get Id via NSC call, if needed for a Non-Secure side of the binary
//*id_out = pico_unique_board_id_t{};
NSC_GetBoardUID(id_out);
}
// Size of the task's stack (number of stk::Word)
// R2350 requires larger stack due to stack-memory heavy SDK API
#ifdef _PICO_H
static constexpr size_t TASK_STACK_SIZE = 1024U;
#else
static constexpr size_t TASK_STACK_SIZE = 256U;
#endif
// One flag bit per LED task; task 0 (RED) goes first
static const uint32_t FLAGS_ALL[] = {
(1U << LED_RED),
(1U << LED_ORANGE),
(1U << LED_GREEN),
(1U << LED_BLUE)
};
// Start with the RED task's flag set so it runs first
// Timeline for a precise LED switching
// Task's core (thread)
template <stk::EAccessMode _AccessMode>
class MyTask : public stk::Task<TASK_STACK_SIZE, _AccessMode>
{
uint8_t m_task_id;
uint32_t m_my_flag;
uint32_t m_next_flag;
public:
MyTask(uint8_t task_id) : m_task_id(task_id), m_my_flag(FLAGS_ALL[task_id]),
m_next_flag(FLAGS_ALL[(task_id + 1) % LED_MAX])
{}
private:
void Run() override
{
// we switch LEDs with 250ms period
const stk::Ticks period = stk::GetTicksFromMs(250);
// get a start of the timeline
while (true)
{
// block until this task's flag is set; auto-cleared on return
continue;
// change active LED
{
const stk::hw::CriticalSection::ScopedLock __guard;
}
// sleep 1s drift-free and then delegate work to the next task
// we could use simple stk::Sleep() but due to other work around Sleep call we
// will get a time drift, STK allows to sleep until exact timestamp making it
// possible precise sleeping with 1 tick precision, you could also use
// time::TimerHost for timer-related tasks (see related 'timer' example)
// hand off to the next task
}
}
};
void RunExample()
{
using namespace stk;
uint8_t key[4] = {0};
NSC_GetKey(key, 4);
// Note: using ACCESS_PRIVILEGED as Cortex-M3+ may not allow writing to GPIO from a less secure user thread.
static MyTask<ACCESS_USER> task3(LED_GREEN); // you can set to ACCESS_PRIVILEGED too, see NSC_bsp_Led_SwitchOnExclusive on secure side, but for smooth performance on RP2350 let background thread handle GPIO
static tz::nsec::Kernel kernel;
kernel.Initialize(0);
// Register threads (tasks).
kernel.AddTask(&task1);
kernel.AddTask(&task2);
kernel.AddTask(&task3);
kernel.AddTask(&task4);
// Start scheduler (it will start threads added by AddTask), execution in main() will be blocked on this line.
kernel.Start();
// Return back to Secure binary. Note: kernel.Start() will exit only if Secure side initialized Kernel instance
// with KERNEL_DYNAMIC mode and when all tasks exited on both sides.
}
LedId
Definition led.h:16
@ LED_ORANGE
Definition led.h:18
@ LED_GREEN
Definition led.h:17
@ LED_MAX
Definition led.h:22
@ LED_BLUE
Definition led.h:20
@ LED_RED
Definition led.h:19
Top-level STK include. Provides the Kernel class template and all built-in task-switching strategies.
Collection of synchronization primitives (stk::sync namespace).
Collection of time-related primitives (stk::time namespace).
Namespace of STK package.
int64_t Ticks
Ticks value.
Definition stk_common.h:221
static bool SleepUntil(Ticks timestamp)
Put calling process into a sleep state until the specified timestamp.
Definition stk_helper.h:523
static Ticks GetTicksFromMs(Time ms, uint32_t resolution)
Convert milliseconds to ticks.
Definition stk_helper.h:406
static Ticks GetTicks()
Get number of ticks elapsed since kernel start.
Definition stk_helper.h:448
EventFlagsT< uint32_t > EventFlags
Default event flags group with a 32-bit flags word (31 usable flags, bit 31 reserved).
Definition cpu.h:17
Partial implementation of the user task.
Definition stk_helper.h:98
T Set(T flags)
Set one or more flags.
static constexpr uint32_t OPT_WAIT_ANY
T Wait(T flags, uint32_t options=OPT_WAIT_ANY, Timeout timeout_ticks=WAIT_INFINITE)
Wait for one or more flags to be set.
static bool IsError(uint32_t result)
void Run() override
Entry point of the user task.
LedId Id
Definition led.h:32
void NSC_GetBoardUID(pico_unique_board_id_t *id_out)
void NSC_OnExitNs(void)
static stk::Ticks g_Timeline
void runtime_init_clocks(void)
static const uint32_t FLAGS_ALL[]
uint32_t NSC_GetKey(uint8_t key[], uint32_t size)
void NSC_bsp_Led_SwitchOnExclusive(bsp::Led::Id led)
static stk::sync::EventFlags g_TaskFlags(FLAGS_ALL[LED_RED])
void pico_get_unique_board_id(pico_unique_board_id_t *id_out)
#define TASK_STACK_SIZE
static stk::Ticks g_Timeline