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.h File Reference

Hardware Abstraction Layer (HAL) declarations for the stk::hw namespace. More...

#include "stk_defs.h"
Include dependency graph for stk_arch.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  stk::hw::CriticalSection
 Nestable, SMP-safe critical section that combines local interrupt masking with a global cross-core spinlock. More...
class  stk::hw::CriticalSection::ScopedLock
 RAII instance that enters the critical section on construction and exits it on destruction. More...
class  stk::hw::SpinLock
 Atomic busy-wait lock used as the global cross-core synchronisation primitive inside CriticalSection. More...
class  stk::hw::SpinLock::ScopedLock
 RAII guard that the spin lock is locked on construction and unlocked it on destruction. More...
class  stk::hw::HiResClock
 High-resolution clock for high-precision measurements. More...

Namespaces

namespace  stk
 Namespace of STK package.
namespace  stk::hw
 Hardware Abstraction Layer (HAL) for architecture-specific operations.

Macros

#define STK_PANIC_HANDLER(id)
#define STK_MPU_SHARED_DATA_SECTION
#define STK_MPU_SHARED_CODE_SECTION
#define STK_MPU_SHARED_BSS_SECTION
#define STK_MPU_KERNEL_DATA_SECTION
#define STK_MPU_KERNEL_CODE_SECTION
#define STK_MPU_KERNEL_BSS_SECTION

Functions

void STK_PANIC_HANDLER_DEFAULT (stk::EKernelPanicId id)
 Default panic handler: disable interrupts, record the id, and spin in a tight loop - a defined, detectable safe state.
static void stk::STK_KERNEL_PANIC (stk::EKernelPanicId id)
 Called when the kernel detects an unrecoverable internal fault.
template<typename T>
static constexpr Word stk::hw::PtrToWord (T *const ptr) noexcept
 Cast a pointer to a CPU register-width integer.
template<typename T>
static constexpr T * stk::hw::WordToPtr (Word value) noexcept
 Cast a CPU register-width integer back to a pointer.
bool stk::hw::IsInsideISR ()
 Check whether the CPU is currently executing inside a hardware interrupt service routine (ISR).
bool stk::hw::IsPrivilegedContext ()
 Check if caller context is Privileged.
template<typename T>
static T stk::hw::ReadVolatile64 (volatile const T *addr)
 Atomically read a 64-bit volatile value.
template<typename T>
static void stk::hw::WriteVolatile64 (volatile T *addr, T value)
 Atomically write a 64-bit volatile value.
static constexpr TId stk::GetTidFromUserTask (const ITask *task) noexcept
 Get task identifier from ITask instance.
static constexpr ITaskstk::GetUserTaskFromTid (TId task_id) noexcept
 Get task instance from its identifier.
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.

Detailed Description

Hardware Abstraction Layer (HAL) declarations for the stk::hw namespace.

Selects and includes the correct architecture back-end header based on the active architecture macro (_STK_ARCH_ARM_CORTEX_M, _STK_ARCH_RISC_V, _STK_ARCH_X86_WIN32), then declares the portable stk::hw interface that the rest of the kernel uses:

  • Thread-local storage (TLS) read/write: GetTls, SetTls, GetTlsPtr, SetTlsPtr.
  • ISR context detection: IsInsideISR.
  • Preemption control: CriticalSection (nestable, single-core) and SpinLock (SMP).
  • Lock-free 64-bit volatile I/O: ReadVolatile64, WriteVolatile64.

Definition in file stk_arch.h.

Macro Definition Documentation

◆ STK_MPU_KERNEL_BSS_SECTION

#define STK_MPU_KERNEL_BSS_SECTION

Definition at line 162 of file stk_arch.h.

◆ STK_MPU_KERNEL_CODE_SECTION

#define STK_MPU_KERNEL_CODE_SECTION

Definition at line 161 of file stk_arch.h.

◆ STK_MPU_KERNEL_DATA_SECTION

#define STK_MPU_KERNEL_DATA_SECTION

Definition at line 160 of file stk_arch.h.

◆ STK_MPU_SHARED_BSS_SECTION

#define STK_MPU_SHARED_BSS_SECTION

Definition at line 159 of file stk_arch.h.

◆ STK_MPU_SHARED_CODE_SECTION

#define STK_MPU_SHARED_CODE_SECTION

Definition at line 158 of file stk_arch.h.

◆ STK_MPU_SHARED_DATA_SECTION

#define STK_MPU_SHARED_DATA_SECTION

Definition at line 157 of file stk_arch.h.

◆ STK_PANIC_HANDLER

#define STK_PANIC_HANDLER ( id)
Value:
void STK_PANIC_HANDLER_DEFAULT(stk::EKernelPanicId id)
Default panic handler: disable interrupts, record the id, and spin in a tight loop - a defined,...

Definition at line 59 of file stk_arch.h.

Referenced by stk::STK_KERNEL_PANIC().

Function Documentation

◆ STK_MEMCPY()

void STK_MEMCPY ( void *const dest,
const void *const src,
const size_t size )
inlinestatic

Implementation of STK_MEMCPY.

Definition at line 731 of file stk_arch.h.

732{
733 using namespace stk;
734
735 if ((dest != nullptr) && (src != nullptr) && (size != 0U))
736 {
737 const Word dest_addr = hw::PtrToWord(dest);
738 const Word src_addr = hw::PtrToWord(src);
739
740 // fast path: check if destination, source, and size are all 4-byte aligned
741 // then copy data in 4-byte chunks
742 if (((dest_addr & 0x03U) == 0U) &&
743 ((src_addr & 0x03U) == 0U) &&
744 ((size & 0x03U) == 0U))
745 {
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);
749
750 STK_UNUSED(std::copy_n(p_s32, words, p_d32));
751 }
752 // slow path
753 else
754 {
755 uint8_t *const p_d = static_cast<uint8_t *>(dest);
756 const uint8_t *const p_s = static_cast<const uint8_t *>(src);
757
758 STK_UNUSED(std::copy_n(p_s, size, p_d));
759 }
760 }
761}
#define STK_UNUSED(X)
Explicitly marks a variable as unused to suppress compiler warnings.
Definition stk_defs.h:715
Namespace of STK package.
uintptr_t Word
Native processor word type.
Definition stk_common.h:143
static constexpr Word PtrToWord(T *const ptr) noexcept
Cast a pointer to a CPU register-width integer.
Definition stk_arch.h:214

References stk::hw::PtrToWord(), and STK_UNUSED.

Referenced by stk::sync::Pipe::DrainLocked(), stk::sync::MessageQueue::Get(), osKernelGetInfo(), stk::sync::MessageQueue::Peek(), stk::sync::MessageQueue::PeekFront(), stk::sync::MessageQueue::Put(), stk::sync::MessageQueue::PutFront(), stk::sync::Pipe::Read(), stk::sync::PipeT< Timer *, 32U >::ReadBulk(), stk::sync::Pipe::Write(), stk::sync::Pipe::WriteBulk(), stk::sync::PipeT< Timer *, 32U >::WriteBulk(), xMessageBufferReceive(), xMessageBufferReceiveFromISR(), xMessageBufferSend(), and xMessageBufferSendFromISR().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ STK_MEMSET()

void STK_MEMSET ( void *const dest,
const uint8_t value,
const size_t size )
inlinestatic

Implementation of STK_MEMSET.

Definition at line 766 of file stk_arch.h.

767{
768 using namespace stk;
769
770 if ((dest != nullptr) && (size != 0U))
771 {
772 const Word dest_addr = hw::PtrToWord(dest);
773
774 // fast path: destination and size are 4-byte aligned, fill in 4-byte chunks
775 if (((dest_addr & 0x03U) == 0U) &&
776 ((size & 0x03U) == 0U))
777 {
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);
781
782 STK_UNUSED(std::fill_n(p_d32, words, word));
783 }
784 // slow path
785 else
786 {
787 uint8_t *const p_d = static_cast<uint8_t *>(dest);
788
789 STK_UNUSED(std::fill_n(p_d, size, value));
790 }
791 }
792}

References stk::hw::PtrToWord(), and STK_UNUSED.

Here is the call graph for this function:

◆ STK_PANIC_HANDLER_DEFAULT()

void STK_PANIC_HANDLER_DEFAULT ( stk::EKernelPanicId id)
extern

Default panic handler: disable interrupts, record the id, and spin in a tight loop - a defined, detectable safe state.

Note
On a system with a watchdog enabled this will trigger a watchdog reset after the watchdog period, which is the desired behaviour.
Replace with a platform-specific handler (e.g. one that writes a fault log to non-volatile memory and calls NVIC_SystemReset()) by defining STK_PANIC_HANDLER in stk_config.h.