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...

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)

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 __stk_forceinline 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 __stk_forceinlinestk::hw::ReadVolatile64 (volatile const T *addr)
 Atomically read a 64-bit volatile value.
template<typename T>
static __stk_forceinline 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_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 57 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 623 of file stk_arch.h.

624{
625 using namespace stk;
626
627 if ((dest != nullptr) && (src != nullptr) && (size != 0U))
628 {
629 const Word dest_addr = hw::PtrToWord(dest);
630 const Word src_addr = hw::PtrToWord(src);
631
632 // fast path: check if destination, source, and size are all 4-byte aligned
633 // then copy data in 4-byte chunks
634 if (((dest_addr & 0x03U) == 0U) &&
635 ((src_addr & 0x03U) == 0U) &&
636 ((size & 0x03U) == 0U))
637 {
638 uint32_t *const p_d32 = static_cast<uint32_t *>(dest);
639 const uint32_t *const p_s32 = static_cast<const uint32_t *>(src);
640 const size_t words = (size >> 2U);
641
642 STK_UNUSED(std::copy_n(p_s32, words, p_d32));
643 }
644 // slow path
645 else
646 {
647 uint8_t *const p_d = static_cast<uint8_t *>(dest);
648 const uint8_t *const p_s = static_cast<const uint8_t *>(src);
649
650 STK_UNUSED(std::copy_n(p_s, size, p_d));
651 }
652 }
653}
#define STK_UNUSED(X)
Explicitly marks a variable as unused to suppress compiler warnings.
Definition stk_defs.h:654
Namespace of STK package.
uintptr_t Word
Native processor word type.
Definition stk_common.h:140
static constexpr Word PtrToWord(T *const ptr) noexcept
Cast a pointer to a CPU register-width integer.
Definition stk_arch.h:106

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 658 of file stk_arch.h.

659{
660 using namespace stk;
661
662 if ((dest != nullptr) && (size != 0U))
663 {
664 const Word dest_addr = hw::PtrToWord(dest);
665
666 // fast path: destination and size are 4-byte aligned, fill in 4-byte chunks
667 if (((dest_addr & 0x03U) == 0U) &&
668 ((size & 0x03U) == 0U))
669 {
670 uint32_t *const p_d32 = static_cast<uint32_t *>(dest);
671 const uint32_t word = static_cast<uint32_t>(value) * 0x01010101U;
672 const size_t words = (size >> 2U);
673
674 STK_UNUSED(std::fill_n(p_d32, words, word));
675 }
676 // slow path
677 else
678 {
679 uint8_t *const p_d = static_cast<uint8_t *>(dest);
680
681 STK_UNUSED(std::fill_n(p_d, size, value));
682 }
683 }
684}

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.