2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
31 * Tests the behaviour of timers. Some timers are created before the scheduler
\r
32 * is started, and some after.
\r
35 /* Standard includes. */
\r
38 /* Scheduler include files. */
\r
39 #include "FreeRTOS.h"
\r
43 /* Demo program include files. */
\r
44 #include "TimerDemo.h"
\r
46 #if ( configTIMER_TASK_PRIORITY < 1 )
\r
47 #error configTIMER_TASK_PRIORITY must be set to at least 1 for this test/demo to function correctly.
\r
50 #define tmrdemoDONT_BLOCK ( ( TickType_t ) 0 )
\r
51 #define tmrdemoONE_SHOT_TIMER_PERIOD ( xBasePeriod * ( TickType_t ) 3 )
\r
52 #define trmdemoNUM_TIMER_RESETS ( ( uint8_t ) 10 )
\r
54 /*-----------------------------------------------------------*/
\r
56 /* The callback functions used by the timers. These each increment a counter
\r
57 to indicate which timer has expired. The auto-reload timers that are used by
\r
58 the test task (as opposed to being used from an ISR) all share the same
\r
59 prvAutoReloadTimerCallback() callback function, and use the ID of the
\r
60 pxExpiredTimer parameter passed into that function to know which counter to
\r
61 increment. The other timers all have their own unique callback function and
\r
62 simply increment their counters without using the callback function parameter. */
\r
63 static void prvAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer );
\r
64 static void prvOneShotTimerCallback( TimerHandle_t pxExpiredTimer );
\r
65 static void prvTimerTestTask( void *pvParameters );
\r
66 static void prvISRAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer );
\r
67 static void prvISROneShotTimerCallback( TimerHandle_t pxExpiredTimer );
\r
69 /* The test functions used by the timer test task. These manipulate the auto
\r
70 reload and one shot timers in various ways, then delay, then inspect the timers
\r
71 to ensure they have behaved as expected. */
\r
72 static void prvTest1_CreateTimersWithoutSchedulerRunning( void );
\r
73 static void prvTest2_CheckTaskAndTimersInitialState( void );
\r
74 static void prvTest3_CheckAutoReloadExpireRates( void );
\r
75 static void prvTest4_CheckAutoReloadTimersCanBeStopped( void );
\r
76 static void prvTest5_CheckBasicOneShotTimerBehaviour( void );
\r
77 static void prvTest6_CheckAutoReloadResetBehaviour( void );
\r
78 static void prvResetStartConditionsForNextIteration( void );
\r
80 /*-----------------------------------------------------------*/
\r
82 /* Flag that will be latched to pdFAIL should any unexpected behaviour be
\r
83 detected in any of the demo tests. */
\r
84 static volatile BaseType_t xTestStatus = pdPASS;
\r
86 /* Counter that is incremented on each cycle of a test. This is used to
\r
87 detect a stalled task - a test that is no longer running. */
\r
88 static volatile uint32_t ulLoopCounter = 0;
\r
90 /* A set of auto reload timers - each of which use the same callback function.
\r
91 The callback function uses the timer ID to index into, and then increment, a
\r
92 counter in the ucAutoReloadTimerCounters[] array. The auto reload timers
\r
93 referenced from xAutoReloadTimers[] are used by the prvTimerTestTask task. */
\r
94 static TimerHandle_t xAutoReloadTimers[ configTIMER_QUEUE_LENGTH + 1 ] = { 0 };
\r
95 static uint8_t ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH + 1 ] = { 0 };
\r
97 /* The one shot timer is configured to use a callback function that increments
\r
98 ucOneShotTimerCounter each time it gets called. */
\r
99 static TimerHandle_t xOneShotTimer = NULL;
\r
100 static uint8_t ucOneShotTimerCounter = ( uint8_t ) 0;
\r
102 /* The ISR reload timer is controlled from the tick hook to exercise the timer
\r
103 API functions that can be used from an ISR. It is configured to increment
\r
104 ucISRReloadTimerCounter each time its callback function is executed. */
\r
105 static TimerHandle_t xISRAutoReloadTimer = NULL;
\r
106 static uint8_t ucISRAutoReloadTimerCounter = ( uint8_t ) 0;
\r
108 /* The ISR one shot timer is controlled from the tick hook to exercise the timer
\r
109 API functions that can be used from an ISR. It is configured to increment
\r
110 ucISRReloadTimerCounter each time its callback function is executed. */
\r
111 static TimerHandle_t xISROneShotTimer = NULL;
\r
112 static uint8_t ucISROneShotTimerCounter = ( uint8_t ) 0;
\r
114 /* The period of all the timers are a multiple of the base period. The base
\r
115 period is configured by the parameter to vStartTimerDemoTask(). */
\r
116 static TickType_t xBasePeriod = 0;
\r
118 /*-----------------------------------------------------------*/
\r
120 void vStartTimerDemoTask( TickType_t xBasePeriodIn )
\r
122 /* Start with the timer and counter arrays clear - this is only necessary
\r
123 where the compiler does not clear them automatically on start up. */
\r
124 memset( ucAutoReloadTimerCounters, 0x00, sizeof( ucAutoReloadTimerCounters ) );
\r
125 memset( xAutoReloadTimers, 0x00, sizeof( xAutoReloadTimers ) );
\r
127 /* Store the period from which all the timer periods will be generated from
\r
129 xBasePeriod = xBasePeriodIn;
\r
131 /* Create a set of timers for use by this demo/test. */
\r
132 prvTest1_CreateTimersWithoutSchedulerRunning();
\r
134 /* Create the task that will control and monitor the timers. This is
\r
135 created at a lower priority than the timer service task to ensure, as
\r
136 far as it is concerned, commands on timers are actioned immediately
\r
137 (sending a command to the timer service task will unblock the timer service
\r
138 task, which will then preempt this task). */
\r
139 if( xTestStatus != pdFAIL )
\r
141 xTaskCreate( prvTimerTestTask, "Tmr Tst", configMINIMAL_STACK_SIZE, NULL, configTIMER_TASK_PRIORITY - 1, NULL );
\r
144 /*-----------------------------------------------------------*/
\r
146 static void prvTimerTestTask( void *pvParameters )
\r
148 ( void ) pvParameters;
\r
150 /* Create a one-shot timer for use later on in this test. */
\r
151 xOneShotTimer = xTimerCreate( "Oneshot Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
\r
152 tmrdemoONE_SHOT_TIMER_PERIOD, /* The period for the timer. */
\r
153 pdFALSE, /* Don't auto-reload - hence a one shot timer. */
\r
154 ( void * ) 0, /* The timer identifier. Initialise to 0, then increment each time it is called. */
\r
155 prvOneShotTimerCallback ); /* The callback to be called when the timer expires. */
\r
157 if( xOneShotTimer == NULL )
\r
159 xTestStatus = pdFAIL;
\r
160 configASSERT( xTestStatus );
\r
164 /* Ensure all the timers are in their expected initial state. This
\r
165 depends on the timer service task having a higher priority than this task. */
\r
166 prvTest2_CheckTaskAndTimersInitialState();
\r
170 /* Check the auto reload timers expire at the expected/correct rates. */
\r
171 prvTest3_CheckAutoReloadExpireRates();
\r
173 /* Check the auto reload timers can be stopped correctly, and correctly
\r
174 report their state. */
\r
175 prvTest4_CheckAutoReloadTimersCanBeStopped();
\r
177 /* Check the one shot timer only calls its callback once after it has been
\r
178 started, and that it reports its state correctly. */
\r
179 prvTest5_CheckBasicOneShotTimerBehaviour();
\r
181 /* Check timer reset behaviour. */
\r
182 prvTest6_CheckAutoReloadResetBehaviour();
\r
184 /* Start the timers again to restart all the tests over again. */
\r
185 prvResetStartConditionsForNextIteration();
\r
188 /*-----------------------------------------------------------*/
\r
190 /* This is called to check that the created task is still running and has not
\r
191 detected any errors. */
\r
192 BaseType_t xAreTimerDemoTasksStillRunning( TickType_t xCycleFrequency )
\r
194 static uint32_t ulLastLoopCounter = 0UL;
\r
195 TickType_t xMaxBlockTimeUsedByTheseTests, xLoopCounterIncrementTimeMax;
\r
196 static TickType_t xIterationsWithoutCounterIncrement = ( TickType_t ) 0, xLastCycleFrequency;
\r
198 if( xLastCycleFrequency != xCycleFrequency )
\r
200 /* The cycle frequency has probably become much faster due to an error
\r
201 elsewhere. Start counting Iterations again. */
\r
202 xIterationsWithoutCounterIncrement = ( TickType_t ) 0;
\r
203 xLastCycleFrequency = xCycleFrequency;
\r
206 /* Calculate the maximum number of times that it is permissible for this
\r
207 function to be called without ulLoopCounter being incremented. This is
\r
208 necessary because the tests in this file block for extended periods, and the
\r
209 block period might be longer than the time between calls to this function. */
\r
210 xMaxBlockTimeUsedByTheseTests = ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod;
\r
211 xLoopCounterIncrementTimeMax = ( xMaxBlockTimeUsedByTheseTests / xCycleFrequency ) + 1;
\r
213 /* If the demo task is still running then the loop counter is expected to
\r
214 have incremented every xLoopCounterIncrementTimeMax calls. */
\r
215 if( ulLastLoopCounter == ulLoopCounter )
\r
217 xIterationsWithoutCounterIncrement++;
\r
218 if( xIterationsWithoutCounterIncrement > xLoopCounterIncrementTimeMax )
\r
220 /* The tests appear to be no longer running (stalled). */
\r
221 xTestStatus = pdFAIL;
\r
222 configASSERT( xTestStatus );
\r
227 /* ulLoopCounter changed, so the count of times this function was called
\r
228 without a change can be reset to zero. */
\r
229 xIterationsWithoutCounterIncrement = ( TickType_t ) 0;
\r
232 ulLastLoopCounter = ulLoopCounter;
\r
234 /* Errors detected in the task itself will have latched xTestStatus
\r
237 return xTestStatus;
\r
239 /*-----------------------------------------------------------*/
\r
241 static void prvTest1_CreateTimersWithoutSchedulerRunning( void )
\r
245 for( xTimer = 0; xTimer < configTIMER_QUEUE_LENGTH; xTimer++ )
\r
247 /* As the timer queue is not yet full, it should be possible to both
\r
248 create and start a timer. These timers are being started before the
\r
249 scheduler has been started, so their block times should get set to zero
\r
250 within the timer API itself. */
\r
251 xAutoReloadTimers[ xTimer ] = xTimerCreate( "FR Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
\r
252 ( ( xTimer + ( TickType_t ) 1 ) * xBasePeriod ),/* The period for the timer. The plus 1 ensures a period of zero is not specified. */
\r
253 pdTRUE, /* Auto-reload is set to true. */
\r
254 ( void * ) xTimer, /* An identifier for the timer as all the auto reload timers use the same callback. */
\r
255 prvAutoReloadTimerCallback ); /* The callback to be called when the timer expires. */
\r
257 if( xAutoReloadTimers[ xTimer ] == NULL )
\r
259 xTestStatus = pdFAIL;
\r
260 configASSERT( xTestStatus );
\r
264 configASSERT( strcmp( pcTimerGetName( xAutoReloadTimers[ xTimer ] ), "FR Timer" ) == 0 );
\r
266 /* The scheduler has not yet started, so the block period of
\r
267 portMAX_DELAY should just get set to zero in xTimerStart(). Also,
\r
268 the timer queue is not yet full so xTimerStart() should return
\r
270 if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) != pdPASS )
\r
272 xTestStatus = pdFAIL;
\r
273 configASSERT( xTestStatus );
\r
278 /* The timers queue should now be full, so it should be possible to create
\r
279 another timer, but not possible to start it (the timer queue will not get
\r
280 drained until the scheduler has been started. */
\r
281 xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] = xTimerCreate( "FR Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
\r
282 ( configTIMER_QUEUE_LENGTH * xBasePeriod ), /* The period for the timer. */
\r
283 pdTRUE, /* Auto-reload is set to true. */
\r
284 ( void * ) xTimer, /* An identifier for the timer as all the auto reload timers use the same callback. */
\r
285 prvAutoReloadTimerCallback ); /* The callback executed when the timer expires. */
\r
287 if( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] == NULL )
\r
289 xTestStatus = pdFAIL;
\r
290 configASSERT( xTestStatus );
\r
294 if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) == pdPASS )
\r
296 /* This time it would not be expected that the timer could be
\r
297 started at this point. */
\r
298 xTestStatus = pdFAIL;
\r
299 configASSERT( xTestStatus );
\r
303 /* Create the timers that are used from the tick interrupt to test the timer
\r
304 API functions that can be called from an ISR. */
\r
305 xISRAutoReloadTimer = xTimerCreate( "ISR AR", /* The text name given to the timer. */
\r
306 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
307 pdTRUE, /* This is an auto reload timer. */
\r
308 ( void * ) NULL, /* The identifier is not required. */
\r
309 prvISRAutoReloadTimerCallback ); /* The callback that is executed when the timer expires. */
\r
311 xISROneShotTimer = xTimerCreate( "ISR OS", /* The text name given to the timer. */
\r
312 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
313 pdFALSE, /* This is a one shot timer. */
\r
314 ( void * ) NULL, /* The identifier is not required. */
\r
315 prvISROneShotTimerCallback ); /* The callback that is executed when the timer expires. */
\r
317 if( ( xISRAutoReloadTimer == NULL ) || ( xISROneShotTimer == NULL ) )
\r
319 xTestStatus = pdFAIL;
\r
320 configASSERT( xTestStatus );
\r
323 /*-----------------------------------------------------------*/
\r
325 static void prvTest2_CheckTaskAndTimersInitialState( void )
\r
329 /* Ensure all the timers are in their expected initial state. This depends
\r
330 on the timer service task having a higher priority than this task.
\r
332 auto reload timers 0 to ( configTIMER_QUEUE_LENGTH - 1 ) should now be active,
\r
333 and auto reload timer configTIMER_QUEUE_LENGTH should not yet be active (it
\r
334 could not be started prior to the scheduler being started when it was
\r
336 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
338 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
340 xTestStatus = pdFAIL;
\r
341 configASSERT( xTestStatus );
\r
345 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] ) != pdFALSE )
\r
347 xTestStatus = pdFAIL;
\r
348 configASSERT( xTestStatus );
\r
351 /*-----------------------------------------------------------*/
\r
353 static void prvTest3_CheckAutoReloadExpireRates( void )
\r
355 uint8_t ucMaxAllowableValue, ucMinAllowableValue, ucTimer;
\r
356 TickType_t xBlockPeriod, xTimerPeriod, xExpectedNumber;
\r
357 UBaseType_t uxOriginalPriority;
\r
359 /* Check the auto reload timers expire at the expected rates. Do this at a
\r
360 high priority for maximum accuracy. This is ok as most of the time is spent
\r
361 in the Blocked state. */
\r
362 uxOriginalPriority = uxTaskPriorityGet( NULL );
\r
363 vTaskPrioritySet( NULL, ( configMAX_PRIORITIES - 1 ) );
\r
365 /* Delaying for configTIMER_QUEUE_LENGTH * xBasePeriod ticks should allow
\r
366 all the auto reload timers to expire at least once. */
\r
367 xBlockPeriod = ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod;
\r
368 vTaskDelay( xBlockPeriod );
\r
370 /* Check that all the auto reload timers have called their callback
\r
371 function the expected number of times. */
\r
372 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
374 /* The expected number of expiries is equal to the block period divided
\r
375 by the timer period. */
\r
376 xTimerPeriod = ( ( ( TickType_t ) ucTimer + ( TickType_t ) 1 ) * xBasePeriod );
\r
377 xExpectedNumber = xBlockPeriod / xTimerPeriod;
\r
379 ucMaxAllowableValue = ( ( uint8_t ) xExpectedNumber ) ;
\r
380 ucMinAllowableValue = ( uint8_t ) ( ( uint8_t ) xExpectedNumber - ( uint8_t ) 1 ); /* Weird casting to try and please all compilers. */
\r
382 if( ( ucAutoReloadTimerCounters[ ucTimer ] < ucMinAllowableValue ) ||
\r
383 ( ucAutoReloadTimerCounters[ ucTimer ] > ucMaxAllowableValue )
\r
386 xTestStatus = pdFAIL;
\r
387 configASSERT( xTestStatus );
\r
391 /* Return to the original priority. */
\r
392 vTaskPrioritySet( NULL, uxOriginalPriority );
\r
394 if( xTestStatus == pdPASS )
\r
396 /* No errors have been reported so increment the loop counter so the
\r
397 check task knows this task is still running. */
\r
401 /*-----------------------------------------------------------*/
\r
403 static void prvTest4_CheckAutoReloadTimersCanBeStopped( void )
\r
407 /* Check the auto reload timers can be stopped correctly, and correctly
\r
408 report their state. */
\r
410 /* Stop all the active timers. */
\r
411 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
413 /* The timer has not been stopped yet! */
\r
414 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
416 xTestStatus = pdFAIL;
\r
417 configASSERT( xTestStatus );
\r
420 /* Now stop the timer. This will appear to happen immediately to
\r
421 this task because this task is running at a priority below the
\r
422 timer service task. */
\r
423 xTimerStop( xAutoReloadTimers[ ucTimer ], tmrdemoDONT_BLOCK );
\r
425 /* The timer should now be inactive. */
\r
426 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )
\r
428 xTestStatus = pdFAIL;
\r
429 configASSERT( xTestStatus );
\r
433 taskENTER_CRITICAL();
\r
435 /* The timer in array position configTIMER_QUEUE_LENGTH should not
\r
436 be active. The critical section is used to ensure the timer does
\r
437 not call its callback between the next line running and the array
\r
438 being cleared back to zero, as that would mask an error condition. */
\r
439 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH ] != ( uint8_t ) 0 )
\r
441 xTestStatus = pdFAIL;
\r
442 configASSERT( xTestStatus );
\r
445 /* Clear the timer callback count. */
\r
446 memset( ( void * ) ucAutoReloadTimerCounters, 0, sizeof( ucAutoReloadTimerCounters ) );
\r
448 taskEXIT_CRITICAL();
\r
450 /* The timers are now all inactive, so this time, after delaying, none
\r
451 of the callback counters should have incremented. */
\r
452 vTaskDelay( ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod );
\r
453 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
455 if( ucAutoReloadTimerCounters[ ucTimer ] != ( uint8_t ) 0 )
\r
457 xTestStatus = pdFAIL;
\r
458 configASSERT( xTestStatus );
\r
462 if( xTestStatus == pdPASS )
\r
464 /* No errors have been reported so increment the loop counter so
\r
465 the check task knows this task is still running. */
\r
469 /*-----------------------------------------------------------*/
\r
471 static void prvTest5_CheckBasicOneShotTimerBehaviour( void )
\r
473 /* Check the one shot timer only calls its callback once after it has been
\r
474 started, and that it reports its state correctly. */
\r
476 /* The one shot timer should not be active yet. */
\r
477 if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )
\r
479 xTestStatus = pdFAIL;
\r
480 configASSERT( xTestStatus );
\r
483 if( ucOneShotTimerCounter != ( uint8_t ) 0 )
\r
485 xTestStatus = pdFAIL;
\r
486 configASSERT( xTestStatus );
\r
489 /* Start the one shot timer and check that it reports its state correctly. */
\r
490 xTimerStart( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
491 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
493 xTestStatus = pdFAIL;
\r
494 configASSERT( xTestStatus );
\r
497 /* Delay for three times as long as the one shot timer period, then check
\r
498 to ensure it has only called its callback once, and is now not in the
\r
500 vTaskDelay( tmrdemoONE_SHOT_TIMER_PERIOD * ( TickType_t ) 3 );
\r
502 if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )
\r
504 xTestStatus = pdFAIL;
\r
505 configASSERT( xTestStatus );
\r
508 if( ucOneShotTimerCounter != ( uint8_t ) 1 )
\r
510 xTestStatus = pdFAIL;
\r
511 configASSERT( xTestStatus );
\r
515 /* Reset the one shot timer callback count. */
\r
516 ucOneShotTimerCounter = ( uint8_t ) 0;
\r
519 if( xTestStatus == pdPASS )
\r
521 /* No errors have been reported so increment the loop counter so the
\r
522 check task knows this task is still running. */
\r
526 /*-----------------------------------------------------------*/
\r
528 static void prvTest6_CheckAutoReloadResetBehaviour( void )
\r
532 /* Check timer reset behaviour. */
\r
534 /* Restart the one shot timer and check it reports its status correctly. */
\r
535 xTimerStart( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
536 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
538 xTestStatus = pdFAIL;
\r
539 configASSERT( xTestStatus );
\r
542 /* Restart one of the auto reload timers and check that it reports its
\r
543 status correctly. */
\r
544 xTimerStart( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
545 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
547 xTestStatus = pdFAIL;
\r
548 configASSERT( xTestStatus );
\r
551 for( ucTimer = 0; ucTimer < trmdemoNUM_TIMER_RESETS; ucTimer++ )
\r
553 /* Delay for half as long as the one shot timer period, then reset it.
\r
554 It should never expire while this is done, so its callback count should
\r
555 never increment. */
\r
556 vTaskDelay( tmrdemoONE_SHOT_TIMER_PERIOD / 2 );
\r
558 /* Check both running timers are still active, but have not called their
\r
559 callback functions. */
\r
560 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
562 xTestStatus = pdFAIL;
\r
563 configASSERT( xTestStatus );
\r
566 if( ucOneShotTimerCounter != ( uint8_t ) 0 )
\r
568 xTestStatus = pdFAIL;
\r
569 configASSERT( xTestStatus );
\r
572 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
574 xTestStatus = pdFAIL;
\r
575 configASSERT( xTestStatus );
\r
578 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] != ( uint8_t ) 0 )
\r
580 xTestStatus = pdFAIL;
\r
581 configASSERT( xTestStatus );
\r
584 /* Reset both running timers. */
\r
585 xTimerReset( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
586 xTimerReset( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
588 if( xTestStatus == pdPASS )
\r
590 /* No errors have been reported so increment the loop counter so
\r
591 the check task knows this task is still running. */
\r
596 /* Finally delay long enough for both running timers to expire. */
\r
597 vTaskDelay( ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod );
\r
599 /* The timers were not reset during the above delay period so should now
\r
600 both have called their callback functions. */
\r
601 if( ucOneShotTimerCounter != ( uint8_t ) 1 )
\r
603 xTestStatus = pdFAIL;
\r
604 configASSERT( xTestStatus );
\r
607 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] == 0 )
\r
609 xTestStatus = pdFAIL;
\r
610 configASSERT( xTestStatus );
\r
613 /* The one shot timer should no longer be active, while the auto reload
\r
614 timer should still be active. */
\r
615 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
617 xTestStatus = pdFAIL;
\r
618 configASSERT( xTestStatus );
\r
621 if( xTimerIsTimerActive( xOneShotTimer ) == pdTRUE )
\r
623 xTestStatus = pdFAIL;
\r
624 configASSERT( xTestStatus );
\r
627 /* Stop the auto reload timer again. */
\r
628 xTimerStop( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
630 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) != pdFALSE )
\r
632 xTestStatus = pdFAIL;
\r
633 configASSERT( xTestStatus );
\r
636 /* Clear the timer callback counts, ready for another iteration of these
\r
638 ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] = ( uint8_t ) 0;
\r
639 ucOneShotTimerCounter = ( uint8_t ) 0;
\r
641 if( xTestStatus == pdPASS )
\r
643 /* No errors have been reported so increment the loop counter so the check
\r
644 task knows this task is still running. */
\r
648 /*-----------------------------------------------------------*/
\r
650 static void prvResetStartConditionsForNextIteration( void )
\r
654 /* Start the timers again to start all the tests over again. */
\r
656 /* Start the timers again. */
\r
657 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
659 /* The timer has not been started yet! */
\r
660 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )
\r
662 xTestStatus = pdFAIL;
\r
663 configASSERT( xTestStatus );
\r
666 /* Now start the timer. This will appear to happen immediately to
\r
667 this task because this task is running at a priority below the timer
\r
669 xTimerStart( xAutoReloadTimers[ ucTimer ], tmrdemoDONT_BLOCK );
\r
671 /* The timer should now be active. */
\r
672 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
674 xTestStatus = pdFAIL;
\r
675 configASSERT( xTestStatus );
\r
679 if( xTestStatus == pdPASS )
\r
681 /* No errors have been reported so increment the loop counter so the
\r
682 check task knows this task is still running. */
\r
686 /*-----------------------------------------------------------*/
\r
688 void vTimerPeriodicISRTests( void )
\r
690 static TickType_t uxTick = ( TickType_t ) -1;
\r
692 #if( configTIMER_TASK_PRIORITY != ( configMAX_PRIORITIES - 1 ) )
\r
693 /* The timer service task is not the highest priority task, so it cannot
\r
694 be assumed that timings will be exact. Timers should never call their
\r
695 callback before their expiry time, but a margin is permissible for calling
\r
696 their callback after their expiry time. If exact timing is required then
\r
697 configTIMER_TASK_PRIORITY must be set to ensure the timer service task
\r
698 is the highest priority task in the system.
\r
700 This function is called from the tick hook. The tick hook is called
\r
701 even when the scheduler is suspended. Therefore it is possible that the
\r
702 uxTick count maintained in this function is temporarily ahead of the tick
\r
703 count maintained by the kernel. When this is the case a message posted from
\r
704 this function will assume a time stamp in advance of the real time stamp,
\r
705 which can result in a timer being processed before this function expects it
\r
706 to. For example, if the kernel's tick count was 100, and uxTick was 102,
\r
707 then this function will not expect the timer to have expired until the
\r
708 kernel's tick count is (102 + xBasePeriod), whereas in reality the timer
\r
709 will expire when the kernel's tick count is (100 + xBasePeriod). For this
\r
710 reason xMargin is used as an allowable margin for premature timer expiries
\r
711 as well as late timer expiries. */
\r
713 /* Windows is not real real time. */
\r
714 const TickType_t xMargin = 20;
\r
716 const TickType_t xMargin = 6;
\r
717 #endif /* _WINDOWS_ */
\r
720 /* Windows is not real real time. */
\r
721 const TickType_t xMargin = 20;
\r
723 const TickType_t xMargin = 4;
\r
724 #endif /* _WINDOWS_ */
\r
732 /* The timers will have been created, but not started. Start them now
\r
733 by setting their period. */
\r
734 ucISRAutoReloadTimerCounter = 0;
\r
735 ucISROneShotTimerCounter = 0;
\r
737 /* It is possible that the timer task has not yet made room in the
\r
738 timer queue. If the timers cannot be started then reset uxTick so
\r
739 another attempt is made later. */
\r
740 uxTick = ( TickType_t ) -1;
\r
742 /* Try starting first timer. */
\r
743 if( xTimerChangePeriodFromISR( xISRAutoReloadTimer, xBasePeriod, NULL ) == pdPASS )
\r
745 /* First timer was started, try starting the second timer. */
\r
746 if( xTimerChangePeriodFromISR( xISROneShotTimer, xBasePeriod, NULL ) == pdPASS )
\r
748 /* Both timers were started, so set the uxTick back to its
\r
754 /* Second timer could not be started, so stop the first one
\r
756 xTimerStopFromISR( xISRAutoReloadTimer, NULL );
\r
760 else if( uxTick == ( xBasePeriod - xMargin ) )
\r
762 /* Neither timer should have expired yet. */
\r
763 if( ( ucISRAutoReloadTimerCounter != 0 ) || ( ucISROneShotTimerCounter != 0 ) )
\r
765 xTestStatus = pdFAIL;
\r
766 configASSERT( xTestStatus );
\r
769 else if( uxTick == ( xBasePeriod + xMargin ) )
\r
771 /* Both timers should now have expired once. The auto reload timer will
\r
772 still be active, but the one shot timer should now have stopped. */
\r
773 if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )
\r
775 xTestStatus = pdFAIL;
\r
776 configASSERT( xTestStatus );
\r
779 else if( uxTick == ( ( 2 * xBasePeriod ) - xMargin ) )
\r
781 /* The auto reload timer will still be active, but the one shot timer
\r
782 should now have stopped - however, at this time neither of the timers
\r
783 should have expired again since the last test. */
\r
784 if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )
\r
786 xTestStatus = pdFAIL;
\r
787 configASSERT( xTestStatus );
\r
790 else if( uxTick == ( ( 2 * xBasePeriod ) + xMargin ) )
\r
792 /* The auto reload timer will still be active, but the one shot timer
\r
793 should now have stopped. At this time the auto reload timer should have
\r
794 expired again, but the one shot timer count should not have changed. */
\r
795 if( ucISRAutoReloadTimerCounter != 2 )
\r
797 xTestStatus = pdFAIL;
\r
798 configASSERT( xTestStatus );
\r
801 if( ucISROneShotTimerCounter != 1 )
\r
803 xTestStatus = pdFAIL;
\r
804 configASSERT( xTestStatus );
\r
807 else if( uxTick == ( ( 2 * xBasePeriod ) + ( xBasePeriod >> ( TickType_t ) 2U ) ) )
\r
809 /* The auto reload timer will still be active, but the one shot timer
\r
810 should now have stopped. Again though, at this time, neither timer call
\r
811 back should have been called since the last test. */
\r
812 if( ucISRAutoReloadTimerCounter != 2 )
\r
814 xTestStatus = pdFAIL;
\r
815 configASSERT( xTestStatus );
\r
818 if( ucISROneShotTimerCounter != 1 )
\r
820 xTestStatus = pdFAIL;
\r
821 configASSERT( xTestStatus );
\r
824 else if( uxTick == ( 3 * xBasePeriod ) )
\r
826 /* Start the one shot timer again. */
\r
827 xTimerStartFromISR( xISROneShotTimer, NULL );
\r
829 else if( uxTick == ( ( 3 * xBasePeriod ) + xMargin ) )
\r
831 /* The auto reload timer and one shot timer will be active. At
\r
832 this time the auto reload timer should have expired again, but the one
\r
833 shot timer count should not have changed yet. */
\r
834 if( ucISRAutoReloadTimerCounter != 3 )
\r
836 xTestStatus = pdFAIL;
\r
837 configASSERT( xTestStatus );
\r
840 if( ucISROneShotTimerCounter != 1 )
\r
842 xTestStatus = pdFAIL;
\r
843 configASSERT( xTestStatus );
\r
846 /* Now stop the auto reload timer. The one shot timer was started
\r
847 a few ticks ago. */
\r
848 xTimerStopFromISR( xISRAutoReloadTimer, NULL );
\r
850 else if( uxTick == ( 4 * ( xBasePeriod - xMargin ) ) )
\r
852 /* The auto reload timer is now stopped, and the one shot timer is
\r
853 active, but at this time neither timer should have expired since the
\r
855 if( ucISRAutoReloadTimerCounter != 3 )
\r
857 xTestStatus = pdFAIL;
\r
858 configASSERT( xTestStatus );
\r
861 if( ucISROneShotTimerCounter != 1 )
\r
863 xTestStatus = pdFAIL;
\r
864 configASSERT( xTestStatus );
\r
867 else if( uxTick == ( ( 4 * xBasePeriod ) + xMargin ) )
\r
869 /* The auto reload timer is now stopped, and the one shot timer is
\r
870 active. The one shot timer should have expired again, but the auto
\r
871 reload timer should not have executed its callback. */
\r
872 if( ucISRAutoReloadTimerCounter != 3 )
\r
874 xTestStatus = pdFAIL;
\r
875 configASSERT( xTestStatus );
\r
878 if( ucISROneShotTimerCounter != 2 )
\r
880 xTestStatus = pdFAIL;
\r
881 configASSERT( xTestStatus );
\r
884 else if( uxTick == ( 8 * xBasePeriod ) )
\r
886 /* The auto reload timer is now stopped, and the one shot timer has
\r
887 already expired and then stopped itself. Both callback counters should
\r
888 not have incremented since the last test. */
\r
889 if( ucISRAutoReloadTimerCounter != 3 )
\r
891 xTestStatus = pdFAIL;
\r
892 configASSERT( xTestStatus );
\r
895 if( ucISROneShotTimerCounter != 2 )
\r
897 xTestStatus = pdFAIL;
\r
898 configASSERT( xTestStatus );
\r
901 /* Now reset the one shot timer. */
\r
902 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
904 else if( uxTick == ( ( 9 * xBasePeriod ) - xMargin ) )
\r
906 /* Only the one shot timer should be running, but it should not have
\r
907 expired since the last test. Check the callback counters have not
\r
908 incremented, then reset the one shot timer again. */
\r
909 if( ucISRAutoReloadTimerCounter != 3 )
\r
911 xTestStatus = pdFAIL;
\r
912 configASSERT( xTestStatus );
\r
915 if( ucISROneShotTimerCounter != 2 )
\r
917 xTestStatus = pdFAIL;
\r
918 configASSERT( xTestStatus );
\r
921 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
923 else if( uxTick == ( ( 10 * xBasePeriod ) - ( 2 * xMargin ) ) )
\r
925 /* Only the one shot timer should be running, but it should not have
\r
926 expired since the last test. Check the callback counters have not
\r
927 incremented, then reset the one shot timer again. */
\r
928 if( ucISRAutoReloadTimerCounter != 3 )
\r
930 xTestStatus = pdFAIL;
\r
931 configASSERT( xTestStatus );
\r
934 if( ucISROneShotTimerCounter != 2 )
\r
936 xTestStatus = pdFAIL;
\r
937 configASSERT( xTestStatus );
\r
940 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
942 else if( uxTick == ( ( 11 * xBasePeriod ) - ( 3 * xMargin ) ) )
\r
944 /* Only the one shot timer should be running, but it should not have
\r
945 expired since the last test. Check the callback counters have not
\r
946 incremented, then reset the one shot timer once again. */
\r
947 if( ucISRAutoReloadTimerCounter != 3 )
\r
949 xTestStatus = pdFAIL;
\r
950 configASSERT( xTestStatus );
\r
953 if( ucISROneShotTimerCounter != 2 )
\r
955 xTestStatus = pdFAIL;
\r
956 configASSERT( xTestStatus );
\r
959 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
961 else if( uxTick == ( ( 12 * xBasePeriod ) - ( 2 * xMargin ) ) )
\r
963 /* Only the one shot timer should have been running and this time it
\r
964 should have expired. Check its callback count has been incremented.
\r
965 The auto reload timer is still not running so should still have the same
\r
966 count value. This time the one shot timer is not reset so should not
\r
967 restart from its expiry period again. */
\r
968 if( ucISRAutoReloadTimerCounter != 3 )
\r
970 xTestStatus = pdFAIL;
\r
971 configASSERT( xTestStatus );
\r
974 if( ucISROneShotTimerCounter != 3 )
\r
976 xTestStatus = pdFAIL;
\r
977 configASSERT( xTestStatus );
\r
980 else if( uxTick == ( 15 * xBasePeriod ) )
\r
982 /* Neither timer should be running now. Check neither callback count
\r
983 has incremented, then go back to the start to run these tests all
\r
985 if( ucISRAutoReloadTimerCounter != 3 )
\r
987 xTestStatus = pdFAIL;
\r
988 configASSERT( xTestStatus );
\r
991 if( ucISROneShotTimerCounter != 3 )
\r
993 xTestStatus = pdFAIL;
\r
994 configASSERT( xTestStatus );
\r
997 uxTick = ( TickType_t ) -1;
\r
1000 /*-----------------------------------------------------------*/
\r
1002 /*** Timer callback functions are defined below here. ***/
\r
1004 static void prvAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer )
\r
1008 uxTimerID = ( size_t ) pvTimerGetTimerID( pxExpiredTimer );
\r
1009 if( uxTimerID <= ( configTIMER_QUEUE_LENGTH + 1 ) )
\r
1011 ( ucAutoReloadTimerCounters[ uxTimerID ] )++;
\r
1015 /* The timer ID appears to be unexpected (invalid). */
\r
1016 xTestStatus = pdFAIL;
\r
1017 configASSERT( xTestStatus );
\r
1020 /*-----------------------------------------------------------*/
\r
1022 static void prvOneShotTimerCallback( TimerHandle_t pxExpiredTimer )
\r
1024 /* A count is kept of the number of times this callback function is executed.
\r
1025 The count is stored as the timer's ID. This is only done to test the
\r
1026 vTimerSetTimerID() function. */
\r
1027 static size_t uxCallCount = 0;
\r
1028 size_t uxLastCallCount;
\r
1030 /* Obtain the timer's ID, which should be a count of the number of times
\r
1031 this callback function has been executed. */
\r
1032 uxLastCallCount = ( size_t ) pvTimerGetTimerID( pxExpiredTimer );
\r
1033 configASSERT( uxLastCallCount == uxCallCount );
\r
1035 /* Increment the call count, then save it back as the timer's ID. This is
\r
1036 only done to test the vTimerSetTimerID() API function. */
\r
1037 uxLastCallCount++;
\r
1038 vTimerSetTimerID( pxExpiredTimer, ( void * ) uxLastCallCount );
\r
1041 ucOneShotTimerCounter++;
\r
1043 /*-----------------------------------------------------------*/
\r
1045 static void prvISRAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer )
\r
1047 /* The parameter is not used in this case as only one timer uses this
\r
1048 callback function. */
\r
1049 ( void ) pxExpiredTimer;
\r
1051 ucISRAutoReloadTimerCounter++;
\r
1053 /*-----------------------------------------------------------*/
\r
1055 static void prvISROneShotTimerCallback( TimerHandle_t pxExpiredTimer )
\r
1057 /* The parameter is not used in this case as only one timer uses this
\r
1058 callback function. */
\r
1059 ( void ) pxExpiredTimer;
\r
1061 ucISROneShotTimerCounter++;
\r
1063 /*-----------------------------------------------------------*/
\r