Skip to content

Commit eac1557

Browse files
committed
Moved scheduler start to mcu rtos details
1 parent 060cc54 commit eac1557

2 files changed

Lines changed: 26 additions & 16 deletions

File tree

klib/rtos/scheduler.hpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -123,22 +123,8 @@ namespace klib::rtos {
123123
// add the idle task to the scheduler. This task is always ready to run
124124
create_task(&idle_task);
125125

126-
// set the stack pointer so we dont access any wrong memory
127-
// before starting the scheduler. Wrong memory in this case
128-
// is whatever is left in the process stack pointer
129-
uint32_t psp_stack[32] = {};
130-
131-
// switch to the array for the process stack pointer
132-
__set_PSP(reinterpret_cast<uint32_t>(&psp_stack[(sizeof(psp_stack) / sizeof(psp_stack[0]))]));
133-
134-
// switch to the process stack pointer and start executing in unprivileged mode
135-
asm volatile(
136-
"msr control, %0\n"
137-
"isb\n"
138-
:
139-
: "r" (0x3) // unprivileged, use PSP
140-
: "memory"
141-
);
126+
// start the scheduler from the target
127+
klib::target::rtos::detail::scheduler_start();
142128

143129
// endless loop to wait on the start of the scheduler
144130
while (true) {

targets/arm/rtos/cortex-m3.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,30 @@ namespace klib::rtos::cortex_m3 {
168168
: "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11", "memory"
169169
);
170170
}
171+
172+
/**
173+
* @brief Start the scheduler by switching to unprivileged mode and
174+
* switching to the process stack pointer
175+
*
176+
*/
177+
static void scheduler_start() {
178+
// set the stack pointer so we dont access any wrong memory
179+
// before starting the scheduler. Wrong memory in this case
180+
// is whatever is left in the process stack pointer
181+
uint32_t psp_stack[32] = {};
182+
183+
// switch to the array for the process stack pointer
184+
__set_PSP(reinterpret_cast<uint32_t>(&psp_stack[(sizeof(psp_stack) / sizeof(psp_stack[0]))]));
185+
186+
// switch to the process stack pointer and start executing in unprivileged mode
187+
asm volatile(
188+
"msr control, %0\n"
189+
"isb\n"
190+
:
191+
: "r" (0x3) // unprivileged, use PSP
192+
: "memory"
193+
);
194+
}
171195
}
172196

173197
#endif

0 commit comments

Comments
 (0)