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