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