2 FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
68 * Tests the behaviour of timers. Some timers are created before the scheduler
\r
69 * is started, and some after.
\r
72 /* Standard includes. */
\r
75 /* Scheduler include files. */
\r
76 #include "FreeRTOS.h"
\r
80 /* Demo program include files. */
\r
81 #include "TimerDemo.h"
\r
83 #if ( configTIMER_TASK_PRIORITY < 1 )
\r
84 #error configTIMER_TASK_PRIORITY must be set to at least 1 for this test/demo to function correctly.
\r
87 #define tmrdemoDONT_BLOCK ( ( TickType_t ) 0 )
\r
88 #define tmrdemoONE_SHOT_TIMER_PERIOD ( xBasePeriod * ( TickType_t ) 3 )
\r
89 #define trmdemoNUM_TIMER_RESETS ( ( uint8_t ) 10 )
\r
91 /*-----------------------------------------------------------*/
\r
93 /* The callback functions used by the timers. These each increment a counter
\r
94 to indicate which timer has expired. The auto-reload timers that are used by
\r
95 the test task (as opposed to being used from an ISR) all share the same
\r
96 prvAutoReloadTimerCallback() callback function, and use the ID of the
\r
97 pxExpiredTimer parameter passed into that function to know which counter to
\r
98 increment. The other timers all have their own unique callback function and
\r
99 simply increment their counters without using the callback function parameter. */
\r
100 static void prvAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer );
\r
101 static void prvOneShotTimerCallback( TimerHandle_t pxExpiredTimer );
\r
102 static void prvTimerTestTask( void *pvParameters );
\r
103 static void prvISRAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer );
\r
104 static void prvISROneShotTimerCallback( TimerHandle_t pxExpiredTimer );
\r
106 /* The test functions used by the timer test task. These manipulate the auto
\r
107 reload and one shot timers in various ways, then delay, then inspect the timers
\r
108 to ensure they have behaved as expected. */
\r
109 static void prvTest1_CreateTimersWithoutSchedulerRunning( void );
\r
110 static void prvTest2_CheckTaskAndTimersInitialState( void );
\r
111 static void prvTest3_CheckAutoReloadExpireRates( void );
\r
112 static void prvTest4_CheckAutoReloadTimersCanBeStopped( void );
\r
113 static void prvTest5_CheckBasicOneShotTimerBehaviour( void );
\r
114 static void prvTest6_CheckAutoReloadResetBehaviour( void );
\r
115 static void prvResetStartConditionsForNextIteration( void );
\r
117 /*-----------------------------------------------------------*/
\r
119 /* Flag that will be latched to pdFAIL should any unexpected behaviour be
\r
120 detected in any of the demo tests. */
\r
121 static volatile BaseType_t xTestStatus = pdPASS;
\r
123 /* Counter that is incremented on each cycle of a test. This is used to
\r
124 detect a stalled task - a test that is no longer running. */
\r
125 static volatile uint32_t ulLoopCounter = 0;
\r
127 /* A set of auto reload timers - each of which use the same callback function.
\r
128 The callback function uses the timer ID to index into, and then increment, a
\r
129 counter in the ucAutoReloadTimerCounters[] array. The auto reload timers
\r
130 referenced from xAutoReloadTimers[] are used by the prvTimerTestTask task. */
\r
131 static TimerHandle_t xAutoReloadTimers[ configTIMER_QUEUE_LENGTH + 1 ] = { 0 };
\r
132 static uint8_t ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH + 1 ] = { 0 };
\r
134 /* The one shot timer is configured to use a callback function that increments
\r
135 ucOneShotTimerCounter each time it gets called. */
\r
136 static TimerHandle_t xOneShotTimer = NULL;
\r
137 static uint8_t ucOneShotTimerCounter = ( uint8_t ) 0;
\r
139 /* The ISR reload timer is controlled from the tick hook to exercise the timer
\r
140 API functions that can be used from an ISR. It is configured to increment
\r
141 ucISRReloadTimerCounter each time its callback function is executed. */
\r
142 static TimerHandle_t xISRAutoReloadTimer = NULL;
\r
143 static uint8_t ucISRAutoReloadTimerCounter = ( uint8_t ) 0;
\r
145 /* The ISR one shot timer is controlled from the tick hook to exercise the timer
\r
146 API functions that can be used from an ISR. It is configured to increment
\r
147 ucISRReloadTimerCounter each time its callback function is executed. */
\r
148 static TimerHandle_t xISROneShotTimer = NULL;
\r
149 static uint8_t ucISROneShotTimerCounter = ( uint8_t ) 0;
\r
151 /* The period of all the timers are a multiple of the base period. The base
\r
152 period is configured by the parameter to vStartTimerDemoTask(). */
\r
153 static TickType_t xBasePeriod = 0;
\r
155 /*-----------------------------------------------------------*/
\r
157 void vStartTimerDemoTask( TickType_t xBasePeriodIn )
\r
159 /* Start with the timer and counter arrays clear - this is only necessary
\r
160 where the compiler does not clear them automatically on start up. */
\r
161 memset( ucAutoReloadTimerCounters, 0x00, sizeof( ucAutoReloadTimerCounters ) );
\r
162 memset( xAutoReloadTimers, 0x00, sizeof( xAutoReloadTimers ) );
\r
164 /* Store the period from which all the timer periods will be generated from
\r
166 xBasePeriod = xBasePeriodIn;
\r
168 /* Create a set of timers for use by this demo/test. */
\r
169 prvTest1_CreateTimersWithoutSchedulerRunning();
\r
171 /* Create the task that will control and monitor the timers. This is
\r
172 created at a lower priority than the timer service task to ensure, as
\r
173 far as it is concerned, commands on timers are actioned immediately
\r
174 (sending a command to the timer service task will unblock the timer service
\r
175 task, which will then preempt this task). */
\r
176 if( xTestStatus != pdFAIL )
\r
178 xTaskCreate( prvTimerTestTask, "Tmr Tst", configMINIMAL_STACK_SIZE, NULL, configTIMER_TASK_PRIORITY - 1, NULL );
\r
181 /*-----------------------------------------------------------*/
\r
183 static void prvTimerTestTask( void *pvParameters )
\r
185 ( void ) pvParameters;
\r
187 /* Create a one-shot timer for use later on in this test. */
\r
188 xOneShotTimer = xTimerCreate( "Oneshot Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
\r
189 tmrdemoONE_SHOT_TIMER_PERIOD, /* The period for the timer. */
\r
190 pdFALSE, /* Don't auto-reload - hence a one shot timer. */
\r
191 ( void * ) 0, /* The timer identifier. In this case this is not used as the timer has its own callback. */
\r
192 prvOneShotTimerCallback ); /* The callback to be called when the timer expires. */
\r
194 if( xOneShotTimer == NULL )
\r
196 xTestStatus = pdFAIL;
\r
197 configASSERT( xTestStatus );
\r
201 /* Ensure all the timers are in their expected initial state. This
\r
202 depends on the timer service task having a higher priority than this task. */
\r
203 prvTest2_CheckTaskAndTimersInitialState();
\r
207 /* Check the auto reload timers expire at the expected/correct rates. */
\r
208 prvTest3_CheckAutoReloadExpireRates();
\r
210 /* Check the auto reload timers can be stopped correctly, and correctly
\r
211 report their state. */
\r
212 prvTest4_CheckAutoReloadTimersCanBeStopped();
\r
214 /* Check the one shot timer only calls its callback once after it has been
\r
215 started, and that it reports its state correctly. */
\r
216 prvTest5_CheckBasicOneShotTimerBehaviour();
\r
218 /* Check timer reset behaviour. */
\r
219 prvTest6_CheckAutoReloadResetBehaviour();
\r
221 /* Start the timers again to restart all the tests over again. */
\r
222 prvResetStartConditionsForNextIteration();
\r
225 /*-----------------------------------------------------------*/
\r
227 /* This is called to check that the created task is still running and has not
\r
228 detected any errors. */
\r
229 BaseType_t xAreTimerDemoTasksStillRunning( TickType_t xCycleFrequency )
\r
231 static uint32_t ulLastLoopCounter = 0UL;
\r
232 TickType_t xMaxBlockTimeUsedByTheseTests, xLoopCounterIncrementTimeMax;
\r
233 static TickType_t xIterationsWithoutCounterIncrement = ( TickType_t ) 0, xLastCycleFrequency;
\r
235 if( xLastCycleFrequency != xCycleFrequency )
\r
237 /* The cycle frequency has probably become much faster due to an error
\r
238 elsewhere. Start counting Iterations again. */
\r
239 xIterationsWithoutCounterIncrement = ( TickType_t ) 0;
\r
240 xLastCycleFrequency = xCycleFrequency;
\r
243 /* Calculate the maximum number of times that it is permissible for this
\r
244 function to be called without ulLoopCounter being incremented. This is
\r
245 necessary because the tests in this file block for extended periods, and the
\r
246 block period might be longer than the time between calls to this function. */
\r
247 xMaxBlockTimeUsedByTheseTests = ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod;
\r
248 xLoopCounterIncrementTimeMax = ( xMaxBlockTimeUsedByTheseTests / xCycleFrequency ) + 1;
\r
250 /* If the demo task is still running then the loop counter is expected to
\r
251 have incremented every xLoopCounterIncrementTimeMax calls. */
\r
252 if( ulLastLoopCounter == ulLoopCounter )
\r
254 xIterationsWithoutCounterIncrement++;
\r
255 if( xIterationsWithoutCounterIncrement > xLoopCounterIncrementTimeMax )
\r
257 /* The tests appear to be no longer running (stalled). */
\r
258 xTestStatus = pdFAIL;
\r
259 configASSERT( xTestStatus );
\r
264 /* ulLoopCounter changed, so the count of times this function was called
\r
265 without a change can be reset to zero. */
\r
266 xIterationsWithoutCounterIncrement = ( TickType_t ) 0;
\r
269 ulLastLoopCounter = ulLoopCounter;
\r
271 /* Errors detected in the task itself will have latched xTestStatus
\r
274 return xTestStatus;
\r
276 /*-----------------------------------------------------------*/
\r
278 static void prvTest1_CreateTimersWithoutSchedulerRunning( void )
\r
282 for( xTimer = 0; xTimer < configTIMER_QUEUE_LENGTH; xTimer++ )
\r
284 /* As the timer queue is not yet full, it should be possible to both create
\r
285 and start a timer. These timers are being started before the scheduler has
\r
286 been started, so their block times should get set to zero within the timer
\r
288 xAutoReloadTimers[ xTimer ] = xTimerCreate( "FR Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
\r
289 ( ( xTimer + ( TickType_t ) 1 ) * xBasePeriod ),/* The period for the timer. The plus 1 ensures a period of zero is not specified. */
\r
290 pdTRUE, /* Auto-reload is set to true. */
\r
291 ( void * ) xTimer, /* An identifier for the timer as all the auto reload timers use the same callback. */
\r
292 prvAutoReloadTimerCallback ); /* The callback to be called when the timer expires. */
\r
294 configASSERT( strcmp( pcTimerGetTimerName( xAutoReloadTimers[ xTimer ] ), "FR Timer" ) == 0 );
\r
296 if( xAutoReloadTimers[ xTimer ] == NULL )
\r
298 xTestStatus = pdFAIL;
\r
299 configASSERT( xTestStatus );
\r
303 /* The scheduler has not yet started, so the block period of
\r
304 portMAX_DELAY should just get set to zero in xTimerStart(). Also,
\r
305 the timer queue is not yet full so xTimerStart() should return
\r
307 if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) != pdPASS )
\r
309 xTestStatus = pdFAIL;
\r
310 configASSERT( xTestStatus );
\r
315 /* The timers queue should now be full, so it should be possible to create
\r
316 another timer, but not possible to start it (the timer queue will not get
\r
317 drained until the scheduler has been started. */
\r
318 xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] = xTimerCreate( "FR Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
\r
319 ( configTIMER_QUEUE_LENGTH * xBasePeriod ), /* The period for the timer. */
\r
320 pdTRUE, /* Auto-reload is set to true. */
\r
321 ( void * ) xTimer, /* An identifier for the timer as all the auto reload timers use the same callback. */
\r
322 prvAutoReloadTimerCallback ); /* The callback executed when the timer expires. */
\r
324 if( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] == NULL )
\r
326 xTestStatus = pdFAIL;
\r
327 configASSERT( xTestStatus );
\r
331 if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) == pdPASS )
\r
333 /* This time it would not be expected that the timer could be
\r
334 started at this point. */
\r
335 xTestStatus = pdFAIL;
\r
336 configASSERT( xTestStatus );
\r
340 /* Create the timers that are used from the tick interrupt to test the timer
\r
341 API functions that can be called from an ISR. */
\r
342 xISRAutoReloadTimer = xTimerCreate( "ISR AR", /* The text name given to the timer. */
\r
343 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
344 pdTRUE, /* This is an auto reload timer. */
\r
345 ( void * ) NULL, /* The identifier is not required. */
\r
346 prvISRAutoReloadTimerCallback ); /* The callback that is executed when the timer expires. */
\r
348 xISROneShotTimer = xTimerCreate( "ISR OS", /* The text name given to the timer. */
\r
349 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
350 pdFALSE, /* This is a one shot timer. */
\r
351 ( void * ) NULL, /* The identifier is not required. */
\r
352 prvISROneShotTimerCallback ); /* The callback that is executed when the timer expires. */
\r
354 if( ( xISRAutoReloadTimer == NULL ) || ( xISROneShotTimer == NULL ) )
\r
356 xTestStatus = pdFAIL;
\r
357 configASSERT( xTestStatus );
\r
360 /*-----------------------------------------------------------*/
\r
362 static void prvTest2_CheckTaskAndTimersInitialState( void )
\r
366 /* Ensure all the timers are in their expected initial state. This depends
\r
367 on the timer service task having a higher priority than this task.
\r
369 auto reload timers 0 to ( configTIMER_QUEUE_LENGTH - 1 ) should now be active,
\r
370 and auto reload timer configTIMER_QUEUE_LENGTH should not yet be active (it
\r
371 could not be started prior to the scheduler being started when it was
\r
373 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
375 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
377 xTestStatus = pdFAIL;
\r
378 configASSERT( xTestStatus );
\r
382 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] ) != pdFALSE )
\r
384 xTestStatus = pdFAIL;
\r
385 configASSERT( xTestStatus );
\r
388 /*-----------------------------------------------------------*/
\r
390 static void prvTest3_CheckAutoReloadExpireRates( void )
\r
392 uint8_t ucMaxAllowableValue, ucMinAllowableValue, ucTimer;
\r
393 TickType_t xBlockPeriod, xTimerPeriod, xExpectedNumber;
\r
395 /* Check the auto reload timers expire at the expected rates. */
\r
398 /* Delaying for configTIMER_QUEUE_LENGTH * xBasePeriod ticks should allow
\r
399 all the auto reload timers to expire at least once. */
\r
400 xBlockPeriod = ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod;
\r
401 vTaskDelay( xBlockPeriod );
\r
403 /* Check that all the auto reload timers have called their callback
\r
404 function the expected number of times. */
\r
405 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
407 /* The expected number of expiries is equal to the block period divided
\r
408 by the timer period. */
\r
409 xTimerPeriod = ( ( ( TickType_t ) ucTimer + ( TickType_t ) 1 ) * xBasePeriod );
\r
410 xExpectedNumber = xBlockPeriod / xTimerPeriod;
\r
412 ucMaxAllowableValue = ( ( uint8_t ) xExpectedNumber ) ;
\r
413 ucMinAllowableValue = ( uint8_t ) ( ( uint8_t ) xExpectedNumber - ( uint8_t ) 1 ); /* Weird casting to try and please all compilers. */
\r
415 if( ( ucAutoReloadTimerCounters[ ucTimer ] < ucMinAllowableValue ) ||
\r
416 ( ucAutoReloadTimerCounters[ ucTimer ] > ucMaxAllowableValue )
\r
419 xTestStatus = pdFAIL;
\r
420 configASSERT( xTestStatus );
\r
424 if( xTestStatus == pdPASS )
\r
426 /* No errors have been reported so increment the loop counter so the
\r
427 check task knows this task is still running. */
\r
431 /*-----------------------------------------------------------*/
\r
433 static void prvTest4_CheckAutoReloadTimersCanBeStopped( void )
\r
437 /* Check the auto reload timers can be stopped correctly, and correctly
\r
438 report their state. */
\r
440 /* Stop all the active timers. */
\r
441 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
443 /* The timer has not been stopped yet! */
\r
444 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
446 xTestStatus = pdFAIL;
\r
447 configASSERT( xTestStatus );
\r
450 /* Now stop the timer. This will appear to happen immediately to
\r
451 this task because this task is running at a priority below the
\r
452 timer service task. */
\r
453 xTimerStop( xAutoReloadTimers[ ucTimer ], tmrdemoDONT_BLOCK );
\r
455 /* The timer should now be inactive. */
\r
456 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )
\r
458 xTestStatus = pdFAIL;
\r
459 configASSERT( xTestStatus );
\r
463 taskENTER_CRITICAL();
\r
465 /* The timer in array position configTIMER_QUEUE_LENGTH should not
\r
466 be active. The critical section is used to ensure the timer does
\r
467 not call its callback between the next line running and the array
\r
468 being cleared back to zero, as that would mask an error condition. */
\r
469 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH ] != ( uint8_t ) 0 )
\r
471 xTestStatus = pdFAIL;
\r
472 configASSERT( xTestStatus );
\r
475 /* Clear the timer callback count. */
\r
476 memset( ( void * ) ucAutoReloadTimerCounters, 0, sizeof( ucAutoReloadTimerCounters ) );
\r
478 taskEXIT_CRITICAL();
\r
480 /* The timers are now all inactive, so this time, after delaying, none
\r
481 of the callback counters should have incremented. */
\r
482 vTaskDelay( ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod );
\r
483 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
485 if( ucAutoReloadTimerCounters[ ucTimer ] != ( uint8_t ) 0 )
\r
487 xTestStatus = pdFAIL;
\r
488 configASSERT( xTestStatus );
\r
492 if( xTestStatus == pdPASS )
\r
494 /* No errors have been reported so increment the loop counter so
\r
495 the check task knows this task is still running. */
\r
499 /*-----------------------------------------------------------*/
\r
501 static void prvTest5_CheckBasicOneShotTimerBehaviour( void )
\r
503 /* Check the one shot timer only calls its callback once after it has been
\r
504 started, and that it reports its state correctly. */
\r
506 /* The one shot timer should not be active yet. */
\r
507 if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )
\r
509 xTestStatus = pdFAIL;
\r
510 configASSERT( xTestStatus );
\r
513 if( ucOneShotTimerCounter != ( uint8_t ) 0 )
\r
515 xTestStatus = pdFAIL;
\r
516 configASSERT( xTestStatus );
\r
519 /* Start the one shot timer and check that it reports its state correctly. */
\r
520 xTimerStart( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
521 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
523 xTestStatus = pdFAIL;
\r
524 configASSERT( xTestStatus );
\r
527 /* Delay for three times as long as the one shot timer period, then check
\r
528 to ensure it has only called its callback once, and is now not in the
\r
530 vTaskDelay( tmrdemoONE_SHOT_TIMER_PERIOD * ( TickType_t ) 3 );
\r
532 if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )
\r
534 xTestStatus = pdFAIL;
\r
535 configASSERT( xTestStatus );
\r
538 if( ucOneShotTimerCounter != ( uint8_t ) 1 )
\r
540 xTestStatus = pdFAIL;
\r
541 configASSERT( xTestStatus );
\r
545 /* Reset the one shot timer callback count. */
\r
546 ucOneShotTimerCounter = ( uint8_t ) 0;
\r
549 if( xTestStatus == pdPASS )
\r
551 /* No errors have been reported so increment the loop counter so the
\r
552 check task knows this task is still running. */
\r
556 /*-----------------------------------------------------------*/
\r
558 static void prvTest6_CheckAutoReloadResetBehaviour( void )
\r
562 /* Check timer reset behaviour. */
\r
564 /* Restart the one shot timer and check it reports its status correctly. */
\r
565 xTimerStart( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
566 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
568 xTestStatus = pdFAIL;
\r
569 configASSERT( xTestStatus );
\r
572 /* Restart one of the auto reload timers and check that it reports its
\r
573 status correctly. */
\r
574 xTimerStart( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
575 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
577 xTestStatus = pdFAIL;
\r
578 configASSERT( xTestStatus );
\r
581 for( ucTimer = 0; ucTimer < trmdemoNUM_TIMER_RESETS; ucTimer++ )
\r
583 /* Delay for half as long as the one shot timer period, then reset it.
\r
584 It should never expire while this is done, so its callback count should
\r
585 never increment. */
\r
586 vTaskDelay( tmrdemoONE_SHOT_TIMER_PERIOD / 2 );
\r
588 /* Check both running timers are still active, but have not called their
\r
589 callback functions. */
\r
590 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
592 xTestStatus = pdFAIL;
\r
593 configASSERT( xTestStatus );
\r
596 if( ucOneShotTimerCounter != ( uint8_t ) 0 )
\r
598 xTestStatus = pdFAIL;
\r
599 configASSERT( xTestStatus );
\r
602 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
604 xTestStatus = pdFAIL;
\r
605 configASSERT( xTestStatus );
\r
608 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] != ( uint8_t ) 0 )
\r
610 xTestStatus = pdFAIL;
\r
611 configASSERT( xTestStatus );
\r
614 /* Reset both running timers. */
\r
615 xTimerReset( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
616 xTimerReset( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
618 if( xTestStatus == pdPASS )
\r
620 /* No errors have been reported so increment the loop counter so
\r
621 the check task knows this task is still running. */
\r
626 /* Finally delay long enough for both running timers to expire. */
\r
627 vTaskDelay( ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod );
\r
629 /* The timers were not reset during the above delay period so should now
\r
630 both have called their callback functions. */
\r
631 if( ucOneShotTimerCounter != ( uint8_t ) 1 )
\r
633 xTestStatus = pdFAIL;
\r
634 configASSERT( xTestStatus );
\r
637 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] == 0 )
\r
639 xTestStatus = pdFAIL;
\r
640 configASSERT( xTestStatus );
\r
643 /* The one shot timer should no longer be active, while the auto reload
\r
644 timer should still be active. */
\r
645 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
647 xTestStatus = pdFAIL;
\r
648 configASSERT( xTestStatus );
\r
651 if( xTimerIsTimerActive( xOneShotTimer ) == pdTRUE )
\r
653 xTestStatus = pdFAIL;
\r
654 configASSERT( xTestStatus );
\r
657 /* Stop the auto reload timer again. */
\r
658 xTimerStop( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
660 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) != pdFALSE )
\r
662 xTestStatus = pdFAIL;
\r
663 configASSERT( xTestStatus );
\r
666 /* Clear the timer callback counts, ready for another iteration of these
\r
668 ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] = ( uint8_t ) 0;
\r
669 ucOneShotTimerCounter = ( uint8_t ) 0;
\r
671 if( xTestStatus == pdPASS )
\r
673 /* No errors have been reported so increment the loop counter so the check
\r
674 task knows this task is still running. */
\r
678 /*-----------------------------------------------------------*/
\r
680 static void prvResetStartConditionsForNextIteration( void )
\r
684 /* Start the timers again to start all the tests over again. */
\r
686 /* Start the timers again. */
\r
687 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
689 /* The timer has not been started yet! */
\r
690 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )
\r
692 xTestStatus = pdFAIL;
\r
693 configASSERT( xTestStatus );
\r
696 /* Now start the timer. This will appear to happen immediately to
\r
697 this task because this task is running at a priority below the timer
\r
699 xTimerStart( xAutoReloadTimers[ ucTimer ], tmrdemoDONT_BLOCK );
\r
701 /* The timer should now be active. */
\r
702 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
704 xTestStatus = pdFAIL;
\r
705 configASSERT( xTestStatus );
\r
709 if( xTestStatus == pdPASS )
\r
711 /* No errors have been reported so increment the loop counter so the
\r
712 check task knows this task is still running. */
\r
716 /*-----------------------------------------------------------*/
\r
718 void vTimerPeriodicISRTests( void )
\r
720 static TickType_t uxTick = ( TickType_t ) -1;
\r
722 #if( configTIMER_TASK_PRIORITY != ( configMAX_PRIORITIES - 1 ) )
\r
723 /* The timer service task is not the highest priority task, so it cannot
\r
724 be assumed that timings will be exact. Timers should never call their
\r
725 callback before their expiry time, but a margin is permissible for calling
\r
726 their callback after their expiry time. If exact timing is required then
\r
727 configTIMER_TASK_PRIORITY must be set to ensure the timer service task
\r
728 is the highest priority task in the system.
\r
730 This function is called from the tick hook. The tick hook is called
\r
731 even when the scheduler is suspended. Therefore it is possible that the
\r
732 uxTick count maintained in this function is temporarily ahead of the tick
\r
733 count maintained by the kernel. When this is the case a message posted from
\r
734 this function will assume a time stamp in advance of the real time stamp,
\r
735 which can result in a timer being processed before this function expects it
\r
736 to. For example, if the kernel's tick count was 100, and uxTick was 102,
\r
737 then this function will not expect the timer to have expired until the
\r
738 kernel's tick count is (102 + xBasePeriod), whereas in reality the timer
\r
739 will expire when the kernel's tick count is (100 + xBasePeriod). For this
\r
740 reason xMargin is used as an allowable margin for premature timer expiries
\r
741 as well as late timer expiries. */
\r
742 const TickType_t xMargin = 6;
\r
745 /* Windows is not real real time. */
\r
746 const TickType_t xMargin = 10;
\r
748 const TickType_t xMargin = 4;
\r
749 #endif /* _WINDOWS_ */
\r
757 /* The timers will have been created, but not started. Start them now
\r
758 by setting their period. */
\r
759 ucISRAutoReloadTimerCounter = 0;
\r
760 ucISROneShotTimerCounter = 0;
\r
762 /* It is possible that the timer task has not yet made room in the
\r
763 timer queue. If the timers cannot be started then reset uxTick so
\r
764 another attempt is made later. */
\r
765 uxTick = ( TickType_t ) -1;
\r
767 /* Try starting first timer. */
\r
768 if( xTimerChangePeriodFromISR( xISRAutoReloadTimer, xBasePeriod, NULL ) == pdPASS )
\r
770 /* First timer was started, try starting the second timer. */
\r
771 if( xTimerChangePeriodFromISR( xISROneShotTimer, xBasePeriod, NULL ) == pdPASS )
\r
773 /* Both timers were started, so set the uxTick back to its
\r
779 /* Second timer could not be started, so stop the first one
\r
781 xTimerStopFromISR( xISRAutoReloadTimer, NULL );
\r
785 else if( uxTick == ( xBasePeriod - xMargin ) )
\r
787 /* Neither timer should have expired yet. */
\r
788 if( ( ucISRAutoReloadTimerCounter != 0 ) || ( ucISROneShotTimerCounter != 0 ) )
\r
790 xTestStatus = pdFAIL;
\r
791 configASSERT( xTestStatus );
\r
794 else if( uxTick == ( xBasePeriod + xMargin ) )
\r
796 /* Both timers should now have expired once. The auto reload timer will
\r
797 still be active, but the one shot timer should now have stopped. */
\r
798 if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )
\r
800 xTestStatus = pdFAIL;
\r
801 configASSERT( xTestStatus );
\r
804 else if( uxTick == ( ( 2 * xBasePeriod ) - xMargin ) )
\r
806 /* The auto reload timer will still be active, but the one shot timer
\r
807 should now have stopped - however, at this time neither of the timers
\r
808 should have expired again since the last test. */
\r
809 if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )
\r
811 xTestStatus = pdFAIL;
\r
812 configASSERT( xTestStatus );
\r
815 else if( uxTick == ( ( 2 * xBasePeriod ) + xMargin ) )
\r
817 /* The auto reload timer will still be active, but the one shot timer
\r
818 should now have stopped. At this time the auto reload timer should have
\r
819 expired again, but the one shot timer count should not have changed. */
\r
820 if( ucISRAutoReloadTimerCounter != 2 )
\r
822 xTestStatus = pdFAIL;
\r
823 configASSERT( xTestStatus );
\r
826 if( ucISROneShotTimerCounter != 1 )
\r
828 xTestStatus = pdFAIL;
\r
829 configASSERT( xTestStatus );
\r
832 else if( uxTick == ( ( 2 * xBasePeriod ) + ( xBasePeriod >> ( TickType_t ) 2U ) ) )
\r
834 /* The auto reload timer will still be active, but the one shot timer
\r
835 should now have stopped. Again though, at this time, neither timer call
\r
836 back should have been called since the last test. */
\r
837 if( ucISRAutoReloadTimerCounter != 2 )
\r
839 xTestStatus = pdFAIL;
\r
840 configASSERT( xTestStatus );
\r
843 if( ucISROneShotTimerCounter != 1 )
\r
845 xTestStatus = pdFAIL;
\r
846 configASSERT( xTestStatus );
\r
849 else if( uxTick == ( 3 * xBasePeriod ) )
\r
851 /* Start the one shot timer again. */
\r
852 xTimerStartFromISR( xISROneShotTimer, NULL );
\r
854 else if( uxTick == ( ( 3 * xBasePeriod ) + xMargin ) )
\r
856 /* The auto reload timer and one shot timer will be active. At
\r
857 this time the auto reload timer should have expired again, but the one
\r
858 shot timer count should not have changed yet. */
\r
859 if( ucISRAutoReloadTimerCounter != 3 )
\r
861 xTestStatus = pdFAIL;
\r
862 configASSERT( xTestStatus );
\r
865 if( ucISROneShotTimerCounter != 1 )
\r
867 xTestStatus = pdFAIL;
\r
868 configASSERT( xTestStatus );
\r
871 /* Now stop the auto reload timer. The one shot timer was started
\r
872 a few ticks ago. */
\r
873 xTimerStopFromISR( xISRAutoReloadTimer, NULL );
\r
875 else if( uxTick == ( 4 * ( xBasePeriod - xMargin ) ) )
\r
877 /* The auto reload timer is now stopped, and the one shot timer is
\r
878 active, but at this time neither timer should have expired since the
\r
880 if( ucISRAutoReloadTimerCounter != 3 )
\r
882 xTestStatus = pdFAIL;
\r
883 configASSERT( xTestStatus );
\r
886 if( ucISROneShotTimerCounter != 1 )
\r
888 xTestStatus = pdFAIL;
\r
889 configASSERT( xTestStatus );
\r
892 else if( uxTick == ( ( 4 * xBasePeriod ) + xMargin ) )
\r
894 /* The auto reload timer is now stopped, and the one shot timer is
\r
895 active. The one shot timer should have expired again, but the auto
\r
896 reload timer should not have executed its callback. */
\r
897 if( ucISRAutoReloadTimerCounter != 3 )
\r
899 xTestStatus = pdFAIL;
\r
900 configASSERT( xTestStatus );
\r
903 if( ucISROneShotTimerCounter != 2 )
\r
905 xTestStatus = pdFAIL;
\r
906 configASSERT( xTestStatus );
\r
909 else if( uxTick == ( 8 * xBasePeriod ) )
\r
911 /* The auto reload timer is now stopped, and the one shot timer has
\r
912 already expired and then stopped itself. Both callback counters should
\r
913 not have incremented since the last test. */
\r
914 if( ucISRAutoReloadTimerCounter != 3 )
\r
916 xTestStatus = pdFAIL;
\r
917 configASSERT( xTestStatus );
\r
920 if( ucISROneShotTimerCounter != 2 )
\r
922 xTestStatus = pdFAIL;
\r
923 configASSERT( xTestStatus );
\r
926 /* Now reset the one shot timer. */
\r
927 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
929 else if( uxTick == ( ( 9 * xBasePeriod ) - xMargin ) )
\r
931 /* Only the one shot timer should be running, but it should not have
\r
932 expired since the last test. Check the callback counters have not
\r
933 incremented, then reset the one shot timer again. */
\r
934 if( ucISRAutoReloadTimerCounter != 3 )
\r
936 xTestStatus = pdFAIL;
\r
937 configASSERT( xTestStatus );
\r
940 if( ucISROneShotTimerCounter != 2 )
\r
942 xTestStatus = pdFAIL;
\r
943 configASSERT( xTestStatus );
\r
946 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
948 else if( uxTick == ( ( 10 * xBasePeriod ) - ( 2 * xMargin ) ) )
\r
950 /* Only the one shot timer should be running, but it should not have
\r
951 expired since the last test. Check the callback counters have not
\r
952 incremented, then reset the one shot timer again. */
\r
953 if( ucISRAutoReloadTimerCounter != 3 )
\r
955 xTestStatus = pdFAIL;
\r
956 configASSERT( xTestStatus );
\r
959 if( ucISROneShotTimerCounter != 2 )
\r
961 xTestStatus = pdFAIL;
\r
962 configASSERT( xTestStatus );
\r
965 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
967 else if( uxTick == ( ( 11 * xBasePeriod ) - ( 3 * xMargin ) ) )
\r
969 /* Only the one shot timer should be running, but it should not have
\r
970 expired since the last test. Check the callback counters have not
\r
971 incremented, then reset the one shot timer once again. */
\r
972 if( ucISRAutoReloadTimerCounter != 3 )
\r
974 xTestStatus = pdFAIL;
\r
975 configASSERT( xTestStatus );
\r
978 if( ucISROneShotTimerCounter != 2 )
\r
980 xTestStatus = pdFAIL;
\r
981 configASSERT( xTestStatus );
\r
984 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
986 else if( uxTick == ( ( 12 * xBasePeriod ) - ( 2 * xMargin ) ) )
\r
988 /* Only the one shot timer should have been running and this time it
\r
989 should have expired. Check its callback count has been incremented.
\r
990 The auto reload timer is still not running so should still have the same
\r
991 count value. This time the one shot timer is not reset so should not
\r
992 restart from its expiry period again. */
\r
993 if( ucISRAutoReloadTimerCounter != 3 )
\r
995 xTestStatus = pdFAIL;
\r
996 configASSERT( xTestStatus );
\r
999 if( ucISROneShotTimerCounter != 3 )
\r
1001 xTestStatus = pdFAIL;
\r
1002 configASSERT( xTestStatus );
\r
1005 else if( uxTick == ( 15 * xBasePeriod ) )
\r
1007 /* Neither timer should be running now. Check neither callback count
\r
1008 has incremented, then go back to the start to run these tests all
\r
1010 if( ucISRAutoReloadTimerCounter != 3 )
\r
1012 xTestStatus = pdFAIL;
\r
1013 configASSERT( xTestStatus );
\r
1016 if( ucISROneShotTimerCounter != 3 )
\r
1018 xTestStatus = pdFAIL;
\r
1019 configASSERT( xTestStatus );
\r
1022 uxTick = ( TickType_t ) -1;
\r
1025 /*-----------------------------------------------------------*/
\r
1027 /*** Timer callback functions are defined below here. ***/
\r
1029 static void prvAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer )
\r
1031 uint32_t ulTimerID;
\r
1033 ulTimerID = ( uint32_t ) pvTimerGetTimerID( pxExpiredTimer );
\r
1034 if( ulTimerID <= ( configTIMER_QUEUE_LENGTH + 1 ) )
\r
1036 ( ucAutoReloadTimerCounters[ ulTimerID ] )++;
\r
1040 /* The timer ID appears to be unexpected (invalid). */
\r
1041 xTestStatus = pdFAIL;
\r
1042 configASSERT( xTestStatus );
\r
1045 /*-----------------------------------------------------------*/
\r
1047 static void prvOneShotTimerCallback( TimerHandle_t pxExpiredTimer )
\r
1049 /* The parameter is not used in this case as only one timer uses this
\r
1050 callback function. */
\r
1051 ( void ) pxExpiredTimer;
\r
1053 ucOneShotTimerCounter++;
\r
1055 /*-----------------------------------------------------------*/
\r
1057 static void prvISRAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer )
\r
1059 /* The parameter is not used in this case as only one timer uses this
\r
1060 callback function. */
\r
1061 ( void ) pxExpiredTimer;
\r
1063 ucISRAutoReloadTimerCounter++;
\r
1065 /*-----------------------------------------------------------*/
\r
1067 static void prvISROneShotTimerCallback( TimerHandle_t pxExpiredTimer )
\r
1069 /* The parameter is not used in this case as only one timer uses this
\r
1070 callback function. */
\r
1071 ( void ) pxExpiredTimer;
\r
1073 ucISROneShotTimerCounter++;
\r
1075 /*-----------------------------------------------------------*/
\r