forked from khoih-prog/TimerInterrupt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathISR_Timer.cpp
More file actions
295 lines (225 loc) · 7.7 KB
/
Copy pathISR_Timer.cpp
File metadata and controls
295 lines (225 loc) · 7.7 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
/****************************************************************************************************************************
ISR_Timer.cpp
For Arduino boards (UNO, Nano, Mega, etc. )
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt
Licensed under MIT license
TCNTx - Timer/Counter Register. The actual timer value is stored here.
OCRx - Output Compare Register
ICRx - Input Capture Register (only for 16bit timer)
TIMSKx - Timer/Counter Interrupt Mask Register. To enable/disable timer interrupts.
TIFRx - Timer/Counter Interrupt Flag Register. Indicates a pending timer interrupt.
Now with we can use these new 16 ISR-based timers, while consuming only 1 hwarware Timer.
Their independently-selected, maximum interval is practically unlimited (limited only by unsigned long miliseconds)
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
Therefore, their executions are not blocked by bad-behaving functions / tasks.
This important feature is absolutely necessary for mission-critical tasks.
Version: 1.1.1
Version Modified By Date Comments
------- ----------- ---------- -----------
1.0.0 K Hoang 23/11/2019 Initial coding
1.0.1 K Hoang 25/11/2019 New release fixing compiler error
1.0.2 K.Hoang 28/11/2019 Permit up to 16 super-long-time, super-accurate ISR-based timers to avoid being blocked
1.0.3 K.Hoang 01/12/2020 Add complex examples ISR_16_Timers_Array_Complex and ISR_16_Timers_Array_Complex
1.1.1 K.Hoang 06/12/2020 Add example Change_Interval. Bump up version to sync with other TimerInterrupt Libraries
*****************************************************************************************************************************/
#include "ISR_Timer.h"
#include <string.h>
// Select time function:
//static inline unsigned long elapsed() { return micros(); }
static inline unsigned long elapsed() {
return millis();
}
ISR_Timer::ISR_Timer()
: numTimers (-1)
{
}
void ISR_Timer::init() {
unsigned long current_millis = millis(); //elapsed();
for (int i = 0; i < MAX_TIMERS; i++) {
memset((void*) &timer[i], 0, sizeof (timer_t));
timer[i].prev_millis = current_millis;
}
numTimers = 0;
}
void ISR_Timer::run() {
int i;
unsigned long current_millis;
// get current time
current_millis = millis(); //elapsed();
for (i = 0; i < MAX_TIMERS; i++) {
timer[i].toBeCalled = DEFCALL_DONTRUN;
// no callback == no timer, i.e. jump over empty slots
if (timer[i].callback != NULL) {
// is it time to process this timer ?
// see http://arduino.cc/forum/index.php/topic,124048.msg932592.html#msg932592
if ((current_millis - timer[i].prev_millis) >= timer[i].delay) {
unsigned long skipTimes = (current_millis - timer[i].prev_millis) / timer[i].delay;
// update time
timer[i].prev_millis += timer[i].delay * skipTimes;
// check if the timer callback has to be executed
if (timer[i].enabled) {
// "run forever" timers must always be executed
if (timer[i].maxNumRuns == RUN_FOREVER) {
timer[i].toBeCalled = DEFCALL_RUNONLY;
}
// other timers get executed the specified number of times
else if (timer[i].numRuns < timer[i].maxNumRuns) {
timer[i].toBeCalled = DEFCALL_RUNONLY;
timer[i].numRuns++;
// after the last run, delete the timer
if (timer[i].numRuns >= timer[i].maxNumRuns) {
timer[i].toBeCalled = DEFCALL_RUNANDDEL;
}
}
}
}
}
}
for (i = 0; i < MAX_TIMERS; i++) {
if (timer[i].toBeCalled == DEFCALL_DONTRUN)
continue;
if (timer[i].hasParam)
(*(timer_callback_p)timer[i].callback)(timer[i].param);
else
(*(timer_callback)timer[i].callback)();
if (timer[i].toBeCalled == DEFCALL_RUNANDDEL)
deleteTimer(i);
}
}
// find the first available slot
// return -1 if none found
int ISR_Timer::findFirstFreeSlot() {
// all slots are used
if (numTimers >= MAX_TIMERS) {
return -1;
}
// return the first slot with no callback (i.e. free)
for (int i = 0; i < MAX_TIMERS; i++) {
if (timer[i].callback == NULL) {
return i;
}
}
// no free slots found
return -1;
}
int ISR_Timer::setupTimer(unsigned long d, void* f, void* p, bool h, unsigned n) {
int freeTimer;
if (numTimers < 0) {
init();
}
freeTimer = findFirstFreeSlot();
if (freeTimer < 0) {
return -1;
}
if (f == NULL) {
return -1;
}
timer[freeTimer].delay = d;
timer[freeTimer].callback = f;
timer[freeTimer].param = p;
timer[freeTimer].hasParam = h;
timer[freeTimer].maxNumRuns = n;
timer[freeTimer].enabled = true;
timer[freeTimer].prev_millis = elapsed();
numTimers++;
return freeTimer;
}
int ISR_Timer::setTimer(unsigned long d, timer_callback f, unsigned n) {
return setupTimer(d, (void *)f, NULL, false, n);
}
int ISR_Timer::setTimer(unsigned long d, timer_callback_p f, void* p, unsigned n) {
return setupTimer(d, (void *)f, p, true, n);
}
int ISR_Timer::setInterval(unsigned long d, timer_callback f) {
return setupTimer(d, (void *)f, NULL, false, RUN_FOREVER);
}
int ISR_Timer::setInterval(unsigned long d, timer_callback_p f, void* p) {
return setupTimer(d, (void *)f, p, true, RUN_FOREVER);
}
int ISR_Timer::setTimeout(unsigned long d, timer_callback f) {
return setupTimer(d, (void *)f, NULL, false, RUN_ONCE);
}
int ISR_Timer::setTimeout(unsigned long d, timer_callback_p f, void* p) {
return setupTimer(d, (void *)f, p, true, RUN_ONCE);
}
bool ISR_Timer::changeInterval(unsigned numTimer, unsigned long d) {
if (numTimer >= MAX_TIMERS) {
return false;
}
// Updates interval of existing specified timer
if (timer[numTimer].callback != NULL) {
timer[numTimer].delay = d;
timer[numTimer].prev_millis = elapsed();
return true;
}
// false return for non-used numTimer, no callback
return false;
}
void ISR_Timer::deleteTimer(unsigned timerId) {
if (timerId >= MAX_TIMERS) {
return;
}
// nothing to delete if no timers are in use
if (numTimers == 0) {
return;
}
// don't decrease the number of timers if the
// specified slot is already empty
if (timer[timerId].callback != NULL) {
memset((void*) &timer[timerId], 0, sizeof (timer_t));
timer[timerId].prev_millis = elapsed();
// update number of timers
numTimers--;
}
}
// function contributed by [email protected]
void ISR_Timer::restartTimer(unsigned numTimer) {
if (numTimer >= MAX_TIMERS) {
return;
}
timer[numTimer].prev_millis = elapsed();
}
bool ISR_Timer::isEnabled(unsigned numTimer) {
if (numTimer >= MAX_TIMERS) {
return false;
}
return timer[numTimer].enabled;
}
void ISR_Timer::enable(unsigned numTimer) {
if (numTimer >= MAX_TIMERS) {
return;
}
timer[numTimer].enabled = true;
}
void ISR_Timer::disable(unsigned numTimer) {
if (numTimer >= MAX_TIMERS) {
return;
}
timer[numTimer].enabled = false;
}
void ISR_Timer::enableAll() {
// Enable all timers with a callback assigned (used)
for (int i = 0; i < MAX_TIMERS; i++) {
if (timer[i].callback != NULL && timer[i].numRuns == RUN_FOREVER) {
timer[i].enabled = true;
}
}
}
void ISR_Timer::disableAll() {
// Disable all timers with a callback assigned (used)
for (int i = 0; i < MAX_TIMERS; i++) {
if (timer[i].callback != NULL && timer[i].numRuns == RUN_FOREVER) {
timer[i].enabled = false;
}
}
}
void ISR_Timer::toggle(unsigned numTimer) {
if (numTimer >= MAX_TIMERS) {
return;
}
timer[numTimer].enabled = !timer[numTimer].enabled;
}
unsigned ISR_Timer::getNumTimers() {
return numTimers;
}