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