]> git.sur5r.net Git - freertos/blob - Source/timers.c
Add a #if ( configUSE_TIMERS ) conditional compilation constant in to allow the softw...
[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         return xReturn;\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 xTimerHandle xTimerCreate( const signed char *pcTimerName, portTickType xTimerPeriodInTicks, unsigned portBASE_TYPE uxAutoReload, void *pvTimerID, tmrTIMER_CALLBACK pxCallbackFunction )\r
188 {\r
189 xTIMER *pxNewTimer;\r
190 \r
191         /* Allocate the timer structure. */\r
192         if( xTimerPeriodInTicks == ( portTickType ) 0U )\r
193         {\r
194                 pxNewTimer = NULL;\r
195                 configASSERT( ( xTimerPeriodInTicks > 0 ) );\r
196         }\r
197         else\r
198         {\r
199                 pxNewTimer = ( xTIMER * ) pvPortMalloc( sizeof( xTIMER ) );\r
200                 if( pxNewTimer != NULL )\r
201                 {\r
202                         /* Ensure the infrastructure used by the timer service task has been\r
203                         created/initialised. */\r
204                         prvCheckForValidListAndQueue();\r
205         \r
206                         configASSERT( ( xTimerPeriodInTicks > 0 ) );\r
207         \r
208                         /* Initialise the timer structure members using the function parameters. */\r
209                         pxNewTimer->pcTimerName = pcTimerName;\r
210                         pxNewTimer->xTimerPeriodInTicks = xTimerPeriodInTicks;\r
211                         pxNewTimer->uxAutoReload = uxAutoReload;\r
212                         pxNewTimer->pvTimerID = pvTimerID;\r
213                         pxNewTimer->pxCallbackFunction = pxCallbackFunction;\r
214                         vListInitialiseItem( &( pxNewTimer->xTimerListItem ) );\r
215                 }\r
216         }\r
217         \r
218         return ( xTimerHandle ) pxNewTimer;\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 portBASE_TYPE xTimerGenericCommand( xTimerHandle xTimer, portBASE_TYPE xCommandID, portTickType xOptionalValue, portBASE_TYPE *pxHigherPriorityTaskWoken, portTickType xBlockTime )\r
223 {\r
224 portBASE_TYPE xReturn = pdFAIL;\r
225 xTIMER_MESSAGE xMessage;\r
226 \r
227         /* Send a message to the timer service task to perform a particular action\r
228         on a particular timer definition. */\r
229         if( xTimerQueue != NULL )\r
230         {\r
231                 /* Send a command to the timer service task to start the xTimer timer. */\r
232                 xMessage.xMessageID = xCommandID;\r
233                 xMessage.xMessageValue = xOptionalValue;\r
234                 xMessage.pxTimer = ( xTIMER * ) xTimer;\r
235 \r
236                 if( pxHigherPriorityTaskWoken == NULL )\r
237                 {\r
238                         if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )\r
239                         {\r
240                                 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xBlockTime );\r
241                         }\r
242                         else\r
243                         {\r
244                                 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, tmrNO_DELAY );\r
245                         }\r
246                 }\r
247                 else\r
248                 {\r
249                         xReturn = xQueueSendToBackFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );\r
250                 }\r
251         }\r
252         \r
253         return xReturn;\r
254 }\r
255 /*-----------------------------------------------------------*/\r
256 \r
257 static void prvProcessExpiredTimer( portTickType xNextExpireTime, portTickType xTimeNow )\r
258 {\r
259 xTIMER *pxTimer;\r
260 portBASE_TYPE xResult;\r
261 \r
262         /* Remove the timer from the list of active timers.  A check has already\r
263         been performed to ensure the list is not empty. */\r
264         pxTimer = ( xTIMER * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );\r
265         vListRemove( &( pxTimer->xTimerListItem ) );\r
266 \r
267         /* If the timer is an auto reload timer then calculate the next\r
268         expiry time and re-insert the timer in the list of active timers. */\r
269         if( pxTimer->uxAutoReload == pdTRUE )\r
270         {\r
271                 /* This is the only time a timer is inserted into a list using\r
272                 a time relative to anything other than the current time.  It\r
273                 will therefore be inserted into the correct list relative to\r
274                 the time this task thinks it is now, even if a command to\r
275                 switch lists due to a tick count overflow is already waiting in\r
276                 the timer queue. */\r
277                 if( prvInsertTimerInActiveList( pxTimer, ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ), xTimeNow, xNextExpireTime ) == pdTRUE )\r
278                 {\r
279                         /* The timer expired before it was added to the active timer\r
280                         list.  Reload it now.  */\r
281                         xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START, xNextExpireTime, NULL, tmrNO_DELAY );\r
282                         configASSERT( xResult );\r
283                         ( void ) xResult;\r
284                 }\r
285         }\r
286 \r
287         /* Call the timer callback. */\r
288         pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );\r
289 }\r
290 /*-----------------------------------------------------------*/\r
291 \r
292 static void prvTimerTask( void *pvParameters )\r
293 {\r
294 portTickType xNextExpireTime;\r
295 portBASE_TYPE xListWasEmpty;\r
296 \r
297         /* Just to avoid compiler warnings. */\r
298         ( void ) pvParameters;\r
299 \r
300         for( ;; )\r
301         {\r
302                 /* Query the timers list to see if it contains any timers, and if so,\r
303                 obtain the time at which the next timer will expire. */\r
304                 xNextExpireTime = prvGetNextExpireTime( &xListWasEmpty );\r
305 \r
306                 /* If a timer has expired, process it.  Otherwise, block this task\r
307                 until either a timer does expire, or a command is received. */\r
308                 prvProcessTimerOrBlockTask( xNextExpireTime, xListWasEmpty );\r
309                 \r
310                 /* Empty the command queue. */\r
311                 prvProcessReceivedCommands();           \r
312         }\r
313 }\r
314 /*-----------------------------------------------------------*/\r
315 \r
316 static void prvProcessTimerOrBlockTask( portTickType xNextExpireTime, portBASE_TYPE xListWasEmpty )\r
317 {\r
318 portTickType xTimeNow;\r
319 portBASE_TYPE xTimerListsWereSwitched;\r
320 \r
321         vTaskSuspendAll();\r
322         {\r
323                 /* Obtain the time now to make an assessment as to whether the timer\r
324                 has expired or not.  If obtaining the time causes the lists to switch\r
325                 then don't process this timer as any timers that remained in the list\r
326                 when the lists were switched will have been processed within the\r
327                 prvSampelTimeNow() function. */\r
328                 xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );\r
329                 if( xTimerListsWereSwitched == pdFALSE )\r
330                 {\r
331                         /* The tick count has not overflowed, has the timer expired? */\r
332                         if( ( xListWasEmpty == pdFALSE ) && ( xNextExpireTime <= xTimeNow ) )\r
333                         {\r
334                                 xTaskResumeAll();\r
335                                 prvProcessExpiredTimer( xNextExpireTime, xTimeNow );\r
336                         }\r
337                         else\r
338                         {\r
339                                 /* The tick count has not overflowed, and the next expire\r
340                                 time has not been reached yet.  This task should therefore\r
341                                 block to wait for the next expire time or a command to be\r
342                                 received - whichever comes first.  The following line cannot\r
343                                 be reached unless xNextExpireTime > xTimeNow, except in the\r
344                                 case when the current timer list is empty. */\r
345                                 vQueueWaitForMessageRestricted( xTimerQueue, ( xNextExpireTime - xTimeNow ) );\r
346 \r
347                                 if( xTaskResumeAll() == pdFALSE )\r
348                                 {\r
349                                         /* Yield to wait for either a command to arrive, or the block time\r
350                                         to expire.  If a command arrived between the critical section being\r
351                                         exited and this yield then the yield will not cause the task\r
352                                         to block. */\r
353                                         portYIELD_WITHIN_API();\r
354                                 }\r
355                         }\r
356                 }\r
357                 else\r
358                 {\r
359                         xTaskResumeAll();\r
360                 }\r
361         }\r
362 }\r
363 /*-----------------------------------------------------------*/\r
364 \r
365 static portTickType prvGetNextExpireTime( portBASE_TYPE *pxListWasEmpty )\r
366 {\r
367 portTickType xNextExpireTime;\r
368 \r
369         /* Timers are listed in expiry time order, with the head of the list\r
370         referencing the task that will expire first.  Obtain the time at which\r
371         the timer with the nearest expiry time will expire.  If there are no\r
372         active timers then just set the next expire time to 0.  That will cause\r
373         this task to unblock when the tick count overflows, at which point the\r
374         timer lists will be switched and the next expiry time can be\r
375         re-assessed.  */\r
376         *pxListWasEmpty = listLIST_IS_EMPTY( pxCurrentTimerList );\r
377         if( *pxListWasEmpty == pdFALSE )\r
378         {\r
379                 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );\r
380         }\r
381         else\r
382         {\r
383                 /* Ensure the task unblocks when the tick count rolls over. */\r
384                 xNextExpireTime = ( portTickType ) 0U;\r
385         }\r
386 \r
387         return xNextExpireTime;\r
388 }\r
389 /*-----------------------------------------------------------*/\r
390 \r
391 static portTickType prvSampleTimeNow( portBASE_TYPE *pxTimerListsWereSwitched )\r
392 {\r
393 portTickType xTimeNow;\r
394 static portTickType xLastTime = ( portTickType ) 0U;\r
395 \r
396         xTimeNow = xTaskGetTickCount();\r
397         \r
398         if( xTimeNow < xLastTime )\r
399         {\r
400                 prvSwitchTimerLists( xLastTime );\r
401                 *pxTimerListsWereSwitched = pdTRUE;\r
402         }\r
403         else\r
404         {\r
405                 *pxTimerListsWereSwitched = pdFALSE;\r
406         }\r
407         \r
408         xLastTime = xTimeNow;\r
409         \r
410         return xTimeNow;\r
411 }\r
412 /*-----------------------------------------------------------*/\r
413 \r
414 static portBASE_TYPE prvInsertTimerInActiveList( xTIMER *pxTimer, portTickType xNextExpiryTime, portTickType xTimeNow, portTickType xCommandTime )\r
415 {\r
416 portBASE_TYPE xProcessTimerNow = pdFALSE;\r
417 \r
418         listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xNextExpiryTime );\r
419         listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );\r
420         \r
421         if( xNextExpiryTime <= xTimeNow )\r
422         {\r
423                 /* Has the expiry time elapsed between the command to start/reset a\r
424                 timer was issued, and the time the command was processed? */\r
425                 if( ( ( portTickType ) ( xTimeNow - xCommandTime ) ) >= pxTimer->xTimerPeriodInTicks )\r
426                 {\r
427                         /* The time between a command being issued and the command being\r
428                         processed actually exceeds the timers period.  */\r
429                         xProcessTimerNow = pdTRUE;\r
430                 }\r
431                 else\r
432                 {\r
433                         vListInsert( pxOverflowTimerList, &( pxTimer->xTimerListItem ) );\r
434                 }\r
435         }\r
436         else\r
437         {\r
438                 if( ( xTimeNow < xCommandTime ) && ( xNextExpiryTime >= xCommandTime ) )\r
439                 {\r
440                         /* If, since the command was issued, the tick count has overflowed\r
441                         but the expiry time has not, then the timer must have already passed\r
442                         its expiry time and should be processed immediately. */\r
443                         xProcessTimerNow = pdTRUE;\r
444                 }\r
445                 else\r
446                 {\r
447                         vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );\r
448                 }\r
449         }\r
450 \r
451         return xProcessTimerNow;\r
452 }\r
453 /*-----------------------------------------------------------*/\r
454 \r
455 static void     prvProcessReceivedCommands( void )\r
456 {\r
457 xTIMER_MESSAGE xMessage;\r
458 xTIMER *pxTimer;\r
459 portBASE_TYPE xTimerListsWereSwitched, xResult;\r
460 portTickType xTimeNow;\r
461 \r
462         /* In this case the xTimerListsWereSwitched parameter is not used, but it\r
463         must be present in the function call. */\r
464         xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );\r
465 \r
466         while( xQueueReceive( xTimerQueue, &xMessage, tmrNO_DELAY ) != pdFAIL )\r
467         {\r
468                 pxTimer = xMessage.pxTimer;\r
469 \r
470                 /* Is the timer already in a list of active timers?  When the command\r
471                 is trmCOMMAND_PROCESS_TIMER_OVERFLOW, the timer will be NULL as the\r
472                 command is to the task rather than to an individual timer. */\r
473                 if( pxTimer != NULL )\r
474                 {\r
475                         if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )\r
476                         {\r
477                                 /* The timer is in a list, remove it. */\r
478                                 vListRemove( &( pxTimer->xTimerListItem ) );\r
479                         }\r
480                 }\r
481 \r
482                 switch( xMessage.xMessageID )\r
483                 {\r
484                         case tmrCOMMAND_START : \r
485                                 /* Start or restart a timer. */\r
486                                 if( prvInsertTimerInActiveList( pxTimer,  xMessage.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow, xMessage.xMessageValue ) == pdTRUE )\r
487                                 {\r
488                                         /* The timer expired before it was added to the active timer\r
489                                         list.  Process it now. */\r
490                                         pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );\r
491 \r
492                                         if( pxTimer->uxAutoReload == pdTRUE )\r
493                                         {\r
494                                                 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START, xMessage.xMessageValue + pxTimer->xTimerPeriodInTicks, NULL, tmrNO_DELAY );\r
495                                                 configASSERT( xResult );\r
496                                                 ( void ) xResult;\r
497                                         }\r
498                                 }\r
499                                 break;\r
500 \r
501                         case tmrCOMMAND_STOP :  \r
502                                 /* The timer has already been removed from the active list.\r
503                                 There is nothing to do here. */\r
504                                 break;\r
505 \r
506                         case tmrCOMMAND_CHANGE_PERIOD :\r
507                                 pxTimer->xTimerPeriodInTicks = xMessage.xMessageValue;\r
508                                 configASSERT( ( pxTimer->xTimerPeriodInTicks > 0 ) );\r
509                                 prvInsertTimerInActiveList( pxTimer, ( xTimeNow + pxTimer->xTimerPeriodInTicks ), xTimeNow, xTimeNow );\r
510                                 break;\r
511 \r
512                         case tmrCOMMAND_DELETE :\r
513                                 /* The timer has already been removed from the active list,\r
514                                 just free up the memory. */\r
515                                 vPortFree( pxTimer );\r
516                                 break;\r
517 \r
518                         default :                       \r
519                                 /* Don't expect to get here. */\r
520                                 break;\r
521                 }\r
522         }\r
523 }\r
524 /*-----------------------------------------------------------*/\r
525 \r
526 static void prvSwitchTimerLists( portTickType xLastTime )\r
527 {\r
528 portTickType xNextExpireTime, xReloadTime;\r
529 xList *pxTemp;\r
530 xTIMER *pxTimer;\r
531 portBASE_TYPE xResult;\r
532 \r
533         /* Remove compiler warnings if configASSERT() is not defined. */\r
534         ( void ) xLastTime;\r
535         \r
536         /* The tick count has overflowed.  The timer lists must be switched.\r
537         If there are any timers still referenced from the current timer list\r
538         then they must have expired and should be processed before the lists\r
539         are switched. */\r
540         while( listLIST_IS_EMPTY( pxCurrentTimerList ) == pdFALSE )\r
541         {\r
542                 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );\r
543 \r
544                 /* Remove the timer from the list. */\r
545                 pxTimer = ( xTIMER * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );\r
546                 vListRemove( &( pxTimer->xTimerListItem ) );\r
547 \r
548                 /* Execute its callback, then send a command to restart the timer if\r
549                 it is an auto-reload timer.  It cannot be restarted here as the lists\r
550                 have not yet been switched. */\r
551                 pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );\r
552 \r
553                 if( pxTimer->uxAutoReload == pdTRUE )\r
554                 {\r
555                         /* Calculate the reload value, and if the reload value results in\r
556                         the timer going into the same timer list then it has already expired\r
557                         and the timer should be re-inserted into the current list so it is\r
558                         processed again within this loop.  Otherwise a command should be sent\r
559                         to restart the timer to ensure it is only inserted into a list after\r
560                         the lists have been swapped. */\r
561                         xReloadTime = ( xNextExpireTime + pxTimer->xTimerPeriodInTicks );\r
562                         if( xReloadTime > xNextExpireTime )\r
563                         {\r
564                                 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xReloadTime );\r
565                                 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );\r
566                                 vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );\r
567                         }\r
568                         else\r
569                         {\r
570                                 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START, xNextExpireTime, NULL, tmrNO_DELAY );\r
571                                 configASSERT( xResult );\r
572                                 ( void ) xResult;\r
573                         }\r
574                 }\r
575         }\r
576 \r
577         pxTemp = pxCurrentTimerList;\r
578         pxCurrentTimerList = pxOverflowTimerList;\r
579         pxOverflowTimerList = pxTemp;\r
580 }\r
581 /*-----------------------------------------------------------*/\r
582 \r
583 static void prvCheckForValidListAndQueue( void )\r
584 {\r
585         /* Check that the list from which active timers are referenced, and the\r
586         queue used to communicate with the timer service, have been\r
587         initialised. */\r
588         taskENTER_CRITICAL();\r
589         {\r
590                 if( xTimerQueue == NULL )\r
591                 {\r
592                         vListInitialise( &xActiveTimerList1 );\r
593                         vListInitialise( &xActiveTimerList2 );\r
594                         pxCurrentTimerList = &xActiveTimerList1;\r
595                         pxOverflowTimerList = &xActiveTimerList2;\r
596                         xTimerQueue = xQueueCreate( configTIMER_QUEUE_LENGTH, sizeof( xTIMER_MESSAGE ) );\r
597                 }\r
598         }\r
599         taskEXIT_CRITICAL();\r
600 }\r
601 /*-----------------------------------------------------------*/\r
602 \r
603 portBASE_TYPE xTimerIsTimerActive( xTimerHandle xTimer )\r
604 {\r
605 portBASE_TYPE xTimerIsInActiveList;\r
606 xTIMER *pxTimer = ( xTIMER * ) xTimer;\r
607 \r
608         /* Is the timer in the list of active timers? */\r
609         taskENTER_CRITICAL();\r
610         {\r
611                 /* Checking to see if it is in the NULL list in effect checks to see if\r
612                 it is referenced from either the current or the overflow timer lists in\r
613                 one go, but the logic has to be reversed, hence the '!'. */\r
614                 xTimerIsInActiveList = !( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) );\r
615         }\r
616         taskEXIT_CRITICAL();\r
617 \r
618         return xTimerIsInActiveList;\r
619 }\r
620 /*-----------------------------------------------------------*/\r
621 \r
622 void *pvTimerGetTimerID( xTimerHandle xTimer )\r
623 {\r
624 xTIMER *pxTimer = ( xTIMER * ) xTimer;\r
625 \r
626         return pxTimer->pvTimerID;\r
627 }\r
628 /*-----------------------------------------------------------*/\r
629 \r
630 /* This entire source file will be skipped if the application is not configured\r
631 to include software timer functionality.  If you want to include software timer\r
632 functionality then ensure configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */\r
633 #endif /* configUSE_TIMERS == 1 */\r