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