2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
98 * Tests the behaviour of timers. Some timers are created before the scheduler
\r
99 * is started, and some after.
\r
102 /* Standard includes. */
\r
103 #include <string.h>
\r
105 /* Scheduler include files. */
\r
106 #include "FreeRTOS.h"
\r
108 #include "timers.h"
\r
110 /* Demo program include files. */
\r
111 #include "TimerDemo.h"
\r
113 #if ( configTIMER_TASK_PRIORITY < 1 )
\r
114 #error configTIMER_TASK_PRIORITY must be set to at least 1 for this test/demo to function correctly.
\r
117 #define tmrdemoDONT_BLOCK ( ( TickType_t ) 0 )
\r
118 #define tmrdemoONE_SHOT_TIMER_PERIOD ( xBasePeriod * ( TickType_t ) 3 )
\r
119 #define trmdemoNUM_TIMER_RESETS ( ( uint8_t ) 10 )
\r
121 /*-----------------------------------------------------------*/
\r
123 /* The callback functions used by the timers. These each increment a counter
\r
124 to indicate which timer has expired. The auto-reload timers that are used by
\r
125 the test task (as opposed to being used from an ISR) all share the same
\r
126 prvAutoReloadTimerCallback() callback function, and use the ID of the
\r
127 pxExpiredTimer parameter passed into that function to know which counter to
\r
128 increment. The other timers all have their own unique callback function and
\r
129 simply increment their counters without using the callback function parameter. */
\r
130 static void prvAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer );
\r
131 static void prvOneShotTimerCallback( TimerHandle_t pxExpiredTimer );
\r
132 static void prvTimerTestTask( void *pvParameters );
\r
133 static void prvISRAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer );
\r
134 static void prvISROneShotTimerCallback( TimerHandle_t pxExpiredTimer );
\r
136 /* The test functions used by the timer test task. These manipulate the auto
\r
137 reload and one shot timers in various ways, then delay, then inspect the timers
\r
138 to ensure they have behaved as expected. */
\r
139 static void prvTest1_CreateTimersWithoutSchedulerRunning( void );
\r
140 static void prvTest2_CheckTaskAndTimersInitialState( void );
\r
141 static void prvTest3_CheckAutoReloadExpireRates( void );
\r
142 static void prvTest4_CheckAutoReloadTimersCanBeStopped( void );
\r
143 static void prvTest5_CheckBasicOneShotTimerBehaviour( void );
\r
144 static void prvTest6_CheckAutoReloadResetBehaviour( void );
\r
145 static void prvResetStartConditionsForNextIteration( void );
\r
147 /*-----------------------------------------------------------*/
\r
149 /* Flag that will be latched to pdFAIL should any unexpected behaviour be
\r
150 detected in any of the demo tests. */
\r
151 static volatile BaseType_t xTestStatus = pdPASS;
\r
153 /* Counter that is incremented on each cycle of a test. This is used to
\r
154 detect a stalled task - a test that is no longer running. */
\r
155 static volatile uint32_t ulLoopCounter = 0;
\r
157 /* A set of auto reload timers - each of which use the same callback function.
\r
158 The callback function uses the timer ID to index into, and then increment, a
\r
159 counter in the ucAutoReloadTimerCounters[] array. The auto reload timers
\r
160 referenced from xAutoReloadTimers[] are used by the prvTimerTestTask task. */
\r
161 static TimerHandle_t xAutoReloadTimers[ configTIMER_QUEUE_LENGTH + 1 ] = { 0 };
\r
162 static uint8_t ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH + 1 ] = { 0 };
\r
164 /* The one shot timer is configured to use a callback function that increments
\r
165 ucOneShotTimerCounter each time it gets called. */
\r
166 static TimerHandle_t xOneShotTimer = NULL;
\r
167 static uint8_t ucOneShotTimerCounter = ( uint8_t ) 0;
\r
169 /* The ISR reload timer is controlled from the tick hook to exercise the timer
\r
170 API functions that can be used from an ISR. It is configured to increment
\r
171 ucISRReloadTimerCounter each time its callback function is executed. */
\r
172 static TimerHandle_t xISRAutoReloadTimer = NULL;
\r
173 static uint8_t ucISRAutoReloadTimerCounter = ( uint8_t ) 0;
\r
175 /* The ISR one shot timer is controlled from the tick hook to exercise the timer
\r
176 API functions that can be used from an ISR. It is configured to increment
\r
177 ucISRReloadTimerCounter each time its callback function is executed. */
\r
178 static TimerHandle_t xISROneShotTimer = NULL;
\r
179 static uint8_t ucISROneShotTimerCounter = ( uint8_t ) 0;
\r
181 /* The period of all the timers are a multiple of the base period. The base
\r
182 period is configured by the parameter to vStartTimerDemoTask(). */
\r
183 static TickType_t xBasePeriod = 0;
\r
185 /*-----------------------------------------------------------*/
\r
187 void vStartTimerDemoTask( TickType_t xBasePeriodIn )
\r
189 /* Start with the timer and counter arrays clear - this is only necessary
\r
190 where the compiler does not clear them automatically on start up. */
\r
191 memset( ucAutoReloadTimerCounters, 0x00, sizeof( ucAutoReloadTimerCounters ) );
\r
192 memset( xAutoReloadTimers, 0x00, sizeof( xAutoReloadTimers ) );
\r
194 /* Store the period from which all the timer periods will be generated from
\r
196 xBasePeriod = xBasePeriodIn;
\r
198 /* Create a set of timers for use by this demo/test. */
\r
199 prvTest1_CreateTimersWithoutSchedulerRunning();
\r
201 /* Create the task that will control and monitor the timers. This is
\r
202 created at a lower priority than the timer service task to ensure, as
\r
203 far as it is concerned, commands on timers are actioned immediately
\r
204 (sending a command to the timer service task will unblock the timer service
\r
205 task, which will then preempt this task). */
\r
206 if( xTestStatus != pdFAIL )
\r
208 xTaskCreate( prvTimerTestTask, "Tmr Tst", configMINIMAL_STACK_SIZE, NULL, configTIMER_TASK_PRIORITY - 1, NULL );
\r
211 /*-----------------------------------------------------------*/
\r
213 static void prvTimerTestTask( void *pvParameters )
\r
215 ( void ) pvParameters;
\r
217 /* Create a one-shot timer for use later on in this test. */
\r
218 xOneShotTimer = xTimerCreate( "Oneshot Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
\r
219 tmrdemoONE_SHOT_TIMER_PERIOD, /* The period for the timer. */
\r
220 pdFALSE, /* Don't auto-reload - hence a one shot timer. */
\r
221 ( void * ) 0, /* The timer identifier. In this case this is not used as the timer has its own callback. */
\r
222 prvOneShotTimerCallback ); /* The callback to be called when the timer expires. */
\r
224 if( xOneShotTimer == NULL )
\r
226 xTestStatus = pdFAIL;
\r
227 configASSERT( xTestStatus );
\r
231 /* Ensure all the timers are in their expected initial state. This
\r
232 depends on the timer service task having a higher priority than this task. */
\r
233 prvTest2_CheckTaskAndTimersInitialState();
\r
237 /* Check the auto reload timers expire at the expected/correct rates. */
\r
238 prvTest3_CheckAutoReloadExpireRates();
\r
240 /* Check the auto reload timers can be stopped correctly, and correctly
\r
241 report their state. */
\r
242 prvTest4_CheckAutoReloadTimersCanBeStopped();
\r
244 /* Check the one shot timer only calls its callback once after it has been
\r
245 started, and that it reports its state correctly. */
\r
246 prvTest5_CheckBasicOneShotTimerBehaviour();
\r
248 /* Check timer reset behaviour. */
\r
249 prvTest6_CheckAutoReloadResetBehaviour();
\r
251 /* Start the timers again to restart all the tests over again. */
\r
252 prvResetStartConditionsForNextIteration();
\r
255 /*-----------------------------------------------------------*/
\r
257 /* This is called to check that the created task is still running and has not
\r
258 detected any errors. */
\r
259 BaseType_t xAreTimerDemoTasksStillRunning( TickType_t xCycleFrequency )
\r
261 static uint32_t ulLastLoopCounter = 0UL;
\r
262 TickType_t xMaxBlockTimeUsedByTheseTests, xLoopCounterIncrementTimeMax;
\r
263 static TickType_t xIterationsWithoutCounterIncrement = ( TickType_t ) 0, xLastCycleFrequency;
\r
265 if( xLastCycleFrequency != xCycleFrequency )
\r
267 /* The cycle frequency has probably become much faster due to an error
\r
268 elsewhere. Start counting Iterations again. */
\r
269 xIterationsWithoutCounterIncrement = ( TickType_t ) 0;
\r
270 xLastCycleFrequency = xCycleFrequency;
\r
273 /* Calculate the maximum number of times that it is permissible for this
\r
274 function to be called without ulLoopCounter being incremented. This is
\r
275 necessary because the tests in this file block for extended periods, and the
\r
276 block period might be longer than the time between calls to this function. */
\r
277 xMaxBlockTimeUsedByTheseTests = ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod;
\r
278 xLoopCounterIncrementTimeMax = ( xMaxBlockTimeUsedByTheseTests / xCycleFrequency ) + 1;
\r
280 /* If the demo task is still running then the loop counter is expected to
\r
281 have incremented every xLoopCounterIncrementTimeMax calls. */
\r
282 if( ulLastLoopCounter == ulLoopCounter )
\r
284 xIterationsWithoutCounterIncrement++;
\r
285 if( xIterationsWithoutCounterIncrement > xLoopCounterIncrementTimeMax )
\r
287 /* The tests appear to be no longer running (stalled). */
\r
288 xTestStatus = pdFAIL;
\r
289 configASSERT( xTestStatus );
\r
294 /* ulLoopCounter changed, so the count of times this function was called
\r
295 without a change can be reset to zero. */
\r
296 xIterationsWithoutCounterIncrement = ( TickType_t ) 0;
\r
299 ulLastLoopCounter = ulLoopCounter;
\r
301 /* Errors detected in the task itself will have latched xTestStatus
\r
304 return xTestStatus;
\r
306 /*-----------------------------------------------------------*/
\r
308 static void prvTest1_CreateTimersWithoutSchedulerRunning( void )
\r
312 for( xTimer = 0; xTimer < configTIMER_QUEUE_LENGTH; xTimer++ )
\r
314 /* As the timer queue is not yet full, it should be possible to both create
\r
315 and start a timer. These timers are being started before the scheduler has
\r
316 been started, so their block times should get set to zero within the timer
\r
318 xAutoReloadTimers[ xTimer ] = xTimerCreate( "FR Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
\r
319 ( ( xTimer + ( TickType_t ) 1 ) * xBasePeriod ),/* The period for the timer. The plus 1 ensures a period of zero is not specified. */
\r
320 pdTRUE, /* Auto-reload is set to true. */
\r
321 ( void * ) xTimer, /* An identifier for the timer as all the auto reload timers use the same callback. */
\r
322 prvAutoReloadTimerCallback ); /* The callback to be called when the timer expires. */
\r
324 configASSERT( strcmp( pcTimerGetTimerName( xAutoReloadTimers[ xTimer ] ), "FR Timer" ) == 0 );
\r
326 if( xAutoReloadTimers[ xTimer ] == NULL )
\r
328 xTestStatus = pdFAIL;
\r
329 configASSERT( xTestStatus );
\r
333 /* The scheduler has not yet started, so the block period of
\r
334 portMAX_DELAY should just get set to zero in xTimerStart(). Also,
\r
335 the timer queue is not yet full so xTimerStart() should return
\r
337 if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) != pdPASS )
\r
339 xTestStatus = pdFAIL;
\r
340 configASSERT( xTestStatus );
\r
345 /* The timers queue should now be full, so it should be possible to create
\r
346 another timer, but not possible to start it (the timer queue will not get
\r
347 drained until the scheduler has been started. */
\r
348 xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] = xTimerCreate( "FR Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */
\r
349 ( configTIMER_QUEUE_LENGTH * xBasePeriod ), /* The period for the timer. */
\r
350 pdTRUE, /* Auto-reload is set to true. */
\r
351 ( void * ) xTimer, /* An identifier for the timer as all the auto reload timers use the same callback. */
\r
352 prvAutoReloadTimerCallback ); /* The callback executed when the timer expires. */
\r
354 if( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] == NULL )
\r
356 xTestStatus = pdFAIL;
\r
357 configASSERT( xTestStatus );
\r
361 if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) == pdPASS )
\r
363 /* This time it would not be expected that the timer could be
\r
364 started at this point. */
\r
365 xTestStatus = pdFAIL;
\r
366 configASSERT( xTestStatus );
\r
370 /* Create the timers that are used from the tick interrupt to test the timer
\r
371 API functions that can be called from an ISR. */
\r
372 xISRAutoReloadTimer = xTimerCreate( "ISR AR", /* The text name given to the timer. */
\r
373 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
374 pdTRUE, /* This is an auto reload timer. */
\r
375 ( void * ) NULL, /* The identifier is not required. */
\r
376 prvISRAutoReloadTimerCallback ); /* The callback that is executed when the timer expires. */
\r
378 xISROneShotTimer = xTimerCreate( "ISR OS", /* The text name given to the timer. */
\r
379 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
380 pdFALSE, /* This is a one shot timer. */
\r
381 ( void * ) NULL, /* The identifier is not required. */
\r
382 prvISROneShotTimerCallback ); /* The callback that is executed when the timer expires. */
\r
384 if( ( xISRAutoReloadTimer == NULL ) || ( xISROneShotTimer == NULL ) )
\r
386 xTestStatus = pdFAIL;
\r
387 configASSERT( xTestStatus );
\r
390 /*-----------------------------------------------------------*/
\r
392 static void prvTest2_CheckTaskAndTimersInitialState( void )
\r
396 /* Ensure all the timers are in their expected initial state. This depends
\r
397 on the timer service task having a higher priority than this task.
\r
399 auto reload timers 0 to ( configTIMER_QUEUE_LENGTH - 1 ) should now be active,
\r
400 and auto reload timer configTIMER_QUEUE_LENGTH should not yet be active (it
\r
401 could not be started prior to the scheduler being started when it was
\r
403 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
405 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
407 xTestStatus = pdFAIL;
\r
408 configASSERT( xTestStatus );
\r
412 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] ) != pdFALSE )
\r
414 xTestStatus = pdFAIL;
\r
415 configASSERT( xTestStatus );
\r
418 /*-----------------------------------------------------------*/
\r
420 static void prvTest3_CheckAutoReloadExpireRates( void )
\r
422 uint8_t ucMaxAllowableValue, ucMinAllowableValue, ucTimer;
\r
423 TickType_t xBlockPeriod, xTimerPeriod, xExpectedNumber;
\r
425 /* Check the auto reload timers expire at the expected rates. */
\r
428 /* Delaying for configTIMER_QUEUE_LENGTH * xBasePeriod ticks should allow
\r
429 all the auto reload timers to expire at least once. */
\r
430 xBlockPeriod = ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod;
\r
431 vTaskDelay( xBlockPeriod );
\r
433 /* Check that all the auto reload timers have called their callback
\r
434 function the expected number of times. */
\r
435 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
437 /* The expected number of expiries is equal to the block period divided
\r
438 by the timer period. */
\r
439 xTimerPeriod = ( ( ( TickType_t ) ucTimer + ( TickType_t ) 1 ) * xBasePeriod );
\r
440 xExpectedNumber = xBlockPeriod / xTimerPeriod;
\r
442 ucMaxAllowableValue = ( ( uint8_t ) xExpectedNumber ) ;
\r
443 ucMinAllowableValue = ( uint8_t ) ( ( uint8_t ) xExpectedNumber - ( uint8_t ) 1 ); /* Weird casting to try and please all compilers. */
\r
445 if( ( ucAutoReloadTimerCounters[ ucTimer ] < ucMinAllowableValue ) ||
\r
446 ( ucAutoReloadTimerCounters[ ucTimer ] > ucMaxAllowableValue )
\r
449 xTestStatus = pdFAIL;
\r
450 configASSERT( xTestStatus );
\r
454 if( xTestStatus == pdPASS )
\r
456 /* No errors have been reported so increment the loop counter so the
\r
457 check task knows this task is still running. */
\r
461 /*-----------------------------------------------------------*/
\r
463 static void prvTest4_CheckAutoReloadTimersCanBeStopped( void )
\r
467 /* Check the auto reload timers can be stopped correctly, and correctly
\r
468 report their state. */
\r
470 /* Stop all the active timers. */
\r
471 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
473 /* The timer has not been stopped yet! */
\r
474 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
476 xTestStatus = pdFAIL;
\r
477 configASSERT( xTestStatus );
\r
480 /* Now stop the timer. This will appear to happen immediately to
\r
481 this task because this task is running at a priority below the
\r
482 timer service task. */
\r
483 xTimerStop( xAutoReloadTimers[ ucTimer ], tmrdemoDONT_BLOCK );
\r
485 /* The timer should now be inactive. */
\r
486 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )
\r
488 xTestStatus = pdFAIL;
\r
489 configASSERT( xTestStatus );
\r
493 taskENTER_CRITICAL();
\r
495 /* The timer in array position configTIMER_QUEUE_LENGTH should not
\r
496 be active. The critical section is used to ensure the timer does
\r
497 not call its callback between the next line running and the array
\r
498 being cleared back to zero, as that would mask an error condition. */
\r
499 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH ] != ( uint8_t ) 0 )
\r
501 xTestStatus = pdFAIL;
\r
502 configASSERT( xTestStatus );
\r
505 /* Clear the timer callback count. */
\r
506 memset( ( void * ) ucAutoReloadTimerCounters, 0, sizeof( ucAutoReloadTimerCounters ) );
\r
508 taskEXIT_CRITICAL();
\r
510 /* The timers are now all inactive, so this time, after delaying, none
\r
511 of the callback counters should have incremented. */
\r
512 vTaskDelay( ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod );
\r
513 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
515 if( ucAutoReloadTimerCounters[ ucTimer ] != ( uint8_t ) 0 )
\r
517 xTestStatus = pdFAIL;
\r
518 configASSERT( xTestStatus );
\r
522 if( xTestStatus == pdPASS )
\r
524 /* No errors have been reported so increment the loop counter so
\r
525 the check task knows this task is still running. */
\r
529 /*-----------------------------------------------------------*/
\r
531 static void prvTest5_CheckBasicOneShotTimerBehaviour( void )
\r
533 /* Check the one shot timer only calls its callback once after it has been
\r
534 started, and that it reports its state correctly. */
\r
536 /* The one shot timer should not be active yet. */
\r
537 if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )
\r
539 xTestStatus = pdFAIL;
\r
540 configASSERT( xTestStatus );
\r
543 if( ucOneShotTimerCounter != ( uint8_t ) 0 )
\r
545 xTestStatus = pdFAIL;
\r
546 configASSERT( xTestStatus );
\r
549 /* Start the one shot timer and check that it reports its state correctly. */
\r
550 xTimerStart( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
551 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
553 xTestStatus = pdFAIL;
\r
554 configASSERT( xTestStatus );
\r
557 /* Delay for three times as long as the one shot timer period, then check
\r
558 to ensure it has only called its callback once, and is now not in the
\r
560 vTaskDelay( tmrdemoONE_SHOT_TIMER_PERIOD * ( TickType_t ) 3 );
\r
562 if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )
\r
564 xTestStatus = pdFAIL;
\r
565 configASSERT( xTestStatus );
\r
568 if( ucOneShotTimerCounter != ( uint8_t ) 1 )
\r
570 xTestStatus = pdFAIL;
\r
571 configASSERT( xTestStatus );
\r
575 /* Reset the one shot timer callback count. */
\r
576 ucOneShotTimerCounter = ( uint8_t ) 0;
\r
579 if( xTestStatus == pdPASS )
\r
581 /* No errors have been reported so increment the loop counter so the
\r
582 check task knows this task is still running. */
\r
586 /*-----------------------------------------------------------*/
\r
588 static void prvTest6_CheckAutoReloadResetBehaviour( void )
\r
592 /* Check timer reset behaviour. */
\r
594 /* Restart the one shot timer and check it reports its status correctly. */
\r
595 xTimerStart( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
596 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
598 xTestStatus = pdFAIL;
\r
599 configASSERT( xTestStatus );
\r
602 /* Restart one of the auto reload timers and check that it reports its
\r
603 status correctly. */
\r
604 xTimerStart( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
605 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
607 xTestStatus = pdFAIL;
\r
608 configASSERT( xTestStatus );
\r
611 for( ucTimer = 0; ucTimer < trmdemoNUM_TIMER_RESETS; ucTimer++ )
\r
613 /* Delay for half as long as the one shot timer period, then reset it.
\r
614 It should never expire while this is done, so its callback count should
\r
615 never increment. */
\r
616 vTaskDelay( tmrdemoONE_SHOT_TIMER_PERIOD / 2 );
\r
618 /* Check both running timers are still active, but have not called their
\r
619 callback functions. */
\r
620 if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )
\r
622 xTestStatus = pdFAIL;
\r
623 configASSERT( xTestStatus );
\r
626 if( ucOneShotTimerCounter != ( uint8_t ) 0 )
\r
628 xTestStatus = pdFAIL;
\r
629 configASSERT( xTestStatus );
\r
632 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
634 xTestStatus = pdFAIL;
\r
635 configASSERT( xTestStatus );
\r
638 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] != ( uint8_t ) 0 )
\r
640 xTestStatus = pdFAIL;
\r
641 configASSERT( xTestStatus );
\r
644 /* Reset both running timers. */
\r
645 xTimerReset( xOneShotTimer, tmrdemoDONT_BLOCK );
\r
646 xTimerReset( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
648 if( xTestStatus == pdPASS )
\r
650 /* No errors have been reported so increment the loop counter so
\r
651 the check task knows this task is still running. */
\r
656 /* Finally delay long enough for both running timers to expire. */
\r
657 vTaskDelay( ( ( TickType_t ) configTIMER_QUEUE_LENGTH ) * xBasePeriod );
\r
659 /* The timers were not reset during the above delay period so should now
\r
660 both have called their callback functions. */
\r
661 if( ucOneShotTimerCounter != ( uint8_t ) 1 )
\r
663 xTestStatus = pdFAIL;
\r
664 configASSERT( xTestStatus );
\r
667 if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] == 0 )
\r
669 xTestStatus = pdFAIL;
\r
670 configASSERT( xTestStatus );
\r
673 /* The one shot timer should no longer be active, while the auto reload
\r
674 timer should still be active. */
\r
675 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )
\r
677 xTestStatus = pdFAIL;
\r
678 configASSERT( xTestStatus );
\r
681 if( xTimerIsTimerActive( xOneShotTimer ) == pdTRUE )
\r
683 xTestStatus = pdFAIL;
\r
684 configASSERT( xTestStatus );
\r
687 /* Stop the auto reload timer again. */
\r
688 xTimerStop( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );
\r
690 if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) != pdFALSE )
\r
692 xTestStatus = pdFAIL;
\r
693 configASSERT( xTestStatus );
\r
696 /* Clear the timer callback counts, ready for another iteration of these
\r
698 ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] = ( uint8_t ) 0;
\r
699 ucOneShotTimerCounter = ( uint8_t ) 0;
\r
701 if( xTestStatus == pdPASS )
\r
703 /* No errors have been reported so increment the loop counter so the check
\r
704 task knows this task is still running. */
\r
708 /*-----------------------------------------------------------*/
\r
710 static void prvResetStartConditionsForNextIteration( void )
\r
714 /* Start the timers again to start all the tests over again. */
\r
716 /* Start the timers again. */
\r
717 for( ucTimer = 0; ucTimer < ( uint8_t ) configTIMER_QUEUE_LENGTH; ucTimer++ )
\r
719 /* The timer has not been started yet! */
\r
720 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )
\r
722 xTestStatus = pdFAIL;
\r
723 configASSERT( xTestStatus );
\r
726 /* Now start the timer. This will appear to happen immediately to
\r
727 this task because this task is running at a priority below the timer
\r
729 xTimerStart( xAutoReloadTimers[ ucTimer ], tmrdemoDONT_BLOCK );
\r
731 /* The timer should now be active. */
\r
732 if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )
\r
734 xTestStatus = pdFAIL;
\r
735 configASSERT( xTestStatus );
\r
739 if( xTestStatus == pdPASS )
\r
741 /* No errors have been reported so increment the loop counter so the
\r
742 check task knows this task is still running. */
\r
746 /*-----------------------------------------------------------*/
\r
748 void vTimerPeriodicISRTests( void )
\r
750 static TickType_t uxTick = ( TickType_t ) -1;
\r
752 #if( configTIMER_TASK_PRIORITY != ( configMAX_PRIORITIES - 1 ) )
\r
753 /* The timer service task is not the highest priority task, so it cannot
\r
754 be assumed that timings will be exact. Timers should never call their
\r
755 callback before their expiry time, but a margin is permissible for calling
\r
756 their callback after their expiry time. If exact timing is required then
\r
757 configTIMER_TASK_PRIORITY must be set to ensure the timer service task
\r
758 is the highest priority task in the system.
\r
760 This function is called from the tick hook. The tick hook is called
\r
761 even when the scheduler is suspended. Therefore it is possible that the
\r
762 uxTick count maintained in this function is temporarily ahead of the tick
\r
763 count maintained by the kernel. When this is the case a message posted from
\r
764 this function will assume a time stamp in advance of the real time stamp,
\r
765 which can result in a timer being processed before this function expects it
\r
766 to. For example, if the kernel's tick count was 100, and uxTick was 102,
\r
767 then this function will not expect the timer to have expired until the
\r
768 kernel's tick count is (102 + xBasePeriod), whereas in reality the timer
\r
769 will expire when the kernel's tick count is (100 + xBasePeriod). For this
\r
770 reason xMargin is used as an allowable margin for premature timer expiries
\r
771 as well as late timer expiries. */
\r
773 /* Windows is not real real time. */
\r
774 const TickType_t xMargin = 20;
\r
776 const TickType_t xMargin = 6;
\r
777 #endif /* _WINDOWS_ */
\r
780 /* Windows is not real real time. */
\r
781 const TickType_t xMargin = 20;
\r
783 const TickType_t xMargin = 4;
\r
784 #endif /* _WINDOWS_ */
\r
792 /* The timers will have been created, but not started. Start them now
\r
793 by setting their period. */
\r
794 ucISRAutoReloadTimerCounter = 0;
\r
795 ucISROneShotTimerCounter = 0;
\r
797 /* It is possible that the timer task has not yet made room in the
\r
798 timer queue. If the timers cannot be started then reset uxTick so
\r
799 another attempt is made later. */
\r
800 uxTick = ( TickType_t ) -1;
\r
802 /* Try starting first timer. */
\r
803 if( xTimerChangePeriodFromISR( xISRAutoReloadTimer, xBasePeriod, NULL ) == pdPASS )
\r
805 /* First timer was started, try starting the second timer. */
\r
806 if( xTimerChangePeriodFromISR( xISROneShotTimer, xBasePeriod, NULL ) == pdPASS )
\r
808 /* Both timers were started, so set the uxTick back to its
\r
814 /* Second timer could not be started, so stop the first one
\r
816 xTimerStopFromISR( xISRAutoReloadTimer, NULL );
\r
820 else if( uxTick == ( xBasePeriod - xMargin ) )
\r
822 /* Neither timer should have expired yet. */
\r
823 if( ( ucISRAutoReloadTimerCounter != 0 ) || ( ucISROneShotTimerCounter != 0 ) )
\r
825 xTestStatus = pdFAIL;
\r
826 configASSERT( xTestStatus );
\r
829 else if( uxTick == ( xBasePeriod + xMargin ) )
\r
831 /* Both timers should now have expired once. The auto reload timer will
\r
832 still be active, but the one shot timer should now have stopped. */
\r
833 if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )
\r
835 xTestStatus = pdFAIL;
\r
836 configASSERT( xTestStatus );
\r
839 else if( uxTick == ( ( 2 * xBasePeriod ) - xMargin ) )
\r
841 /* The auto reload timer will still be active, but the one shot timer
\r
842 should now have stopped - however, at this time neither of the timers
\r
843 should have expired again since the last test. */
\r
844 if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )
\r
846 xTestStatus = pdFAIL;
\r
847 configASSERT( xTestStatus );
\r
850 else if( uxTick == ( ( 2 * xBasePeriod ) + xMargin ) )
\r
852 /* The auto reload timer will still be active, but the one shot timer
\r
853 should now have stopped. At this time the auto reload timer should have
\r
854 expired again, but the one shot timer count should not have changed. */
\r
855 if( ucISRAutoReloadTimerCounter != 2 )
\r
857 xTestStatus = pdFAIL;
\r
858 configASSERT( xTestStatus );
\r
861 if( ucISROneShotTimerCounter != 1 )
\r
863 xTestStatus = pdFAIL;
\r
864 configASSERT( xTestStatus );
\r
867 else if( uxTick == ( ( 2 * xBasePeriod ) + ( xBasePeriod >> ( TickType_t ) 2U ) ) )
\r
869 /* The auto reload timer will still be active, but the one shot timer
\r
870 should now have stopped. Again though, at this time, neither timer call
\r
871 back should have been called since the last test. */
\r
872 if( ucISRAutoReloadTimerCounter != 2 )
\r
874 xTestStatus = pdFAIL;
\r
875 configASSERT( xTestStatus );
\r
878 if( ucISROneShotTimerCounter != 1 )
\r
880 xTestStatus = pdFAIL;
\r
881 configASSERT( xTestStatus );
\r
884 else if( uxTick == ( 3 * xBasePeriod ) )
\r
886 /* Start the one shot timer again. */
\r
887 xTimerStartFromISR( xISROneShotTimer, NULL );
\r
889 else if( uxTick == ( ( 3 * xBasePeriod ) + xMargin ) )
\r
891 /* The auto reload timer and one shot timer will be active. At
\r
892 this time the auto reload timer should have expired again, but the one
\r
893 shot timer count should not have changed yet. */
\r
894 if( ucISRAutoReloadTimerCounter != 3 )
\r
896 xTestStatus = pdFAIL;
\r
897 configASSERT( xTestStatus );
\r
900 if( ucISROneShotTimerCounter != 1 )
\r
902 xTestStatus = pdFAIL;
\r
903 configASSERT( xTestStatus );
\r
906 /* Now stop the auto reload timer. The one shot timer was started
\r
907 a few ticks ago. */
\r
908 xTimerStopFromISR( xISRAutoReloadTimer, NULL );
\r
910 else if( uxTick == ( 4 * ( xBasePeriod - xMargin ) ) )
\r
912 /* The auto reload timer is now stopped, and the one shot timer is
\r
913 active, but at this time neither timer should have expired since the
\r
915 if( ucISRAutoReloadTimerCounter != 3 )
\r
917 xTestStatus = pdFAIL;
\r
918 configASSERT( xTestStatus );
\r
921 if( ucISROneShotTimerCounter != 1 )
\r
923 xTestStatus = pdFAIL;
\r
924 configASSERT( xTestStatus );
\r
927 else if( uxTick == ( ( 4 * xBasePeriod ) + xMargin ) )
\r
929 /* The auto reload timer is now stopped, and the one shot timer is
\r
930 active. The one shot timer should have expired again, but the auto
\r
931 reload timer should not have executed its callback. */
\r
932 if( ucISRAutoReloadTimerCounter != 3 )
\r
934 xTestStatus = pdFAIL;
\r
935 configASSERT( xTestStatus );
\r
938 if( ucISROneShotTimerCounter != 2 )
\r
940 xTestStatus = pdFAIL;
\r
941 configASSERT( xTestStatus );
\r
944 else if( uxTick == ( 8 * xBasePeriod ) )
\r
946 /* The auto reload timer is now stopped, and the one shot timer has
\r
947 already expired and then stopped itself. Both callback counters should
\r
948 not have incremented since the last test. */
\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 /* Now reset the one shot timer. */
\r
962 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
964 else if( uxTick == ( ( 9 * xBasePeriod ) - xMargin ) )
\r
966 /* Only the one shot timer should be running, but it should not have
\r
967 expired since the last test. Check the callback counters have not
\r
968 incremented, then reset the one shot timer again. */
\r
969 if( ucISRAutoReloadTimerCounter != 3 )
\r
971 xTestStatus = pdFAIL;
\r
972 configASSERT( xTestStatus );
\r
975 if( ucISROneShotTimerCounter != 2 )
\r
977 xTestStatus = pdFAIL;
\r
978 configASSERT( xTestStatus );
\r
981 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
983 else if( uxTick == ( ( 10 * xBasePeriod ) - ( 2 * xMargin ) ) )
\r
985 /* Only the one shot timer should be running, but it should not have
\r
986 expired since the last test. Check the callback counters have not
\r
987 incremented, then reset the one shot timer again. */
\r
988 if( ucISRAutoReloadTimerCounter != 3 )
\r
990 xTestStatus = pdFAIL;
\r
991 configASSERT( xTestStatus );
\r
994 if( ucISROneShotTimerCounter != 2 )
\r
996 xTestStatus = pdFAIL;
\r
997 configASSERT( xTestStatus );
\r
1000 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
1002 else if( uxTick == ( ( 11 * xBasePeriod ) - ( 3 * xMargin ) ) )
\r
1004 /* Only the one shot timer should be running, but it should not have
\r
1005 expired since the last test. Check the callback counters have not
\r
1006 incremented, then reset the one shot timer once again. */
\r
1007 if( ucISRAutoReloadTimerCounter != 3 )
\r
1009 xTestStatus = pdFAIL;
\r
1010 configASSERT( xTestStatus );
\r
1013 if( ucISROneShotTimerCounter != 2 )
\r
1015 xTestStatus = pdFAIL;
\r
1016 configASSERT( xTestStatus );
\r
1019 xTimerResetFromISR( xISROneShotTimer, NULL );
\r
1021 else if( uxTick == ( ( 12 * xBasePeriod ) - ( 2 * xMargin ) ) )
\r
1023 /* Only the one shot timer should have been running and this time it
\r
1024 should have expired. Check its callback count has been incremented.
\r
1025 The auto reload timer is still not running so should still have the same
\r
1026 count value. This time the one shot timer is not reset so should not
\r
1027 restart from its expiry period again. */
\r
1028 if( ucISRAutoReloadTimerCounter != 3 )
\r
1030 xTestStatus = pdFAIL;
\r
1031 configASSERT( xTestStatus );
\r
1034 if( ucISROneShotTimerCounter != 3 )
\r
1036 xTestStatus = pdFAIL;
\r
1037 configASSERT( xTestStatus );
\r
1040 else if( uxTick == ( 15 * xBasePeriod ) )
\r
1042 /* Neither timer should be running now. Check neither callback count
\r
1043 has incremented, then go back to the start to run these tests all
\r
1045 if( ucISRAutoReloadTimerCounter != 3 )
\r
1047 xTestStatus = pdFAIL;
\r
1048 configASSERT( xTestStatus );
\r
1051 if( ucISROneShotTimerCounter != 3 )
\r
1053 xTestStatus = pdFAIL;
\r
1054 configASSERT( xTestStatus );
\r
1057 uxTick = ( TickType_t ) -1;
\r
1060 /*-----------------------------------------------------------*/
\r
1062 /*** Timer callback functions are defined below here. ***/
\r
1064 static void prvAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer )
\r
1066 uint32_t ulTimerID;
\r
1068 ulTimerID = ( uint32_t ) pvTimerGetTimerID( pxExpiredTimer );
\r
1069 if( ulTimerID <= ( configTIMER_QUEUE_LENGTH + 1 ) )
\r
1071 ( ucAutoReloadTimerCounters[ ulTimerID ] )++;
\r
1075 /* The timer ID appears to be unexpected (invalid). */
\r
1076 xTestStatus = pdFAIL;
\r
1077 configASSERT( xTestStatus );
\r
1080 /*-----------------------------------------------------------*/
\r
1082 static void prvOneShotTimerCallback( TimerHandle_t pxExpiredTimer )
\r
1084 /* The parameter is not used in this case as only one timer uses this
\r
1085 callback function. */
\r
1086 ( void ) pxExpiredTimer;
\r
1088 ucOneShotTimerCounter++;
\r
1090 /*-----------------------------------------------------------*/
\r
1092 static void prvISRAutoReloadTimerCallback( TimerHandle_t pxExpiredTimer )
\r
1094 /* The parameter is not used in this case as only one timer uses this
\r
1095 callback function. */
\r
1096 ( void ) pxExpiredTimer;
\r
1098 ucISRAutoReloadTimerCounter++;
\r
1100 /*-----------------------------------------------------------*/
\r
1102 static void prvISROneShotTimerCallback( TimerHandle_t pxExpiredTimer )
\r
1104 /* The parameter is not used in this case as only one timer uses this
\r
1105 callback function. */
\r
1106 ( void ) pxExpiredTimer;
\r
1108 ucISROneShotTimerCounter++;
\r
1110 /*-----------------------------------------------------------*/
\r