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_risc-v.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_RISC_V_H_
11#define STK_ARCH_RISC_V_H_
12
13#include "stk_common.h"
14
18
19namespace stk {
20
25{
26public:
31
36 {
37 public:
41 virtual bool OnException(Word cause) = 0;
42 };
43
44 void Initialize(IEventHandler *event_handler, IKernelService *service, uint32_t resolution_us, Stack *exit_trap) override;
45 void Start() override;
46 void Stop() override;
47 void InitStack(EStackType stack_type, Stack *stack, IStackMemory *stack_memory, ITask *user_task) override;
48 uint32_t GetTickResolution() const override;
49 Cycles GetSysTimerCount() const override;
50 uint32_t GetSysTimerFrequency() const override;
51 void SwitchToNext() override;
52 void ForceContextSwitch(TId id) override;
53 void Sleep(Timeout ticks) override;
54 bool SleepUntil(Ticks timestamp) override;
55 EWaitResult Wait(ISyncObject *sync_obj, IMutex *mutex, Timeout timeout) override;
56 void ProcessTick() override;
57 void ProcessHardFault() override;
58 void SetEventOverrider(IEventOverrider *overrider, bool non_secure) override;
59 Word GetCallerSP() const override;
60 TId GetTid() const override;
61 Timeout Suspend() override;
62 void Resume(Timeout elapsed_ticks) override;
63 void SetCpuFrequency(uint8_t core_id, uint32_t frequency) override;
64
66};
67
72
73// Inline TLS via x4/tp. Active when both STK_TLS and STK_TLS_PREFER_REGISTER are enabled.
74#if STK_TLS && STK_TLS_PREFER_REGISTER
75
80static __stk_forceinline Word GetTls()
81{
82 Word tp;
83 __asm volatile("mv %0, tp" : "=r"(tp) : /* input: none */ : /* clobbers: none */);
84 return tp;
85}
86
91static __stk_forceinline void SetTls(Word tp)
92{
93 __asm volatile("mv tp, %0" : /* output: none */ : "r"(tp) : /* clobbers: none */);
94}
95
96// Notify stk_arch.h that we defined inline versions of GetTls/SetTls.
97#define STK_INLINE_TLS (1)
98
99#endif // STK_TLS && STK_TLS_PREFER_REGISTER
100
101} // namespace stk
102
105static __stk_forceinline void __stk_dmb() { __asm volatile("fence rw, rw" ::: "memory"); }
106
111#ifndef STK_SUBMICORSECOND_PRECISION_TIMER
112 #define STK_SUBMICORSECOND_PRECISION_TIMER 0
113#endif
114
118#ifndef STK_SYSTEM_CORE_CLOCK_FREQUENCY
119 #define STK_SYSTEM_CORE_CLOCK_FREQUENCY 150000000U
120#endif
121
125extern "C" volatile uint32_t STK_SYSTEM_CORE_CLOCK_VAR;
126
127#endif /* STK_ARCH_RISC_V_H_ */
static __stk_forceinline void __stk_dmb()
Data memory barrier.
volatile uint32_t STK_SYSTEM_CORE_CLOCK_VAR
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_VIRT_DTOR
Makes destructors virtual and compliant to strict rules if STK_STRICT_COMPLIANCY=0.
Definition stk_defs.h:261
Namespace of STK package.
uintptr_t Word
Native processor word type.
Definition stk_common.h:143
EWaitResult
Wait result (see IKernelService::Wait).
Definition stk_common.h:121
int64_t Ticks
Ticks value.
Definition stk_common.h:158
int32_t Timeout
Timeout time (ticks).
Definition stk_common.h:153
PlatformArmCortexM PlatformDefault
Default platform implementation.
EStackType
Stack type.
Definition stk_common.h:79
uint64_t Cycles
Cycles value.
Definition stk_common.h:168
Word TId
Task (thread) id.
Definition stk_common.h:148
Concrete implementation of IPlatform driver for the Risc-V processors.
Cycles GetSysTimerCount() const override
Get system timer count value.
Word GetCallerSP() const override
Get caller's Stack Pointer (SP).
void Resume(Timeout elapsed_ticks) override
Resume scheduling after a prior Suspend() call.
void Start() override
Start scheduling.
uint32_t GetTickResolution() const override
Get resolution of the system tick timer in microseconds. Resolution means a number of microseconds be...
bool SleepUntil(Ticks timestamp) override
Put calling process into a sleep state until the specified timestamp.
uint32_t GetSysTimerFrequency() const override
Get system timer frequency.
void ProcessHardFault() override
Cause a hard fault of the system.
void InitStack(EStackType stack_type, Stack *stack, IStackMemory *stack_memory, ITask *user_task) override
Initialize stack memory of the user task.
void ProcessTick() override
Process one tick.
TId GetTid() const override
Get thread Id.
void ForceContextSwitch(TId id) override
Force context switch.
EWaitResult Wait(ISyncObject *sync_obj, IMutex *mutex, Timeout timeout) override
Put calling process into a waiting state until synchronization object is signaled or timeout occurs.
void SetSpecificEventHandler(ISpecificEventHandler *handler)
void SetCpuFrequency(uint8_t core_id, uint32_t frequency) override
Notify the scheduler of a CPU core's operating frequency.
void SetEventOverrider(IEventOverrider *overrider, bool non_secure) override
Set platform event overrider.
void Stop() override
Stop scheduling.
void SwitchToNext() override
Switch to a next task.
STK_VIRT_DTOR ~PlatformRiscV()=default
Destructor.
Timeout Suspend() override
Suspend scheduling.
void Sleep(Timeout ticks) override
Put calling process into a sleep state.
void Initialize(IEventHandler *event_handler, IKernelService *service, uint32_t resolution_us, Stack *exit_trap) override
Initialize scheduler's context.
virtual bool OnException(Word cause)=0
Called by ISR handler on IRQ_XXX (see encoding.h).
Stack descriptor.
Definition stk_common.h:392
Interface for a stack memory region.
Definition stk_common.h:413
Synchronization object interface.
Definition stk_common.h:564
Interface for mutex synchronization primitive.
Definition stk_common.h:697
Interface for a user task.
Definition stk_common.h:755
Interface for a platform driver.
Definition stk_common.h:978
Interface for a back-end event handler.
Definition stk_common.h:986
Interface for a platform event overrider.
Interface for the kernel services exposed to the user processes during run-time when Kernel started s...