2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
71 * Tests the behaviour of timers. Some timers are created before the scheduler
\r
72 * is started, and some after.
\r
75 /* Standard includes. */
\r
78 /* Scheduler include files. */
\r
79 #include "FreeRTOS.h"
\r
83 /* Demo program include files. */
\r
84 #include "TimerDemo.h"
\r
86 #if ( configTIMER_TASK_PRIORITY < 1 )
\r
87 #error configTIMER_TASK_PRIORITY must be set to at least 1 for this test/demo to function correctly.
\r
90 #define tmrdemoDONT_BLOCK ( ( portTickType ) 0 )
\r
91 #define tmrdemoONE_SHOT_TIMER_PERIOD ( xBasePeriod * ( portTickType ) 3 )
\r
92 #define trmdemoNUM_TIMER_RESETS ( ( unsigned char ) 10 )
\r
94 /*-----------------------------------------------------------*/
\r
96 /* The callback functions used by the timers. These each increment a counter
\r
97 to indicate which timer has expired. The auto-reload timers that are used by
\r
98 the test task (as opposed to being used from an ISR) all share the same
\r
99 prvAutoReloadTimerCallback() callback function, and use the ID of the
\r
100 pxExpiredTimer parameter passed into that function to know which counter to
\r
101 increment. The other timers all have their own unique callback function and
\r
102 simply increment their counters without using the callback function parameter. */
\r
103 static void prvAutoReloadTimerCallback( xTimerHandle pxExpiredTimer );
\r
104 static void prvOneShotTimerCallback( xTimerHandle pxExpiredTimer );
\r
105 static void prvTimerTestTask( void *pvParameters );
\r
106 static void prvISRAutoReloadTimerCallback( xTimerHandle pxExpiredTimer );
\r
107 static void prvISROneShotTimerCallback( xTimerHandle pxExpiredTimer );
\r
109 /* The test functions used by the timer test task. These manipulate the auto
\r
110 reload and one shot timers in various ways, then delay, then inspect the timers
\r
111 to ensure they have behaved as expected. */
\r
112 static void prvTest1_CreateTimersWithoutSchedulerRunning( void );
\r
113 static void prvTest2_CheckTaskAndTimersInitialState( void );
\r
114 static void prvTest3_CheckAutoReloadExpireRates( void );
\r
115 static void prvTest4_CheckAutoReloadTimersCanBeStopped( void );
\r
116 static void prvTest5_CheckBasicOneShotTimerBehaviour( void );
\r
117 static void prvTest6_CheckAutoReloadResetBehaviour( void );
\r
118 static void prvResetStartConditionsForNextIteration( void );
\r
120 /*-----------------------------------------------------------*/
\r
122 /* Flag that will be latched to pdFAIL should any unexpected behaviour be
\r
123 detected in any of the demo tests. */
\r
124 static volatile portBASE_TYPE xTestStatus = pdPASS;
\r
126 /* Counter that is incremented on each cycle of a test. This is used to
\r
127 detect a stalled task - a test that is no longer running. */
\r
128 static volatile unsigned long ulLoopCounter = 0;
\r
130 /* A set of auto reload timers - each of which use the same callback function.
\r
131 The callback function uses the timer ID to index into, and then increment, a
\r
132 counter in the ucAutoReloadTimerCounters[] array. The auto reload timers
\r
133 referenced from xAutoReloadTimers[] are used by the prvTimerTestTask task. */
\r
134 static xTimerHandle xAutoReloadTimers[ configTIMER_QUEUE_LENGTH + 1 ] = { 0 };
\r
135 static unsigned char ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH + 1 ] = { 0 };
\r
137 /* The one shot timer is configured to use a callback function that increments
\r
138 ucOneShotTimerCounter each time it gets called. */
\r
139 static xTimerHandle xOneShotTimer = NULL;
\r
140 static unsigned char ucOneShotTimerCounter = ( unsigned char ) 0;
\r
142 /* The ISR reload timer is controlled from the tick hook to exercise the timer
\r
143 API functions that can be used from an ISR. It is configured to increment
\r
144 ucISRReloadTimerCounter each time its callback function is executed. */
\r
145 static xTimerHandle xISRAutoReloadTimer = NULL;
\r
146 static unsigned char ucISRAutoReloadTimerCounter = ( unsigned char ) 0;
\r
148 /* The ISR one shot timer is controlled from the tick hook to exercise the timer
\r
149 API functions that can be used from an ISR. It is configured to increment
\r
150 ucISRReloadTimerCounter each time its callback function is executed. */
\r
151 static xTimerHandle xISROneShotTimer = NULL;
\r
152 static unsigned char ucISROneShotTimerCounter = ( unsigned char ) 0;
\r
154 /* The period of all the timers are a multiple of the base period. The base
\r
155 period is configured by the parameter to vStartTimerDemoTask(). */
\r
156 static portTickType xBasePeriod = 0;
\r
158 /*-----------------------------------------------------------*/
\r
160 void vStartTimerDemoTask( portTickType xBasePeriodIn )
\r
162 /* Start with the timer and counter arrays clear - this is only necessary
\r
163 where the compiler does not clear them automatically on start up. */
\r
164 memset( ucAutoReloadTimerCounters, 0x00, sizeof( ucAutoReloadTimerCounters ) );
\r
165 memset( xAutoReloadTimers, 0x00, sizeof( xAutoReloadTimers ) );
\r
167 /* Store the period from which all the timer periods will be generated from
\r
169 xBasePeriod = xBasePeriodIn;
\r
171 /* Create a set of timers for use by this demo/test. */
\r
172 prvTest1_CreateTimersWithoutSchedulerRunning();
\r
174 /* Create the task that will control and monitor the timers. This is
\r
175 created at a lower priority than the timer service task to ensure, as
\r
176 far as it is concerned, commands on timers are actioned immediately
\r
177 (sending a command to the timer service task will unblock the timer service
\r
178 task, which will then preempt this task). */
\r
179 if( xTestStatus != pdFAIL )
\r
181 xTaskCreate( prvTimerTestTask, ( signed portCHAR * ) "Tmr Tst", configMINIMAL_STACK_SIZE, NULL, configTIMER_TASK_PRIORITY - 1, NULL );
\r
184 /*-----------------------------------------------------------*/
\r
186 static void prvTimerTestTask( void *pvParameters )
\r
188 ( void ) pvParameters;
\r
190 /* Create a one-shot timer for use later on in this test. */
\r
191 xOneShotTimer = xTimerCreate( ( const signed char * ) "Oneshot Timer",/* Text name to facilitate debugging. The kernel does not use this itself. */
\r
192 tmrdemoONE_SHOT_TIMER_PERIOD, /* The period for the timer. */
\r
193 pdFALSE, /* Don't auto-reload - hence a one shot timer. */
\r
194 ( void * ) 0, /* The timer identifier. In this case this is not used as the timer has its own callback. */
\r
195 prvOneShotTimerCallback ); /* The callback to be called when the timer expires. */
\r
197 if( xOneShotTimer == NULL )
\r
199 xTestStatus = pdFAIL;
\r
200 configASSERT( xTestStatus );
\r
204 /* Ensure all the timers are in their expected initial state. This
\r
205 depends on the timer service task having a higher priority than this task. */
\r
206 prvTest2_CheckTaskAndTimersInitialState();
\r
210 /* Check the auto reload timers expire at the expected/correct rates. */
\r
211 prvTest3_CheckAutoReloadExpireRates();
\r
213 /* Check the auto reload timers can be stopped correctly, and correctly
\r
214 report their state. */
\r
215 prvTest4_CheckAutoReloadTimersCanBeStopped();
\r
217 /* Check the one shot timer only calls its callback once after it has been
\r
218 started, and that it reports its state correctly. */
\r
219 prvTest5_CheckBasicOneShotTimerBehaviour();
\r
221 /* Check timer reset behaviour. */
\r
222 prvTest6_CheckAutoReloadResetBehaviour();
\r
224 /* Start the timers again to restart all the tests over again. */
\r
225 prvResetStartConditionsForNextIteration();
\r
228 /*-----------------------------------------------------------*/
\r
230 /* This is called to check that the created task is still running and has not
\r
231 detected any errors. */
\r
232 portBASE_TYPE xAreTimerDemoTasksStillRunning( portTickType xCycleFrequency )
\r
234 static unsigned long ulLastLoopCounter = 0UL;
\r
235 portTickType xMaxBlockTimeUsedByTheseTests, xLoopCounterIncrementTimeMax;
\r
236 static portTickType xIterationsWithoutCounterIncrement = ( portTickType ) 0, xLastCycleFrequency;
\r
238 if( xLastCycleFrequency != xCycleFrequency )
\r
240 /* The cycle frequency has probably become much faster due to an error
\r
241 elsewhere. Start counting Iterations again. */
\r
242 xIterationsWithoutCounterIncrement = ( portTickType ) 0;
\r
243 xLastCycleFrequency = xCycleFrequency;
\r
246 /* Calculate the maximum number of times that it is permissible for this
\r
247 function to be called without ulLoopCounter being incremented. This is
\r
248 necessary because the tests in this file block for extended periods, and the
\r
249 block period might be longer than the time between calls to this function. */
\r
250 xMaxBlockTimeUsedByTheseTests = ( ( portTickType ) configTIMER_QUEUE_LENGTH ) * xBasePeriod;
\r
251 xLoopCounterIncrementTimeMax = xMaxBlockTimeUsedByTheseTests / xCycleFrequency;
\r
253 /* If the demo task is still running then we expect the loopcounter to
\r
254 have incremented every xLoopCounterIncrementTimeMax calls. */
\r
255 if( ulLastLoopCounter == ulLoopCounter )
\r
257 xIterationsWithoutCounterIncrement++;
\r
258 if( xIterationsWithoutCounterIncrement > xLoopCounterIncrementTimeMax )
\r
260 /* The tests appear to be no longer running (stalled). */
\r
261 xTestStatus = pdFAIL;
\r
262 configASSERT( xTestStatus );
\r
267 /* ulLoopCounter changed, so the count of times this function was called
\r
268 without a change can be reset to zero. */
\r
269 xIterationsWithoutCounterIncrement = ( portTickType ) 0;
\r
272 ulLastLoopCounter = ulLoopCounter;
\r
274 /* Errors detected in the task itself will have latched xTestStatus
\r
277 return xTestStatus;
\r
279 /*-----------------------------------------------------------*/
\r
281 static void prvTest1_CreateTimersWithoutSchedulerRunning( void )
\r
283 unsigned portBASE_TYPE xTimer;
\r
285 for( xTimer = 0; xTimer < configTIMER_QUEUE_LENGTH; xTimer++ )
\r
287 /* As the timer queue is not yet full, it should be possible to both create
\r
288 and start a timer. These timers are being started before the scheduler has
\r
289 been started, so their block times should get set to zero within the timer
\r
291 xAutoReloadTimers[ xTimer ] = xTimerCreate( ( const signed char * )"FR Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
\r
292 ( ( xTimer + ( portTickType ) 1 ) * xBasePeriod ),/* The period for the timer. The plus 1 ensures a period of zero is not specified. */
\r
293 pdTRUE, /* Auto-reload is set to true. */
\r
294 ( void * ) xTimer, /* An identifier for the timer as all the auto reload timers use the same callback. */
\r
295 prvAutoReloadTimerCallback ); /* The callback to be called when the timer expires. */
\r
297 if( xAutoReloadTimers[ xTimer ] == NULL )
\r
299 xTestStatus = pdFAIL;
\r
300 configASSERT( xTestStatus );
\r
304 /* The scheduler has not yet started, so the block period of
\r
305 portMAX_DELAY should just get set to zero in xTimerStart(). Also,
\r
306 the timer queue is not yet full so xTimerStart() should return
\r
308 if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) != pdPASS )
\r
310 xTestStatus = pdFAIL;
\r
311 configASSERT( xTestStatus );
\r
316 /* The timers queue should now be full, so it should be possible to create
\r
317 another timer, but not possible to start it (the timer queue will not get
\r
318 drained until the scheduler has been started. */
\r
319 xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] = xTimerCreate( ( const signed char * ) "FR Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
\r
320 ( configTIMER_QUEUE_LENGTH * xBasePeriod ), /* The period for the timer. */
\r
321 pdTRUE, /* Auto-reload is set to true. */
\r
322 ( void * ) xTimer, /* An identifier for the timer as all the auto reload timers use the same callback. */
\r
323 prvAutoReloadTimerCallback ); /* The callback executed when the timer expires. */
\r
325 if( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] == NULL )
\r
327 xTestStatus = pdFAIL;
\r
328 configASSERT( xTestStatus );
\r
332 if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) == pdPASS )
\r
334 /* This time it would not be expected that the timer could be
\r
335 started at this point. */
\r
336 xTestStatus = pdFAIL;
\r
337 configASSERT( xTestStatus );
\r
341 /* Create the timers that are used from the tick interrupt to test the timer
\r
342 API functions that can be called from an ISR. */
\r
343 xISRAutoReloadTimer = xTimerCreate( ( const signed char * ) "ISR AR", /* The text name given to the timer. */
\r
344 0xffff, /* The timer is not given a period yet - this will be done from the tick hook, but a period of 0 is invalid. */
\r
345 pdTRUE, /* This is an auto reload timer. */
\r
346 ( void * ) NULL, /* The identifier is not required. */
\r
347 prvISRAutoReloadTimerCallback ); /* The callback that is executed when the timer expires. */
\r
349 xISROneShotTimer = xTimerCreate( ( const signed char * ) "ISR OS", /* The text name given to the timer. */
\r
350 0xffff, /* The timer is not given a period yet - this will be done from the tick hook, but a period of 0 is invalid. */
\r
351 pdFALSE, /* This is a one shot timer. */
\r
352 ( void * ) NULL, /* The identifier is not required. */
\r
353 prvISROneShotTimerCallback ); /* The callback that is executed when the timer expires. */
\r
355 if( ( xISRAutoReloadTimer == NULL ) || ( xISROneShotTimer == NULL ) )
\r
357 xTestStatus = pdFAIL;
\r
358 configASSERT( xTestStatus );
\r
361 /*-----------------------------------------------------------*/
\r
363 static void prvTest2_CheckTaskAndTimersInitialState( void )
\r
365 unsigned char ucTimer;
\r
367 /* Ensure all the timers are in their expected initial state. This depends
\r
368 on the timer service task having a higher priority than this task.
\r
370 auto reload timers 0 to ( configTIMER_QUEUE_LENGTH - 1 ) should now be active,
\r
371 and auto reload timer configTIMER_QUEUE_LENGTH should not yet be active (it
\r
372 could not be started prior to the scheduler being started when it was
\r
374 for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
376 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
378 xTestStatus = pdFAIL;
\r
379 configASSERT( xTestStatus );
\r
383 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] ) != pdFALSE )
\r
385 xTestStatus = pdFAIL;
\r
386 configASSERT( xTestStatus );
\r
389 /*-----------------------------------------------------------*/
\r
391 static void prvTest3_CheckAutoReloadExpireRates( void )
\r
393 unsigned char ucMaxAllowableValue, ucMinAllowableValue, ucTimer;
\r
394 portTickType xBlockPeriod, xTimerPeriod, xExpectedNumber;
\r
396 /* Check the auto reload timers expire at the expected rates. */
\r
399 /* Delaying for configTIMER_QUEUE_LENGTH * xBasePeriod ticks should allow
\r
400 all the auto reload timers to expire at least once. */
\r
401 xBlockPeriod = ( ( portTickType ) configTIMER_QUEUE_LENGTH ) * xBasePeriod;
\r
402 vTaskDelay( xBlockPeriod );
\r
404 /* Check that all the auto reload timers have called their callback
\r
405 function the expected number of times. */
\r
406 for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
408 /* The expected number of expiries is equal to the block period divided
\r
409 by the timer period. */
\r
410 xTimerPeriod = ( ( ( portTickType ) ucTimer + ( portTickType ) 1 ) * xBasePeriod );
\r
411 xExpectedNumber = xBlockPeriod / xTimerPeriod;
\r
413 ucMaxAllowableValue = ( ( unsigned char ) xExpectedNumber ) ;
\r
414 ucMinAllowableValue = ( ( unsigned char ) xExpectedNumber - ( unsigned char ) 1 );
\r
416 if( ( ucAutoReloadTimerCounters[ ucTimer ] < ucMinAllowableValue ) ||
\r
417 ( ucAutoReloadTimerCounters[ ucTimer ] > ucMaxAllowableValue )
\r
420 xTestStatus = pdFAIL;
\r
421 configASSERT( xTestStatus );
\r
425 if( xTestStatus == pdPASS )
\r
427 /* No errors have been reported so increment the loop counter so the
\r
428 check task knows this task is still running. */
\r
432 /*-----------------------------------------------------------*/
\r
434 static void prvTest4_CheckAutoReloadTimersCanBeStopped( void )
\r
436 unsigned char ucTimer;
\r
438 /* Check the auto reload timers can be stopped correctly, and correctly
\r
439 report their state. */
\r
441 /* Stop all the active timers. */
\r
442 for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
444 /* The timer has not been stopped yet! */
\r
445 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
447 xTestStatus = pdFAIL;
\r
448 configASSERT( xTestStatus );
\r
451 /* Now stop the timer. This will appear to happen immediately to
\r
452 this task because this task is running at a priority below the
\r
453 timer service task. */
\r
454 xTimerStop( xAutoReloadTimers[ ucTimer ], tmrdemoDONT_BLOCK );
\r
456 /* The timer should now be inactive. */
\r
457 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )
\r
459 xTestStatus = pdFAIL;
\r
460 configASSERT( xTestStatus );
\r
464 taskENTER_CRITICAL();
\r
466 /* The timer in array position configTIMER_QUEUE_LENGTH should not
\r
467 be active. The critical section is used to ensure the timer does
\r
468 not call its callback between the next line running and the array
\r
469 being cleared back to zero, as that would mask an error condition. */
\r
470 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH ] != ( unsigned char ) 0 )
\r
472 xTestStatus = pdFAIL;
\r
473 configASSERT( xTestStatus );
\r
476 /* Clear the timer callback count. */
\r
477 memset( ( void * ) ucAutoReloadTimerCounters, 0, sizeof( ucAutoReloadTimerCounters ) );
\r
479 taskEXIT_CRITICAL();
\r
481 /* The timers are now all inactive, so this time, after delaying, none
\r
482 of the callback counters should have incremented. */
\r
483 vTaskDelay( ( ( portTickType ) configTIMER_QUEUE_LENGTH ) * xBasePeriod );
\r
484 for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
486 if( ucAutoReloadTimerCounters[ ucTimer ] != ( unsigned char ) 0 )
\r
488 xTestStatus = pdFAIL;
\r
489 configASSERT( xTestStatus );
\r
493 if( xTestStatus == pdPASS )
\r
495 /* No errors have been reported so increment the loop counter so
\r
496 the check task knows this task is still running. */
\r
500 /*-----------------------------------------------------------*/
\r
502 static void prvTest5_CheckBasicOneShotTimerBehaviour( void )
\r
504 /* Check the one shot timer only calls its callback once after it has been
\r
505 started, and that it reports its state correctly. */
\r
507 /* The one shot timer should not be active yet. */
\r
508 if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )
\r
510 xTestStatus = pdFAIL;
\r
511 configASSERT( xTestStatus );
\r
514 if( ucOneShotTimerCounter != ( unsigned char ) 0 )
\r
516 xTestStatus = pdFAIL;
\r
517 configASSERT( xTestStatus );
\r
520 /* Start the one shot timer and check that it reports its state correctly. */
\r
521 xTimerStart( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
522 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
524 xTestStatus = pdFAIL;
\r
525 configASSERT( xTestStatus );
\r
528 /* Delay for three times as long as the one shot timer period, then check
\r
529 to ensure it has only called its callback once, and is now not in the
\r
531 vTaskDelay( tmrdemoONE_SHOT_TIMER_PERIOD * ( portTickType ) 3 );
\r
533 if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )
\r
535 xTestStatus = pdFAIL;
\r
536 configASSERT( xTestStatus );
\r
539 if( ucOneShotTimerCounter != ( unsigned char ) 1 )
\r
541 xTestStatus = pdFAIL;
\r
542 configASSERT( xTestStatus );
\r
546 /* Reset the one shot timer callback count. */
\r
547 ucOneShotTimerCounter = ( unsigned char ) 0;
\r
550 if( xTestStatus == pdPASS )
\r
552 /* No errors have been reported so increment the loop counter so the
\r
553 check task knows this task is still running. */
\r
557 /*-----------------------------------------------------------*/
\r
559 static void prvTest6_CheckAutoReloadResetBehaviour( void )
\r
561 unsigned char ucTimer;
\r
563 /* Check timer reset behaviour. */
\r
565 /* Restart the one shot timer and check it reports its status correctly. */
\r
566 xTimerStart( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
567 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
569 xTestStatus = pdFAIL;
\r
570 configASSERT( xTestStatus );
\r
573 /* Restart one of the auto reload timers and check that it reports its
\r
574 status correctly. */
\r
575 xTimerStart( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
576 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
578 xTestStatus = pdFAIL;
\r
579 configASSERT( xTestStatus );
\r
582 for( ucTimer = 0; ucTimer < trmdemoNUM_TIMER_RESETS; ucTimer++ )
\r
584 /* Delay for half as long as the one shot timer period, then reset it.
\r
585 It should never expire while this is done, so its callback count should
\r
586 never increment. */
\r
587 vTaskDelay( tmrdemoONE_SHOT_TIMER_PERIOD / 2 );
\r
589 /* Check both running timers are still active, but have not called their
\r
590 callback functions. */
\r
591 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
593 xTestStatus = pdFAIL;
\r
594 configASSERT( xTestStatus );
\r
597 if( ucOneShotTimerCounter != ( unsigned char ) 0 )
\r
599 xTestStatus = pdFAIL;
\r
600 configASSERT( xTestStatus );
\r
603 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
605 xTestStatus = pdFAIL;
\r
606 configASSERT( xTestStatus );
\r
609 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] != ( unsigned char ) 0 )
\r
611 xTestStatus = pdFAIL;
\r
612 configASSERT( xTestStatus );
\r
615 /* Reset both running timers. */
\r
616 xTimerReset( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
617 xTimerReset( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
619 if( xTestStatus == pdPASS )
\r
621 /* No errors have been reported so increment the loop counter so
\r
622 the check task knows this task is still running. */
\r
627 /* Finally delay long enough for both running timers to expire. */
\r
628 vTaskDelay( ( ( portTickType ) configTIMER_QUEUE_LENGTH ) * xBasePeriod );
\r
630 /* The timers were not reset during the above delay period so should now
\r
631 both have called their callback functions. */
\r
632 if( ucOneShotTimerCounter != ( unsigned char ) 1 )
\r
634 xTestStatus = pdFAIL;
\r
635 configASSERT( xTestStatus );
\r
638 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] == 0 )
\r
640 xTestStatus = pdFAIL;
\r
641 configASSERT( xTestStatus );
\r
644 /* The one shot timer should no longer be active, while the auto reload
\r
645 timer should still be active. */
\r
646 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
648 xTestStatus = pdFAIL;
\r
649 configASSERT( xTestStatus );
\r
652 if( xTimerIsTimerActive( xOneShotTimer ) == pdTRUE )
\r
654 xTestStatus = pdFAIL;
\r
655 configASSERT( xTestStatus );
\r
658 /* Stop the auto reload timer again. */
\r
659 xTimerStop( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
661 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) != pdFALSE )
\r
663 xTestStatus = pdFAIL;
\r
664 configASSERT( xTestStatus );
\r
667 /* Clear the timer callback counts, ready for another iteration of these
\r
669 ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] = ( unsigned char ) 0;
\r
670 ucOneShotTimerCounter = ( unsigned char ) 0;
\r
672 if( xTestStatus == pdPASS )
\r
674 /* No errors have been reported so increment the loop counter so the check
\r
675 task knows this task is still running. */
\r
679 /*-----------------------------------------------------------*/
\r
681 static void prvResetStartConditionsForNextIteration( void )
\r
683 unsigned char ucTimer;
\r
685 /* Start the timers again to start all the tests over again. */
\r
687 /* Start the timers again. */
\r
688 for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
690 /* The timer has not been started yet! */
\r
691 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )
\r
693 xTestStatus = pdFAIL;
\r
694 configASSERT( xTestStatus );
\r
697 /* Now start the timer. This will appear to happen immediately to
\r
698 this task because this task is running at a priority below the timer
\r
700 xTimerStart( xAutoReloadTimers[ ucTimer ], tmrdemoDONT_BLOCK );
\r
702 /* The timer should now be active. */
\r
703 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
705 xTestStatus = pdFAIL;
\r
706 configASSERT( xTestStatus );
\r
710 if( xTestStatus == pdPASS )
\r
712 /* No errors have been reported so increment the loop counter so the
\r
713 check task knows this task is still running. */
\r
717 /*-----------------------------------------------------------*/
\r
719 void vTimerPeriodicISRTests( void )
\r
721 static portTickType uxTick = ( portTickType ) -1;
\r
723 /* The xHigherPriorityTaskWoken parameter is not used in this case as this
\r
724 function is called from the tick hook anyway. However the API required it
\r
726 signed portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
727 portTickType xMargin;
\r
729 if( configTIMER_TASK_PRIORITY != ( configMAX_PRIORITIES - 1 ) )
\r
731 /* The timer service task is not the highest priority task, so it cannot
\r
732 be assumed that timings will be exact. Timers should never call their
\r
733 callback before their expiry time, but a margin is permissible for calling
\r
734 their callback after their expiry time. If exact timing is required then
\r
735 configTIMER_TASK_PRIORITY must be set to ensure the timer service task
\r
736 is the highest priority task in the system. */
\r
744 /* This test is called from the tick ISR even when the scheduler is suspended.
\r
745 Therefore, it is possible for the xTickCount to be temporarily less than the
\r
746 uxTicks count maintained in this function. That can result in calculated
\r
747 unblock times being too short, as this function is not called as missed ticks
\r
748 (ticks that occur while the scheduler is suspended) are unwound to re-instate
\r
749 the real tick value. Therefore, if this happens, just abandon the test
\r
750 and start again. */
\r
751 if( xTaskGetSchedulerState() != taskSCHEDULER_RUNNING )
\r
753 uxTick = ( portTickType ) -1;
\r
762 /* The timers will have been created, but not started. Start them
\r
763 now by setting their period. */
\r
764 ucISRAutoReloadTimerCounter = 0;
\r
765 ucISROneShotTimerCounter = 0;
\r
766 xTimerChangePeriodFromISR( xISRAutoReloadTimer, xBasePeriod, &xHigherPriorityTaskWoken );
\r
767 xTimerChangePeriodFromISR( xISROneShotTimer, xBasePeriod, &xHigherPriorityTaskWoken );
\r
769 else if( uxTick == xBasePeriod )
\r
771 /* Neither timer should have expired yet. */
\r
772 if( ( ucISRAutoReloadTimerCounter != 0 ) || ( ucISROneShotTimerCounter != 0 ) )
\r
774 xTestStatus = pdFAIL;
\r
775 configASSERT( xTestStatus );
\r
778 else if( uxTick == ( xBasePeriod + xMargin ) )
\r
780 /* Both timers should now have expired once. The auto reload timer will
\r
781 still be active, but the one shot timer should now have stopped. */
\r
782 if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )
\r
784 xTestStatus = pdFAIL;
\r
785 configASSERT( xTestStatus );
\r
788 else if( uxTick == ( 2 * xBasePeriod ) )
\r
790 /* The auto reload timer will still be active, but the one shot timer
\r
791 should now have stopped - however, at this time neither of the timers
\r
792 should have expired again since the last test. */
\r
793 if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )
\r
795 xTestStatus = pdFAIL;
\r
796 configASSERT( xTestStatus );
\r
799 else if( uxTick == ( ( 2 * xBasePeriod ) + xMargin ) )
\r
801 /* The auto reload timer will still be active, but the one shot timer
\r
802 should now have stopped. At this time the auto reload timer should have
\r
803 expired again, but the one shot timer count should not have changed. */
\r
804 if( ucISRAutoReloadTimerCounter != 2 )
\r
806 xTestStatus = pdFAIL;
\r
807 configASSERT( xTestStatus );
\r
810 if( ucISROneShotTimerCounter != 1 )
\r
812 xTestStatus = pdFAIL;
\r
813 configASSERT( xTestStatus );
\r
816 else if( uxTick == ( ( 2 * xBasePeriod ) + ( xBasePeriod >> ( portTickType ) 2U ) ) )
\r
818 /* The auto reload timer will still be active, but the one shot timer
\r
819 should now have stopped. Again though, at this time, neither timer call
\r
820 back should have been called since the last test. */
\r
821 if( ucISRAutoReloadTimerCounter != 2 )
\r
823 xTestStatus = pdFAIL;
\r
824 configASSERT( xTestStatus );
\r
827 if( ucISROneShotTimerCounter != 1 )
\r
829 xTestStatus = pdFAIL;
\r
830 configASSERT( xTestStatus );
\r
833 else if( uxTick == ( 3 * xBasePeriod ) )
\r
835 /* Start the one shot timer again. */
\r
836 xTimerStartFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );
\r
838 else if( uxTick == ( ( 3 * xBasePeriod ) + xMargin ) )
\r
840 /* The auto reload timer and one shot timer will be active. At
\r
841 this time the auto reload timer should have expired again, but the one
\r
842 shot timer count should not have changed yet. */
\r
843 if( ucISRAutoReloadTimerCounter != 3 )
\r
845 xTestStatus = pdFAIL;
\r
846 configASSERT( xTestStatus );
\r
849 if( ucISROneShotTimerCounter != 1 )
\r
851 xTestStatus = pdFAIL;
\r
852 configASSERT( xTestStatus );
\r
855 /* Now stop the auto reload timer. The one shot timer was started
\r
856 a few ticks ago. */
\r
857 xTimerStopFromISR( xISRAutoReloadTimer, &xHigherPriorityTaskWoken );
\r
859 else if( uxTick == ( 4 * xBasePeriod ) )
\r
861 /* The auto reload timer is now stopped, and the one shot timer is
\r
862 active, but at this time neither timer should have expired since the
\r
864 if( ucISRAutoReloadTimerCounter != 3 )
\r
866 xTestStatus = pdFAIL;
\r
867 configASSERT( xTestStatus );
\r
870 if( ucISROneShotTimerCounter != 1 )
\r
872 xTestStatus = pdFAIL;
\r
873 configASSERT( xTestStatus );
\r
876 else if( uxTick == ( ( 4 * xBasePeriod ) + xMargin ) )
\r
878 /* The auto reload timer is now stopped, and the one shot timer is
\r
879 active. The one shot timer should have expired again, but the auto
\r
880 reload timer should not have executed its callback. */
\r
881 if( ucISRAutoReloadTimerCounter != 3 )
\r
883 xTestStatus = pdFAIL;
\r
884 configASSERT( xTestStatus );
\r
887 if( ucISROneShotTimerCounter != 2 )
\r
889 xTestStatus = pdFAIL;
\r
890 configASSERT( xTestStatus );
\r
893 else if( uxTick == ( ( 8 * xBasePeriod ) + xMargin ) )
\r
895 /* The auto reload timer is now stopped, and the one shot timer has
\r
896 already expired and then stopped itself. Both callback counters should
\r
897 not have incremented since the last test. */
\r
898 if( ucISRAutoReloadTimerCounter != 3 )
\r
900 xTestStatus = pdFAIL;
\r
901 configASSERT( xTestStatus );
\r
904 if( ucISROneShotTimerCounter != 2 )
\r
906 xTestStatus = pdFAIL;
\r
907 configASSERT( xTestStatus );
\r
910 /* Now reset the one shot timer. */
\r
911 xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );
\r
913 else if( uxTick == ( 9 * xBasePeriod ) )
\r
915 /* Only the one shot timer should be running, but it should not have
\r
916 expired since the last test. Check the callback counters have not
\r
917 incremented, then reset the one shot timer again. */
\r
918 if( ucISRAutoReloadTimerCounter != 3 )
\r
920 xTestStatus = pdFAIL;
\r
921 configASSERT( xTestStatus );
\r
924 if( ucISROneShotTimerCounter != 2 )
\r
926 xTestStatus = pdFAIL;
\r
927 configASSERT( xTestStatus );
\r
930 xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );
\r
932 else if( uxTick == ( 10 * xBasePeriod ) )
\r
934 /* Only the one shot timer should be running, but it should not have
\r
935 expired since the last test. Check the callback counters have not
\r
936 incremented, then reset the one shot timer again. */
\r
937 if( ucISRAutoReloadTimerCounter != 3 )
\r
939 xTestStatus = pdFAIL;
\r
940 configASSERT( xTestStatus );
\r
943 if( ucISROneShotTimerCounter != 2 )
\r
945 xTestStatus = pdFAIL;
\r
946 configASSERT( xTestStatus );
\r
949 xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );
\r
951 else if( uxTick == ( 11 * xBasePeriod ) )
\r
953 /* Only the one shot timer should be running, but it should not have
\r
954 expired since the last test. Check the callback counters have not
\r
955 incremented, then reset the one shot timer once again. */
\r
956 if( ucISRAutoReloadTimerCounter != 3 )
\r
958 xTestStatus = pdFAIL;
\r
959 configASSERT( xTestStatus );
\r
962 if( ucISROneShotTimerCounter != 2 )
\r
964 xTestStatus = pdFAIL;
\r
965 configASSERT( xTestStatus );
\r
968 xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );
\r
970 else if( uxTick == ( ( 12 * xBasePeriod ) + xMargin ) )
\r
972 /* Only the one shot timer should have been running and this time it
\r
973 should have expired. Check its callback count has been incremented.
\r
974 The auto reload timer is still not running so should still have the same
\r
975 count value. This time the one shot timer is not reset so should not
\r
976 restart from its expiry period again. */
\r
977 if( ucISRAutoReloadTimerCounter != 3 )
\r
979 xTestStatus = pdFAIL;
\r
980 configASSERT( xTestStatus );
\r
983 if( ucISROneShotTimerCounter != 3 )
\r
985 xTestStatus = pdFAIL;
\r
986 configASSERT( xTestStatus );
\r
989 else if( uxTick == ( 15 * xBasePeriod ) )
\r
991 /* Neither timer should be running now. Check neither callback count
\r
992 has incremented, then go back to the start to run these tests all
\r
994 if( ucISRAutoReloadTimerCounter != 3 )
\r
996 xTestStatus = pdFAIL;
\r
997 configASSERT( xTestStatus );
\r
1000 if( ucISROneShotTimerCounter != 3 )
\r
1002 xTestStatus = pdFAIL;
\r
1003 configASSERT( xTestStatus );
\r
1006 uxTick = ( portTickType ) -1;
\r
1009 /*-----------------------------------------------------------*/
\r
1011 /*** Timer callback functions are defined below here. ***/
\r
1013 static void prvAutoReloadTimerCallback( xTimerHandle pxExpiredTimer )
\r
1015 unsigned long ulTimerID;
\r
1017 ulTimerID = ( unsigned long ) pvTimerGetTimerID( pxExpiredTimer );
\r
1018 if( ulTimerID <= ( configTIMER_QUEUE_LENGTH + 1 ) )
\r
1020 ( ucAutoReloadTimerCounters[ ulTimerID ] )++;
\r
1024 /* The timer ID appears to be unexpected (invalid). */
\r
1025 xTestStatus = pdFAIL;
\r
1026 configASSERT( xTestStatus );
\r
1029 /*-----------------------------------------------------------*/
\r
1031 static void prvOneShotTimerCallback( xTimerHandle pxExpiredTimer )
\r
1033 /* The parameter is not used in this case as only one timer uses this
\r
1034 callback function. */
\r
1035 ( void ) pxExpiredTimer;
\r
1037 ucOneShotTimerCounter++;
\r
1039 /*-----------------------------------------------------------*/
\r
1041 static void prvISRAutoReloadTimerCallback( xTimerHandle pxExpiredTimer )
\r
1043 /* The parameter is not used in this case as only one timer uses this
\r
1044 callback function. */
\r
1045 ( void ) pxExpiredTimer;
\r
1047 ucISRAutoReloadTimerCounter++;
\r
1049 /*-----------------------------------------------------------*/
\r
1051 static void prvISROneShotTimerCallback( xTimerHandle pxExpiredTimer )
\r
1053 /* The parameter is not used in this case as only one timer uses this
\r
1054 callback function. */
\r
1055 ( void ) pxExpiredTimer;
\r
1057 ucISROneShotTimerCounter++;
\r
1059 /*-----------------------------------------------------------*/
\r