--- /dev/null
+/*\r
+ FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
+ \r
+\r
+ ***************************************************************************\r
+ * *\r
+ * FreeRTOS tutorial books are available in pdf and paperback. *\r
+ * Complete, revised, and edited pdf reference manuals are also *\r
+ * available. *\r
+ * *\r
+ * Purchasing FreeRTOS documentation will not only help you, by *\r
+ * ensuring you get running as quickly as possible and with an *\r
+ * in-depth knowledge of how to use FreeRTOS, it will also help *\r
+ * the FreeRTOS project to continue with its mission of providing *\r
+ * professional grade, cross platform, de facto standard solutions *\r
+ * for microcontrollers - completely free of charge! *\r
+ * *\r
+ * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *\r
+ * *\r
+ * Thank you for using FreeRTOS, and thank you for your support! *\r
+ * *\r
+ ***************************************************************************\r
+\r
+\r
+ This file is part of the FreeRTOS distribution.\r
+\r
+ FreeRTOS is free software; you can redistribute it and/or modify it under\r
+ the terms of the GNU General Public License (version 2) as published by the\r
+ Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
+ >>>NOTE<<< The modification to the GPL is included to allow you to\r
+ distribute a combined work that includes FreeRTOS without being obliged to\r
+ provide the source code for proprietary components outside of the FreeRTOS\r
+ kernel. FreeRTOS is distributed in the hope that it will be useful, but\r
+ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for\r
+ more details. You should have received a copy of the GNU General Public\r
+ License and the FreeRTOS license exception along with FreeRTOS; if not it\r
+ can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
+ by writing to Richard Barry, contact details for whom are available on the\r
+ FreeRTOS WEB site.\r
+\r
+ 1 tab == 4 spaces!\r
+\r
+ http://www.FreeRTOS.org - Documentation, latest information, license and\r
+ contact details.\r
+\r
+ http://www.SafeRTOS.com - A version that is certified for use in safety\r
+ critical systems.\r
+\r
+ http://www.OpenRTOS.com - Commercial support, development, porting,\r
+ licensing and training services.\r
+*/\r
+\r
+\r
+/*\r
+ * Tests the behaviour of timers. Some timers are created before the scheduler\r
+ * is started, and some after.\r
+ */\r
+\r
+/* Standard includes. */\r
+#include <string.h>\r
+\r
+/* Scheduler include files. */\r
+#include "FreeRTOS.h"\r
+#include "task.h"\r
+#include "timers.h"\r
+\r
+/* Demo program include files. */\r
+#include "TimerDemo.h"\r
+\r
+#if ( configTIMER_TASK_PRIORITY < 1 )\r
+ #error configTIMER_TASK_PRIORITY must be set to at least 1 for this test/demo to function correctly.\r
+#endif\r
+\r
+#define tmrdemoDONT_BLOCK ( ( portTickType ) 0 )\r
+#define tmrdemoONE_SHOT_TIMER_PERIOD ( xBasePeriod * ( portTickType ) 3 )\r
+#define trmdemoNUM_TIMER_RESETS ( ( unsigned char ) 10 )\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+/* The callback functions used by the timers. These each increment a counter\r
+to indicate which timer has expired. The auto-reload timers that are used by\r
+the test task (as opposed to being used from an ISR) all share the same\r
+prvAutoReloadTimerCallback() callback function, and use the ID of the\r
+pxExpiredTimer parameter passed into that function to know which counter to\r
+increment. The other timers all have their own unique callback function and\r
+simply increment their counters without using the callback function parameter. */\r
+static void prvAutoReloadTimerCallback( xTimerHandle pxExpiredTimer );\r
+static void prvOneShotTimerCallback( xTimerHandle pxExpiredTimer );\r
+static void prvTimerTestTask( void *pvParameters );\r
+static void prvISRAutoReloadTimerCallback( xTimerHandle pxExpiredTimer );\r
+static void prvISROneShotTimerCallback( xTimerHandle pxExpiredTimer );\r
+\r
+/* The test functions used by the timer test task. These manipulate the auto\r
+reload and one shot timers in various ways, then delay, then inspect the timers\r
+to ensure they have behaved as expected. */\r
+static void prvTest1_CreateTimersWithoutSchedulerRunning( void );\r
+static void prvTest2_CheckTaskAndTimersInitialState( void );\r
+static void prvTest3_CheckAutoReloadExpireRates( void );\r
+static void prvTest4_CheckAutoReloadTimersCanBeStopped( void );\r
+static void prvTest5_CheckBasicOneShotTimerBehaviour( void );\r
+static void prvTest6_CheckAutoReloadResetBehaviour( void );\r
+static void prvResetStartConditionsForNextIteration( void );\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+/* Flag that will be latched to pdFAIL should any unexpected behaviour be\r
+detected in any of the demo tests. */\r
+static volatile portBASE_TYPE xTestStatus = pdPASS;\r
+\r
+/* Counter that is incremented on each cycle of a test. This is used to\r
+detect a stalled task - a test that is no longer running. */\r
+static volatile unsigned long ulLoopCounter = 0;\r
+\r
+/* A set of auto reload timers - each of which use the same callback function.\r
+The callback function uses the timer ID to index into, and then increment, a\r
+counter in the ucAutoReloadTimerCounters[] array. The auto reload timers\r
+referenced from xAutoReloadTimers[] are used by the prvTimerTestTask task. */\r
+static xTimerHandle xAutoReloadTimers[ configTIMER_QUEUE_LENGTH + 1 ] = { 0 };\r
+static unsigned char ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH + 1 ] = { 0 };\r
+\r
+/* The one shot timer is configured to use a callback function that increments\r
+ucOneShotTimerCounter each time it gets called. */\r
+static xTimerHandle xOneShotTimer = NULL;\r
+static unsigned char ucOneShotTimerCounter = ( unsigned char ) 0;\r
+\r
+/* The ISR reload timer is controlled from the tick hook to exercise the timer\r
+API functions that can be used from an ISR. It is configured to increment\r
+ucISRReloadTimerCounter each time its callback function is executed. */\r
+static xTimerHandle xISRAutoReloadTimer = NULL;\r
+static unsigned char ucISRAutoReloadTimerCounter = ( unsigned char ) 0;\r
+\r
+/* The ISR one shot timer is controlled from the tick hook to exercise the timer\r
+API functions that can be used from an ISR. It is configured to increment\r
+ucISRReloadTimerCounter each time its callback function is executed. */\r
+static xTimerHandle xISROneShotTimer = NULL;\r
+static unsigned char ucISROneShotTimerCounter = ( unsigned char ) 0;\r
+\r
+/* The period of all the timers are a multiple of the base period. The base\r
+period is configured by the parameter to vStartTimerDemoTask(). */\r
+static portTickType xBasePeriod = 0;\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+void vStartTimerDemoTask( portTickType xBasePeriodIn )\r
+{\r
+ /* Start with the timer and counter arrays clear - this is only necessary\r
+ where the compiler does not clear them automatically on start up. */\r
+ memset( ucAutoReloadTimerCounters, 0x00, sizeof( ucAutoReloadTimerCounters ) );\r
+ memset( xAutoReloadTimers, 0x00, sizeof( xAutoReloadTimers ) );\r
+\r
+ /* Store the period from which all the timer periods will be generated from\r
+ (multiples of). */\r
+ xBasePeriod = xBasePeriodIn;\r
+\r
+ /* Create a set of timers for use by this demo/test. */\r
+ prvTest1_CreateTimersWithoutSchedulerRunning();\r
+\r
+ /* Create the task that will control and monitor the timers. This is\r
+ created at a lower priority than the timer service task to ensure, as\r
+ far as it is concerned, commands on timers are actioned immediately\r
+ (sending a command to the timer service task will unblock the timer service\r
+ task, which will then preempt this task). */\r
+ if( xTestStatus != pdFAIL )\r
+ {\r
+ xTaskCreate( prvTimerTestTask, ( signed portCHAR * ) "Tmr Tst", configMINIMAL_STACK_SIZE, NULL, configTIMER_TASK_PRIORITY - 1, NULL );\r
+ }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvTimerTestTask( void *pvParameters )\r
+{\r
+ ( void ) pvParameters;\r
+\r
+ /* Create a one-shot timer for use later on in this test. */\r
+ xOneShotTimer = xTimerCreate( ( const signed char * ) "Oneshot Timer",/* Text name to facilitate debugging. The kernel does not use this itself. */\r
+ tmrdemoONE_SHOT_TIMER_PERIOD, /* The period for the timer. */\r
+ pdFALSE, /* Don't auto-reload - hence a one shot timer. */\r
+ ( void * ) 0, /* The timer identifier. In this case this is not used as the timer has its own callback. */\r
+ prvOneShotTimerCallback ); /* The callback to be called when the timer expires. */\r
+\r
+ if( xOneShotTimer == NULL )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+\r
+\r
+ /* Ensure all the timers are in their expected initial state. This\r
+ depends on the timer service task having a higher priority than this task. */\r
+ prvTest2_CheckTaskAndTimersInitialState();\r
+\r
+ for( ;; )\r
+ {\r
+ /* Check the auto reload timers expire at the expected/correct rates. */\r
+ prvTest3_CheckAutoReloadExpireRates();\r
+\r
+ /* Check the auto reload timers can be stopped correctly, and correctly\r
+ report their state. */\r
+ prvTest4_CheckAutoReloadTimersCanBeStopped();\r
+ \r
+ /* Check the one shot timer only calls its callback once after it has been\r
+ started, and that it reports its state correctly. */\r
+ prvTest5_CheckBasicOneShotTimerBehaviour();\r
+\r
+ /* Check timer reset behaviour. */\r
+ prvTest6_CheckAutoReloadResetBehaviour();\r
+\r
+ /* Start the timers again to restart all the tests over again. */\r
+ prvResetStartConditionsForNextIteration();\r
+ }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+/* This is called to check that the created task is still running and has not\r
+detected any errors. */\r
+portBASE_TYPE xAreTimerDemoTasksStillRunning( portTickType xCycleFrequency )\r
+{\r
+static unsigned long ulLastLoopCounter = 0UL;\r
+portTickType xMaxBlockTimeUsedByTheseTests, xLoopCounterIncrementTimeMax;\r
+static portTickType xIterationsWithoutCounterIncrement = ( portTickType ) 0, xLastCycleFrequency;\r
+\r
+ if( xLastCycleFrequency != xCycleFrequency )\r
+ {\r
+ /* The cycle frequency has probably become much faster due to an error\r
+ elsewhere. Start counting Iterations again. */\r
+ xIterationsWithoutCounterIncrement = ( portTickType ) 0;\r
+ xLastCycleFrequency = xCycleFrequency;\r
+ } \r
+\r
+ /* Calculate the maximum number of times that it is permissible for this\r
+ function to be called without ulLoopCounter being incremented. This is\r
+ necessary because the tests in this file block for extended periods, and the\r
+ block period might be longer than the time between calls to this function. */\r
+ xMaxBlockTimeUsedByTheseTests = ( ( portTickType ) configTIMER_QUEUE_LENGTH ) * xBasePeriod;\r
+ xLoopCounterIncrementTimeMax = xMaxBlockTimeUsedByTheseTests / xCycleFrequency;\r
+\r
+ /* If the demo task is still running then we expect the loopcounter to\r
+ have incremented every xLoopCounterIncrementTimeMax calls. */\r
+ if( ulLastLoopCounter == ulLoopCounter )\r
+ {\r
+ xIterationsWithoutCounterIncrement++;\r
+ if( xIterationsWithoutCounterIncrement > xLoopCounterIncrementTimeMax )\r
+ {\r
+ /* The tests appear to be no longer running (stalled). */\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ }\r
+ else\r
+ {\r
+ /* ulLoopCounter changed, so the count of times this function was called\r
+ without a change can be reset to zero. */\r
+ xIterationsWithoutCounterIncrement = ( portTickType ) 0;\r
+ }\r
+\r
+ ulLastLoopCounter = ulLoopCounter;\r
+\r
+ /* Errors detected in the task itself will have latched xTestStatus\r
+ to pdFAIL. */\r
+\r
+ return xTestStatus;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvTest1_CreateTimersWithoutSchedulerRunning( void )\r
+{\r
+unsigned portBASE_TYPE xTimer;\r
+\r
+ for( xTimer = 0; xTimer < configTIMER_QUEUE_LENGTH; xTimer++ )\r
+ {\r
+ /* As the timer queue is not yet full, it should be possible to both create\r
+ and start a timer. These timers are being started before the scheduler has\r
+ been started, so their block times should get set to zero within the timer\r
+ API itself. */\r
+ xAutoReloadTimers[ xTimer ] = xTimerCreate( ( const signed char * )"FR Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */\r
+ ( ( xTimer + ( portTickType ) 1 ) * xBasePeriod ),/* The period for the timer. The plus 1 ensures a period of zero is not specified. */\r
+ pdTRUE, /* Auto-reload is set to true. */\r
+ ( void * ) xTimer, /* An identifier for the timer as all the auto reload timers use the same callback. */\r
+ prvAutoReloadTimerCallback ); /* The callback to be called when the timer expires. */\r
+\r
+ if( xAutoReloadTimers[ xTimer ] == NULL )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ else\r
+ {\r
+ /* The scheduler has not yet started, so the block period of\r
+ portMAX_DELAY should just get set to zero in xTimerStart(). Also,\r
+ the timer queue is not yet full so xTimerStart() should return\r
+ pdPASS. */\r
+ if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) != pdPASS )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ }\r
+ }\r
+\r
+ /* The timers queue should now be full, so it should be possible to create\r
+ another timer, but not possible to start it (the timer queue will not get\r
+ drained until the scheduler has been started. */\r
+ xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] = xTimerCreate( ( const signed char * ) "FR Timer", /* Text name to facilitate debugging. The kernel does not use this itself. */\r
+ ( configTIMER_QUEUE_LENGTH * xBasePeriod ), /* The period for the timer. */\r
+ pdTRUE, /* Auto-reload is set to true. */\r
+ ( void * ) xTimer, /* An identifier for the timer as all the auto reload timers use the same callback. */\r
+ prvAutoReloadTimerCallback ); /* The callback executed when the timer expires. */\r
+\r
+ if( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] == NULL )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ else\r
+ {\r
+ if( xTimerStart( xAutoReloadTimers[ xTimer ], portMAX_DELAY ) == pdPASS )\r
+ {\r
+ /* This time it would not be expected that the timer could be\r
+ started at this point. */\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ }\r
+ \r
+ /* Create the timers that are used from the tick interrupt to test the timer\r
+ API functions that can be called from an ISR. */\r
+ xISRAutoReloadTimer = xTimerCreate( ( const signed char * ) "ISR AR", /* The text name given to the timer. */\r
+ 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
+ pdTRUE, /* This is an auto reload timer. */\r
+ ( void * ) NULL, /* The identifier is not required. */\r
+ prvISRAutoReloadTimerCallback ); /* The callback that is executed when the timer expires. */\r
+\r
+ xISROneShotTimer = xTimerCreate( ( const signed char * ) "ISR OS", /* The text name given to the timer. */\r
+ 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
+ pdFALSE, /* This is a one shot timer. */\r
+ ( void * ) NULL, /* The identifier is not required. */\r
+ prvISROneShotTimerCallback ); /* The callback that is executed when the timer expires. */\r
+ \r
+ if( ( xISRAutoReloadTimer == NULL ) || ( xISROneShotTimer == NULL ) )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+}\r
+/*-----------------------------------------------------------*/\r
+volatile int icalls = 0;\r
+static void prvTest2_CheckTaskAndTimersInitialState( void )\r
+{\r
+unsigned char ucTimer;\r
+icalls++;\r
+ /* Ensure all the timers are in their expected initial state. This depends\r
+ on the timer service task having a higher priority than this task.\r
+\r
+ auto reload timers 0 to ( configTIMER_QUEUE_LENGTH - 1 ) should now be active,\r
+ and auto reload timer configTIMER_QUEUE_LENGTH should not yet be active (it\r
+ could not be started prior to the scheduler being started when it was\r
+ created). */\r
+ for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )\r
+ {\r
+ if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ }\r
+\r
+ if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH ] ) != pdFALSE )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvTest3_CheckAutoReloadExpireRates( void )\r
+{\r
+unsigned char ucMaxAllowableValue, ucMinAllowableValue, ucTimer;\r
+portTickType xBlockPeriod, xTimerPeriod, xExpectedNumber;\r
+\r
+ /* Check the auto reload timers expire at the expected rates. */\r
+\r
+ \r
+ /* Delaying for configTIMER_QUEUE_LENGTH * xBasePeriod ticks should allow\r
+ all the auto reload timers to expire at least once. */\r
+ xBlockPeriod = ( ( portTickType ) configTIMER_QUEUE_LENGTH ) * xBasePeriod;\r
+ vTaskDelay( xBlockPeriod );\r
+\r
+ /* Check that all the auto reload timers have called their callback \r
+ function the expected number of times. */\r
+ for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )\r
+ {\r
+ /* The expected number of expiries is equal to the block period divided\r
+ by the timer period. */\r
+ xTimerPeriod = ( ( ( portTickType ) ucTimer + ( portTickType ) 1 ) * xBasePeriod );\r
+ xExpectedNumber = xBlockPeriod / xTimerPeriod;\r
+ \r
+ ucMaxAllowableValue = ( ( unsigned char ) xExpectedNumber ) ;\r
+ ucMinAllowableValue = ( ( unsigned char ) xExpectedNumber - ( unsigned char ) 1 );\r
+\r
+ if( ( ucAutoReloadTimerCounters[ ucTimer ] < ucMinAllowableValue ) ||\r
+ ( ucAutoReloadTimerCounters[ ucTimer ] > ucMaxAllowableValue )\r
+ )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ }\r
+\r
+ if( xTestStatus == pdPASS )\r
+ {\r
+ /* No errors have been reported so increment the loop counter so the\r
+ check task knows this task is still running. */\r
+ ulLoopCounter++;\r
+ }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvTest4_CheckAutoReloadTimersCanBeStopped( void )\r
+{ \r
+unsigned char ucTimer;\r
+\r
+ /* Check the auto reload timers can be stopped correctly, and correctly\r
+ report their state. */\r
+\r
+ /* Stop all the active timers. */\r
+ for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )\r
+ {\r
+ /* The timer has not been stopped yet! */\r
+ if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+\r
+ /* Now stop the timer. This will appear to happen immediately to\r
+ this task because this task is running at a priority below the\r
+ timer service task. */\r
+ xTimerStop( xAutoReloadTimers[ ucTimer ], tmrdemoDONT_BLOCK );\r
+\r
+ /* The timer should now be inactive. */\r
+ if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ }\r
+\r
+ taskENTER_CRITICAL();\r
+ {\r
+ /* The timer in array position configTIMER_QUEUE_LENGTH should not\r
+ be active. The critical section is used to ensure the timer does\r
+ not call its callback between the next line running and the array\r
+ being cleared back to zero, as that would mask an error condition. */\r
+ if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH ] != ( unsigned char ) 0 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+\r
+ /* Clear the timer callback count. */\r
+ memset( ( void * ) ucAutoReloadTimerCounters, 0, sizeof( ucAutoReloadTimerCounters ) );\r
+ }\r
+ taskEXIT_CRITICAL();\r
+\r
+ /* The timers are now all inactive, so this time, after delaying, none\r
+ of the callback counters should have incremented. */\r
+ vTaskDelay( ( ( portTickType ) configTIMER_QUEUE_LENGTH ) * xBasePeriod );\r
+ for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )\r
+ {\r
+ if( ucAutoReloadTimerCounters[ ucTimer ] != ( unsigned char ) 0 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ }\r
+\r
+ if( xTestStatus == pdPASS )\r
+ {\r
+ /* No errors have been reported so increment the loop counter so\r
+ the check task knows this task is still running. */\r
+ ulLoopCounter++;\r
+ }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvTest5_CheckBasicOneShotTimerBehaviour( void )\r
+{\r
+ /* Check the one shot timer only calls its callback once after it has been\r
+ started, and that it reports its state correctly. */\r
+\r
+ /* The one shot timer should not be active yet. */\r
+ if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+\r
+ if( ucOneShotTimerCounter != ( unsigned char ) 0 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+\r
+ /* Start the one shot timer and check that it reports its state correctly. */\r
+ xTimerStart( xOneShotTimer, tmrdemoDONT_BLOCK );\r
+ if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+\r
+ /* Delay for three times as long as the one shot timer period, then check\r
+ to ensure it has only called its callback once, and is now not in the\r
+ active state. */\r
+ vTaskDelay( tmrdemoONE_SHOT_TIMER_PERIOD * ( portTickType ) 3 );\r
+\r
+ if( xTimerIsTimerActive( xOneShotTimer ) != pdFALSE )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+\r
+ if( ucOneShotTimerCounter != ( unsigned char ) 1 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ else\r
+ {\r
+ /* Reset the one shot timer callback count. */\r
+ ucOneShotTimerCounter = ( unsigned char ) 0;\r
+ }\r
+\r
+ if( xTestStatus == pdPASS )\r
+ {\r
+ /* No errors have been reported so increment the loop counter so the\r
+ check task knows this task is still running. */\r
+ ulLoopCounter++;\r
+ }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvTest6_CheckAutoReloadResetBehaviour( void )\r
+{\r
+unsigned char ucTimer;\r
+\r
+ /* Check timer reset behaviour. */\r
+\r
+ /* Restart the one shot timer and check it reports its status correctly. */\r
+ xTimerStart( xOneShotTimer, tmrdemoDONT_BLOCK );\r
+ if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+\r
+ /* Restart one of the auto reload timers and check that it reports its\r
+ status correctly. */\r
+ xTimerStart( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );\r
+ if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+\r
+ for( ucTimer = 0; ucTimer < trmdemoNUM_TIMER_RESETS; ucTimer++ )\r
+ {\r
+ /* Delay for half as long as the one shot timer period, then reset it.\r
+ It should never expire while this is done, so its callback count should\r
+ never increment. */\r
+ vTaskDelay( tmrdemoONE_SHOT_TIMER_PERIOD / 2 );\r
+\r
+ /* Check both running timers are still active, but have not called their\r
+ callback functions. */\r
+ if( xTimerIsTimerActive( xOneShotTimer ) == pdFALSE )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+\r
+ if( ucOneShotTimerCounter != ( unsigned char ) 0 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+\r
+ if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+\r
+ if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] != ( unsigned char ) 0 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+\r
+ /* Reset both running timers. */\r
+ xTimerReset( xOneShotTimer, tmrdemoDONT_BLOCK );\r
+ xTimerReset( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );\r
+\r
+ if( xTestStatus == pdPASS )\r
+ {\r
+ /* No errors have been reported so increment the loop counter so\r
+ the check task knows this task is still running. */\r
+ ulLoopCounter++;\r
+ }\r
+ }\r
+\r
+ /* Finally delay long enough for both running timers to expire. */\r
+ vTaskDelay( ( ( portTickType ) configTIMER_QUEUE_LENGTH ) * xBasePeriod );\r
+\r
+ /* The timers were not reset during the above delay period so should now\r
+ both have called their callback functions. */\r
+ if( ucOneShotTimerCounter != ( unsigned char ) 1 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+\r
+ if( ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] == 0 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+\r
+ /* The one shot timer should no longer be active, while the auto reload\r
+ timer should still be active. */\r
+ if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) == pdFALSE )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+\r
+ if( xTimerIsTimerActive( xOneShotTimer ) == pdTRUE )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+\r
+ /* Stop the auto reload timer again. */\r
+ xTimerStop( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ], tmrdemoDONT_BLOCK );\r
+\r
+ if( xTimerIsTimerActive( xAutoReloadTimers[ configTIMER_QUEUE_LENGTH - 1 ] ) != pdFALSE )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+\r
+ /* Clear the timer callback counts, ready for another iteration of these\r
+ tests. */\r
+ ucAutoReloadTimerCounters[ configTIMER_QUEUE_LENGTH - 1 ] = ( unsigned char ) 0;\r
+ ucOneShotTimerCounter = ( unsigned char ) 0;\r
+\r
+ if( xTestStatus == pdPASS )\r
+ {\r
+ /* No errors have been reported so increment the loop counter so the check\r
+ task knows this task is still running. */\r
+ ulLoopCounter++;\r
+ }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvResetStartConditionsForNextIteration( void )\r
+{\r
+unsigned char ucTimer;\r
+\r
+ /* Start the timers again to start all the tests over again. */\r
+\r
+ /* Start the timers again. */\r
+ for( ucTimer = 0; ucTimer < ( unsigned char ) configTIMER_QUEUE_LENGTH; ucTimer++ )\r
+ {\r
+ /* The timer has not been started yet! */\r
+ if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) != pdFALSE )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+\r
+ /* Now start the timer. This will appear to happen immediately to\r
+ this task because this task is running at a priority below the timer\r
+ service task. */\r
+ xTimerStart( xAutoReloadTimers[ ucTimer ], tmrdemoDONT_BLOCK );\r
+\r
+ /* The timer should now be active. */\r
+ if( xTimerIsTimerActive( xAutoReloadTimers[ ucTimer ] ) == pdFALSE )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ }\r
+\r
+ if( xTestStatus == pdPASS )\r
+ {\r
+ /* No errors have been reported so increment the loop counter so the\r
+ check task knows this task is still running. */\r
+ ulLoopCounter++;\r
+ }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+void vTimerPeriodicISRTests( void )\r
+{\r
+static portTickType uxTick = ( portTickType ) -1;\r
+\r
+/* The xHigherPriorityTaskWoken parameter is not used in this case as this\r
+function is called from the tick hook anyway. However the API required it\r
+to be present. */\r
+portBASE_TYPE xHigherPriorityTaskWoken = pdTRUE;\r
+portTickType xMargin;\r
+\r
+ if( configTIMER_TASK_PRIORITY != ( configMAX_PRIORITIES - 1 ) )\r
+ {\r
+ /* The timer service task is not the highest priority task, so it cannot\r
+ be assumed that timings will be exact. Timers should never call their\r
+ callback before their expiry time, but a margin is permissible for calling\r
+ their callback after their expiry time. If exact timing is required then\r
+ configTIMER_TASK_PRIORITY must be set to ensure the timer service task\r
+ is the highest priority task in the system. */\r
+ xMargin = 5;\r
+ }\r
+ else\r
+ {\r
+ xMargin = 1;\r
+ }\r
+\r
+ /* This test is called from the tick ISR even when the scheduler is suspended.\r
+ Therefore, it is possible for the xTickCount to be temporarily less than the\r
+ uxTicks count maintained in this function. That can result in calculated\r
+ unblock times being too short, as this function is not called as missed ticks\r
+ (ticks that occur while the scheduler is suspended) are unwound to re-instate\r
+ the real tick value. Therefore, if this happens, just abandon the test\r
+ and start again. */\r
+ if( xTaskGetSchedulerState() != taskSCHEDULER_RUNNING )\r
+ {\r
+ uxTick = ( portTickType ) -1;\r
+ }\r
+ else\r
+ {\r
+ uxTick++;\r
+ }\r
+\r
+ if( uxTick == 0 )\r
+ {\r
+ /* The timers will have been created, but not started. Start them\r
+ now by setting their period. */\r
+ ucISRAutoReloadTimerCounter = 0;\r
+ ucISROneShotTimerCounter = 0;\r
+ xTimerChangePeriodFromISR( xISRAutoReloadTimer, xBasePeriod, &xHigherPriorityTaskWoken );\r
+ xTimerChangePeriodFromISR( xISROneShotTimer, xBasePeriod, &xHigherPriorityTaskWoken );\r
+ }\r
+ else if( uxTick == xBasePeriod )\r
+ {\r
+ /* Neither timer should have expired yet. */\r
+ if( ( ucISRAutoReloadTimerCounter != 0 ) || ( ucISROneShotTimerCounter != 0 ) )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ }\r
+ else if( uxTick == ( xBasePeriod + xMargin ) )\r
+ {\r
+ /* Both timers should now have expired once. The auto reload timer will\r
+ still be active, but the one shot timer should now have stopped. */\r
+ if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ }\r
+ else if( uxTick == ( 2 * xBasePeriod ) )\r
+ {\r
+ /* The auto reload timer will still be active, but the one shot timer\r
+ should now have stopped - however, at this time neither of the timers\r
+ should have expired again since the last test. */\r
+ if( ( ucISRAutoReloadTimerCounter != 1 ) || ( ucISROneShotTimerCounter != 1 ) )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ } \r
+ }\r
+ else if( uxTick == ( ( 2 * xBasePeriod ) + xMargin ) )\r
+ {\r
+ /* The auto reload timer will still be active, but the one shot timer\r
+ should now have stopped. At this time the auto reload timer should have\r
+ expired again, but the one shot timer count should not have changed. */\r
+ if( ucISRAutoReloadTimerCounter != 2 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ \r
+ if( ucISROneShotTimerCounter != 1 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ }\r
+ else if( uxTick == ( ( 2 * xBasePeriod ) + ( xBasePeriod >> ( portTickType ) 2U ) ) )\r
+ {\r
+ /* The auto reload timer will still be active, but the one shot timer\r
+ should now have stopped. Again though, at this time, neither timer call\r
+ back should have been called since the last test. */\r
+ if( ucISRAutoReloadTimerCounter != 2 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ \r
+ if( ucISROneShotTimerCounter != 1 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ } \r
+ else if( uxTick == ( 3 * xBasePeriod ) )\r
+ {\r
+ /* Start the one shot timer again. */\r
+ xTimerStartFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );\r
+ }\r
+ else if( uxTick == ( ( 3 * xBasePeriod ) + xMargin ) )\r
+ {\r
+ /* The auto reload timer and one shot timer will be active. At\r
+ this time the auto reload timer should have expired again, but the one\r
+ shot timer count should not have changed yet. */\r
+ if( ucISRAutoReloadTimerCounter != 3 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ \r
+ if( ucISROneShotTimerCounter != 1 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ \r
+ /* Now stop the auto reload timer. The one shot timer was started\r
+ a few ticks ago. */\r
+ xTimerStopFromISR( xISRAutoReloadTimer, &xHigherPriorityTaskWoken );\r
+ } \r
+ else if( uxTick == ( 4 * xBasePeriod ) )\r
+ {\r
+ /* The auto reload timer is now stopped, and the one shot timer is\r
+ active, but at this time neither timer should have expired since the\r
+ last test. */\r
+ if( ucISRAutoReloadTimerCounter != 3 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ \r
+ if( ucISROneShotTimerCounter != 1 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ } \r
+ else if( uxTick == ( ( 4 * xBasePeriod ) + xMargin ) )\r
+ {\r
+ /* The auto reload timer is now stopped, and the one shot timer is\r
+ active. The one shot timer should have expired again, but the auto\r
+ reload timer should not have executed its callback. */\r
+ if( ucISRAutoReloadTimerCounter != 3 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ \r
+ if( ucISROneShotTimerCounter != 2 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ } \r
+ else if( uxTick == ( ( 8 * xBasePeriod ) + xMargin ) )\r
+ {\r
+ /* The auto reload timer is now stopped, and the one shot timer has\r
+ already expired and then stopped itself. Both callback counters should\r
+ not have incremented since the last test. */\r
+ if( ucISRAutoReloadTimerCounter != 3 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ \r
+ if( ucISROneShotTimerCounter != 2 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ \r
+ /* Now reset the one shot timer. */\r
+ xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );\r
+ } \r
+ else if( uxTick == ( 9 * xBasePeriod ) )\r
+ {\r
+ /* Only the one shot timer should be running, but it should not have\r
+ expired since the last test. Check the callback counters have not\r
+ incremented, then reset the one shot timer again. */\r
+ if( ucISRAutoReloadTimerCounter != 3 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ \r
+ if( ucISROneShotTimerCounter != 2 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ \r
+ xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );\r
+ } \r
+ else if( uxTick == ( 10 * xBasePeriod ) )\r
+ {\r
+ /* Only the one shot timer should be running, but it should not have\r
+ expired since the last test. Check the callback counters have not\r
+ incremented, then reset the one shot timer again. */\r
+ if( ucISRAutoReloadTimerCounter != 3 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ \r
+ if( ucISROneShotTimerCounter != 2 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ \r
+ xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );\r
+ }\r
+ else if( uxTick == ( 11 * xBasePeriod ) )\r
+ {\r
+ /* Only the one shot timer should be running, but it should not have\r
+ expired since the last test. Check the callback counters have not\r
+ incremented, then reset the one shot timer once again. */\r
+ if( ucISRAutoReloadTimerCounter != 3 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ \r
+ if( ucISROneShotTimerCounter != 2 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ \r
+ xTimerResetFromISR( xISROneShotTimer, &xHigherPriorityTaskWoken );\r
+ } \r
+ else if( uxTick == ( ( 12 * xBasePeriod ) + xMargin ) )\r
+ {\r
+ /* Only the one shot timer should have been running and this time it\r
+ should have expired. Check its callback count has been incremented.\r
+ The auto reload timer is still not running so should still have the same\r
+ count value. This time the one shot timer is not reset so should not\r
+ restart from its expiry period again. */\r
+ if( ucISRAutoReloadTimerCounter != 3 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ \r
+ if( ucISROneShotTimerCounter != 3 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ }\r
+ else if( uxTick == ( 15 * xBasePeriod ) )\r
+ {\r
+ /* Neither timer should be running now. Check neither callback count\r
+ has incremented, then go back to the start to run these tests all\r
+ over again. */\r
+ if( ucISRAutoReloadTimerCounter != 3 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ \r
+ if( ucISROneShotTimerCounter != 3 )\r
+ {\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+ \r
+ uxTick = ( portTickType ) -1;\r
+ } \r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+/*** Timer callback functions are defined below here. ***/\r
+\r
+static void prvAutoReloadTimerCallback( xTimerHandle pxExpiredTimer )\r
+{\r
+portBASE_TYPE xTimerID;\r
+\r
+ xTimerID = ( portBASE_TYPE ) pvTimerGetTimerID( pxExpiredTimer );\r
+ if( xTimerID <= ( configTIMER_QUEUE_LENGTH + 1 ) )\r
+ {\r
+ ( ucAutoReloadTimerCounters[ xTimerID ] )++;\r
+ }\r
+ else\r
+ {\r
+ /* The timer ID appears to be unexpected (invalid). */\r
+ xTestStatus = pdFAIL;\r
+ configASSERT( xTestStatus );\r
+ }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvOneShotTimerCallback( xTimerHandle pxExpiredTimer )\r
+{\r
+ /* The parameter is not used in this case as only one timer uses this\r
+ callback function. */\r
+ ( void ) pxExpiredTimer;\r
+\r
+ ucOneShotTimerCounter++;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvISRAutoReloadTimerCallback( xTimerHandle pxExpiredTimer )\r
+{\r
+ /* The parameter is not used in this case as only one timer uses this\r
+ callback function. */\r
+ ( void ) pxExpiredTimer;\r
+\r
+ ucISRAutoReloadTimerCounter++;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvISROneShotTimerCallback( xTimerHandle pxExpiredTimer )\r
+{\r
+ /* The parameter is not used in this case as only one timer uses this\r
+ callback function. */\r
+ ( void ) pxExpiredTimer;\r
+\r
+ ucISROneShotTimerCounter++;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+\r
+\r
+\r