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
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 * Non-Secure binary of the STK TrustZone example. Four LED tasks pass a baton to each other with
18 * stk::sync::EventFlags just like in the \ref blinky/example.cpp example. The kernel is initialized on the Secure
19 * side, the Non-Secure side only adds its tasks and starts the scheduler. Must be used together with the
20 * Secure side of the example, see \ref blinky-trustzone-sc/example.cpp. See README.md in the example folder for
21 * the details.
22 */
23
24#include <stk.h>
25#include <sync/stk_sync.h>
26#include <time/stk_time.h>
27#include <arch/arm/cortex-m/stk_arch_arm-tz.h> // for Secure/Non-Secure-specific TrustZone API
28
29#include "example.h"
30
31#if (__ARM_FEATURE_CMSE & 1) == 0
32#error "Need ARMv8-M security extensions"
33#elif (__ARM_FEATURE_CMSE & 2) != 0
34#error "Compile without --mcmse"
35#endif
36
37#include "pico/unique_id.h"
38
39using namespace bsp;
40
41void NSC_OnExitNs(void);
42uint32_t NSC_GetKey(uint8_t key[], uint32_t size);
43void NSC_GetBoardUID(pico_unique_board_id_t *id_out);
45
46extern "C" void runtime_init_clocks(void)
47{
48 // clocks are initialized on Secure side of the binary
49}
50
51void pico_get_unique_board_id(pico_unique_board_id_t *id_out)
52{
53 // you can get Id via NSC call, if needed for a Non-Secure side of the binary
54 //*id_out = pico_unique_board_id_t{};
55
56 NSC_GetBoardUID(id_out);
57}
58
59// Size of the task's stack (number of stk::Word)
60// R2350 requires larger stack due to stack-memory heavy SDK API
61#ifdef _PICO_H
62static constexpr size_t TASK_STACK_SIZE = 1024U;
63#else
64static constexpr size_t TASK_STACK_SIZE = 256U;
65#endif
66
67// One flag bit per LED task; task 0 (RED) goes first
68static const uint32_t FLAGS_ALL[] = {
69 (1U << LED_RED),
70 (1U << LED_ORANGE),
71 (1U << LED_GREEN),
72 (1U << LED_BLUE)
73};
74
75// Start with the RED task's flag set so it runs first
77
78// Timeline for a precise LED switching
80
81// Task's core (thread)
82template <stk::EAccessMode _AccessMode>
83class MyTask : public stk::Task<TASK_STACK_SIZE, _AccessMode>
84{
85 uint8_t m_task_id;
86 uint32_t m_my_flag;
87 uint32_t m_next_flag;
88
89public:
90 MyTask(uint8_t task_id) : m_task_id(task_id), m_my_flag(FLAGS_ALL[task_id]),
91 m_next_flag(FLAGS_ALL[(task_id + 1) % LED_MAX])
92 {}
93
94private:
95 void Run() override
96 {
97 // we switch LEDs with 250ms period
98 const stk::Ticks period = stk::GetTicksFromMs(250);
99
100 // get a start of the timeline
102
103 while (true)
104 {
105 // block until this task's flag is set; auto-cleared on return
106 const uint32_t result = g_TaskFlags.Wait(m_my_flag, stk::sync::EventFlags::OPT_WAIT_ANY);
108 continue;
109
110 // change active LED
111 {
114 }
115
116 // sleep 1s drift-free and then delegate work to the next task
117 // we could use simple stk::Sleep() but due to other work around Sleep call we
118 // will get a time drift, STK allows to sleep until exact timestamp making it
119 // possible precise sleeping with 1 tick precision, you could also use
120 // time::TimerHost for timer-related tasks (see related 'timer' example)
121 stk::SleepUntil(g_Timeline += period);
122
123 // hand off to the next task
125 }
126 }
127};
128
130{
131 using namespace stk;
132
133 uint8_t key[4] = {0};
134 NSC_GetKey(key, 4);
135
136 // Note: using ACCESS_PRIVILEGED as Cortex-M3+ may not allow writing to GPIO from a less secure user thread.
137 static MyTask<ACCESS_USER> task1(LED_RED);
138 static MyTask<ACCESS_USER> task2(LED_ORANGE);
139 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
140 static MyTask<ACCESS_USER> task4(LED_BLUE);
141
142 static tz::nsec::Kernel kernel;
143
144 kernel.Initialize(0);
145
146 // Register threads (tasks).
147 kernel.AddTask(&task1);
148 kernel.AddTask(&task2);
149 kernel.AddTask(&task3);
150 kernel.AddTask(&task4);
151
152 // Start scheduler (it will start threads added by AddTask), execution in main() will be blocked on this line.
153 kernel.Start();
154
155 // Return back to Secure binary. Note: kernel.Start() will exit only if Secure side initialized Kernel instance
156 // with KERNEL_DYNAMIC mode and when all tasks exited on both sides.
157 NSC_OnExitNs();
158}
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
RAII instance that enters the critical section on construction and exits it on destruction.
Definition stk_arch.h:495
Partial implementation of the user task.
Definition stk_helper.h:98
static constexpr uint32_t OPT_WAIT_ANY
static bool IsError(uint32_t result)
MyTask(uint8_t task_id)
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