-/* Need to consider the switching of timer lists, and the placement of tasks into\r
-the current and overflow timer lists very carefully. For example, should the\r
-assessment as to which list a timer should be inserted into be relative the the\r
-tick count at the timer, or the tick count when the timer task unblocked, etc. */\r
-\r
/*\r
FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
\r
typedef struct tmrTimerQueueMessage\r
{\r
portBASE_TYPE xMessageID; /*<< The command being sent to the timer service task. */\r
- portTickType xMessageValue; /*<< An optional value used by a subset of commands, for example, when chaning the period of a timer. */\r
+ portTickType xMessageValue; /*<< An optional value used by a subset of commands, for example, when changing the period of a timer. */\r
xTIMER * pxTimer; /*<< The timer to which the command will be applied. */\r
} xTIMER_MESSAGE;\r
\r
/*-----------------------------------------------------------*/\r
\r
/*\r
- * Initialise the infrustructure used by the timer service task if it has not\r
+ * Initialise the infrastructure used by the timer service task if it has not\r
* been initialised already.\r
*/\r
static void prvCheckForValidListAndQueue( void ) PRIVILEGED_FUNCTION;\r
* Called by the timer service task to interpret and process a command it\r
* received on the timer queue.\r
*/\r
-static void prvProcessReceivedCommands( void ) PRIVILEGED_FUNCTION;\r
+static void prvProcessReceivedCommands( portTickType xAssumedTimeNow ) PRIVILEGED_FUNCTION;\r
\r
/*\r
* Insert the timer into either xActiveTimerList1, or xActiveTimerList2, \r
* depending on if the expire time causes a timer counter overflow. \r
*/\r
-static void prvInsertTimerInActiveList( xTIMER *pxTimer, portTickType xNextExpiryTime );\r
+static void prvInsertTimerInActiveList( xTIMER *pxTimer, portTickType xNextExpiryTime, portTickType xAssumedTimeNow ) PRIVILEGED_FUNCTION;\r
+\r
+/*\r
+ * An active timer has reached its expire time. Reload the timer if it is an\r
+ * auto reload timer, then call its callback.\r
+ */\r
+static void prvProcessExpiredTimer( portTickType xNextExpireTime, portTickType xAssumedTimeNow ) PRIVILEGED_FUNCTION;\r
+\r
+/*\r
+ * The tick count has overflowed. Switch the timer lists after ensuring the\r
+ * current timer list does not still reference some timers.\r
+ */\r
+static void prvSwitchTimerLists( portTickType xAssumedTimeNow ) PRIVILEGED_FUNCTION;\r
\r
/*-----------------------------------------------------------*/\r
\r
portBASE_TYPE xReturn = pdFAIL;\r
\r
/* This function is called when the scheduler is started if\r
- configUSE_TIMERS is set to 1. Check that the infrustructure used by the\r
+ configUSE_TIMERS is set to 1. Check that the infrastructure used by the\r
timer service task has been created/initialised. If timers have already\r
been created then the initialisation will already have been performed. */\r
prvCheckForValidListAndQueue();\r
pxNewTimer = ( xTIMER * ) pvPortMalloc( sizeof( xTIMER ) );\r
if( pxNewTimer != NULL )\r
{\r
- /* Ensure the infrustructure used by the timer service task has been\r
+ /* Ensure the infrastructure used by the timer service task has been\r
created/initialised. */\r
prvCheckForValidListAndQueue();\r
\r
}\r
/*-----------------------------------------------------------*/\r
\r
-static void prvTimerTask( void *pvParameters )\r
+static void prvProcessExpiredTimer( portTickType xNextExpireTime, portTickType xAssumedTimeNow )\r
{\r
-portTickType xNextExpireTime, xTimeNow;\r
xTIMER *pxTimer;\r
\r
+ if( listLIST_IS_EMPTY( pxCurrentTimerList ) == pdFALSE )\r
+ {\r
+ /* Remove the timer from the list of active timers. */\r
+ pxTimer = ( xTIMER * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );\r
+ vListRemove( &( pxTimer->xTimerListItem ) );\r
+\r
+ /* If the timer is an auto reload timer then calculate the next\r
+ expiry time and re-insert the timer in the list of active timers. */\r
+ if( pxTimer->uxAutoReload == pdTRUE )\r
+ {\r
+ /* This is the only time a timer is inserted into a list using\r
+ a time relative to anything other than the current time. It\r
+ will therefore be inserted into the correct list relative to\r
+ the time this task thinks it is now, even if a command to\r
+ switch lists due to a tick count overflow is already waiting in\r
+ the timer queue. */\r
+ prvInsertTimerInActiveList( pxTimer, ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ), xAssumedTimeNow );\r
+ }\r
+\r
+ /* Call the timer callback. */\r
+ pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );\r
+ }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvTimerTask( void *pvParameters )\r
+{\r
+portTickType xNextExpireTime, xTimeNow, xFrozenTimeNow;\r
+\r
/* Just to avoid compiler warnings. */\r
( void ) pvParameters;\r
\r
for( ;; )\r
{\r
+ /* Take a snapshot of the time to use while assessing expiry and auto\r
+ reload times. */\r
+ xFrozenTimeNow = xTaskGetTickCount();\r
+\r
/* Timers are listed in expiry time order, with the head of the list\r
referencing the task that will expire first. Obtain the time at which\r
the timer with the nearest expiry time will expire. If there are no\r
active timers then just set the next expire time to the maximum possible\r
- time to ensure this task does not run unnecessarily. */\r
+ time to ensure this task does not run unnecessarily. */\r
if( listLIST_IS_EMPTY( pxCurrentTimerList ) == pdFALSE )\r
{\r
xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );\r
xNextExpireTime = portMAX_DELAY;\r
}\r
\r
- /* Has the timer expired? */\r
- if( xNextExpireTime <= xTaskGetTickCount() )\r
+ /* Has the timer expired? This expiry time is relative to the snapshot\r
+ of the time taken to be used in this loop iteration - so it doesn't \r
+ matter at this point if a tick count overflows here. */\r
+ if( xNextExpireTime <= xFrozenTimeNow )\r
{\r
- /* Remove the timer from the list of active timers. */\r
- pxTimer = listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );\r
- vListRemove( &( pxTimer->xTimerListItem ) );\r
-\r
- /* If the timer is an autoreload timer then calculate the next\r
- expiry time and re-insert the timer in the list of active timers. */\r
- if( pxTimer->uxAutoReload == pdTRUE )\r
- {\r
- prvInsertTimerInActiveList( pxTimer, ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ) ); \r
- }\r
-\r
- /* Call the timer callback. */\r
- pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );\r
+ prvProcessExpiredTimer( xNextExpireTime, xFrozenTimeNow );\r
}\r
else\r
{\r
/* Block this task until the next timer expires, or a command is\r
received. */\r
- taskENTER_CRITICAL();\r
+ vTaskSuspendAll();\r
{\r
+ /* Has the tick overflowed since a time snapshot was taken? */\r
xTimeNow = xTaskGetTickCount();\r
- if( xTimeNow < xNextExpireTime )\r
+ if( xTimeNow >= xFrozenTimeNow )\r
+ {\r
+ /* Has the expire not still not been met? The tick count\r
+ may be greater now than when the time snapshot was taken. */\r
+ if( xNextExpireTime <= xTimeNow )\r
+ {\r
+ prvProcessExpiredTimer( xNextExpireTime, xFrozenTimeNow );\r
+ }\r
+ else\r
+ {\r
+ /* The tick count has not overflowed since the time \r
+ snapshot, and the next expire time has not been reached\r
+ since the last snapshot was taken. This task should\r
+ therefore block to wait for the next expire time. */\r
+ vQueueWaitForMessageRestricted( xTimerQueue, ( xNextExpireTime - xTimeNow ) );\r
+ }\r
+ }\r
+ else\r
{\r
- /* This is a simple fast function - a yield will not be\r
- performed until after this critical section exits. */\r
- vQueueWaitForMessageRestricted( xTimerQueue, ( xNextExpireTime - xTimeNow ) );\r
+ /* The tick count has overflowed since the time snapshot\r
+ was taken, therefore, the task should not block but continue\r
+ with another loop. The command queue should contain a\r
+ command to switch lists. */\r
}\r
}\r
- taskEXIT_CRITICAL();\r
+ if( xTaskResumeAll() == pdFALSE )\r
+ {\r
+ /* Yield to wait for either a command to arrive, or the block time\r
+ to expire. If a command arrived between the critical section being\r
+ exited and this yield then the yield will just return to the same\r
+ task. */\r
+ portYIELD_WITHIN_API();\r
+ }\r
\r
- /* Yield to wait for either a command to arrive, or the block time\r
- to expire. If a command arrived between the critical section being\r
- exited and this yeild then the yield will just return to the same\r
- task. */\r
- portYIELD_WITHIN_API();\r
+ /* Take a snapshot of the time now for use in this iteration of the\r
+ task loop. */\r
+ xFrozenTimeNow = xTaskGetTickCount();\r
\r
/* Empty the command queue, if it contains any commands. */\r
- prvProcessReceivedCommands();\r
+ prvProcessReceivedCommands( xFrozenTimeNow );\r
}\r
}\r
}\r
/*-----------------------------------------------------------*/\r
\r
-static void prvInsertTimerInActiveList( xTIMER *pxTimer, portTickType xNextExpiryTime )\r
+static void prvInsertTimerInActiveList( xTIMER *pxTimer, portTickType xNextExpiryTime, portTickType xAssumedTimeNow )\r
{\r
listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xNextExpiryTime );\r
listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );\r
\r
- if( xNextExpiryTime < xTaskGetTickCount() )\r
+ if( xNextExpiryTime < xAssumedTimeNow )\r
{\r
vListInsert( pxOverflowTimerList, &( pxTimer->xTimerListItem ) );\r
}\r
}\r
/*-----------------------------------------------------------*/\r
\r
-static void prvProcessReceivedCommands( void )\r
+static void prvProcessReceivedCommands( portTickType xAssumedTimeNow )\r
{\r
xTIMER_MESSAGE xMessage;\r
xTIMER *pxTimer;\r
-xList *pxTemp;\r
+portBASE_TYPE xSwitchListsOnExit = pdFALSE;\r
\r
while( xQueueReceive( xTimerQueue, &xMessage, tmrNO_DELAY ) != pdFAIL )\r
{\r
pxTimer = xMessage.pxTimer;\r
\r
- /* Is the timer already in the list of active timers? When the command\r
+ /* Is the timer already in a list of active timers? When the command\r
is trmCOMMAND_PROCESS_TIMER_OVERFLOW, the timer will be NULL as the\r
command is to the task rather than to an individual timer. */\r
if( pxTimer != NULL )\r
{\r
if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )\r
{\r
- /* The timer is in the list, remove it. */\r
+ /* The timer is in a list, remove it. */\r
vListRemove( &( pxTimer->xTimerListItem ) );\r
}\r
}\r
{\r
case tmrCOMMAND_START : \r
/* Start or restart a timer. */\r
- prvInsertTimerInActiveList( pxTimer, xTaskGetTickCount() + pxTimer->xTimerPeriodInTicks );\r
+ prvInsertTimerInActiveList( pxTimer, xAssumedTimeNow + pxTimer->xTimerPeriodInTicks, xAssumedTimeNow );\r
break;\r
\r
case tmrCOMMAND_STOP : \r
\r
case tmrCOMMAND_CHANGE_PERIOD :\r
pxTimer->xTimerPeriodInTicks = xMessage.xMessageValue;\r
- prvInsertTimerInActiveList( pxTimer, ( xTaskGetTickCount() + pxTimer->xTimerPeriodInTicks ) );\r
+ prvInsertTimerInActiveList( pxTimer, ( xAssumedTimeNow + pxTimer->xTimerPeriodInTicks ), xAssumedTimeNow );\r
break;\r
\r
case tmrCOMMAND_DELETE :\r
break;\r
\r
case trmCOMMAND_PROCESS_TIMER_OVERFLOW :\r
- /* The tick count has overflowed. The timer lists must be\r
- switched. */\r
- pxTemp = pxCurrentTimerList;\r
- pxCurrentTimerList = pxOverflowTimerList;\r
- pxOverflowTimerList = pxTemp;\r
+ /* Hold this pending until all the other messages have been \r
+ processed. */\r
+ xSwitchListsOnExit = pdTRUE;\r
break;\r
\r
default : \r
break;\r
}\r
}\r
+\r
+ if( xSwitchListsOnExit == pdTRUE )\r
+ {\r
+ prvSwitchTimerLists( xAssumedTimeNow );\r
+ }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvSwitchTimerLists( portTickType xAssumedTimeNow )\r
+{\r
+portTickType xNextExpireTime;\r
+xList *pxTemp;\r
+\r
+ /* The tick count has overflowed. The timer lists must be switched. \r
+ If there are any timers still referenced from the current timer list \r
+ then they must have expired and should be processed before the lists \r
+ are switched. */\r
+ while( listLIST_IS_EMPTY( pxCurrentTimerList ) == pdFALSE )\r
+ {\r
+ xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );\r
+ prvProcessExpiredTimer( xNextExpireTime, xAssumedTimeNow );\r
+ }\r
+\r
+ pxTemp = pxCurrentTimerList;\r
+ pxCurrentTimerList = pxOverflowTimerList;\r
+ pxOverflowTimerList = pxTemp;\r
}\r
/*-----------------------------------------------------------*/\r
\r
}\r
/*-----------------------------------------------------------*/\r
\r
+\r
+\r
+\r