pxDelayedTaskList = pxOverflowDelayedTaskList;\r
pxOverflowDelayedTaskList = pxTemp;\r
xNumOfOverflows++;\r
+ \r
+ #if configUSE_TIMERS == 1\r
+ {\r
+ /* The timer service task needs to know to switch its lists\r
+ too. */\r
+ xTimerGenericCommand( NULL, trmCOMMAND_PROCESS_TIMER_OVERFLOW, 0, 0 );\r
+ }\r
+ #endif\r
+ \r
if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )\r
{\r
/* The delayed list is empty. Set xNextTaskUnblockTime to the\r
}\r
/*-----------------------------------------------------------*/\r
\r
+#if configUSE_TIMERS == 1\r
+\r
+ void vTaskPlaceOnEventListRestricted( const xList * const pxEventList, portTickType xTicksToWait )\r
+ {\r
+ portTickType xTimeToWake;\r
+\r
+ /* This function should not be called by application code hence the \r
+ 'Restricted' in its name. It is not part of the public API. It is \r
+ designed for use by kernel code, and has special calling requirements - \r
+ it should be called from a critical section. */\r
+\r
+ \r
+ /* Place the event list item of the TCB in the appropriate event list.\r
+ In this case it is assume that this is the only task that is going to\r
+ be waiting on this event list, so the faster vListInsertEnd() function\r
+ can be used in place of vListInsert. */\r
+ vListInsertEnd( ( xList * ) pxEventList, ( xListItem * ) &( pxCurrentTCB->xEventListItem ) );\r
+\r
+ /* We must remove this task from the ready list before adding it to the\r
+ blocked list as the same list item is used for both lists. This \r
+ function is called form a critical section. */\r
+ vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
+\r
+ /* Calculate the time at which the task should be woken if the event does\r
+ not occur. This may overflow but this doesn't matter. */\r
+ xTimeToWake = xTickCount + xTicksToWait;\r
+ prvAddCurrentTaskToDelayedList( xTimeToWake );\r
+ }\r
+ \r
+#endif /* configUSE_TIMERS */\r
+/*-----------------------------------------------------------*/\r
+\r
signed portBASE_TYPE xTaskRemoveFromEventList( const xList * const pxEventList )\r
{\r
tskTCB *pxUnblockedTCB;\r
-/* Need a method of switching to an overflow list. _RB_*/\r
+/* 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
/* The list in which active timers are stored. Timers are referenced in expire\r
time order, with the nearest expiry time at the front of the list. Only the\r
timer service task is allowed to access xActiveTimerList. */\r
-PRIVILEGED_DATA static xList xActiveTimerList;\r
+PRIVILEGED_DATA static xList xActiveTimerList1;\r
+PRIVILEGED_DATA static xList xActiveTimerList2;\r
+PRIVILEGED_DATA static xList *pxCurrentTimerList;\r
+PRIVILEGED_DATA static xList *pxOverflowTimerList;\r
\r
/* A queue that is used to send commands to the timer service task. */\r
PRIVILEGED_DATA static xQueueHandle xTimerQueue = NULL;\r
*/\r
static void prvProcessReceivedCommands( void ) 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
+\r
/*-----------------------------------------------------------*/\r
\r
portBASE_TYPE xTimerCreateTimerTask( void )\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
- if( listLIST_IS_EMPTY( &xActiveTimerList ) == pdFALSE )\r
+ if( listLIST_IS_EMPTY( pxCurrentTimerList ) == pdFALSE )\r
{\r
- xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( &xActiveTimerList );\r
+ xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );\r
}\r
else\r
{\r
if( xNextExpireTime <= xTaskGetTickCount() )\r
{\r
/* Remove the timer from the list of active timers. */\r
- pxTimer = listGET_OWNER_OF_HEAD_ENTRY( &xActiveTimerList );\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
- listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ) );\r
- vListInsert( &xActiveTimerList, &( pxTimer->xTimerListItem ) );\r
+ prvInsertTimerInActiveList( pxTimer, ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ) ); \r
}\r
\r
/* Call the timer callback. */\r
}\r
/*-----------------------------------------------------------*/\r
\r
+static void prvInsertTimerInActiveList( xTIMER *pxTimer, portTickType xNextExpiryTime )\r
+{\r
+ listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xNextExpiryTime );\r
+ listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );\r
+ \r
+ if( xNextExpiryTime < xTaskGetTickCount() )\r
+ {\r
+ vListInsert( pxOverflowTimerList, &( pxTimer->xTimerListItem ) );\r
+ }\r
+ else\r
+ {\r
+ vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );\r
+ }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
static void prvProcessReceivedCommands( void )\r
{\r
xTIMER_MESSAGE xMessage;\r
-portTickType xTimeToExpire;\r
xTIMER *pxTimer;\r
+xList *pxTemp;\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? */\r
- if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )\r
+ /* Is the timer already in the 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
- /* The timer is in the list, remove it. */\r
- vListRemove( &( pxTimer->xTimerListItem ) );\r
+ if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )\r
+ {\r
+ /* The timer is in the list, remove it. */\r
+ vListRemove( &( pxTimer->xTimerListItem ) );\r
+ }\r
}\r
\r
switch( xMessage.xMessageID )\r
{\r
case tmrCOMMAND_START : \r
/* Start or restart a timer. */\r
- xTimeToExpire = xTaskGetTickCount() + pxTimer->xTimerPeriodInTicks;\r
- listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xTimeToExpire );\r
- listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );\r
- vListInsert( &xActiveTimerList, &( pxTimer->xTimerListItem ) );\r
+ prvInsertTimerInActiveList( pxTimer, xTaskGetTickCount() + pxTimer->xTimerPeriodInTicks );\r
break;\r
\r
case tmrCOMMAND_STOP : \r
\r
case tmrCOMMAND_CHANGE_PERIOD :\r
pxTimer->xTimerPeriodInTicks = xMessage.xMessageValue;\r
- xTimeToExpire = xTaskGetTickCount() + pxTimer->xTimerPeriodInTicks;\r
- listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xTimeToExpire );\r
- listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );\r
- vListInsert( &xActiveTimerList, &( pxTimer->xTimerListItem ) );\r
+ prvInsertTimerInActiveList( pxTimer, ( xTaskGetTickCount() + pxTimer->xTimerPeriodInTicks ) );\r
break;\r
\r
case tmrCOMMAND_DELETE :\r
just free up the memory. */\r
vPortFree( pxTimer );\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
+ break;\r
\r
default : \r
/* Don't expect to get here. */\r
{\r
if( xTimerQueue == NULL )\r
{\r
- vListInitialise( &xActiveTimerList );\r
+ vListInitialise( &xActiveTimerList1 );\r
+ vListInitialise( &xActiveTimerList2 );\r
+ pxCurrentTimerList = &xActiveTimerList1;\r
+ pxOverflowTimerList = &xActiveTimerList2;\r
xTimerQueue = xQueueCreate( configTIMER_QUEUE_LENGTH, sizeof( xTIMER_MESSAGE ) );\r
}\r
}\r
/* Is the timer in the list of active timers? */\r
taskENTER_CRITICAL();\r
{\r
- xTimerIsInActiveList = listIS_CONTAINED_WITHIN( &xActiveTimerList, &( pxTimer->xTimerListItem ) );\r
+ /* Checking to see if it is in the NULL list in effect checks to see if\r
+ it is referenced from either the current or the overflow timer lists in\r
+ one go, but the logic has to be reversed, hence the '!'. */\r
+ xTimerIsInActiveList = !( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) );\r
}\r
taskEXIT_CRITICAL();\r
\r