-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtestTimerRollover.ino
More file actions
470 lines (407 loc) · 18.2 KB
/
testTimerRollover.ino
File metadata and controls
470 lines (407 loc) · 18.2 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
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
* Author: Willem Aandewiel
* Thanks to Erik and Edgar Bonet
*
* The sole purpose of this program is to demonstrate that de timers
* declared with 'safeTimers.h' are rollover proof.
* It services no other use.
*
* The 'safeTimers.h' file gives you a toolset to define and use timers
* in any program on a standard and easy way.
*
* The file "safeTimersFastRO.h" uses uint32_t ('unsigned long') micros()
* in stead of millis() and so reduce the rollover time to only 1 hour
* 11 minutes and some seconds (the rollover time of the millis() function
* is about 49 day's).
*
* There is also an equivalent 16 bit timer and set of macro's.
* The timer16Bit() counts milli-seconds and has a rollover time of only
* 1 minute and 6 seconds!
*
* The timerTest1, 2, 3 and 4 make use of the timer16Bit()
*
* If you want to use the safeTimers just copy the
* tab "safeTimers.h" (or the file safeTimers.h) to your own program.
*/
//--- define USE_SAFETIMERS_H to include the real (production) safeTimers.h file
// #define USE_SAFETIMERS_H
//--- define SHOW_COUNTERS to print the testCounters at every wait
#define SHOW_COUNTERS
#define DO_SHORT_DELAY 1 // {0|1} => startWaiting timer
#define DO_LONG_DELAY 1 // {0|1} => startHolding timer
//--- select tests to run 1,2,3,4,5,6,7,8,9,10
bool runTest[10] = { 1,1,1,1,1,0,0,0,0,0 }; // => 1=run, 0=skip
#define DUE_TEST1 3000 // set 16Bit timer 3000ms
#define DUE_TEST2 3000 // set 16Bit timer 3000ms
#define DUE_TEST3 3000 // set 16Bit timer 3000ms
#define DUE_TEST4 7500 // set 16Bit timer 7500ms
#define DUE_TEST5 12000 // set 16Bit timer 12000ms
#ifdef USE_SAFETIMERS_H
#include "safeTimers.h"
#define DECLARE_TIMER_16BIT DECLARE_TIMER
#define CHANGE_TIMER_16BIT CHANGE_TIMER
#define RESTART_TIMER_16BIT RESTART_TIMER
#define TIME_LEFT_16BIT TIME_LEFT
#define TIME_LEFT_SEC_16BIT TIME_LEFT_SEC
#define TIME_PAST_16BIT TIME_PAST
#define TIME_PAST_SEC_16BIT TIME_PAST_SEC
#define DUE_16BIT DUE
#define timer16Bit() millis()
#define timer32Bit() millis()
#else
#include "safeTimersFastRO.h" // uses 16Bit timer --> rollover in 1 minute and some seconds
// and micros() -> rollover in 1 hour and 10 minutes
#endif
//--- print text every INTERVAL timer16Bit() ms
DECLARE_TIMER_16BIT(timerTest1, DUE_TEST1, SKIP_MISSED_TICKS )
DECLARE_TIMER_16BIT(timerTest2, DUE_TEST2, SKIP_MISSED_TICKS_WITH_SYNC )
DECLARE_TIMER_16BIT(timerTest3, DUE_TEST3, CATCH_UP_MISSED_TICKS )
DECLARE_TIMER_16BIT(timerTest4, DUE_TEST4) // default is SKIP_MISSED_TICKS
DECLARE_TIMER_16BIT(timerTest5, DUE_TEST5, SKIP_MISSED_TICKS)
DECLARE_TIMER_SEC(startWaiting, 41) // every 41 seconds
DECLARE_TIMER_SEC(startHolding, 130)
DECLARE_TIMER(debugPrint, 5000);
uint32_t detect32BitRollover = timer32Bit();
uint32_t detect16BitRollover = timer16Bit();
uint32_t startTime = 0;
uint32_t test1Counter = 0;
uint32_t test2Counter = 0;
uint32_t test3Counter = 0;
uint32_t test4Counter = 0;
uint32_t test5Counter = 0;
bool bRandomDelays = false;
//================================================================================================
void printTimerTypes()
{
Serial.println(F("---TYPE 0------SKIP_MISSED_TICKS--------------------------------------------------"));
Serial.println(F(" t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12"));
Serial.println(F(" | | < processor > \\ \\"));
Serial.println(F(" | | < bussy > \\ \\"));
Serial.println(F(" d1<int>d2<int>d3<....................>d4<int>d5<int>d6<int>d7<int>d8 enz"));
Serial.println(F(" t4 t5 t6 missed"));
Serial.println(F(" d3 --> d4 == longer then interval"));
Serial.println(F(" d4 --> d5 (etc) == interval (shifted from time-ticks)"));
Serial.println();
Serial.println(F("---TYPE 1------SKIP_MISSED_TICKS_WITH_SYNC-------------------------------------------------------"));
Serial.println(F(" t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12"));
Serial.println(F(" | | < processor > | |"));
Serial.println(F(" | | < bussy > | |"));
Serial.println(F(" d1<int>d2<int>d3<........................>d4<int>d5<int>d6<int>d7 enz"));
Serial.println(F(" t4 t5 t6 missed"));
Serial.println(F(" d3 --> d4 == longer then interval"));
Serial.println(F(" d4 --> d5 (etc) == interval (always on time-ticks!)"));
Serial.println();
Serial.println(F("---TYPE 2------CATCH_UP_MISSED_TICKS----------------------------------------------"));
Serial.println(F(" t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12"));
Serial.println(F(" | | < processor > | |"));
Serial.println(F(" | | < bussy > | |"));
Serial.println(F(" d1<int>d2<int>d3<...................>d4.d5.d6.d7.d8<int>d9<int>d10 enz"));
Serial.println(F(" d3 --> d4 == longer then interval"));
Serial.println(F(" d4>d5>d6>d7<.>d8 < less then interval, then sync to time-ticks"));
Serial.println(F(" d8 --> d9 (etc) == interval "));
Serial.println();
} // printTimerTypes()
//================================================================================================
void printTestData(int testNr, uint32_t duration, uint32_t next_due)
{
static uint32_t line = 0;
// 1...5....10...5....20...5....30...5....40...5....50...5....60
char spaces[61] = " ";
char dots[61] = " . . . . . . . . . . . . . . . . . . . . ";
char spaceB[61] = "";
char spaceA[61] = "";
uint8_t a = 0; // after
uint8_t b = 0; // before
for (int n=0; n<sizeof(runTest); n++)
{
if ((testNr-1) == n) b = a;
if (runTest[n]) a++;
}
strncpy( spaceB, spaces, (b*10) );
if ((line++ % 2) == 0)
strncpy( spaceA, spaces, (((a-1)* 10)-(b*10)) );
else strncpy( spaceA, dots, (((a-1)* 10)-(b*10)) );
if (runTest[(testNr-1)])
{
Serial.printf("[%5d]%s [%02d:%02d:%03d] %s=> test%d runned! ([%4.1f]Sec.) -> %d_due[%5d]\r\n"
, timer16Bit()
, spaceB
, ((millis() / (60 * 1000)) % 60)
, ((millis() / 1000) % 60)
, (millis() % 1000)
, spaceA
, testNr
, (round(duration / 100.0) / 10.0)
, testNr
, next_due);
}
Serial.flush();
} // printTestData()
//================================================================================================
void printCounter(byte tNr, uint32_t counter, uint32_t interval)
{
if (runTest[(tNr-1)])
{
Serial.printf("[%5d] test%d counted[%d] times run in [%d] events"
, timer16Bit()
, tNr
, counter
, ((millis() - startTime) / interval));
if (counter == ((millis() - startTime) / interval))
Serial.println(" --> EQUAL");
else
{
int16_t error = (int16_t)(((millis() - startTime) / interval) - counter);
Serial.printf(" --> ? (%d off)\r\n", error);
}
}
} // printCounter()
//================================================================================================
void printTimersLeftTime()
{
if (runTest[0]) {
Serial.printf("[%5d] timerTest1: Time past/left [%5d/%5d]ms next due after [%5d]!\r\n"
, timer16Bit()
, TIME_PAST_16BIT(timerTest1)
, TIME_LEFT_16BIT(timerTest1)
, ((timer16Bit() + TIME_LEFT_16BIT(timerTest1)) / 10) * 10 );
}
if (runTest[1]) {
Serial.printf("[%5d] timerTest2: Time past/left [%5d/%5d]ms next due after [%5d]!\r\n"
, timer16Bit()
, TIME_PAST_16BIT(timerTest2)
, TIME_LEFT_16BIT(timerTest2)
, ((timer16Bit() + TIME_LEFT_16BIT(timerTest2)) / 10) * 10 );
}
if (runTest[2]) {
Serial.printf("[%5d] timerTest3: Time past/left [%5d/%5d]ms next due after [%5d]!\r\n"
, timer16Bit()
, TIME_PAST_16BIT(timerTest3)
, TIME_LEFT_16BIT(timerTest3)
, ((timer16Bit() + TIME_LEFT_16BIT(timerTest3)) / 10) * 10 );
}
if (runTest[3]) {
Serial.printf("[%5d] timerTest4: Time past/left [%5d/%5d]ms next due after [%5d]!\r\n"
, timer16Bit()
, TIME_PAST_16BIT(timerTest4)
, TIME_LEFT_16BIT(timerTest4)
, ((timer16Bit() + TIME_LEFT_16BIT(timerTest4)) / 10) * 10 );
}
if (runTest[4]) {
Serial.printf("[%5d] timerTest5: Time past/left [%5d/%5d]sec next due after [%5d]!\r\n"
, timer16Bit()
, TIME_PAST_SEC_16BIT(timerTest5)
, TIME_LEFT_SEC_16BIT(timerTest5)
, ((timer16Bit() + TIME_LEFT_16BIT(timerTest5)) / 10) * 10 );
}
Serial.println();
} // printTimersLeftTime()
//================================================================================================
void setup() {
Serial.begin(115200);
Serial.println("\r\n\n.. and then it begins ...\r\n\n");
#ifdef USE_SAFETIMERS_H
Serial.println(F("Stresstest for safeTimers.h macro's \r\n"));
#else
Serial.println(F("Stresstest for safeTimersFastRO.h macro's \r\n"));
#endif
//--- This is 8266 HWRNG used to seed the Random PRNG:
//--- See: https://config9.com/arduino/getting-a-truly-random-number-in-arduino/
randomSeed(RANDOM_REG32);
delay(random(500));
printTimerTypes();
int8_t n;
for (int n=0; n<sizeof(runTest); n++)
{
if (runTest[n])
{
switch(n) {
case 0: Serial.printf("Due_Test1[%5d]ms ", DUE_TEST1);
break;
case 1: Serial.printf("Due_Test2[%5d]ms ", DUE_TEST2);
break;
case 2: Serial.printf("Due_Test3[%5d]ms ", DUE_TEST3);
break;
case 3: Serial.printf("Due_Test4[%5d]ms ", DUE_TEST4);
break;
case 4: Serial.printf("Due_Test5[%5d]ms ", DUE_TEST5);
break;
}
}
}
Serial.println();
for (int n=0; n<sizeof(runTest); n++)
{
if (runTest[n])
{
switch(n) {
case 0: Serial.printf("testType1[%d] ", timerTest1_type);
break;
case 1: Serial.printf("testType2[%d] ", timerTest2_type);
break;
case 2: Serial.printf("testType3[%d] ", timerTest3_type);
break;
case 3: Serial.printf("testType4[%d] ", timerTest4_type);
break;
case 4: Serial.printf("testType5[%d] ", timerTest5_type);
break;
}
}
}
Serial.println("\r\n");
RESTART_TIMER(startWaiting);
RESTART_TIMER(startHolding);
RESTART_TIMER_16BIT(timerTest5);
//--- ok, this is only to sequence the timers ----------
timerTest1_due = timer16Bit() + DUE_TEST1;
timerTest2_due = timerTest1_due + 500;
timerTest3_due = timerTest1_due + 1000;
Serial.printf("[%5d] StartTime[%02d:%02d:%03d]\r\n\n" , millis()
, ((millis() / (60 * 1000)) % 60)
, ((millis() / 1000) % 60)
, (millis() % 1000));
startTime = millis();
} // setup()
//================================================================================================
void loop() {
//============ Start 16 bit timers test's ==================================
if (DUE(debugPrint) & 0 )
{
if (runTest[4]) {
Serial.printf("[%5d] debug Test5: Time past/left [%5d/%5d]ms next due after [%5d]!\r\n"
, timer16Bit()
, TIME_PAST_16BIT(timerTest5)
, TIME_LEFT_16BIT(timerTest5)
, ((timer16Bit() + TIME_LEFT_16BIT(timerTest5)) / 10) * 10 );
}
} // debugPrint
if ( DUE_16BIT(timerTest1) && runTest[0] )
{
static uint32_t lastDue = 0;
uint32_t duration = millis() - lastDue;
test1Counter++;
printTestData(1, duration, timerTest1_due);
lastDue = millis();
}
if ( DUE_16BIT(timerTest2) && runTest[1] )
{
static uint32_t lastDue = 0;
uint32_t duration = millis() - lastDue;
lastDue = millis();
test2Counter++;
printTestData(2, duration, timerTest2_due);
}
if ( DUE_16BIT(timerTest3) && runTest[2] )
{
static uint32_t lastDue = 0;
uint32_t duration = millis() - lastDue;
lastDue = millis();
test3Counter++;
printTestData(3, duration, timerTest3_due);
}
if ( DUE_16BIT(timerTest4) && runTest[3] )
{
static uint32_t lastDue = 0;
uint32_t duration = millis() - lastDue;
lastDue = millis();
test4Counter++;
printTestData(4, duration, timerTest4_due);
}
if ( DUE_16BIT(timerTest5) && runTest[4] )
{
static uint32_t lastDue = 0;
uint32_t duration = millis() - lastDue;
lastDue = millis();
test5Counter++;
printTestData(5, duration, timerTest5_due);
}
//============ End of 16 bit timers test's ===============================
//--- see what happens if the processor is occupied and can not handle timers (every 41 seconds)
if ( DUE(startWaiting) && DO_SHORT_DELAY)
{
Serial.println();
#ifdef SHOW_COUNTERS
{
printCounter(1, test1Counter, DUE_TEST1);
printCounter(2, test2Counter, DUE_TEST2);
printCounter(3, test3Counter, DUE_TEST3);
printCounter(4, test4Counter, DUE_TEST4);
printCounter(5, test5Counter, DUE_TEST5);
}
#endif
Serial.println();
printTimersLeftTime();
//--- be bussy for 4 seconds. This influances mainly Test1 & 2 & 3
Serial.printf("[%5d] wait ... ", timer16Bit());
for(byte cc=0; cc<40; cc++)
{
Serial.printf("w");
delay(100);
yield();
}
Serial.println(F(".. continue"));
if (bRandomDelays) delay(random(1000));
Serial.println();
Serial.printf("[%5d] startHolding: Time left [%7d]ms, [%5d]sec, [%2d]min.\r\n"
, timer16Bit()
, TIME_LEFT_MS(startHolding)
, TIME_LEFT_SEC(startHolding)
, TIME_LEFT_MIN(startHolding));
printTimersLeftTime();
Serial.println();
RESTART_TIMER(startWaiting);
}
//-- every x seconds do the stress test for all the timers --
if (DUE(startHolding) && DO_LONG_DELAY)
{
Serial.println();
printTimersLeftTime();
//--- test what happens if something keeps the system bussy for 15 seconds ---
Serial.printf("\r\n[%5d] hold ... ", timer16Bit());
bRandomDelays = !bRandomDelays;
byte cc = 15;
//--- be bussy for 15 seconds ---
while (cc > 0)
{
Serial.printf("%d ", cc--);
delay(1000);
yield();
}
Serial.println(F(".. continue"));
if (bRandomDelays)
Serial.println(F("************************* using Random Delays from here on ***********************\r\n"));
else Serial.println(F("*********************** using NO random delays! from here on *********************\r\n"));
Serial.printf("[%5d] startWaiting: Time left [%7d]ms, [%5d]sec., [%2d]min.\r\n\n"
, timer16Bit()
, TIME_LEFT_MS(startWaiting)
, TIME_LEFT_SEC(startWaiting)
, TIME_LEFT_MIN(startWaiting));
printTimersLeftTime();
Serial.println();
RESTART_TIMER(startWaiting);
RESTART_TIMER(startHolding);
detect16BitRollover = timer16Bit();
}
if (timer16Bit() < detect16BitRollover)
{
Serial.printf("\r\n[%5d] ***** timer16Bit() has rolled-over! *****\r\n\n", timer16Bit());
printTimersLeftTime();
}
detect16BitRollover = timer16Bit();
if (timer32Bit() < detect32BitRollover)
{
Serial.println(F("\r\n\n***************************************************************************"));
#ifdef USE_SAFETIMER_H
Serial.println(F( "*************************** millis() Rolled Over **************************"));
Serial.println(F( "************************ next Rollover in 49 day's ************************"));
#else
Serial.println(F( "*********************** timer32bit() Rolled Over ***************************"));
Serial.println(F( "****************** next Rolloverin 1 hour and 6 minutes *******************"));
#endif
Serial.println(F( "***************************************************************************\r\n\n"));
RESTART_TIMER(startWaiting);
RESTART_TIMER(startHolding);
}
detect32BitRollover = timer32Bit();
if (bRandomDelays) delay(random(250));
} // loop()