]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/timers.c
0414604f1fa9c3a8b20e8101872f6b3f049cedcd
[freertos] / FreeRTOS / Source / timers.c
1 /*\r
2     FreeRTOS V7.5.2 - 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         }\r
237         else\r
238         {\r
239                 pxNewTimer = ( xTIMER * ) pvPortMalloc( sizeof( xTIMER ) );\r
240                 if( pxNewTimer != NULL )\r
241                 {\r
242                         /* Ensure the infrastructure used by the timer service task has been\r
243                         created/initialised. */\r
244                         prvCheckForValidListAndQueue();\r
245 \r
246                         /* Initialise the timer structure members using the function parameters. */\r
247                         pxNewTimer->pcTimerName = pcTimerName;\r
248                         pxNewTimer->xTimerPeriodInTicks = xTimerPeriodInTicks;\r
249                         pxNewTimer->uxAutoReload = uxAutoReload;\r
250                         pxNewTimer->pvTimerID = pvTimerID;\r
251                         pxNewTimer->pxCallbackFunction = pxCallbackFunction;\r
252                         vListInitialiseItem( &( pxNewTimer->xTimerListItem ) );\r
253 \r
254                         traceTIMER_CREATE( pxNewTimer );\r
255                 }\r
256                 else\r
257                 {\r
258                         traceTIMER_CREATE_FAILED();\r
259                 }\r
260         }\r
261 \r
262         /* 0 is not a valid value for xTimerPeriodInTicks. */\r
263         configASSERT( ( xTimerPeriodInTicks > 0 ) );\r
264 \r
265         return ( xTimerHandle ) pxNewTimer;\r
266 }\r
267 /*-----------------------------------------------------------*/\r
268 \r
269 portBASE_TYPE xTimerGenericCommand( xTimerHandle xTimer, portBASE_TYPE xCommandID, portTickType xOptionalValue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portTickType xBlockTime )\r
270 {\r
271 portBASE_TYPE xReturn = pdFAIL;\r
272 xTIMER_MESSAGE xMessage;\r
273 \r
274         /* Send a message to the timer service task to perform a particular action\r
275         on a particular timer definition. */\r
276         if( xTimerQueue != NULL )\r
277         {\r
278                 /* Send a command to the timer service task to start the xTimer timer. */\r
279                 xMessage.xMessageID = xCommandID;\r
280                 xMessage.xMessageValue = xOptionalValue;\r
281                 xMessage.pxTimer = ( xTIMER * ) xTimer;\r
282 \r
283                 if( pxHigherPriorityTaskWoken == NULL )\r
284                 {\r
285                         if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )\r
286                         {\r
287                                 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xBlockTime );\r
288                         }\r
289                         else\r
290                         {\r
291                                 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, tmrNO_DELAY );\r
292                         }\r
293                 }\r
294                 else\r
295                 {\r
296                         xReturn = xQueueSendToBackFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );\r
297                 }\r
298 \r
299                 traceTIMER_COMMAND_SEND( xTimer, xCommandID, xOptionalValue, xReturn );\r
300         }\r
301 \r
302         return xReturn;\r
303 }\r
304 /*-----------------------------------------------------------*/\r
305 \r
306 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )\r
307 \r
308         xTaskHandle xTimerGetTimerDaemonTaskHandle( void )\r
309         {\r
310                 /* If xTimerGetTimerDaemonTaskHandle() is called before the scheduler has been\r
311                 started, then xTimerTaskHandle will be NULL. */\r
312                 configASSERT( ( xTimerTaskHandle != NULL ) );\r
313                 return xTimerTaskHandle;\r
314         }\r
315 \r
316 #endif\r
317 /*-----------------------------------------------------------*/\r
318 \r
319 static void prvProcessExpiredTimer( portTickType xNextExpireTime, portTickType xTimeNow )\r
320 {\r
321 xTIMER *pxTimer;\r
322 portBASE_TYPE xResult;\r
323 \r
324         /* Remove the timer from the list of active timers.  A check has already\r
325         been performed to ensure the list is not empty. */\r
326         pxTimer = ( xTIMER * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );\r
327         ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );\r
328         traceTIMER_EXPIRED( pxTimer );\r
329 \r
330         /* If the timer is an auto reload timer then calculate the next\r
331         expiry time and re-insert the timer in the list of active timers. */\r
332         if( pxTimer->uxAutoReload == ( unsigned portBASE_TYPE ) pdTRUE )\r
333         {\r
334                 /* This is the only time a timer is inserted into a list using\r
335                 a time relative to anything other than the current time.  It\r
336                 will therefore be inserted into the correct list relative to\r
337                 the time this task thinks it is now, even if a command to\r
338                 switch lists due to a tick count overflow is already waiting in\r
339                 the timer queue. */\r
340                 if( prvInsertTimerInActiveList( pxTimer, ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ), xTimeNow, xNextExpireTime ) == pdTRUE )\r
341                 {\r
342                         /* The timer expired before it was added to the active timer\r
343                         list.  Reload it now.  */\r
344                         xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START, xNextExpireTime, NULL, tmrNO_DELAY );\r
345                         configASSERT( xResult );\r
346                         ( void ) xResult;\r
347                 }\r
348         }\r
349 \r
350         /* Call the timer callback. */\r
351         pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );\r
352 }\r
353 /*-----------------------------------------------------------*/\r
354 \r
355 static void prvTimerTask( void *pvParameters )\r
356 {\r
357 portTickType xNextExpireTime;\r
358 portBASE_TYPE xListWasEmpty;\r
359 \r
360         /* Just to avoid compiler warnings. */\r
361         ( void ) pvParameters;\r
362 \r
363         for( ;; )\r
364         {\r
365                 /* Query the timers list to see if it contains any timers, and if so,\r
366                 obtain the time at which the next timer will expire. */\r
367                 xNextExpireTime = prvGetNextExpireTime( &xListWasEmpty );\r
368 \r
369                 /* If a timer has expired, process it.  Otherwise, block this task\r
370                 until either a timer does expire, or a command is received. */\r
371                 prvProcessTimerOrBlockTask( xNextExpireTime, xListWasEmpty );\r
372 \r
373                 /* Empty the command queue. */\r
374                 prvProcessReceivedCommands();\r
375         }\r
376 }\r
377 /*-----------------------------------------------------------*/\r
378 \r
379 static void prvProcessTimerOrBlockTask( portTickType xNextExpireTime, portBASE_TYPE xListWasEmpty )\r
380 {\r
381 portTickType xTimeNow;\r
382 portBASE_TYPE xTimerListsWereSwitched;\r
383 \r
384         vTaskSuspendAll();\r
385         {\r
386                 /* Obtain the time now to make an assessment as to whether the timer\r
387                 has expired or not.  If obtaining the time causes the lists to switch\r
388                 then don't process this timer as any timers that remained in the list\r
389                 when the lists were switched will have been processed within the\r
390                 prvSampelTimeNow() function. */\r
391                 xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );\r
392                 if( xTimerListsWereSwitched == pdFALSE )\r
393                 {\r
394                         /* The tick count has not overflowed, has the timer expired? */\r
395                         if( ( xListWasEmpty == pdFALSE ) && ( xNextExpireTime <= xTimeNow ) )\r
396                         {\r
397                                 ( void ) xTaskResumeAll();\r
398                                 prvProcessExpiredTimer( xNextExpireTime, xTimeNow );\r
399                         }\r
400                         else\r
401                         {\r
402                                 /* The tick count has not overflowed, and the next expire\r
403                                 time has not been reached yet.  This task should therefore\r
404                                 block to wait for the next expire time or a command to be\r
405                                 received - whichever comes first.  The following line cannot\r
406                                 be reached unless xNextExpireTime > xTimeNow, except in the\r
407                                 case when the current timer list is empty. */\r
408                                 vQueueWaitForMessageRestricted( xTimerQueue, ( xNextExpireTime - xTimeNow ) );\r
409 \r
410                                 if( xTaskResumeAll() == pdFALSE )\r
411                                 {\r
412                                         /* Yield to wait for either a command to arrive, or the block time\r
413                                         to expire.  If a command arrived between the critical section being\r
414                                         exited and this yield then the yield will not cause the task\r
415                                         to block. */\r
416                                         portYIELD_WITHIN_API();\r
417                                 }\r
418                         }\r
419                 }\r
420                 else\r
421                 {\r
422                         ( void ) xTaskResumeAll();\r
423                 }\r
424         }\r
425 }\r
426 /*-----------------------------------------------------------*/\r
427 \r
428 static portTickType prvGetNextExpireTime( portBASE_TYPE *pxListWasEmpty )\r
429 {\r
430 portTickType xNextExpireTime;\r
431 \r
432         /* Timers are listed in expiry time order, with the head of the list\r
433         referencing the task that will expire first.  Obtain the time at which\r
434         the timer with the nearest expiry time will expire.  If there are no\r
435         active timers then just set the next expire time to 0.  That will cause\r
436         this task to unblock when the tick count overflows, at which point the\r
437         timer lists will be switched and the next expiry time can be\r
438         re-assessed.  */\r
439         *pxListWasEmpty = listLIST_IS_EMPTY( pxCurrentTimerList );\r
440         if( *pxListWasEmpty == pdFALSE )\r
441         {\r
442                 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );\r
443         }\r
444         else\r
445         {\r
446                 /* Ensure the task unblocks when the tick count rolls over. */\r
447                 xNextExpireTime = ( portTickType ) 0U;\r
448         }\r
449 \r
450         return xNextExpireTime;\r
451 }\r
452 /*-----------------------------------------------------------*/\r
453 \r
454 static portTickType prvSampleTimeNow( portBASE_TYPE *pxTimerListsWereSwitched )\r
455 {\r
456 portTickType xTimeNow;\r
457 PRIVILEGED_DATA static portTickType xLastTime = ( portTickType ) 0U; /*lint !e956 Variable is only accessible to one task. */\r
458 \r
459         xTimeNow = xTaskGetTickCount();\r
460 \r
461         if( xTimeNow < xLastTime )\r
462         {\r
463                 prvSwitchTimerLists( xLastTime );\r
464                 *pxTimerListsWereSwitched = pdTRUE;\r
465         }\r
466         else\r
467         {\r
468                 *pxTimerListsWereSwitched = pdFALSE;\r
469         }\r
470 \r
471         xLastTime = xTimeNow;\r
472 \r
473         return xTimeNow;\r
474 }\r
475 /*-----------------------------------------------------------*/\r
476 \r
477 static portBASE_TYPE prvInsertTimerInActiveList( xTIMER *pxTimer, portTickType xNextExpiryTime, portTickType xTimeNow, portTickType xCommandTime )\r
478 {\r
479 portBASE_TYPE xProcessTimerNow = pdFALSE;\r
480 \r
481         listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xNextExpiryTime );\r
482         listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );\r
483 \r
484         if( xNextExpiryTime <= xTimeNow )\r
485         {\r
486                 /* Has the expiry time elapsed between the command to start/reset a\r
487                 timer was issued, and the time the command was processed? */\r
488                 if( ( xTimeNow - xCommandTime ) >= pxTimer->xTimerPeriodInTicks )\r
489                 {\r
490                         /* The time between a command being issued and the command being\r
491                         processed actually exceeds the timers period.  */\r
492                         xProcessTimerNow = pdTRUE;\r
493                 }\r
494                 else\r
495                 {\r
496                         vListInsert( pxOverflowTimerList, &( pxTimer->xTimerListItem ) );\r
497                 }\r
498         }\r
499         else\r
500         {\r
501                 if( ( xTimeNow < xCommandTime ) && ( xNextExpiryTime >= xCommandTime ) )\r
502                 {\r
503                         /* If, since the command was issued, the tick count has overflowed\r
504                         but the expiry time has not, then the timer must have already passed\r
505                         its expiry time and should be processed immediately. */\r
506                         xProcessTimerNow = pdTRUE;\r
507                 }\r
508                 else\r
509                 {\r
510                         vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );\r
511                 }\r
512         }\r
513 \r
514         return xProcessTimerNow;\r
515 }\r
516 /*-----------------------------------------------------------*/\r
517 \r
518 static void     prvProcessReceivedCommands( void )\r
519 {\r
520 xTIMER_MESSAGE xMessage;\r
521 xTIMER *pxTimer;\r
522 portBASE_TYPE xTimerListsWereSwitched, xResult;\r
523 portTickType xTimeNow;\r
524 \r
525         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
526         {\r
527                 pxTimer = xMessage.pxTimer;\r
528 \r
529                 if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )\r
530                 {\r
531                         /* The timer is in a list, remove it. */\r
532                         ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );\r
533                 }\r
534 \r
535                 traceTIMER_COMMAND_RECEIVED( pxTimer, xMessage.xMessageID, xMessage.xMessageValue );\r
536 \r
537                 /* In this case the xTimerListsWereSwitched parameter is not used, but \r
538                 it must be present in the function call.  prvSampleTimeNow() must be \r
539                 called after the message is received from xTimerQueue so there is no \r
540                 possibility of a higher priority task adding a message to the message\r
541                 queue with a time that is ahead of the timer daemon task (because it\r
542                 pre-empted the timer daemon task after the xTimeNow value was set). */\r
543                 xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );\r
544 \r
545                 switch( xMessage.xMessageID )\r
546                 {\r
547                         case tmrCOMMAND_START :\r
548                                 /* Start or restart a timer. */\r
549                                 if( prvInsertTimerInActiveList( pxTimer,  xMessage.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow, xMessage.xMessageValue ) == pdTRUE )\r
550                                 {\r
551                                         /* The timer expired before it was added to the active timer\r
552                                         list.  Process it now. */\r
553                                         pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );\r
554 \r
555                                         if( pxTimer->uxAutoReload == ( unsigned portBASE_TYPE ) pdTRUE )\r
556                                         {\r
557                                                 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START, xMessage.xMessageValue + pxTimer->xTimerPeriodInTicks, NULL, tmrNO_DELAY );\r
558                                                 configASSERT( xResult );\r
559                                                 ( void ) xResult;\r
560                                         }\r
561                                 }\r
562                                 break;\r
563 \r
564                         case tmrCOMMAND_STOP :\r
565                                 /* The timer has already been removed from the active list.\r
566                                 There is nothing to do here. */\r
567                                 break;\r
568 \r
569                         case tmrCOMMAND_CHANGE_PERIOD :\r
570                                 pxTimer->xTimerPeriodInTicks = xMessage.xMessageValue;\r
571                                 configASSERT( ( pxTimer->xTimerPeriodInTicks > 0 ) );\r
572                                 ( void ) prvInsertTimerInActiveList( pxTimer, ( xTimeNow + pxTimer->xTimerPeriodInTicks ), xTimeNow, xTimeNow );\r
573                                 break;\r
574 \r
575                         case tmrCOMMAND_DELETE :\r
576                                 /* The timer has already been removed from the active list,\r
577                                 just free up the memory. */\r
578                                 vPortFree( pxTimer );\r
579                                 break;\r
580 \r
581                         default :\r
582                                 /* Don't expect to get here. */\r
583                                 break;\r
584                 }\r
585         }\r
586 }\r
587 /*-----------------------------------------------------------*/\r
588 \r
589 static void prvSwitchTimerLists( portTickType xLastTime )\r
590 {\r
591 portTickType xNextExpireTime, xReloadTime;\r
592 xList *pxTemp;\r
593 xTIMER *pxTimer;\r
594 portBASE_TYPE xResult;\r
595 \r
596         /* Remove compiler warnings if configASSERT() is not defined. */\r
597         ( void ) xLastTime;\r
598 \r
599         /* The tick count has overflowed.  The timer lists must be switched.\r
600         If there are any timers still referenced from the current timer list\r
601         then they must have expired and should be processed before the lists\r
602         are switched. */\r
603         while( listLIST_IS_EMPTY( pxCurrentTimerList ) == pdFALSE )\r
604         {\r
605                 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );\r
606 \r
607                 /* Remove the timer from the list. */\r
608                 pxTimer = ( xTIMER * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );\r
609                 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );\r
610 \r
611                 /* Execute its callback, then send a command to restart the timer if\r
612                 it is an auto-reload timer.  It cannot be restarted here as the lists\r
613                 have not yet been switched. */\r
614                 pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );\r
615 \r
616                 if( pxTimer->uxAutoReload == ( unsigned portBASE_TYPE ) pdTRUE )\r
617                 {\r
618                         /* Calculate the reload value, and if the reload value results in\r
619                         the timer going into the same timer list then it has already expired\r
620                         and the timer should be re-inserted into the current list so it is\r
621                         processed again within this loop.  Otherwise a command should be sent\r
622                         to restart the timer to ensure it is only inserted into a list after\r
623                         the lists have been swapped. */\r
624                         xReloadTime = ( xNextExpireTime + pxTimer->xTimerPeriodInTicks );\r
625                         if( xReloadTime > xNextExpireTime )\r
626                         {\r
627                                 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xReloadTime );\r
628                                 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );\r
629                                 vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );\r
630                         }\r
631                         else\r
632                         {\r
633                                 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START, xNextExpireTime, NULL, tmrNO_DELAY );\r
634                                 configASSERT( xResult );\r
635                                 ( void ) xResult;\r
636                         }\r
637                 }\r
638         }\r
639 \r
640         pxTemp = pxCurrentTimerList;\r
641         pxCurrentTimerList = pxOverflowTimerList;\r
642         pxOverflowTimerList = pxTemp;\r
643 }\r
644 /*-----------------------------------------------------------*/\r
645 \r
646 static void prvCheckForValidListAndQueue( void )\r
647 {\r
648         /* Check that the list from which active timers are referenced, and the\r
649         queue used to communicate with the timer service, have been\r
650         initialised. */\r
651         taskENTER_CRITICAL();\r
652         {\r
653                 if( xTimerQueue == NULL )\r
654                 {\r
655                         vListInitialise( &xActiveTimerList1 );\r
656                         vListInitialise( &xActiveTimerList2 );\r
657                         pxCurrentTimerList = &xActiveTimerList1;\r
658                         pxOverflowTimerList = &xActiveTimerList2;\r
659                         xTimerQueue = xQueueCreate( ( unsigned portBASE_TYPE ) configTIMER_QUEUE_LENGTH, sizeof( xTIMER_MESSAGE ) );\r
660                 }\r
661         }\r
662         taskEXIT_CRITICAL();\r
663 }\r
664 /*-----------------------------------------------------------*/\r
665 \r
666 portBASE_TYPE xTimerIsTimerActive( xTimerHandle xTimer )\r
667 {\r
668 portBASE_TYPE xTimerIsInActiveList;\r
669 xTIMER *pxTimer = ( xTIMER * ) xTimer;\r
670 \r
671         /* Is the timer in the list of active timers? */\r
672         taskENTER_CRITICAL();\r
673         {\r
674                 /* Checking to see if it is in the NULL list in effect checks to see if\r
675                 it is referenced from either the current or the overflow timer lists in\r
676                 one go, but the logic has to be reversed, hence the '!'. */\r
677                 xTimerIsInActiveList = !( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) );\r
678         }\r
679         taskEXIT_CRITICAL();\r
680 \r
681         return xTimerIsInActiveList;\r
682 }\r
683 /*-----------------------------------------------------------*/\r
684 \r
685 void *pvTimerGetTimerID( xTimerHandle xTimer )\r
686 {\r
687 xTIMER *pxTimer = ( xTIMER * ) xTimer;\r
688 \r
689         return pxTimer->pvTimerID;\r
690 }\r
691 /*-----------------------------------------------------------*/\r
692 \r
693 /* This entire source file will be skipped if the application is not configured\r
694 to include software timer functionality.  If you want to include software timer\r
695 functionality then ensure configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */\r
696 #endif /* configUSE_TIMERS == 1 */\r
697 \r
698 \r
699 \r