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