2 * FreeRTOS Kernel V10.2.1
\r
3 * Copyright (C) 2019 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.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
30 * Tests the behaviour of timers. Some timers are created before the scheduler
\r
31 * is started, and some after.
\r
34 /* Standard includes. */
\r
37 /* Scheduler include files. */
\r
38 #include "FreeRTOS.h"
\r
42 /* Demo program include files. */
\r
43 #include "TimerDemo.h"
\r
45 #if ( configTIMER_TASK_PRIORITY < 1 )
\r
46 #error configTIMER_TASK_PRIORITY must be set to at least 1 for this test/demo to function correctly.
\r
49 #define tmrdemoDONT_BLOCK ( ( TickType_t ) 0 )
\r
50 #define tmrdemoONE_SHOT_TIMER_PERIOD ( xBasePeriod * ( TickType_t ) 3 )
\r
51 #define tmrdemoNUM_TIMER_RESETS ( ( uint8_t ) 10 )
\r
53 #ifndef tmrTIMER_TEST_TASK_STACK_SIZE
\r
54 #define tmrTIMER_TEST_TASK_STACK_SIZE configMINIMAL_STACK_SIZE
\r
57 /*-----------------------------------------------------------*/
\r
59 /* The callback functions used by the timers. These each increment a counter
\r
60 to indicate which timer has expired. The auto-reload timers that are used by
\r
61 the test task (as opposed to being used from an ISR) all share the same
\r
62 prvAutoReloadTimerCallback() callback function, and use the ID of the
\r
63 pxExpiredTimer parameter passed into that function to know which counter to
\r
64 increment. The other timers all have their own unique callback function and
\r
65 simply increment their counters without using the callback function parameter. */
\r
66 static void prvAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer );
\r
67 static void prvOneShotTimerCallback( TimerHandle_t pxExpiredTimer );
\r
68 static void prvTimerTestTask( void *pvParameters );
\r
69 static void prvISRAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer );
\r
70 static void prvISROneShotTimerCallback( TimerHandle_t pxExpiredTimer );
\r
72 /* The test functions used by the timer test task. These manipulate the auto
\r
73 reload and one shot timers in various ways, then delay, then inspect the timers
\r
74 to ensure they have behaved as expected. */
\r
75 static void prvTest1_CreateTimersWithoutSchedulerRunning( void );
\r
76 static void prvTest2_CheckTaskAndTimersInitialState( void );
\r
77 static void prvTest3_CheckAutoReloadExpireRates( void );
\r
78 static void prvTest4_CheckAutoReloadTimersCanBeStopped( void );
\r
79 static void prvTest5_CheckBasicOneShotTimerBehaviour( void );
\r
80 static void prvTest6_CheckAutoReloadResetBehaviour( void );
\r
81 static void prvResetStartConditionsForNextIteration( void );
\r
83 /*-----------------------------------------------------------*/
\r
85 /* Flag that will be latched to pdFAIL should any unexpected behaviour be
\r
86 detected in any of the demo tests. */
\r
87 static volatile BaseType_t xTestStatus = pdPASS;
\r
89 /* Counter that is incremented on each cycle of a test. This is used to
\r
90 detect a stalled task - a test that is no longer running. */
\r
91 static volatile uint32_t ulLoopCounter = 0;
\r
93 /* A set of auto reload timers - each of which use the same callback function.
\r
94 The callback function uses the timer ID to index into, and then increment, a
\r
95 counter in the ucAutoReloadTimerCounters[] array. The auto reload timers
\r
96 referenced from xAutoReloadTimers[] are used by the prvTimerTestTask task. */
\r
97 static TimerHandle_t xAutoReloadTimers[ configTIMER_QUEUE_LENGTH + 1 ] = { 0 };
\r
98 static uint8_t ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH + 1 ] = { 0 };
\r
100 /* The one shot timer is configured to use a callback function that increments
\r
101 ucOneShotTimerCounter each time it gets called. */
\r
102 static TimerHandle_t xOneShotTimer = NULL;
\r
103 static uint8_t ucOneShotTimerCounter = ( uint8_t ) 0;
\r
105 /* The ISR reload timer is controlled from the tick hook to exercise the timer
\r
106 API functions that can be used from an ISR. It is configured to increment
\r
107 ucISRReloadTimerCounter each time its callback function is executed. */
\r
108 static TimerHandle_t xISRAutoReloadTimer = NULL;
\r
109 static uint8_t ucISRAutoReloadTimerCounter = ( uint8_t ) 0;
\r
111 /* The ISR one shot timer is controlled from the tick hook to exercise the timer
\r
112 API functions that can be used from an ISR. It is configured to increment
\r
113 ucISRReloadTimerCounter each time its callback function is executed. */
\r
114 static TimerHandle_t xISROneShotTimer = NULL;
\r
115 static uint8_t ucISROneShotTimerCounter = ( uint8_t ) 0;
\r
117 /* The period of all the timers are a multiple of the base period. The base
\r
118 period is configured by the parameter to vStartTimerDemoTask(). */
\r
119 static TickType_t xBasePeriod = 0;
\r
121 /*-----------------------------------------------------------*/
\r
123 void vStartTimerDemoTask( TickType_t xBasePeriodIn )
\r
125 /* Start with the timer and counter arrays clear - this is only necessary
\r
126 where the compiler does not clear them automatically on start up. */
\r
127 memset( ucAutoReloadTimerCounters, 0x00, sizeof( ucAutoReloadTimerCounters ) );
\r
128 memset( xAutoReloadTimers, 0x00, sizeof( xAutoReloadTimers ) );
\r
130 /* Store the period from which all the timer periods will be generated from
\r
132 xBasePeriod = xBasePeriodIn;
\r
134 /* Create a set of timers for use by this demo/test. */
\r
135 prvTest1_CreateTimersWithoutSchedulerRunning();
\r
137 /* Create the task that will control and monitor the timers. This is
\r
138 created at a lower priority than the timer service task to ensure, as
\r
139 far as it is concerned, commands on timers are actioned immediately
\r
140 (sending a command to the timer service task will unblock the timer service
\r
141 task, which will then preempt this task). */
\r
142 if( xTestStatus != pdFAIL )
\r
144 xTaskCreate( prvTimerTestTask, "Tmr Tst", tmrTIMER_TEST_TASK_STACK_SIZE, NULL, configTIMER_TASK_PRIORITY - 1, NULL );
\r
147 /*-----------------------------------------------------------*/
\r
149 static void prvTimerTestTask( void *pvParameters )
\r
151 ( void ) pvParameters;
\r
153 /* Create a one-shot timer for use later on in this test. */
\r
154 xOneShotTimer = xTimerCreate( "Oneshot Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
\r
155 tmrdemoONE_SHOT_TIMER_PERIOD, /* The period for the timer. */
\r
156 pdFALSE, /* Don't auto-reload - hence a one shot timer. */
\r
157 ( void * ) 0, /* The timer identifier. Initialise to 0, then increment each time it is called. */
\r
158 prvOneShotTimerCallback ); /* The callback to be called when the timer expires. */
\r
160 if( xOneShotTimer == NULL )
\r
162 xTestStatus = pdFAIL;
\r
163 configASSERT( xTestStatus );
\r
167 /* Ensure all the timers are in their expected initial state. This
\r
168 depends on the timer service task having a higher priority than this task. */
\r
169 prvTest2_CheckTaskAndTimersInitialState();
\r
173 /* Check the auto reload timers expire at the expected/correct rates. */
\r
174 prvTest3_CheckAutoReloadExpireRates();
\r
176 /* Check the auto reload timers can be stopped correctly, and correctly
\r
177 report their state. */
\r
178 prvTest4_CheckAutoReloadTimersCanBeStopped();
\r
180 /* Check the one shot timer only calls its callback once after it has been
\r
181 started, and that it reports its state correctly. */
\r
182 prvTest5_CheckBasicOneShotTimerBehaviour();
\r
184 /* Check timer reset behaviour. */
\r
185 prvTest6_CheckAutoReloadResetBehaviour();
\r
187 /* Start the timers again to restart all the tests over again. */
\r
188 prvResetStartConditionsForNextIteration();
\r
191 /*-----------------------------------------------------------*/
\r
193 /* This is called to check that the created task is still running and has not
\r
194 detected any errors. */
\r
195 BaseType_t xAreTimerDemoTasksStillRunning( TickType_t xCycleFrequency )
\r
197 static uint32_t ulLastLoopCounter = 0UL;
\r
198 TickType_t xMaxBlockTimeUsedByTheseTests, xLoopCounterIncrementTimeMax;
\r
199 static TickType_t xIterationsWithoutCounterIncrement = ( TickType_t ) 0, xLastCycleFrequency;
\r
201 if( xLastCycleFrequency != xCycleFrequency )
\r
203 /* The cycle frequency has probably become much faster due to an error
\r
204 elsewhere. Start counting Iterations again. */
\r
205 xIterationsWithoutCounterIncrement = ( TickType_t ) 0;
\r
206 xLastCycleFrequency = xCycleFrequency;
\r
209 /* Calculate the maximum number of times that it is permissible for this
\r
210 function to be called without ulLoopCounter being incremented. This is
\r
211 necessary because the tests in this file block for extended periods, and the
\r
212 block period might be longer than the time between calls to this function. */
\r
213 xMaxBlockTimeUsedByTheseTests = ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod;
\r
214 xLoopCounterIncrementTimeMax = ( xMaxBlockTimeUsedByTheseTests / xCycleFrequency ) + 1;
\r
216 /* If the demo task is still running then the loop counter is expected to
\r
217 have incremented every xLoopCounterIncrementTimeMax calls. */
\r
218 if( ulLastLoopCounter == ulLoopCounter )
\r
220 xIterationsWithoutCounterIncrement++;
\r
221 if( xIterationsWithoutCounterIncrement > xLoopCounterIncrementTimeMax )
\r
223 /* The tests appear to be no longer running (stalled). */
\r
224 xTestStatus = pdFAIL;
\r
229 /* ulLoopCounter changed, so the count of times this function was called
\r
230 without a change can be reset to zero. */
\r
231 xIterationsWithoutCounterIncrement = ( TickType_t ) 0;
\r
234 ulLastLoopCounter = ulLoopCounter;
\r
236 /* Errors detected in the task itself will have latched xTestStatus
\r
239 return xTestStatus;
\r
241 /*-----------------------------------------------------------*/
\r
243 static void prvTest1_CreateTimersWithoutSchedulerRunning( void )
\r
247 for( xTimer = 0; xTimer < configTIMER_QUEUE_LENGTH; xTimer++ )
\r
249 /* As the timer queue is not yet full, it should be possible to both
\r
250 create and start a timer. These timers are being started before the
\r
251 scheduler has been started, so their block times should get set to zero
\r
252 within the timer API itself. */
\r
253 xAutoReloadTimers[ xTimer ] = xTimerCreate( "FR Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
\r
254 ( ( xTimer + ( TickType_t ) 1 ) * xBasePeriod ),/* The period for the timer. The plus 1 ensures a period of zero is not specified. */
\r
255 pdTRUE, /* Auto-reload is set to true. */
\r
256 ( void * ) xTimer, /* An identifier for the timer as all the auto reload timers use the same callback. */
\r
257 prvAutoReloadTimerCallback ); /* The callback to be called when the timer expires. */
\r
259 if( xAutoReloadTimers[ xTimer ] == NULL )
\r
261 xTestStatus = pdFAIL;
\r
262 configASSERT( xTestStatus );
\r
266 configASSERT( strcmp( pcTimerGetName( xAutoReloadTimers[ xTimer ] ), "FR Timer" ) == 0 );
\r
268 /* The scheduler has not yet started, so the block period of
\r
269 portMAX_DELAY should just get set to zero in xTimerStart(). Also,
\r
270 the timer queue is not yet full so xTimerStart() should return
\r
272 if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) != pdPASS )
\r
274 xTestStatus = pdFAIL;
\r
275 configASSERT( xTestStatus );
\r
280 /* The timers queue should now be full, so it should be possible to create
\r
281 another timer, but not possible to start it (the timer queue will not get
\r
282 drained until the scheduler has been started. */
\r
283 xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] = xTimerCreate( "FR Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
\r
284 ( configTIMER_QUEUE_LENGTH * xBasePeriod ), /* The period for the timer. */
\r
285 pdTRUE, /* Auto-reload is set to true. */
\r
286 ( void * ) xTimer, /* An identifier for the timer as all the auto reload timers use the same callback. */
\r
287 prvAutoReloadTimerCallback ); /* The callback executed when the timer expires. */
\r
289 if( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] == NULL )
\r
291 xTestStatus = pdFAIL;
\r
292 configASSERT( xTestStatus );
\r
296 if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) == pdPASS )
\r
298 /* This time it would not be expected that the timer could be
\r
299 started at this point. */
\r
300 xTestStatus = pdFAIL;
\r
301 configASSERT( xTestStatus );
\r
305 /* Create the timers that are used from the tick interrupt to test the timer
\r
306 API functions that can be called from an ISR. */
\r
307 xISRAutoReloadTimer = xTimerCreate( "ISR AR", /* The text name given to the timer. */
\r
308 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
309 pdTRUE, /* This is an auto reload timer. */
\r
310 ( void * ) NULL, /* The identifier is not required. */
\r
311 prvISRAutoReloadTimerCallback ); /* The callback that is executed when the timer expires. */
\r
313 xISROneShotTimer = xTimerCreate( "ISR OS", /* The text name given to the timer. */
\r
314 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
315 pdFALSE, /* This is a one shot timer. */
\r
316 ( void * ) NULL, /* The identifier is not required. */
\r
317 prvISROneShotTimerCallback ); /* The callback that is executed when the timer expires. */
\r
319 if( ( xISRAutoReloadTimer == NULL ) || ( xISROneShotTimer == NULL ) )
\r
321 xTestStatus = pdFAIL;
\r
322 configASSERT( xTestStatus );
\r
325 /*-----------------------------------------------------------*/
\r
327 static void prvTest2_CheckTaskAndTimersInitialState( void )
\r
331 /* Ensure all the timers are in their expected initial state. This depends
\r
332 on the timer service task having a higher priority than this task.
\r
334 auto reload timers 0 to ( configTIMER_QUEUE_LENGTH - 1 ) should now be active,
\r
335 and auto reload timer configTIMER_QUEUE_LENGTH should not yet be active (it
\r
336 could not be started prior to the scheduler being started when it was
\r
338 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
340 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
342 xTestStatus = pdFAIL;
\r
343 configASSERT( xTestStatus );
\r
347 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] ) != pdFALSE )
\r
349 xTestStatus = pdFAIL;
\r
350 configASSERT( xTestStatus );
\r
353 /*-----------------------------------------------------------*/
\r
355 static void prvTest3_CheckAutoReloadExpireRates( void )
\r
357 uint8_t ucMaxAllowableValue, ucMinAllowableValue, ucTimer;
\r
358 TickType_t xBlockPeriod, xTimerPeriod, xExpectedNumber;
\r
359 UBaseType_t uxOriginalPriority;
\r
361 /* Check the auto reload timers expire at the expected rates. Do this at a
\r
362 high priority for maximum accuracy. This is ok as most of the time is spent
\r
363 in the Blocked state. */
\r
364 uxOriginalPriority = uxTaskPriorityGet( NULL );
\r
365 vTaskPrioritySet( NULL, ( configMAX_PRIORITIES - 1 ) );
\r
367 /* Delaying for configTIMER_QUEUE_LENGTH * xBasePeriod ticks should allow
\r
368 all the auto reload timers to expire at least once. */
\r
369 xBlockPeriod = ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod;
\r
370 vTaskDelay( xBlockPeriod );
\r
372 /* Check that all the auto reload timers have called their callback
\r
373 function the expected number of times. */
\r
374 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
376 /* The expected number of expiries is equal to the block period divided
\r
377 by the timer period. */
\r
378 xTimerPeriod = ( ( ( TickType_t ) ucTimer + ( TickType_t ) 1 ) * xBasePeriod );
\r
379 xExpectedNumber = xBlockPeriod / xTimerPeriod;
\r
381 ucMaxAllowableValue = ( ( uint8_t ) xExpectedNumber ) ;
\r
382 ucMinAllowableValue = ( uint8_t ) ( ( uint8_t ) xExpectedNumber - ( uint8_t ) 1 ); /* Weird casting to try and please all compilers. */
\r
384 if( ( ucAutoReloadTimerCounters[ ucTimer ] < ucMinAllowableValue ) ||
\r
385 ( ucAutoReloadTimerCounters[ ucTimer ] > ucMaxAllowableValue )
\r
388 xTestStatus = pdFAIL;
\r
389 configASSERT( xTestStatus );
\r
393 /* Return to the original priority. */
\r
394 vTaskPrioritySet( NULL, uxOriginalPriority );
\r
396 if( xTestStatus == pdPASS )
\r
398 /* No errors have been reported so increment the loop counter so the
\r
399 check task knows this task is still running. */
\r
403 /*-----------------------------------------------------------*/
\r
405 static void prvTest4_CheckAutoReloadTimersCanBeStopped( void )
\r
409 /* Check the auto reload timers can be stopped correctly, and correctly
\r
410 report their state. */
\r
412 /* Stop all the active timers. */
\r
413 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
415 /* The timer has not been stopped yet! */
\r
416 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
418 xTestStatus = pdFAIL;
\r
419 configASSERT( xTestStatus );
\r
422 /* Now stop the timer. This will appear to happen immediately to
\r
423 this task because this task is running at a priority below the
\r
424 timer service task. */
\r
425 xTimerStop( xAutoReloadTimers[ ucTimer ], tmrdemoDONT_BLOCK );
\r
427 /* The timer should now be inactive. */
\r
428 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )
\r
430 xTestStatus = pdFAIL;
\r
431 configASSERT( xTestStatus );
\r
435 taskENTER_CRITICAL();
\r
437 /* The timer in array position configTIMER_QUEUE_LENGTH should not
\r
438 be active. The critical section is used to ensure the timer does
\r
439 not call its callback between the next line running and the array
\r
440 being cleared back to zero, as that would mask an error condition. */
\r
441 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH ] != ( uint8_t ) 0 )
\r
443 xTestStatus = pdFAIL;
\r
444 configASSERT( xTestStatus );
\r
447 /* Clear the timer callback count. */
\r
448 memset( ( void * ) ucAutoReloadTimerCounters, 0, sizeof( ucAutoReloadTimerCounters ) );
\r
450 taskEXIT_CRITICAL();
\r
452 /* The timers are now all inactive, so this time, after delaying, none
\r
453 of the callback counters should have incremented. */
\r
454 vTaskDelay( ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod );
\r
455 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
457 if( ucAutoReloadTimerCounters[ ucTimer ] != ( uint8_t ) 0 )
\r
459 xTestStatus = pdFAIL;
\r
460 configASSERT( xTestStatus );
\r
464 if( xTestStatus == pdPASS )
\r
466 /* No errors have been reported so increment the loop counter so
\r
467 the check task knows this task is still running. */
\r
471 /*-----------------------------------------------------------*/
\r
473 static void prvTest5_CheckBasicOneShotTimerBehaviour( void )
\r
475 /* Check the one shot timer only calls its callback once after it has been
\r
476 started, and that it reports its state correctly. */
\r
478 /* The one shot timer should not be active yet. */
\r
479 if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )
\r
481 xTestStatus = pdFAIL;
\r
482 configASSERT( xTestStatus );
\r
485 if( ucOneShotTimerCounter != ( uint8_t ) 0 )
\r
487 xTestStatus = pdFAIL;
\r
488 configASSERT( xTestStatus );
\r
491 /* Start the one shot timer and check that it reports its state correctly. */
\r
492 xTimerStart( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
493 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
495 xTestStatus = pdFAIL;
\r
496 configASSERT( xTestStatus );
\r
499 /* Delay for three times as long as the one shot timer period, then check
\r
500 to ensure it has only called its callback once, and is now not in the
\r
502 vTaskDelay( tmrdemoONE_SHOT_TIMER_PERIOD * ( TickType_t ) 3 );
\r
504 if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )
\r
506 xTestStatus = pdFAIL;
\r
507 configASSERT( xTestStatus );
\r
510 if( ucOneShotTimerCounter != ( uint8_t ) 1 )
\r
512 xTestStatus = pdFAIL;
\r
513 configASSERT( xTestStatus );
\r
517 /* Reset the one shot timer callback count. */
\r
518 ucOneShotTimerCounter = ( uint8_t ) 0;
\r
521 if( xTestStatus == pdPASS )
\r
523 /* No errors have been reported so increment the loop counter so the
\r
524 check task knows this task is still running. */
\r
528 /*-----------------------------------------------------------*/
\r
530 static void prvTest6_CheckAutoReloadResetBehaviour( void )
\r
534 /* Check timer reset behaviour. */
\r
536 /* Restart the one shot timer and check it reports its status correctly. */
\r
537 xTimerStart( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
538 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
540 xTestStatus = pdFAIL;
\r
541 configASSERT( xTestStatus );
\r
544 /* Restart one of the auto reload timers and check that it reports its
\r
545 status correctly. */
\r
546 xTimerStart( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
547 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
549 xTestStatus = pdFAIL;
\r
550 configASSERT( xTestStatus );
\r
553 for( ucTimer = 0; ucTimer < tmrdemoNUM_TIMER_RESETS; ucTimer++ )
\r
555 /* Delay for half as long as the one shot timer period, then reset it.
\r
556 It should never expire while this is done, so its callback count should
\r
557 never increment. */
\r
558 vTaskDelay( tmrdemoONE_SHOT_TIMER_PERIOD / 2 );
\r
560 /* Check both running timers are still active, but have not called their
\r
561 callback functions. */
\r
562 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
564 xTestStatus = pdFAIL;
\r
565 configASSERT( xTestStatus );
\r
568 if( ucOneShotTimerCounter != ( uint8_t ) 0 )
\r
570 xTestStatus = pdFAIL;
\r
571 configASSERT( xTestStatus );
\r
574 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
576 xTestStatus = pdFAIL;
\r
577 configASSERT( xTestStatus );
\r
580 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] != ( uint8_t ) 0 )
\r
582 xTestStatus = pdFAIL;
\r
583 configASSERT( xTestStatus );
\r
586 /* Reset both running timers. */
\r
587 xTimerReset( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
588 xTimerReset( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
590 if( xTestStatus == pdPASS )
\r
592 /* No errors have been reported so increment the loop counter so
\r
593 the check task knows this task is still running. */
\r
598 /* Finally delay long enough for both running timers to expire. */
\r
599 vTaskDelay( ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod );
\r
601 /* The timers were not reset during the above delay period so should now
\r
602 both have called their callback functions. */
\r
603 if( ucOneShotTimerCounter != ( uint8_t ) 1 )
\r
605 xTestStatus = pdFAIL;
\r
606 configASSERT( xTestStatus );
\r
609 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] == 0 )
\r
611 xTestStatus = pdFAIL;
\r
612 configASSERT( xTestStatus );
\r
615 /* The one shot timer should no longer be active, while the auto reload
\r
616 timer should still be active. */
\r
617 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
619 xTestStatus = pdFAIL;
\r
620 configASSERT( xTestStatus );
\r
623 if( xTimerIsTimerActive( xOneShotTimer ) == pdTRUE )
\r
625 xTestStatus = pdFAIL;
\r
626 configASSERT( xTestStatus );
\r
629 /* Stop the auto reload timer again. */
\r
630 xTimerStop( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
632 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) != pdFALSE )
\r
634 xTestStatus = pdFAIL;
\r
635 configASSERT( xTestStatus );
\r
638 /* Clear the timer callback counts, ready for another iteration of these
\r
640 ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] = ( uint8_t ) 0;
\r
641 ucOneShotTimerCounter = ( uint8_t ) 0;
\r
643 if( xTestStatus == pdPASS )
\r
645 /* No errors have been reported so increment the loop counter so the check
\r
646 task knows this task is still running. */
\r
650 /*-----------------------------------------------------------*/
\r
652 static void prvResetStartConditionsForNextIteration( void )
\r
656 /* Start the timers again to start all the tests over again. */
\r
658 /* Start the timers again. */
\r
659 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
661 /* The timer has not been started yet! */
\r
662 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )
\r
664 xTestStatus = pdFAIL;
\r
665 configASSERT( xTestStatus );
\r
668 /* Now start the timer. This will appear to happen immediately to
\r
669 this task because this task is running at a priority below the timer
\r
671 xTimerStart( xAutoReloadTimers[ ucTimer ], tmrdemoDONT_BLOCK );
\r
673 /* The timer should now be active. */
\r
674 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
676 xTestStatus = pdFAIL;
\r
677 configASSERT( xTestStatus );
\r
681 if( xTestStatus == pdPASS )
\r
683 /* No errors have been reported so increment the loop counter so the
\r
684 check task knows this task is still running. */
\r
688 /*-----------------------------------------------------------*/
\r
690 void vTimerPeriodicISRTests( void )
\r
692 static TickType_t uxTick = ( TickType_t ) -1;
\r
694 #if( configTIMER_TASK_PRIORITY != ( configMAX_PRIORITIES - 1 ) )
\r
695 /* The timer service task is not the highest priority task, so it cannot
\r
696 be assumed that timings will be exact. Timers should never call their
\r
697 callback before their expiry time, but a margin is permissible for calling
\r
698 their callback after their expiry time. If exact timing is required then
\r
699 configTIMER_TASK_PRIORITY must be set to ensure the timer service task
\r
700 is the highest priority task in the system.
\r
702 This function is called from the tick hook. The tick hook is called
\r
703 even when the scheduler is suspended. Therefore it is possible that the
\r
704 uxTick count maintained in this function is temporarily ahead of the tick
\r
705 count maintained by the kernel. When this is the case a message posted from
\r
706 this function will assume a time stamp in advance of the real time stamp,
\r
707 which can result in a timer being processed before this function expects it
\r
708 to. For example, if the kernel's tick count was 100, and uxTick was 102,
\r
709 then this function will not expect the timer to have expired until the
\r
710 kernel's tick count is (102 + xBasePeriod), whereas in reality the timer
\r
711 will expire when the kernel's tick count is (100 + xBasePeriod). For this
\r
712 reason xMargin is used as an allowable margin for premature timer expiries
\r
713 as well as late timer expiries. */
\r
715 /* Windows is not real real time. */
\r
716 const TickType_t xMargin = 20;
\r
718 const TickType_t xMargin = 6;
\r
719 #endif /* _WINDOWS_ */
\r
722 /* Windows is not real real time. */
\r
723 const TickType_t xMargin = 20;
\r
725 const TickType_t xMargin = 4;
\r
726 #endif /* _WINDOWS_ */
\r
734 /* The timers will have been created, but not started. Start them now
\r
735 by setting their period. */
\r
736 ucISRAutoReloadTimerCounter = 0;
\r
737 ucISROneShotTimerCounter = 0;
\r
739 /* It is possible that the timer task has not yet made room in the
\r
740 timer queue. If the timers cannot be started then reset uxTick so
\r
741 another attempt is made later. */
\r
742 uxTick = ( TickType_t ) -1;
\r
744 /* Try starting first timer. */
\r
745 if( xTimerChangePeriodFromISR( xISRAutoReloadTimer, xBasePeriod, NULL ) == pdPASS )
\r
747 /* First timer was started, try starting the second timer. */
\r
748 if( xTimerChangePeriodFromISR( xISROneShotTimer, xBasePeriod, NULL ) == pdPASS )
\r
750 /* Both timers were started, so set the uxTick back to its
\r
756 /* Second timer could not be started, so stop the first one
\r
758 xTimerStopFromISR( xISRAutoReloadTimer, NULL );
\r
762 else if( uxTick == ( xBasePeriod - xMargin ) )
\r
764 /* Neither timer should have expired yet. */
\r
765 if( ( ucISRAutoReloadTimerCounter != 0 ) || ( ucISROneShotTimerCounter != 0 ) )
\r
767 xTestStatus = pdFAIL;
\r
768 configASSERT( xTestStatus );
\r
771 else if( uxTick == ( xBasePeriod + xMargin ) )
\r
773 /* Both timers should now have expired once. The auto reload timer will
\r
774 still be active, but the one shot timer should now have stopped. */
\r
775 if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )
\r
777 xTestStatus = pdFAIL;
\r
778 configASSERT( xTestStatus );
\r
781 else if( uxTick == ( ( 2 * xBasePeriod ) - xMargin ) )
\r
783 /* The auto reload timer will still be active, but the one shot timer
\r
784 should now have stopped - however, at this time neither of the timers
\r
785 should have expired again since the last test. */
\r
786 if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )
\r
788 xTestStatus = pdFAIL;
\r
789 configASSERT( xTestStatus );
\r
792 else if( uxTick == ( ( 2 * xBasePeriod ) + xMargin ) )
\r
794 /* The auto reload timer will still be active, but the one shot timer
\r
795 should now have stopped. At this time the auto reload timer should have
\r
796 expired again, but the one shot timer count should not have changed. */
\r
797 if( ucISRAutoReloadTimerCounter != 2 )
\r
799 xTestStatus = pdFAIL;
\r
800 configASSERT( xTestStatus );
\r
803 if( ucISROneShotTimerCounter != 1 )
\r
805 xTestStatus = pdFAIL;
\r
806 configASSERT( xTestStatus );
\r
809 else if( uxTick == ( ( 2 * xBasePeriod ) + ( xBasePeriod >> ( TickType_t ) 2U ) ) )
\r
811 /* The auto reload timer will still be active, but the one shot timer
\r
812 should now have stopped. Again though, at this time, neither timer call
\r
813 back should have been called since the last test. */
\r
814 if( ucISRAutoReloadTimerCounter != 2 )
\r
816 xTestStatus = pdFAIL;
\r
817 configASSERT( xTestStatus );
\r
820 if( ucISROneShotTimerCounter != 1 )
\r
822 xTestStatus = pdFAIL;
\r
823 configASSERT( xTestStatus );
\r
826 else if( uxTick == ( 3 * xBasePeriod ) )
\r
828 /* Start the one shot timer again. */
\r
829 xTimerStartFromISR( xISROneShotTimer, NULL );
\r
831 else if( uxTick == ( ( 3 * xBasePeriod ) + xMargin ) )
\r
833 /* The auto reload timer and one shot timer will be active. At
\r
834 this time the auto reload timer should have expired again, but the one
\r
835 shot timer count should not have changed yet. */
\r
836 if( ucISRAutoReloadTimerCounter != 3 )
\r
838 xTestStatus = pdFAIL;
\r
839 configASSERT( xTestStatus );
\r
842 if( ucISROneShotTimerCounter != 1 )
\r
844 xTestStatus = pdFAIL;
\r
845 configASSERT( xTestStatus );
\r
848 /* Now stop the auto reload timer. The one shot timer was started
\r
849 a few ticks ago. */
\r
850 xTimerStopFromISR( xISRAutoReloadTimer, NULL );
\r
852 else if( uxTick == ( 4 * ( xBasePeriod - xMargin ) ) )
\r
854 /* The auto reload timer is now stopped, and the one shot timer is
\r
855 active, but at this time neither timer should have expired since the
\r
857 if( ucISRAutoReloadTimerCounter != 3 )
\r
859 xTestStatus = pdFAIL;
\r
860 configASSERT( xTestStatus );
\r
863 if( ucISROneShotTimerCounter != 1 )
\r
865 xTestStatus = pdFAIL;
\r
866 configASSERT( xTestStatus );
\r
869 else if( uxTick == ( ( 4 * xBasePeriod ) + xMargin ) )
\r
871 /* The auto reload timer is now stopped, and the one shot timer is
\r
872 active. The one shot timer should have expired again, but the auto
\r
873 reload timer should not have executed its callback. */
\r
874 if( ucISRAutoReloadTimerCounter != 3 )
\r
876 xTestStatus = pdFAIL;
\r
877 configASSERT( xTestStatus );
\r
880 if( ucISROneShotTimerCounter != 2 )
\r
882 xTestStatus = pdFAIL;
\r
883 configASSERT( xTestStatus );
\r
886 else if( uxTick == ( 8 * xBasePeriod ) )
\r
888 /* The auto reload timer is now stopped, and the one shot timer has
\r
889 already expired and then stopped itself. Both callback counters should
\r
890 not have incremented since the last test. */
\r
891 if( ucISRAutoReloadTimerCounter != 3 )
\r
893 xTestStatus = pdFAIL;
\r
894 configASSERT( xTestStatus );
\r
897 if( ucISROneShotTimerCounter != 2 )
\r
899 xTestStatus = pdFAIL;
\r
900 configASSERT( xTestStatus );
\r
903 /* Now reset the one shot timer. */
\r
904 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
906 else if( uxTick == ( ( 9 * xBasePeriod ) - xMargin ) )
\r
908 /* Only the one shot timer should be running, but it should not have
\r
909 expired since the last test. Check the callback counters have not
\r
910 incremented, then reset the one shot timer again. */
\r
911 if( ucISRAutoReloadTimerCounter != 3 )
\r
913 xTestStatus = pdFAIL;
\r
914 configASSERT( xTestStatus );
\r
917 if( ucISROneShotTimerCounter != 2 )
\r
919 xTestStatus = pdFAIL;
\r
920 configASSERT( xTestStatus );
\r
923 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
925 else if( uxTick == ( ( 10 * xBasePeriod ) - ( 2 * xMargin ) ) )
\r
927 /* Only the one shot timer should be running, but it should not have
\r
928 expired since the last test. Check the callback counters have not
\r
929 incremented, then reset the one shot timer again. */
\r
930 if( ucISRAutoReloadTimerCounter != 3 )
\r
932 xTestStatus = pdFAIL;
\r
933 configASSERT( xTestStatus );
\r
936 if( ucISROneShotTimerCounter != 2 )
\r
938 xTestStatus = pdFAIL;
\r
939 configASSERT( xTestStatus );
\r
942 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
944 else if( uxTick == ( ( 11 * xBasePeriod ) - ( 3 * xMargin ) ) )
\r
946 /* Only the one shot timer should be running, but it should not have
\r
947 expired since the last test. Check the callback counters have not
\r
948 incremented, then reset the one shot timer once again. */
\r
949 if( ucISRAutoReloadTimerCounter != 3 )
\r
951 xTestStatus = pdFAIL;
\r
952 configASSERT( xTestStatus );
\r
955 if( ucISROneShotTimerCounter != 2 )
\r
957 xTestStatus = pdFAIL;
\r
958 configASSERT( xTestStatus );
\r
961 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
963 else if( uxTick == ( ( 12 * xBasePeriod ) - ( 2 * xMargin ) ) )
\r
965 /* Only the one shot timer should have been running and this time it
\r
966 should have expired. Check its callback count has been incremented.
\r
967 The auto reload timer is still not running so should still have the same
\r
968 count value. This time the one shot timer is not reset so should not
\r
969 restart from its expiry period again. */
\r
970 if( ucISRAutoReloadTimerCounter != 3 )
\r
972 xTestStatus = pdFAIL;
\r
973 configASSERT( xTestStatus );
\r
976 if( ucISROneShotTimerCounter != 3 )
\r
978 xTestStatus = pdFAIL;
\r
979 configASSERT( xTestStatus );
\r
982 else if( uxTick == ( 15 * xBasePeriod ) )
\r
984 /* Neither timer should be running now. Check neither callback count
\r
985 has incremented, then go back to the start to run these tests all
\r
987 if( ucISRAutoReloadTimerCounter != 3 )
\r
989 xTestStatus = pdFAIL;
\r
990 configASSERT( xTestStatus );
\r
993 if( ucISROneShotTimerCounter != 3 )
\r
995 xTestStatus = pdFAIL;
\r
996 configASSERT( xTestStatus );
\r
999 uxTick = ( TickType_t ) -1;
\r
1002 /*-----------------------------------------------------------*/
\r
1004 /*** Timer callback functions are defined below here. ***/
\r
1006 static void prvAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer )
\r
1010 uxTimerID = ( size_t ) pvTimerGetTimerID( pxExpiredTimer );
\r
1011 if( uxTimerID <= ( configTIMER_QUEUE_LENGTH + 1 ) )
\r
1013 ( ucAutoReloadTimerCounters[ uxTimerID ] )++;
\r
1017 /* The timer ID appears to be unexpected (invalid). */
\r
1018 xTestStatus = pdFAIL;
\r
1019 configASSERT( xTestStatus );
\r
1022 /*-----------------------------------------------------------*/
\r
1024 static void prvOneShotTimerCallback( TimerHandle_t pxExpiredTimer )
\r
1026 /* A count is kept of the number of times this callback function is executed.
\r
1027 The count is stored as the timer's ID. This is only done to test the
\r
1028 vTimerSetTimerID() function. */
\r
1029 static size_t uxCallCount = 0;
\r
1030 size_t uxLastCallCount;
\r
1032 /* Obtain the timer's ID, which should be a count of the number of times
\r
1033 this callback function has been executed. */
\r
1034 uxLastCallCount = ( size_t ) pvTimerGetTimerID( pxExpiredTimer );
\r
1035 configASSERT( uxLastCallCount == uxCallCount );
\r
1037 /* Increment the call count, then save it back as the timer's ID. This is
\r
1038 only done to test the vTimerSetTimerID() API function. */
\r
1039 uxLastCallCount++;
\r
1040 vTimerSetTimerID( pxExpiredTimer, ( void * ) uxLastCallCount );
\r
1043 ucOneShotTimerCounter++;
\r
1045 /*-----------------------------------------------------------*/
\r
1047 static void prvISRAutoReloadTimerCallback( 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 ucISRAutoReloadTimerCounter++;
\r
1055 /*-----------------------------------------------------------*/
\r
1057 static void prvISROneShotTimerCallback( 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 ucISROneShotTimerCounter++;
\r
1065 /*-----------------------------------------------------------*/
\r