-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.c
More file actions
120 lines (111 loc) · 2.21 KB
/
task.c
File metadata and controls
120 lines (111 loc) · 2.21 KB
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
119
120
/*
* task.c
*
* Created: 2/16/2018 6:30:38 PM
* Author: embedded
*/
#include "task.h"
// keeps track of number of timer interrupts
uint16_t count = 0;
// initializes the task list
void initScheduler(void)
{
// use 1/8 of system clock frequency
TCCR0B |= (1<<CS01);
// initialize timer value = 0
TCNT0 = 0;
// enable Timer0 Overflow interrupt
TIMSK0 |= (1<<TOIE0);
for(uint8_t i=0; i<MAX_TASKS; i++)
{
task_list[i].id = 0;
task_list[i].task = (task_t)0x00;
task_list[i].delay = 0;
task_list[i].period = 0;
task_list[i].status = STOPPED;
}
}
// adds a new task to the task list
// scans through the list and
// places the new task data where
// it finds free space
void addTask(uint8_t id, task_t task, uint16_t period)
{
uint8_t idx = 0, done = 0x00;
while( idx < MAX_TASKS )
{
if( task_list[idx].status == STOPPED )
{
task_list[idx].id = id;
task_list[idx].task = task;
task_list[idx].delay = period;
task_list[idx].period = period;
task_list[idx].status = RUNNABLE;
done = 0x01;
}
if( done ) break;
idx++;
}
}
// remove task from task list
// note STOPPED is equivalent
// to removing a task
void deleteTask(uint8_t id)
{
for(uint8_t i=0;i<MAX_TASKS;i++)
{
if( task_list[i].id == id )
{
task_list[i].status = STOPPED;
break;
}
}
}
// gets the task status
// returns ERROR if id is invalid
uint8_t getTaskStatus(uint8_t id)
{
for(uint8_t i=0;i<MAX_TASKS;i++)
{
if( task_list[i].id == id )
return task_list[i].status;
}
return ERROR;
}
// dispatches tasks when they are ready to run
void dispatchTasks(void)
{
for(uint8_t i=0;i<MAX_TASKS;i++)
{
// check for a valid task ready to run
if( !task_list[i].delay &&
task_list[i].status == RUNNABLE )
{
// task is now running
task_list[i].status = RUNNING;
// call the task
(*task_list[i].task)();
// reset the delay
task_list[i].delay =
task_list[i].period;
// task is runnable again
task_list[i].status = RUNNABLE;
}
}
}
// generates a "tick"
// each tick 100ms apart
ISR(TIMER0_OVF_vect)
{
count ++;
if( count == 392 )
{
count = 0;
// cycle through available tasks
for(uint8_t i=0;i<MAX_TASKS;i++)
{
if( task_list[i].status == RUNNABLE )
task_list[i].delay--;
}
}
}