SuperTinyKernel™ RTOS 1.06.x
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
stk_arch_arm-cortex-m.h
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
10#ifndef STK_ARCH_ARM_CORTEX_M_H_
11#define STK_ARCH_ARM_CORTEX_M_H_
12
13#include "stk_common.h"
14#if defined(__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE != 0)
15 #include <arm_cmse.h> // for ARM TrustZone
16#endif
17
21
22namespace stk {
23
27class PlatformArmCortexM final : public IPlatform
28{
29public:
34
35 void Initialize(IEventHandler *event_handler, IKernelService *service, uint32_t resolution_us, Stack *exit_trap) override;
36 void Start() override;
37 void Stop() override;
38 void InitStack(EStackType stack_type, Stack *stack, IStackMemory *stack_memory, ITask *user_task) override;
39 uint32_t GetTickResolution() const override;
40 Cycles GetSysTimerCount() const override;
41 uint32_t GetSysTimerFrequency() const override;
42 void SwitchToNext() override;
43 void Sleep(Timeout ticks) override;
44 bool SleepUntil(Ticks timestamp) override;
45 EWaitResult Wait(ISyncObject *sync_obj, IMutex *mutex, Timeout timeout) override;
46 void ProcessTick() override;
47 void ProcessHardFault() override;
48 void SetEventOverrider(IEventOverrider *overrider) override;
49 Word GetCallerSP() const override;
50 TId GetTid() const override;
51 Timeout Suspend() override;
52 void Resume(Timeout elapsed_ticks) override;
53};
54
59
60// Inline TLS via r9. Active when both STK_TLS and STK_TLS_PREFER_REGISTER are
61// enabled. Requires -ffixed-r9 on all translation units containing task code
62// (see GetTls/SetTls warnings below). If -ffixed-r9 is unavailable or
63// undesirable, leave STK_TLS_PREFER_REGISTER disabled; the kernel will fall back
64// to a memory-based TLS slot with a small additional load/store per access.
65#if STK_TLS && STK_TLS_PREFER_REGISTER
66
81{
82 Word tp;
83 __asm volatile("MOV %0, r9" : "=r"(tp) : /* input: none */ : /* clobbers: none */);
84 return tp;
85}
86
95static __stk_forceinline void SetTls(Word tp)
96{
97 __asm volatile("MOV r9, %0" : /* output: none */ : "r"(tp) : /* clobbers: none */);
98}
99
100// Notify stk_arch.h that we defined inline versions of GetTls/SetTls.
101#define STK_INLINE_TLS 1
102
103#endif // STK_TLS_PREFER_REGISTER
104
105} // namespace stk
106
109static __stk_forceinline void __stk_dmb() { __asm volatile("dmb sy" ::: "memory"); }
110
114#if defined(__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3)
115 #define STK_TZ_SECURE (1)
116#else
117 #define STK_TZ_SECURE (0)
118#endif
119
123#if defined(__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 1)
124 #define STK_TZ_NON_SECURE (1)
125#else
126 #define STK_TZ_NON_SECURE (0)
127#endif
128
133#if defined(__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3)
134 #define __stk_tz_nsc_entry __attribute__((cmse_nonsecure_entry))
135#else
136 #define __stk_tz_nsc_entry
137#endif
138
142#if defined(__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3)
143 #define __stk_tz_ns_call __attribute__((cmse_nonsecure_call))
144#else
145 #define __stk_tz_ns_call
146#endif
147
151#define STK_TZ_NSC_GATEWAY extern "C" __stk_tz_nsc_entry
152
153// ARM TrustZone Non-Secure binary configuration validation.
154#ifdef _STK_CORTEX_M_TRUSTZONE_NON_SECURE
155#if !STK_TZ_NON_SECURE
156 #error "Switch of -cmse compiler flag for Non-Secure binary!"
157#endif
158#endif
159
160#endif /* STK_ARCH_ARM_CORTEX_M_H_ */
static void __stk_dmb()
Hardware memory barrier: ensures visibility across cores and bus masters.
Contains interface definitions of the library.
#define __stk_forceinline
Forces compiler to always inline the decorated function, regardless of optimisation level.
Definition stk_defs.h:175
#define STK_VIRT_DTOR
Makes destructors virtual and compliant to strict rules if STK_STRICT_COMPLIANCY=0.
Definition stk_defs.h:159
Namespace of STK package.
uintptr_t Word
Native processor word type.
Definition stk_common.h:136
static __stk_forceinline void SetTls(Word tp)
Set thread-local storage (TLS).
int64_t Ticks
Ticks value.
Definition stk_common.h:151
int32_t Timeout
Timeout time (ticks).
Definition stk_common.h:146
static __stk_forceinline Word GetTls()
Get thread-local storage (TLS).
EStackType
Stack type.
Definition stk_common.h:83
EWaitResult
Wait result (see IKernelService::Wait).
Definition stk_common.h:123
PlatformArmCortexM PlatformDefault
Default platform implementation.
uint64_t Cycles
Cycles value.
Definition stk_common.h:161
Word TId
Task (thread) id.
Definition stk_common.h:141
Concrete implementation of IPlatform driver for the Arm Cortex-M0, M3, M4, M7 processors.
void SwitchToNext() override
Switch to a next task.
void Sleep(Timeout ticks) override
Put calling process into a sleep state.
void ProcessTick() override
Process one tick.
Word GetCallerSP() const override
Get caller's Stack Pointer (SP).
void ProcessHardFault() override
Cause a hard fault of the system.
uint32_t GetTickResolution() const override
Get resolution of the system tick timer in microseconds. Resolution means a number of microseconds be...
Timeout Suspend() override
Suspend scheduling.
void Start() override
Start scheduling.
void Stop() override
Stop scheduling.
TId GetTid() const override
Get thread Id.
uint32_t GetSysTimerFrequency() const override
Get system timer frequency.
Cycles GetSysTimerCount() const override
Get system timer count value.
void InitStack(EStackType stack_type, Stack *stack, IStackMemory *stack_memory, ITask *user_task) override
Initialize stack memory of the user task.
EWaitResult Wait(ISyncObject *sync_obj, IMutex *mutex, Timeout timeout) override
Put calling process into a waiting state until synchronization object is signaled or timeout occurs.
void Resume(Timeout elapsed_ticks) override
Resume scheduling after a prior Suspend() call.
void SetEventOverrider(IEventOverrider *overrider) override
Set platform event overrider.
void Initialize(IEventHandler *event_handler, IKernelService *service, uint32_t resolution_us, Stack *exit_trap) override
Initialize scheduler's context.
~PlatformArmCortexM()=default
Destructor.
bool SleepUntil(Ticks timestamp) override
Put calling process into a sleep state until the specified timestamp.
Stack descriptor.
Definition stk_common.h:302
Interface for a stack memory region.
Definition stk_common.h:319
Synchronization object.
Definition stk_common.h:456
Interface for mutex synchronization primitive.
Definition stk_common.h:613
Interface for a user task.
Definition stk_common.h:670
Interface for a platform driver.
Definition stk_common.h:840
Interface for a back-end event handler.
Definition stk_common.h:848
Interface for a platform event overrider.
Definition stk_common.h:930
Interface for the kernel services exposed to the user processes during run-time when Kernel started s...