]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/timers.c
Add the pcTimerGetTimerName() API function.
[freertos] / FreeRTOS / Source / timers.c
1 /*\r
2     FreeRTOS V8.0.0 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /* Standard includes. */\r
67 #include <stdlib.h>\r
68 \r
69 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
70 all the API functions to use the MPU wrappers.  That should only be done when\r
71 task.h is included from an application file. */\r
72 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
73 \r
74 #include "FreeRTOS.h"\r
75 #include "task.h"\r
76 #include "queue.h"\r
77 #include "timers.h"\r
78 \r
79 #if ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 0 )\r
80         #error configUSE_TIMERS must be set to 1 to make the xTimerPendFunctionCall() function available.\r
81 #endif\r
82 \r
83 /* Lint e961 and e750 are suppressed as a MISRA exception justified because the\r
84 MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined for the\r
85 header files above, but not in this file, in order to generate the correct\r
86 privileged Vs unprivileged linkage and placement. */\r
87 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750. */\r
88 \r
89 \r
90 /* This entire source file will be skipped if the application is not configured\r
91 to include software timer functionality.  This #if is closed at the very bottom\r
92 of this file.  If you want to include software timer functionality then ensure\r
93 configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */\r
94 #if ( configUSE_TIMERS == 1 )\r
95 \r
96 /* Misc definitions. */\r
97 #define tmrNO_DELAY             ( TickType_t ) 0U\r
98 \r
99 /* The definition of the timers themselves. */\r
100 typedef struct tmrTimerControl\r
101 {\r
102         const char                              *pcTimerName;           /*<< Text name.  This is not used by the kernel, it is included simply to make debugging easier. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
103         ListItem_t                              xTimerListItem;         /*<< Standard linked list item as used by all kernel features for event management. */\r
104         TickType_t                              xTimerPeriodInTicks;/*<< How quickly and often the timer expires. */\r
105         UBaseType_t                             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
106         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
107         TimerCallbackFunction_t pxCallbackFunction;     /*<< The function that will be called when the timer expires. */\r
108         #if( configUSE_TRACE_FACILITY == 1 )\r
109                 UBaseType_t                     uxTimerNumber;          /*<< An ID assigned by trace tools such as FreeRTOS+Trace */\r
110         #endif\r
111 } xTIMER;\r
112 \r
113 /* The old xTIMER name is maintained above then typedefed to the new Timer_t\r
114 name below to enable the use of older kernel aware debuggers. */\r
115 typedef xTIMER Timer_t;\r
116 \r
117 /* The definition of messages that can be sent and received on the timer queue.\r
118 Two types of message can be queued - messages that manipulate a software timer,\r
119 and messages that request the execution of a non-timer related callback.  The\r
120 two message types are defined in two separate structures, xTimerParametersType\r
121 and xCallbackParametersType respectively. */\r
122 typedef struct tmrTimerParameters\r
123 {\r
124         TickType_t                      xMessageValue;          /*<< An optional value used by a subset of commands, for example, when changing the period of a timer. */\r
125         Timer_t *                       pxTimer;                        /*<< The timer to which the command will be applied. */\r
126 } TimerParameter_t;\r
127 \r
128 \r
129 typedef struct tmrCallbackParameters\r
130 {\r
131         PendedFunction_t        pxCallbackFunction;     /* << The callback function to execute. */\r
132         void *pvParameter1;                                             /* << The value that will be used as the callback functions first parameter. */\r
133         uint32_t ulParameter2;                                  /* << The value that will be used as the callback functions second parameter. */\r
134 } CallbackParameters_t;\r
135 \r
136 /* The structure that contains the two message types, along with an identifier\r
137 that is used to determine which message type is valid. */\r
138 typedef struct tmrTimerQueueMessage\r
139 {\r
140         BaseType_t                      xMessageID;                     /*<< The command being sent to the timer service task. */\r
141         union\r
142         {\r
143                 TimerParameter_t xTimerParameters;\r
144 \r
145                 /* Don't include xCallbackParameters if it is not going to be used as\r
146                 it makes the structure (and therefore the timer queue) larger. */\r
147                 #if ( INCLUDE_xTimerPendFunctionCall == 1 )\r
148                         CallbackParameters_t xCallbackParameters;\r
149                 #endif /* INCLUDE_xTimerPendFunctionCall */\r
150         } u;\r
151 } DaemonTaskMessage_t;\r
152 \r
153 /*lint -e956 A manual analysis and inspection has been used to determine which\r
154 static variables must be declared volatile. */\r
155 \r
156 /* The list in which active timers are stored.  Timers are referenced in expire\r
157 time order, with the nearest expiry time at the front of the list.  Only the\r
158 timer service task is allowed to access these lists. */\r
159 PRIVILEGED_DATA static List_t xActiveTimerList1;\r
160 PRIVILEGED_DATA static List_t xActiveTimerList2;\r
161 PRIVILEGED_DATA static List_t *pxCurrentTimerList;\r
162 PRIVILEGED_DATA static List_t *pxOverflowTimerList;\r
163 \r
164 /* A queue that is used to send commands to the timer service task. */\r
165 PRIVILEGED_DATA static QueueHandle_t xTimerQueue = NULL;\r
166 \r
167 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )\r
168 \r
169         PRIVILEGED_DATA static TaskHandle_t xTimerTaskHandle = NULL;\r
170 \r
171 #endif\r
172 \r
173 /*lint +e956 */\r
174 \r
175 /*-----------------------------------------------------------*/\r
176 \r
177 /*\r
178  * Initialise the infrastructure used by the timer service task if it has not\r
179  * been initialised already.\r
180  */\r
181 static void prvCheckForValidListAndQueue( void ) PRIVILEGED_FUNCTION;\r
182 \r
183 /*\r
184  * The timer service task (daemon).  Timer functionality is controlled by this\r
185  * task.  Other tasks communicate with the timer service task using the\r
186  * xTimerQueue queue.\r
187  */\r
188 static void prvTimerTask( void *pvParameters ) PRIVILEGED_FUNCTION;\r
189 \r
190 /*\r
191  * Called by the timer service task to interpret and process a command it\r
192  * received on the timer queue.\r
193  */\r
194 static void     prvProcessReceivedCommands( void ) PRIVILEGED_FUNCTION;\r
195 \r
196 /*\r
197  * Insert the timer into either xActiveTimerList1, or xActiveTimerList2,\r
198  * depending on if the expire time causes a timer counter overflow.\r
199  */\r
200 static BaseType_t prvInsertTimerInActiveList( Timer_t * const pxTimer, const TickType_t xNextExpiryTime, const TickType_t xTimeNow, const TickType_t xCommandTime ) PRIVILEGED_FUNCTION;\r
201 \r
202 /*\r
203  * An active timer has reached its expire time.  Reload the timer if it is an\r
204  * auto reload timer, then call its callback.\r
205  */\r
206 static void prvProcessExpiredTimer( const TickType_t xNextExpireTime, const TickType_t xTimeNow ) PRIVILEGED_FUNCTION;\r
207 \r
208 /*\r
209  * The tick count has overflowed.  Switch the timer lists after ensuring the\r
210  * current timer list does not still reference some timers.\r
211  */\r
212 static void prvSwitchTimerLists( void ) PRIVILEGED_FUNCTION;\r
213 \r
214 /*\r
215  * Obtain the current tick count, setting *pxTimerListsWereSwitched to pdTRUE\r
216  * if a tick count overflow occurred since prvSampleTimeNow() was last called.\r
217  */\r
218 static TickType_t prvSampleTimeNow( BaseType_t * const pxTimerListsWereSwitched ) PRIVILEGED_FUNCTION;\r
219 \r
220 /*\r
221  * If the timer list contains any active timers then return the expire time of\r
222  * the timer that will expire first and set *pxListWasEmpty to false.  If the\r
223  * timer list does not contain any timers then return 0 and set *pxListWasEmpty\r
224  * to pdTRUE.\r
225  */\r
226 static TickType_t prvGetNextExpireTime( BaseType_t * const pxListWasEmpty ) PRIVILEGED_FUNCTION;\r
227 \r
228 /*\r
229  * If a timer has expired, process it.  Otherwise, block the timer service task\r
230  * until either a timer does expire or a command is received.\r
231  */\r
232 static void prvProcessTimerOrBlockTask( const TickType_t xNextExpireTime, const BaseType_t xListWasEmpty ) PRIVILEGED_FUNCTION;\r
233 \r
234 /*-----------------------------------------------------------*/\r
235 \r
236 BaseType_t xTimerCreateTimerTask( void )\r
237 {\r
238 BaseType_t xReturn = pdFAIL;\r
239 \r
240         /* This function is called when the scheduler is started if\r
241         configUSE_TIMERS is set to 1.  Check that the infrastructure used by the\r
242         timer service task has been created/initialised.  If timers have already\r
243         been created then the initialisation will already have been performed. */\r
244         prvCheckForValidListAndQueue();\r
245 \r
246         if( xTimerQueue != NULL )\r
247         {\r
248                 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )\r
249                 {\r
250                         /* Create the timer task, storing its handle in xTimerTaskHandle so\r
251                         it can be returned by the xTimerGetTimerDaemonTaskHandle() function. */\r
252                         xReturn = xTaskCreate( prvTimerTask, "Tmr Svc", ( uint16_t ) configTIMER_TASK_STACK_DEPTH, NULL, ( ( UBaseType_t ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT, &xTimerTaskHandle );\r
253                 }\r
254                 #else\r
255                 {\r
256                         /* Create the timer task without storing its handle. */\r
257                         xReturn = xTaskCreate( prvTimerTask, "Tmr Svc", ( uint16_t ) configTIMER_TASK_STACK_DEPTH, NULL, ( ( UBaseType_t ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT, NULL);\r
258                 }\r
259                 #endif\r
260         }\r
261         else\r
262         {\r
263                 mtCOVERAGE_TEST_MARKER();\r
264         }\r
265 \r
266         configASSERT( xReturn );\r
267         return xReturn;\r
268 }\r
269 /*-----------------------------------------------------------*/\r
270 \r
271 TimerHandle_t xTimerCreate( const char * const pcTimerName, const TickType_t xTimerPeriodInTicks, const UBaseType_t uxAutoReload, void * const pvTimerID, TimerCallbackFunction_t pxCallbackFunction ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
272 {\r
273 Timer_t *pxNewTimer;\r
274 \r
275         /* Allocate the timer structure. */\r
276         if( xTimerPeriodInTicks == ( TickType_t ) 0U )\r
277         {\r
278                 pxNewTimer = NULL;\r
279         }\r
280         else\r
281         {\r
282                 pxNewTimer = ( Timer_t * ) pvPortMalloc( sizeof( Timer_t ) );\r
283                 if( pxNewTimer != NULL )\r
284                 {\r
285                         /* Ensure the infrastructure used by the timer service task has been\r
286                         created/initialised. */\r
287                         prvCheckForValidListAndQueue();\r
288 \r
289                         /* Initialise the timer structure members using the function parameters. */\r
290                         pxNewTimer->pcTimerName = pcTimerName;\r
291                         pxNewTimer->xTimerPeriodInTicks = xTimerPeriodInTicks;\r
292                         pxNewTimer->uxAutoReload = uxAutoReload;\r
293                         pxNewTimer->pvTimerID = pvTimerID;\r
294                         pxNewTimer->pxCallbackFunction = pxCallbackFunction;\r
295                         vListInitialiseItem( &( pxNewTimer->xTimerListItem ) );\r
296 \r
297                         traceTIMER_CREATE( pxNewTimer );\r
298                 }\r
299                 else\r
300                 {\r
301                         traceTIMER_CREATE_FAILED();\r
302                 }\r
303         }\r
304 \r
305         /* 0 is not a valid value for xTimerPeriodInTicks. */\r
306         configASSERT( ( xTimerPeriodInTicks > 0 ) );\r
307 \r
308         return ( TimerHandle_t ) pxNewTimer;\r
309 }\r
310 /*-----------------------------------------------------------*/\r
311 \r
312 BaseType_t xTimerGenericCommand( TimerHandle_t xTimer, const BaseType_t xCommandID, const TickType_t xOptionalValue, BaseType_t * const pxHigherPriorityTaskWoken, const TickType_t xTicksToWait )\r
313 {\r
314 BaseType_t xReturn = pdFAIL;\r
315 DaemonTaskMessage_t xMessage;\r
316 \r
317         /* Send a message to the timer service task to perform a particular action\r
318         on a particular timer definition. */\r
319         if( xTimerQueue != NULL )\r
320         {\r
321                 /* Send a command to the timer service task to start the xTimer timer. */\r
322                 xMessage.xMessageID = xCommandID;\r
323                 xMessage.u.xTimerParameters.xMessageValue = xOptionalValue;\r
324                 xMessage.u.xTimerParameters.pxTimer = ( Timer_t * ) xTimer;\r
325 \r
326                 if( xCommandID < tmrFIRST_FROM_ISR_COMMAND )\r
327                 {\r
328                         if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )\r
329                         {\r
330                                 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xTicksToWait );\r
331                         }\r
332                         else\r
333                         {\r
334                                 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, tmrNO_DELAY );\r
335                         }\r
336                 }\r
337                 else\r
338                 {\r
339                         xReturn = xQueueSendToBackFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );\r
340                 }\r
341 \r
342                 traceTIMER_COMMAND_SEND( xTimer, xCommandID, xOptionalValue, xReturn );\r
343         }\r
344         else\r
345         {\r
346                 mtCOVERAGE_TEST_MARKER();\r
347         }\r
348 \r
349         return xReturn;\r
350 }\r
351 /*-----------------------------------------------------------*/\r
352 \r
353 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )\r
354 \r
355         TaskHandle_t xTimerGetTimerDaemonTaskHandle( void )\r
356         {\r
357                 /* If xTimerGetTimerDaemonTaskHandle() is called before the scheduler has been\r
358                 started, then xTimerTaskHandle will be NULL. */\r
359                 configASSERT( ( xTimerTaskHandle != NULL ) );\r
360                 return xTimerTaskHandle;\r
361         }\r
362 \r
363 #endif\r
364 /*-----------------------------------------------------------*/\r
365 \r
366 const char * const pcTimerGetTimerName( TimerHandle_t xTimer )\r
367 {\r
368 Timer_t *pxTimer = ( Timer_t * ) xTimer;\r
369 \r
370         return pxTimer->pcTimerName;\r
371 }\r
372 /*-----------------------------------------------------------*/\r
373 \r
374 static void prvProcessExpiredTimer( const TickType_t xNextExpireTime, const TickType_t xTimeNow )\r
375 {\r
376 BaseType_t xResult;\r
377 Timer_t * const pxTimer = ( Timer_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );\r
378 \r
379         /* Remove the timer from the list of active timers.  A check has already\r
380         been performed to ensure the list is not empty. */\r
381         ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );\r
382         traceTIMER_EXPIRED( pxTimer );\r
383 \r
384         /* If the timer is an auto reload timer then calculate the next\r
385         expiry time and re-insert the timer in the list of active timers. */\r
386         if( pxTimer->uxAutoReload == ( UBaseType_t ) pdTRUE )\r
387         {\r
388                 /* The timer is inserted into a list using a time relative to anything\r
389                 other than the current time.  It will therefore be inserted into the\r
390                 correct list relative to the time this task thinks it is now. */\r
391                 if( prvInsertTimerInActiveList( pxTimer, ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ), xTimeNow, xNextExpireTime ) == pdTRUE )\r
392                 {\r
393                         /* The timer expired before it was added to the active timer\r
394                         list.  Reload it now.  */\r
395                         xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START_DONT_TRACE, xNextExpireTime, NULL, tmrNO_DELAY );\r
396                         configASSERT( xResult );\r
397                         ( void ) xResult;\r
398                 }\r
399                 else\r
400                 {\r
401                         mtCOVERAGE_TEST_MARKER();\r
402                 }\r
403         }\r
404         else\r
405         {\r
406                 mtCOVERAGE_TEST_MARKER();\r
407         }\r
408 \r
409         /* Call the timer callback. */\r
410         pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );\r
411 }\r
412 /*-----------------------------------------------------------*/\r
413 \r
414 static void prvTimerTask( void *pvParameters )\r
415 {\r
416 TickType_t xNextExpireTime;\r
417 BaseType_t xListWasEmpty;\r
418 \r
419         /* Just to avoid compiler warnings. */\r
420         ( void ) pvParameters;\r
421 \r
422         for( ;; )\r
423         {\r
424                 /* Query the timers list to see if it contains any timers, and if so,\r
425                 obtain the time at which the next timer will expire. */\r
426                 xNextExpireTime = prvGetNextExpireTime( &xListWasEmpty );\r
427 \r
428                 /* If a timer has expired, process it.  Otherwise, block this task\r
429                 until either a timer does expire, or a command is received. */\r
430                 prvProcessTimerOrBlockTask( xNextExpireTime, xListWasEmpty );\r
431 \r
432                 /* Empty the command queue. */\r
433                 prvProcessReceivedCommands();\r
434         }\r
435 }\r
436 /*-----------------------------------------------------------*/\r
437 \r
438 static void prvProcessTimerOrBlockTask( const TickType_t xNextExpireTime, const BaseType_t xListWasEmpty )\r
439 {\r
440 TickType_t xTimeNow;\r
441 BaseType_t xTimerListsWereSwitched;\r
442 \r
443         vTaskSuspendAll();\r
444         {\r
445                 /* Obtain the time now to make an assessment as to whether the timer\r
446                 has expired or not.  If obtaining the time causes the lists to switch\r
447                 then don't process this timer as any timers that remained in the list\r
448                 when the lists were switched will have been processed within the\r
449                 prvSampleTimeNow() function. */\r
450                 xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );\r
451                 if( xTimerListsWereSwitched == pdFALSE )\r
452                 {\r
453                         /* The tick count has not overflowed, has the timer expired? */\r
454                         if( ( xListWasEmpty == pdFALSE ) && ( xNextExpireTime <= xTimeNow ) )\r
455                         {\r
456                                 ( void ) xTaskResumeAll();\r
457                                 prvProcessExpiredTimer( xNextExpireTime, xTimeNow );\r
458                         }\r
459                         else\r
460                         {\r
461                                 /* The tick count has not overflowed, and the next expire\r
462                                 time has not been reached yet.  This task should therefore\r
463                                 block to wait for the next expire time or a command to be\r
464                                 received - whichever comes first.  The following line cannot\r
465                                 be reached unless xNextExpireTime > xTimeNow, except in the\r
466                                 case when the current timer list is empty. */\r
467                                 vQueueWaitForMessageRestricted( xTimerQueue, ( xNextExpireTime - xTimeNow ) );\r
468 \r
469                                 if( xTaskResumeAll() == pdFALSE )\r
470                                 {\r
471                                         /* Yield to wait for either a command to arrive, or the block time\r
472                                         to expire.  If a command arrived between the critical section being\r
473                                         exited and this yield then the yield will not cause the task\r
474                                         to block. */\r
475                                         portYIELD_WITHIN_API();\r
476                                 }\r
477                                 else\r
478                                 {\r
479                                         mtCOVERAGE_TEST_MARKER();\r
480                                 }\r
481                         }\r
482                 }\r
483                 else\r
484                 {\r
485                         ( void ) xTaskResumeAll();\r
486                 }\r
487         }\r
488 }\r
489 /*-----------------------------------------------------------*/\r
490 \r
491 static TickType_t prvGetNextExpireTime( BaseType_t * const pxListWasEmpty )\r
492 {\r
493 TickType_t xNextExpireTime;\r
494 \r
495         /* Timers are listed in expiry time order, with the head of the list\r
496         referencing the task that will expire first.  Obtain the time at which\r
497         the timer with the nearest expiry time will expire.  If there are no\r
498         active timers then just set the next expire time to 0.  That will cause\r
499         this task to unblock when the tick count overflows, at which point the\r
500         timer lists will be switched and the next expiry time can be\r
501         re-assessed.  */\r
502         *pxListWasEmpty = listLIST_IS_EMPTY( pxCurrentTimerList );\r
503         if( *pxListWasEmpty == pdFALSE )\r
504         {\r
505                 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );\r
506         }\r
507         else\r
508         {\r
509                 /* Ensure the task unblocks when the tick count rolls over. */\r
510                 xNextExpireTime = ( TickType_t ) 0U;\r
511         }\r
512 \r
513         return xNextExpireTime;\r
514 }\r
515 /*-----------------------------------------------------------*/\r
516 \r
517 static TickType_t prvSampleTimeNow( BaseType_t * const pxTimerListsWereSwitched )\r
518 {\r
519 TickType_t xTimeNow;\r
520 PRIVILEGED_DATA static TickType_t xLastTime = ( TickType_t ) 0U; /*lint !e956 Variable is only accessible to one task. */\r
521 \r
522         xTimeNow = xTaskGetTickCount();\r
523 \r
524         if( xTimeNow < xLastTime )\r
525         {\r
526                 prvSwitchTimerLists();\r
527                 *pxTimerListsWereSwitched = pdTRUE;\r
528         }\r
529         else\r
530         {\r
531                 *pxTimerListsWereSwitched = pdFALSE;\r
532         }\r
533 \r
534         xLastTime = xTimeNow;\r
535 \r
536         return xTimeNow;\r
537 }\r
538 /*-----------------------------------------------------------*/\r
539 \r
540 static BaseType_t prvInsertTimerInActiveList( Timer_t * const pxTimer, const TickType_t xNextExpiryTime, const TickType_t xTimeNow, const TickType_t xCommandTime )\r
541 {\r
542 BaseType_t xProcessTimerNow = pdFALSE;\r
543 \r
544         listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xNextExpiryTime );\r
545         listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );\r
546 \r
547         if( xNextExpiryTime <= xTimeNow )\r
548         {\r
549                 /* Has the expiry time elapsed between the command to start/reset a\r
550                 timer was issued, and the time the command was processed? */\r
551                 if( ( xTimeNow - xCommandTime ) >= pxTimer->xTimerPeriodInTicks )\r
552                 {\r
553                         /* The time between a command being issued and the command being\r
554                         processed actually exceeds the timers period.  */\r
555                         xProcessTimerNow = pdTRUE;\r
556                 }\r
557                 else\r
558                 {\r
559                         vListInsert( pxOverflowTimerList, &( pxTimer->xTimerListItem ) );\r
560                 }\r
561         }\r
562         else\r
563         {\r
564                 if( ( xTimeNow < xCommandTime ) && ( xNextExpiryTime >= xCommandTime ) )\r
565                 {\r
566                         /* If, since the command was issued, the tick count has overflowed\r
567                         but the expiry time has not, then the timer must have already passed\r
568                         its expiry time and should be processed immediately. */\r
569                         xProcessTimerNow = pdTRUE;\r
570                 }\r
571                 else\r
572                 {\r
573                         vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );\r
574                 }\r
575         }\r
576 \r
577         return xProcessTimerNow;\r
578 }\r
579 /*-----------------------------------------------------------*/\r
580 \r
581 static void     prvProcessReceivedCommands( void )\r
582 {\r
583 DaemonTaskMessage_t xMessage;\r
584 Timer_t *pxTimer;\r
585 BaseType_t xTimerListsWereSwitched, xResult;\r
586 TickType_t xTimeNow;\r
587 \r
588         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
589         {\r
590                 #if ( INCLUDE_xTimerPendFunctionCall == 1 )\r
591                 {\r
592                         /* Negative commands are pended function calls rather than timer\r
593                         commands. */\r
594                         if( xMessage.xMessageID < 0 )\r
595                         {\r
596                                 const CallbackParameters_t * const pxCallback = &( xMessage.u.xCallbackParameters );\r
597 \r
598                                 /* The timer uses the xCallbackParameters member to request a\r
599                                 callback be executed.  Check the callback is not NULL. */\r
600                                 configASSERT( pxCallback );\r
601 \r
602                                 /* Call the function. */\r
603                                 pxCallback->pxCallbackFunction( pxCallback->pvParameter1, pxCallback->ulParameter2 );\r
604                         }\r
605                         else\r
606                         {\r
607                                 mtCOVERAGE_TEST_MARKER();\r
608                         }\r
609                 }\r
610                 #endif /* INCLUDE_xTimerPendFunctionCall */\r
611 \r
612                 /* Commands that are positive are timer commands rather than pended\r
613                 function calls. */\r
614                 if( xMessage.xMessageID >= ( BaseType_t ) 0 )\r
615                 {\r
616                         /* The messages uses the xTimerParameters member to work on a\r
617                         software timer. */\r
618                         pxTimer = xMessage.u.xTimerParameters.pxTimer;\r
619 \r
620                         if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )\r
621                         {\r
622                                 /* The timer is in a list, remove it. */\r
623                                 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );\r
624                         }\r
625                         else\r
626                         {\r
627                                 mtCOVERAGE_TEST_MARKER();\r
628                         }\r
629 \r
630                         traceTIMER_COMMAND_RECEIVED( pxTimer, xMessage.xMessageID, xMessage.u.xTimerParameters.xMessageValue );\r
631 \r
632                         /* In this case the xTimerListsWereSwitched parameter is not used, but\r
633                         it must be present in the function call.  prvSampleTimeNow() must be\r
634                         called after the message is received from xTimerQueue so there is no\r
635                         possibility of a higher priority task adding a message to the message\r
636                         queue with a time that is ahead of the timer daemon task (because it\r
637                         pre-empted the timer daemon task after the xTimeNow value was set). */\r
638                         xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );\r
639 \r
640                         switch( xMessage.xMessageID )\r
641                         {\r
642                                 case tmrCOMMAND_START :\r
643                             case tmrCOMMAND_START_FROM_ISR :\r
644                             case tmrCOMMAND_RESET :\r
645                             case tmrCOMMAND_RESET_FROM_ISR :\r
646                                 case tmrCOMMAND_START_DONT_TRACE :\r
647                                         /* Start or restart a timer. */\r
648                                         if( prvInsertTimerInActiveList( pxTimer,  xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow, xMessage.u.xTimerParameters.xMessageValue ) == pdTRUE )\r
649                                         {\r
650                                                 /* The timer expired before it was added to the active\r
651                                                 timer list.  Process it now. */\r
652                                                 pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );\r
653                                                 traceTIMER_EXPIRED( pxTimer );\r
654 \r
655                                                 if( pxTimer->uxAutoReload == ( UBaseType_t ) pdTRUE )\r
656                                                 {\r
657                                                         xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START_DONT_TRACE, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, NULL, tmrNO_DELAY );\r
658                                                         configASSERT( xResult );\r
659                                                         ( void ) xResult;\r
660                                                 }\r
661                                                 else\r
662                                                 {\r
663                                                         mtCOVERAGE_TEST_MARKER();\r
664                                                 }\r
665                                         }\r
666                                         else\r
667                                         {\r
668                                                 mtCOVERAGE_TEST_MARKER();\r
669                                         }\r
670                                         break;\r
671 \r
672                                 case tmrCOMMAND_STOP :\r
673                                 case tmrCOMMAND_STOP_FROM_ISR :\r
674                                         /* The timer has already been removed from the active list.\r
675                                         There is nothing to do here. */\r
676                                         break;\r
677 \r
678                                 case tmrCOMMAND_CHANGE_PERIOD :\r
679                                 case tmrCOMMAND_CHANGE_PERIOD_FROM_ISR :\r
680                                         pxTimer->xTimerPeriodInTicks = xMessage.u.xTimerParameters.xMessageValue;\r
681                                         configASSERT( ( pxTimer->xTimerPeriodInTicks > 0 ) );\r
682 \r
683                                         /* The new period does not really have a reference, and can be\r
684                                         longer or shorter than the old one.  The command time is\r
685                                         therefore set to the current time, and as the period cannot be\r
686                                         zero the next expiry time can only be in the future, meaning\r
687                                         (unlike for the xTimerStart() case above) there is no fail case\r
688                                         that needs to be handled here. */\r
689                                         ( void ) prvInsertTimerInActiveList( pxTimer, ( xTimeNow + pxTimer->xTimerPeriodInTicks ), xTimeNow, xTimeNow );\r
690                                         break;\r
691 \r
692                                 case tmrCOMMAND_DELETE :\r
693                                         /* The timer has already been removed from the active list,\r
694                                         just free up the memory. */\r
695                                         vPortFree( pxTimer );\r
696                                         break;\r
697 \r
698                                 default :\r
699                                         /* Don't expect to get here. */\r
700                                         break;\r
701                         }\r
702                 }\r
703         }\r
704 }\r
705 /*-----------------------------------------------------------*/\r
706 \r
707 static void prvSwitchTimerLists( void )\r
708 {\r
709 TickType_t xNextExpireTime, xReloadTime;\r
710 List_t *pxTemp;\r
711 Timer_t *pxTimer;\r
712 BaseType_t xResult;\r
713 \r
714         /* The tick count has overflowed.  The timer lists must be switched.\r
715         If there are any timers still referenced from the current timer list\r
716         then they must have expired and should be processed before the lists\r
717         are switched. */\r
718         while( listLIST_IS_EMPTY( pxCurrentTimerList ) == pdFALSE )\r
719         {\r
720                 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );\r
721 \r
722                 /* Remove the timer from the list. */\r
723                 pxTimer = ( Timer_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );\r
724                 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );\r
725                 traceTIMER_EXPIRED( pxTimer );\r
726 \r
727                 /* Execute its callback, then send a command to restart the timer if\r
728                 it is an auto-reload timer.  It cannot be restarted here as the lists\r
729                 have not yet been switched. */\r
730                 pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );\r
731 \r
732                 if( pxTimer->uxAutoReload == ( UBaseType_t ) pdTRUE )\r
733                 {\r
734                         /* Calculate the reload value, and if the reload value results in\r
735                         the timer going into the same timer list then it has already expired\r
736                         and the timer should be re-inserted into the current list so it is\r
737                         processed again within this loop.  Otherwise a command should be sent\r
738                         to restart the timer to ensure it is only inserted into a list after\r
739                         the lists have been swapped. */\r
740                         xReloadTime = ( xNextExpireTime + pxTimer->xTimerPeriodInTicks );\r
741                         if( xReloadTime > xNextExpireTime )\r
742                         {\r
743                                 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xReloadTime );\r
744                                 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );\r
745                                 vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );\r
746                         }\r
747                         else\r
748                         {\r
749                                 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START_DONT_TRACE, xNextExpireTime, NULL, tmrNO_DELAY );\r
750                                 configASSERT( xResult );\r
751                                 ( void ) xResult;\r
752                         }\r
753                 }\r
754                 else\r
755                 {\r
756                         mtCOVERAGE_TEST_MARKER();\r
757                 }\r
758         }\r
759 \r
760         pxTemp = pxCurrentTimerList;\r
761         pxCurrentTimerList = pxOverflowTimerList;\r
762         pxOverflowTimerList = pxTemp;\r
763 }\r
764 /*-----------------------------------------------------------*/\r
765 \r
766 static void prvCheckForValidListAndQueue( void )\r
767 {\r
768         /* Check that the list from which active timers are referenced, and the\r
769         queue used to communicate with the timer service, have been\r
770         initialised. */\r
771         taskENTER_CRITICAL();\r
772         {\r
773                 if( xTimerQueue == NULL )\r
774                 {\r
775                         vListInitialise( &xActiveTimerList1 );\r
776                         vListInitialise( &xActiveTimerList2 );\r
777                         pxCurrentTimerList = &xActiveTimerList1;\r
778                         pxOverflowTimerList = &xActiveTimerList2;\r
779                         xTimerQueue = xQueueCreate( ( UBaseType_t ) configTIMER_QUEUE_LENGTH, sizeof( DaemonTaskMessage_t ) );\r
780                         configASSERT( xTimerQueue );\r
781 \r
782                         #if ( configQUEUE_REGISTRY_SIZE > 0 )\r
783                         {\r
784                                 if( xTimerQueue != NULL )\r
785                                 {\r
786                                         vQueueAddToRegistry( xTimerQueue, "TmrQ" );\r
787                                 }\r
788                                 else\r
789                                 {\r
790                                         mtCOVERAGE_TEST_MARKER();\r
791                                 }\r
792                         }\r
793                         #endif /* configQUEUE_REGISTRY_SIZE */\r
794                 }\r
795                 else\r
796                 {\r
797                         mtCOVERAGE_TEST_MARKER();\r
798                 }\r
799         }\r
800         taskEXIT_CRITICAL();\r
801 }\r
802 /*-----------------------------------------------------------*/\r
803 \r
804 BaseType_t xTimerIsTimerActive( TimerHandle_t xTimer )\r
805 {\r
806 BaseType_t xTimerIsInActiveList;\r
807 Timer_t *pxTimer = ( Timer_t * ) xTimer;\r
808 \r
809         /* Is the timer in the list of active timers? */\r
810         taskENTER_CRITICAL();\r
811         {\r
812                 /* Checking to see if it is in the NULL list in effect checks to see if\r
813                 it is referenced from either the current or the overflow timer lists in\r
814                 one go, but the logic has to be reversed, hence the '!'. */\r
815                 xTimerIsInActiveList = ( BaseType_t ) !( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) );\r
816         }\r
817         taskEXIT_CRITICAL();\r
818 \r
819         return xTimerIsInActiveList;\r
820 } /*lint !e818 Can't be pointer to const due to the typedef. */\r
821 /*-----------------------------------------------------------*/\r
822 \r
823 void *pvTimerGetTimerID( const TimerHandle_t xTimer )\r
824 {\r
825 Timer_t * const pxTimer = ( Timer_t * ) xTimer;\r
826 \r
827         return pxTimer->pvTimerID;\r
828 }\r
829 /*-----------------------------------------------------------*/\r
830 \r
831 #if( INCLUDE_xTimerPendFunctionCall == 1 )\r
832 \r
833         BaseType_t xTimerPendFunctionCallFromISR( PendedFunction_t xFunctionToPend, void *pvParameter1, uint32_t ulParameter2, BaseType_t *pxHigherPriorityTaskWoken )\r
834         {\r
835         DaemonTaskMessage_t xMessage;\r
836         BaseType_t xReturn;\r
837 \r
838                 /* Complete the message with the function parameters and post it to the\r
839                 daemon task. */\r
840                 xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK_FROM_ISR;\r
841                 xMessage.u.xCallbackParameters.pxCallbackFunction = xFunctionToPend;\r
842                 xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;\r
843                 xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;\r
844 \r
845                 xReturn = xQueueSendFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );\r
846                 \r
847                 tracePEND_FUNC_CALL_FROM_ISR( xFunctionToPend, pvParameter1, ulParameter2, xReturn );\r
848 \r
849                 return xReturn;\r
850         }\r
851 \r
852 #endif /* INCLUDE_xTimerPendFunctionCall */\r
853 /*-----------------------------------------------------------*/\r
854 \r
855 #if( INCLUDE_xTimerPendFunctionCall == 1 )\r
856 \r
857         BaseType_t xTimerPendFunctionCall( PendedFunction_t xFunctionToPend, void *pvParameter1, uint32_t ulParameter2, TickType_t xTicksToWait )\r
858         {\r
859         DaemonTaskMessage_t xMessage;\r
860         BaseType_t xReturn;\r
861 \r
862                 /* Complete the message with the function parameters and post it to the\r
863                 daemon task. */\r
864                 xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK;\r
865                 xMessage.u.xCallbackParameters.pxCallbackFunction = xFunctionToPend;\r
866                 xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;\r
867                 xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;\r
868 \r
869                 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xTicksToWait );\r
870 \r
871                 tracePEND_FUNC_CALL( xFunctionToPend, pvParameter1, ulParameter2, xReturn );\r
872                 \r
873                 return xReturn;\r
874         }\r
875 \r
876 #endif /* INCLUDE_xTimerPendFunctionCall */\r
877 /*-----------------------------------------------------------*/\r
878 \r
879 /* This entire source file will be skipped if the application is not configured\r
880 to include software timer functionality.  If you want to include software timer\r
881 functionality then ensure configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */\r
882 #endif /* configUSE_TIMERS == 1 */\r
883 \r
884 \r
885 \r