-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.c
220 lines (202 loc) · 7.43 KB
/
scheduler.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*==================================================================
File Name : scheduler.c
Author : Emile
------------------------------------------------------------------
Purpose : This files contains all the functions for adding and
executing tasks in a cooperative (non pre-emptive) way.
------------------------------------------------------------------
Soft Power-On for Power-Amps (SPOPA) is free software: you can
redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation, either
version 3 of the License, or (at your option) any later version.
SPOPA is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SPOPA. If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------
$Log: $
==================================================================
*/
#include "scheduler.h"
#include <string.h>
task_struct task_list[MAX_TASKS]; // struct with all tasks
uint8_t max_tasks = 0;
/*-----------------------------------------------------------------------------
Purpose : Initialization function for scheduler. Should be called before
calling any other scheduler function.
Variables: task_list[] structure
Returns : -
---------------------------------------------------------------------------*/
void scheduler_init(void)
{
memset(task_list,0x00,sizeof(task_list)); // clear task_list array
} // scheduler_init()
/*-----------------------------------------------------------------------------
Purpose : Run-time function for scheduler. Should be called from within
an ISR. This function goes through the task-list and decrements
eacht task counter. On time-out, the ready flag is set.
Variables: task_list[] structure
Returns : -
---------------------------------------------------------------------------*/
void scheduler_isr(void)
{
uint8_t index = 0; // index in task_list struct
while ((index < MAX_TASKS) && task_list[index].pFunction)
{
//First go through the initial delay
if(task_list[index].Delay > 0)
{
task_list[index].Delay--;
} // if
else
{ //now we decrement the actual period counter
task_list[index].Counter--;
if(task_list[index].Counter == 0)
{
//Set the flag and reset the counter;
task_list[index].Status |= TASK_READY;
} // if
} // else
index++;
} // while
} // scheduler_isr()
/*-----------------------------------------------------------------------------
Purpose : Run all tasks for which the ready flag is set. Should be called
from within the main() function, not from an interrupt routine!
Variables: task_list[] structure
Returns : -
---------------------------------------------------------------------------*/
void dispatch_tasks(void)
{
uint8_t index = 0;
//go through the active tasks
while ((index < MAX_TASKS) && task_list[index].pFunction)
{
if((task_list[index].Status & (TASK_READY | TASK_ENABLED)) == (TASK_READY | TASK_ENABLED))
{
task_list[index].pFunction(); // run the task
task_list[index].Status &= ~TASK_READY; // reset the task when finished
task_list[index].Counter = task_list[index].Period; // reset counter
} // if
index++;
} // while
//go to sleep till next tick!
} // dispatch_tasks()
/*-----------------------------------------------------------------------------
Purpose : Add a function to the task-list struct. Should be called upon
initialization.
Variables: task_ptr: pointer to function
delay : initial delay in msec.
period : period between two calls in msec.
Returns : [NO_ERR, ERR_MAX_TASKS]
---------------------------------------------------------------------------*/
uint8_t add_task(void (*task_ptr)(), char *Name, uint16_t delay, uint16_t period)
{
uint8_t index = 0;
uint16_t temp1 = (uint16_t)(delay * TICKS_PER_SEC / 1000);
uint16_t temp2 = (uint16_t)(period * TICKS_PER_SEC / 1000);
if (max_tasks >= MAX_TASKS) return ERR_MAX_TASKS;
//go through the active tasks
while ((index < MAX_TASKS) && task_list[index].Period) index++;
if (index >= MAX_TASKS) return ERR_MAX_TASKS;
//if(task_list[index].Period != 0)
//{
// while(task_list[++index].Period != 0) ;
//} // if
if (task_list[index].Period == 0)
{
task_list[index].pFunction = task_ptr; // Pointer to Function
task_list[index].Period = temp2; // Period in msec.
task_list[index].Counter = temp2; // Countdown timer
task_list[index].Delay = temp1; // Initial delay before start
task_list[index].Status |= TASK_ENABLED; // Enable task by default
task_list[index].Status &= ~TASK_READY; // Task not ready to run
strncpy(task_list[index].Name, Name, NAME_LEN); // Name of Task
max_tasks++; // increase number of tasks
} // if
return NO_ERR;
} // add_task()
/*-----------------------------------------------------------------------------
Purpose : Enable a task.
Variables: Name: Name of task to enable
Returns : error [NO_ERR, ERR_NAME, ERR_EMPTY]
---------------------------------------------------------------------------*/
uint8_t enable_task(char *Name)
{
uint8_t index = 0;
bool found = false;
//go through the active tasks
if(task_list[index].Period != 0)
{
while ((task_list[index].Period != 0) && !found)
{
if (!strcmp(task_list[index].Name,Name))
{
task_list[index].Status |= TASK_ENABLED;
found = true;
} // if
index++;
} // while
} // if
else return ERR_EMPTY;
if (!found)
return ERR_NAME;
else return NO_ERR;
} // enable_task()
/*-----------------------------------------------------------------------------
Purpose : Disable a task.
Variables: Name: Name of task to disable
Returns : error [NO_ERR, ERR_NAME, ERR_EMPTY]
---------------------------------------------------------------------------*/
uint8_t disable_task(char *Name)
{
uint8_t index = 0;
bool found = false;
//go through the active tasks
if(task_list[index].Period != 0)
{
while ((task_list[index].Period != 0) && !found)
{
if (!strcmp(task_list[index].Name,Name))
{
task_list[index].Status &= ~TASK_ENABLED;
found = true;
} // if
index++;
} // while
} // if
else return ERR_EMPTY;
if (!found)
return ERR_NAME;
else return NO_ERR;
} // disable_task()
/*-----------------------------------------------------------------------------
Purpose : Set the time-period (msec.) of a task.
Variables: msec: the time in milliseconds
name: the name of the task to set the time for
Returns : error [NO_ERR, ERR_NAME, ERR_EMPTY]
---------------------------------------------------------------------------*/
uint8_t set_task_time_period(uint16_t Period, char *Name)
{
uint8_t index = 0;
bool found = false;
//go through the active tasks
if(task_list[index].Period != 0)
{
while ((task_list[index].Period != 0) && !found)
{
if (!strcmp(task_list[index].Name,Name))
{
task_list[index].Period = (uint16_t)(Period * TICKS_PER_SEC / 1000);
found = true;
} // if
index++;
} // while
} // if
else return ERR_EMPTY;
if (!found)
return ERR_NAME;
else return NO_ERR;
} // set_task_time_period()