SuperTinyKernel™ RTOS 1.08.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
37 void InitializeBase(IPlatform::IEventHandler *handler, IKernelService *service, Stack *exit_trap,
38 uint32_t resolution_us)
39 {
40 m_handler = handler;
41 m_service = service;
42 m_stack_idle = exit_trap;
43 m_stack_active = nullptr;
44 m_tick_resolution = resolution_us;
45 }
46
53 {
54 STK_ASSERT(memory != nullptr);
55
56 ArrayView<Word> stack(const_cast<Word *>(memory->GetStack()), memory->GetStackSize());
58
59 // initialize stack memory to satisfy stack integrity check in Kernel::StateSwitch
60 for (size_t i = 0U; i < stack.GetSize(); ++i)
61 {
62 stack[i] = STK_STACK_MEMORY_FILLER;
63 }
64
65 // get address of the last valid item and step forward by 1 Word size to point to the top
66 const Word stack_top = hw::PtrToWord(memory->GetStack()) + (stack.GetSize() * sizeof(Word));
67
68 // expecting STK_STACK_MEMORY_ALIGN-byte aligned memory for a stack
69 STK_ASSERT((stack_top & (STK_STACK_MEMORY_ALIGN - 1U)) == 0U);
70
71 return stack_top;
72 }
73
79
80protected:
82
86 ~PlatformContext() = default;
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:277
#define STK_ASSERT(e)
Runtime assertion. Halts execution if the expression e evaluates to false.
Definition stk_defs.h:516
#define STK_STACK_MEMORY_ALIGN
Stack memory alignment.
Definition stk_defs.h:575
#define STK_STACK_MEMORY_FILLER
Sentinel value written to the entire stack region at initialization (stack watermark pattern).
Definition stk_defs.h:563
Namespace of STK package.
uintptr_t Word
Native processor word type.
Definition stk_common.h:143
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:92
int64_t Ticks
Ticks value.
Definition stk_common.h:158
uint64_t Cycles
Cycles value.
Definition stk_common.h:168
static constexpr Word PtrToWord(T *const ptr) noexcept
Cast a pointer to a CPU register-width integer.
Definition stk_arch.h:214
Memory-related primitives.
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
Stack * m_stack_idle
idle task stack
void InitializeBase(IPlatform::IEventHandler *handler, IKernelService *service, Stack *exit_trap, uint32_t resolution_us)
Initialize context.
~PlatformContext()=default
Destructor.
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:254
size_t GetSize() const
Get number of elements in the view.
Definition stk_common.h:294
Stack descriptor.
Definition stk_common.h:392
Interface for a stack memory region.
Definition stk_common.h:413
Interface for a back-end event handler.
Definition stk_common.h:986
Interface for the kernel services exposed to the user processes during run-time when Kernel started s...