2 FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\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
13 * then take a look at the FreeRTOS books - available as PDF or paperback *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\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
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\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
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
56 * Tests the behaviour of timers. Some timers are created before the scheduler
\r
57 * is started, and some after.
\r
60 /* Standard includes. */
\r
63 /* Scheduler include files. */
\r
64 #include "FreeRTOS.h"
\r
68 /* Demo program include files. */
\r
69 #include "TimerDemo.h"
\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
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
79 /*-----------------------------------------------------------*/
\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
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
105 /*-----------------------------------------------------------*/
\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
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
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
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
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
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
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
143 /*-----------------------------------------------------------*/
\r
145 void vStartTimerDemoTask( portTickType xBasePeriodIn )
\r
147 /* Store the period from which all the timer periods will be generated from
\r
149 xBasePeriod = xBasePeriodIn;
\r
151 /* Create a set of timers for use by this demo/test. */
\r
152 prvTest1_CreateTimersWithoutSchedulerRunning();
\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
161 xTaskCreate( prvTimerTestTask, ( signed portCHAR * ) "Tmr Tst", configMINIMAL_STACK_SIZE, NULL, configTIMER_TASK_PRIORITY - 1, NULL );
\r
164 /*-----------------------------------------------------------*/
\r
166 static void prvTimerTestTask( void *pvParameters )
\r
168 ( void ) pvParameters;
\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
177 if( xOneShotTimer == NULL )
\r
179 xTestStatus = pdFAIL;
\r
180 configASSERT( xTestStatus );
\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
190 /* Check the auto reload timers expire at the expected/correct rates. */
\r
191 prvTest3_CheckAutoReloadExpireRates();
\r
193 /* Check the auto reload timers can be stopped correctly, and correctly
\r
194 report their state. */
\r
195 prvTest4_CheckAutoReloadTimersCanBeStopped();
\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
201 /* Check timer reset behaviour. */
\r
202 prvTest6_CheckAutoReloadResetBehaviour();
\r
204 /* Start the timers again to restart all the tests over again. */
\r
205 prvResetStartConditionsForNextIteration();
\r
208 /*-----------------------------------------------------------*/
\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
214 static unsigned long ulLastLoopCounter = 0UL;
\r
215 portTickType xMaxBlockTimeUsedByTheseTests, xLoopCounterIncrementTimeMax;
\r
216 static portTickType xIterationsWithoutCounterIncrement = ( portTickType ) 0, xLastCycleFrequency;
\r
218 if( xLastCycleFrequency != xCycleFrequency )
\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
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
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
237 xIterationsWithoutCounterIncrement++;
\r
238 if( xIterationsWithoutCounterIncrement > xLoopCounterIncrementTimeMax )
\r
240 /* The tests appear to be no longer running (stalled). */
\r
241 xTestStatus = pdFAIL;
\r
242 configASSERT( xTestStatus );
\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
252 ulLastLoopCounter = ulLoopCounter;
\r
254 /* Errors detected in the task itself will have latched xTestStatus
\r
257 return xTestStatus;
\r
259 /*-----------------------------------------------------------*/
\r
261 static void prvTest1_CreateTimersWithoutSchedulerRunning( void )
\r
263 portBASE_TYPE xTimer;
\r
265 for( xTimer = 0; xTimer < configTIMER_QUEUE_LENGTH; xTimer++ )
\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
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
277 if( xAutoReloadTimers[ xTimer ] == NULL )
\r
279 xTestStatus = pdFAIL;
\r
280 configASSERT( xTestStatus );
\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
288 if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) != pdPASS )
\r
290 xTestStatus = pdFAIL;
\r
291 configASSERT( xTestStatus );
\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
305 if( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] == NULL )
\r
307 xTestStatus = pdFAIL;
\r
308 configASSERT( xTestStatus );
\r
312 if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) == pdPASS )
\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
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
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
335 if( ( xISRAutoReloadTimer == NULL ) || ( xISROneShotTimer == NULL ) )
\r
337 xTestStatus = pdFAIL;
\r
338 configASSERT( xTestStatus );
\r
341 /*-----------------------------------------------------------*/
\r
343 static void prvTest2_CheckTaskAndTimersInitialState( void )
\r
345 unsigned char ucTimer;
\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
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
354 for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
356 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
358 xTestStatus = pdFAIL;
\r
359 configASSERT( xTestStatus );
\r
363 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] ) != pdFALSE )
\r
365 xTestStatus = pdFAIL;
\r
366 configASSERT( xTestStatus );
\r
369 /*-----------------------------------------------------------*/
\r
371 static void prvTest3_CheckAutoReloadExpireRates( void )
\r
373 unsigned char ucMaxAllowableValue, ucMinAllowableValue, ucTimer;
\r
374 portTickType xBlockPeriod, xTimerPeriod, xExpectedNumber;
\r
376 /* Check the auto reload timers expire at the expected rates. */
\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
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
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
393 ucMaxAllowableValue = ( ( unsigned char ) xExpectedNumber ) ;
\r
394 ucMinAllowableValue = ( ( unsigned char ) xExpectedNumber - ( unsigned char ) 1 );
\r
396 if( ( ucAutoReloadTimerCounters[ ucTimer ] < ucMinAllowableValue ) ||
\r
397 ( ucAutoReloadTimerCounters[ ucTimer ] > ucMaxAllowableValue )
\r
400 xTestStatus = pdFAIL;
\r
401 configASSERT( xTestStatus );
\r
405 if( xTestStatus == pdPASS )
\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
412 /*-----------------------------------------------------------*/
\r
414 static void prvTest4_CheckAutoReloadTimersCanBeStopped( void )
\r
416 unsigned char ucTimer;
\r
418 /* Check the auto reload timers can be stopped correctly, and correctly
\r
419 report their state. */
\r
421 /* Stop all the active timers. */
\r
422 for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
424 /* The timer has not been stopped yet! */
\r
425 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
427 xTestStatus = pdFAIL;
\r
428 configASSERT( xTestStatus );
\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
436 /* The timer should now be inactive. */
\r
437 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )
\r
439 xTestStatus = pdFAIL;
\r
440 configASSERT( xTestStatus );
\r
444 taskENTER_CRITICAL();
\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
452 xTestStatus = pdFAIL;
\r
453 configASSERT( xTestStatus );
\r
456 /* Clear the timer callback count. */
\r
457 memset( ( void * ) ucAutoReloadTimerCounters, 0, sizeof( ucAutoReloadTimerCounters ) );
\r
459 taskEXIT_CRITICAL();
\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
466 if( ucAutoReloadTimerCounters[ ucTimer ] != ( unsigned char ) 0 )
\r
468 xTestStatus = pdFAIL;
\r
469 configASSERT( xTestStatus );
\r
473 if( xTestStatus == pdPASS )
\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
480 /*-----------------------------------------------------------*/
\r
482 static void prvTest5_CheckBasicOneShotTimerBehaviour( void )
\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
487 /* The one shot timer should not be active yet. */
\r
488 if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )
\r
490 xTestStatus = pdFAIL;
\r
491 configASSERT( xTestStatus );
\r
494 if( ucOneShotTimerCounter != ( unsigned char ) 0 )
\r
496 xTestStatus = pdFAIL;
\r
497 configASSERT( xTestStatus );
\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
504 xTestStatus = pdFAIL;
\r
505 configASSERT( xTestStatus );
\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
511 vTaskDelay( tmrdemoONE_SHOT_TIMER_PERIOD * ( portTickType ) 3 );
\r
513 if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )
\r
515 xTestStatus = pdFAIL;
\r
516 configASSERT( xTestStatus );
\r
519 if( ucOneShotTimerCounter != ( unsigned char ) 1 )
\r
521 xTestStatus = pdFAIL;
\r
522 configASSERT( xTestStatus );
\r
526 /* Reset the one shot timer callback count. */
\r
527 ucOneShotTimerCounter = ( unsigned char ) 0;
\r
530 if( xTestStatus == pdPASS )
\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
537 /*-----------------------------------------------------------*/
\r
539 static void prvTest6_CheckAutoReloadResetBehaviour( void )
\r
541 unsigned char ucTimer;
\r
543 /* Check timer reset behaviour. */
\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
549 xTestStatus = pdFAIL;
\r
550 configASSERT( xTestStatus );
\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
558 xTestStatus = pdFAIL;
\r
559 configASSERT( xTestStatus );
\r
562 for( ucTimer = 0; ucTimer < trmdemoNUM_TIMER_RESETS; ucTimer++ )
\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
569 /* Check both running timers are still active, but have not called their
\r
570 callback functions. */
\r
571 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
573 xTestStatus = pdFAIL;
\r
574 configASSERT( xTestStatus );
\r
577 if( ucOneShotTimerCounter != ( unsigned char ) 0 )
\r
579 xTestStatus = pdFAIL;
\r
580 configASSERT( xTestStatus );
\r
583 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
585 xTestStatus = pdFAIL;
\r
586 configASSERT( xTestStatus );
\r
589 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] != ( unsigned char ) 0 )
\r
591 xTestStatus = pdFAIL;
\r
592 configASSERT( xTestStatus );
\r
595 /* Reset both running timers. */
\r
596 xTimerReset( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
597 xTimerReset( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
599 if( xTestStatus == pdPASS )
\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
607 /* Finally delay long enough for both running timers to expire. */
\r
608 vTaskDelay( ( ( portTickType ) configTIMER_QUEUE_LENGTH ) * xBasePeriod );
\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
614 xTestStatus = pdFAIL;
\r
615 configASSERT( xTestStatus );
\r
618 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] == 0 )
\r
620 xTestStatus = pdFAIL;
\r
621 configASSERT( xTestStatus );
\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
628 xTestStatus = pdFAIL;
\r
629 configASSERT( xTestStatus );
\r
632 if( xTimerIsTimerActive( xOneShotTimer ) == pdTRUE )
\r
634 xTestStatus = pdFAIL;
\r
635 configASSERT( xTestStatus );
\r
638 /* Stop the auto reload timer again. */
\r
639 xTimerStop( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
641 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) != pdFALSE )
\r
643 xTestStatus = pdFAIL;
\r
644 configASSERT( xTestStatus );
\r
647 /* Clear the timer callback counts, ready for another iteration of these
\r
649 ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] = ( unsigned char ) 0;
\r
650 ucOneShotTimerCounter = ( unsigned char ) 0;
\r
652 if( xTestStatus == pdPASS )
\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
659 /*-----------------------------------------------------------*/
\r
661 static void prvResetStartConditionsForNextIteration( void )
\r
663 unsigned char ucTimer;
\r
665 /* Start the timers again to start all the tests over again. */
\r
667 /* Start the timers again. */
\r
668 for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
670 /* The timer has not been started yet! */
\r
671 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )
\r
673 xTestStatus = pdFAIL;
\r
674 configASSERT( xTestStatus );
\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
680 xTimerStart( xAutoReloadTimers[ ucTimer ], tmrdemoDONT_BLOCK );
\r
682 /* The timer should now be active. */
\r
683 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
685 xTestStatus = pdFAIL;
\r
686 configASSERT( xTestStatus );
\r
690 if( xTestStatus == pdPASS )
\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
697 /*-----------------------------------------------------------*/
\r
699 void vTimerPeriodicISRTests( void )
\r
701 static unsigned portBASE_TYPE uxTick = ( unsigned portBASE_TYPE ) -1;
\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
706 portBASE_TYPE xHigherPriorityTaskWoken = pdTRUE;
\r
707 portBASE_TYPE xMargin;
\r
709 if( configTIMER_TASK_PRIORITY != ( configMAX_PRIORITIES - 1 ) )
\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
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
735 else if( uxTick == xBasePeriod )
\r
737 /* Neither timer should have expired yet. */
\r
738 if( ( ucISRAutoReloadTimerCounter != 0 ) || ( ucISROneShotTimerCounter != 0 ) )
\r
740 xTestStatus = pdFAIL;
\r
741 configASSERT( xTestStatus );
\r
744 else if( uxTick == ( xBasePeriod + xMargin ) )
\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
750 xTestStatus = pdFAIL;
\r
751 configASSERT( xTestStatus );
\r
754 else if( uxTick == ( 2 * xBasePeriod ) )
\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
761 xTestStatus = pdFAIL;
\r
762 configASSERT( xTestStatus );
\r
765 else if( uxTick == ( ( 2 * xBasePeriod ) + xMargin ) )
\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
772 xTestStatus = pdFAIL;
\r
773 configASSERT( xTestStatus );
\r
776 if( ucISROneShotTimerCounter != 1 )
\r
778 xTestStatus = pdFAIL;
\r
779 configASSERT( xTestStatus );
\r
782 else if( uxTick == ( ( 2 * xBasePeriod ) + ( xBasePeriod >> ( portTickType ) 2U ) ) )
\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
789 xTestStatus = pdFAIL;
\r
790 configASSERT( xTestStatus );
\r
793 if( ucISROneShotTimerCounter != 1 )
\r
795 xTestStatus = pdFAIL;
\r
796 configASSERT( xTestStatus );
\r
799 else if( uxTick == ( 3 * xBasePeriod ) )
\r
801 /* Start the one shot timer again. */
\r
802 xTimerStartFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );
\r
804 else if( uxTick == ( ( 3 * xBasePeriod ) + xMargin ) )
\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
811 xTestStatus = pdFAIL;
\r
812 configASSERT( xTestStatus );
\r
815 if( ucISROneShotTimerCounter != 1 )
\r
817 xTestStatus = pdFAIL;
\r
818 configASSERT( xTestStatus );
\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
825 else if( uxTick == ( 4 * xBasePeriod ) )
\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
830 if( ucISRAutoReloadTimerCounter != 3 )
\r
832 xTestStatus = pdFAIL;
\r
833 configASSERT( xTestStatus );
\r
836 if( ucISROneShotTimerCounter != 1 )
\r
838 xTestStatus = pdFAIL;
\r
839 configASSERT( xTestStatus );
\r
842 else if( uxTick == ( ( 4 * xBasePeriod ) + xMargin ) )
\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
849 xTestStatus = pdFAIL;
\r
850 configASSERT( xTestStatus );
\r
853 if( ucISROneShotTimerCounter != 2 )
\r
855 xTestStatus = pdFAIL;
\r
856 configASSERT( xTestStatus );
\r
859 else if( uxTick == ( ( 8 * xBasePeriod ) + xMargin ) )
\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
866 xTestStatus = pdFAIL;
\r
867 configASSERT( xTestStatus );
\r
870 if( ucISROneShotTimerCounter != 2 )
\r
872 xTestStatus = pdFAIL;
\r
873 configASSERT( xTestStatus );
\r
876 /* Now reset the one shot timer. */
\r
877 xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );
\r
879 else if( uxTick == ( 9 * xBasePeriod ) )
\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
886 xTestStatus = pdFAIL;
\r
887 configASSERT( xTestStatus );
\r
890 if( ucISROneShotTimerCounter != 2 )
\r
892 xTestStatus = pdFAIL;
\r
893 configASSERT( xTestStatus );
\r
896 xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );
\r
898 else if( uxTick == ( 10 * xBasePeriod ) )
\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
905 xTestStatus = pdFAIL;
\r
906 configASSERT( xTestStatus );
\r
909 if( ucISROneShotTimerCounter != 2 )
\r
911 xTestStatus = pdFAIL;
\r
912 configASSERT( xTestStatus );
\r
915 xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );
\r
917 else if( uxTick == ( 11 * xBasePeriod ) )
\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
924 xTestStatus = pdFAIL;
\r
925 configASSERT( xTestStatus );
\r
928 if( ucISROneShotTimerCounter != 2 )
\r
930 xTestStatus = pdFAIL;
\r
931 configASSERT( xTestStatus );
\r
934 xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );
\r
936 else if( uxTick == ( ( 12 * xBasePeriod ) + xMargin ) )
\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
945 xTestStatus = pdFAIL;
\r
946 configASSERT( xTestStatus );
\r
949 if( ucISROneShotTimerCounter != 3 )
\r
951 xTestStatus = pdFAIL;
\r
952 configASSERT( xTestStatus );
\r
955 else if( uxTick == ( 15 * xBasePeriod ) )
\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
960 if( ucISRAutoReloadTimerCounter != 3 )
\r
962 xTestStatus = pdFAIL;
\r
963 configASSERT( xTestStatus );
\r
966 if( ucISROneShotTimerCounter != 3 )
\r
968 xTestStatus = pdFAIL;
\r
969 configASSERT( xTestStatus );
\r
972 uxTick = ( unsigned portBASE_TYPE ) -1;
\r
975 /*-----------------------------------------------------------*/
\r
977 /*** Timer callback functions are defined below here. ***/
\r
979 static void prvAutoReloadTimerCallback( xTimerHandle pxExpiredTimer )
\r
981 portBASE_TYPE xTimerID;
\r
983 xTimerID = ( portBASE_TYPE ) pvTimerGetTimerID( pxExpiredTimer );
\r
984 if( xTimerID <= ( configTIMER_QUEUE_LENGTH + 1 ) )
\r
986 ( ucAutoReloadTimerCounters[ xTimerID ] )++;
\r
990 /* The timer ID appears to be unexpected (invalid). */
\r
991 xTestStatus = pdFAIL;
\r
992 configASSERT( xTestStatus );
\r
995 /*-----------------------------------------------------------*/
\r
997 static void prvOneShotTimerCallback( xTimerHandle pxExpiredTimer )
\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
1003 ucOneShotTimerCounter++;
\r
1005 /*-----------------------------------------------------------*/
\r
1007 static void prvISRAutoReloadTimerCallback( xTimerHandle pxExpiredTimer )
\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
1013 ucISRAutoReloadTimerCounter++;
\r
1015 /*-----------------------------------------------------------*/
\r
1017 static void prvISROneShotTimerCallback( xTimerHandle pxExpiredTimer )
\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
1023 ucISROneShotTimerCounter++;
\r
1025 /*-----------------------------------------------------------*/
\r