2 FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
72 * Tests the behaviour of timers. Some timers are created before the scheduler
\r
73 * is started, and some after.
\r
76 /* Standard includes. */
\r
79 /* Scheduler include files. */
\r
80 #include "FreeRTOS.h"
\r
84 /* Demo program include files. */
\r
85 #include "TimerDemo.h"
\r
87 #if ( configTIMER_TASK_PRIORITY < 1 )
\r
88 #error configTIMER_TASK_PRIORITY must be set to at least 1 for this test/demo to function correctly.
\r
91 #define tmrdemoDONT_BLOCK ( ( TickType_t ) 0 )
\r
92 #define tmrdemoONE_SHOT_TIMER_PERIOD ( xBasePeriod * ( TickType_t ) 3 )
\r
93 #define trmdemoNUM_TIMER_RESETS ( ( uint8_t ) 10 )
\r
95 /*-----------------------------------------------------------*/
\r
97 /* The callback functions used by the timers. These each increment a counter
\r
98 to indicate which timer has expired. The auto-reload timers that are used by
\r
99 the test task (as opposed to being used from an ISR) all share the same
\r
100 prvAutoReloadTimerCallback() callback function, and use the ID of the
\r
101 pxExpiredTimer parameter passed into that function to know which counter to
\r
102 increment. The other timers all have their own unique callback function and
\r
103 simply increment their counters without using the callback function parameter. */
\r
104 static void prvAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer );
\r
105 static void prvOneShotTimerCallback( TimerHandle_t pxExpiredTimer );
\r
106 static void prvTimerTestTask( void *pvParameters );
\r
107 static void prvISRAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer );
\r
108 static void prvISROneShotTimerCallback( TimerHandle_t pxExpiredTimer );
\r
110 /* The test functions used by the timer test task. These manipulate the auto
\r
111 reload and one shot timers in various ways, then delay, then inspect the timers
\r
112 to ensure they have behaved as expected. */
\r
113 static void prvTest1_CreateTimersWithoutSchedulerRunning( void );
\r
114 static void prvTest2_CheckTaskAndTimersInitialState( void );
\r
115 static void prvTest3_CheckAutoReloadExpireRates( void );
\r
116 static void prvTest4_CheckAutoReloadTimersCanBeStopped( void );
\r
117 static void prvTest5_CheckBasicOneShotTimerBehaviour( void );
\r
118 static void prvTest6_CheckAutoReloadResetBehaviour( void );
\r
119 static void prvResetStartConditionsForNextIteration( void );
\r
121 /*-----------------------------------------------------------*/
\r
123 /* Flag that will be latched to pdFAIL should any unexpected behaviour be
\r
124 detected in any of the demo tests. */
\r
125 static volatile BaseType_t xTestStatus = pdPASS;
\r
127 /* Counter that is incremented on each cycle of a test. This is used to
\r
128 detect a stalled task - a test that is no longer running. */
\r
129 static volatile uint32_t ulLoopCounter = 0;
\r
131 /* A set of auto reload timers - each of which use the same callback function.
\r
132 The callback function uses the timer ID to index into, and then increment, a
\r
133 counter in the ucAutoReloadTimerCounters[] array. The auto reload timers
\r
134 referenced from xAutoReloadTimers[] are used by the prvTimerTestTask task. */
\r
135 static TimerHandle_t xAutoReloadTimers[ configTIMER_QUEUE_LENGTH + 1 ] = { 0 };
\r
136 static uint8_t ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH + 1 ] = { 0 };
\r
138 /* The one shot timer is configured to use a callback function that increments
\r
139 ucOneShotTimerCounter each time it gets called. */
\r
140 static TimerHandle_t xOneShotTimer = NULL;
\r
141 static uint8_t ucOneShotTimerCounter = ( uint8_t ) 0;
\r
143 /* The ISR reload timer is controlled from the tick hook to exercise the timer
\r
144 API functions that can be used from an ISR. It is configured to increment
\r
145 ucISRReloadTimerCounter each time its callback function is executed. */
\r
146 static TimerHandle_t xISRAutoReloadTimer = NULL;
\r
147 static uint8_t ucISRAutoReloadTimerCounter = ( uint8_t ) 0;
\r
149 /* The ISR one shot timer is controlled from the tick hook to exercise the timer
\r
150 API functions that can be used from an ISR. It is configured to increment
\r
151 ucISRReloadTimerCounter each time its callback function is executed. */
\r
152 static TimerHandle_t xISROneShotTimer = NULL;
\r
153 static uint8_t ucISROneShotTimerCounter = ( uint8_t ) 0;
\r
155 /* The period of all the timers are a multiple of the base period. The base
\r
156 period is configured by the parameter to vStartTimerDemoTask(). */
\r
157 static TickType_t xBasePeriod = 0;
\r
159 /*-----------------------------------------------------------*/
\r
161 void vStartTimerDemoTask( TickType_t xBasePeriodIn )
\r
163 /* Start with the timer and counter arrays clear - this is only necessary
\r
164 where the compiler does not clear them automatically on start up. */
\r
165 memset( ucAutoReloadTimerCounters, 0x00, sizeof( ucAutoReloadTimerCounters ) );
\r
166 memset( xAutoReloadTimers, 0x00, sizeof( xAutoReloadTimers ) );
\r
168 /* Store the period from which all the timer periods will be generated from
\r
170 xBasePeriod = xBasePeriodIn;
\r
172 /* Create a set of timers for use by this demo/test. */
\r
173 prvTest1_CreateTimersWithoutSchedulerRunning();
\r
175 /* Create the task that will control and monitor the timers. This is
\r
176 created at a lower priority than the timer service task to ensure, as
\r
177 far as it is concerned, commands on timers are actioned immediately
\r
178 (sending a command to the timer service task will unblock the timer service
\r
179 task, which will then preempt this task). */
\r
180 if( xTestStatus != pdFAIL )
\r
182 xTaskCreate( prvTimerTestTask, "Tmr Tst", configMINIMAL_STACK_SIZE, NULL, configTIMER_TASK_PRIORITY - 1, NULL );
\r
185 /*-----------------------------------------------------------*/
\r
187 static void prvTimerTestTask( void *pvParameters )
\r
189 ( void ) pvParameters;
\r
191 /* Create a one-shot timer for use later on in this test. */
\r
192 xOneShotTimer = xTimerCreate( "Oneshot Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
\r
193 tmrdemoONE_SHOT_TIMER_PERIOD, /* The period for the timer. */
\r
194 pdFALSE, /* Don't auto-reload - hence a one shot timer. */
\r
195 ( void * ) 0, /* The timer identifier. In this case this is not used as the timer has its own callback. */
\r
196 prvOneShotTimerCallback ); /* The callback to be called when the timer expires. */
\r
198 if( xOneShotTimer == NULL )
\r
200 xTestStatus = pdFAIL;
\r
201 configASSERT( xTestStatus );
\r
205 /* Ensure all the timers are in their expected initial state. This
\r
206 depends on the timer service task having a higher priority than this task. */
\r
207 prvTest2_CheckTaskAndTimersInitialState();
\r
211 /* Check the auto reload timers expire at the expected/correct rates. */
\r
212 prvTest3_CheckAutoReloadExpireRates();
\r
214 /* Check the auto reload timers can be stopped correctly, and correctly
\r
215 report their state. */
\r
216 prvTest4_CheckAutoReloadTimersCanBeStopped();
\r
218 /* Check the one shot timer only calls its callback once after it has been
\r
219 started, and that it reports its state correctly. */
\r
220 prvTest5_CheckBasicOneShotTimerBehaviour();
\r
222 /* Check timer reset behaviour. */
\r
223 prvTest6_CheckAutoReloadResetBehaviour();
\r
225 /* Start the timers again to restart all the tests over again. */
\r
226 prvResetStartConditionsForNextIteration();
\r
229 /*-----------------------------------------------------------*/
\r
231 /* This is called to check that the created task is still running and has not
\r
232 detected any errors. */
\r
233 BaseType_t xAreTimerDemoTasksStillRunning( TickType_t xCycleFrequency )
\r
235 static uint32_t ulLastLoopCounter = 0UL;
\r
236 TickType_t xMaxBlockTimeUsedByTheseTests, xLoopCounterIncrementTimeMax;
\r
237 static TickType_t xIterationsWithoutCounterIncrement = ( TickType_t ) 0, xLastCycleFrequency;
\r
239 if( xLastCycleFrequency != xCycleFrequency )
\r
241 /* The cycle frequency has probably become much faster due to an error
\r
242 elsewhere. Start counting Iterations again. */
\r
243 xIterationsWithoutCounterIncrement = ( TickType_t ) 0;
\r
244 xLastCycleFrequency = xCycleFrequency;
\r
247 /* Calculate the maximum number of times that it is permissible for this
\r
248 function to be called without ulLoopCounter being incremented. This is
\r
249 necessary because the tests in this file block for extended periods, and the
\r
250 block period might be longer than the time between calls to this function. */
\r
251 xMaxBlockTimeUsedByTheseTests = ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod;
\r
252 xLoopCounterIncrementTimeMax = ( xMaxBlockTimeUsedByTheseTests / xCycleFrequency ) + 1;
\r
254 /* If the demo task is still running then the loop counter is expected to
\r
255 have incremented every xLoopCounterIncrementTimeMax calls. */
\r
256 if( ulLastLoopCounter == ulLoopCounter )
\r
258 xIterationsWithoutCounterIncrement++;
\r
259 if( xIterationsWithoutCounterIncrement > xLoopCounterIncrementTimeMax )
\r
261 /* The tests appear to be no longer running (stalled). */
\r
262 xTestStatus = pdFAIL;
\r
263 configASSERT( xTestStatus );
\r
268 /* ulLoopCounter changed, so the count of times this function was called
\r
269 without a change can be reset to zero. */
\r
270 xIterationsWithoutCounterIncrement = ( TickType_t ) 0;
\r
273 ulLastLoopCounter = ulLoopCounter;
\r
275 /* Errors detected in the task itself will have latched xTestStatus
\r
278 return xTestStatus;
\r
280 /*-----------------------------------------------------------*/
\r
282 static void prvTest1_CreateTimersWithoutSchedulerRunning( void )
\r
286 for( xTimer = 0; xTimer < configTIMER_QUEUE_LENGTH; xTimer++ )
\r
288 /* As the timer queue is not yet full, it should be possible to both create
\r
289 and start a timer. These timers are being started before the scheduler has
\r
290 been started, so their block times should get set to zero within the timer
\r
292 xAutoReloadTimers[ xTimer ] = xTimerCreate( "FR Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
\r
293 ( ( xTimer + ( TickType_t ) 1 ) * xBasePeriod ),/* The period for the timer. The plus 1 ensures a period of zero is not specified. */
\r
294 pdTRUE, /* Auto-reload is set to true. */
\r
295 ( void * ) xTimer, /* An identifier for the timer as all the auto reload timers use the same callback. */
\r
296 prvAutoReloadTimerCallback ); /* The callback to be called when the timer expires. */
\r
298 configASSERT( strcmp( pcTimerGetTimerName( xAutoReloadTimers[ xTimer ] ), "FR Timer" ) == 0 );
\r
300 if( xAutoReloadTimers[ xTimer ] == NULL )
\r
302 xTestStatus = pdFAIL;
\r
303 configASSERT( xTestStatus );
\r
307 /* The scheduler has not yet started, so the block period of
\r
308 portMAX_DELAY should just get set to zero in xTimerStart(). Also,
\r
309 the timer queue is not yet full so xTimerStart() should return
\r
311 if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) != pdPASS )
\r
313 xTestStatus = pdFAIL;
\r
314 configASSERT( xTestStatus );
\r
319 /* The timers queue should now be full, so it should be possible to create
\r
320 another timer, but not possible to start it (the timer queue will not get
\r
321 drained until the scheduler has been started. */
\r
322 xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] = xTimerCreate( "FR Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
\r
323 ( configTIMER_QUEUE_LENGTH * xBasePeriod ), /* The period for the timer. */
\r
324 pdTRUE, /* Auto-reload is set to true. */
\r
325 ( void * ) xTimer, /* An identifier for the timer as all the auto reload timers use the same callback. */
\r
326 prvAutoReloadTimerCallback ); /* The callback executed when the timer expires. */
\r
328 if( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] == NULL )
\r
330 xTestStatus = pdFAIL;
\r
331 configASSERT( xTestStatus );
\r
335 if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) == pdPASS )
\r
337 /* This time it would not be expected that the timer could be
\r
338 started at this point. */
\r
339 xTestStatus = pdFAIL;
\r
340 configASSERT( xTestStatus );
\r
344 /* Create the timers that are used from the tick interrupt to test the timer
\r
345 API functions that can be called from an ISR. */
\r
346 xISRAutoReloadTimer = xTimerCreate( "ISR AR", /* The text name given to the timer. */
\r
347 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
348 pdTRUE, /* This is an auto reload timer. */
\r
349 ( void * ) NULL, /* The identifier is not required. */
\r
350 prvISRAutoReloadTimerCallback ); /* The callback that is executed when the timer expires. */
\r
352 xISROneShotTimer = xTimerCreate( "ISR OS", /* The text name given to the timer. */
\r
353 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
354 pdFALSE, /* This is a one shot timer. */
\r
355 ( void * ) NULL, /* The identifier is not required. */
\r
356 prvISROneShotTimerCallback ); /* The callback that is executed when the timer expires. */
\r
358 if( ( xISRAutoReloadTimer == NULL ) || ( xISROneShotTimer == NULL ) )
\r
360 xTestStatus = pdFAIL;
\r
361 configASSERT( xTestStatus );
\r
364 /*-----------------------------------------------------------*/
\r
366 static void prvTest2_CheckTaskAndTimersInitialState( void )
\r
370 /* Ensure all the timers are in their expected initial state. This depends
\r
371 on the timer service task having a higher priority than this task.
\r
373 auto reload timers 0 to ( configTIMER_QUEUE_LENGTH - 1 ) should now be active,
\r
374 and auto reload timer configTIMER_QUEUE_LENGTH should not yet be active (it
\r
375 could not be started prior to the scheduler being started when it was
\r
377 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
379 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
381 xTestStatus = pdFAIL;
\r
382 configASSERT( xTestStatus );
\r
386 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] ) != pdFALSE )
\r
388 xTestStatus = pdFAIL;
\r
389 configASSERT( xTestStatus );
\r
392 /*-----------------------------------------------------------*/
\r
394 static void prvTest3_CheckAutoReloadExpireRates( void )
\r
396 uint8_t ucMaxAllowableValue, ucMinAllowableValue, ucTimer;
\r
397 TickType_t xBlockPeriod, xTimerPeriod, xExpectedNumber;
\r
399 /* Check the auto reload timers expire at the expected rates. */
\r
402 /* Delaying for configTIMER_QUEUE_LENGTH * xBasePeriod ticks should allow
\r
403 all the auto reload timers to expire at least once. */
\r
404 xBlockPeriod = ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod;
\r
405 vTaskDelay( xBlockPeriod );
\r
407 /* Check that all the auto reload timers have called their callback
\r
408 function the expected number of times. */
\r
409 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
411 /* The expected number of expiries is equal to the block period divided
\r
412 by the timer period. */
\r
413 xTimerPeriod = ( ( ( TickType_t ) ucTimer + ( TickType_t ) 1 ) * xBasePeriod );
\r
414 xExpectedNumber = xBlockPeriod / xTimerPeriod;
\r
416 ucMaxAllowableValue = ( ( uint8_t ) xExpectedNumber ) ;
\r
417 ucMinAllowableValue = ( uint8_t ) ( ( uint8_t ) xExpectedNumber - ( uint8_t ) 1 ); /* Weird casting to try and please all compilers. */
\r
419 if( ( ucAutoReloadTimerCounters[ ucTimer ] < ucMinAllowableValue ) ||
\r
420 ( ucAutoReloadTimerCounters[ ucTimer ] > ucMaxAllowableValue )
\r
423 xTestStatus = pdFAIL;
\r
424 configASSERT( xTestStatus );
\r
428 if( xTestStatus == pdPASS )
\r
430 /* No errors have been reported so increment the loop counter so the
\r
431 check task knows this task is still running. */
\r
435 /*-----------------------------------------------------------*/
\r
437 static void prvTest4_CheckAutoReloadTimersCanBeStopped( void )
\r
441 /* Check the auto reload timers can be stopped correctly, and correctly
\r
442 report their state. */
\r
444 /* Stop all the active timers. */
\r
445 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
447 /* The timer has not been stopped yet! */
\r
448 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
450 xTestStatus = pdFAIL;
\r
451 configASSERT( xTestStatus );
\r
454 /* Now stop the timer. This will appear to happen immediately to
\r
455 this task because this task is running at a priority below the
\r
456 timer service task. */
\r
457 xTimerStop( xAutoReloadTimers[ ucTimer ], tmrdemoDONT_BLOCK );
\r
459 /* The timer should now be inactive. */
\r
460 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )
\r
462 xTestStatus = pdFAIL;
\r
463 configASSERT( xTestStatus );
\r
467 taskENTER_CRITICAL();
\r
469 /* The timer in array position configTIMER_QUEUE_LENGTH should not
\r
470 be active. The critical section is used to ensure the timer does
\r
471 not call its callback between the next line running and the array
\r
472 being cleared back to zero, as that would mask an error condition. */
\r
473 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH ] != ( uint8_t ) 0 )
\r
475 xTestStatus = pdFAIL;
\r
476 configASSERT( xTestStatus );
\r
479 /* Clear the timer callback count. */
\r
480 memset( ( void * ) ucAutoReloadTimerCounters, 0, sizeof( ucAutoReloadTimerCounters ) );
\r
482 taskEXIT_CRITICAL();
\r
484 /* The timers are now all inactive, so this time, after delaying, none
\r
485 of the callback counters should have incremented. */
\r
486 vTaskDelay( ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod );
\r
487 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
489 if( ucAutoReloadTimerCounters[ ucTimer ] != ( uint8_t ) 0 )
\r
491 xTestStatus = pdFAIL;
\r
492 configASSERT( xTestStatus );
\r
496 if( xTestStatus == pdPASS )
\r
498 /* No errors have been reported so increment the loop counter so
\r
499 the check task knows this task is still running. */
\r
503 /*-----------------------------------------------------------*/
\r
505 static void prvTest5_CheckBasicOneShotTimerBehaviour( void )
\r
507 /* Check the one shot timer only calls its callback once after it has been
\r
508 started, and that it reports its state correctly. */
\r
510 /* The one shot timer should not be active yet. */
\r
511 if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )
\r
513 xTestStatus = pdFAIL;
\r
514 configASSERT( xTestStatus );
\r
517 if( ucOneShotTimerCounter != ( uint8_t ) 0 )
\r
519 xTestStatus = pdFAIL;
\r
520 configASSERT( xTestStatus );
\r
523 /* Start the one shot timer and check that it reports its state correctly. */
\r
524 xTimerStart( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
525 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
527 xTestStatus = pdFAIL;
\r
528 configASSERT( xTestStatus );
\r
531 /* Delay for three times as long as the one shot timer period, then check
\r
532 to ensure it has only called its callback once, and is now not in the
\r
534 vTaskDelay( tmrdemoONE_SHOT_TIMER_PERIOD * ( TickType_t ) 3 );
\r
536 if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )
\r
538 xTestStatus = pdFAIL;
\r
539 configASSERT( xTestStatus );
\r
542 if( ucOneShotTimerCounter != ( uint8_t ) 1 )
\r
544 xTestStatus = pdFAIL;
\r
545 configASSERT( xTestStatus );
\r
549 /* Reset the one shot timer callback count. */
\r
550 ucOneShotTimerCounter = ( uint8_t ) 0;
\r
553 if( xTestStatus == pdPASS )
\r
555 /* No errors have been reported so increment the loop counter so the
\r
556 check task knows this task is still running. */
\r
560 /*-----------------------------------------------------------*/
\r
562 static void prvTest6_CheckAutoReloadResetBehaviour( void )
\r
566 /* Check timer reset behaviour. */
\r
568 /* Restart the one shot timer and check it reports its status correctly. */
\r
569 xTimerStart( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
570 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
572 xTestStatus = pdFAIL;
\r
573 configASSERT( xTestStatus );
\r
576 /* Restart one of the auto reload timers and check that it reports its
\r
577 status correctly. */
\r
578 xTimerStart( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
579 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
581 xTestStatus = pdFAIL;
\r
582 configASSERT( xTestStatus );
\r
585 for( ucTimer = 0; ucTimer < trmdemoNUM_TIMER_RESETS; ucTimer++ )
\r
587 /* Delay for half as long as the one shot timer period, then reset it.
\r
588 It should never expire while this is done, so its callback count should
\r
589 never increment. */
\r
590 vTaskDelay( tmrdemoONE_SHOT_TIMER_PERIOD / 2 );
\r
592 /* Check both running timers are still active, but have not called their
\r
593 callback functions. */
\r
594 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
596 xTestStatus = pdFAIL;
\r
597 configASSERT( xTestStatus );
\r
600 if( ucOneShotTimerCounter != ( uint8_t ) 0 )
\r
602 xTestStatus = pdFAIL;
\r
603 configASSERT( xTestStatus );
\r
606 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
608 xTestStatus = pdFAIL;
\r
609 configASSERT( xTestStatus );
\r
612 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] != ( uint8_t ) 0 )
\r
614 xTestStatus = pdFAIL;
\r
615 configASSERT( xTestStatus );
\r
618 /* Reset both running timers. */
\r
619 xTimerReset( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
620 xTimerReset( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
622 if( xTestStatus == pdPASS )
\r
624 /* No errors have been reported so increment the loop counter so
\r
625 the check task knows this task is still running. */
\r
630 /* Finally delay long enough for both running timers to expire. */
\r
631 vTaskDelay( ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod );
\r
633 /* The timers were not reset during the above delay period so should now
\r
634 both have called their callback functions. */
\r
635 if( ucOneShotTimerCounter != ( uint8_t ) 1 )
\r
637 xTestStatus = pdFAIL;
\r
638 configASSERT( xTestStatus );
\r
641 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] == 0 )
\r
643 xTestStatus = pdFAIL;
\r
644 configASSERT( xTestStatus );
\r
647 /* The one shot timer should no longer be active, while the auto reload
\r
648 timer should still be active. */
\r
649 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
651 xTestStatus = pdFAIL;
\r
652 configASSERT( xTestStatus );
\r
655 if( xTimerIsTimerActive( xOneShotTimer ) == pdTRUE )
\r
657 xTestStatus = pdFAIL;
\r
658 configASSERT( xTestStatus );
\r
661 /* Stop the auto reload timer again. */
\r
662 xTimerStop( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
664 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) != pdFALSE )
\r
666 xTestStatus = pdFAIL;
\r
667 configASSERT( xTestStatus );
\r
670 /* Clear the timer callback counts, ready for another iteration of these
\r
672 ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] = ( uint8_t ) 0;
\r
673 ucOneShotTimerCounter = ( uint8_t ) 0;
\r
675 if( xTestStatus == pdPASS )
\r
677 /* No errors have been reported so increment the loop counter so the check
\r
678 task knows this task is still running. */
\r
682 /*-----------------------------------------------------------*/
\r
684 static void prvResetStartConditionsForNextIteration( void )
\r
688 /* Start the timers again to start all the tests over again. */
\r
690 /* Start the timers again. */
\r
691 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
693 /* The timer has not been started yet! */
\r
694 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )
\r
696 xTestStatus = pdFAIL;
\r
697 configASSERT( xTestStatus );
\r
700 /* Now start the timer. This will appear to happen immediately to
\r
701 this task because this task is running at a priority below the timer
\r
703 xTimerStart( xAutoReloadTimers[ ucTimer ], tmrdemoDONT_BLOCK );
\r
705 /* The timer should now be active. */
\r
706 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
708 xTestStatus = pdFAIL;
\r
709 configASSERT( xTestStatus );
\r
713 if( xTestStatus == pdPASS )
\r
715 /* No errors have been reported so increment the loop counter so the
\r
716 check task knows this task is still running. */
\r
720 /*-----------------------------------------------------------*/
\r
722 void vTimerPeriodicISRTests( void )
\r
724 static TickType_t uxTick = ( TickType_t ) -1;
\r
726 #if( configTIMER_TASK_PRIORITY != ( configMAX_PRIORITIES - 1 ) )
\r
727 /* The timer service task is not the highest priority task, so it cannot
\r
728 be assumed that timings will be exact. Timers should never call their
\r
729 callback before their expiry time, but a margin is permissible for calling
\r
730 their callback after their expiry time. If exact timing is required then
\r
731 configTIMER_TASK_PRIORITY must be set to ensure the timer service task
\r
732 is the highest priority task in the system.
\r
734 This function is called from the tick hook. The tick hook is called
\r
735 even when the scheduler is suspended. Therefore it is possible that the
\r
736 uxTick count maintained in this function is temporarily ahead of the tick
\r
737 count maintained by the kernel. When this is the case a message posted from
\r
738 this function will assume a time stamp in advance of the real time stamp,
\r
739 which can result in a timer being processed before this function expects it
\r
740 to. For example, if the kernel's tick count was 100, and uxTick was 102,
\r
741 then this function will not expect the timer to have expired until the
\r
742 kernel's tick count is (102 + xBasePeriod), whereas in reality the timer
\r
743 will expire when the kernel's tick count is (100 + xBasePeriod). For this
\r
744 reason xMargin is used as an allowable margin for premature timer expiries
\r
745 as well as late timer expiries. */
\r
747 /* Windows is not real real time. */
\r
748 const TickType_t xMargin = 20;
\r
750 const TickType_t xMargin = 6;
\r
751 #endif /* _WINDOWS_ */
\r
754 /* Windows is not real real time. */
\r
755 const TickType_t xMargin = 20;
\r
757 const TickType_t xMargin = 4;
\r
758 #endif /* _WINDOWS_ */
\r
766 /* The timers will have been created, but not started. Start them now
\r
767 by setting their period. */
\r
768 ucISRAutoReloadTimerCounter = 0;
\r
769 ucISROneShotTimerCounter = 0;
\r
771 /* It is possible that the timer task has not yet made room in the
\r
772 timer queue. If the timers cannot be started then reset uxTick so
\r
773 another attempt is made later. */
\r
774 uxTick = ( TickType_t ) -1;
\r
776 /* Try starting first timer. */
\r
777 if( xTimerChangePeriodFromISR( xISRAutoReloadTimer, xBasePeriod, NULL ) == pdPASS )
\r
779 /* First timer was started, try starting the second timer. */
\r
780 if( xTimerChangePeriodFromISR( xISROneShotTimer, xBasePeriod, NULL ) == pdPASS )
\r
782 /* Both timers were started, so set the uxTick back to its
\r
788 /* Second timer could not be started, so stop the first one
\r
790 xTimerStopFromISR( xISRAutoReloadTimer, NULL );
\r
794 else if( uxTick == ( xBasePeriod - xMargin ) )
\r
796 /* Neither timer should have expired yet. */
\r
797 if( ( ucISRAutoReloadTimerCounter != 0 ) || ( ucISROneShotTimerCounter != 0 ) )
\r
799 xTestStatus = pdFAIL;
\r
800 configASSERT( xTestStatus );
\r
803 else if( uxTick == ( xBasePeriod + xMargin ) )
\r
805 /* Both timers should now have expired once. The auto reload timer will
\r
806 still be active, but the one shot timer should now have stopped. */
\r
807 if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )
\r
809 xTestStatus = pdFAIL;
\r
810 configASSERT( xTestStatus );
\r
813 else if( uxTick == ( ( 2 * xBasePeriod ) - xMargin ) )
\r
815 /* The auto reload timer will still be active, but the one shot timer
\r
816 should now have stopped - however, at this time neither of the timers
\r
817 should have expired again since the last test. */
\r
818 if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )
\r
820 xTestStatus = pdFAIL;
\r
821 configASSERT( xTestStatus );
\r
824 else if( uxTick == ( ( 2 * xBasePeriod ) + xMargin ) )
\r
826 /* The auto reload timer will still be active, but the one shot timer
\r
827 should now have stopped. At this time the auto reload timer should have
\r
828 expired again, but the one shot timer count should not have changed. */
\r
829 if( ucISRAutoReloadTimerCounter != 2 )
\r
831 xTestStatus = pdFAIL;
\r
832 configASSERT( xTestStatus );
\r
835 if( ucISROneShotTimerCounter != 1 )
\r
837 xTestStatus = pdFAIL;
\r
838 configASSERT( xTestStatus );
\r
841 else if( uxTick == ( ( 2 * xBasePeriod ) + ( xBasePeriod >> ( TickType_t ) 2U ) ) )
\r
843 /* The auto reload timer will still be active, but the one shot timer
\r
844 should now have stopped. Again though, at this time, neither timer call
\r
845 back should have been called since the last test. */
\r
846 if( ucISRAutoReloadTimerCounter != 2 )
\r
848 xTestStatus = pdFAIL;
\r
849 configASSERT( xTestStatus );
\r
852 if( ucISROneShotTimerCounter != 1 )
\r
854 xTestStatus = pdFAIL;
\r
855 configASSERT( xTestStatus );
\r
858 else if( uxTick == ( 3 * xBasePeriod ) )
\r
860 /* Start the one shot timer again. */
\r
861 xTimerStartFromISR( xISROneShotTimer, NULL );
\r
863 else if( uxTick == ( ( 3 * xBasePeriod ) + xMargin ) )
\r
865 /* The auto reload timer and one shot timer will be active. At
\r
866 this time the auto reload timer should have expired again, but the one
\r
867 shot timer count should not have changed yet. */
\r
868 if( ucISRAutoReloadTimerCounter != 3 )
\r
870 xTestStatus = pdFAIL;
\r
871 configASSERT( xTestStatus );
\r
874 if( ucISROneShotTimerCounter != 1 )
\r
876 xTestStatus = pdFAIL;
\r
877 configASSERT( xTestStatus );
\r
880 /* Now stop the auto reload timer. The one shot timer was started
\r
881 a few ticks ago. */
\r
882 xTimerStopFromISR( xISRAutoReloadTimer, NULL );
\r
884 else if( uxTick == ( 4 * ( xBasePeriod - xMargin ) ) )
\r
886 /* The auto reload timer is now stopped, and the one shot timer is
\r
887 active, but at this time neither timer should have expired since the
\r
889 if( ucISRAutoReloadTimerCounter != 3 )
\r
891 xTestStatus = pdFAIL;
\r
892 configASSERT( xTestStatus );
\r
895 if( ucISROneShotTimerCounter != 1 )
\r
897 xTestStatus = pdFAIL;
\r
898 configASSERT( xTestStatus );
\r
901 else if( uxTick == ( ( 4 * xBasePeriod ) + xMargin ) )
\r
903 /* The auto reload timer is now stopped, and the one shot timer is
\r
904 active. The one shot timer should have expired again, but the auto
\r
905 reload timer should not have executed its callback. */
\r
906 if( ucISRAutoReloadTimerCounter != 3 )
\r
908 xTestStatus = pdFAIL;
\r
909 configASSERT( xTestStatus );
\r
912 if( ucISROneShotTimerCounter != 2 )
\r
914 xTestStatus = pdFAIL;
\r
915 configASSERT( xTestStatus );
\r
918 else if( uxTick == ( 8 * xBasePeriod ) )
\r
920 /* The auto reload timer is now stopped, and the one shot timer has
\r
921 already expired and then stopped itself. Both callback counters should
\r
922 not have incremented since the last test. */
\r
923 if( ucISRAutoReloadTimerCounter != 3 )
\r
925 xTestStatus = pdFAIL;
\r
926 configASSERT( xTestStatus );
\r
929 if( ucISROneShotTimerCounter != 2 )
\r
931 xTestStatus = pdFAIL;
\r
932 configASSERT( xTestStatus );
\r
935 /* Now reset the one shot timer. */
\r
936 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
938 else if( uxTick == ( ( 9 * xBasePeriod ) - xMargin ) )
\r
940 /* Only the one shot timer should be running, but it should not have
\r
941 expired since the last test. Check the callback counters have not
\r
942 incremented, then reset the one shot timer again. */
\r
943 if( ucISRAutoReloadTimerCounter != 3 )
\r
945 xTestStatus = pdFAIL;
\r
946 configASSERT( xTestStatus );
\r
949 if( ucISROneShotTimerCounter != 2 )
\r
951 xTestStatus = pdFAIL;
\r
952 configASSERT( xTestStatus );
\r
955 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
957 else if( uxTick == ( ( 10 * xBasePeriod ) - ( 2 * xMargin ) ) )
\r
959 /* Only the one shot timer should be running, but it should not have
\r
960 expired since the last test. Check the callback counters have not
\r
961 incremented, then reset the one shot timer again. */
\r
962 if( ucISRAutoReloadTimerCounter != 3 )
\r
964 xTestStatus = pdFAIL;
\r
965 configASSERT( xTestStatus );
\r
968 if( ucISROneShotTimerCounter != 2 )
\r
970 xTestStatus = pdFAIL;
\r
971 configASSERT( xTestStatus );
\r
974 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
976 else if( uxTick == ( ( 11 * xBasePeriod ) - ( 3 * xMargin ) ) )
\r
978 /* Only the one shot timer should be running, but it should not have
\r
979 expired since the last test. Check the callback counters have not
\r
980 incremented, then reset the one shot timer once again. */
\r
981 if( ucISRAutoReloadTimerCounter != 3 )
\r
983 xTestStatus = pdFAIL;
\r
984 configASSERT( xTestStatus );
\r
987 if( ucISROneShotTimerCounter != 2 )
\r
989 xTestStatus = pdFAIL;
\r
990 configASSERT( xTestStatus );
\r
993 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
995 else if( uxTick == ( ( 12 * xBasePeriod ) - ( 2 * xMargin ) ) )
\r
997 /* Only the one shot timer should have been running and this time it
\r
998 should have expired. Check its callback count has been incremented.
\r
999 The auto reload timer is still not running so should still have the same
\r
1000 count value. This time the one shot timer is not reset so should not
\r
1001 restart from its expiry period again. */
\r
1002 if( ucISRAutoReloadTimerCounter != 3 )
\r
1004 xTestStatus = pdFAIL;
\r
1005 configASSERT( xTestStatus );
\r
1008 if( ucISROneShotTimerCounter != 3 )
\r
1010 xTestStatus = pdFAIL;
\r
1011 configASSERT( xTestStatus );
\r
1014 else if( uxTick == ( 15 * xBasePeriod ) )
\r
1016 /* Neither timer should be running now. Check neither callback count
\r
1017 has incremented, then go back to the start to run these tests all
\r
1019 if( ucISRAutoReloadTimerCounter != 3 )
\r
1021 xTestStatus = pdFAIL;
\r
1022 configASSERT( xTestStatus );
\r
1025 if( ucISROneShotTimerCounter != 3 )
\r
1027 xTestStatus = pdFAIL;
\r
1028 configASSERT( xTestStatus );
\r
1031 uxTick = ( TickType_t ) -1;
\r
1034 /*-----------------------------------------------------------*/
\r
1036 /*** Timer callback functions are defined below here. ***/
\r
1038 static void prvAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer )
\r
1040 uint32_t ulTimerID;
\r
1042 ulTimerID = ( uint32_t ) pvTimerGetTimerID( pxExpiredTimer );
\r
1043 if( ulTimerID <= ( configTIMER_QUEUE_LENGTH + 1 ) )
\r
1045 ( ucAutoReloadTimerCounters[ ulTimerID ] )++;
\r
1049 /* The timer ID appears to be unexpected (invalid). */
\r
1050 xTestStatus = pdFAIL;
\r
1051 configASSERT( xTestStatus );
\r
1054 /*-----------------------------------------------------------*/
\r
1056 static void prvOneShotTimerCallback( TimerHandle_t pxExpiredTimer )
\r
1058 /* The parameter is not used in this case as only one timer uses this
\r
1059 callback function. */
\r
1060 ( void ) pxExpiredTimer;
\r
1062 ucOneShotTimerCounter++;
\r
1064 /*-----------------------------------------------------------*/
\r
1066 static void prvISRAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer )
\r
1068 /* The parameter is not used in this case as only one timer uses this
\r
1069 callback function. */
\r
1070 ( void ) pxExpiredTimer;
\r
1072 ucISRAutoReloadTimerCounter++;
\r
1074 /*-----------------------------------------------------------*/
\r
1076 static void prvISROneShotTimerCallback( TimerHandle_t pxExpiredTimer )
\r
1078 /* The parameter is not used in this case as only one timer uses this
\r
1079 callback function. */
\r
1080 ( void ) pxExpiredTimer;
\r
1082 ucISROneShotTimerCounter++;
\r
1084 /*-----------------------------------------------------------*/
\r