144 using namespace stk::hw::mpu;
146 enum EMemPartition : uint32_t
148 FLASH_BASE_ADDR = 0x08000000UL,
149 FLASH_SIZE_BYTES = 1024 * 1024,
150 SRAM_BASE_ADDR = 0x20000000UL,
151 SRAM_SIZE_BYTES = 128 * 1024,
152 CCMRAM_BASE_ADDR = 0x10000000UL,
153 PERIPH_BASE_ADDR = 0x40000000UL,
154 PERIPH_SIZE_BYTES = 512 * 1024 * 1024
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[];
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[];
167 static const stk::MpuRegionConfig mpu_table[] =
170 .addr = FLASH_BASE_ADDR,
171 .size = FLASH_SIZE_BYTES,
172 .access_perm = ACCESS_PRIV_RO_USER_RO,
173 .mem_type = TYPE_NORMAL_CACHEABLE,
178 .addr = SRAM_BASE_ADDR,
179 .size = SRAM_SIZE_BYTES,
180 .access_perm = ACCESS_PRIV_RW_USER_NO,
181 .mem_type = TYPE_NORMAL_CACHEABLE,
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
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
205 .access_perm = ACCESS_PRIV_RO_USER_RO,
206 .mem_type = TYPE_NORMAL_CACHEABLE,
214 .access_perm = ACCESS_FULL,
215 .mem_type = TYPE_NORMAL_CACHEABLE,
225template <stk::EAccessMode _AccessMode>
285 using namespace stk::hw::mpu;
296 for (
size_t i = 0; i < global_mpu.GetSize(); ++i)
298 task_regions[i] = global_mpu[i];
303 const MpuRegionConfig self =
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
314 static const stk::MpuRegionList mpu_regions(
322template <stk::EAccessMode _AccessMode>
355 static const stk::MpuConfig mpu_config
358 (stk::hw::mpu::MPU_CFG_PRIVILEGED_BG_MEM |
359 stk::hw::mpu::MPU_CFG_CLEAR_ON_INIT)
368 printf(
"--- %s MPU Status & Config ---\r\n", label);
369 printf(
"CTRL: 0x%08X\r\n", (
unsigned int)mpu.
CTRL);
371 printf(
"MAIR0: 0x%08X MAIR1: 0x%08X\r\n", (
unsigned int)mpu.MAIR0, (
unsigned int)mpu.MAIR1);
374 printf(
"--- %s MPU Regions Configuration ---\r\n", label);
377 printf(
" Region %u -> RBAR: 0x%08X R%s: 0x%08X\r\n",
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");
402 printf(
"\r\n================== HARD FAULT DETECTED ===================\r\n");
405 printf(
"ITask *: 0x%08X:\r\n", (
unsigned int)tid);
407 printf(
"EXC_RETURN: 0x%08X\r\n\r\n", (
unsigned int)ctx->
EXC_RETURN);
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);
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));
423 #if STK_ARCH_ARMV8_M && STK_TZ_SECURE
425 PrintMpuConfig(
"Non-Secure", ctx->mpu_ns);
429 printf(
"=====================================================\r\n");
#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
#define __stk_aligned(x)
Specifies minimum alignment in bytes for the decorated variable or struct member (data instance prefi...
#define STK_TZ_SECURE
ARM TrustZone: Defines Secure (1) build.
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
#define STK_MPU_TASK_REGIONS
Number of hardware MPU region slots reserved per task (stk::TaskMpu::NUM_REGIONS).
static void __stk_debug_break()
#define STK_STATIC_ARRAY_SIZE(ARRAY)
Get size of the static array.
#define STK_STATIC_ASSERT(X)
Compile-time assertion. Produces a compilation error if X is false.
Collection of synchronization primitives (stk::sync namespace).
Namespace of STK package.
uintptr_t Word
Native processor word type.
int64_t Ticks
Ticks value.
static bool SleepUntil(Ticks timestamp)
Put calling process into a sleep state until the specified timestamp.
static Ticks GetTicksFromMs(Time ms, uint32_t resolution)
Convert milliseconds to ticks.
static Ticks GetTicks()
Get number of ticks elapsed since kernel start.
EHwException
Hardware exception id (see IPlatform::IEventOverrider::OnException).
@ HW_EXCEPT_MEMACCESS
MemManage on ARM / Page Fault or PMP violation on RISC-V.
Word TId
Task (thread) id.
@ KERNEL_TICKLESS
Tickless mode. To use this mode STK_TICKLESS_IDLE must be defined to 1 in stk_config....
@ KERNEL_SYNC
Synchronization support (see Event).
@ KERNEL_STATIC
All tasks are static and can not exit.
static Word PtrToWord(T *const ptr) noexcept
Cast a pointer to a CPU register-width integer.
EventFlagsT< uint32_t > EventFlags
Default event flags group with a 32-bit flags word (31 usable flags, bit 31 reserved).
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.
void Start() override
Start the scheduler. This call does not return until all tasks have exited (KERNEL_DYNAMIC mode) or i...
EAddResult AddTask(ITask *user_task) override
Register task for a soft real-time (SRT) scheduling.
IPlatform * GetPlatform() override
Get platform driver instance owned by this kernel.
void Initialize(uint32_t resolution_us=PERIODICITY_DEFAULT) override
Initialize kernel.
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.
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
static void InitAll(bool init_state)
static void SwitchOnExclusive(Id led)
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()
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