blob: 4a5ff85bf335792f2b5109bbf2e2e6da35822442 (
plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
#if !(defined(LL) || defined(FLAT))
#include "../sys/schedule.h"
#include "../lib/q.h"
#include "../lib/mem.h"
#include "../drivers/uart.h"
extern void preserveregs(void*);
extern void restoreregs(void*);
extern void exetask(void*);
static unsigned long next_pid = 3;
unsigned char table[256] = {0, };
static struct __attribute__((packed,align(4))) Q_base bq = {
.next = 0,
.last = 0,
};
struct __attribute__((packed,align(4))) Scheduler scheduler = {
.running_pid = 0,
.tasks = &bq,
};
extern unsigned long __stacks_start;
unsigned long getstack(void)
{
for(unsigned int i = 0; i < 256; i++) {
if (table[i] == 0) {
table[i] = 1;
return i;
}
}
return -1;
}
void add_fxn(void (*task)(void), unsigned char priority)
{
struct Task* t = (struct Task*)malloc(sizeof(struct Task));
// Allocate a stack frame and space for registers to be preserved on context switches
t->priority = priority;
t->task = task;
t->stacki = getstack();
t->pid = next_pid;
next_pid += 1;
if(t->stacki > 256)
t->stack = 0;
else
t->stack = (void*)(__stacks_start + STACK_SIZE*t->stacki);
t->status = S_READY;
for(unsigned char i = 0; i < 13; i++)
t->reg[i] = 0;
t->reg[13] = (unsigned long)t->stack;
t->reg[14] = 0; // LR
t->reg[15] = (unsigned long)task; // PC
pushq(scheduler.tasks, t);
}
unsigned char exists(void (*task)(void))
{
if (scheduler.tasks->next == 0)
return 0;
struct Q* q = scheduler.tasks->next;
while (q != 0) {
struct Task* t = q->data;
if (t->task == task)
return 1;
q = q->next;
}
return 0;
}
void add_no_repeat(void (*task)(void), unsigned char priority)
{
if (!(exists(task)))
add_fxn(task, priority);
}
unsigned int get_task_length(void)
{
unsigned int length = 0;
if (scheduler.tasks->last == 0)
return length;
else if (scheduler.tasks->next == scheduler.tasks->last)
return 1;
else {
struct Q* q = scheduler.tasks->next;
length += 1;
while (q->next != 0) {
q = q->next;
length += 1;
}
return length;
}
}
void execute_task(void)
{
// Preserve Current Running PID's Data
if (scheduler.tasks->last != 0) {
struct Q* q = (struct Q*)scheduler.tasks->next;
while (((struct Task*)q->data)->status == S_WAITING) {
q = q->next;
}
struct Task* tsk = (struct Task*)q->data;
// Restore registers
// Including program counter as the entry point
// Including allocated stack position
// Set lr to the return address to restore system stack
//preserveregs(tsk->reg);
//restoreregs(tsk->reg);
scheduler.running_pid = tsk->pid;
exetask(tsk->reg);
scheduler.running_pid = 0;
table[tsk->stacki] = 0;
popq(scheduler.tasks);
}
}
#endif
|