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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
|
/*
FreeRTOS V8.0.1 - Copyright (C) 2014 Real Time Engineers Ltd.
All rights reserved
VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
***************************************************************************
* *
* FreeRTOS provides completely free yet professionally developed, *
* robust, strictly quality controlled, supported, and cross *
* platform software that has become a de facto standard. *
* *
* Help yourself get started quickly and support the FreeRTOS *
* project by purchasing a FreeRTOS tutorial book, reference *
* manual, or both from: http://www.FreeRTOS.org/Documentation *
* *
* Thank you! *
* *
***************************************************************************
This file is part of the FreeRTOS distribution.
FreeRTOS is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License (version 2) as published by the
Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
>>! NOTE: The modification to the GPL is included to allow you to !<<
>>! distribute a combined work that includes FreeRTOS without being !<<
>>! obliged to provide the source code for proprietary components !<<
>>! outside of the FreeRTOS kernel. !<<
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. Full license text is available from the following
link: http://www.freertos.org/a00114.html
1 tab == 4 spaces!
***************************************************************************
* *
* Having a problem? Start by reading the FAQ "My application does *
* not run, what could be wrong?" *
* *
* http://www.FreeRTOS.org/FAQHelp.html *
* *
***************************************************************************
http://www.FreeRTOS.org - Documentation, books, training, latest versions,
license and Real Time Engineers Ltd. contact details.
http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
including FreeRTOS+Trace - an indispensable productivity tool, a DOS
compatible FAT file system, and our tiny thread aware UDP/IP stack.
http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
licenses offer ticketed support, indemnification and middleware.
http://www.SafeRTOS.com - High Integrity Systems also provide a safety
engineered and independently SIL3 certified version for use in safety and
mission critical applications that require provable dependability.
1 tab == 4 spaces!
*/
/*
* Tests the behaviour of timers. Some timers are created before the scheduler
* is started, and some after.
*/
/* Standard includes. */
#include <string.h>
/* Scheduler include files. */
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
/* Demo program include files. */
#include "TimerDemo.h"
#if ( configTIMER_TASK_PRIORITY < 1 )
#error configTIMER_TASK_PRIORITY must be set to at least 1 for this test/demo to function correctly.
#endif
#define tmrdemoDONT_BLOCK ( ( TickType_t ) 0 )
#define tmrdemoONE_SHOT_TIMER_PERIOD ( xBasePeriod * ( TickType_t ) 3 )
#define trmdemoNUM_TIMER_RESETS ( ( unsigned char ) 10 )
/*-----------------------------------------------------------*/
/* The callback functions used by the timers. These each increment a counter
to indicate which timer has expired. The auto-reload timers that are used by
the test task (as opposed to being used from an ISR) all share the same
prvAutoReloadTimerCallback() callback function, and use the ID of the
pxExpiredTimer parameter passed into that function to know which counter to
increment. The other timers all have their own unique callback function and
simply increment their counters without using the callback function parameter. */
static void prvAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer );
static void prvOneShotTimerCallback( TimerHandle_t pxExpiredTimer );
static void prvTimerTestTask( void *pvParameters );
static void prvISRAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer );
static void prvISROneShotTimerCallback( TimerHandle_t pxExpiredTimer );
/* The test functions used by the timer test task. These manipulate the auto
reload and one shot timers in various ways, then delay, then inspect the timers
to ensure they have behaved as expected. */
static void prvTest1_CreateTimersWithoutSchedulerRunning( void );
static void prvTest2_CheckTaskAndTimersInitialState( void );
static void prvTest3_CheckAutoReloadExpireRates( void );
static void prvTest4_CheckAutoReloadTimersCanBeStopped( void );
static void prvTest5_CheckBasicOneShotTimerBehaviour( void );
static void prvTest6_CheckAutoReloadResetBehaviour( void );
static void prvResetStartConditionsForNextIteration( void );
/*-----------------------------------------------------------*/
/* Flag that will be latched to pdFAIL should any unexpected behaviour be
detected in any of the demo tests. */
static volatile portBASE_TYPE xTestStatus = pdPASS;
/* Counter that is incremented on each cycle of a test. This is used to
detect a stalled task - a test that is no longer running. */
static volatile unsigned long ulLoopCounter = 0;
/* A set of auto reload timers - each of which use the same callback function.
The callback function uses the timer ID to index into, and then increment, a
counter in the ucAutoReloadTimerCounters[] array. The auto reload timers
referenced from xAutoReloadTimers[] are used by the prvTimerTestTask task. */
static TimerHandle_t xAutoReloadTimers[ configTIMER_QUEUE_LENGTH + 1 ] = { 0 };
static unsigned char ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH + 1 ] = { 0 };
/* The one shot timer is configured to use a callback function that increments
ucOneShotTimerCounter each time it gets called. */
static TimerHandle_t xOneShotTimer = NULL;
static unsigned char ucOneShotTimerCounter = ( unsigned char ) 0;
/* The ISR reload timer is controlled from the tick hook to exercise the timer
API functions that can be used from an ISR. It is configured to increment
ucISRReloadTimerCounter each time its callback function is executed. */
static TimerHandle_t xISRAutoReloadTimer = NULL;
static unsigned char ucISRAutoReloadTimerCounter = ( unsigned char ) 0;
/* The ISR one shot timer is controlled from the tick hook to exercise the timer
API functions that can be used from an ISR. It is configured to increment
ucISRReloadTimerCounter each time its callback function is executed. */
static TimerHandle_t xISROneShotTimer = NULL;
static unsigned char ucISROneShotTimerCounter = ( unsigned char ) 0;
/* The period of all the timers are a multiple of the base period. The base
period is configured by the parameter to vStartTimerDemoTask(). */
static TickType_t xBasePeriod = 0;
/*-----------------------------------------------------------*/
void vStartTimerDemoTask( TickType_t xBasePeriodIn )
{
/* Start with the timer and counter arrays clear - this is only necessary
where the compiler does not clear them automatically on start up. */
memset( ucAutoReloadTimerCounters, 0x00, sizeof( ucAutoReloadTimerCounters ) );
memset( xAutoReloadTimers, 0x00, sizeof( xAutoReloadTimers ) );
/* Store the period from which all the timer periods will be generated from
(multiples of). */
xBasePeriod = xBasePeriodIn;
/* Create a set of timers for use by this demo/test. */
prvTest1_CreateTimersWithoutSchedulerRunning();
/* Create the task that will control and monitor the timers. This is
created at a lower priority than the timer service task to ensure, as
far as it is concerned, commands on timers are actioned immediately
(sending a command to the timer service task will unblock the timer service
task, which will then preempt this task). */
if( xTestStatus != pdFAIL )
{
xTaskCreate( prvTimerTestTask, "Tmr Tst", configMINIMAL_STACK_SIZE, NULL, configTIMER_TASK_PRIORITY - 1, NULL );
}
}
/*-----------------------------------------------------------*/
static void prvTimerTestTask( void *pvParameters )
{
( void ) pvParameters;
/* Create a one-shot timer for use later on in this test. */
xOneShotTimer = xTimerCreate( "Oneshot Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
tmrdemoONE_SHOT_TIMER_PERIOD, /* The period for the timer. */
pdFALSE, /* Don't auto-reload - hence a one shot timer. */
( void * ) 0, /* The timer identifier. In this case this is not used as the timer has its own callback. */
prvOneShotTimerCallback ); /* The callback to be called when the timer expires. */
if( xOneShotTimer == NULL )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
/* Ensure all the timers are in their expected initial state. This
depends on the timer service task having a higher priority than this task. */
prvTest2_CheckTaskAndTimersInitialState();
for( ;; )
{
/* Check the auto reload timers expire at the expected/correct rates. */
prvTest3_CheckAutoReloadExpireRates();
/* Check the auto reload timers can be stopped correctly, and correctly
report their state. */
prvTest4_CheckAutoReloadTimersCanBeStopped();
/* Check the one shot timer only calls its callback once after it has been
started, and that it reports its state correctly. */
prvTest5_CheckBasicOneShotTimerBehaviour();
/* Check timer reset behaviour. */
prvTest6_CheckAutoReloadResetBehaviour();
/* Start the timers again to restart all the tests over again. */
prvResetStartConditionsForNextIteration();
}
}
/*-----------------------------------------------------------*/
/* This is called to check that the created task is still running and has not
detected any errors. */
portBASE_TYPE xAreTimerDemoTasksStillRunning( TickType_t xCycleFrequency )
{
static unsigned long ulLastLoopCounter = 0UL;
TickType_t xMaxBlockTimeUsedByTheseTests, xLoopCounterIncrementTimeMax;
static TickType_t xIterationsWithoutCounterIncrement = ( TickType_t ) 0, xLastCycleFrequency;
if( xLastCycleFrequency != xCycleFrequency )
{
/* The cycle frequency has probably become much faster due to an error
elsewhere. Start counting Iterations again. */
xIterationsWithoutCounterIncrement = ( TickType_t ) 0;
xLastCycleFrequency = xCycleFrequency;
}
/* Calculate the maximum number of times that it is permissible for this
function to be called without ulLoopCounter being incremented. This is
necessary because the tests in this file block for extended periods, and the
block period might be longer than the time between calls to this function. */
xMaxBlockTimeUsedByTheseTests = ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod;
xLoopCounterIncrementTimeMax = ( xMaxBlockTimeUsedByTheseTests / xCycleFrequency ) + 1;
/* If the demo task is still running then the loop counter is expected to
have incremented every xLoopCounterIncrementTimeMax calls. */
if( ulLastLoopCounter == ulLoopCounter )
{
xIterationsWithoutCounterIncrement++;
if( xIterationsWithoutCounterIncrement > xLoopCounterIncrementTimeMax )
{
/* The tests appear to be no longer running (stalled). */
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
}
else
{
/* ulLoopCounter changed, so the count of times this function was called
without a change can be reset to zero. */
xIterationsWithoutCounterIncrement = ( TickType_t ) 0;
}
ulLastLoopCounter = ulLoopCounter;
/* Errors detected in the task itself will have latched xTestStatus
to pdFAIL. */
return xTestStatus;
}
/*-----------------------------------------------------------*/
static void prvTest1_CreateTimersWithoutSchedulerRunning( void )
{
unsigned portBASE_TYPE xTimer;
for( xTimer = 0; xTimer < configTIMER_QUEUE_LENGTH; xTimer++ )
{
/* As the timer queue is not yet full, it should be possible to both create
and start a timer. These timers are being started before the scheduler has
been started, so their block times should get set to zero within the timer
API itself. */
xAutoReloadTimers[ xTimer ] = xTimerCreate( "FR Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
( ( xTimer + ( TickType_t ) 1 ) * xBasePeriod ),/* The period for the timer. The plus 1 ensures a period of zero is not specified. */
pdTRUE, /* Auto-reload is set to true. */
( void * ) xTimer, /* An identifier for the timer as all the auto reload timers use the same callback. */
prvAutoReloadTimerCallback ); /* The callback to be called when the timer expires. */
configASSERT( strcmp( pcTimerGetTimerName( xAutoReloadTimers[ xTimer ] ), "FR Timer" ) == 0 );
if( xAutoReloadTimers[ xTimer ] == NULL )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
else
{
/* The scheduler has not yet started, so the block period of
portMAX_DELAY should just get set to zero in xTimerStart(). Also,
the timer queue is not yet full so xTimerStart() should return
pdPASS. */
if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) != pdPASS )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
}
}
/* The timers queue should now be full, so it should be possible to create
another timer, but not possible to start it (the timer queue will not get
drained until the scheduler has been started. */
xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] = xTimerCreate( "FR Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
( configTIMER_QUEUE_LENGTH * xBasePeriod ), /* The period for the timer. */
pdTRUE, /* Auto-reload is set to true. */
( void * ) xTimer, /* An identifier for the timer as all the auto reload timers use the same callback. */
prvAutoReloadTimerCallback ); /* The callback executed when the timer expires. */
if( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] == NULL )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
else
{
if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) == pdPASS )
{
/* This time it would not be expected that the timer could be
started at this point. */
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
}
/* Create the timers that are used from the tick interrupt to test the timer
API functions that can be called from an ISR. */
xISRAutoReloadTimer = xTimerCreate( "ISR AR", /* The text name given to the timer. */
0xffff, /* The timer is not given a period yet - this will be done from the tick hook, but a period of 0 is invalid. */
pdTRUE, /* This is an auto reload timer. */
( void * ) NULL, /* The identifier is not required. */
prvISRAutoReloadTimerCallback ); /* The callback that is executed when the timer expires. */
xISROneShotTimer = xTimerCreate( "ISR OS", /* The text name given to the timer. */
0xffff, /* The timer is not given a period yet - this will be done from the tick hook, but a period of 0 is invalid. */
pdFALSE, /* This is a one shot timer. */
( void * ) NULL, /* The identifier is not required. */
prvISROneShotTimerCallback ); /* The callback that is executed when the timer expires. */
if( ( xISRAutoReloadTimer == NULL ) || ( xISROneShotTimer == NULL ) )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
}
/*-----------------------------------------------------------*/
static void prvTest2_CheckTaskAndTimersInitialState( void )
{
unsigned char ucTimer;
/* Ensure all the timers are in their expected initial state. This depends
on the timer service task having a higher priority than this task.
auto reload timers 0 to ( configTIMER_QUEUE_LENGTH - 1 ) should now be active,
and auto reload timer configTIMER_QUEUE_LENGTH should not yet be active (it
could not be started prior to the scheduler being started when it was
created). */
for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )
{
if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
}
if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] ) != pdFALSE )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
}
/*-----------------------------------------------------------*/
static void prvTest3_CheckAutoReloadExpireRates( void )
{
unsigned char ucMaxAllowableValue, ucMinAllowableValue, ucTimer;
TickType_t xBlockPeriod, xTimerPeriod, xExpectedNumber;
/* Check the auto reload timers expire at the expected rates. */
/* Delaying for configTIMER_QUEUE_LENGTH * xBasePeriod ticks should allow
all the auto reload timers to expire at least once. */
xBlockPeriod = ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod;
vTaskDelay( xBlockPeriod );
/* Check that all the auto reload timers have called their callback
function the expected number of times. */
for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )
{
/* The expected number of expiries is equal to the block period divided
by the timer period. */
xTimerPeriod = ( ( ( TickType_t ) ucTimer + ( TickType_t ) 1 ) * xBasePeriod );
xExpectedNumber = xBlockPeriod / xTimerPeriod;
ucMaxAllowableValue = ( ( unsigned char ) xExpectedNumber ) ;
ucMinAllowableValue = ( ( unsigned char ) xExpectedNumber - ( unsigned char ) 1 );
if( ( ucAutoReloadTimerCounters[ ucTimer ] < ucMinAllowableValue ) ||
( ucAutoReloadTimerCounters[ ucTimer ] > ucMaxAllowableValue )
)
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
}
if( xTestStatus == pdPASS )
{
/* No errors have been reported so increment the loop counter so the
check task knows this task is still running. */
ulLoopCounter++;
}
}
/*-----------------------------------------------------------*/
static void prvTest4_CheckAutoReloadTimersCanBeStopped( void )
{
unsigned char ucTimer;
/* Check the auto reload timers can be stopped correctly, and correctly
report their state. */
/* Stop all the active timers. */
for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )
{
/* The timer has not been stopped yet! */
if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
/* Now stop the timer. This will appear to happen immediately to
this task because this task is running at a priority below the
timer service task. */
xTimerStop( xAutoReloadTimers[ ucTimer ], tmrdemoDONT_BLOCK );
/* The timer should now be inactive. */
if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
}
taskENTER_CRITICAL();
{
/* The timer in array position configTIMER_QUEUE_LENGTH should not
be active. The critical section is used to ensure the timer does
not call its callback between the next line running and the array
being cleared back to zero, as that would mask an error condition. */
if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH ] != ( unsigned char ) 0 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
/* Clear the timer callback count. */
memset( ( void * ) ucAutoReloadTimerCounters, 0, sizeof( ucAutoReloadTimerCounters ) );
}
taskEXIT_CRITICAL();
/* The timers are now all inactive, so this time, after delaying, none
of the callback counters should have incremented. */
vTaskDelay( ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod );
for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )
{
if( ucAutoReloadTimerCounters[ ucTimer ] != ( unsigned char ) 0 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
}
if( xTestStatus == pdPASS )
{
/* No errors have been reported so increment the loop counter so
the check task knows this task is still running. */
ulLoopCounter++;
}
}
/*-----------------------------------------------------------*/
static void prvTest5_CheckBasicOneShotTimerBehaviour( void )
{
/* Check the one shot timer only calls its callback once after it has been
started, and that it reports its state correctly. */
/* The one shot timer should not be active yet. */
if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
if( ucOneShotTimerCounter != ( unsigned char ) 0 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
/* Start the one shot timer and check that it reports its state correctly. */
xTimerStart( xOneShotTimer, tmrdemoDONT_BLOCK );
if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
/* Delay for three times as long as the one shot timer period, then check
to ensure it has only called its callback once, and is now not in the
active state. */
vTaskDelay( tmrdemoONE_SHOT_TIMER_PERIOD * ( TickType_t ) 3 );
if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
if( ucOneShotTimerCounter != ( unsigned char ) 1 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
else
{
/* Reset the one shot timer callback count. */
ucOneShotTimerCounter = ( unsigned char ) 0;
}
if( xTestStatus == pdPASS )
{
/* No errors have been reported so increment the loop counter so the
check task knows this task is still running. */
ulLoopCounter++;
}
}
/*-----------------------------------------------------------*/
static void prvTest6_CheckAutoReloadResetBehaviour( void )
{
unsigned char ucTimer;
/* Check timer reset behaviour. */
/* Restart the one shot timer and check it reports its status correctly. */
xTimerStart( xOneShotTimer, tmrdemoDONT_BLOCK );
if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
/* Restart one of the auto reload timers and check that it reports its
status correctly. */
xTimerStart( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
for( ucTimer = 0; ucTimer < trmdemoNUM_TIMER_RESETS; ucTimer++ )
{
/* Delay for half as long as the one shot timer period, then reset it.
It should never expire while this is done, so its callback count should
never increment. */
vTaskDelay( tmrdemoONE_SHOT_TIMER_PERIOD / 2 );
/* Check both running timers are still active, but have not called their
callback functions. */
if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
if( ucOneShotTimerCounter != ( unsigned char ) 0 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] != ( unsigned char ) 0 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
/* Reset both running timers. */
xTimerReset( xOneShotTimer, tmrdemoDONT_BLOCK );
xTimerReset( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
if( xTestStatus == pdPASS )
{
/* No errors have been reported so increment the loop counter so
the check task knows this task is still running. */
ulLoopCounter++;
}
}
/* Finally delay long enough for both running timers to expire. */
vTaskDelay( ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod );
/* The timers were not reset during the above delay period so should now
both have called their callback functions. */
if( ucOneShotTimerCounter != ( unsigned char ) 1 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] == 0 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
/* The one shot timer should no longer be active, while the auto reload
timer should still be active. */
if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
if( xTimerIsTimerActive( xOneShotTimer ) == pdTRUE )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
/* Stop the auto reload timer again. */
xTimerStop( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) != pdFALSE )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
/* Clear the timer callback counts, ready for another iteration of these
tests. */
ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] = ( unsigned char ) 0;
ucOneShotTimerCounter = ( unsigned char ) 0;
if( xTestStatus == pdPASS )
{
/* No errors have been reported so increment the loop counter so the check
task knows this task is still running. */
ulLoopCounter++;
}
}
/*-----------------------------------------------------------*/
static void prvResetStartConditionsForNextIteration( void )
{
unsigned char ucTimer;
/* Start the timers again to start all the tests over again. */
/* Start the timers again. */
for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )
{
/* The timer has not been started yet! */
if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
/* Now start the timer. This will appear to happen immediately to
this task because this task is running at a priority below the timer
service task. */
xTimerStart( xAutoReloadTimers[ ucTimer ], tmrdemoDONT_BLOCK );
/* The timer should now be active. */
if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
}
if( xTestStatus == pdPASS )
{
/* No errors have been reported so increment the loop counter so the
check task knows this task is still running. */
ulLoopCounter++;
}
}
/*-----------------------------------------------------------*/
void vTimerPeriodicISRTests( void )
{
static TickType_t uxTick = ( TickType_t ) -1;
#if( configTIMER_TASK_PRIORITY != ( configMAX_PRIORITIES - 1 ) )
/* The timer service task is not the highest priority task, so it cannot
be assumed that timings will be exact. Timers should never call their
callback before their expiry time, but a margin is permissible for calling
their callback after their expiry time. If exact timing is required then
configTIMER_TASK_PRIORITY must be set to ensure the timer service task
is the highest priority task in the system.
This function is called from the tick hook. The tick hook is called
even when the scheduler is suspended. Therefore it is possible that the
uxTick count maintained in this function is temporarily ahead of the tick
count maintained by the kernel. When this is the case a message posted from
this function will assume a time stamp in advance of the real time stamp,
which can result in a timer being processed before this function expects it
to. For example, if the kernel's tick count was 100, and uxTick was 102,
then this function will not expect the timer to have expired until the
kernel's tick count is (102 + xBasePeriod), whereas in reality the timer
will expire when the kernel's tick count is (100 + xBasePeriod). For this
reason xMargin is used as an allowable margin for premature timer expiries
as well as late timer expiries. */
const TickType_t xMargin = 6;
#else
const TickType_t xMargin = 3;
#endif
uxTick++;
if( uxTick == 0 )
{
/* The timers will have been created, but not started. Start them now
by setting their period. */
ucISRAutoReloadTimerCounter = 0;
ucISROneShotTimerCounter = 0;
/* It is possible that the timer task has not yet made room in the
timer queue. If the timers cannot be started then reset uxTick so
another attempt is made later. */
uxTick = ( TickType_t ) -1;
/* Try starting first timer. */
if( xTimerChangePeriodFromISR( xISRAutoReloadTimer, xBasePeriod, NULL ) == pdPASS )
{
/* First timer was started, try starting the second timer. */
if( xTimerChangePeriodFromISR( xISROneShotTimer, xBasePeriod, NULL ) == pdPASS )
{
/* Both timers were started, so set the uxTick back to its
proper value. */
uxTick = 0;
}
else
{
/* Second timer could not be started, so stop the first one
again. */
xTimerStopFromISR( xISRAutoReloadTimer, NULL );
}
}
}
else if( uxTick == ( xBasePeriod - xMargin ) )
{
/* Neither timer should have expired yet. */
if( ( ucISRAutoReloadTimerCounter != 0 ) || ( ucISROneShotTimerCounter != 0 ) )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
}
else if( uxTick == ( xBasePeriod + xMargin ) )
{
/* Both timers should now have expired once. The auto reload timer will
still be active, but the one shot timer should now have stopped. */
if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
}
else if( uxTick == ( ( 2 * xBasePeriod ) - xMargin ) )
{
/* The auto reload timer will still be active, but the one shot timer
should now have stopped - however, at this time neither of the timers
should have expired again since the last test. */
if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
}
else if( uxTick == ( ( 2 * xBasePeriod ) + xMargin ) )
{
/* The auto reload timer will still be active, but the one shot timer
should now have stopped. At this time the auto reload timer should have
expired again, but the one shot timer count should not have changed. */
if( ucISRAutoReloadTimerCounter != 2 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
if( ucISROneShotTimerCounter != 1 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
}
else if( uxTick == ( ( 2 * xBasePeriod ) + ( xBasePeriod >> ( TickType_t ) 2U ) ) )
{
/* The auto reload timer will still be active, but the one shot timer
should now have stopped. Again though, at this time, neither timer call
back should have been called since the last test. */
if( ucISRAutoReloadTimerCounter != 2 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
if( ucISROneShotTimerCounter != 1 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
}
else if( uxTick == ( 3 * xBasePeriod ) )
{
/* Start the one shot timer again. */
xTimerStartFromISR( xISROneShotTimer, NULL );
}
else if( uxTick == ( ( 3 * xBasePeriod ) + xMargin ) )
{
/* The auto reload timer and one shot timer will be active. At
this time the auto reload timer should have expired again, but the one
shot timer count should not have changed yet. */
if( ucISRAutoReloadTimerCounter != 3 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
if( ucISROneShotTimerCounter != 1 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
/* Now stop the auto reload timer. The one shot timer was started
a few ticks ago. */
xTimerStopFromISR( xISRAutoReloadTimer, NULL );
}
else if( uxTick == ( 4 * ( xBasePeriod - xMargin ) ) )
{
/* The auto reload timer is now stopped, and the one shot timer is
active, but at this time neither timer should have expired since the
last test. */
if( ucISRAutoReloadTimerCounter != 3 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
if( ucISROneShotTimerCounter != 1 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
}
else if( uxTick == ( ( 4 * xBasePeriod ) + xMargin ) )
{
/* The auto reload timer is now stopped, and the one shot timer is
active. The one shot timer should have expired again, but the auto
reload timer should not have executed its callback. */
if( ucISRAutoReloadTimerCounter != 3 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
if( ucISROneShotTimerCounter != 2 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
}
else if( uxTick == ( 8 * xBasePeriod ) )
{
/* The auto reload timer is now stopped, and the one shot timer has
already expired and then stopped itself. Both callback counters should
not have incremented since the last test. */
if( ucISRAutoReloadTimerCounter != 3 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
if( ucISROneShotTimerCounter != 2 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
/* Now reset the one shot timer. */
xTimerResetFromISR( xISROneShotTimer, NULL );
}
else if( uxTick == ( ( 9 * xBasePeriod ) - xMargin ) )
{
/* Only the one shot timer should be running, but it should not have
expired since the last test. Check the callback counters have not
incremented, then reset the one shot timer again. */
if( ucISRAutoReloadTimerCounter != 3 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
if( ucISROneShotTimerCounter != 2 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
xTimerResetFromISR( xISROneShotTimer, NULL );
}
else if( uxTick == ( ( 10 * xBasePeriod ) - ( 2 * xMargin ) ) )
{
/* Only the one shot timer should be running, but it should not have
expired since the last test. Check the callback counters have not
incremented, then reset the one shot timer again. */
if( ucISRAutoReloadTimerCounter != 3 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
if( ucISROneShotTimerCounter != 2 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
xTimerResetFromISR( xISROneShotTimer, NULL );
}
else if( uxTick == ( ( 11 * xBasePeriod ) - ( 3 * xMargin ) ) )
{
/* Only the one shot timer should be running, but it should not have
expired since the last test. Check the callback counters have not
incremented, then reset the one shot timer once again. */
if( ucISRAutoReloadTimerCounter != 3 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
if( ucISROneShotTimerCounter != 2 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
xTimerResetFromISR( xISROneShotTimer, NULL );
}
else if( uxTick == ( ( 12 * xBasePeriod ) - ( 2 * xMargin ) ) )
{
/* Only the one shot timer should have been running and this time it
should have expired. Check its callback count has been incremented.
The auto reload timer is still not running so should still have the same
count value. This time the one shot timer is not reset so should not
restart from its expiry period again. */
if( ucISRAutoReloadTimerCounter != 3 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
if( ucISROneShotTimerCounter != 3 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
}
else if( uxTick == ( 15 * xBasePeriod ) )
{
/* Neither timer should be running now. Check neither callback count
has incremented, then go back to the start to run these tests all
over again. */
if( ucISRAutoReloadTimerCounter != 3 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
if( ucISROneShotTimerCounter != 3 )
{
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
uxTick = ( TickType_t ) -1;
}
}
/*-----------------------------------------------------------*/
/*** Timer callback functions are defined below here. ***/
static void prvAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer )
{
unsigned long ulTimerID;
ulTimerID = ( unsigned long ) pvTimerGetTimerID( pxExpiredTimer );
if( ulTimerID <= ( configTIMER_QUEUE_LENGTH + 1 ) )
{
( ucAutoReloadTimerCounters[ ulTimerID ] )++;
}
else
{
/* The timer ID appears to be unexpected (invalid). */
xTestStatus = pdFAIL;
configASSERT( xTestStatus );
}
}
/*-----------------------------------------------------------*/
static void prvOneShotTimerCallback( TimerHandle_t pxExpiredTimer )
{
/* The parameter is not used in this case as only one timer uses this
callback function. */
( void ) pxExpiredTimer;
ucOneShotTimerCounter++;
}
/*-----------------------------------------------------------*/
static void prvISRAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer )
{
/* The parameter is not used in this case as only one timer uses this
callback function. */
( void ) pxExpiredTimer;
ucISRAutoReloadTimerCounter++;
}
/*-----------------------------------------------------------*/
static void prvISROneShotTimerCallback( TimerHandle_t pxExpiredTimer )
{
/* The parameter is not used in this case as only one timer uses this
callback function. */
( void ) pxExpiredTimer;
ucISROneShotTimerCounter++;
}
/*-----------------------------------------------------------*/
|