]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/TimerDemo.c
Spell check comments in TimerDemo.c.
[freertos] / Demo / Common / Minimal / TimerDemo.c
1 /*\r
2     FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS books - available as PDF or paperback  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 \r
55 /*\r
56  * Tests the behaviour of timers.  Some timers are created before the scheduler\r
57  * is started, and some after.\r
58  */\r
59 \r
60 /* Standard includes. */\r
61 #include <string.h>\r
62 \r
63 /* Scheduler include files. */\r
64 #include "FreeRTOS.h"\r
65 #include "task.h"\r
66 #include "timers.h"\r
67 \r
68 /* Demo program include files. */\r
69 #include "TimerDemo.h"\r
70 \r
71 #if ( configTIMER_TASK_PRIORITY < 1 )\r
72         #error configTIMER_TASK_PRIORITY must be set to at least 1 for this test/demo to function correctly.\r
73 #endif\r
74 \r
75 #define tmrdemoDONT_BLOCK                               ( ( portTickType ) 0 )\r
76 #define tmrdemoONE_SHOT_TIMER_PERIOD    ( xBasePeriod * ( portTickType ) 3 )\r
77 #define trmdemoNUM_TIMER_RESETS                 ( ( unsigned char ) 10 )\r
78 \r
79 /*-----------------------------------------------------------*/\r
80 \r
81 /* The callback functions used by the timers.  These each increment a counter\r
82 to indicate which timer has expired.  The auto-reload timers that are used by\r
83 the test task (as opposed to being used from an ISR) all share the same\r
84 prvAutoReloadTimerCallback() callback function, and use the ID of the\r
85 pxExpiredTimer parameter passed into that function to know which counter to\r
86 increment.  The other timers all have their own unique callback function and\r
87 simply increment their counters without using the callback function parameter. */\r
88 static void prvAutoReloadTimerCallback( xTimerHandle pxExpiredTimer );\r
89 static void prvOneShotTimerCallback( xTimerHandle pxExpiredTimer );\r
90 static void prvTimerTestTask( void *pvParameters );\r
91 static void prvISRAutoReloadTimerCallback( xTimerHandle pxExpiredTimer );\r
92 static void prvISROneShotTimerCallback( xTimerHandle pxExpiredTimer );\r
93 \r
94 /* The test functions used by the timer test task.  These manipulate the auto\r
95 reload and one shot timers in various ways, then delay, then inspect the timers\r
96 to ensure they have behaved as expected. */\r
97 static void prvTest1_CreateTimersWithoutSchedulerRunning( void );\r
98 static void prvTest2_CheckTaskAndTimersInitialState( void );\r
99 static void     prvTest3_CheckAutoReloadExpireRates( void );\r
100 static void prvTest4_CheckAutoReloadTimersCanBeStopped( void );\r
101 static void prvTest5_CheckBasicOneShotTimerBehaviour( void );\r
102 static void prvTest6_CheckAutoReloadResetBehaviour( void );\r
103 static void prvResetStartConditionsForNextIteration( void );\r
104 \r
105 /*-----------------------------------------------------------*/\r
106 \r
107 /* Flag that will be latched to pdFAIL should any unexpected behaviour be\r
108 detected in any of the demo tests. */\r
109 static volatile portBASE_TYPE xTestStatus = pdPASS;\r
110 \r
111 /* Counter that is incremented on each cycle of a test.  This is used to\r
112 detect a stalled task - a test that is no longer running. */\r
113 static volatile unsigned long ulLoopCounter = 0;\r
114 \r
115 /* A set of auto reload timers - each of which use the same callback function.\r
116 The callback function uses the timer ID to index into, and then increment, a\r
117 counter in the ucAutoReloadTimerCounters[] array.  The auto reload timers\r
118 referenced from xAutoReloadTimers[] are used by the prvTimerTestTask task. */\r
119 static xTimerHandle xAutoReloadTimers[ configTIMER_QUEUE_LENGTH + 1 ] = { 0 };\r
120 static unsigned char ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH + 1 ] = { 0 };\r
121 \r
122 /* The one shot timer is configured to use a callback function that increments\r
123 ucOneShotTimerCounter each time it gets called. */\r
124 static xTimerHandle xOneShotTimer = NULL;\r
125 static unsigned char ucOneShotTimerCounter = ( unsigned char ) 0;\r
126 \r
127 /* The ISR reload timer is controlled from the tick hook to exercise the timer\r
128 API functions that can be used from an ISR.  It is configured to increment\r
129 ucISRReloadTimerCounter each time its callback function is executed. */\r
130 static xTimerHandle xISRAutoReloadTimer = NULL;\r
131 static unsigned char ucISRAutoReloadTimerCounter = ( unsigned char ) 0;\r
132 \r
133 /* The ISR one shot timer is controlled from the tick hook to exercise the timer\r
134 API functions that can be used from an ISR.  It is configured to increment\r
135 ucISRReloadTimerCounter each time its callback function is executed. */\r
136 static xTimerHandle xISROneShotTimer = NULL;\r
137 static unsigned char ucISROneShotTimerCounter = ( unsigned char ) 0;\r
138 \r
139 /* The period of all the timers are a multiple of the base period.  The base\r
140 period is configured by the parameter to vStartTimerDemoTask(). */\r
141 static portTickType xBasePeriod = 0;\r
142 \r
143 /*-----------------------------------------------------------*/\r
144 \r
145 void vStartTimerDemoTask( portTickType xBasePeriodIn )\r
146 {\r
147         /* Store the period from which all the timer periods will be generated from\r
148         (multiples of). */\r
149         xBasePeriod = xBasePeriodIn;\r
150 \r
151         /* Create a set of timers for use by this demo/test. */\r
152         prvTest1_CreateTimersWithoutSchedulerRunning();\r
153 \r
154         /* Create the task that will control and monitor the timers.  This is\r
155         created at a lower priority than the timer service task to ensure, as\r
156         far as it is concerned, commands on timers are actioned immediately\r
157         (sending a command to the timer service task will unblock the timer service\r
158         task, which will then preempt this task). */\r
159         if( xTestStatus != pdFAIL )\r
160         {\r
161                 xTaskCreate( prvTimerTestTask, ( signed portCHAR * ) "Tmr Tst", configMINIMAL_STACK_SIZE, NULL, configTIMER_TASK_PRIORITY - 1, NULL );\r
162         }\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 static void prvTimerTestTask( void *pvParameters )\r
167 {\r
168         ( void ) pvParameters;\r
169 \r
170         /* Create a one-shot timer for use later on in this test. */\r
171         xOneShotTimer = xTimerCreate(   "Oneshot Timer",                                /* Text name to facilitate debugging.  The kernel does not use this itself. */\r
172                                                                         tmrdemoONE_SHOT_TIMER_PERIOD,   /* The period for the timer. */\r
173                                                                         pdFALSE,                                                /* Don't auto-reload - hence a one shot timer. */\r
174                                                                         ( void * ) 0,                                   /* The timer identifier.  In this case this is not used as the timer has its own callback. */\r
175                                                                         prvOneShotTimerCallback );              /* The callback to be called when the timer expires. */\r
176 \r
177         if( xOneShotTimer == NULL )\r
178         {\r
179                 xTestStatus = pdFAIL;\r
180                 configASSERT( xTestStatus );\r
181         }\r
182 \r
183 \r
184         /* Ensure all the timers are in their expected initial state.  This\r
185         depends on the timer service task having a higher priority than this task. */\r
186         prvTest2_CheckTaskAndTimersInitialState();\r
187 \r
188         for( ;; )\r
189         {\r
190                 /* Check the auto reload timers expire at the expected/correct rates. */\r
191                 prvTest3_CheckAutoReloadExpireRates();\r
192 \r
193                 /* Check the auto reload timers can be stopped correctly, and correctly\r
194                 report their state. */\r
195                 prvTest4_CheckAutoReloadTimersCanBeStopped();\r
196                                 \r
197                 /* Check the one shot timer only calls its callback once after it has been\r
198                 started, and that it reports its state correctly. */\r
199                 prvTest5_CheckBasicOneShotTimerBehaviour();\r
200 \r
201                 /* Check timer reset behaviour. */\r
202                 prvTest6_CheckAutoReloadResetBehaviour();\r
203 \r
204                 /* Start the timers again to restart all the tests over again. */\r
205                 prvResetStartConditionsForNextIteration();\r
206         }\r
207 }\r
208 /*-----------------------------------------------------------*/\r
209 \r
210 /* This is called to check that the created task is still running and has not\r
211 detected any errors. */\r
212 portBASE_TYPE xAreTimerDemoTasksStillRunning( portTickType xCycleFrequency )\r
213 {\r
214 static unsigned long ulLastLoopCounter = 0UL;\r
215 portTickType xMaxBlockTimeUsedByTheseTests, xLoopCounterIncrementTimeMax;\r
216 static portTickType xIterationsWithoutCounterIncrement = ( portTickType ) 0, xLastCycleFrequency;\r
217 \r
218         if( xLastCycleFrequency != xCycleFrequency )\r
219         {\r
220                 /* The cycle frequency has probably become much faster due to an error\r
221                 elsewhere.  Start counting Iterations again. */\r
222                 xIterationsWithoutCounterIncrement = ( portTickType ) 0;\r
223                 xLastCycleFrequency = xCycleFrequency;\r
224         }               \r
225 \r
226         /* Calculate the maximum number of times that it is permissible for this\r
227         function to be called without ulLoopCounter being incremented.  This is\r
228         necessary because the tests in this file block for extended periods, and the\r
229         block period might be longer than the time between calls to this function. */\r
230         xMaxBlockTimeUsedByTheseTests = ( ( portTickType ) configTIMER_QUEUE_LENGTH ) * xBasePeriod;\r
231         xLoopCounterIncrementTimeMax = xMaxBlockTimeUsedByTheseTests / xCycleFrequency;\r
232 \r
233         /* If the demo task is still running then we expect the loopcounter to\r
234         have incremented every xLoopCounterIncrementTimeMax calls. */\r
235         if( ulLastLoopCounter == ulLoopCounter )\r
236         {\r
237                 xIterationsWithoutCounterIncrement++;\r
238                 if( xIterationsWithoutCounterIncrement > xLoopCounterIncrementTimeMax )\r
239                 {\r
240                         /* The tests appear to be no longer running (stalled). */\r
241                         xTestStatus = pdFAIL;\r
242                         configASSERT( xTestStatus );\r
243                 }\r
244         }\r
245         else\r
246         {\r
247                 /* ulLoopCounter changed, so the count of times this function was called\r
248                 without a change can be reset to zero. */\r
249                 xIterationsWithoutCounterIncrement = ( portTickType ) 0;\r
250         }\r
251 \r
252         ulLastLoopCounter = ulLoopCounter;\r
253 \r
254         /* Errors detected in the task itself will have latched xTestStatus\r
255         to pdFAIL. */\r
256 \r
257         return xTestStatus;\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 static void prvTest1_CreateTimersWithoutSchedulerRunning( void )\r
262 {\r
263 portBASE_TYPE xTimer;\r
264 \r
265         for( xTimer = 0; xTimer < configTIMER_QUEUE_LENGTH; xTimer++ )\r
266         {\r
267                 /* As the timer queue is not yet full, it should be possible to both create\r
268                 and start a timer.  These timers are being started before the scheduler has\r
269                 been started, so their block times should get set to zero within the timer\r
270                 API itself. */\r
271                 xAutoReloadTimers[ xTimer ] = xTimerCreate( "FR Timer",                                         /* Text name to facilitate debugging.  The kernel does not use this itself. */\r
272                                                                                                         ( ( xTimer + 1 ) * xBasePeriod ),/* The period for the timer.  The plus 1 ensures a period of zero is not specified. */\r
273                                                                                                         pdTRUE,                                                         /* Auto-reload is set to true. */\r
274                                                                                                         ( void * ) xTimer,                                      /* An identifier for the timer as all the auto reload timers use the same callback. */\r
275                                                                                                         prvAutoReloadTimerCallback );           /* The callback to be called when the timer expires. */\r
276 \r
277                 if( xAutoReloadTimers[ xTimer ] == NULL )\r
278                 {\r
279                         xTestStatus = pdFAIL;\r
280                         configASSERT( xTestStatus );\r
281                 }\r
282                 else\r
283                 {\r
284                         /* The scheduler has not yet started, so the block period of\r
285                         portMAX_DELAY should just get set to zero in xTimerStart().  Also,\r
286                         the timer queue is not yet full so xTimerStart() should return\r
287                         pdPASS. */\r
288                         if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) != pdPASS )\r
289                         {\r
290                                 xTestStatus = pdFAIL;\r
291                                 configASSERT( xTestStatus );\r
292                         }\r
293                 }\r
294         }\r
295 \r
296         /* The timers queue should now be full, so it should be possible to create\r
297         another timer, but not possible to start it (the timer queue will not get\r
298         drained until the scheduler has been started. */\r
299         xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] = xTimerCreate( "FR Timer",               /* Text name to facilitate debugging.  The kernel does not use this itself. */\r
300                                                                                                         ( configTIMER_QUEUE_LENGTH * xBasePeriod ),     /* The period for the timer. */\r
301                                                                                                         pdTRUE,                                                 /* Auto-reload is set to true. */\r
302                                                                                                         ( void * ) xTimer,                              /* An identifier for the timer as all the auto reload timers use the same callback. */\r
303                                                                                                         prvAutoReloadTimerCallback );   /* The callback executed when the timer expires. */\r
304 \r
305         if( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] == NULL )\r
306         {\r
307                 xTestStatus = pdFAIL;\r
308                 configASSERT( xTestStatus );\r
309         }\r
310         else\r
311         {\r
312                 if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) == pdPASS )\r
313                 {\r
314                         /* This time it would not be expected that the timer could be\r
315                         started at this point. */\r
316                         xTestStatus = pdFAIL;\r
317                 configASSERT( xTestStatus );\r
318                 }\r
319         }\r
320         \r
321         /* Create the timers that are used from the tick interrupt to test the timer\r
322         API functions that can be called from an ISR. */\r
323         xISRAutoReloadTimer = xTimerCreate( "ISR AR",                                           /* The text name given to the timer. */\r
324                                                                                 0,                                                              /* The timer is not given a period yet - this will be done from the tick hook. */\r
325                                                                                 pdTRUE,                                                 /* This is an auto reload timer. */\r
326                                                                                 ( void * ) NULL,                                /* The identifier is not required. */\r
327                                                                                 prvISRAutoReloadTimerCallback );/* The callback that is executed when the timer expires. */\r
328 \r
329         xISROneShotTimer = xTimerCreate(        "ISR OS",                                               /* The text name given to the timer. */\r
330                                                                                 0,                                                              /* The timer is not given a period yet - this will be done from the tick hook. */\r
331                                                                                 pdFALSE,                                                /* This is a one shot timer. */\r
332                                                                                 ( void * ) NULL,                                /* The identifier is not required. */\r
333                                                                                 prvISROneShotTimerCallback );   /* The callback that is executed when the timer expires. */\r
334                                                                                 \r
335         if( ( xISRAutoReloadTimer == NULL ) || ( xISROneShotTimer == NULL ) )\r
336         {\r
337                 xTestStatus = pdFAIL;\r
338                 configASSERT( xTestStatus );\r
339         }\r
340 }\r
341 /*-----------------------------------------------------------*/\r
342 \r
343 static void prvTest2_CheckTaskAndTimersInitialState( void )\r
344 {\r
345 unsigned char ucTimer;\r
346 \r
347         /* Ensure all the timers are in their expected initial state.  This     depends\r
348         on the timer service task having a higher priority than this task.\r
349 \r
350         auto reload timers 0 to ( configTIMER_QUEUE_LENGTH - 1 ) should now be active,\r
351         and auto reload timer configTIMER_QUEUE_LENGTH should not yet be active (it\r
352         could not be started prior to the scheduler being started when it was\r
353         created). */\r
354         for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )\r
355         {\r
356                 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )\r
357                 {\r
358                         xTestStatus = pdFAIL;\r
359                         configASSERT( xTestStatus );\r
360                 }\r
361         }\r
362 \r
363         if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] ) != pdFALSE )\r
364         {\r
365                 xTestStatus = pdFAIL;\r
366                 configASSERT( xTestStatus );\r
367         }\r
368 }\r
369 /*-----------------------------------------------------------*/\r
370 \r
371 static void     prvTest3_CheckAutoReloadExpireRates( void )\r
372 {\r
373 unsigned char ucMaxAllowableValue, ucMinAllowableValue, ucTimer;\r
374 portTickType xBlockPeriod, xTimerPeriod, xExpectedNumber;\r
375 \r
376         /* Check the auto reload timers expire at the expected rates. */\r
377 \r
378         \r
379         /* Delaying for configTIMER_QUEUE_LENGTH * xBasePeriod ticks should allow\r
380         all the auto reload timers to expire at least once. */\r
381         xBlockPeriod = ( ( portTickType ) configTIMER_QUEUE_LENGTH ) * xBasePeriod;\r
382         vTaskDelay( xBlockPeriod );\r
383 \r
384         /* Check that all the auto reload timers have called their callback     \r
385         function the expected number of times. */\r
386         for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )\r
387         {\r
388                 /* The expected number of expiries is equal to the block period divided\r
389                 by the timer period. */\r
390                 xTimerPeriod = ( ( ( portTickType ) ucTimer + ( portTickType ) 1 ) * xBasePeriod );\r
391                 xExpectedNumber = xBlockPeriod / xTimerPeriod;\r
392                 \r
393                 ucMaxAllowableValue = ( ( unsigned char ) xExpectedNumber ) ;\r
394                 ucMinAllowableValue = ( ( unsigned char ) xExpectedNumber - ( unsigned char ) 1 );\r
395 \r
396                 if( ( ucAutoReloadTimerCounters[ ucTimer ] < ucMinAllowableValue ) ||\r
397                         ( ucAutoReloadTimerCounters[ ucTimer ] > ucMaxAllowableValue )\r
398                         )\r
399                 {\r
400                         xTestStatus = pdFAIL;\r
401                         configASSERT( xTestStatus );\r
402                 }\r
403         }\r
404 \r
405         if( xTestStatus == pdPASS )\r
406         {\r
407                 /* No errors have been reported so increment the loop counter so the\r
408                 check task knows this task is still running. */\r
409                 ulLoopCounter++;\r
410         }\r
411 }\r
412 /*-----------------------------------------------------------*/\r
413 \r
414 static void prvTest4_CheckAutoReloadTimersCanBeStopped( void )\r
415 {               \r
416 unsigned char ucTimer;\r
417 \r
418         /* Check the auto reload timers can be stopped correctly, and correctly\r
419         report their state. */\r
420 \r
421         /* Stop all the active timers. */\r
422         for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )\r
423         {\r
424                 /* The timer has not been stopped yet! */\r
425                 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )\r
426                 {\r
427                         xTestStatus = pdFAIL;\r
428                         configASSERT( xTestStatus );\r
429                 }\r
430 \r
431                 /* Now stop the timer.  This will appear to happen immediately to\r
432                 this task because this task is running at a priority below the\r
433                 timer service task. */\r
434                 xTimerStop( xAutoReloadTimers[ ucTimer ], tmrdemoDONT_BLOCK );\r
435 \r
436                 /* The timer should now be inactive. */\r
437                 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )\r
438                 {\r
439                         xTestStatus = pdFAIL;\r
440                         configASSERT( xTestStatus );\r
441                 }\r
442         }\r
443 \r
444         taskENTER_CRITICAL();\r
445         {\r
446                 /* The timer in array position configTIMER_QUEUE_LENGTH should not\r
447                 be active.  The critical section is used to ensure the timer does\r
448                 not call its callback between the next line running and the array\r
449                 being cleared back to zero, as that would mask an error condition. */\r
450                 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH ] != ( unsigned char ) 0 )\r
451                 {\r
452                         xTestStatus = pdFAIL;\r
453                         configASSERT( xTestStatus );\r
454                 }\r
455 \r
456                 /* Clear the timer callback count. */\r
457                 memset( ( void * ) ucAutoReloadTimerCounters, 0, sizeof( ucAutoReloadTimerCounters ) );\r
458         }\r
459         taskEXIT_CRITICAL();\r
460 \r
461         /* The timers are now all inactive, so this time, after delaying, none\r
462         of the callback counters should have incremented. */\r
463         vTaskDelay( ( ( portTickType ) configTIMER_QUEUE_LENGTH ) * xBasePeriod );\r
464         for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )\r
465         {\r
466                 if( ucAutoReloadTimerCounters[ ucTimer ] != ( unsigned char ) 0 )\r
467                 {\r
468                         xTestStatus = pdFAIL;\r
469                         configASSERT( xTestStatus );\r
470                 }\r
471         }\r
472 \r
473         if( xTestStatus == pdPASS )\r
474         {\r
475                 /* No errors have been reported so increment the loop counter so\r
476                 the check task knows this task is still running. */\r
477                 ulLoopCounter++;\r
478         }\r
479 }\r
480 /*-----------------------------------------------------------*/\r
481 \r
482 static void prvTest5_CheckBasicOneShotTimerBehaviour( void )\r
483 {\r
484         /* Check the one shot timer only calls its callback once after it has been\r
485         started, and that it reports its state correctly. */\r
486 \r
487         /* The one shot timer should not be active yet. */\r
488         if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )\r
489         {\r
490                 xTestStatus = pdFAIL;\r
491                 configASSERT( xTestStatus );\r
492         }\r
493 \r
494         if( ucOneShotTimerCounter != ( unsigned char ) 0 )\r
495         {\r
496                 xTestStatus = pdFAIL;\r
497                 configASSERT( xTestStatus );\r
498         }\r
499 \r
500         /* Start the one shot timer and check that it reports its state correctly. */\r
501         xTimerStart( xOneShotTimer, tmrdemoDONT_BLOCK );\r
502         if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )\r
503         {\r
504                 xTestStatus = pdFAIL;\r
505                 configASSERT( xTestStatus );\r
506         }\r
507 \r
508         /* Delay for three times as long as the one shot timer period, then check\r
509         to ensure it has only called its callback once, and is now not in the\r
510         active state. */\r
511         vTaskDelay( tmrdemoONE_SHOT_TIMER_PERIOD * ( portTickType ) 3 );\r
512 \r
513         if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )\r
514         {\r
515                 xTestStatus = pdFAIL;\r
516                 configASSERT( xTestStatus );\r
517         }\r
518 \r
519         if( ucOneShotTimerCounter != ( unsigned char ) 1 )\r
520         {\r
521                 xTestStatus = pdFAIL;\r
522                 configASSERT( xTestStatus );\r
523         }\r
524         else\r
525         {\r
526                 /* Reset the one shot timer callback count. */\r
527                 ucOneShotTimerCounter = ( unsigned char ) 0;\r
528         }\r
529 \r
530         if( xTestStatus == pdPASS )\r
531         {\r
532                 /* No errors have been reported so increment the loop counter so the\r
533                 check task knows this task is still running. */\r
534                 ulLoopCounter++;\r
535         }\r
536 }\r
537 /*-----------------------------------------------------------*/\r
538 \r
539 static void prvTest6_CheckAutoReloadResetBehaviour( void )\r
540 {\r
541 unsigned char ucTimer;\r
542 \r
543         /* Check timer reset behaviour. */\r
544 \r
545         /* Restart the one shot timer and check it reports its status correctly. */\r
546         xTimerStart( xOneShotTimer, tmrdemoDONT_BLOCK );\r
547         if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )\r
548         {\r
549                 xTestStatus = pdFAIL;\r
550                 configASSERT( xTestStatus );\r
551         }\r
552 \r
553         /* Restart one of the auto reload timers and check that it reports its\r
554         status correctly. */\r
555         xTimerStart( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );\r
556         if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )\r
557         {\r
558                 xTestStatus = pdFAIL;\r
559                 configASSERT( xTestStatus );\r
560         }\r
561 \r
562         for( ucTimer = 0; ucTimer < trmdemoNUM_TIMER_RESETS; ucTimer++ )\r
563         {\r
564                 /* Delay for half as long as the one shot timer period, then reset it.\r
565                 It should never expire while this is done, so its callback count should\r
566                 never increment. */\r
567                 vTaskDelay( tmrdemoONE_SHOT_TIMER_PERIOD / 2 );\r
568 \r
569                 /* Check both running timers are still active, but have not called their\r
570                 callback functions. */\r
571                 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )\r
572                 {\r
573                         xTestStatus = pdFAIL;\r
574                         configASSERT( xTestStatus );\r
575                 }\r
576 \r
577                 if( ucOneShotTimerCounter != ( unsigned char ) 0 )\r
578                 {\r
579                         xTestStatus = pdFAIL;\r
580                         configASSERT( xTestStatus );\r
581                 }\r
582 \r
583                 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )\r
584                 {\r
585                         xTestStatus = pdFAIL;\r
586                         configASSERT( xTestStatus );\r
587                 }\r
588 \r
589                 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] != ( unsigned char ) 0 )\r
590                 {\r
591                         xTestStatus = pdFAIL;\r
592                         configASSERT( xTestStatus );\r
593                 }\r
594 \r
595                 /* Reset both running timers. */\r
596                 xTimerReset( xOneShotTimer, tmrdemoDONT_BLOCK );\r
597                 xTimerReset( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );\r
598 \r
599                 if( xTestStatus == pdPASS )\r
600                 {\r
601                         /* No errors have been reported so increment the loop counter so\r
602                         the check task knows this task is still running. */\r
603                         ulLoopCounter++;\r
604                 }\r
605         }\r
606 \r
607         /* Finally delay long enough for both running timers to expire. */\r
608         vTaskDelay( ( ( portTickType ) configTIMER_QUEUE_LENGTH ) * xBasePeriod );\r
609 \r
610         /* The timers were not reset during the above delay period so should now\r
611         both have called their callback functions. */\r
612         if( ucOneShotTimerCounter != ( unsigned char ) 1 )\r
613         {\r
614                 xTestStatus = pdFAIL;\r
615                 configASSERT( xTestStatus );\r
616         }\r
617 \r
618         if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] == 0 )\r
619         {\r
620                 xTestStatus = pdFAIL;\r
621                 configASSERT( xTestStatus );\r
622         }\r
623 \r
624         /* The one shot timer should no longer be active, while the auto reload\r
625         timer should still be active. */\r
626         if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )\r
627         {\r
628                 xTestStatus = pdFAIL;\r
629                 configASSERT( xTestStatus );\r
630         }\r
631 \r
632         if( xTimerIsTimerActive( xOneShotTimer ) == pdTRUE )\r
633         {\r
634                 xTestStatus = pdFAIL;\r
635                 configASSERT( xTestStatus );\r
636         }\r
637 \r
638         /* Stop the auto reload timer again. */\r
639         xTimerStop( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );\r
640 \r
641         if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) != pdFALSE )\r
642         {\r
643                 xTestStatus = pdFAIL;\r
644                 configASSERT( xTestStatus );\r
645         }\r
646 \r
647         /* Clear the timer callback counts, ready for another iteration of these\r
648         tests. */\r
649         ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] = ( unsigned char ) 0;\r
650         ucOneShotTimerCounter = ( unsigned char ) 0;\r
651 \r
652         if( xTestStatus == pdPASS )\r
653         {\r
654                 /* No errors have been reported so increment the loop counter so the check\r
655                 task knows this task is still running. */\r
656                 ulLoopCounter++;\r
657         }\r
658 }\r
659 /*-----------------------------------------------------------*/\r
660 \r
661 static void prvResetStartConditionsForNextIteration( void )\r
662 {\r
663 unsigned char ucTimer;\r
664 \r
665         /* Start the timers again to start all the tests over again. */\r
666 \r
667         /* Start the timers again. */\r
668         for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )\r
669         {\r
670                 /* The timer has not been started yet! */\r
671                 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )\r
672                 {\r
673                         xTestStatus = pdFAIL;\r
674                         configASSERT( xTestStatus );\r
675                 }\r
676 \r
677                 /* Now start the timer.  This will appear to happen immediately to\r
678                 this task because this task is running at a priority below the timer\r
679                 service task. */\r
680                 xTimerStart( xAutoReloadTimers[ ucTimer ], tmrdemoDONT_BLOCK );\r
681 \r
682                 /* The timer should now be active. */\r
683                 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )\r
684                 {\r
685                         xTestStatus = pdFAIL;\r
686                         configASSERT( xTestStatus );\r
687                 }\r
688         }\r
689 \r
690         if( xTestStatus == pdPASS )\r
691         {\r
692                 /* No errors have been reported so increment the loop counter so the\r
693                 check task knows this task is still running. */\r
694                 ulLoopCounter++;\r
695         }\r
696 }\r
697 /*-----------------------------------------------------------*/\r
698 \r
699 void vTimerPeriodicISRTests( void )\r
700 {\r
701 static unsigned portBASE_TYPE uxTick = ( unsigned portBASE_TYPE ) -1;\r
702 \r
703 /* The xHigherPriorityTaskWoken parameter is not used in this case as this\r
704 function is called from the tick hook anyway.  However the API required it\r
705 to be present. */\r
706 portBASE_TYPE xHigherPriorityTaskWoken = pdTRUE;\r
707 portBASE_TYPE xMargin;\r
708 \r
709         if( configTIMER_TASK_PRIORITY != ( configMAX_PRIORITIES - 1 ) )\r
710         {\r
711                 /* The timer service task is not the highest priority task, so it cannot\r
712                 be assumed that timings will be exact.  Timers should never call their\r
713                 callback before their expiry time, but a margin is permissible for calling\r
714                 their callback after their expiry time.  If exact timing is required then\r
715                 configTIMER_TASK_PRIORITY must be set to ensure the timer service task\r
716                 is the highest priority task in the system. */\r
717                 xMargin = 5;\r
718         }\r
719         else\r
720         {\r
721                 xMargin = 0;\r
722         }\r
723         \r
724         uxTick++;\r
725 \r
726         if( uxTick == 0 )\r
727         {\r
728                 /* The timers will have been created, but not started.  Start them\r
729                 now by setting their period. */\r
730                 ucISRAutoReloadTimerCounter = 0;\r
731                 ucISROneShotTimerCounter = 0;\r
732                 xTimerChangePeriodFromISR( xISRAutoReloadTimer, xBasePeriod, &xHigherPriorityTaskWoken );\r
733                 xTimerChangePeriodFromISR( xISROneShotTimer, xBasePeriod, &xHigherPriorityTaskWoken );\r
734         }\r
735         else if( uxTick == xBasePeriod )\r
736         {\r
737                 /* Neither timer should have expired yet. */\r
738                 if( ( ucISRAutoReloadTimerCounter != 0 ) || ( ucISROneShotTimerCounter != 0 ) )\r
739                 {\r
740                         xTestStatus = pdFAIL;\r
741                         configASSERT( xTestStatus );\r
742                 }\r
743         }\r
744         else if( uxTick == ( xBasePeriod + xMargin ) )\r
745         {\r
746                 /* Both timers should now have expired once.  The auto reload timer will\r
747                 still be active, but the one shot timer should now have stopped. */\r
748                 if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )\r
749                 {\r
750                         xTestStatus = pdFAIL;\r
751                         configASSERT( xTestStatus );\r
752                 }\r
753         }\r
754         else if( uxTick == ( 2 * xBasePeriod ) )\r
755         {\r
756                 /* The auto reload timer will still be active, but the one shot timer\r
757                 should now have stopped - however, at this time neither of the timers\r
758                 should have expired again since the last test. */\r
759                 if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )\r
760                 {\r
761                         xTestStatus = pdFAIL;\r
762                         configASSERT( xTestStatus );\r
763                 }               \r
764         }\r
765         else if( uxTick == ( ( 2 * xBasePeriod ) + xMargin ) )\r
766         {\r
767                 /* The auto reload timer will still be active, but the one shot timer\r
768                 should now have stopped.  At this time the auto reload timer should have\r
769                 expired again, but the one shot timer count should not have changed. */\r
770                 if( ucISRAutoReloadTimerCounter != 2 )\r
771                 {\r
772                         xTestStatus = pdFAIL;\r
773                         configASSERT( xTestStatus );\r
774                 }\r
775                 \r
776                 if( ucISROneShotTimerCounter != 1 )\r
777                 {\r
778                         xTestStatus = pdFAIL;\r
779                         configASSERT( xTestStatus );\r
780                 }\r
781         }\r
782         else if( uxTick == ( ( 2 * xBasePeriod ) + ( xBasePeriod >> ( portTickType ) 2U ) ) )\r
783         {\r
784                 /* The auto reload timer will still be active, but the one shot timer\r
785                 should now have stopped.  Again though, at this time, neither timer call\r
786                 back should have been called since the last test. */\r
787                 if( ucISRAutoReloadTimerCounter != 2 )\r
788                 {\r
789                         xTestStatus = pdFAIL;\r
790                         configASSERT( xTestStatus );\r
791                 }\r
792                 \r
793                 if( ucISROneShotTimerCounter != 1 )\r
794                 {\r
795                         xTestStatus = pdFAIL;\r
796                         configASSERT( xTestStatus );\r
797                 }\r
798         }       \r
799         else if( uxTick == ( 3 * xBasePeriod ) )\r
800         {\r
801                 /* Start the one shot timer again. */\r
802                 xTimerStartFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );\r
803         }\r
804         else if( uxTick == ( ( 3 * xBasePeriod ) + xMargin ) )\r
805         {\r
806                 /* The auto reload timer and one shot timer will be active.  At\r
807                 this time the auto reload timer should have     expired again, but the one\r
808                 shot timer count should not have changed yet. */\r
809                 if( ucISRAutoReloadTimerCounter != 3 )\r
810                 {\r
811                         xTestStatus = pdFAIL;\r
812                         configASSERT( xTestStatus );\r
813                 }\r
814                 \r
815                 if( ucISROneShotTimerCounter != 1 )\r
816                 {\r
817                         xTestStatus = pdFAIL;\r
818                         configASSERT( xTestStatus );\r
819                 }\r
820                 \r
821                 /* Now stop the auto reload timer.  The one shot timer was started\r
822                 a few ticks ago. */\r
823                 xTimerStopFromISR( xISRAutoReloadTimer, &xHigherPriorityTaskWoken );\r
824         }       \r
825         else if( uxTick == ( 4 * xBasePeriod ) )\r
826         {\r
827                 /* The auto reload timer is now stopped, and the one shot timer is\r
828                 active, but at this time neither timer should have expired since the\r
829                 last test. */\r
830                 if( ucISRAutoReloadTimerCounter != 3 )\r
831                 {\r
832                         xTestStatus = pdFAIL;\r
833                         configASSERT( xTestStatus );\r
834                 }\r
835                 \r
836                 if( ucISROneShotTimerCounter != 1 )\r
837                 {\r
838                         xTestStatus = pdFAIL;\r
839                         configASSERT( xTestStatus );\r
840                 }\r
841         }       \r
842         else if( uxTick == ( ( 4 * xBasePeriod ) + xMargin ) )\r
843         {\r
844                 /* The auto reload timer is now stopped, and the one shot timer is\r
845                 active.  The one shot timer should have expired again, but the auto\r
846                 reload timer should not have executed its callback. */\r
847                 if( ucISRAutoReloadTimerCounter != 3 )\r
848                 {\r
849                         xTestStatus = pdFAIL;\r
850                         configASSERT( xTestStatus );\r
851                 }\r
852                 \r
853                 if( ucISROneShotTimerCounter != 2 )\r
854                 {\r
855                         xTestStatus = pdFAIL;\r
856                         configASSERT( xTestStatus );\r
857                 }\r
858         }       \r
859         else if( uxTick == ( ( 8 * xBasePeriod ) + xMargin ) )\r
860         {\r
861                 /* The auto reload timer is now stopped, and the one shot timer has\r
862                 already expired and then stopped itself.  Both callback counters should\r
863                 not have incremented since the last test. */\r
864                 if( ucISRAutoReloadTimerCounter != 3 )\r
865                 {\r
866                         xTestStatus = pdFAIL;\r
867                         configASSERT( xTestStatus );\r
868                 }\r
869                 \r
870                 if( ucISROneShotTimerCounter != 2 )\r
871                 {\r
872                         xTestStatus = pdFAIL;\r
873                         configASSERT( xTestStatus );\r
874                 }\r
875                 \r
876                 /* Now reset the one shot timer. */\r
877                 xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );\r
878         }       \r
879         else if( uxTick == ( 9 * xBasePeriod ) )\r
880         {\r
881                 /* Only the one shot timer should be running, but it should not have\r
882                 expired since the last test.  Check the callback counters have not\r
883                 incremented, then reset the one shot timer again. */\r
884                 if( ucISRAutoReloadTimerCounter != 3 )\r
885                 {\r
886                         xTestStatus = pdFAIL;\r
887                         configASSERT( xTestStatus );\r
888                 }\r
889                 \r
890                 if( ucISROneShotTimerCounter != 2 )\r
891                 {\r
892                         xTestStatus = pdFAIL;\r
893                         configASSERT( xTestStatus );\r
894                 }\r
895                 \r
896                 xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );\r
897         }       \r
898         else if( uxTick == ( 10 * xBasePeriod ) )\r
899         {\r
900                 /* Only the one shot timer should be running, but it should not have\r
901                 expired since the last test.  Check the callback counters have not\r
902                 incremented, then reset the one shot timer again. */\r
903                 if( ucISRAutoReloadTimerCounter != 3 )\r
904                 {\r
905                         xTestStatus = pdFAIL;\r
906                         configASSERT( xTestStatus );\r
907                 }\r
908                 \r
909                 if( ucISROneShotTimerCounter != 2 )\r
910                 {\r
911                         xTestStatus = pdFAIL;\r
912                         configASSERT( xTestStatus );\r
913                 }\r
914                 \r
915                 xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );\r
916         }\r
917         else if( uxTick == ( 11 * xBasePeriod ) )\r
918         {\r
919                 /* Only the one shot timer should be running, but it should not have\r
920                 expired since the last test.  Check the callback counters have not\r
921                 incremented, then reset the one shot timer once again. */\r
922                 if( ucISRAutoReloadTimerCounter != 3 )\r
923                 {\r
924                         xTestStatus = pdFAIL;\r
925                         configASSERT( xTestStatus );\r
926                 }\r
927                 \r
928                 if( ucISROneShotTimerCounter != 2 )\r
929                 {\r
930                         xTestStatus = pdFAIL;\r
931                         configASSERT( xTestStatus );\r
932                 }\r
933                 \r
934                 xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );\r
935         }       \r
936         else if( uxTick == ( ( 12 * xBasePeriod ) + xMargin ) )\r
937         {\r
938                 /* Only the one shot timer should have been running and this time it\r
939                 should have     expired.  Check its callback count has been incremented.\r
940                 The auto reload timer is still not running so should still have the same\r
941                 count value.  This time the one shot timer is not reset so should not\r
942                 restart from its expiry period again. */\r
943                 if( ucISRAutoReloadTimerCounter != 3 )\r
944                 {\r
945                         xTestStatus = pdFAIL;\r
946                         configASSERT( xTestStatus );\r
947                 }\r
948                 \r
949                 if( ucISROneShotTimerCounter != 3 )\r
950                 {\r
951                         xTestStatus = pdFAIL;\r
952                         configASSERT( xTestStatus );\r
953                 }\r
954         }\r
955         else if( uxTick == ( 15 * xBasePeriod ) )\r
956         {\r
957                 /* Neither timer should be running now.  Check neither callback count\r
958                 has incremented, then go back to the start to run these tests all\r
959                 over again. */\r
960                 if( ucISRAutoReloadTimerCounter != 3 )\r
961                 {\r
962                         xTestStatus = pdFAIL;\r
963                         configASSERT( xTestStatus );\r
964                 }\r
965                 \r
966                 if( ucISROneShotTimerCounter != 3 )\r
967                 {\r
968                         xTestStatus = pdFAIL;\r
969                         configASSERT( xTestStatus );\r
970                 }\r
971                 \r
972                 uxTick = ( unsigned portBASE_TYPE ) -1;\r
973         }       \r
974 }\r
975 /*-----------------------------------------------------------*/\r
976 \r
977 /*** Timer callback functions are defined below here. ***/\r
978 \r
979 static void prvAutoReloadTimerCallback( xTimerHandle pxExpiredTimer )\r
980 {\r
981 portBASE_TYPE xTimerID;\r
982 \r
983         xTimerID = ( portBASE_TYPE ) pvTimerGetTimerID( pxExpiredTimer );\r
984         if( xTimerID <= ( configTIMER_QUEUE_LENGTH + 1 ) )\r
985         {\r
986                 ( ucAutoReloadTimerCounters[ xTimerID ] )++;\r
987         }\r
988         else\r
989         {\r
990                 /* The timer ID appears to be unexpected (invalid). */\r
991                 xTestStatus = pdFAIL;\r
992                 configASSERT( xTestStatus );\r
993         }\r
994 }\r
995 /*-----------------------------------------------------------*/\r
996 \r
997 static void prvOneShotTimerCallback( xTimerHandle pxExpiredTimer )\r
998 {\r
999         /* The parameter is not used in this case as only one timer uses this\r
1000         callback function. */\r
1001         ( void ) pxExpiredTimer;\r
1002 \r
1003         ucOneShotTimerCounter++;\r
1004 }\r
1005 /*-----------------------------------------------------------*/\r
1006 \r
1007 static void prvISRAutoReloadTimerCallback( xTimerHandle pxExpiredTimer )\r
1008 {\r
1009         /* The parameter is not used in this case as only one timer uses this\r
1010         callback function. */\r
1011         ( void ) pxExpiredTimer;\r
1012 \r
1013         ucISRAutoReloadTimerCounter++;\r
1014 }\r
1015 /*-----------------------------------------------------------*/\r
1016 \r
1017 static void prvISROneShotTimerCallback( xTimerHandle pxExpiredTimer )\r
1018 {\r
1019         /* The parameter is not used in this case as only one timer uses this\r
1020         callback function. */\r
1021         ( void ) pxExpiredTimer;\r
1022 \r
1023         ucISROneShotTimerCounter++;\r
1024 }\r
1025 /*-----------------------------------------------------------*/\r
1026 \r
1027 \r
1028 \r
1029 \r