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_mpu/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#include <stdio.h>
17#include <new>
18
19#include <stk.h>
20#include <sync/stk_sync.h>
21#include "example.h"
22
23using namespace bsp;
24
25// ---------------------------------------------------------------------------
26// This example is built with STK_MPU_TASK_REGIONS=2 (in addition to STK_MPU=1
27// and STK_MPU_STACK_GUARD=1): each task gets only 2 hardware MPU region slots -
28// slot 0 (automatic stack guard) and slot 1 (one application-defined region).
29//
30// On an 8-region MPU this raises STK_CORTEX_M_MPU_TASK_REGION_IDX to 6 (=
31// STK_CORTEX_M_MPU_REGIONS_MAX - STK_MPU_TASK_REGIONS), so the static/global
32// region budget below in OnConfigureMpu() grows from 4 to 6 regions - the driver
33// only writes the RBAR + one alias register (A1) per context switch in this
34// configuration, which stays within its 4-region-aligned hardware block at
35// index 6 (no override of STK_CORTEX_M_MPU_TASK_REGION_IDX needed).
36//
37// Since 'shared code' / 'shared data' are identical for every LED task instance,
38// they are configured once as two of those global regions instead of being
39// repeated per-task; the single remaining per-task slot is then reserved for
40// what genuinely differs per task instance: its own object memory (the 'self'
41// region).
42//
43// ALTERNATIVE: hardening via full MPU reprogramming on every context switch
44// -------------------------------------------------------------------------
45// STK_MPU_TASK_REGIONS can instead be set to the maximum number of MPU regions
46// the target supports per task (8 on STM32F4's 8-region MPU; up to 16 on parts
47// with a larger MPU). GetMpuRegions() below already contains the logic for
48// this: when STK_MPU_TASK_REGIONS equals the hardware's max region count, it
49// copies the entire global region table (see GetGlobalMpuRegions()) into the
50// per-task region list alongside the task's own 'self' region, so every
51// region - global and per-task alike - is rewritten from scratch on every
52// context switch, rather than the global regions being configured once and
53// left in place across switches (as they are with STK_MPU_TASK_REGIONS=2).
54//
55// This is useful as a defense-in-depth measure against a compromised task
56// that manages to escalate to Privileged mode and tamper with the MPU: with
57// the static/global configuration, a rogue region write could persist and
58// affect every task scheduled afterwards, since only the small per-task slot
59// is normally reprogrammed at each switch. With full reprogramming, any such
60// tampering is wiped out and replaced with the known-good table at the very
61// next context switch, so its effect can never outlive the task's own
62// timeslice. The tradeoff is cost: writing every region's RBAR/attribute
63// registers on each switch is more cycles than the partial update used here,
64// so this is best reserved for systems where the additional protection is
65// worth the extra context-switch overhead (e.g. when running less-trusted or
66// third-party task code).
67// ---------------------------------------------------------------------------
68
69// Size of the task's stack (number of stk::Word)
70static constexpr size_t TASK_STACK_SIZE = 256U;
71
72// One flag bit per LED task; task 0 (RED) goes first
73static constexpr uint32_t FLAGS_ALL[] = {
74 (1U << LED_RED),
75 (1U << LED_ORANGE),
76 (1U << LED_GREEN),
77 (1U << LED_BLUE)
78};
79
80// Start with the RED task's flag set so it runs first
82
83// Timeline for a precise LED switching
85
86// Variable residing in Secure memory region accessible by only Secure tasks.
87static uint32_t g_SecureCounter = 0;
88
89// Per-task instance memory: on ARMv7-M the MPU requires a region's base address to be
90// naturally aligned to its size (power-of-two rounded up), so each task's storage block
91// is both sized and aligned to TASK_MEMORY_SIZE - this is what lets the 'self' region in
92// GetMpuRegions() below cover exactly one task instance without spilling into its neighbor.
93static constexpr uint32_t TASK_MEMORY_SIZE = 4096;
94static uint8_t s_LedTaskMem[4][TASK_MEMORY_SIZE] __stk_aligned(TASK_MEMORY_SIZE);
95
96// Hw commands.
97struct HwCommand
98{
99 enum EId
100 {
101 CMD_NONE = 0,
102 CMD_LED_ON = 1
103 };
104
105 EId id;
107};
109
110static stk::MpuRegionList GetGlobalMpuRegions()
111{
112 // ---------------------------------------------------------------------------
113 // MPU (Memory Protection Unit) setup for STM32F4 (Cortex-M4, ARMv7-M MPU, 8 regions)
114 // ---------------------------------------------------------------------------
115 // This adds coarse-grained memory sandboxing that applies globally, regardless
116 // of which task is running:
117 // Region 0: Flash - read-only, executable (code + rodata)
118 // Region 1: SRAM - read/write, execute-never (data, stacks, heap)
119 // Region 2: NULL guard - first 256 bytes, no access at all (catches null derefs)
120 // Region 3: Reserved - currently a disabled/zero-sized placeholder (see NOTE)
121 // Region 4: Shared code - read/execute, common code shared by every task
122 // Region 5: Shared data - read/write, globals shared by every task
123 //
124 // Regions 4/5 are configured here (instead of per-task, as they would be with
125 // STK_MPU_TASK_REGIONS=4) because this example builds with STK_MPU_TASK_REGIONS=2:
126 // each task only gets 1 application-defined region slot, reserved below for the
127 // task's own (per-instance) object memory. 'Shared code'/'shared data' are
128 // identical for every task instance, so a single global region covers all of them
129 // at once - and the region budget for this scales accordingly:
130 // STK_CORTEX_M_MPU_TASK_REGION_IDX == STK_CORTEX_M_MPU_REGIONS_MAX - STK_MPU_TASK_REGIONS,
131 // i.e. 8 - 2 == 6 static regions available here (vs. only 4 with 4 task regions).
132 //
133 // (If STK_MPU_TASK_REGIONS is instead set to the hardware's max region count,
134 // this whole table is copied into each task's own region list and reprogrammed
135 // on every context switch instead of being configured once here - see the
136 // "ALTERNATIVE" note near the top of this file.)
137 //
138 // Adjust FLASH_SIZE_BYTES / SRAM_SIZE_BYTES below to match your exact STM32F4
139 // part (e.g. STM32F407: 1 MB flash / 192 KB SRAM; STM32F429: 2 MB / 256 KB).
140 // MPU region sizes must be a power of two, so pick the next power-of-two that
141 // covers your actual flash/RAM size.
142
143 using namespace stk;
144 using namespace stk::hw::mpu;
145
146 enum EMemPartition : uint32_t
147 {
148 FLASH_BASE_ADDR = 0x08000000UL,
149 FLASH_SIZE_BYTES = 1024 * 1024, // 1 MB -> ARM_MPU_REGION_SIZE_1MB
150 SRAM_BASE_ADDR = 0x20000000UL,
151 SRAM_SIZE_BYTES = 128 * 1024, // 128 KB -> ARM_MPU_REGION_SIZE_128KB
152 CCMRAM_BASE_ADDR = 0x10000000UL, // 64KB CCM block - currently unused below
153 PERIPH_BASE_ADDR = 0x40000000UL, // currently unused below - see NOTE above
154 PERIPH_SIZE_BYTES = 512 * 1024 * 1024 // covers the whole peripheral aperture
155 };
156
157 extern char __stk_mpu_shared_code_start[];
158 extern char __stk_mpu_shared_code_end[];
159 extern char __stk_mpu_shared_data_start[];
160 extern char __stk_mpu_shared_data_end[];
161
162 extern char __stk_mpu_kernel_code_start[];
163 extern char __stk_mpu_kernel_code_end[];
164 extern char __stk_mpu_kernel_data_start[];
165 extern char __stk_mpu_kernel_data_end[];
166
167 static const stk::MpuRegionConfig mpu_table[] =
168 {
169 { // REGION 0: Flash - Privileged RO / User RO, executable, cacheable
170 .addr = FLASH_BASE_ADDR,
171 .size = FLASH_SIZE_BYTES,
172 .access_perm = ACCESS_PRIV_RO_USER_RO,
173 .mem_type = TYPE_NORMAL_CACHEABLE,
174 .share = SHARE_NON,
175 .exec = EXEC_ALLOWED
176 },
177 { // REGION 1: SRAM - full RW, execute-never (blocks code injection into RAM)
178 .addr = SRAM_BASE_ADDR,
179 .size = SRAM_SIZE_BYTES,
180 .access_perm = ACCESS_PRIV_RW_USER_NO,
181 .mem_type = TYPE_NORMAL_CACHEABLE,
182 .share = SHARE_NON,
183 .exec = EXEC_NEVER
184 },
185 { // REGION 2: STK Kernel Code Space (Privileged Executable)
186 .addr = hw::PtrToWord(__stk_mpu_kernel_code_start),
187 .size = hw::PtrToWord(__stk_mpu_kernel_code_end) - hw::PtrToWord(__stk_mpu_kernel_code_start),
188 .access_perm = hw::mpu::ACCESS_PRIV_RO_USER_NO,
189 .mem_type = hw::mpu::TYPE_NORMAL_CACHEABLE,
190 .share = hw::mpu::SHARE_NON,
191 .exec = hw::mpu::EXEC_ALLOWED
192 },
193 { // REGION 3: STK Kernel State & Data (Privileged-Only Access)
194 .addr = hw::PtrToWord(__stk_mpu_kernel_data_start),
195 .size = hw::PtrToWord(__stk_mpu_kernel_data_end) - hw::PtrToWord(__stk_mpu_kernel_data_start),
196 .access_perm = hw::mpu::ACCESS_PRIV_RW_USER_NO,
197 .mem_type = hw::mpu::TYPE_NORMAL_CACHEABLE,
198 .share = hw::mpu::SHARE_NON,
199 .exec = hw::mpu::EXEC_NEVER
200 },
201 { // REGION 4: Shared code - Privileged RO / User RO, executable, common
202 // to every task (STK_MPU_SHARED_CODE_SECTION)
203 .addr = hw::PtrToWord(__stk_mpu_shared_code_start),
204 .size = hw::PtrToWord(__stk_mpu_shared_code_end) - hw::PtrToWord(__stk_mpu_shared_code_start),
205 .access_perm = ACCESS_PRIV_RO_USER_RO,
206 .mem_type = TYPE_NORMAL_CACHEABLE,
207 .share = SHARE_NON,
208 .exec = EXEC_ALLOWED
209 },
210 { // REGION 5: Shared data - full RW, execute-never, common to every task
211 // (STK_MPU_SHARED_DATA_SECTION / STK_MPU_SHARED_BSS_SECTION)
212 .addr = hw::PtrToWord(__stk_mpu_shared_data_start),
213 .size = hw::PtrToWord(__stk_mpu_shared_data_end) - hw::PtrToWord(__stk_mpu_shared_data_start),
214 .access_perm = ACCESS_FULL,
215 .mem_type = TYPE_NORMAL_CACHEABLE,
216 .share = SHARE_NON,
217 .exec = EXEC_NEVER
218 }
219 };
220
221 return stk::MpuRegionList(mpu_table, STK_STATIC_ARRAY_SIZE(mpu_table));
222}
223
224// Task's core (thread)
225template <stk::EAccessMode _AccessMode>
226class NonSecureLedTask : public stk::Task<TASK_STACK_SIZE, _AccessMode>
227{
228 uint8_t m_task_id;
229 uint32_t m_my_flag;
230 uint32_t m_next_flag;
231
232public:
233 explicit NonSecureLedTask(uint8_t task_id) : m_task_id(task_id), m_my_flag(FLAGS_ALL[task_id]),
234 m_next_flag(FLAGS_ALL[(task_id + 1) % LED_MAX])
235 {}
236
237private:
238 void Run() override
239 {
240 // we switch LEDs with 250ms period
241 const stk::Ticks period = stk::GetTicksFromMs(250);
242
243 // get a start of the timeline
245
246 while (true)
247 {
248 // block until this task's flag is set; auto-cleared on return
249 const uint32_t result = g_TaskFlags.Wait(m_my_flag, stk::sync::EventFlags::OPT_WAIT_ANY);
251 continue;
252
253 // change active LED
254 s_HwCmdQueue.Write({
256 .param_0 = m_task_id}
257 );
258
259 // sleep 1s drift-free and then delegate work to the next task
260 // we could use simple stk::Sleep() but due to other work around Sleep call we
261 // will get a time drift, STK allows to sleep until exact timestamp making it
262 // possible precise sleeping with 1 tick precision, you could also use
263 // time::TimerHost for timer-related tasks (see related 'timer' example)
264 stk::SleepUntil(g_Timeline += period);
265
266 // hand off to the next task
268
269 // uncommenting this will cause MemManage exception due to access of Secure
270 // memory region by Non-Secure task, under debugger you will see such call stack:
271 //
272 // * PlatformEventHandler::OnException() at example.cpp:383
273 // * StkExceptionHandlerMain() at stk_arch_arm-cortex-m.cpp:2 683
274 // * <signal handler called>() at 0xfffffffd
275 // * NonSecureLedTask<(stk::EAccessMode)0>::Run at example.cpp:140 <--- points to offending ++g_SecureCounter
276 // * OnTaskRun() at stk_arch_arm-cortex-m.cpp:2 751
277 //
278 //++g_SecureCounter;
279 }
280 }
281
282 const stk::MpuRegionList *GetMpuRegions() const override
283 {
284 using namespace stk;
285 using namespace stk::hw::mpu;
286
287 // leave last slot for a stack protector
288 static MpuRegionConfig task_regions[STK_MPU_TASK_REGIONS - 1];
289
290 // on every context switch global regions will be re-programmed which is
291 // a defensive measure if compromised task escalated to Privileged and
292 // attempted to modify MPU regions
293 if (STK_MPU_TASK_REGIONS == 8)
294 {
295 const stk::MpuRegionList global_mpu = GetGlobalMpuRegions();
296 for (size_t i = 0; i < global_mpu.GetSize(); ++i)
297 {
298 task_regions[i] = global_mpu[i];
299 }
300 }
301
302 // allow access to self
303 const MpuRegionConfig self =
304 {
305 .addr = hw::PtrToWord(this), // 'this'
306 .size = TASK_MEMORY_SIZE, // cover whole allocated region of the task instance
307 .access_perm = hw::mpu::ACCESS_FULL,
308 .mem_type = hw::mpu::TYPE_NORMAL_CACHEABLE,
309 .share = hw::mpu::SHARE_NON,
310 .exec = hw::mpu::EXEC_NEVER
311 };
312 task_regions[STK_STATIC_ARRAY_SIZE(task_regions) - 1] = self;
313
314 static const stk::MpuRegionList mpu_regions(
315 task_regions, STK_STATIC_ARRAY_SIZE(task_regions));
316
317 return &mpu_regions;
318 }
319};
320
321// Secure task's core.
322template <stk::EAccessMode _AccessMode>
323class SecureHwCommandQueueTask : public stk::Task<TASK_STACK_SIZE, _AccessMode>
324{
325private:
326 void Run() override
327 {
328 while (true)
329 {
330 HwCommand cmd;
331 if (s_HwCmdQueue.Read(cmd))
332 {
333 switch (cmd.id)
334 {
337
338 // counter is accessed by a Safe task, no MemManage exception in this case
340 break; }
341 default: {
342 STK_ASSERT(false);
343 break; }
344 }
345 }
346 }
347 }
348};
349
351{
352 const stk::MpuConfig *OnConfigureMpu() const override
353 {
354 // set MPU regions and notify driver that we want to enable MPU
355 static const stk::MpuConfig mpu_config
356 (
358 (stk::hw::mpu::MPU_CFG_PRIVILEGED_BG_MEM |
359 stk::hw::mpu::MPU_CFG_CLEAR_ON_INIT)
360 );
361
362 return &mpu_config;
363 }
364
365#if STK_MPU
366 static void PrintMpuConfig(const char *label, const stk::FaultContext::Mpu &mpu)
367 {
368 printf("--- %s MPU Status & Config ---\r\n", label);
369 printf("CTRL: 0x%08X\r\n", (unsigned int)mpu.CTRL);
370 #if STK_ARCH_ARMV8_M
371 printf("MAIR0: 0x%08X MAIR1: 0x%08X\r\n", (unsigned int)mpu.MAIR0, (unsigned int)mpu.MAIR1);
372 #endif
373
374 printf("--- %s MPU Regions Configuration ---\r\n", label);
375 for (size_t i = 0U; i < STK_STATIC_ARRAY_SIZE(mpu.regions); i++)
376 {
377 printf(" Region %u -> RBAR: 0x%08X R%s: 0x%08X\r\n",
378 (unsigned int)i,
379 (unsigned int)mpu.regions[i].RBAR,
380 STK_ARCH_ARMV8_M ? "LAR" : "ASR",
381 (unsigned int)mpu.regions[i].ATTR);
382 }
383 }
384#endif
385
386 // Kernel-invoked fault handler: fires on MemManage faults (e.g. the Non-Secure LED
387 // tasks touching g_SecureCounter, or a stack-guard violation) and on generic hard
388 // faults. Dumps CPU/MPU state for diagnostics and halts via a debug breakpoint -
389 // this is intentionally non-recoverable diagnostic code, not a fault-recovery example.
390#ifdef DEBUG
391 bool OnException(stk::EHwException exc_id, stk::TId tid, const struct stk::FaultContext *const ctx) override
392 {
393 if (exc_id == stk::HW_EXCEPT_MEMACCESS)
394 {
395 printf("\r\n================ MEMMANAGE FAULT DETECTED ================\r\n");
396 printf("(Sandbox Violation: a task's Run() reached outside the MPU\r\n");
397 printf(" regions granted to it - see region dump below for what\r\n");
398 printf(" WAS active for this task at fault time)\r\n");
399 }
400 else
401 {
402 printf("\r\n================== HARD FAULT DETECTED ===================\r\n");
403 }
404
405 printf("ITask *: 0x%08X:\r\n", (unsigned int)tid);
406
407 printf("EXC_RETURN: 0x%08X\r\n\r\n", (unsigned int)ctx->EXC_RETURN);
408
409 printf("--- Stacked CPU Registers ---\r\n");
410 printf("R0: 0x%08X R1: 0x%08X R2: 0x%08X\r\n", (unsigned int)ctx->frame.R0, (unsigned int)ctx->frame.R1, (unsigned int)ctx->frame.R2);
411 printf("R3: 0x%08X R12: 0x%08X LR: 0x%08X\r\n", (unsigned int)ctx->frame.R3, (unsigned int)ctx->frame.R12, (unsigned int)ctx->frame.LR);
412 printf("PC: 0x%08X xPSR: 0x%08X\r\n\r\n", (unsigned int)ctx->frame.PC, (unsigned int)ctx->frame.xPSR);
413
414 printf("--- System Control Registers ---\r\n");
415 printf("CFSR: 0x%08X HFSR: 0x%08X AFSR: 0x%08X\r\n", (unsigned int)ctx->CFSR, (unsigned int)ctx->HFSR, (unsigned int)ctx->AFSR);
416 printf("MMFAR: 0x%08X (%s)\r\n", (unsigned int)ctx->MMFAR, ctx->mmfar_valid ? "VALID" : "INVALID");
417 printf("BFAR: 0x%08X (%s)\r\n\r\n", (unsigned int)ctx->BFAR, ctx->bfar_valid ? "VALID" : "INVALID");
418 printf("CONTROL: 0x%08X (nPRIV=%u)\r\n", (unsigned int)ctx->CONTROL, (unsigned int)(ctx->CONTROL & 1U));
419
420#if STK_MPU
421 PrintMpuConfig((STK_ARCH_ARMV8_M && STK_TZ_SECURE) ? "Secure" : "Primary", ctx->mpu);
422
423 #if STK_ARCH_ARMV8_M && STK_TZ_SECURE
424 printf("\r\n");
425 PrintMpuConfig("Non-Secure", ctx->mpu_ns);
426 #endif
427#endif
428
429 printf("=====================================================\r\n");
430
432 return false; // allow default handling by the platform driver
433 }
434#endif // DEBUG
435};
436
438{
439 using namespace stk;
440
441 Led::InitAll(false);
442
443 // operating in Static + Sync mode (EventFlags requires KERNEL_SYNC) and optionally tickless
444 const uint8_t KernelMode = KERNEL_STATIC | KERNEL_SYNC | (STK_TICKLESS_IDLE ? KERNEL_TICKLESS : 0);
445
446 // allocate scheduling kernel for 3 threads (tasks) with Round-Robin scheduling strategy
448
449 // for MPU configuration and MemFault/HardFault exceptions processing
450 static PlatformEventHandler event_overrider;
451 kernel.GetPlatform()->SetEventOverrider(&event_overrider);
452
453 // make sure memory is enough
455
456 // Non-Secure tasks
457 NonSecureLedTask<ACCESS_USER> *led_task1 = new (s_LedTaskMem[0]) NonSecureLedTask<ACCESS_USER>(LED_RED);
460 NonSecureLedTask<ACCESS_USER> *led_task4 = new (s_LedTaskMem[3]) NonSecureLedTask<ACCESS_USER>(LED_BLUE);
461
463
464 // init scheduling kernel
465 kernel.Initialize();
466
467 // register non-secure threads (LED tasks)
468 kernel.AddTask(led_task1);
469 kernel.AddTask(led_task2);
470 kernel.AddTask(led_task3);
471 kernel.AddTask(led_task4);
472
473 // register secure thread which will interact with hardware
474 kernel.AddTask(&hw_cmd_proc);
475
476 // start scheduler (it will start threads added by AddTask), execution in main() will be blocked on this line
477 kernel.Start();
478
479 // shall not reach here after Start() was called
480 STK_ASSERT(false);
481}
@ 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
#define STK_ARCH_ARMV8_M
ARMv8-M platform (Cortex-M23, Cortex-M33, Cortex-M55, Cortex-M85).
Top-level STK include. Provides the Kernel class template and all built-in task-switching strategies.
#define STK_MPU_SHARED_DATA_SECTION
Definition stk_arch.h:158
#define __stk_aligned(x)
Specifies minimum alignment in bytes for the decorated variable or struct member (data instance prefi...
Definition stk_defs.h:282
#define STK_TZ_SECURE
ARM TrustZone: Defines Secure (1) build.
Definition stk_defs.h:180
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:532
#define STK_MPU_TASK_REGIONS
Number of hardware MPU region slots reserved per task (stk::TaskMpu::NUM_REGIONS).
Definition stk_defs.h:155
static void __stk_debug_break()
Definition stk_defs.h:495
#define STK_STATIC_ARRAY_SIZE(ARRAY)
Get size of the static array.
Definition stk_defs.h:742
#define STK_STATIC_ASSERT(X)
Compile-time assertion. Produces a compilation error if X is false.
Definition stk_defs.h:560
Collection of synchronization primitives (stk::sync namespace).
Namespace of STK package.
uintptr_t Word
Native processor word type.
Definition stk_common.h:206
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
EHwException
Hardware exception id (see IPlatform::IEventOverrider::OnException).
Definition stk_common.h:194
@ HW_EXCEPT_MEMACCESS
MemManage on ARM / Page Fault or PMP violation on RISC-V.
Definition stk_common.h:196
Word TId
Task (thread) id.
Definition stk_common.h:211
@ 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_SYNC
Synchronization support (see Event).
Definition stk_common.h:50
@ KERNEL_STATIC
All tasks are static and can not exit.
Definition stk_common.h:47
static Word PtrToWord(T *const ptr) noexcept
Cast a pointer to a CPU register-width integer.
Definition stk_arch.h:215
EventFlagsT< uint32_t > EventFlags
Default event flags group with a 32-bit flags word (31 usable flags, bit 31 reserved).
Definition cpu.h:17
Word BFAR
BusFault Address Register (BFAR).
Word AFSR
Auxiliary Fault Status Register (AFSR).
Word HFSR
HardFault Status Register (HFSR).
hw::ExceptionFrame frame
Saved hardware exception stack frame.
Word MMFAR
Memory Management Fault Address Register (MMFAR).
Word CFSR
Configurable Fault Status Register (CFSR: MemManage, BusFault, UsageFault).
Word CONTROL
Core CONTROL register state snapshot.
bool mmfar_valid
Flag indicating validity of MMFAR address value (derived from CFSR.MMARVALID).
Word EXC_RETURN
Exception return magic value (EXC_RETURN).
bool bfar_valid
Flag indicating validity of BFAR address value (derived from CFSR.BFARVALID).
Hardware Memory Protection Unit (MPU) status register snapshot.
Region regions[(8U)]
Active hardware MPU region register state table.
Word CTRL
MPU Control Register (MPU_CTRL).
Word ATTR
Region Attribute and Size Register (RASR / RLAR / MPU_RLAR).
Word RBAR
Region Base Address Register (RBAR).
Concrete implementation of IKernel.
Definition stk.h:102
void Start() override
Start the scheduler. This call does not return until all tasks have exited (KERNEL_DYNAMIC mode) or i...
Definition stk.h:1999
EAddResult AddTask(ITask *user_task) override
Register task for a soft real-time (SRT) scheduling.
Definition stk.h:1465
IPlatform * GetPlatform() override
Get platform driver instance owned by this kernel.
Definition stk.h:2034
void Initialize(uint32_t resolution_us=PERIODICITY_DEFAULT) override
Initialize kernel.
Definition stk.h:1425
virtual void SetEventOverrider(IEventOverrider *overrider, bool non_secure=false)=0
Set platform event overrider.
Interface for a platform event overrider.
Partial implementation of the user task.
Definition stk_helper.h:98
static constexpr uint32_t OPT_WAIT_ANY
static bool IsError(uint32_t result)
Thread-safe, type-safe FIFO communication pipe with internal storage.
void Run() override
Entry point of the user task.
bool OnException(stk::EHwException exc_id, stk::TId tid, const struct stk::FaultContext *const ctx) override
const stk::MpuConfig * OnConfigureMpu() const override
void Run() override
Entry point of the user task.
NonSecureLedTask(uint8_t task_id)
const stk::MpuRegionList * GetMpuRegions() const override
LedId Id
Definition led.h:32
static void InitAll(bool init_state)
Definition led.h:40
static void SwitchOnExclusive(Id led)
Definition led.h:48
static stk::Ticks g_Timeline
static const uint32_t FLAGS_ALL[]
static stk::sync::EventFlags g_TaskFlags(FLAGS_ALL[LED_RED])
static sync::PipeT< HwCommand, 4 > s_HwCmdQueue
static constexpr size_t TASK_MEMORY_SIZE
static STK_MPU_KERNEL_BSS_SECTION uint32_t g_SecureCounter
static stk::MpuRegionList GetGlobalMpuRegions()
void RunExample()
static STK_MPU_SHARED_DATA_SECTION stk::sync::PipeT< HwCommand, 4 > s_HwCmdQueue
static constexpr size_t TASK_MEMORY_SIZE
static STK_MPU_SHARED_DATA_SECTION stk::sync::EventFlags g_TaskFlags(FLAGS_ALL[LED_RED])
static uint32_t g_SecureCounter
static constexpr uint32_t FLAGS_ALL[]
static STK_MPU_SHARED_DATA_SECTION stk::Ticks g_Timeline
#define TASK_STACK_SIZE