forked from EffectiveRange/fw-mrhat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.c
70 lines (59 loc) · 1.55 KB
/
tasks.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
#include <stdlib.h>
#include "mcc_generated_files/system/system.h"
#include "tasks.h"
static volatile struct TaskDescr _tasks[TASK_NUM] = {};
static volatile int _has_task = 0;
void TASKS_Initialize() {
for (int i = 0; i < TASK_NUM; ++i) {
_tasks[i].task_fn = NULL;
_tasks[i].task_state = NULL;
_tasks[i].suspended = 0;
}
_has_task = 0;
}
void add_task(enum TASKS id, task_run_fun fn, void* state) {
const int diff = _tasks[id].task_fn == NULL || _tasks[id].suspended ? 1 : 0;
_tasks[id].task_fn = fn;
_tasks[id].task_state = state;
_tasks[id].suspended = 0;
_has_task += diff;
}
void resume_task(enum TASKS id) {
add_task(id, _tasks[id].task_fn, _tasks[id].task_state);
}
void rm_task(enum TASKS id) {
const int diff = _tasks[id].task_fn != NULL && _tasks[id].suspended == 0 ? 1 : 0;
_tasks[id].task_fn = NULL;
_tasks[id].task_state = NULL;
_tasks[id].suspended = 0;
_has_task -= diff;
}
void suspend_task(enum TASKS id) {
if (_tasks[id].task_fn != NULL) {
_tasks[id].suspended = 1;
_has_task -= 1;
}
}
static void __idle() {
CPUDOZEbits.IDLEN = 1;
SLEEP();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
}
void run_tasks() {
while (1) {
if (_has_task > 0) {
for (int i = 0; i < TASK_NUM; ++i) {
if (_tasks[i].task_fn != NULL && _tasks[i].suspended == 0) {
_tasks[i].task_fn(&_tasks[i]);
}
}
} else {
__idle();
}
}
}