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