-
Notifications
You must be signed in to change notification settings - Fork 4
/
leOS.cpp
451 lines (401 loc) · 14.3 KB
/
leOS.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* This file is part of leOS library.
Please check the README file and the notes
inside the leOS.h file
*/
//include required libraries
#include "leOS.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 _counterMs = 0; //use a 64-bit counter so it will overflow after 584,942,417 years!
#else
volatile unsigned long _counterMs = 0; //use a 32bit counter, so max intervals cannot exceed 49.7 days
#endif
//set your max interval here (max 2^32-1) - default 3600000 (1 hour)
#define MAX_TASK_INTERVAL 3600000UL
//global variables
#if defined(ATMEGAxU)
volatile unsigned int _starter = 0;
#else
volatile uint8_t _starter = 0;
#endif
uint8_t _initialized;
//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
//class constructor
leOS::leOS(void) {
_initialized = 0;
}
//class initialization
void leOS::begin(void) {
setTimer();
_initialized = 1;
_numTasks = 0;
}
//halt the scheduler
void leOS::haltScheduler(void) {
SREG &= ~(1<<SREG_I);
#if defined (ATMEGAx8) || defined (ATMEGAx4) || defined (ATMEGAx0)
TIMSK2 &= ~(1<<TOIE2);
#elif defined (ATMEGA8)
TIMSK &= ~(1<<TOIE2);
#elif defined (ATTINYx5) || defined (ATTINYx313)
TIMSK &= ~(1<<TOIE0);
#elif defined (ATTINYx4)
TIMSK0 &= ~(1<<TOIE0);
#elif defined (ATMEGAxU)
TIMSK3 &= ~(1<<TOIE3);
#endif
SREG |= (1<<SREG_I);
}
//restart the scheduler
void leOS::restartScheduler(void) {
if (_initialized) {
SREG &= ~(1<<SREG_I);
#if defined (ATMEGAx8) || defined (ATMEGAx4) || defined (ATMEGAx0)
TIMSK2 |= (1<<TOIE2);
#elif defined (ATMEGA8)
TIMSK |= (1<<TOIE2);
#elif defined (ATTINYx5) || defined (ATTINYx313)
TIMSK |= (1<<TOIE0);
#elif defined (ATTINYx4)
TIMSK0 |= (1<<TOIE0);
#elif defined (ATMEGAxU)
TIMSK3 |= (1<<TOIE3);
#endif
SREG |= (1<<SREG_I);
}
}
//add a task to the scheduler
uint8_t leOS::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 = 50; //50 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 = _counterMs + ((taskStatus & 0b00000100)? 0 : taskInterval);
_numTasks++;
SREG |= (1<<SREG_I); //restart the scheduler
return 0;
}
//pause a specific task
uint8_t leOS::pauseTask(void (*userTask)(void)) {
return (setTask(userTask, 0));
}
//restart a specific task
uint8_t leOS::restartTask(void (*userTask)(void)) {
return (setTask(userTask, 1));
}
//modify an existing task
uint8_t leOS::modifyTask(void (*userTask)(void), unsigned long taskInterval, uint8_t oneTimeTask) {
if ((oneTimeTask < SCHEDULED) && (oneTimeTask > ONETIME)) {
oneTimeTask = NULL;
}
//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) { //task found
tasks[tempI].userTasksInterval = taskInterval;
if (oneTimeTask != NULL) {
tasks[tempI].taskIsActive = oneTimeTask;
}
tasks[tempI].plannedTask = _counterMs + taskInterval;
_done = 0;
break;
}
tempI++;
} while (tempI < _numTasks);
SREG |= (1<<SREG_I); //restart the scheduler
return _done;
}
//manage the tasks' status
uint8_t leOS::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 == NULL) {
tasks[tempI].plannedTask = _counterMs + tasks[tempI].userTasksInterval;
} else {
tasks[tempI].plannedTask = _counterMs + taskInterval;
}
}
break;
} else {
tempI++;
}
} while (tempI < _numTasks);
SREG |= (1<<SREG_I); //restart the scheduler
return 0;
}
//remove a task from the scheduler
uint8_t leOS::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 leOS::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
}
/*
**************************************************************
* WARNING - The following code contains the core of leOS: *
* 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
#if defined (ATMEGAx8) || defined (ATMEGA8) || defined (ATMEGAx4) || defined (ATMEGAx0)
ISR(TIMER2_OVF_vect) {
TCNT2 = _starter;
#elif defined (ATTINYx313)
ISR(TIMER0_OVF_vect) {
TCNT0 = _starter;
#elif defined (ATTINYx4) || defined (ATTINYx5)
ISR (TIM0_OVF_vect) {
TCNT0 = _starter;
#elif defined (ATMEGAxU)
ISR (TIMER3_OVF_vect) {
TCNT3 = _starter;
#endif
_counterMs++; //increment the ms counter
//this is the scheduler - it checks if a task has to be executed
uint8_t tempI = 0;
void (*savedJobPointer)(void);
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 (_counterMs > tasks[tempI].plannedTask) {
#else
if ((long)(_counterMs - tasks[tempI].plannedTask) >=0) { //this trick overrun the overflow of _counterMs
#endif
//if it's a one-time task, than it has to be removed after running
if (tasks[tempI].taskIsActive == ONETIME) {
savedJobPointer = tasks[tempI].taskPointer; //store its pointer
savedJobPointer(); //call the task
//re-determine the task's position in the 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 = _counterMs + tasks[tempI].userTasksInterval;
tasks[tempI].taskPointer(); //call the task
}
}
}
tempI++;
}
}
//
//private methods
//
void leOS::setTimer() {
float prescaler = 0.0;
//halt all the interrupts
SREG &= ~(1<<SREG_I);
#if defined (ATMEGAx8) || defined (ATMEGAx4) || defined (ATMEGAx0)
//during setup, disable all the interrupts based on timer
TIMSK2 &= ~((1<<TOIE2) | (1<<OCIE2A) | (1<<OCIE2B));
//prescaler source clock set to internal Atmega clock (asynch mode)
ASSR &= ~(1<<AS2);
//this sets the timer to increment the counter until overflow
TCCR2A &= ~((1<<WGM21) | (1<<WGM20));
TCCR2B &= ~(1<<WGM22);
//the following code sets the prescaler depending on the system clock
if (F_CPU == 16000000UL) { // prescaler set to 64
TCCR2B |= (1<<CS22);
TCCR2B &= ~((1<<CS21) | (1<<CS20));
prescaler = 64.0;
} else if ((F_CPU == 8000000UL) || (F_CPU == 4000000UL)) { // prescaler set to 32
TCCR2B &= ~(1<<CS22);
TCCR2B |= ((1<<CS21) | (1<<CS20));
prescaler = 32.0;
} else if (F_CPU == 1000000UL) { // prescaler set to 8
TCCR2B &= ~((1<<CS22) | (1<<CS20));
TCCR2B |= (1<<CS21);
prescaler = 8.0;
}
#elif defined (ATTINYx5) || defined (ATTINYx313)
//during setup, disable all the interrupts based on timer 0
TIMSK &= ~((1<<TOIE0) | (1<<OCIE0A) | (1<<OCIE0B));
//normal mode: counter not connected to external pins
TCCR0A &= ~((1<<COM0A0) | (1<<COM0A1));
//this sets the timer to increment the counter until overflow
TCCR0A &= ~((1<<WGM01) | (1<<WGM00));
TCCR0B &= ~(1<<WGM02);
//the following code sets the prescaler depending on the system clock
if ((F_CPU == 16000000UL) || (F_CPU == 8000000UL)) { // prescaler set to 64
TCCR0B &= ~(1<<CS02);
TCCR0B |= ((1<<CS01) | (1<<CS00));
prescaler = 64.0;
} else if (F_CPU == 1000000UL) { // prescaler set to 8
TCCR0B &= ~((1<<CS02) | (1<<CS00));
TCCR0B |= (1<<CS01);
prescaler = 8.0;
}
#elif defined (ATTINYx4)
//on Attinyx4 we must use the timer 0 because timer1 is a 16 bit counter
//during setup, disable all the interrupts based on timer 0
TIMSK0 &= ~((1<<TOIE0) | (1<<OCIE0A) | (1<<OCIE0B));
//normal mode: increment counter until overflow & disconnect timer from pins
TCCR0B &= ~(1<<WGM02);
TCCR0A &= ~((1<<WGM01) | (1<<WGM00) | (1<<COM0A0) | (1<<COM0A1));
//the following code sets the prescaler depending on the system clock
if ((F_CPU == 16000000UL) || (F_CPU == 8000000UL)) { // prescaler set to 64
TCCR0B &= ~(1<<CS02);
TCCR0B |= ((1<<CS01) | (1<<CS00));
prescaler = 64.0;
} else if (F_CPU == 1000000UL) { // prescaler set to 8
TCCR0B &= ~((1<<CS02) | (1<<CS00));
TCCR0B |= (1<<CS01);
prescaler = 8.0;
}
#elif defined (ATMEGA8)
//during setup, disable all the interrupts based on timer2
TIMSK &= ~((1<<TOIE2) | (1<<OCIE2));
//normal mode: counter incremented until overflow
TCCR2 &= ~((1<<WGM21) | (1<<WGM20));
//prescaler source clock set to internal Atmega clock (synch mode)
ASSR &= ~(1<<AS2);
if (F_CPU == 1600000UL) { // prescaler set to 64
TCCR2 |= (1<<CS22);
TCCR2 &= ~((1<<CS21) | (1<<CS20));
prescaler = 64.0;
} else if ((F_CPU == 8000000UL) || (F_CPU == 4000000UL)) { // prescaler set to 32
TCCR2 &= ~(1<<CS22);
TCCR2 |= ((1<<CS21) | (1<<CS20));
prescaler = 32.0;
} else if (F_CPU == 1000000L) { // prescaler set to 8
TCCR2 |= (1<<CS21);
TCCR2 &= ~((1<<CS22) | (1<<CS20));
prescaler = 8.0;
}
#elif defined (ATMEGAxU)
//during setup, disable all the interrupts based on timer3
TIMSK3 &= ~((1<<TOIE3) | (1<<OCIE3A) | (1<<OCIE3B) | (1<<OCIE3C) | (1<<ICIE3));
//normal mode: counter incremented until overflow, prescaler set to /1
TCCR3A &= ~((1<<WGM31) | (1<<WGM30));
TCCR3B &= ~((1<<WGM33) | (1<<WGM32) | (1<<CS32) | (1<<CS31));
TCCR3B |= (1<<CS30);
//TCCR3B = 1;
prescaler = 1.0;
#endif
//set the initial value of the counter depending on the prescaler
#if defined (ATMEGAxU)
_starter = 65536 - (uint16_t)((float)F_CPU * 0.001 / prescaler); //for 16 MHz: 49536
#else
_starter = 256 - (int)((float)F_CPU * 0.001 / prescaler); //for 16 MHz: 6
#endif
//start the counter
#if defined (ATMEGAx8) || defined (ATMEGAx4) || defined (ATMEGAx0)
TCNT2 = _starter;
TIMSK2 |= (1<<TOIE2);
#elif defined (ATMEGA8)
TCNT2 = _starter;
TIMSK |= (1<<TOIE2);
#elif defined (ATTINYx5) || defined (ATTINYx313)
TCNT0 = _starter;
TIMSK |= (1<<TOIE0);
#elif defined (ATTINYx4)
TCNT0 = _starter;
TIMSK0 |= (1<<TOIE0);
#elif defined (ATMEGAxU)
TCNT3 = _starter;
TIMSK3 |= (1<<TOIE3);
#endif
SREG |= (1<<SREG_I);
}