SuperTinyKernel™ RTOS 1.05.3
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
stk_arch_common.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_COMMON_H_
11#define STK_ARCH_COMMON_H_
12
16
17#include "stk_common.h"
18
19namespace stk {
20
25{
26public:
27 explicit PlatformContext() : m_handler(nullptr), m_service(nullptr), m_stack_idle(nullptr),
29 {}
30
36
42 virtual void Initialize(IPlatform::IEventHandler *handler, IKernelService *service, Stack *exit_trap,
43 uint32_t resolution_us)
44 {
45 m_handler = handler;
46 m_service = service;
47 m_stack_idle = exit_trap;
48 m_stack_active = nullptr;
49 m_tick_resolution = resolution_us;
50 }
51
57 static inline Word *InitStackMemory(IStackMemory *memory)
58 {
59 size_t stack_size = memory->GetStackSize();
60 Word *itr = memory->GetStack();
61 Word *stack_top = itr + stack_size;
62
63 STK_ASSERT(stack_size >= STACK_SIZE_MIN);
64
65 // initialization of the stack memory satisfies stack integrity check in Kernel::StateSwitch
66 while (itr < stack_top)
68
69 // expecting STK_STACK_MEMORY_ALIGN-byte aligned memory for a stack
70 STK_ASSERT((hw::PtrToWord(stack_top) & (STK_STACK_MEMORY_ALIGN - 1)) == 0U);
71
72 return stack_top;
73 }
74
80
81protected:
83};
84
88#ifndef STK_ARCH_GET_CPU_ID
89 #define STK_ARCH_GET_CPU_ID() (0)
90#endif
91
95#ifndef _STK_UNDER_TEST
96 #define GetContext() s_StkPlatformContext[STK_ARCH_GET_CPU_ID()]
97#endif
98
105{
106 return ((clock_freq * static_cast<Cycles>(time_us)) / 1000000ULL);
107}
108
109} // namespace stk
110
111#endif /* STK_ARCH_COMMON_H_ */
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:104
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:330
#define STK_STACK_MEMORY_ALIGN
Stack memory alignment.
Definition stk_defs.h:389
#define STK_STACK_MEMORY_FILLER
Sentinel value written to the entire stack region at initialization (stack watermark pattern).
Definition stk_defs.h:377
Namespace of STK package.
uintptr_t Word
Native processor word type.
Definition stk_common.h:112
static __stk_forceinline Cycles ConvertTimeUsToClockCycles(Cycles clock_freq, Ticks time_us)
Convert time (microseconds) to core clock cycles.
int64_t Ticks
Ticks value.
Definition stk_common.h:150
@ STACK_SIZE_MIN
Minimum stack size in elements of Word. Used as a lower bound for all stack allocations (user task,...
Definition stk_common.h:83
uint64_t Cycles
Cycles value.
Definition stk_common.h:155
__stk_forceinline Word PtrToWord(T *ptr) noexcept
Cast a pointer to a CPU register-width integer.
Definition stk_arch.h:94
virtual void Initialize(IPlatform::IEventHandler *handler, IKernelService *service, Stack *exit_trap, uint32_t resolution_us)
Initialize context.
IPlatform::IEventHandler * m_handler
kernel event handler
Stack * m_stack_active
active task stack
~PlatformContext()
Destructor.
static Word * InitStackMemory(IStackMemory *memory)
Initialize stack memory by filling it with STK_STACK_MEMORY_FILLER.
Stack * m_stack_idle
idle task stack
IKernelService * m_service
kernel service
uint32_t m_tick_resolution
system tick resolution (microseconds)
STK_NONCOPYABLE_CLASS(PlatformContext)
Stack descriptor.
Definition stk_common.h:181
Interface for a stack memory region.
Definition stk_common.h:193
virtual size_t GetStackSize() const =0
Get number of elements of the stack memory array.
virtual Word * GetStack() const =0
Get pointer to the stack memory.
Interface for a back-end event handler.
Definition stk_common.h:583
Interface for the kernel services exposed to the user processes during run-time when Kernel started s...
Definition stk_common.h:929