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