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