-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRTevents.cpp
More file actions
316 lines (253 loc) · 8.63 KB
/
RTevents.cpp
File metadata and controls
316 lines (253 loc) · 8.63 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
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
/*
* RTevents.cpp
*
* Created on: Apr 29, 2016
* Author: hodai
*/
#include "RTevents.h"
#define RT_TIMER_AUTOSTOP
#include "RTtimer.h"
/* constants */
#define RT_MAX_MILLIS 0xFFFFFFFF // ~50 days
#define RT_MAX_MICROS 0xFFFFFFFF // ~70 minutes
/* flags */
#define RT_FLAG_ACTIVE 1 // without this flag the task is not scheduled
#define RT_FLAG_USED 2
#define RT_FLAG_ONE_TIME 4 // if set, this task will occur once and removed
#define RT_FLAG_OVERFLOW 8 // the next occurrence is after the counter is overflow ( > 2^32)
#define RT_FLAG_NESTED 32 // this task can be called nested - TODO - in development, don't use!
/* macros */
#define RT_FLAG_ADD(var, flag) var = (var | (flag))
#define RT_FLAG_DEL(var, flag) var = (var & ~flag)
#define RT_CHECK_FLAG(var, flag) (var & flag)
#define RT_CALC_NEXT_TIME(from, delay) ((unsigned long)((unsigned long)from + (unsigned long)delay)) // TODO - add check for overflow
#ifdef RT_UNITS_US
# define RT_GET_TIME() micros()
#else
# define RT_GET_TIME() millis()
#endif
#define RT_LOCK_TABLE() cli();
#define RT_RELEASE_TABLE() sei();
#ifndef MIN
#define MIN(a, b) ((a<b) ? a : b)
#endif
/* static variables */
RTtask_t RTevents::_tasksQueue[RT_QUEUE_SIZE];
volatile unsigned long RTevents::_curentNextIntrerupt;
#ifdef RT_LITE_MEM_MODE
unsigned long RTevents::_lastUpdate;
#endif
volatile bool RTevents::_interuptIsActivate;
void RTevents::begin(){
#ifdef RT_UNITS_US
_curentNextIntrerupt = RT_MAX_MICROS;
#else
_curentNextIntrerupt = RT_MAX_MILLIS;
#endif
_interuptIsActivate = false;
#ifdef RT_LITE_MEM_MODE
_lastUpdate = RT_GET_TIME();
#endif
memset(&_tasksQueue, 0x0, sizeof(_tasksQueue));
// define the timer and attach the interrupt handler
RTtimer_begin();
RTtimer_attachInterrupt(RTevents::RTinteruptHandler);
}
#ifdef RT_LITE_MEM_MODE
uint8_t RTevents::addTask(RTfunc_t func, uint16_t delay, uint16_t period){
#else
uint8_t RTevents::addTask(RTfunc_t func, uint32_t delay, uint32_t period){
#endif
// basic validation
if (delay > RT_MAX_DELAY || period > RT_MAX_PERIOD){
return 0;
}
// search for free task cell
for (int i=0 ; i<RT_QUEUE_SIZE ; i++){
if (!RT_CHECK_FLAG(_tasksQueue[i].flags, RT_FLAG_USED)){
// found! use it
RT_FLAG_ADD(_tasksQueue[i].flags, RT_FLAG_USED);
_tasksQueue[i].func = func;
if (period > 0){
// is continues task
_tasksQueue[i].period = period;
if(0 == delay){
// by default first occurrence will be after 1 period
delay = period;
}
} else {
RT_FLAG_ADD(_tasksQueue[i].flags, RT_FLAG_ONE_TIME);
}
#ifdef RT_LITE_MEM_MODE
_tasksQueue[i].leftForNextOccur = delay + (RT_GET_TIME()-_lastUpdate);
#else
_tasksQueue[i].nextOccurrence = RT_CALC_NEXT_TIME(RT_GET_TIME(), delay);
#endif
RT_FLAG_ADD(_tasksQueue[i].flags, RT_FLAG_ACTIVE);
// now this task can be scheduled
#ifdef RT_LITE_MEM_MODE
if (0 == _curentNextIntrerupt || RT_CALC_NEXT_TIME(RT_GET_TIME(), delay) < _curentNextIntrerupt){
#else
if (0 == _curentNextIntrerupt || _tasksQueue[i].nextOccurrence < _curentNextIntrerupt){
#endif
RTattachInterrupt(delay);
}
return i;
}
}
// no free cells!
return 0;
}
bool RTevents::removeTask(uint8_t taskID){
if (taskID < 0 || taskID >= RT_QUEUE_SIZE) {
return false;
}
memset(&_tasksQueue[taskID], 0x0, sizeof(RTtask_t));
return true;
}
#ifdef RT_LITE_MEM_MODE
void RTevents::RTattachInterrupt(uint16_t delay){
#else
void RTevents::RTattachInterrupt(unsigned long delay){
#endif
_interuptIsActivate = true;
_curentNextIntrerupt = RT_GET_TIME() + delay;
#ifdef RT_UNITS_US
if (delay < RT_MIN_TIMER) delay = RT_MIN_TIMER;
RTtimer_schedNext_us(delay);
#else
RTtimer_schedNext_us(delay*1000);
#endif
}
void RTevents::RTdetachInterrupt(){
_interuptIsActivate = false;
#ifdef RT_UNITS_US
_curentNextIntrerupt = RT_MAX_MICROS;
#else
_curentNextIntrerupt = RT_MAX_MILLIS;
#endif
RTtimer_stop();
}
void RTevents::RTinteruptHandler(){
#ifdef RT_UNITS_US
unsigned long nextInterrupt = RT_MAX_MICROS;
#else
unsigned long nextInterrupt = RT_MAX_MILLIS;
#endif
unsigned long localMicros;
// TODO - make 'i' static to solve starvation problems
// TODO - wrap all this with 'while' loop instead using recursion to solve stack overflow
static volatile bool lock = false;
if (lock){
return;
} else {
lock = true;
}
// first stop the next interrupts
//interrupts(); // release all the rest interrupts
//RTdetachInterrupt(); // already done by RTtimer
//while(true) {
localMicros = RT_GET_TIME();
#ifdef RT_LITE_MEM_MODE
unsigned long deltaTime= localMicros - _lastUpdate;
// TODO - if negative millis overflow occur
#endif
for (int i=0 ; i<RT_QUEUE_SIZE ; i++){
if (RT_CHECK_FLAG(_tasksQueue[i].flags, RT_FLAG_ACTIVE)) { /* is an active task */
#ifdef RT_LITE_MEM_MODE
if(_tasksQueue[i].leftForNextOccur <= deltaTime) { /* need to be executed now */
#else
if(_tasksQueue[i].nextOccurrence <= localMicros) { /* need to be executed now */
#endif
RTexecuteTask(&_tasksQueue[i]);
}
if (RT_CHECK_FLAG(_tasksQueue[i].flags, RT_FLAG_ACTIVE)) { /* is still activate */
nextInterrupt = MIN(nextInterrupt, _tasksQueue[i].nextOccurrence);
}
}
}
#if 0
// schedule the next interrupt
#ifdef RT_LITE_MEM_MODE
RT_LOCK_TABLE(); // lock the tasks table
#else
localMicros = RT_GET_TIME();
nextInterrupt = RT_MAX_MICROS;
#endif
for (int i=0 ; i<RT_QUEUE_SIZE ; i++){
if (RT_CHECK_FLAG(_tasksQueue[i].flags, RT_FLAG_ACTIVE)){ /* is activate */
#ifdef RT_LITE_MEM_MODE
if (_tasksQueue[i].leftForNextOccur < deltaTime){
_tasksQueue[i].leftForNextOccur = 0;
} else {
_tasksQueue[i].leftForNextOccur -= deltaTime;
}
nextInterrupt = MIN(nextInterrupt, _tasksQueue[i].leftForNextOccur);
#else
nextInterrupt = MIN(nextInterrupt, _tasksQueue[i].nextOccurrence);
#endif
}
}
#ifdef RT_LITE_MEM_MODE
_lastUpdate+=deltaTime; // the time we start that function
RT_RELEASE_TABLE(); // release the table
deltaTime = RT_GET_TIME() - _lastUpdate; // how much time we lost during that function
#endif
#endif
#ifdef RT_LITE_MEM_MODE
if (nextInterrupt < deltaTime){
// still have tasks to execute
RTattachInterrupt(0);
} else {
if (nextInterrupt != RT_MAX_MILLIS){
RTattachInterrupt(nextInterrupt - deltaTime);
}
}
#else
if (nextInterrupt != RT_MAX_MICROS){
//nextInterrupt += localMicros;
//localMicros = RT_GET_TIME();
//nextInterrupt -=localMicros;
if (nextInterrupt < localMicros){
RTattachInterrupt(0);
} else {
RTattachInterrupt(nextInterrupt - localMicros);
}
}
#endif
lock = false;
}
void RTevents::RTexecuteTask(RTtask_t* theTask){
RTfunc_t func = theTask->func;
// halt next execution of this task
RT_FLAG_DEL(theTask->flags, RT_FLAG_ACTIVE);
#if 0
if (RT_CHECK_FLAG(theTask->flags, RT_FLAG_NESTED)) {
sei(); // TODO - allow nested interupt
}
#endif
// execute the task
func();
#if 1
// check if this task was deleted
if (func != theTask->func ||
RT_CHECK_FLAG(theTask->flags, RT_FLAG_ACTIVE)){
return;
}
#endif
if (RT_CHECK_FLAG(theTask->flags, RT_FLAG_ONE_TIME)){
// free this task (not needed any more
memset(theTask, 0x0, sizeof(RTtask_t));
} else {
// re-schedule this task for the next occurrence
#ifdef RT_LITE_MEM_MODE
RT_LOCK_TABLE();
// calculate how much time after the _lastUpdate will be the next occurrence
theTask->leftForNextOccur = theTask->leftForNextOccur + theTask->period;
RT_RELEASE_TABLE();
#else
theTask->nextOccurrence = RT_CALC_NEXT_TIME(theTask->nextOccurrence, theTask->period);
#endif
RT_FLAG_ADD(theTask->flags, RT_FLAG_ACTIVE);
}
}