-
Notifications
You must be signed in to change notification settings - Fork 0
/
sif.c
84 lines (62 loc) · 1.72 KB
/
sif.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// SPDX-License-Identifier: MPL-2.0
/*
* sif.c -- a preemptive rtos
* Copyright (C) 2024 Jacob Koziej <[email protected]>
*/
#include <sif/private/sif.h>
#include <sif/sif.h>
#include <sif/task.h>
sif_t sif;
void sif_init(void)
{
sif_port_init();
}
sif_task_stack_t *sif_pendsv(sif_task_stack_t * const sp)
{
const sif_word_t coreid = sif_port_get_coreid();
sif_core_t * const core = sif.cores + coreid;
if (!core->running) {
if (core->queued) {
sif_task_time_t * const time = &core->idle_time;
sif_task_time_t * const prev_time = &core->prev_time;
sif_task_update_time(prev_time, time);
sif_task_t * const task = core->queued;
task->state = SIF_TASK_STATE_ACTIVE;
core->running = task;
core->queued = NULL;
core->priority = task->priority;
return task->stack.sp;
}
sif_task_idle(sp);
return sp;
}
if (!core->queued) {
sif_task_t * const task = core->running;
if (task->state == SIF_TASK_STATE_ACTIVE) return sp;
sif_task_idle(sp);
return sp;
}
return sif_task_context_switch(sp);
}
sif_task_time_t sif_system_time(void)
{
const sif_word_t coreid = sif_port_get_coreid();
sif_core_t * const core = sif.cores + coreid;
sif_task_time_t * const system_time = &core->system_time;
sif_task_time_t * const prev_count = &core->prev_count;
const sif_task_time_t current_count = sif_port_systick_current_value();
if (sif_port_systick_count_flag()) {
*system_time += *prev_count;
*prev_count = *SIF_PORT_SYSTICK_RELOAD;
}
*system_time
+= (*prev_count - current_count + *SIF_PORT_SYSTICK_RELOAD + 1)
% (*SIF_PORT_SYSTICK_RELOAD + 1);
*prev_count = current_count;
return *system_time;
}
void sif_systick(void)
{
sif_task_systick();
sif_task_reschedule();
}