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