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