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