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