36#ifdef _STK_ARCH_ARM_CORTEX_M
38 #define _STK_ARCH_DEFINED
40#ifdef _STK_ARCH_RISC_V
42 #define _STK_ARCH_DEFINED
44#ifdef _STK_ARCH_X86_WIN32
46 #define _STK_ARCH_DEFINED
49#ifndef STK_PANIC_HANDLER
59 #define STK_PANIC_HANDLER(id) STK_PANIC_HANDLER_DEFAULT(id)
74#define STK_MPU_SHARED_DATA_SECTION __attribute__((section(".stk_mpu_shared_data")))
84#define STK_MPU_SHARED_CODE_SECTION __attribute__((section(".stk_mpu_shared_code")))
101#define STK_MPU_SHARED_BSS_SECTION __attribute__((section(".stk_mpu_shared_bss")))
117#define STK_MPU_KERNEL_DATA_SECTION __attribute__((section(".stk_mpu_kernel_data")))
132#define STK_MPU_KERNEL_CODE_SECTION __attribute__((section(".stk_mpu_kernel_code")))
153#define STK_MPU_KERNEL_BSS_SECTION __attribute__((section(".stk_mpu_kernel_bss")))
157#define STK_MPU_SHARED_DATA_SECTION
158#define STK_MPU_SHARED_CODE_SECTION
159#define STK_MPU_SHARED_BSS_SECTION
160#define STK_MPU_KERNEL_DATA_SECTION
161#define STK_MPU_KERNEL_CODE_SECTION
162#define STK_MPU_KERNEL_BSS_SECTION
217 return reinterpret_cast<Word>(ptr);
234 return reinterpret_cast<T *
>(value);
255#if STK_TLS && !STK_INLINE_TLS
290template <
class _TyTls>
304template <
class _TyTls>
347 #if STK_STRICT_COMPLIANCY
352 volatile const uint32_t *
const p_base =
reinterpret_cast<volatile const uint32_t *
>(addr);
361 __stk_full_memfence();
364 __stk_full_memfence();
366 while (hi != (*phi));
368 const uint64_t result = (
static_cast<uint64_t
>(hi) << 32U) |
static_cast<uint64_t
>(lo);
370 return static_cast<T
>(result);
409 #if STK_STRICT_COMPLIANCY
414 volatile uint32_t *
const p_base =
reinterpret_cast<volatile uint32_t *
>(addr);
421 (*phi) =
static_cast<uint32_t
>(
static_cast<uint64_t
>(value) >> 32U);
422 __stk_full_memfence();
424 (*plo) =
static_cast<uint32_t
>(value);
659#ifdef _STK_ARCH_X86_WIN32
700 const Cycles ticksu = (cycles * 1000000ULL) /
static_cast<Cycles>(freq);
702 ticks =
static_cast<Ticks>(ticksu);
715#ifndef _STK_CORTEX_M_TRUSTZONE_NON_SECURE
730#ifndef _STK_CUSTOM_MEMCPY
731static inline void STK_MEMCPY(
void *
const dest,
const void *
const src,
const size_t size)
735 if ((dest !=
nullptr) && (src !=
nullptr) && (size != 0U))
742 if (((dest_addr & 0x03U) == 0U) &&
743 ((src_addr & 0x03U) == 0U) &&
744 ((size & 0x03U) == 0U))
746 uint32_t *
const p_d32 =
static_cast<uint32_t *
>(dest);
747 const uint32_t *
const p_s32 =
static_cast<const uint32_t *
>(src);
748 const size_t words = (size >> 2U);
755 uint8_t *
const p_d =
static_cast<uint8_t *
>(dest);
756 const uint8_t *
const p_s =
static_cast<const uint8_t *
>(src);
765#ifndef _STK_CUSTOM_MEMSET
766static inline void STK_MEMSET(
void *
const dest,
const uint8_t value,
const size_t size)
770 if ((dest !=
nullptr) && (size != 0U))
775 if (((dest_addr & 0x03U) == 0U) &&
776 ((size & 0x03U) == 0U))
778 uint32_t *
const p_d32 =
static_cast<uint32_t *
>(dest);
779 const uint32_t word =
static_cast<uint32_t
>(value) * 0x01010101U;
780 const size_t words = (size >> 2U);
787 uint8_t *
const p_d =
static_cast<uint8_t *
>(dest);
Platform port for ARM Cortex-M.
Platform port for RISC-V.
Platform port for Windows Win32 (STK emulator).
void STK_PANIC_HANDLER_DEFAULT(stk::EKernelPanicId id)
Default panic handler: disable interrupts, record the id, and spin in a tight loop - a defined,...
static void STK_MEMCPY(void *const dest, const void *const src, const size_t size)
Implementation of STK_MEMCPY.
static void STK_MEMSET(void *const dest, const uint8_t value, const size_t size)
Implementation of STK_MEMSET.
#define STK_PANIC_HANDLER(id)
Compiler and platform low-level definitions for STK.
#define STK_UNUSED(X)
Explicitly marks a variable as unused to suppress compiler warnings.
#define STK_STATIC_ASSERT_N(NAME, X)
Compile-time assertion with a user-defined name suffix.
#define STK_ENDIAN_IDX_LO
Array index of the low 32-bit word when a 64-bit value is viewed as uint32_t[2].
#define __stk_forceinline
Forces compiler to always inline the decorated function, regardless of optimisation level.
#define __stk_aligned(x)
Specifies minimum alignment in bytes for the decorated variable or struct member (data instance prefi...
#define STK_NONCOPYABLE_CLASS(TYPE)
Disables copy construction and assignment for a class.
#define STK_ENDIAN_IDX_HI
Array index of the high 32-bit word when a 64-bit value is viewed as uint32_t[2].
#define __stk_constexpr_cpp17
constexpr definition for C++17 and above.
static void __stk_debug_break()
#define STK_STATIC_ASSERT(X)
Compile-time assertion. Produces a compilation error if X is false.
Namespace of STK package.
uintptr_t Word
Native processor word type.
static constexpr ITask * GetUserTaskFromTid(TId task_id) noexcept
Get task instance from its identifier.
int64_t Ticks
Ticks value.
EKernelPanicId
Identifies the source of a kernel panic.
static void STK_KERNEL_PANIC(stk::EKernelPanicId id)
Called when the kernel detects an unrecoverable internal fault.
static constexpr TId GetTidFromUserTask(const ITask *task) noexcept
Get task identifier from ITask instance.
uint64_t Cycles
Cycles value.
Word TId
Task (thread) id.
bool IsPrivilegedContext()
Check if caller context is Privileged.
static constexpr T * WordToPtr(Word value) noexcept
Cast a CPU register-width integer back to a pointer.
static void WriteVolatile64(volatile T *addr, T value)
Atomically write a 64-bit volatile value.
static constexpr Word PtrToWord(T *const ptr) noexcept
Cast a pointer to a CPU register-width integer.
static T ReadVolatile64(volatile const T *addr)
Atomically read a 64-bit volatile value.
bool IsInsideISR()
Check whether the CPU is currently executing inside a hardware interrupt service routine (ISR).
static constexpr Session DEFAULT_SESSION
Default session value passed to Enter()/Exit() when the caller does not need to force a specific hand...
CriticalSection()
Protected constructor (instantiation is prohibited).
static Session Enter(const Session ses=DEFAULT_SESSION)
Enter a critical section.
uint8_t Session
Opaque session token returned by Enter() and consumed by Exit().
CriticalSection(const CriticalSection &)=delete
static void Exit(const Session ses=DEFAULT_SESSION)
Exit a critical section.
ESessionFlags
Collection of session flags.
@ SESSION_FLAG_NPRIV
Calling context is non-Privileged.
ScopedLock()
Enter the critical section.
CriticalSection::Session m_ses
~ScopedLock()
Exit the critical section.
bool TryLock()
Attempt to acquire SpinLock in a single non-blocking attempt.
SpinLock()
Construct a SpinLock (unlocked by default).
EState
Internal lock state values.
@ UNLOCKED
Lock is free and available for acquisition.
@ LOCKED
Lock is held by a thread or core.
bool IsLocked() const
Sample current lock state.
volatile bool m_lock
Lock state (see EState). 4-byte aligned to prevent cache word boundary crossing on 32-bit cores.
void Lock()
Acquire SpinLock, blocking until it is available.
void Unlock()
Release SpinLock, allowing another thread or core to acquire it.
ScopedLock(SpinLock &sl)
Lock a spin lock instance.
~ScopedLock()
Unlock a spin lock instance.
High-resolution clock for high-precision measurements.
static uint32_t GetFrequency()
Get clock frequency.
static Ticks GetTimeUs()
Get elapsed time in microseconds.
static Cycles GetCycles()
Get number of clock cycles elapsed.
Interface for a user task.