-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathleOS2.cpp
362 lines (315 loc) · 10.8 KB
/
leOS2.cpp
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* This file is part of leOS2 library.
Please check the README file and the notes
inside the leOS2.h file
*/
//include required libraries
#include "leOS2.h"
#include <avr/wdt.h>
#include <avr/interrupt.h>
//global settings - modify them to change the leOS characteristics
const uint8_t MAX_TASKS = 9; //max allowed tasks -1 (i.e.: 9 = 10-1)
#ifdef SIXTYFOUR_MATH
volatile unsigned long long _ticksCounter = 0; //use a 64-bit counter so it will overflow after 584,942,417 years!
#else
volatile unsigned long _ticksCounter = 0; //use a 32bit counter, so max intervals cannot exceed 49.7 days
#endif
//set your max interval here (max 2^32-1) - default 225000 ticks (1 hour)
#define MAX_TASK_INTERVAL 225000UL
//tasks variables
struct leOS_core {
void (*taskPointer)(void); //used to store the pointers to user's tasks
volatile unsigned long userTasksInterval; //used to store the interval between each task's run
//used to store the next time a task will have to be executed
#ifdef SIXTYFOUR_MATH
volatile unsigned long long plannedTask;
#else
volatile unsigned long plannedTask;
#endif
volatile uint8_t taskIsActive; //used to store the status of the tasks
};
leOS_core tasks[MAX_TASKS];
volatile uint8_t _numTasks; //the number of current running tasks
volatile uint8_t _initialized;
volatile uint16_t _wdtResetTimeout;
volatile uint8_t _taskIsRunning;
volatile uint16_t _maxTimeouts;
//class constructor
leOS2::leOS2(void) {
_initialized = 0;
}
//class initialization
void leOS2::begin(uint16_t resetTimeout) {
//store the # of timeouts before to reset the chip
_wdtResetTimeout = resetTimeout;
setWDT(); //initialize the WDT
_initialized = 1;
_numTasks = 0;
_taskIsRunning = 0;
}
//add a task to the scheduler
uint8_t leOS2::addTask(void (*userTask)(void), unsigned long taskInterval, uint8_t taskStatus) {
if ((_initialized == 0) || (_numTasks == MAX_TASKS)) { //max number of allowed tasks reached
return 1;
}
if ((taskInterval < 1) || (taskInterval > MAX_TASK_INTERVAL)) {
taskInterval = 1; //1 tick/16 ms by default
}
//check if taskStatus is valid
if (taskStatus > SCHEDULED_IMMEDIATESTART) {
taskStatus = SCHEDULED;
}
//add the task to the scheduler
SREG &= ~(1<<SREG_I); //halt the scheduler
tasks[_numTasks].taskPointer = *userTask;
tasks[_numTasks].taskIsActive = taskStatus & 0b00000011; //I get only the first 2 bits - I don't need the IMMEDIATESTART bit
tasks[_numTasks].userTasksInterval = taskInterval;
//no wait if the user wants the task up and running once added...
//...otherwise we wait for the interval before to run the task
tasks[_numTasks].plannedTask = _ticksCounter + ((taskStatus & 0b00000100)? 0 : taskInterval);
_numTasks++;
SREG |= (1<<SREG_I); //restart the scheduler
return 0;
}
//pause a specific task
uint8_t leOS2::pauseTask(void (*userTask)(void)) {
return (setTask(userTask, 0));
}
//restart a specific task
uint8_t leOS2::restartTask(void (*userTask)(void)) {
return (setTask(userTask, 1));
}
//modify an existing task
uint8_t leOS2::modifyTask(void (*userTask)(void), unsigned long taskInterval, uint8_t oneTimeTask) {
if ((oneTimeTask < SCHEDULED) && (oneTimeTask > ONETIME)) {
oneTimeTask = NONE;
}
if ((taskInterval < 1) || (taskInterval > MAX_TASK_INTERVAL)) {
taskInterval = 1; //1 tick/16 ms by default
}
//modify the task into the scheduler
SREG &= ~(1<<SREG_I); //halt the scheduler
uint8_t tempI = 0;
uint8_t _done = 1;
do {
if (tasks[tempI].taskPointer == *userTask) { //found the task
tasks[tempI].userTasksInterval = taskInterval;
if (oneTimeTask != NONE) {
tasks[tempI].taskIsActive = oneTimeTask;
}
tasks[tempI].plannedTask = _ticksCounter + taskInterval;
_done = 0;
break;
}
tempI++;
} while (tempI < _numTasks);
SREG |= (1<<SREG_I); //restart the scheduler
return _done;
}
//manage the tasks' status
uint8_t leOS2::setTask(void (*userTask)(void), uint8_t tempStatus, unsigned long taskInterval) {
if ((_initialized == 0) || (_numTasks == 0)) {
return 1;
}
SREG &= ~(1<<SREG_I); //halt the scheduler
uint8_t tempI = 0;
do {
if (tasks[tempI].taskPointer == *userTask) {
tasks[tempI].taskIsActive = tempStatus;
if (tempStatus == SCHEDULED) {
if (taskInterval == NONE) {
tasks[tempI].plannedTask = _ticksCounter + tasks[tempI].userTasksInterval;
} else {
tasks[tempI].plannedTask = _ticksCounter + taskInterval;
}
}
break;
} else {
tempI++;
}
} while (tempI < _numTasks);
SREG |= (1<<SREG_I); //restart the scheduler
return 0;
}
//remove a task from the scheduler
uint8_t leOS2::removeTask(void (*userTask)(void)) {
if ((_initialized == 0) || (_numTasks == 0)) {
return 1;
}
SREG &= ~(1<<SREG_I); //halt the scheduler
uint8_t tempI = 0;
do {
if (tasks[tempI].taskPointer == *userTask) {
if ((tempI + 1) == _numTasks) {
_numTasks--;
} else if (_numTasks > 1) {
for (uint8_t tempJ = tempI; tempJ < _numTasks; tempJ++) {
tasks[tempJ].taskPointer = tasks[tempJ + 1].taskPointer;
tasks[tempJ].taskIsActive = tasks[tempJ + 1].taskIsActive;
tasks[tempJ].userTasksInterval = tasks[tempJ + 1].userTasksInterval;
tasks[tempJ].plannedTask = tasks[tempJ + 1].plannedTask;
}
_numTasks -= 1;
} else {
_numTasks = 0;
}
break;
} else {
tempI++;
}
} while (tempI < _numTasks);
SREG |= (1<<SREG_I); //restart the scheduler
return 0;
}
//check if a task is running
uint8_t leOS2::getTaskStatus(void (*userTask)(void)) {
if ((_initialized == 0) || (_numTasks == 0)) {
return -1;
}
uint8_t tempJ = 255; //return 255 if the task was not found (almost impossible)
SREG &= ~(1<<SREG_I); //halt the scheduler
uint8_t tempI = 0;
//look for the task
do {
if (tasks[tempI].taskPointer == *userTask) {
//return its current status
tempJ = tasks[tempI].taskIsActive;
break;
}
tempI++;
} while (tempI < _numTasks);
SREG |= (1<<SREG_I); //restart the scheduler
return tempJ; //return the task status
}
//convert milliseconds in ticks
uint32_t leOS2::convertMs(uint32_t tempMs) {
if (tempMs < 16) {
return 1;
}
tempMs = tempMs >> 4;
if (tempMs > MAX_TASK_INTERVAL) {
return MAX_TASK_INTERVAL;
} else {
return tempMs;
}
}
//reset the MCU
void leOS2::reset(void) {
wdt_disable();
wdt_enable(WDTO_2S);
while(1){}; //wait for reset
}
/*
**************************************************************
* WARNING - The following code contains the core of leOS2: *
* do not modify it unless you exactly know what you're doing *
**************************************************************
*/
//ISR (Interrupt Service Routine) called by the timer's overflow:
//interrupt-driven routine to run the tasks - this ISR is not atomic
//so other ISRs can interrupt it
ISR(WDT_vect, ISR_NOBLOCK) {
_ticksCounter++; //increment the ticks counter
//check if the next timeout of the WDT an interrupt should be
//called or a reset should be executed
if (_wdtResetTimeout ) {
_WD_CONTROL_REG |= (1<<WDIE); //another interrupt, please...
//check if a task is already running
if (_taskIsRunning) {
//check if the maximum # of timeouts has been reached
_maxTimeouts--;
if (_maxTimeouts == 0) {
//max numb. of timeouts reached - next time we need a chip reset!
_WD_CONTROL_REG &= ~(1<<WDIE);
}
return;
}
}
//THIS IS THE SCHEDULER!
uint8_t tempI = 0; //set the pointer to the tasks to 0
void (*savedJobPointer)(void); //create a task pointer
while (tempI < _numTasks) {
if (tasks[tempI].taskIsActive > 0 ) { //the task is running
//check if it's time to execute the task
#ifdef SIXTYFOUR_MATH
if (_ticksCounter > tasks[tempI].plannedTask) {
#else
if ((long)(_ticksCounter - tasks[tempI].plannedTask) >=0) { //this trick overruns the overflow of _ticksCounter
#endif
//prepare the counters to monitor if a task will freeze
_maxTimeouts = _wdtResetTimeout;
_taskIsRunning = 1;
savedJobPointer = tasks[tempI].taskPointer; //store its pointer
savedJobPointer(); //call the task
//reset the counters
_taskIsRunning = 0;
if (tasks[tempI].taskIsActive == ONETIME) {
//re-determine the task's position in case it's changed
tempI = 0;
do {
if (tasks[tempI].taskPointer == savedJobPointer) { //found the task
break;
} else {
tempI++;
}
} while (tempI <= _numTasks);
//remove it from the scheduler
if (tempI == _numTasks) {
_numTasks--;
} else if (_numTasks > 1) {
for (uint8_t tempJ = tempI; tempJ < _numTasks; tempJ++) {
tasks[tempJ].taskPointer = tasks[tempJ + 1].taskPointer;
tasks[tempJ].taskIsActive = tasks[tempJ + 1].taskIsActive;
tasks[tempJ].userTasksInterval = tasks[tempJ + 1].userTasksInterval;
tasks[tempJ].plannedTask = tasks[tempJ + 1].plannedTask;
}
_numTasks -= 1;
} else {
_numTasks = 0;
}
} else {
//let's schedule next start
tasks[tempI].plannedTask = _ticksCounter + tasks[tempI].userTasksInterval;
}
}
}
tempI++;
}
}
//
//private methods
//
/*
************************************************************
WARNING!! DO NOT MODIFY THE FOLLOWING CODE IF YOU DON'T KNOW
WHAT YOU'RE DOING! YOU COULD PUT YOUR MICROCONTROLLER IN A
NEVERENDING RESET!!
************************************************************
*/
//set the WatchDog Timer
void leOS2::setWDT() {
MCUSR = 0; //ensure that the reset vectors are off
wdt_disable(); //disable WD
SREG &= ~(1<<SREG_I); //disable all the interrupts
//set the WD control register:
//prescaler to /2048 (16 ms)
//"interrupt & reset" mode enabled
byte _tempI = (1<<WDIE);
if (_wdtResetTimeout) {
_tempI |= (1<<WDE);
}
_WD_CONTROL_REG = ((1<<_WD_CHANGE_BIT) | (1<<WDE));
_WD_CONTROL_REG = _tempI;
SREG |= (1<<SREG_I); //re-enable interrupts
}
//halt the scheduler
void leOS2::haltScheduler() {
SREG &= ~(1<<SREG_I); //disable all the interrupts
wdt_disable(); //disable WD
SREG |= (1<<SREG_I); //re-enable interrupts
}
//restart the scheduler
void leOS2::restartScheduler() {
if (_initialized) {
setWDT();
}
}