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