2 * FreeRTOS Kernel V10.0.1
\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.
\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 trmdemoNUM_TIMER_RESETS ( ( uint8_t ) 10 )
\r
53 /*-----------------------------------------------------------*/
\r
55 /* The callback functions used by the timers. These each increment a counter
\r
56 to indicate which timer has expired. The auto-reload timers that are used by
\r
57 the test task (as opposed to being used from an ISR) all share the same
\r
58 prvAutoReloadTimerCallback() callback function, and use the ID of the
\r
59 pxExpiredTimer parameter passed into that function to know which counter to
\r
60 increment. The other timers all have their own unique callback function and
\r
61 simply increment their counters without using the callback function parameter. */
\r
62 static void prvAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer );
\r
63 static void prvOneShotTimerCallback( TimerHandle_t pxExpiredTimer );
\r
64 static void prvTimerTestTask( void *pvParameters );
\r
65 static void prvISRAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer );
\r
66 static void prvISROneShotTimerCallback( TimerHandle_t pxExpiredTimer );
\r
68 /* The test functions used by the timer test task. These manipulate the auto
\r
69 reload and one shot timers in various ways, then delay, then inspect the timers
\r
70 to ensure they have behaved as expected. */
\r
71 static void prvTest1_CreateTimersWithoutSchedulerRunning( void );
\r
72 static void prvTest2_CheckTaskAndTimersInitialState( void );
\r
73 static void prvTest3_CheckAutoReloadExpireRates( void );
\r
74 static void prvTest4_CheckAutoReloadTimersCanBeStopped( void );
\r
75 static void prvTest5_CheckBasicOneShotTimerBehaviour( void );
\r
76 static void prvTest6_CheckAutoReloadResetBehaviour( void );
\r
77 static void prvResetStartConditionsForNextIteration( void );
\r
79 /*-----------------------------------------------------------*/
\r
81 /* Flag that will be latched to pdFAIL should any unexpected behaviour be
\r
82 detected in any of the demo tests. */
\r
83 static volatile BaseType_t xTestStatus = pdPASS;
\r
85 /* Counter that is incremented on each cycle of a test. This is used to
\r
86 detect a stalled task - a test that is no longer running. */
\r
87 static volatile uint32_t ulLoopCounter = 0;
\r
89 /* A set of auto reload timers - each of which use the same callback function.
\r
90 The callback function uses the timer ID to index into, and then increment, a
\r
91 counter in the ucAutoReloadTimerCounters[] array. The auto reload timers
\r
92 referenced from xAutoReloadTimers[] are used by the prvTimerTestTask task. */
\r
93 static TimerHandle_t xAutoReloadTimers[ configTIMER_QUEUE_LENGTH + 1 ] = { 0 };
\r
94 static uint8_t ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH + 1 ] = { 0 };
\r
96 /* The one shot timer is configured to use a callback function that increments
\r
97 ucOneShotTimerCounter each time it gets called. */
\r
98 static TimerHandle_t xOneShotTimer = NULL;
\r
99 static uint8_t ucOneShotTimerCounter = ( uint8_t ) 0;
\r
101 /* The ISR reload timer is controlled from the tick hook to exercise the timer
\r
102 API functions that can be used from an ISR. It is configured to increment
\r
103 ucISRReloadTimerCounter each time its callback function is executed. */
\r
104 static TimerHandle_t xISRAutoReloadTimer = NULL;
\r
105 static uint8_t ucISRAutoReloadTimerCounter = ( uint8_t ) 0;
\r
107 /* The ISR one shot timer is controlled from the tick hook to exercise the timer
\r
108 API functions that can be used from an ISR. It is configured to increment
\r
109 ucISRReloadTimerCounter each time its callback function is executed. */
\r
110 static TimerHandle_t xISROneShotTimer = NULL;
\r
111 static uint8_t ucISROneShotTimerCounter = ( uint8_t ) 0;
\r
113 /* The period of all the timers are a multiple of the base period. The base
\r
114 period is configured by the parameter to vStartTimerDemoTask(). */
\r
115 static TickType_t xBasePeriod = 0;
\r
117 /*-----------------------------------------------------------*/
\r
119 void vStartTimerDemoTask( TickType_t xBasePeriodIn )
\r
121 /* Start with the timer and counter arrays clear - this is only necessary
\r
122 where the compiler does not clear them automatically on start up. */
\r
123 memset( ucAutoReloadTimerCounters, 0x00, sizeof( ucAutoReloadTimerCounters ) );
\r
124 memset( xAutoReloadTimers, 0x00, sizeof( xAutoReloadTimers ) );
\r
126 /* Store the period from which all the timer periods will be generated from
\r
128 xBasePeriod = xBasePeriodIn;
\r
130 /* Create a set of timers for use by this demo/test. */
\r
131 prvTest1_CreateTimersWithoutSchedulerRunning();
\r
133 /* Create the task that will control and monitor the timers. This is
\r
134 created at a lower priority than the timer service task to ensure, as
\r
135 far as it is concerned, commands on timers are actioned immediately
\r
136 (sending a command to the timer service task will unblock the timer service
\r
137 task, which will then preempt this task). */
\r
138 if( xTestStatus != pdFAIL )
\r
140 xTaskCreate( prvTimerTestTask, "Tmr Tst", configMINIMAL_STACK_SIZE, NULL, configTIMER_TASK_PRIORITY - 1, NULL );
\r
143 /*-----------------------------------------------------------*/
\r
145 static void prvTimerTestTask( void *pvParameters )
\r
147 ( void ) pvParameters;
\r
149 /* Create a one-shot timer for use later on in this test. */
\r
150 xOneShotTimer = xTimerCreate( "Oneshot Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
\r
151 tmrdemoONE_SHOT_TIMER_PERIOD, /* The period for the timer. */
\r
152 pdFALSE, /* Don't auto-reload - hence a one shot timer. */
\r
153 ( void * ) 0, /* The timer identifier. Initialise to 0, then increment each time it is called. */
\r
154 prvOneShotTimerCallback ); /* The callback to be called when the timer expires. */
\r
156 if( xOneShotTimer == NULL )
\r
158 xTestStatus = pdFAIL;
\r
159 configASSERT( xTestStatus );
\r
163 /* Ensure all the timers are in their expected initial state. This
\r
164 depends on the timer service task having a higher priority than this task. */
\r
165 prvTest2_CheckTaskAndTimersInitialState();
\r
169 /* Check the auto reload timers expire at the expected/correct rates. */
\r
170 prvTest3_CheckAutoReloadExpireRates();
\r
172 /* Check the auto reload timers can be stopped correctly, and correctly
\r
173 report their state. */
\r
174 prvTest4_CheckAutoReloadTimersCanBeStopped();
\r
176 /* Check the one shot timer only calls its callback once after it has been
\r
177 started, and that it reports its state correctly. */
\r
178 prvTest5_CheckBasicOneShotTimerBehaviour();
\r
180 /* Check timer reset behaviour. */
\r
181 prvTest6_CheckAutoReloadResetBehaviour();
\r
183 /* Start the timers again to restart all the tests over again. */
\r
184 prvResetStartConditionsForNextIteration();
\r
187 /*-----------------------------------------------------------*/
\r
189 /* This is called to check that the created task is still running and has not
\r
190 detected any errors. */
\r
191 BaseType_t xAreTimerDemoTasksStillRunning( TickType_t xCycleFrequency )
\r
193 static uint32_t ulLastLoopCounter = 0UL;
\r
194 TickType_t xMaxBlockTimeUsedByTheseTests, xLoopCounterIncrementTimeMax;
\r
195 static TickType_t xIterationsWithoutCounterIncrement = ( TickType_t ) 0, xLastCycleFrequency;
\r
197 if( xLastCycleFrequency != xCycleFrequency )
\r
199 /* The cycle frequency has probably become much faster due to an error
\r
200 elsewhere. Start counting Iterations again. */
\r
201 xIterationsWithoutCounterIncrement = ( TickType_t ) 0;
\r
202 xLastCycleFrequency = xCycleFrequency;
\r
205 /* Calculate the maximum number of times that it is permissible for this
\r
206 function to be called without ulLoopCounter being incremented. This is
\r
207 necessary because the tests in this file block for extended periods, and the
\r
208 block period might be longer than the time between calls to this function. */
\r
209 xMaxBlockTimeUsedByTheseTests = ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod;
\r
210 xLoopCounterIncrementTimeMax = ( xMaxBlockTimeUsedByTheseTests / xCycleFrequency ) + 1;
\r
212 /* If the demo task is still running then the loop counter is expected to
\r
213 have incremented every xLoopCounterIncrementTimeMax calls. */
\r
214 if( ulLastLoopCounter == ulLoopCounter )
\r
216 xIterationsWithoutCounterIncrement++;
\r
217 if( xIterationsWithoutCounterIncrement > xLoopCounterIncrementTimeMax )
\r
219 /* The tests appear to be no longer running (stalled). */
\r
220 xTestStatus = pdFAIL;
\r
221 configASSERT( xTestStatus );
\r
226 /* ulLoopCounter changed, so the count of times this function was called
\r
227 without a change can be reset to zero. */
\r
228 xIterationsWithoutCounterIncrement = ( TickType_t ) 0;
\r
231 ulLastLoopCounter = ulLoopCounter;
\r
233 /* Errors detected in the task itself will have latched xTestStatus
\r
236 return xTestStatus;
\r
238 /*-----------------------------------------------------------*/
\r
240 static void prvTest1_CreateTimersWithoutSchedulerRunning( void )
\r
244 for( xTimer = 0; xTimer < configTIMER_QUEUE_LENGTH; xTimer++ )
\r
246 /* As the timer queue is not yet full, it should be possible to both
\r
247 create and start a timer. These timers are being started before the
\r
248 scheduler has been started, so their block times should get set to zero
\r
249 within the timer API itself. */
\r
250 xAutoReloadTimers[ xTimer ] = xTimerCreate( "FR Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
\r
251 ( ( xTimer + ( TickType_t ) 1 ) * xBasePeriod ),/* The period for the timer. The plus 1 ensures a period of zero is not specified. */
\r
252 pdTRUE, /* Auto-reload is set to true. */
\r
253 ( void * ) xTimer, /* An identifier for the timer as all the auto reload timers use the same callback. */
\r
254 prvAutoReloadTimerCallback ); /* The callback to be called when the timer expires. */
\r
256 if( xAutoReloadTimers[ xTimer ] == NULL )
\r
258 xTestStatus = pdFAIL;
\r
259 configASSERT( xTestStatus );
\r
263 configASSERT( strcmp( pcTimerGetName( xAutoReloadTimers[ xTimer ] ), "FR Timer" ) == 0 );
\r
265 /* The scheduler has not yet started, so the block period of
\r
266 portMAX_DELAY should just get set to zero in xTimerStart(). Also,
\r
267 the timer queue is not yet full so xTimerStart() should return
\r
269 if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) != pdPASS )
\r
271 xTestStatus = pdFAIL;
\r
272 configASSERT( xTestStatus );
\r
277 /* The timers queue should now be full, so it should be possible to create
\r
278 another timer, but not possible to start it (the timer queue will not get
\r
279 drained until the scheduler has been started. */
\r
280 xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] = xTimerCreate( "FR Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
\r
281 ( configTIMER_QUEUE_LENGTH * xBasePeriod ), /* The period for the timer. */
\r
282 pdTRUE, /* Auto-reload is set to true. */
\r
283 ( void * ) xTimer, /* An identifier for the timer as all the auto reload timers use the same callback. */
\r
284 prvAutoReloadTimerCallback ); /* The callback executed when the timer expires. */
\r
286 if( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] == NULL )
\r
288 xTestStatus = pdFAIL;
\r
289 configASSERT( xTestStatus );
\r
293 if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) == pdPASS )
\r
295 /* This time it would not be expected that the timer could be
\r
296 started at this point. */
\r
297 xTestStatus = pdFAIL;
\r
298 configASSERT( xTestStatus );
\r
302 /* Create the timers that are used from the tick interrupt to test the timer
\r
303 API functions that can be called from an ISR. */
\r
304 xISRAutoReloadTimer = xTimerCreate( "ISR AR", /* The text name given to the timer. */
\r
305 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
306 pdTRUE, /* This is an auto reload timer. */
\r
307 ( void * ) NULL, /* The identifier is not required. */
\r
308 prvISRAutoReloadTimerCallback ); /* The callback that is executed when the timer expires. */
\r
310 xISROneShotTimer = xTimerCreate( "ISR OS", /* The text name given to the timer. */
\r
311 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
312 pdFALSE, /* This is a one shot timer. */
\r
313 ( void * ) NULL, /* The identifier is not required. */
\r
314 prvISROneShotTimerCallback ); /* The callback that is executed when the timer expires. */
\r
316 if( ( xISRAutoReloadTimer == NULL ) || ( xISROneShotTimer == NULL ) )
\r
318 xTestStatus = pdFAIL;
\r
319 configASSERT( xTestStatus );
\r
322 /*-----------------------------------------------------------*/
\r
324 static void prvTest2_CheckTaskAndTimersInitialState( void )
\r
328 /* Ensure all the timers are in their expected initial state. This depends
\r
329 on the timer service task having a higher priority than this task.
\r
331 auto reload timers 0 to ( configTIMER_QUEUE_LENGTH - 1 ) should now be active,
\r
332 and auto reload timer configTIMER_QUEUE_LENGTH should not yet be active (it
\r
333 could not be started prior to the scheduler being started when it was
\r
335 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
337 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
339 xTestStatus = pdFAIL;
\r
340 configASSERT( xTestStatus );
\r
344 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] ) != pdFALSE )
\r
346 xTestStatus = pdFAIL;
\r
347 configASSERT( xTestStatus );
\r
350 /*-----------------------------------------------------------*/
\r
352 static void prvTest3_CheckAutoReloadExpireRates( void )
\r
354 uint8_t ucMaxAllowableValue, ucMinAllowableValue, ucTimer;
\r
355 TickType_t xBlockPeriod, xTimerPeriod, xExpectedNumber;
\r
356 UBaseType_t uxOriginalPriority;
\r
358 /* Check the auto reload timers expire at the expected rates. Do this at a
\r
359 high priority for maximum accuracy. This is ok as most of the time is spent
\r
360 in the Blocked state. */
\r
361 uxOriginalPriority = uxTaskPriorityGet( NULL );
\r
362 vTaskPrioritySet( NULL, ( configMAX_PRIORITIES - 1 ) );
\r
364 /* Delaying for configTIMER_QUEUE_LENGTH * xBasePeriod ticks should allow
\r
365 all the auto reload timers to expire at least once. */
\r
366 xBlockPeriod = ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod;
\r
367 vTaskDelay( xBlockPeriod );
\r
369 /* Check that all the auto reload timers have called their callback
\r
370 function the expected number of times. */
\r
371 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
373 /* The expected number of expiries is equal to the block period divided
\r
374 by the timer period. */
\r
375 xTimerPeriod = ( ( ( TickType_t ) ucTimer + ( TickType_t ) 1 ) * xBasePeriod );
\r
376 xExpectedNumber = xBlockPeriod / xTimerPeriod;
\r
378 ucMaxAllowableValue = ( ( uint8_t ) xExpectedNumber ) ;
\r
379 ucMinAllowableValue = ( uint8_t ) ( ( uint8_t ) xExpectedNumber - ( uint8_t ) 1 ); /* Weird casting to try and please all compilers. */
\r
381 if( ( ucAutoReloadTimerCounters[ ucTimer ] < ucMinAllowableValue ) ||
\r
382 ( ucAutoReloadTimerCounters[ ucTimer ] > ucMaxAllowableValue )
\r
385 xTestStatus = pdFAIL;
\r
386 configASSERT( xTestStatus );
\r
390 /* Return to the original priority. */
\r
391 vTaskPrioritySet( NULL, uxOriginalPriority );
\r
393 if( xTestStatus == pdPASS )
\r
395 /* No errors have been reported so increment the loop counter so the
\r
396 check task knows this task is still running. */
\r
400 /*-----------------------------------------------------------*/
\r
402 static void prvTest4_CheckAutoReloadTimersCanBeStopped( void )
\r
406 /* Check the auto reload timers can be stopped correctly, and correctly
\r
407 report their state. */
\r
409 /* Stop all the active timers. */
\r
410 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
412 /* The timer has not been stopped yet! */
\r
413 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
415 xTestStatus = pdFAIL;
\r
416 configASSERT( xTestStatus );
\r
419 /* Now stop the timer. This will appear to happen immediately to
\r
420 this task because this task is running at a priority below the
\r
421 timer service task. */
\r
422 xTimerStop( xAutoReloadTimers[ ucTimer ], tmrdemoDONT_BLOCK );
\r
424 /* The timer should now be inactive. */
\r
425 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )
\r
427 xTestStatus = pdFAIL;
\r
428 configASSERT( xTestStatus );
\r
432 taskENTER_CRITICAL();
\r
434 /* The timer in array position configTIMER_QUEUE_LENGTH should not
\r
435 be active. The critical section is used to ensure the timer does
\r
436 not call its callback between the next line running and the array
\r
437 being cleared back to zero, as that would mask an error condition. */
\r
438 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH ] != ( uint8_t ) 0 )
\r
440 xTestStatus = pdFAIL;
\r
441 configASSERT( xTestStatus );
\r
444 /* Clear the timer callback count. */
\r
445 memset( ( void * ) ucAutoReloadTimerCounters, 0, sizeof( ucAutoReloadTimerCounters ) );
\r
447 taskEXIT_CRITICAL();
\r
449 /* The timers are now all inactive, so this time, after delaying, none
\r
450 of the callback counters should have incremented. */
\r
451 vTaskDelay( ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod );
\r
452 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
454 if( ucAutoReloadTimerCounters[ ucTimer ] != ( uint8_t ) 0 )
\r
456 xTestStatus = pdFAIL;
\r
457 configASSERT( xTestStatus );
\r
461 if( xTestStatus == pdPASS )
\r
463 /* No errors have been reported so increment the loop counter so
\r
464 the check task knows this task is still running. */
\r
468 /*-----------------------------------------------------------*/
\r
470 static void prvTest5_CheckBasicOneShotTimerBehaviour( void )
\r
472 /* Check the one shot timer only calls its callback once after it has been
\r
473 started, and that it reports its state correctly. */
\r
475 /* The one shot timer should not be active yet. */
\r
476 if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )
\r
478 xTestStatus = pdFAIL;
\r
479 configASSERT( xTestStatus );
\r
482 if( ucOneShotTimerCounter != ( uint8_t ) 0 )
\r
484 xTestStatus = pdFAIL;
\r
485 configASSERT( xTestStatus );
\r
488 /* Start the one shot timer and check that it reports its state correctly. */
\r
489 xTimerStart( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
490 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
492 xTestStatus = pdFAIL;
\r
493 configASSERT( xTestStatus );
\r
496 /* Delay for three times as long as the one shot timer period, then check
\r
497 to ensure it has only called its callback once, and is now not in the
\r
499 vTaskDelay( tmrdemoONE_SHOT_TIMER_PERIOD * ( TickType_t ) 3 );
\r
501 if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )
\r
503 xTestStatus = pdFAIL;
\r
504 configASSERT( xTestStatus );
\r
507 if( ucOneShotTimerCounter != ( uint8_t ) 1 )
\r
509 xTestStatus = pdFAIL;
\r
510 configASSERT( xTestStatus );
\r
514 /* Reset the one shot timer callback count. */
\r
515 ucOneShotTimerCounter = ( uint8_t ) 0;
\r
518 if( xTestStatus == pdPASS )
\r
520 /* No errors have been reported so increment the loop counter so the
\r
521 check task knows this task is still running. */
\r
525 /*-----------------------------------------------------------*/
\r
527 static void prvTest6_CheckAutoReloadResetBehaviour( void )
\r
531 /* Check timer reset behaviour. */
\r
533 /* Restart the one shot timer and check it reports its status correctly. */
\r
534 xTimerStart( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
535 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
537 xTestStatus = pdFAIL;
\r
538 configASSERT( xTestStatus );
\r
541 /* Restart one of the auto reload timers and check that it reports its
\r
542 status correctly. */
\r
543 xTimerStart( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
544 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
546 xTestStatus = pdFAIL;
\r
547 configASSERT( xTestStatus );
\r
550 for( ucTimer = 0; ucTimer < trmdemoNUM_TIMER_RESETS; ucTimer++ )
\r
552 /* Delay for half as long as the one shot timer period, then reset it.
\r
553 It should never expire while this is done, so its callback count should
\r
554 never increment. */
\r
555 vTaskDelay( tmrdemoONE_SHOT_TIMER_PERIOD / 2 );
\r
557 /* Check both running timers are still active, but have not called their
\r
558 callback functions. */
\r
559 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
561 xTestStatus = pdFAIL;
\r
562 configASSERT( xTestStatus );
\r
565 if( ucOneShotTimerCounter != ( uint8_t ) 0 )
\r
567 xTestStatus = pdFAIL;
\r
568 configASSERT( xTestStatus );
\r
571 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
573 xTestStatus = pdFAIL;
\r
574 configASSERT( xTestStatus );
\r
577 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] != ( uint8_t ) 0 )
\r
579 xTestStatus = pdFAIL;
\r
580 configASSERT( xTestStatus );
\r
583 /* Reset both running timers. */
\r
584 xTimerReset( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
585 xTimerReset( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
587 if( xTestStatus == pdPASS )
\r
589 /* No errors have been reported so increment the loop counter so
\r
590 the check task knows this task is still running. */
\r
595 /* Finally delay long enough for both running timers to expire. */
\r
596 vTaskDelay( ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod );
\r
598 /* The timers were not reset during the above delay period so should now
\r
599 both have called their callback functions. */
\r
600 if( ucOneShotTimerCounter != ( uint8_t ) 1 )
\r
602 xTestStatus = pdFAIL;
\r
603 configASSERT( xTestStatus );
\r
606 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] == 0 )
\r
608 xTestStatus = pdFAIL;
\r
609 configASSERT( xTestStatus );
\r
612 /* The one shot timer should no longer be active, while the auto reload
\r
613 timer should still be active. */
\r
614 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
616 xTestStatus = pdFAIL;
\r
617 configASSERT( xTestStatus );
\r
620 if( xTimerIsTimerActive( xOneShotTimer ) == pdTRUE )
\r
622 xTestStatus = pdFAIL;
\r
623 configASSERT( xTestStatus );
\r
626 /* Stop the auto reload timer again. */
\r
627 xTimerStop( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
629 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) != pdFALSE )
\r
631 xTestStatus = pdFAIL;
\r
632 configASSERT( xTestStatus );
\r
635 /* Clear the timer callback counts, ready for another iteration of these
\r
637 ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] = ( uint8_t ) 0;
\r
638 ucOneShotTimerCounter = ( uint8_t ) 0;
\r
640 if( xTestStatus == pdPASS )
\r
642 /* No errors have been reported so increment the loop counter so the check
\r
643 task knows this task is still running. */
\r
647 /*-----------------------------------------------------------*/
\r
649 static void prvResetStartConditionsForNextIteration( void )
\r
653 /* Start the timers again to start all the tests over again. */
\r
655 /* Start the timers again. */
\r
656 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
658 /* The timer has not been started yet! */
\r
659 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )
\r
661 xTestStatus = pdFAIL;
\r
662 configASSERT( xTestStatus );
\r
665 /* Now start the timer. This will appear to happen immediately to
\r
666 this task because this task is running at a priority below the timer
\r
668 xTimerStart( xAutoReloadTimers[ ucTimer ], tmrdemoDONT_BLOCK );
\r
670 /* The timer should now be active. */
\r
671 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
673 xTestStatus = pdFAIL;
\r
674 configASSERT( xTestStatus );
\r
678 if( xTestStatus == pdPASS )
\r
680 /* No errors have been reported so increment the loop counter so the
\r
681 check task knows this task is still running. */
\r
685 /*-----------------------------------------------------------*/
\r
687 void vTimerPeriodicISRTests( void )
\r
689 static TickType_t uxTick = ( TickType_t ) -1;
\r
691 #if( configTIMER_TASK_PRIORITY != ( configMAX_PRIORITIES - 1 ) )
\r
692 /* The timer service task is not the highest priority task, so it cannot
\r
693 be assumed that timings will be exact. Timers should never call their
\r
694 callback before their expiry time, but a margin is permissible for calling
\r
695 their callback after their expiry time. If exact timing is required then
\r
696 configTIMER_TASK_PRIORITY must be set to ensure the timer service task
\r
697 is the highest priority task in the system.
\r
699 This function is called from the tick hook. The tick hook is called
\r
700 even when the scheduler is suspended. Therefore it is possible that the
\r
701 uxTick count maintained in this function is temporarily ahead of the tick
\r
702 count maintained by the kernel. When this is the case a message posted from
\r
703 this function will assume a time stamp in advance of the real time stamp,
\r
704 which can result in a timer being processed before this function expects it
\r
705 to. For example, if the kernel's tick count was 100, and uxTick was 102,
\r
706 then this function will not expect the timer to have expired until the
\r
707 kernel's tick count is (102 + xBasePeriod), whereas in reality the timer
\r
708 will expire when the kernel's tick count is (100 + xBasePeriod). For this
\r
709 reason xMargin is used as an allowable margin for premature timer expiries
\r
710 as well as late timer expiries. */
\r
712 /* Windows is not real real time. */
\r
713 const TickType_t xMargin = 20;
\r
715 const TickType_t xMargin = 6;
\r
716 #endif /* _WINDOWS_ */
\r
719 /* Windows is not real real time. */
\r
720 const TickType_t xMargin = 20;
\r
722 const TickType_t xMargin = 4;
\r
723 #endif /* _WINDOWS_ */
\r
731 /* The timers will have been created, but not started. Start them now
\r
732 by setting their period. */
\r
733 ucISRAutoReloadTimerCounter = 0;
\r
734 ucISROneShotTimerCounter = 0;
\r
736 /* It is possible that the timer task has not yet made room in the
\r
737 timer queue. If the timers cannot be started then reset uxTick so
\r
738 another attempt is made later. */
\r
739 uxTick = ( TickType_t ) -1;
\r
741 /* Try starting first timer. */
\r
742 if( xTimerChangePeriodFromISR( xISRAutoReloadTimer, xBasePeriod, NULL ) == pdPASS )
\r
744 /* First timer was started, try starting the second timer. */
\r
745 if( xTimerChangePeriodFromISR( xISROneShotTimer, xBasePeriod, NULL ) == pdPASS )
\r
747 /* Both timers were started, so set the uxTick back to its
\r
753 /* Second timer could not be started, so stop the first one
\r
755 xTimerStopFromISR( xISRAutoReloadTimer, NULL );
\r
759 else if( uxTick == ( xBasePeriod - xMargin ) )
\r
761 /* Neither timer should have expired yet. */
\r
762 if( ( ucISRAutoReloadTimerCounter != 0 ) || ( ucISROneShotTimerCounter != 0 ) )
\r
764 xTestStatus = pdFAIL;
\r
765 configASSERT( xTestStatus );
\r
768 else if( uxTick == ( xBasePeriod + xMargin ) )
\r
770 /* Both timers should now have expired once. The auto reload timer will
\r
771 still be active, but the one shot timer should now have stopped. */
\r
772 if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )
\r
774 xTestStatus = pdFAIL;
\r
775 configASSERT( xTestStatus );
\r
778 else if( uxTick == ( ( 2 * xBasePeriod ) - xMargin ) )
\r
780 /* The auto reload timer will still be active, but the one shot timer
\r
781 should now have stopped - however, at this time neither of the timers
\r
782 should have expired again since the last test. */
\r
783 if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )
\r
785 xTestStatus = pdFAIL;
\r
786 configASSERT( xTestStatus );
\r
789 else if( uxTick == ( ( 2 * xBasePeriod ) + xMargin ) )
\r
791 /* The auto reload timer will still be active, but the one shot timer
\r
792 should now have stopped. At this time the auto reload timer should have
\r
793 expired again, but the one shot timer count should not have changed. */
\r
794 if( ucISRAutoReloadTimerCounter != 2 )
\r
796 xTestStatus = pdFAIL;
\r
797 configASSERT( xTestStatus );
\r
800 if( ucISROneShotTimerCounter != 1 )
\r
802 xTestStatus = pdFAIL;
\r
803 configASSERT( xTestStatus );
\r
806 else if( uxTick == ( ( 2 * xBasePeriod ) + ( xBasePeriod >> ( TickType_t ) 2U ) ) )
\r
808 /* The auto reload timer will still be active, but the one shot timer
\r
809 should now have stopped. Again though, at this time, neither timer call
\r
810 back should have been called since the last test. */
\r
811 if( ucISRAutoReloadTimerCounter != 2 )
\r
813 xTestStatus = pdFAIL;
\r
814 configASSERT( xTestStatus );
\r
817 if( ucISROneShotTimerCounter != 1 )
\r
819 xTestStatus = pdFAIL;
\r
820 configASSERT( xTestStatus );
\r
823 else if( uxTick == ( 3 * xBasePeriod ) )
\r
825 /* Start the one shot timer again. */
\r
826 xTimerStartFromISR( xISROneShotTimer, NULL );
\r
828 else if( uxTick == ( ( 3 * xBasePeriod ) + xMargin ) )
\r
830 /* The auto reload timer and one shot timer will be active. At
\r
831 this time the auto reload timer should have expired again, but the one
\r
832 shot timer count should not have changed yet. */
\r
833 if( ucISRAutoReloadTimerCounter != 3 )
\r
835 xTestStatus = pdFAIL;
\r
836 configASSERT( xTestStatus );
\r
839 if( ucISROneShotTimerCounter != 1 )
\r
841 xTestStatus = pdFAIL;
\r
842 configASSERT( xTestStatus );
\r
845 /* Now stop the auto reload timer. The one shot timer was started
\r
846 a few ticks ago. */
\r
847 xTimerStopFromISR( xISRAutoReloadTimer, NULL );
\r
849 else if( uxTick == ( 4 * ( xBasePeriod - xMargin ) ) )
\r
851 /* The auto reload timer is now stopped, and the one shot timer is
\r
852 active, but at this time neither timer should have expired since the
\r
854 if( ucISRAutoReloadTimerCounter != 3 )
\r
856 xTestStatus = pdFAIL;
\r
857 configASSERT( xTestStatus );
\r
860 if( ucISROneShotTimerCounter != 1 )
\r
862 xTestStatus = pdFAIL;
\r
863 configASSERT( xTestStatus );
\r
866 else if( uxTick == ( ( 4 * xBasePeriod ) + xMargin ) )
\r
868 /* The auto reload timer is now stopped, and the one shot timer is
\r
869 active. The one shot timer should have expired again, but the auto
\r
870 reload timer should not have executed its callback. */
\r
871 if( ucISRAutoReloadTimerCounter != 3 )
\r
873 xTestStatus = pdFAIL;
\r
874 configASSERT( xTestStatus );
\r
877 if( ucISROneShotTimerCounter != 2 )
\r
879 xTestStatus = pdFAIL;
\r
880 configASSERT( xTestStatus );
\r
883 else if( uxTick == ( 8 * xBasePeriod ) )
\r
885 /* The auto reload timer is now stopped, and the one shot timer has
\r
886 already expired and then stopped itself. Both callback counters should
\r
887 not have incremented since the last test. */
\r
888 if( ucISRAutoReloadTimerCounter != 3 )
\r
890 xTestStatus = pdFAIL;
\r
891 configASSERT( xTestStatus );
\r
894 if( ucISROneShotTimerCounter != 2 )
\r
896 xTestStatus = pdFAIL;
\r
897 configASSERT( xTestStatus );
\r
900 /* Now reset the one shot timer. */
\r
901 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
903 else if( uxTick == ( ( 9 * xBasePeriod ) - xMargin ) )
\r
905 /* Only the one shot timer should be running, but it should not have
\r
906 expired since the last test. Check the callback counters have not
\r
907 incremented, then reset the one shot timer again. */
\r
908 if( ucISRAutoReloadTimerCounter != 3 )
\r
910 xTestStatus = pdFAIL;
\r
911 configASSERT( xTestStatus );
\r
914 if( ucISROneShotTimerCounter != 2 )
\r
916 xTestStatus = pdFAIL;
\r
917 configASSERT( xTestStatus );
\r
920 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
922 else if( uxTick == ( ( 10 * xBasePeriod ) - ( 2 * xMargin ) ) )
\r
924 /* Only the one shot timer should be running, but it should not have
\r
925 expired since the last test. Check the callback counters have not
\r
926 incremented, then reset the one shot timer again. */
\r
927 if( ucISRAutoReloadTimerCounter != 3 )
\r
929 xTestStatus = pdFAIL;
\r
930 configASSERT( xTestStatus );
\r
933 if( ucISROneShotTimerCounter != 2 )
\r
935 xTestStatus = pdFAIL;
\r
936 configASSERT( xTestStatus );
\r
939 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
941 else if( uxTick == ( ( 11 * xBasePeriod ) - ( 3 * xMargin ) ) )
\r
943 /* Only the one shot timer should be running, but it should not have
\r
944 expired since the last test. Check the callback counters have not
\r
945 incremented, then reset the one shot timer once again. */
\r
946 if( ucISRAutoReloadTimerCounter != 3 )
\r
948 xTestStatus = pdFAIL;
\r
949 configASSERT( xTestStatus );
\r
952 if( ucISROneShotTimerCounter != 2 )
\r
954 xTestStatus = pdFAIL;
\r
955 configASSERT( xTestStatus );
\r
958 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
960 else if( uxTick == ( ( 12 * xBasePeriod ) - ( 2 * xMargin ) ) )
\r
962 /* Only the one shot timer should have been running and this time it
\r
963 should have expired. Check its callback count has been incremented.
\r
964 The auto reload timer is still not running so should still have the same
\r
965 count value. This time the one shot timer is not reset so should not
\r
966 restart from its expiry period again. */
\r
967 if( ucISRAutoReloadTimerCounter != 3 )
\r
969 xTestStatus = pdFAIL;
\r
970 configASSERT( xTestStatus );
\r
973 if( ucISROneShotTimerCounter != 3 )
\r
975 xTestStatus = pdFAIL;
\r
976 configASSERT( xTestStatus );
\r
979 else if( uxTick == ( 15 * xBasePeriod ) )
\r
981 /* Neither timer should be running now. Check neither callback count
\r
982 has incremented, then go back to the start to run these tests all
\r
984 if( ucISRAutoReloadTimerCounter != 3 )
\r
986 xTestStatus = pdFAIL;
\r
987 configASSERT( xTestStatus );
\r
990 if( ucISROneShotTimerCounter != 3 )
\r
992 xTestStatus = pdFAIL;
\r
993 configASSERT( xTestStatus );
\r
996 uxTick = ( TickType_t ) -1;
\r
999 /*-----------------------------------------------------------*/
\r
1001 /*** Timer callback functions are defined below here. ***/
\r
1003 static void prvAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer )
\r
1007 uxTimerID = ( size_t ) pvTimerGetTimerID( pxExpiredTimer );
\r
1008 if( uxTimerID <= ( configTIMER_QUEUE_LENGTH + 1 ) )
\r
1010 ( ucAutoReloadTimerCounters[ uxTimerID ] )++;
\r
1014 /* The timer ID appears to be unexpected (invalid). */
\r
1015 xTestStatus = pdFAIL;
\r
1016 configASSERT( xTestStatus );
\r
1019 /*-----------------------------------------------------------*/
\r
1021 static void prvOneShotTimerCallback( TimerHandle_t pxExpiredTimer )
\r
1023 /* A count is kept of the number of times this callback function is executed.
\r
1024 The count is stored as the timer's ID. This is only done to test the
\r
1025 vTimerSetTimerID() function. */
\r
1026 static size_t uxCallCount = 0;
\r
1027 size_t uxLastCallCount;
\r
1029 /* Obtain the timer's ID, which should be a count of the number of times
\r
1030 this callback function has been executed. */
\r
1031 uxLastCallCount = ( size_t ) pvTimerGetTimerID( pxExpiredTimer );
\r
1032 configASSERT( uxLastCallCount == uxCallCount );
\r
1034 /* Increment the call count, then save it back as the timer's ID. This is
\r
1035 only done to test the vTimerSetTimerID() API function. */
\r
1036 uxLastCallCount++;
\r
1037 vTimerSetTimerID( pxExpiredTimer, ( void * ) uxLastCallCount );
\r
1040 ucOneShotTimerCounter++;
\r
1042 /*-----------------------------------------------------------*/
\r
1044 static void prvISRAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer )
\r
1046 /* The parameter is not used in this case as only one timer uses this
\r
1047 callback function. */
\r
1048 ( void ) pxExpiredTimer;
\r
1050 ucISRAutoReloadTimerCounter++;
\r
1052 /*-----------------------------------------------------------*/
\r
1054 static void prvISROneShotTimerCallback( TimerHandle_t pxExpiredTimer )
\r
1056 /* The parameter is not used in this case as only one timer uses this
\r
1057 callback function. */
\r
1058 ( void ) pxExpiredTimer;
\r
1060 ucISROneShotTimerCounter++;
\r
1062 /*-----------------------------------------------------------*/
\r