]> git.sur5r.net Git - freertos/blob - Source/timers.c
Add some assertion points to timers.c.
[freertos] / Source / timers.c
1 /*\r
2     FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS books - available as PDF or paperback  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
55 all the API functions to use the MPU wrappers.  That should only be done when\r
56 task.h is included from an application file. */\r
57 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
58 \r
59 #include "FreeRTOS.h"\r
60 #include "task.h"\r
61 #include "queue.h"\r
62 #include "timers.h"\r
63 \r
64 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
65 \r
66 /* Misc definitions. */\r
67 #define tmrNO_DELAY             ( portTickType ) 0U\r
68 \r
69 /* The definition of the timers themselves. */\r
70 typedef struct tmrTimerControl\r
71 {\r
72         const signed char               *pcTimerName;           /*<< Text name.  This is not used by the kernel, it is included simply to make debugging easier. */\r
73         xListItem                               xTimerListItem;         /*<< Standard linked list item as used by all kernel features for event management. */\r
74         portTickType                    xTimerPeriodInTicks;/*<< How quickly and often the timer expires. */\r
75         unsigned portBASE_TYPE  uxAutoReload;           /*<< Set to pdTRUE if the timer should be automatically restarted once expired.  Set to pdFALSE if the timer is, in effect, a one shot timer. */\r
76         void                                    *pvTimerID;                     /*<< An ID to identify the timer.  This allows the timer to be identified when the same callback is used for multiple timers. */\r
77         tmrTIMER_CALLBACK               pxCallbackFunction;     /*<< The function that will be called when the timer expires. */\r
78 } xTIMER;\r
79 \r
80 /* The definition of messages that can be sent and received on the timer\r
81 queue. */\r
82 typedef struct tmrTimerQueueMessage\r
83 {\r
84         portBASE_TYPE                   xMessageID;                     /*<< The command being sent to the timer service task. */\r
85         portTickType                    xMessageValue;          /*<< An optional value used by a subset of commands, for example, when changing the period of a timer. */\r
86         xTIMER *                                pxTimer;                        /*<< The timer to which the command will be applied. */\r
87 } xTIMER_MESSAGE;\r
88 \r
89 \r
90 /* The list in which active timers are stored.  Timers are referenced in expire\r
91 time order, with the nearest expiry time at the front of the list.  Only the\r
92 timer service task is allowed to access xActiveTimerList. */\r
93 PRIVILEGED_DATA static xList xActiveTimerList1;\r
94 PRIVILEGED_DATA static xList xActiveTimerList2;\r
95 PRIVILEGED_DATA static xList *pxCurrentTimerList;\r
96 PRIVILEGED_DATA static xList *pxOverflowTimerList;\r
97 \r
98 /* A queue that is used to send commands to the timer service task. */\r
99 PRIVILEGED_DATA static xQueueHandle xTimerQueue = NULL;\r
100 \r
101 /*-----------------------------------------------------------*/\r
102 \r
103 /*\r
104  * Initialise the infrastructure used by the timer service task if it has not\r
105  * been initialised already.\r
106  */\r
107 static void prvCheckForValidListAndQueue( void ) PRIVILEGED_FUNCTION;\r
108 \r
109 /*\r
110  * The timer service task (daemon).  Timer functionality is controlled by this\r
111  * task.  Other tasks communicate with the timer service task using the\r
112  * xTimerQueue queue.\r
113  */\r
114 static void prvTimerTask( void *pvParameters ) PRIVILEGED_FUNCTION;\r
115 \r
116 /*\r
117  * Called by the timer service task to interpret and process a command it\r
118  * received on the timer queue.\r
119  */\r
120 static void     prvProcessReceivedCommands( void ) PRIVILEGED_FUNCTION;\r
121 \r
122 /*\r
123  * Insert the timer into either xActiveTimerList1, or xActiveTimerList2,\r
124  * depending on if the expire time causes a timer counter overflow.\r
125  */\r
126 static portBASE_TYPE prvInsertTimerInActiveList( xTIMER *pxTimer, portTickType xNextExpiryTime, portTickType xTimeNow, portTickType xCommandTime ) PRIVILEGED_FUNCTION;\r
127 \r
128 /*\r
129  * An active timer has reached its expire time.  Reload the timer if it is an\r
130  * auto reload timer, then call its callback.\r
131  */\r
132 static void prvProcessExpiredTimer( portTickType xNextExpireTime, portTickType xTimeNow ) PRIVILEGED_FUNCTION;\r
133 \r
134 /*\r
135  * The tick count has overflowed.  Switch the timer lists after ensuring the\r
136  * current timer list does not still reference some timers.\r
137  */\r
138 static void prvSwitchTimerLists( portTickType xLastTime ) PRIVILEGED_FUNCTION;\r
139 \r
140 /*\r
141  * Obtain the current tick count, setting *pxTimerListsWereSwitched to pdTRUE\r
142  * if a tick count overflow occurred since prvSampleTimeNow() was last called.\r
143  */\r
144 static portTickType prvSampleTimeNow( portBASE_TYPE *pxTimerListsWereSwitched ) PRIVILEGED_FUNCTION;\r
145 \r
146 /*\r
147  * If the timer list contains any active timers then return the expire time of\r
148  * the timer that will expire first and set *pxListWasEmpty to false.  If the\r
149  * timer list does not contain any timers then return 0 and set *pxListWasEmpty\r
150  * to pdTRUE.\r
151  */\r
152 static portTickType prvGetNextExpireTime( portBASE_TYPE *pxListWasEmpty ) PRIVILEGED_FUNCTION;\r
153 \r
154 /*\r
155  * If a timer has expired, process it.  Otherwise, block the timer service task\r
156  * until either a timer does expire or a command is received.\r
157  */\r
158 static void prvProcessTimerOrBlockTask( portTickType xNextExpireTime, portBASE_TYPE xListWasEmpty ) PRIVILEGED_FUNCTION;\r
159 \r
160 /*-----------------------------------------------------------*/\r
161 \r
162 portBASE_TYPE xTimerCreateTimerTask( void )\r
163 {\r
164 portBASE_TYPE xReturn = pdFAIL;\r
165 \r
166         /* This function is called when the scheduler is started if\r
167         configUSE_TIMERS is set to 1.  Check that the infrastructure used by the\r
168         timer service task has been created/initialised.  If timers have already\r
169         been created then the initialisation will already have been performed. */\r
170         prvCheckForValidListAndQueue();\r
171 \r
172         if( xTimerQueue != NULL )\r
173         {\r
174                 xReturn = xTaskCreate( prvTimerTask, ( const signed char * ) "Tmr Svc", configTIMER_TASK_STACK_DEPTH, NULL, configTIMER_TASK_PRIORITY, NULL);\r
175         }\r
176 \r
177         return xReturn;\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 xTimerHandle xTimerCreate( const signed char *pcTimerName, portTickType xTimerPeriodInTicks, unsigned portBASE_TYPE uxAutoReload, void *pvTimerID, tmrTIMER_CALLBACK pxCallbackFunction )\r
182 {\r
183 xTIMER *pxNewTimer;\r
184 \r
185         /* Allocate the timer structure. */\r
186         if( xTimerPeriodInTicks == ( portTickType ) 0U )\r
187         {\r
188                 pxNewTimer = NULL;\r
189                 configASSERT( ( xTimerPeriodInTicks > 0 ) );\r
190         }\r
191         else\r
192         {\r
193                 pxNewTimer = ( xTIMER * ) pvPortMalloc( sizeof( xTIMER ) );\r
194                 if( pxNewTimer != NULL )\r
195                 {\r
196                         /* Ensure the infrastructure used by the timer service task has been\r
197                         created/initialised. */\r
198                         prvCheckForValidListAndQueue();\r
199         \r
200                         configASSERT( ( xTimerPeriodInTicks > 0 ) );\r
201         \r
202                         /* Initialise the timer structure members using the function parameters. */\r
203                         pxNewTimer->pcTimerName = pcTimerName;\r
204                         pxNewTimer->xTimerPeriodInTicks = xTimerPeriodInTicks;\r
205                         pxNewTimer->uxAutoReload = uxAutoReload;\r
206                         pxNewTimer->pvTimerID = pvTimerID;\r
207                         pxNewTimer->pxCallbackFunction = pxCallbackFunction;\r
208                         vListInitialiseItem( &( pxNewTimer->xTimerListItem ) );\r
209                 }\r
210         }\r
211         \r
212         return ( xTimerHandle ) pxNewTimer;\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 portBASE_TYPE xTimerGenericCommand( xTimerHandle xTimer, portBASE_TYPE xCommandID, portTickType xOptionalValue, portBASE_TYPE *pxHigherPriorityTaskWoken, portTickType xBlockTime )\r
217 {\r
218 portBASE_TYPE xReturn = pdFAIL;\r
219 xTIMER_MESSAGE xMessage;\r
220 \r
221         /* Send a message to the timer service task to perform a particular action\r
222         on a particular timer definition. */\r
223         if( xTimerQueue != NULL )\r
224         {\r
225                 /* Send a command to the timer service task to start the xTimer timer. */\r
226                 xMessage.xMessageID = xCommandID;\r
227                 xMessage.xMessageValue = xOptionalValue;\r
228                 xMessage.pxTimer = ( xTIMER * ) xTimer;\r
229 \r
230                 if( pxHigherPriorityTaskWoken == NULL )\r
231                 {\r
232                         if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )\r
233                         {\r
234                                 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xBlockTime );\r
235                         }\r
236                         else\r
237                         {\r
238                                 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, tmrNO_DELAY );\r
239                         }\r
240                 }\r
241                 else\r
242                 {\r
243                         xReturn = xQueueSendToBackFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );\r
244                 }\r
245         }\r
246         \r
247         return xReturn;\r
248 }\r
249 /*-----------------------------------------------------------*/\r
250 \r
251 static void prvProcessExpiredTimer( portTickType xNextExpireTime, portTickType xTimeNow )\r
252 {\r
253 xTIMER *pxTimer;\r
254 portBASE_TYPE xResult;\r
255 \r
256         /* Remove the timer from the list of active timers.  A check has already\r
257         been performed to ensure the list is not empty. */\r
258         pxTimer = ( xTIMER * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );\r
259         vListRemove( &( pxTimer->xTimerListItem ) );\r
260 \r
261         /* If the timer is an auto reload timer then calculate the next\r
262         expiry time and re-insert the timer in the list of active timers. */\r
263         if( pxTimer->uxAutoReload == pdTRUE )\r
264         {\r
265                 /* This is the only time a timer is inserted into a list using\r
266                 a time relative to anything other than the current time.  It\r
267                 will therefore be inserted into the correct list relative to\r
268                 the time this task thinks it is now, even if a command to\r
269                 switch lists due to a tick count overflow is already waiting in\r
270                 the timer queue. */\r
271                 if( prvInsertTimerInActiveList( pxTimer, ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ), xTimeNow, xNextExpireTime ) == pdTRUE )\r
272                 {\r
273                         /* The timer expired before it was added to the active timer\r
274                         list.  Reload it now.  */\r
275                         xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START, xNextExpireTime, NULL, tmrNO_DELAY );\r
276                         configASSERT( xResult );\r
277                         ( void ) xResult;\r
278                 }\r
279         }\r
280 \r
281         /* Call the timer callback. */\r
282         pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );\r
283 }\r
284 /*-----------------------------------------------------------*/\r
285 \r
286 static void prvTimerTask( void *pvParameters )\r
287 {\r
288 portTickType xNextExpireTime;\r
289 portBASE_TYPE xListWasEmpty;\r
290 \r
291         /* Just to avoid compiler warnings. */\r
292         ( void ) pvParameters;\r
293 \r
294         for( ;; )\r
295         {\r
296                 /* Query the timers list to see if it contains any timers, and if so,\r
297                 obtain the time at which the next timer will expire. */\r
298                 xNextExpireTime = prvGetNextExpireTime( &xListWasEmpty );\r
299 \r
300                 /* If a timer has expired, process it.  Otherwise, block this task\r
301                 until either a timer does expire, or a command is received. */\r
302                 prvProcessTimerOrBlockTask( xNextExpireTime, xListWasEmpty );\r
303                 \r
304                 /* Empty the command queue. */\r
305                 prvProcessReceivedCommands();           \r
306         }\r
307 }\r
308 /*-----------------------------------------------------------*/\r
309 \r
310 static void prvProcessTimerOrBlockTask( portTickType xNextExpireTime, portBASE_TYPE xListWasEmpty )\r
311 {\r
312 portTickType xTimeNow;\r
313 portBASE_TYPE xTimerListsWereSwitched;\r
314 \r
315         vTaskSuspendAll();\r
316         {\r
317                 /* Obtain the time now to make an assessment as to whether the timer\r
318                 has expired or not.  If obtaining the time causes the lists to switch\r
319                 then don't process this timer as any timers that remained in the list\r
320                 when the lists were switched will have been processed within the\r
321                 prvSampelTimeNow() function. */\r
322                 xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );\r
323                 if( xTimerListsWereSwitched == pdFALSE )\r
324                 {\r
325                         /* The tick count has not overflowed, has the timer expired? */\r
326                         if( ( xListWasEmpty == pdFALSE ) && ( xNextExpireTime <= xTimeNow ) )\r
327                         {\r
328                                 xTaskResumeAll();\r
329                                 prvProcessExpiredTimer( xNextExpireTime, xTimeNow );\r
330                         }\r
331                         else\r
332                         {\r
333                                 /* The tick count has not overflowed, and the next expire\r
334                                 time has not been reached yet.  This task should therefore\r
335                                 block to wait for the next expire time or a command to be\r
336                                 received - whichever comes first.  The following line cannot\r
337                                 be reached unless xNextExpireTime > xTimeNow, except in the\r
338                                 case when the current timer list is empty. */\r
339                                 vQueueWaitForMessageRestricted( xTimerQueue, ( xNextExpireTime - xTimeNow ) );\r
340 \r
341                                 if( xTaskResumeAll() == pdFALSE )\r
342                                 {\r
343                                         /* Yield to wait for either a command to arrive, or the block time\r
344                                         to expire.  If a command arrived between the critical section being\r
345                                         exited and this yield then the yield will not cause the task\r
346                                         to block. */\r
347                                         portYIELD_WITHIN_API();\r
348                                 }\r
349                         }\r
350                 }\r
351                 else\r
352                 {\r
353                         xTaskResumeAll();\r
354                 }\r
355         }\r
356 }\r
357 /*-----------------------------------------------------------*/\r
358 \r
359 static portTickType prvGetNextExpireTime( portBASE_TYPE *pxListWasEmpty )\r
360 {\r
361 portTickType xNextExpireTime;\r
362 \r
363         /* Timers are listed in expiry time order, with the head of the list\r
364         referencing the task that will expire first.  Obtain the time at which\r
365         the timer with the nearest expiry time will expire.  If there are no\r
366         active timers then just set the next expire time to 0.  That will cause\r
367         this task to unblock when the tick count overflows, at which point the\r
368         timer lists will be switched and the next expiry time can be\r
369         re-assessed.  */\r
370         *pxListWasEmpty = listLIST_IS_EMPTY( pxCurrentTimerList );\r
371         if( *pxListWasEmpty == pdFALSE )\r
372         {\r
373                 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );\r
374         }\r
375         else\r
376         {\r
377                 /* Ensure the task unblocks when the tick count rolls over. */\r
378                 xNextExpireTime = ( portTickType ) 0U;\r
379         }\r
380 \r
381         return xNextExpireTime;\r
382 }\r
383 /*-----------------------------------------------------------*/\r
384 \r
385 static portTickType prvSampleTimeNow( portBASE_TYPE *pxTimerListsWereSwitched )\r
386 {\r
387 portTickType xTimeNow;\r
388 static portTickType xLastTime = ( portTickType ) 0U;\r
389 \r
390         xTimeNow = xTaskGetTickCount();\r
391         \r
392         if( xTimeNow < xLastTime )\r
393         {\r
394                 prvSwitchTimerLists( xLastTime );\r
395                 *pxTimerListsWereSwitched = pdTRUE;\r
396         }\r
397         else\r
398         {\r
399                 *pxTimerListsWereSwitched = pdFALSE;\r
400         }\r
401         \r
402         xLastTime = xTimeNow;\r
403         \r
404         return xTimeNow;\r
405 }\r
406 /*-----------------------------------------------------------*/\r
407 \r
408 static portBASE_TYPE prvInsertTimerInActiveList( xTIMER *pxTimer, portTickType xNextExpiryTime, portTickType xTimeNow, portTickType xCommandTime )\r
409 {\r
410 portBASE_TYPE xProcessTimerNow = pdFALSE;\r
411 \r
412         listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xNextExpiryTime );\r
413         listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );\r
414         \r
415         if( xNextExpiryTime <= xTimeNow )\r
416         {\r
417                 /* Has the expiry time elapsed between the command to start/reset a\r
418                 timer was issued, and the time the command was processed? */\r
419                 if( ( ( portTickType ) ( xTimeNow - xCommandTime ) ) >= pxTimer->xTimerPeriodInTicks )\r
420                 {\r
421                         /* The time between a command being issued and the command being\r
422                         processed actually exceeds the timers period.  */\r
423                         xProcessTimerNow = pdTRUE;\r
424                 }\r
425                 else\r
426                 {\r
427                         vListInsert( pxOverflowTimerList, &( pxTimer->xTimerListItem ) );\r
428                 }\r
429         }\r
430         else\r
431         {\r
432                 if( ( xTimeNow < xCommandTime ) && ( xNextExpiryTime >= xCommandTime ) )\r
433                 {\r
434                         /* If, since the command was issued, the tick count has overflowed\r
435                         but the expiry time has not, then the timer must have already passed\r
436                         its expiry time and should be processed immediately. */\r
437                         xProcessTimerNow = pdTRUE;\r
438                 }\r
439                 else\r
440                 {\r
441                         vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );\r
442                 }\r
443         }\r
444 \r
445         return xProcessTimerNow;\r
446 }\r
447 /*-----------------------------------------------------------*/\r
448 \r
449 static void     prvProcessReceivedCommands( void )\r
450 {\r
451 xTIMER_MESSAGE xMessage;\r
452 xTIMER *pxTimer;\r
453 portBASE_TYPE xTimerListsWereSwitched, xResult;\r
454 portTickType xTimeNow;\r
455 \r
456         /* In this case the xTimerListsWereSwitched parameter is not used, but it\r
457         must be present in the function call. */\r
458         xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );\r
459 \r
460         while( xQueueReceive( xTimerQueue, &xMessage, tmrNO_DELAY ) != pdFAIL )\r
461         {\r
462                 pxTimer = xMessage.pxTimer;\r
463 \r
464                 /* Is the timer already in a list of active timers?  When the command\r
465                 is trmCOMMAND_PROCESS_TIMER_OVERFLOW, the timer will be NULL as the\r
466                 command is to the task rather than to an individual timer. */\r
467                 if( pxTimer != NULL )\r
468                 {\r
469                         if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )\r
470                         {\r
471                                 /* The timer is in a list, remove it. */\r
472                                 vListRemove( &( pxTimer->xTimerListItem ) );\r
473                         }\r
474                 }\r
475 \r
476                 switch( xMessage.xMessageID )\r
477                 {\r
478                         case tmrCOMMAND_START : \r
479                                 /* Start or restart a timer. */\r
480                                 if( prvInsertTimerInActiveList( pxTimer,  xMessage.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow, xMessage.xMessageValue ) == pdTRUE )\r
481                                 {\r
482                                         /* The timer expired before it was added to the active timer\r
483                                         list.  Process it now. */\r
484                                         pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );\r
485 \r
486                                         if( pxTimer->uxAutoReload == pdTRUE )\r
487                                         {\r
488                                                 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START, xMessage.xMessageValue + pxTimer->xTimerPeriodInTicks, NULL, tmrNO_DELAY );\r
489                                                 configASSERT( xResult );\r
490                                                 ( void ) xResult;\r
491                                         }\r
492                                 }\r
493                                 break;\r
494 \r
495                         case tmrCOMMAND_STOP :  \r
496                                 /* The timer has already been removed from the active list.\r
497                                 There is nothing to do here. */\r
498                                 break;\r
499 \r
500                         case tmrCOMMAND_CHANGE_PERIOD :\r
501                                 pxTimer->xTimerPeriodInTicks = xMessage.xMessageValue;\r
502                                 configASSERT( ( pxTimer->xTimerPeriodInTicks > 0 ) );\r
503                                 prvInsertTimerInActiveList( pxTimer, ( xTimeNow + pxTimer->xTimerPeriodInTicks ), xTimeNow, xTimeNow );\r
504                                 break;\r
505 \r
506                         case tmrCOMMAND_DELETE :\r
507                                 /* The timer has already been removed from the active list,\r
508                                 just free up the memory. */\r
509                                 vPortFree( pxTimer );\r
510                                 break;\r
511 \r
512                         default :                       \r
513                                 /* Don't expect to get here. */\r
514                                 break;\r
515                 }\r
516         }\r
517 }\r
518 /*-----------------------------------------------------------*/\r
519 \r
520 static void prvSwitchTimerLists( portTickType xLastTime )\r
521 {\r
522 portTickType xNextExpireTime;\r
523 xList *pxTemp;\r
524 xTIMER *pxTimer;\r
525 portBASE_TYPE xResult;\r
526 \r
527         /* Remove compiler warnings if configASSERT() is not defined. */\r
528         ( void ) xLastTime;\r
529         \r
530         /* The tick count has overflowed.  The timer lists must be switched.\r
531         If there are any timers still referenced from the current timer list\r
532         then they must have expired and should be processed before the lists\r
533         are switched. */\r
534         while( listLIST_IS_EMPTY( pxCurrentTimerList ) == pdFALSE )\r
535         {\r
536                 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );\r
537                 configASSERT( ( xNextExpireTime >= xLastTime ) );\r
538 \r
539                 /* Remove the timer from the list. */\r
540                 pxTimer = ( xTIMER * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );\r
541                 vListRemove( &( pxTimer->xTimerListItem ) );\r
542 \r
543                 /* Execute its callback, then send a command to restart the timer if\r
544                 it is an auto-reload timer.  It cannot be restarted here as the lists\r
545                 have not yet been switched. */\r
546                 pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );\r
547                 if( pxTimer->uxAutoReload == pdTRUE )\r
548                 {\r
549                         xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START, xNextExpireTime, NULL, tmrNO_DELAY );\r
550                         configASSERT( xResult );\r
551                         ( void ) xResult;\r
552                 }\r
553         }\r
554 \r
555         pxTemp = pxCurrentTimerList;\r
556         pxCurrentTimerList = pxOverflowTimerList;\r
557         pxOverflowTimerList = pxTemp;\r
558 }\r
559 /*-----------------------------------------------------------*/\r
560 \r
561 static void prvCheckForValidListAndQueue( void )\r
562 {\r
563         /* Check that the list from which active timers are referenced, and the\r
564         queue used to communicate with the timer service, have been\r
565         initialised. */\r
566         taskENTER_CRITICAL();\r
567         {\r
568                 if( xTimerQueue == NULL )\r
569                 {\r
570                         vListInitialise( &xActiveTimerList1 );\r
571                         vListInitialise( &xActiveTimerList2 );\r
572                         pxCurrentTimerList = &xActiveTimerList1;\r
573                         pxOverflowTimerList = &xActiveTimerList2;\r
574                         xTimerQueue = xQueueCreate( configTIMER_QUEUE_LENGTH, sizeof( xTIMER_MESSAGE ) );\r
575                 }\r
576         }\r
577         taskEXIT_CRITICAL();\r
578 }\r
579 /*-----------------------------------------------------------*/\r
580 \r
581 portBASE_TYPE xTimerIsTimerActive( xTimerHandle xTimer )\r
582 {\r
583 portBASE_TYPE xTimerIsInActiveList;\r
584 xTIMER *pxTimer = ( xTIMER * ) xTimer;\r
585 \r
586         /* Is the timer in the list of active timers? */\r
587         taskENTER_CRITICAL();\r
588         {\r
589                 /* Checking to see if it is in the NULL list in effect checks to see if\r
590                 it is referenced from either the current or the overflow timer lists in\r
591                 one go, but the logic has to be reversed, hence the '!'. */\r
592                 xTimerIsInActiveList = !( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) );\r
593         }\r
594         taskEXIT_CRITICAL();\r
595 \r
596         return xTimerIsInActiveList;\r
597 }\r
598 /*-----------------------------------------------------------*/\r
599 \r
600 void *pvTimerGetTimerID( xTimerHandle xTimer )\r
601 {\r
602 xTIMER *pxTimer = ( xTIMER * ) xTimer;\r
603 \r
604         return pxTimer->pvTimerID;\r
605 }\r
606 /*-----------------------------------------------------------*/\r
607 \r