SuperTinyKernel™ RTOS 1.07.x
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
35
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
58 {
59 STK_ASSERT(memory != nullptr);
60
61 ArrayView<Word> stack(const_cast<Word *>(memory->GetStack()), memory->GetStackSize());
63
64 // initialize stack memory to satisfy stack integrity check in Kernel::StateSwitch
65 for (size_t i = 0U; i < stack.GetSize(); ++i)
66 {
67 stack[i] = STK_STACK_MEMORY_FILLER;
68 }
69
70 // get address of the last valid item and step forward by 1 Word size to point to the top
71 const Word stack_top = hw::PtrToWord(memory->GetStack()) + (stack.GetSize() * sizeof(Word));
72
73 // expecting STK_STACK_MEMORY_ALIGN-byte aligned memory for a stack
74 STK_ASSERT((stack_top & (STK_STACK_MEMORY_ALIGN - 1U)) == 0U);
75
76 return stack_top;
77 }
78
84
85protected:
87};
88
92#ifndef STK_ARCH_GET_CPU_ID
93 #define STK_ARCH_GET_CPU_ID() (0)
94#endif
95
99#ifndef _STK_UNDER_TEST
100 #define GetContext() s_StkPlatformContext[STK_ARCH_GET_CPU_ID()]
101#endif
102
108static __stk_forceinline Cycles ConvertTimeUsToClockCycles(uint32_t clock_freq, Ticks time_us)
109{
110 return ((static_cast<Cycles>(time_us) * clock_freq) / 1000000ULL);
111}
112
113} // namespace stk
114
115#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:196
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:430
#define STK_STACK_MEMORY_ALIGN
Stack memory alignment.
Definition stk_defs.h:489
#define STK_STACK_MEMORY_FILLER
Sentinel value written to the entire stack region at initialization (stack watermark pattern).
Definition stk_defs.h:477
#define STK_VIRT_DTOR
Makes destructors virtual and compliant to strict rules if STK_STRICT_COMPLIANCY=0.
Definition stk_defs.h:180
Namespace of STK package.
uintptr_t Word
Native processor word type.
Definition stk_common.h:140
static __stk_forceinline Cycles ConvertTimeUsToClockCycles(uint32_t clock_freq, Ticks time_us)
Convert time (microseconds) to core clock cycles.
@ 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:91
int64_t Ticks
Ticks value.
Definition stk_common.h:155
uint64_t Cycles
Cycles value.
Definition stk_common.h:165
static constexpr Word PtrToWord(T *const ptr) noexcept
Cast a pointer to a CPU register-width integer.
Definition stk_arch.h:106
Memory-related primitives.
virtual void Initialize(IPlatform::IEventHandler *handler, IKernelService *service, Stack *exit_trap, uint32_t resolution_us)
Initialize context.
IPlatform::IEventHandler * m_handler
kernel event handler
static Word InitStackMemory(IStackMemory *const memory)
Initialize stack memory by filling it with STK_STACK_MEMORY_FILLER.
Stack * m_stack_active
active task stack
STK_VIRT_DTOR ~PlatformContext()=default
Destructor.
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)
Lightweight, non-owning view over a contiguous sequence of elements.
Definition stk_common.h:251
size_t GetSize() const
Get number of elements in the view.
Definition stk_common.h:275
Stack descriptor.
Definition stk_common.h:316
Interface for a stack memory region.
Definition stk_common.h:334
Interface for a back-end event handler.
Definition stk_common.h:899
Interface for the kernel services exposed to the user processes during run-time when Kernel started s...