2 FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\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 modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
69 * Tests the behaviour of timers. Some timers are created before the scheduler
\r
70 * is started, and some after.
\r
73 /* Standard includes. */
\r
76 /* Scheduler include files. */
\r
77 #include "FreeRTOS.h"
\r
81 /* Demo program include files. */
\r
82 #include "TimerDemo.h"
\r
84 #if ( configTIMER_TASK_PRIORITY < 1 )
\r
85 #error configTIMER_TASK_PRIORITY must be set to at least 1 for this test/demo to function correctly.
\r
88 #define tmrdemoDONT_BLOCK ( ( portTickType ) 0 )
\r
89 #define tmrdemoONE_SHOT_TIMER_PERIOD ( xBasePeriod * ( portTickType ) 3 )
\r
90 #define trmdemoNUM_TIMER_RESETS ( ( unsigned char ) 10 )
\r
92 /*-----------------------------------------------------------*/
\r
94 /* The callback functions used by the timers. These each increment a counter
\r
95 to indicate which timer has expired. The auto-reload timers that are used by
\r
96 the test task (as opposed to being used from an ISR) all share the same
\r
97 prvAutoReloadTimerCallback() callback function, and use the ID of the
\r
98 pxExpiredTimer parameter passed into that function to know which counter to
\r
99 increment. The other timers all have their own unique callback function and
\r
100 simply increment their counters without using the callback function parameter. */
\r
101 static void prvAutoReloadTimerCallback( xTimerHandle pxExpiredTimer );
\r
102 static void prvOneShotTimerCallback( xTimerHandle pxExpiredTimer );
\r
103 static void prvTimerTestTask( void *pvParameters );
\r
104 static void prvISRAutoReloadTimerCallback( xTimerHandle pxExpiredTimer );
\r
105 static void prvISROneShotTimerCallback( xTimerHandle pxExpiredTimer );
\r
107 /* The test functions used by the timer test task. These manipulate the auto
\r
108 reload and one shot timers in various ways, then delay, then inspect the timers
\r
109 to ensure they have behaved as expected. */
\r
110 static void prvTest1_CreateTimersWithoutSchedulerRunning( void );
\r
111 static void prvTest2_CheckTaskAndTimersInitialState( void );
\r
112 static void prvTest3_CheckAutoReloadExpireRates( void );
\r
113 static void prvTest4_CheckAutoReloadTimersCanBeStopped( void );
\r
114 static void prvTest5_CheckBasicOneShotTimerBehaviour( void );
\r
115 static void prvTest6_CheckAutoReloadResetBehaviour( void );
\r
116 static void prvResetStartConditionsForNextIteration( void );
\r
118 /*-----------------------------------------------------------*/
\r
120 /* Flag that will be latched to pdFAIL should any unexpected behaviour be
\r
121 detected in any of the demo tests. */
\r
122 static volatile portBASE_TYPE xTestStatus = pdPASS;
\r
124 /* Counter that is incremented on each cycle of a test. This is used to
\r
125 detect a stalled task - a test that is no longer running. */
\r
126 static volatile unsigned long ulLoopCounter = 0;
\r
128 /* A set of auto reload timers - each of which use the same callback function.
\r
129 The callback function uses the timer ID to index into, and then increment, a
\r
130 counter in the ucAutoReloadTimerCounters[] array. The auto reload timers
\r
131 referenced from xAutoReloadTimers[] are used by the prvTimerTestTask task. */
\r
132 static xTimerHandle xAutoReloadTimers[ configTIMER_QUEUE_LENGTH + 1 ] = { 0 };
\r
133 static unsigned char ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH + 1 ] = { 0 };
\r
135 /* The one shot timer is configured to use a callback function that increments
\r
136 ucOneShotTimerCounter each time it gets called. */
\r
137 static xTimerHandle xOneShotTimer = NULL;
\r
138 static unsigned char ucOneShotTimerCounter = ( unsigned char ) 0;
\r
140 /* The ISR reload timer is controlled from the tick hook to exercise the timer
\r
141 API functions that can be used from an ISR. It is configured to increment
\r
142 ucISRReloadTimerCounter each time its callback function is executed. */
\r
143 static xTimerHandle xISRAutoReloadTimer = NULL;
\r
144 static unsigned char ucISRAutoReloadTimerCounter = ( unsigned char ) 0;
\r
146 /* The ISR one shot timer is controlled from the tick hook to exercise the timer
\r
147 API functions that can be used from an ISR. It is configured to increment
\r
148 ucISRReloadTimerCounter each time its callback function is executed. */
\r
149 static xTimerHandle xISROneShotTimer = NULL;
\r
150 static unsigned char ucISROneShotTimerCounter = ( unsigned char ) 0;
\r
152 /* The period of all the timers are a multiple of the base period. The base
\r
153 period is configured by the parameter to vStartTimerDemoTask(). */
\r
154 static portTickType xBasePeriod = 0;
\r
156 /*-----------------------------------------------------------*/
\r
158 void vStartTimerDemoTask( portTickType xBasePeriodIn )
\r
160 /* Start with the timer and counter arrays clear - this is only necessary
\r
161 where the compiler does not clear them automatically on start up. */
\r
162 memset( ucAutoReloadTimerCounters, 0x00, sizeof( ucAutoReloadTimerCounters ) );
\r
163 memset( xAutoReloadTimers, 0x00, sizeof( xAutoReloadTimers ) );
\r
165 /* Store the period from which all the timer periods will be generated from
\r
167 xBasePeriod = xBasePeriodIn;
\r
169 /* Create a set of timers for use by this demo/test. */
\r
170 prvTest1_CreateTimersWithoutSchedulerRunning();
\r
172 /* Create the task that will control and monitor the timers. This is
\r
173 created at a lower priority than the timer service task to ensure, as
\r
174 far as it is concerned, commands on timers are actioned immediately
\r
175 (sending a command to the timer service task will unblock the timer service
\r
176 task, which will then preempt this task). */
\r
177 if( xTestStatus != pdFAIL )
\r
179 xTaskCreate( prvTimerTestTask, ( signed portCHAR * ) "Tmr Tst", configMINIMAL_STACK_SIZE, NULL, configTIMER_TASK_PRIORITY - 1, NULL );
\r
182 /*-----------------------------------------------------------*/
\r
184 static void prvTimerTestTask( void *pvParameters )
\r
186 ( void ) pvParameters;
\r
188 /* Create a one-shot timer for use later on in this test. */
\r
189 xOneShotTimer = xTimerCreate( ( const signed char * ) "Oneshot Timer",/* Text name to facilitate debugging. The kernel does not use this itself. */
\r
190 tmrdemoONE_SHOT_TIMER_PERIOD, /* The period for the timer. */
\r
191 pdFALSE, /* Don't auto-reload - hence a one shot timer. */
\r
192 ( void * ) 0, /* The timer identifier. In this case this is not used as the timer has its own callback. */
\r
193 prvOneShotTimerCallback ); /* The callback to be called when the timer expires. */
\r
195 if( xOneShotTimer == NULL )
\r
197 xTestStatus = pdFAIL;
\r
198 configASSERT( xTestStatus );
\r
202 /* Ensure all the timers are in their expected initial state. This
\r
203 depends on the timer service task having a higher priority than this task. */
\r
204 prvTest2_CheckTaskAndTimersInitialState();
\r
208 /* Check the auto reload timers expire at the expected/correct rates. */
\r
209 prvTest3_CheckAutoReloadExpireRates();
\r
211 /* Check the auto reload timers can be stopped correctly, and correctly
\r
212 report their state. */
\r
213 prvTest4_CheckAutoReloadTimersCanBeStopped();
\r
215 /* Check the one shot timer only calls its callback once after it has been
\r
216 started, and that it reports its state correctly. */
\r
217 prvTest5_CheckBasicOneShotTimerBehaviour();
\r
219 /* Check timer reset behaviour. */
\r
220 prvTest6_CheckAutoReloadResetBehaviour();
\r
222 /* Start the timers again to restart all the tests over again. */
\r
223 prvResetStartConditionsForNextIteration();
\r
226 /*-----------------------------------------------------------*/
\r
228 /* This is called to check that the created task is still running and has not
\r
229 detected any errors. */
\r
230 portBASE_TYPE xAreTimerDemoTasksStillRunning( portTickType xCycleFrequency )
\r
232 static unsigned long ulLastLoopCounter = 0UL;
\r
233 portTickType xMaxBlockTimeUsedByTheseTests, xLoopCounterIncrementTimeMax;
\r
234 static portTickType xIterationsWithoutCounterIncrement = ( portTickType ) 0, xLastCycleFrequency;
\r
236 if( xLastCycleFrequency != xCycleFrequency )
\r
238 /* The cycle frequency has probably become much faster due to an error
\r
239 elsewhere. Start counting Iterations again. */
\r
240 xIterationsWithoutCounterIncrement = ( portTickType ) 0;
\r
241 xLastCycleFrequency = xCycleFrequency;
\r
244 /* Calculate the maximum number of times that it is permissible for this
\r
245 function to be called without ulLoopCounter being incremented. This is
\r
246 necessary because the tests in this file block for extended periods, and the
\r
247 block period might be longer than the time between calls to this function. */
\r
248 xMaxBlockTimeUsedByTheseTests = ( ( portTickType ) configTIMER_QUEUE_LENGTH ) * xBasePeriod;
\r
249 xLoopCounterIncrementTimeMax = xMaxBlockTimeUsedByTheseTests / xCycleFrequency;
\r
251 /* If the demo task is still running then we expect the loopcounter to
\r
252 have incremented every xLoopCounterIncrementTimeMax calls. */
\r
253 if( ulLastLoopCounter == ulLoopCounter )
\r
255 xIterationsWithoutCounterIncrement++;
\r
256 if( xIterationsWithoutCounterIncrement > xLoopCounterIncrementTimeMax )
\r
258 /* The tests appear to be no longer running (stalled). */
\r
259 xTestStatus = pdFAIL;
\r
260 configASSERT( xTestStatus );
\r
265 /* ulLoopCounter changed, so the count of times this function was called
\r
266 without a change can be reset to zero. */
\r
267 xIterationsWithoutCounterIncrement = ( portTickType ) 0;
\r
270 ulLastLoopCounter = ulLoopCounter;
\r
272 /* Errors detected in the task itself will have latched xTestStatus
\r
275 return xTestStatus;
\r
277 /*-----------------------------------------------------------*/
\r
279 static void prvTest1_CreateTimersWithoutSchedulerRunning( void )
\r
281 unsigned portBASE_TYPE xTimer;
\r
283 for( xTimer = 0; xTimer < configTIMER_QUEUE_LENGTH; xTimer++ )
\r
285 /* As the timer queue is not yet full, it should be possible to both create
\r
286 and start a timer. These timers are being started before the scheduler has
\r
287 been started, so their block times should get set to zero within the timer
\r
289 xAutoReloadTimers[ xTimer ] = xTimerCreate( ( const signed char * )"FR Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
\r
290 ( ( xTimer + ( portTickType ) 1 ) * xBasePeriod ),/* The period for the timer. The plus 1 ensures a period of zero is not specified. */
\r
291 pdTRUE, /* Auto-reload is set to true. */
\r
292 ( void * ) xTimer, /* An identifier for the timer as all the auto reload timers use the same callback. */
\r
293 prvAutoReloadTimerCallback ); /* The callback to be called when the timer expires. */
\r
295 if( xAutoReloadTimers[ xTimer ] == NULL )
\r
297 xTestStatus = pdFAIL;
\r
298 configASSERT( xTestStatus );
\r
302 /* The scheduler has not yet started, so the block period of
\r
303 portMAX_DELAY should just get set to zero in xTimerStart(). Also,
\r
304 the timer queue is not yet full so xTimerStart() should return
\r
306 if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) != pdPASS )
\r
308 xTestStatus = pdFAIL;
\r
309 configASSERT( xTestStatus );
\r
314 /* The timers queue should now be full, so it should be possible to create
\r
315 another timer, but not possible to start it (the timer queue will not get
\r
316 drained until the scheduler has been started. */
\r
317 xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] = xTimerCreate( ( const signed char * ) "FR Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
\r
318 ( configTIMER_QUEUE_LENGTH * xBasePeriod ), /* The period for the timer. */
\r
319 pdTRUE, /* Auto-reload is set to true. */
\r
320 ( void * ) xTimer, /* An identifier for the timer as all the auto reload timers use the same callback. */
\r
321 prvAutoReloadTimerCallback ); /* The callback executed when the timer expires. */
\r
323 if( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] == NULL )
\r
325 xTestStatus = pdFAIL;
\r
326 configASSERT( xTestStatus );
\r
330 if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) == pdPASS )
\r
332 /* This time it would not be expected that the timer could be
\r
333 started at this point. */
\r
334 xTestStatus = pdFAIL;
\r
335 configASSERT( xTestStatus );
\r
339 /* Create the timers that are used from the tick interrupt to test the timer
\r
340 API functions that can be called from an ISR. */
\r
341 xISRAutoReloadTimer = xTimerCreate( ( const signed char * ) "ISR AR", /* The text name given to the timer. */
\r
342 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
343 pdTRUE, /* This is an auto reload timer. */
\r
344 ( void * ) NULL, /* The identifier is not required. */
\r
345 prvISRAutoReloadTimerCallback ); /* The callback that is executed when the timer expires. */
\r
347 xISROneShotTimer = xTimerCreate( ( const signed char * ) "ISR OS", /* The text name given to the timer. */
\r
348 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
349 pdFALSE, /* This is a one shot timer. */
\r
350 ( void * ) NULL, /* The identifier is not required. */
\r
351 prvISROneShotTimerCallback ); /* The callback that is executed when the timer expires. */
\r
353 if( ( xISRAutoReloadTimer == NULL ) || ( xISROneShotTimer == NULL ) )
\r
355 xTestStatus = pdFAIL;
\r
356 configASSERT( xTestStatus );
\r
359 /*-----------------------------------------------------------*/
\r
361 static void prvTest2_CheckTaskAndTimersInitialState( void )
\r
363 unsigned char ucTimer;
\r
365 /* Ensure all the timers are in their expected initial state. This depends
\r
366 on the timer service task having a higher priority than this task.
\r
368 auto reload timers 0 to ( configTIMER_QUEUE_LENGTH - 1 ) should now be active,
\r
369 and auto reload timer configTIMER_QUEUE_LENGTH should not yet be active (it
\r
370 could not be started prior to the scheduler being started when it was
\r
372 for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
374 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
376 xTestStatus = pdFAIL;
\r
377 configASSERT( xTestStatus );
\r
381 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] ) != pdFALSE )
\r
383 xTestStatus = pdFAIL;
\r
384 configASSERT( xTestStatus );
\r
387 /*-----------------------------------------------------------*/
\r
389 static void prvTest3_CheckAutoReloadExpireRates( void )
\r
391 unsigned char ucMaxAllowableValue, ucMinAllowableValue, ucTimer;
\r
392 portTickType xBlockPeriod, xTimerPeriod, xExpectedNumber;
\r
394 /* Check the auto reload timers expire at the expected rates. */
\r
397 /* Delaying for configTIMER_QUEUE_LENGTH * xBasePeriod ticks should allow
\r
398 all the auto reload timers to expire at least once. */
\r
399 xBlockPeriod = ( ( portTickType ) configTIMER_QUEUE_LENGTH ) * xBasePeriod;
\r
400 vTaskDelay( xBlockPeriod );
\r
402 /* Check that all the auto reload timers have called their callback
\r
403 function the expected number of times. */
\r
404 for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
406 /* The expected number of expiries is equal to the block period divided
\r
407 by the timer period. */
\r
408 xTimerPeriod = ( ( ( portTickType ) ucTimer + ( portTickType ) 1 ) * xBasePeriod );
\r
409 xExpectedNumber = xBlockPeriod / xTimerPeriod;
\r
411 ucMaxAllowableValue = ( ( unsigned char ) xExpectedNumber ) ;
\r
412 ucMinAllowableValue = ( ( unsigned char ) xExpectedNumber - ( unsigned char ) 1 );
\r
414 if( ( ucAutoReloadTimerCounters[ ucTimer ] < ucMinAllowableValue ) ||
\r
415 ( ucAutoReloadTimerCounters[ ucTimer ] > ucMaxAllowableValue )
\r
418 xTestStatus = pdFAIL;
\r
419 configASSERT( xTestStatus );
\r
423 if( xTestStatus == pdPASS )
\r
425 /* No errors have been reported so increment the loop counter so the
\r
426 check task knows this task is still running. */
\r
430 /*-----------------------------------------------------------*/
\r
432 static void prvTest4_CheckAutoReloadTimersCanBeStopped( void )
\r
434 unsigned char ucTimer;
\r
436 /* Check the auto reload timers can be stopped correctly, and correctly
\r
437 report their state. */
\r
439 /* Stop all the active timers. */
\r
440 for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
442 /* The timer has not been stopped yet! */
\r
443 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
445 xTestStatus = pdFAIL;
\r
446 configASSERT( xTestStatus );
\r
449 /* Now stop the timer. This will appear to happen immediately to
\r
450 this task because this task is running at a priority below the
\r
451 timer service task. */
\r
452 xTimerStop( xAutoReloadTimers[ ucTimer ], tmrdemoDONT_BLOCK );
\r
454 /* The timer should now be inactive. */
\r
455 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )
\r
457 xTestStatus = pdFAIL;
\r
458 configASSERT( xTestStatus );
\r
462 taskENTER_CRITICAL();
\r
464 /* The timer in array position configTIMER_QUEUE_LENGTH should not
\r
465 be active. The critical section is used to ensure the timer does
\r
466 not call its callback between the next line running and the array
\r
467 being cleared back to zero, as that would mask an error condition. */
\r
468 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH ] != ( unsigned char ) 0 )
\r
470 xTestStatus = pdFAIL;
\r
471 configASSERT( xTestStatus );
\r
474 /* Clear the timer callback count. */
\r
475 memset( ( void * ) ucAutoReloadTimerCounters, 0, sizeof( ucAutoReloadTimerCounters ) );
\r
477 taskEXIT_CRITICAL();
\r
479 /* The timers are now all inactive, so this time, after delaying, none
\r
480 of the callback counters should have incremented. */
\r
481 vTaskDelay( ( ( portTickType ) configTIMER_QUEUE_LENGTH ) * xBasePeriod );
\r
482 for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
484 if( ucAutoReloadTimerCounters[ ucTimer ] != ( unsigned char ) 0 )
\r
486 xTestStatus = pdFAIL;
\r
487 configASSERT( xTestStatus );
\r
491 if( xTestStatus == pdPASS )
\r
493 /* No errors have been reported so increment the loop counter so
\r
494 the check task knows this task is still running. */
\r
498 /*-----------------------------------------------------------*/
\r
500 static void prvTest5_CheckBasicOneShotTimerBehaviour( void )
\r
502 /* Check the one shot timer only calls its callback once after it has been
\r
503 started, and that it reports its state correctly. */
\r
505 /* The one shot timer should not be active yet. */
\r
506 if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )
\r
508 xTestStatus = pdFAIL;
\r
509 configASSERT( xTestStatus );
\r
512 if( ucOneShotTimerCounter != ( unsigned char ) 0 )
\r
514 xTestStatus = pdFAIL;
\r
515 configASSERT( xTestStatus );
\r
518 /* Start the one shot timer and check that it reports its state correctly. */
\r
519 xTimerStart( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
520 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
522 xTestStatus = pdFAIL;
\r
523 configASSERT( xTestStatus );
\r
526 /* Delay for three times as long as the one shot timer period, then check
\r
527 to ensure it has only called its callback once, and is now not in the
\r
529 vTaskDelay( tmrdemoONE_SHOT_TIMER_PERIOD * ( portTickType ) 3 );
\r
531 if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )
\r
533 xTestStatus = pdFAIL;
\r
534 configASSERT( xTestStatus );
\r
537 if( ucOneShotTimerCounter != ( unsigned char ) 1 )
\r
539 xTestStatus = pdFAIL;
\r
540 configASSERT( xTestStatus );
\r
544 /* Reset the one shot timer callback count. */
\r
545 ucOneShotTimerCounter = ( unsigned char ) 0;
\r
548 if( xTestStatus == pdPASS )
\r
550 /* No errors have been reported so increment the loop counter so the
\r
551 check task knows this task is still running. */
\r
555 /*-----------------------------------------------------------*/
\r
557 static void prvTest6_CheckAutoReloadResetBehaviour( void )
\r
559 unsigned char ucTimer;
\r
561 /* Check timer reset behaviour. */
\r
563 /* Restart the one shot timer and check it reports its status correctly. */
\r
564 xTimerStart( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
565 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
567 xTestStatus = pdFAIL;
\r
568 configASSERT( xTestStatus );
\r
571 /* Restart one of the auto reload timers and check that it reports its
\r
572 status correctly. */
\r
573 xTimerStart( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
574 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
576 xTestStatus = pdFAIL;
\r
577 configASSERT( xTestStatus );
\r
580 for( ucTimer = 0; ucTimer < trmdemoNUM_TIMER_RESETS; ucTimer++ )
\r
582 /* Delay for half as long as the one shot timer period, then reset it.
\r
583 It should never expire while this is done, so its callback count should
\r
584 never increment. */
\r
585 vTaskDelay( tmrdemoONE_SHOT_TIMER_PERIOD / 2 );
\r
587 /* Check both running timers are still active, but have not called their
\r
588 callback functions. */
\r
589 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
591 xTestStatus = pdFAIL;
\r
592 configASSERT( xTestStatus );
\r
595 if( ucOneShotTimerCounter != ( unsigned char ) 0 )
\r
597 xTestStatus = pdFAIL;
\r
598 configASSERT( xTestStatus );
\r
601 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
603 xTestStatus = pdFAIL;
\r
604 configASSERT( xTestStatus );
\r
607 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] != ( unsigned char ) 0 )
\r
609 xTestStatus = pdFAIL;
\r
610 configASSERT( xTestStatus );
\r
613 /* Reset both running timers. */
\r
614 xTimerReset( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
615 xTimerReset( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
617 if( xTestStatus == pdPASS )
\r
619 /* No errors have been reported so increment the loop counter so
\r
620 the check task knows this task is still running. */
\r
625 /* Finally delay long enough for both running timers to expire. */
\r
626 vTaskDelay( ( ( portTickType ) configTIMER_QUEUE_LENGTH ) * xBasePeriod );
\r
628 /* The timers were not reset during the above delay period so should now
\r
629 both have called their callback functions. */
\r
630 if( ucOneShotTimerCounter != ( unsigned char ) 1 )
\r
632 xTestStatus = pdFAIL;
\r
633 configASSERT( xTestStatus );
\r
636 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] == 0 )
\r
638 xTestStatus = pdFAIL;
\r
639 configASSERT( xTestStatus );
\r
642 /* The one shot timer should no longer be active, while the auto reload
\r
643 timer should still be active. */
\r
644 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
646 xTestStatus = pdFAIL;
\r
647 configASSERT( xTestStatus );
\r
650 if( xTimerIsTimerActive( xOneShotTimer ) == pdTRUE )
\r
652 xTestStatus = pdFAIL;
\r
653 configASSERT( xTestStatus );
\r
656 /* Stop the auto reload timer again. */
\r
657 xTimerStop( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
659 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) != pdFALSE )
\r
661 xTestStatus = pdFAIL;
\r
662 configASSERT( xTestStatus );
\r
665 /* Clear the timer callback counts, ready for another iteration of these
\r
667 ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] = ( unsigned char ) 0;
\r
668 ucOneShotTimerCounter = ( unsigned char ) 0;
\r
670 if( xTestStatus == pdPASS )
\r
672 /* No errors have been reported so increment the loop counter so the check
\r
673 task knows this task is still running. */
\r
677 /*-----------------------------------------------------------*/
\r
679 static void prvResetStartConditionsForNextIteration( void )
\r
681 unsigned char ucTimer;
\r
683 /* Start the timers again to start all the tests over again. */
\r
685 /* Start the timers again. */
\r
686 for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
688 /* The timer has not been started yet! */
\r
689 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )
\r
691 xTestStatus = pdFAIL;
\r
692 configASSERT( xTestStatus );
\r
695 /* Now start the timer. This will appear to happen immediately to
\r
696 this task because this task is running at a priority below the timer
\r
698 xTimerStart( xAutoReloadTimers[ ucTimer ], tmrdemoDONT_BLOCK );
\r
700 /* The timer should now be active. */
\r
701 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
703 xTestStatus = pdFAIL;
\r
704 configASSERT( xTestStatus );
\r
708 if( xTestStatus == pdPASS )
\r
710 /* No errors have been reported so increment the loop counter so the
\r
711 check task knows this task is still running. */
\r
715 /*-----------------------------------------------------------*/
\r
717 void vTimerPeriodicISRTests( void )
\r
719 static portTickType uxTick = ( portTickType ) -1;
\r
721 /* The xHigherPriorityTaskWoken parameter is not used in this case as this
\r
722 function is called from the tick hook anyway. However the API required it
\r
724 signed portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
725 portTickType xMargin;
\r
727 if( configTIMER_TASK_PRIORITY != ( configMAX_PRIORITIES - 1 ) )
\r
729 /* The timer service task is not the highest priority task, so it cannot
\r
730 be assumed that timings will be exact. Timers should never call their
\r
731 callback before their expiry time, but a margin is permissible for calling
\r
732 their callback after their expiry time. If exact timing is required then
\r
733 configTIMER_TASK_PRIORITY must be set to ensure the timer service task
\r
734 is the highest priority task in the system. */
\r
742 /* This test is called from the tick ISR even when the scheduler is suspended.
\r
743 Therefore, it is possible for the xTickCount to be temporarily less than the
\r
744 uxTicks count maintained in this function. That can result in calculated
\r
745 unblock times being too short, as this function is not called as missed ticks
\r
746 (ticks that occur while the scheduler is suspended) are unwound to re-instate
\r
747 the real tick value. Therefore, if this happens, just abandon the test
\r
748 and start again. */
\r
749 if( xTaskGetSchedulerState() != taskSCHEDULER_RUNNING )
\r
751 uxTick = ( portTickType ) -1;
\r
760 /* The timers will have been created, but not started. Start them
\r
761 now by setting their period. */
\r
762 ucISRAutoReloadTimerCounter = 0;
\r
763 ucISROneShotTimerCounter = 0;
\r
764 xTimerChangePeriodFromISR( xISRAutoReloadTimer, xBasePeriod, &xHigherPriorityTaskWoken );
\r
765 xTimerChangePeriodFromISR( xISROneShotTimer, xBasePeriod, &xHigherPriorityTaskWoken );
\r
767 else if( uxTick == xBasePeriod )
\r
769 /* Neither timer should have expired yet. */
\r
770 if( ( ucISRAutoReloadTimerCounter != 0 ) || ( ucISROneShotTimerCounter != 0 ) )
\r
772 xTestStatus = pdFAIL;
\r
773 configASSERT( xTestStatus );
\r
776 else if( uxTick == ( xBasePeriod + xMargin ) )
\r
778 /* Both timers should now have expired once. The auto reload timer will
\r
779 still be active, but the one shot timer should now have stopped. */
\r
780 if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )
\r
782 xTestStatus = pdFAIL;
\r
783 configASSERT( xTestStatus );
\r
786 else if( uxTick == ( 2 * xBasePeriod ) )
\r
788 /* The auto reload timer will still be active, but the one shot timer
\r
789 should now have stopped - however, at this time neither of the timers
\r
790 should have expired again since the last test. */
\r
791 if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )
\r
793 xTestStatus = pdFAIL;
\r
794 configASSERT( xTestStatus );
\r
797 else if( uxTick == ( ( 2 * xBasePeriod ) + xMargin ) )
\r
799 /* The auto reload timer will still be active, but the one shot timer
\r
800 should now have stopped. At this time the auto reload timer should have
\r
801 expired again, but the one shot timer count should not have changed. */
\r
802 if( ucISRAutoReloadTimerCounter != 2 )
\r
804 xTestStatus = pdFAIL;
\r
805 configASSERT( xTestStatus );
\r
808 if( ucISROneShotTimerCounter != 1 )
\r
810 xTestStatus = pdFAIL;
\r
811 configASSERT( xTestStatus );
\r
814 else if( uxTick == ( ( 2 * xBasePeriod ) + ( xBasePeriod >> ( portTickType ) 2U ) ) )
\r
816 /* The auto reload timer will still be active, but the one shot timer
\r
817 should now have stopped. Again though, at this time, neither timer call
\r
818 back should have been called since the last test. */
\r
819 if( ucISRAutoReloadTimerCounter != 2 )
\r
821 xTestStatus = pdFAIL;
\r
822 configASSERT( xTestStatus );
\r
825 if( ucISROneShotTimerCounter != 1 )
\r
827 xTestStatus = pdFAIL;
\r
828 configASSERT( xTestStatus );
\r
831 else if( uxTick == ( 3 * xBasePeriod ) )
\r
833 /* Start the one shot timer again. */
\r
834 xTimerStartFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );
\r
836 else if( uxTick == ( ( 3 * xBasePeriod ) + xMargin ) )
\r
838 /* The auto reload timer and one shot timer will be active. At
\r
839 this time the auto reload timer should have expired again, but the one
\r
840 shot timer count should not have changed yet. */
\r
841 if( ucISRAutoReloadTimerCounter != 3 )
\r
843 xTestStatus = pdFAIL;
\r
844 configASSERT( xTestStatus );
\r
847 if( ucISROneShotTimerCounter != 1 )
\r
849 xTestStatus = pdFAIL;
\r
850 configASSERT( xTestStatus );
\r
853 /* Now stop the auto reload timer. The one shot timer was started
\r
854 a few ticks ago. */
\r
855 xTimerStopFromISR( xISRAutoReloadTimer, &xHigherPriorityTaskWoken );
\r
857 else if( uxTick == ( 4 * xBasePeriod ) )
\r
859 /* The auto reload timer is now stopped, and the one shot timer is
\r
860 active, but at this time neither timer should have expired since the
\r
862 if( ucISRAutoReloadTimerCounter != 3 )
\r
864 xTestStatus = pdFAIL;
\r
865 configASSERT( xTestStatus );
\r
868 if( ucISROneShotTimerCounter != 1 )
\r
870 xTestStatus = pdFAIL;
\r
871 configASSERT( xTestStatus );
\r
874 else if( uxTick == ( ( 4 * xBasePeriod ) + xMargin ) )
\r
876 /* The auto reload timer is now stopped, and the one shot timer is
\r
877 active. The one shot timer should have expired again, but the auto
\r
878 reload timer should not have executed its callback. */
\r
879 if( ucISRAutoReloadTimerCounter != 3 )
\r
881 xTestStatus = pdFAIL;
\r
882 configASSERT( xTestStatus );
\r
885 if( ucISROneShotTimerCounter != 2 )
\r
887 xTestStatus = pdFAIL;
\r
888 configASSERT( xTestStatus );
\r
891 else if( uxTick == ( ( 8 * xBasePeriod ) + xMargin ) )
\r
893 /* The auto reload timer is now stopped, and the one shot timer has
\r
894 already expired and then stopped itself. Both callback counters should
\r
895 not have incremented since the last test. */
\r
896 if( ucISRAutoReloadTimerCounter != 3 )
\r
898 xTestStatus = pdFAIL;
\r
899 configASSERT( xTestStatus );
\r
902 if( ucISROneShotTimerCounter != 2 )
\r
904 xTestStatus = pdFAIL;
\r
905 configASSERT( xTestStatus );
\r
908 /* Now reset the one shot timer. */
\r
909 xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );
\r
911 else if( uxTick == ( 9 * xBasePeriod ) )
\r
913 /* Only the one shot timer should be running, but it should not have
\r
914 expired since the last test. Check the callback counters have not
\r
915 incremented, then reset the one shot timer again. */
\r
916 if( ucISRAutoReloadTimerCounter != 3 )
\r
918 xTestStatus = pdFAIL;
\r
919 configASSERT( xTestStatus );
\r
922 if( ucISROneShotTimerCounter != 2 )
\r
924 xTestStatus = pdFAIL;
\r
925 configASSERT( xTestStatus );
\r
928 xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );
\r
930 else if( uxTick == ( 10 * xBasePeriod ) )
\r
932 /* Only the one shot timer should be running, but it should not have
\r
933 expired since the last test. Check the callback counters have not
\r
934 incremented, then reset the one shot timer again. */
\r
935 if( ucISRAutoReloadTimerCounter != 3 )
\r
937 xTestStatus = pdFAIL;
\r
938 configASSERT( xTestStatus );
\r
941 if( ucISROneShotTimerCounter != 2 )
\r
943 xTestStatus = pdFAIL;
\r
944 configASSERT( xTestStatus );
\r
947 xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );
\r
949 else if( uxTick == ( 11 * xBasePeriod ) )
\r
951 /* Only the one shot timer should be running, but it should not have
\r
952 expired since the last test. Check the callback counters have not
\r
953 incremented, then reset the one shot timer once again. */
\r
954 if( ucISRAutoReloadTimerCounter != 3 )
\r
956 xTestStatus = pdFAIL;
\r
957 configASSERT( xTestStatus );
\r
960 if( ucISROneShotTimerCounter != 2 )
\r
962 xTestStatus = pdFAIL;
\r
963 configASSERT( xTestStatus );
\r
966 xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );
\r
968 else if( uxTick == ( ( 12 * xBasePeriod ) + xMargin ) )
\r
970 /* Only the one shot timer should have been running and this time it
\r
971 should have expired. Check its callback count has been incremented.
\r
972 The auto reload timer is still not running so should still have the same
\r
973 count value. This time the one shot timer is not reset so should not
\r
974 restart from its expiry period again. */
\r
975 if( ucISRAutoReloadTimerCounter != 3 )
\r
977 xTestStatus = pdFAIL;
\r
978 configASSERT( xTestStatus );
\r
981 if( ucISROneShotTimerCounter != 3 )
\r
983 xTestStatus = pdFAIL;
\r
984 configASSERT( xTestStatus );
\r
987 else if( uxTick == ( 15 * xBasePeriod ) )
\r
989 /* Neither timer should be running now. Check neither callback count
\r
990 has incremented, then go back to the start to run these tests all
\r
992 if( ucISRAutoReloadTimerCounter != 3 )
\r
994 xTestStatus = pdFAIL;
\r
995 configASSERT( xTestStatus );
\r
998 if( ucISROneShotTimerCounter != 3 )
\r
1000 xTestStatus = pdFAIL;
\r
1001 configASSERT( xTestStatus );
\r
1004 uxTick = ( portTickType ) -1;
\r
1007 /*-----------------------------------------------------------*/
\r
1009 /*** Timer callback functions are defined below here. ***/
\r
1011 static void prvAutoReloadTimerCallback( xTimerHandle pxExpiredTimer )
\r
1013 unsigned long ulTimerID;
\r
1015 ulTimerID = ( unsigned long ) pvTimerGetTimerID( pxExpiredTimer );
\r
1016 if( ulTimerID <= ( configTIMER_QUEUE_LENGTH + 1 ) )
\r
1018 ( ucAutoReloadTimerCounters[ ulTimerID ] )++;
\r
1022 /* The timer ID appears to be unexpected (invalid). */
\r
1023 xTestStatus = pdFAIL;
\r
1024 configASSERT( xTestStatus );
\r
1027 /*-----------------------------------------------------------*/
\r
1029 static void prvOneShotTimerCallback( xTimerHandle pxExpiredTimer )
\r
1031 /* The parameter is not used in this case as only one timer uses this
\r
1032 callback function. */
\r
1033 ( void ) pxExpiredTimer;
\r
1035 ucOneShotTimerCounter++;
\r
1037 /*-----------------------------------------------------------*/
\r
1039 static void prvISRAutoReloadTimerCallback( xTimerHandle pxExpiredTimer )
\r
1041 /* The parameter is not used in this case as only one timer uses this
\r
1042 callback function. */
\r
1043 ( void ) pxExpiredTimer;
\r
1045 ucISRAutoReloadTimerCounter++;
\r
1047 /*-----------------------------------------------------------*/
\r
1049 static void prvISROneShotTimerCallback( xTimerHandle pxExpiredTimer )
\r
1051 /* The parameter is not used in this case as only one timer uses this
\r
1052 callback function. */
\r
1053 ( void ) pxExpiredTimer;
\r
1055 ucISROneShotTimerCounter++;
\r
1057 /*-----------------------------------------------------------*/
\r