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