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