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