]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/TimerDemo.c
Continue work on the new timer implementation test application. Nearly complete.
[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 hte scheudler\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 autoreload 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 portLONG 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 realod 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 autoreload - 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 portLONG ulLastLoopCounter = 0UL;\r
215 portTickType xMaxBlockTimeUsedByTheseTests, xLoopCounterIncrementTimeMax;\r
216 static portTickType xItterationsWithoutCounterIncrement = ( 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 itterations again. */\r
222                 xItterationsWithoutCounterIncrement = ( portTickType ) 0;\r
223                 xLastCycleFrequency = xCycleFrequency;\r
224         }               \r
225 \r
226         /* Calculate the maximum number of times that it is permittable 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                 xItterationsWithoutCounterIncrement++;\r
238                 if( xItterationsWithoutCounterIncrement > 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                 xItterationsWithoutCounterIncrement = ( 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,                                                         /* Autoreload 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,                                                 /* Autoreload 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 expcted 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 const portBASE_TYPE xMargin = 3;\r
708 \r
709         uxTick++;\r
710 \r
711         if( uxTick == 0 )\r
712         {\r
713                 /* The timers will have been created, but not started.  Start them\r
714                 now by setting their period. */\r
715                 ucISRAutoReloadTimerCounter = 0;\r
716                 ucISROneShotTimerCounter = 0;\r
717                 xTimerChangePeriodFromISR( xISRAutoReloadTimer, xBasePeriod, &xHigherPriorityTaskWoken );\r
718                 xTimerChangePeriodFromISR( xISROneShotTimer, xBasePeriod, &xHigherPriorityTaskWoken );\r
719         }\r
720         else if( uxTick == xBasePeriod )\r
721         {\r
722                 /* Neither timer should have expired yet. */\r
723                 if( ( ucISRAutoReloadTimerCounter != 0 ) || ( ucISROneShotTimerCounter != 0 ) )\r
724                 {\r
725                         xTestStatus = pdFAIL;\r
726                         configASSERT( xTestStatus );\r
727                 }\r
728         }\r
729         else if( uxTick == ( xBasePeriod + xMargin ) )\r
730         {\r
731                 /* Both timers should now have expired once.  The auto reload timer will\r
732                 still be active, but the one shot timer should now have stopped. */\r
733                 if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )\r
734                 {\r
735                         xTestStatus = pdFAIL;\r
736                         configASSERT( xTestStatus );\r
737                 }               \r
738         }\r
739         else if( uxTick == ( 2 * xBasePeriod ) )\r
740         {\r
741                 /* The auto reload timer will still be active, but the one shot timer\r
742                 should now have stopped - however, at this time neither of the timers\r
743                 shoud have expired again since the last test. */\r
744                 if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )\r
745                 {\r
746                         xTestStatus = pdFAIL;\r
747                         configASSERT( xTestStatus );\r
748                 }               \r
749         }\r
750         else if( uxTick == ( ( 2 * xBasePeriod ) + xMargin ) )\r
751         {\r
752                 /* The auto reload timer will still be active, but the one shot timer\r
753                 should now have stopped.  At this time the auto reload timer should have\r
754                 expired again, but the one shot timer count should not have changed. */\r
755                 if( ucISRAutoReloadTimerCounter != 2 )\r
756                 {\r
757                         xTestStatus = pdFAIL;\r
758                         configASSERT( xTestStatus );\r
759                 }\r
760                 \r
761                 if( ucISROneShotTimerCounter != 1 )\r
762                 {\r
763                         xTestStatus = pdFAIL;\r
764                         configASSERT( xTestStatus );\r
765                 }\r
766         }\r
767         else if( uxTick == ( ( 2 * xBasePeriod ) + ( xBasePeriod >> ( portTickType ) 2U ) ) )\r
768         {\r
769                 /* The auto reload timer will still be active, but the one shot timer\r
770                 should now have stopped.  Again though, at this time, neither timer call\r
771                 back should have been called since the last test. */\r
772                 if( ucISRAutoReloadTimerCounter != 2 )\r
773                 {\r
774                         xTestStatus = pdFAIL;\r
775                         configASSERT( xTestStatus );\r
776                 }\r
777                 \r
778                 if( ucISROneShotTimerCounter != 1 )\r
779                 {\r
780                         xTestStatus = pdFAIL;\r
781                         configASSERT( xTestStatus );\r
782                 }               \r
783         }       \r
784         else if( uxTick == ( 3 * xBasePeriod ) )\r
785         {\r
786                 /* Start the one shot timer again. */\r
787                 xTimerStartFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );\r
788         }\r
789         else if( uxTick == ( ( 3 * xBasePeriod ) + xMargin ) )\r
790         {\r
791                 /* The auto reload timer and one shot timer will be active.  At\r
792                 this time the auto reload timer should have     expired again, but the one\r
793                 shot timer count should not have changed yet. */\r
794                 if( ucISRAutoReloadTimerCounter != 3 )\r
795                 {\r
796                         xTestStatus = pdFAIL;\r
797                         configASSERT( xTestStatus );\r
798                 }\r
799                 \r
800                 if( ucISROneShotTimerCounter != 1 )\r
801                 {\r
802                         xTestStatus = pdFAIL;\r
803                         configASSERT( xTestStatus );\r
804                 }\r
805                 \r
806                 /* Now stop the auto realod timer.  The one shot timer was started\r
807                 a few ticks ago. */\r
808                 xTimerStopFromISR( xISRAutoReloadTimer, &xHigherPriorityTaskWoken );\r
809         }       \r
810         else if( uxTick == ( 4 * xBasePeriod ) )\r
811         {\r
812                 /* The auto reload timer is now stopped, and the one shot timer is\r
813                 active, but at this time neither timer should have expired since the\r
814                 last test. */\r
815                 if( ucISRAutoReloadTimerCounter != 3 )\r
816                 {\r
817                         xTestStatus = pdFAIL;\r
818                         configASSERT( xTestStatus );\r
819                 }\r
820                 \r
821                 if( ucISROneShotTimerCounter != 1 )\r
822                 {\r
823                         xTestStatus = pdFAIL;\r
824                         configASSERT( xTestStatus );\r
825                 }\r
826         }       \r
827         else if( uxTick == ( ( 4 * xBasePeriod ) + xMargin ) )\r
828         {\r
829                 /* The auto reload timer is now stopped, and the one shot timer is\r
830                 active.  The one shot timer should have expired again, but the auto\r
831                 reload timer should not have executed its callback. */\r
832                 if( ucISRAutoReloadTimerCounter != 3 )\r
833                 {\r
834                         xTestStatus = pdFAIL;\r
835                         configASSERT( xTestStatus );\r
836                 }\r
837                 \r
838                 if( ucISROneShotTimerCounter != 2 )\r
839                 {\r
840                         xTestStatus = pdFAIL;\r
841                         configASSERT( xTestStatus );\r
842                 }\r
843         }       \r
844         else if( uxTick == ( ( 8 * xBasePeriod ) + xMargin ) )\r
845         {\r
846                 /* The auto reload timer is now stopped, and the one shot timer has\r
847                 already expired and then stopped itself.  Both callback counters should\r
848                 not have incremented since the last test. */\r
849                 if( ucISRAutoReloadTimerCounter != 3 )\r
850                 {\r
851                         xTestStatus = pdFAIL;\r
852                         configASSERT( xTestStatus );\r
853                 }\r
854                 \r
855                 if( ucISROneShotTimerCounter != 2 )\r
856                 {\r
857                         xTestStatus = pdFAIL;\r
858                         configASSERT( xTestStatus );\r
859                 }\r
860                 \r
861                 /* Now reset the one shot timer. */\r
862                 xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );\r
863         }       \r
864         else if( uxTick == ( 9 * xBasePeriod ) )\r
865         {\r
866                 /* Only the one shot timer should be running, but it should not have\r
867                 expired since the last test.  Check the callback counters have not\r
868                 incremented, then reset the one shot timer again. */\r
869                 if( ucISRAutoReloadTimerCounter != 3 )\r
870                 {\r
871                         xTestStatus = pdFAIL;\r
872                         configASSERT( xTestStatus );\r
873                 }\r
874                 \r
875                 if( ucISROneShotTimerCounter != 2 )\r
876                 {\r
877                         xTestStatus = pdFAIL;\r
878                         configASSERT( xTestStatus );\r
879                 }\r
880                 \r
881                 xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );\r
882         }       \r
883         else if( uxTick == ( 10 * xBasePeriod ) )\r
884         {\r
885                 /* Only the one shot timer should be running, but it should not have\r
886                 expired since the last test.  Check the callback counters have not\r
887                 incremented, then reset the one shot timer again. */\r
888                 if( ucISRAutoReloadTimerCounter != 3 )\r
889                 {\r
890                         xTestStatus = pdFAIL;\r
891                         configASSERT( xTestStatus );\r
892                 }\r
893                 \r
894                 if( ucISROneShotTimerCounter != 2 )\r
895                 {\r
896                         xTestStatus = pdFAIL;\r
897                         configASSERT( xTestStatus );\r
898                 }\r
899                 \r
900                 xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );\r
901         }\r
902         else if( uxTick == ( 11 * xBasePeriod ) )\r
903         {\r
904                 /* Only the one shot timer should be running, but it should not have\r
905                 expired since the last test.  Check the callback counters have not\r
906                 incremented, then reset the one shot timer once again. */\r
907                 if( ucISRAutoReloadTimerCounter != 3 )\r
908                 {\r
909                         xTestStatus = pdFAIL;\r
910                         configASSERT( xTestStatus );\r
911                 }\r
912                 \r
913                 if( ucISROneShotTimerCounter != 2 )\r
914                 {\r
915                         xTestStatus = pdFAIL;\r
916                         configASSERT( xTestStatus );\r
917                 }\r
918                 \r
919                 xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );\r
920         }       \r
921         else if( uxTick == ( ( 12 * xBasePeriod ) + xMargin ) )\r
922         {\r
923                 /* Only the one shot timer should have been running and this time it\r
924                 should have     expired.  Check its callback count has been incremented.\r
925                 The auto realod timer is still not running so should still have the same\r
926                 count value.  This time the one shot timer is not reset so should not\r
927                 restart from its expiry period again. */\r
928                 if( ucISRAutoReloadTimerCounter != 3 )\r
929                 {\r
930                         xTestStatus = pdFAIL;\r
931                         configASSERT( xTestStatus );\r
932                 }\r
933                 \r
934                 if( ucISROneShotTimerCounter != 3 )\r
935                 {\r
936                         xTestStatus = pdFAIL;\r
937                         configASSERT( xTestStatus );\r
938                 }\r
939         }\r
940         else if( uxTick == ( 15 * xBasePeriod ) )\r
941         {\r
942                 /* Neither timer should be running now.  Check neither callback count\r
943                 has incremented, then go back to the start to run these tests all\r
944                 over again. */\r
945                 if( ucISRAutoReloadTimerCounter != 3 )\r
946                 {\r
947                         xTestStatus = pdFAIL;\r
948                         configASSERT( xTestStatus );\r
949                 }\r
950                 \r
951                 if( ucISROneShotTimerCounter != 3 )\r
952                 {\r
953                         xTestStatus = pdFAIL;\r
954                         configASSERT( xTestStatus );\r
955                 }\r
956                 \r
957                 uxTick = ( unsigned portBASE_TYPE ) -1;\r
958         }       \r
959 }\r
960 /*-----------------------------------------------------------*/\r
961 \r
962 /*** Timer callback functions are defined below here. ***/\r
963 \r
964 static void prvAutoReloadTimerCallback( xTimerHandle pxExpiredTimer )\r
965 {\r
966 portBASE_TYPE xTimerID;\r
967 \r
968         xTimerID = ( portBASE_TYPE ) pvTimerGetTimerID( pxExpiredTimer );\r
969         if( xTimerID <= ( configTIMER_QUEUE_LENGTH + 1 ) )\r
970         {\r
971                 ( ucAutoReloadTimerCounters[ xTimerID ] )++;\r
972         }\r
973         else\r
974         {\r
975                 /* The timer ID appears to be unexpected (invalid). */\r
976                 xTestStatus = pdFAIL;\r
977                 configASSERT( xTestStatus );\r
978         }\r
979 }\r
980 /*-----------------------------------------------------------*/\r
981 \r
982 static void prvOneShotTimerCallback( xTimerHandle pxExpiredTimer )\r
983 {\r
984         /* The parameter is not used in this case as only one timer uses this\r
985         callback function. */\r
986         ( void ) pxExpiredTimer;\r
987 \r
988         ucOneShotTimerCounter++;\r
989 }\r
990 /*-----------------------------------------------------------*/\r
991 \r
992 static void prvISRAutoReloadTimerCallback( xTimerHandle pxExpiredTimer )\r
993 {\r
994         /* The parameter is not used in this case as only one timer uses this\r
995         callback function. */\r
996         ( void ) pxExpiredTimer;\r
997 \r
998         ucISRAutoReloadTimerCounter++;\r
999 }\r
1000 /*-----------------------------------------------------------*/\r
1001 \r
1002 static void prvISROneShotTimerCallback( xTimerHandle pxExpiredTimer )\r
1003 {\r
1004         /* The parameter is not used in this case as only one timer uses this\r
1005         callback function. */\r
1006         ( void ) pxExpiredTimer;\r
1007 \r
1008         ucISROneShotTimerCounter++;\r
1009 }\r
1010 /*-----------------------------------------------------------*/\r
1011 \r
1012 \r
1013 \r
1014 \r