]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/timers.c
vQueueAddToRegistry() now takes a const char * instead of a char *.
[freertos] / FreeRTOS / Source / timers.c
1 /*\r
2     FreeRTOS V8.0.0:rc1 - Copyright (C) 2014 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     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /* Standard includes. */\r
67 #include <stdlib.h>\r
68 \r
69 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
70 all the API functions to use the MPU wrappers.  That should only be done when\r
71 task.h is included from an application file. */\r
72 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
73 \r
74 #include "FreeRTOS.h"\r
75 #include "task.h"\r
76 #include "queue.h"\r
77 #include "timers.h"\r
78 \r
79 #if ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 0 )\r
80         #error configUSE_TIMERS must be set to 1 to make the xTimerPendFunctionCall() function available.\r
81 #endif\r
82 \r
83 /* Lint e961 and e750 are suppressed as a MISRA exception justified because the\r
84 MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined for the\r
85 header files above, but not in this file, in order to generate the correct\r
86 privileged Vs unprivileged linkage and placement. */\r
87 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750. */\r
88 \r
89 \r
90 /* This entire source file will be skipped if the application is not configured\r
91 to include software timer functionality.  This #if is closed at the very bottom\r
92 of this file.  If you want to include software timer functionality then ensure\r
93 configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */\r
94 #if ( configUSE_TIMERS == 1 )\r
95 \r
96 /* Misc definitions. */\r
97 #define tmrNO_DELAY             ( TickType_t ) 0U\r
98 \r
99 /* The definition of the timers themselves. */\r
100 typedef struct tmrTimerControl\r
101 {\r
102         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
103         ListItem_t                              xTimerListItem;         /*<< Standard linked list item as used by all kernel features for event management. */\r
104         TickType_t                              xTimerPeriodInTicks;/*<< How quickly and often the timer expires. */\r
105         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
106         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
107         TimerCallbackFunction_t pxCallbackFunction;     /*<< The function that will be called when the timer expires. */\r
108         #if( configUSE_TRACE_FACILITY == 1 )\r
109                 UBaseType_t                     uxTimerNumber;          /*<< An ID assigned by trace tools such as FreeRTOS+Trace */\r
110         #endif\r
111 } Timer_t;\r
112 \r
113 /* The definition of messages that can be sent and received on the timer queue.\r
114 Two types of message can be queued - messages that manipulate a software timer,\r
115 and messages that request the execution of a non-timer related callback.  The\r
116 two message types are defined in two separate structures, xTimerParametersType\r
117 and xCallbackParametersType respectively. */\r
118 typedef struct tmrTimerParameters\r
119 {\r
120         TickType_t                      xMessageValue;          /*<< An optional value used by a subset of commands, for example, when changing the period of a timer. */\r
121         Timer_t *                       pxTimer;                        /*<< The timer to which the command will be applied. */\r
122 } TimerParameter_t;\r
123 \r
124 \r
125 typedef struct tmrCallbackParameters\r
126 {\r
127         PendedFunction_t        pxCallbackFunction;     /* << The callback function to execute. */\r
128         void *pvParameter1;                                             /* << The value that will be used as the callback functions first parameter. */\r
129         uint32_t ulParameter2;                                  /* << The value that will be used as the callback functions second parameter. */\r
130 } CallbackParameters_t;\r
131 \r
132 /* The structure that contains the two message types, along with an identifier\r
133 that is used to determine which message type is valid. */\r
134 typedef struct tmrTimerQueueMessage\r
135 {\r
136         BaseType_t                      xMessageID;                     /*<< The command being sent to the timer service task. */\r
137         union\r
138         {\r
139                 TimerParameter_t xTimerParameters;\r
140 \r
141                 /* Don't include xCallbackParameters if it is not going to be used as\r
142                 it makes the structure (and therefore the timer queue) larger. */\r
143                 #if ( INCLUDE_xTimerPendFunctionCall == 1 )\r
144                         CallbackParameters_t xCallbackParameters;\r
145                 #endif /* INCLUDE_xTimerPendFunctionCall */\r
146         } u;\r
147 } DaemonTaskMessage_t;\r
148 \r
149 /*lint -e956 A manual analysis and inspection has been used to determine which\r
150 static variables must be declared volatile. */\r
151 \r
152 /* The list in which active timers are stored.  Timers are referenced in expire\r
153 time order, with the nearest expiry time at the front of the list.  Only the\r
154 timer service task is allowed to access these lists. */\r
155 PRIVILEGED_DATA static List_t xActiveTimerList1;\r
156 PRIVILEGED_DATA static List_t xActiveTimerList2;\r
157 PRIVILEGED_DATA static List_t *pxCurrentTimerList;\r
158 PRIVILEGED_DATA static List_t *pxOverflowTimerList;\r
159 \r
160 /* A queue that is used to send commands to the timer service task. */\r
161 PRIVILEGED_DATA static QueueHandle_t xTimerQueue = NULL;\r
162 \r
163 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )\r
164 \r
165         PRIVILEGED_DATA static TaskHandle_t xTimerTaskHandle = NULL;\r
166 \r
167 #endif\r
168 \r
169 /*lint +e956 */\r
170 \r
171 /*-----------------------------------------------------------*/\r
172 \r
173 /*\r
174  * Initialise the infrastructure used by the timer service task if it has not\r
175  * been initialised already.\r
176  */\r
177 static void prvCheckForValidListAndQueue( void ) PRIVILEGED_FUNCTION;\r
178 \r
179 /*\r
180  * The timer service task (daemon).  Timer functionality is controlled by this\r
181  * task.  Other tasks communicate with the timer service task using the\r
182  * xTimerQueue queue.\r
183  */\r
184 static void prvTimerTask( void *pvParameters ) PRIVILEGED_FUNCTION;\r
185 \r
186 /*\r
187  * Called by the timer service task to interpret and process a command it\r
188  * received on the timer queue.\r
189  */\r
190 static void     prvProcessReceivedCommands( void ) PRIVILEGED_FUNCTION;\r
191 \r
192 /*\r
193  * Insert the timer into either xActiveTimerList1, or xActiveTimerList2,\r
194  * depending on if the expire time causes a timer counter overflow.\r
195  */\r
196 static BaseType_t prvInsertTimerInActiveList( Timer_t * const pxTimer, const TickType_t xNextExpiryTime, const TickType_t xTimeNow, const TickType_t xCommandTime ) PRIVILEGED_FUNCTION;\r
197 \r
198 /*\r
199  * An active timer has reached its expire time.  Reload the timer if it is an\r
200  * auto reload timer, then call its callback.\r
201  */\r
202 static void prvProcessExpiredTimer( const TickType_t xNextExpireTime, const TickType_t xTimeNow ) PRIVILEGED_FUNCTION;\r
203 \r
204 /*\r
205  * The tick count has overflowed.  Switch the timer lists after ensuring the\r
206  * current timer list does not still reference some timers.\r
207  */\r
208 static void prvSwitchTimerLists( void ) PRIVILEGED_FUNCTION;\r
209 \r
210 /*\r
211  * Obtain the current tick count, setting *pxTimerListsWereSwitched to pdTRUE\r
212  * if a tick count overflow occurred since prvSampleTimeNow() was last called.\r
213  */\r
214 static TickType_t prvSampleTimeNow( BaseType_t * const pxTimerListsWereSwitched ) PRIVILEGED_FUNCTION;\r
215 \r
216 /*\r
217  * If the timer list contains any active timers then return the expire time of\r
218  * the timer that will expire first and set *pxListWasEmpty to false.  If the\r
219  * timer list does not contain any timers then return 0 and set *pxListWasEmpty\r
220  * to pdTRUE.\r
221  */\r
222 static TickType_t prvGetNextExpireTime( BaseType_t * const pxListWasEmpty ) PRIVILEGED_FUNCTION;\r
223 \r
224 /*\r
225  * If a timer has expired, process it.  Otherwise, block the timer service task\r
226  * until either a timer does expire or a command is received.\r
227  */\r
228 static void prvProcessTimerOrBlockTask( const TickType_t xNextExpireTime, const BaseType_t xListWasEmpty ) PRIVILEGED_FUNCTION;\r
229 \r
230 /*-----------------------------------------------------------*/\r
231 \r
232 BaseType_t xTimerCreateTimerTask( void )\r
233 {\r
234 BaseType_t xReturn = pdFAIL;\r
235 \r
236         /* This function is called when the scheduler is started if\r
237         configUSE_TIMERS is set to 1.  Check that the infrastructure used by the\r
238         timer service task has been created/initialised.  If timers have already\r
239         been created then the initialisation will already have been performed. */\r
240         prvCheckForValidListAndQueue();\r
241 \r
242         if( xTimerQueue != NULL )\r
243         {\r
244                 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )\r
245                 {\r
246                         /* Create the timer task, storing its handle in xTimerTaskHandle so\r
247                         it can be returned by the xTimerGetTimerDaemonTaskHandle() function. */\r
248                         xReturn = xTaskCreate( prvTimerTask, "Tmr Svc", ( uint16_t ) configTIMER_TASK_STACK_DEPTH, NULL, ( ( UBaseType_t ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT, &xTimerTaskHandle );\r
249                 }\r
250                 #else\r
251                 {\r
252                         /* Create the timer task without storing its handle. */\r
253                         xReturn = xTaskCreate( prvTimerTask, "Tmr Svc", ( uint16_t ) configTIMER_TASK_STACK_DEPTH, NULL, ( ( UBaseType_t ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT, NULL);\r
254                 }\r
255                 #endif\r
256         }\r
257         else\r
258         {\r
259                 mtCOVERAGE_TEST_MARKER();\r
260         }\r
261 \r
262         configASSERT( xReturn );\r
263         return xReturn;\r
264 }\r
265 /*-----------------------------------------------------------*/\r
266 \r
267 TimerHandle_t xTimerCreate( const char * const pcTimerName, const TickType_t xTimerPeriodInTicks, const UBaseType_t uxAutoReload, void * const pvTimerID, TimerCallbackFunction_t pxCallbackFunction ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
268 {\r
269 Timer_t *pxNewTimer;\r
270 \r
271         /* Allocate the timer structure. */\r
272         if( xTimerPeriodInTicks == ( TickType_t ) 0U )\r
273         {\r
274                 pxNewTimer = NULL;\r
275         }\r
276         else\r
277         {\r
278                 pxNewTimer = ( Timer_t * ) pvPortMalloc( sizeof( Timer_t ) );\r
279                 if( pxNewTimer != NULL )\r
280                 {\r
281                         /* Ensure the infrastructure used by the timer service task has been\r
282                         created/initialised. */\r
283                         prvCheckForValidListAndQueue();\r
284 \r
285                         /* Initialise the timer structure members using the function parameters. */\r
286                         pxNewTimer->pcTimerName = pcTimerName;\r
287                         pxNewTimer->xTimerPeriodInTicks = xTimerPeriodInTicks;\r
288                         pxNewTimer->uxAutoReload = uxAutoReload;\r
289                         pxNewTimer->pvTimerID = pvTimerID;\r
290                         pxNewTimer->pxCallbackFunction = pxCallbackFunction;\r
291                         vListInitialiseItem( &( pxNewTimer->xTimerListItem ) );\r
292 \r
293                         traceTIMER_CREATE( pxNewTimer );\r
294                 }\r
295                 else\r
296                 {\r
297                         traceTIMER_CREATE_FAILED();\r
298                 }\r
299         }\r
300 \r
301         /* 0 is not a valid value for xTimerPeriodInTicks. */\r
302         configASSERT( ( xTimerPeriodInTicks > 0 ) );\r
303 \r
304         return ( TimerHandle_t ) pxNewTimer;\r
305 }\r
306 /*-----------------------------------------------------------*/\r
307 \r
308 BaseType_t xTimerGenericCommand( TimerHandle_t xTimer, const BaseType_t xCommandID, const TickType_t xOptionalValue, BaseType_t * const pxHigherPriorityTaskWoken, const TickType_t xTicksToWait )\r
309 {\r
310 BaseType_t xReturn = pdFAIL;\r
311 DaemonTaskMessage_t xMessage;\r
312 \r
313         /* Send a message to the timer service task to perform a particular action\r
314         on a particular timer definition. */\r
315         if( xTimerQueue != NULL )\r
316         {\r
317                 /* Send a command to the timer service task to start the xTimer timer. */\r
318                 xMessage.xMessageID = xCommandID;\r
319                 xMessage.u.xTimerParameters.xMessageValue = xOptionalValue;\r
320                 xMessage.u.xTimerParameters.pxTimer = ( Timer_t * ) xTimer;\r
321 \r
322                 if( xCommandID < tmrFIRST_FROM_ISR_COMMAND )\r
323                 {\r
324                         if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )\r
325                         {\r
326                                 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xTicksToWait );\r
327                         }\r
328                         else\r
329                         {\r
330                                 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, tmrNO_DELAY );\r
331                         }\r
332                 }\r
333                 else\r
334                 {\r
335                         xReturn = xQueueSendToBackFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );\r
336                 }\r
337 \r
338                 traceTIMER_COMMAND_SEND( xTimer, xCommandID, xOptionalValue, xReturn );\r
339         }\r
340         else\r
341         {\r
342                 mtCOVERAGE_TEST_MARKER();\r
343         }\r
344 \r
345         return xReturn;\r
346 }\r
347 /*-----------------------------------------------------------*/\r
348 \r
349 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )\r
350 \r
351         TaskHandle_t xTimerGetTimerDaemonTaskHandle( void )\r
352         {\r
353                 /* If xTimerGetTimerDaemonTaskHandle() is called before the scheduler has been\r
354                 started, then xTimerTaskHandle will be NULL. */\r
355                 configASSERT( ( xTimerTaskHandle != NULL ) );\r
356                 return xTimerTaskHandle;\r
357         }\r
358 \r
359 #endif\r
360 /*-----------------------------------------------------------*/\r
361 \r
362 static void prvProcessExpiredTimer( const TickType_t xNextExpireTime, const TickType_t xTimeNow )\r
363 {\r
364 BaseType_t xResult;\r
365 Timer_t * const pxTimer = ( Timer_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );\r
366 \r
367         /* Remove the timer from the list of active timers.  A check has already\r
368         been performed to ensure the list is not empty. */\r
369         ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );\r
370         traceTIMER_EXPIRED( pxTimer );\r
371 \r
372         /* If the timer is an auto reload timer then calculate the next\r
373         expiry time and re-insert the timer in the list of active timers. */\r
374         if( pxTimer->uxAutoReload == ( UBaseType_t ) pdTRUE )\r
375         {\r
376                 /* The timer is inserted into a list using a time relative to anything\r
377                 other than the current time.  It will therefore be inserted into the\r
378                 correct list relative to the time this task thinks it is now. */\r
379                 if( prvInsertTimerInActiveList( pxTimer, ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ), xTimeNow, xNextExpireTime ) == pdTRUE )\r
380                 {\r
381                         /* The timer expired before it was added to the active timer\r
382                         list.  Reload it now.  */\r
383                         xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START_DONT_TRACE, xNextExpireTime, NULL, tmrNO_DELAY );\r
384                         configASSERT( xResult );\r
385                         ( void ) xResult;\r
386                 }\r
387                 else\r
388                 {\r
389                         mtCOVERAGE_TEST_MARKER();\r
390                 }\r
391         }\r
392         else\r
393         {\r
394                 mtCOVERAGE_TEST_MARKER();\r
395         }\r
396 \r
397         /* Call the timer callback. */\r
398         pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );\r
399 }\r
400 /*-----------------------------------------------------------*/\r
401 \r
402 static void prvTimerTask( void *pvParameters )\r
403 {\r
404 TickType_t xNextExpireTime;\r
405 BaseType_t xListWasEmpty;\r
406 \r
407         /* Just to avoid compiler warnings. */\r
408         ( void ) pvParameters;\r
409 \r
410         for( ;; )\r
411         {\r
412                 /* Query the timers list to see if it contains any timers, and if so,\r
413                 obtain the time at which the next timer will expire. */\r
414                 xNextExpireTime = prvGetNextExpireTime( &xListWasEmpty );\r
415 \r
416                 /* If a timer has expired, process it.  Otherwise, block this task\r
417                 until either a timer does expire, or a command is received. */\r
418                 prvProcessTimerOrBlockTask( xNextExpireTime, xListWasEmpty );\r
419 \r
420                 /* Empty the command queue. */\r
421                 prvProcessReceivedCommands();\r
422         }\r
423 }\r
424 /*-----------------------------------------------------------*/\r
425 \r
426 static void prvProcessTimerOrBlockTask( const TickType_t xNextExpireTime, const BaseType_t xListWasEmpty )\r
427 {\r
428 TickType_t xTimeNow;\r
429 BaseType_t xTimerListsWereSwitched;\r
430 \r
431         vTaskSuspendAll();\r
432         {\r
433                 /* Obtain the time now to make an assessment as to whether the timer\r
434                 has expired or not.  If obtaining the time causes the lists to switch\r
435                 then don't process this timer as any timers that remained in the list\r
436                 when the lists were switched will have been processed within the\r
437                 prvSampleTimeNow() function. */\r
438                 xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );\r
439                 if( xTimerListsWereSwitched == pdFALSE )\r
440                 {\r
441                         /* The tick count has not overflowed, has the timer expired? */\r
442                         if( ( xListWasEmpty == pdFALSE ) && ( xNextExpireTime <= xTimeNow ) )\r
443                         {\r
444                                 ( void ) xTaskResumeAll();\r
445                                 prvProcessExpiredTimer( xNextExpireTime, xTimeNow );\r
446                         }\r
447                         else\r
448                         {\r
449                                 /* The tick count has not overflowed, and the next expire\r
450                                 time has not been reached yet.  This task should therefore\r
451                                 block to wait for the next expire time or a command to be\r
452                                 received - whichever comes first.  The following line cannot\r
453                                 be reached unless xNextExpireTime > xTimeNow, except in the\r
454                                 case when the current timer list is empty. */\r
455                                 vQueueWaitForMessageRestricted( xTimerQueue, ( xNextExpireTime - xTimeNow ) );\r
456 \r
457                                 if( xTaskResumeAll() == pdFALSE )\r
458                                 {\r
459                                         /* Yield to wait for either a command to arrive, or the block time\r
460                                         to expire.  If a command arrived between the critical section being\r
461                                         exited and this yield then the yield will not cause the task\r
462                                         to block. */\r
463                                         portYIELD_WITHIN_API();\r
464                                 }\r
465                                 else\r
466                                 {\r
467                                         mtCOVERAGE_TEST_MARKER();\r
468                                 }\r
469                         }\r
470                 }\r
471                 else\r
472                 {\r
473                         ( void ) xTaskResumeAll();\r
474                 }\r
475         }\r
476 }\r
477 /*-----------------------------------------------------------*/\r
478 \r
479 static TickType_t prvGetNextExpireTime( BaseType_t * const pxListWasEmpty )\r
480 {\r
481 TickType_t xNextExpireTime;\r
482 \r
483         /* Timers are listed in expiry time order, with the head of the list\r
484         referencing the task that will expire first.  Obtain the time at which\r
485         the timer with the nearest expiry time will expire.  If there are no\r
486         active timers then just set the next expire time to 0.  That will cause\r
487         this task to unblock when the tick count overflows, at which point the\r
488         timer lists will be switched and the next expiry time can be\r
489         re-assessed.  */\r
490         *pxListWasEmpty = listLIST_IS_EMPTY( pxCurrentTimerList );\r
491         if( *pxListWasEmpty == pdFALSE )\r
492         {\r
493                 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );\r
494         }\r
495         else\r
496         {\r
497                 /* Ensure the task unblocks when the tick count rolls over. */\r
498                 xNextExpireTime = ( TickType_t ) 0U;\r
499         }\r
500 \r
501         return xNextExpireTime;\r
502 }\r
503 /*-----------------------------------------------------------*/\r
504 \r
505 static TickType_t prvSampleTimeNow( BaseType_t * const pxTimerListsWereSwitched )\r
506 {\r
507 TickType_t xTimeNow;\r
508 PRIVILEGED_DATA static TickType_t xLastTime = ( TickType_t ) 0U; /*lint !e956 Variable is only accessible to one task. */\r
509 \r
510         xTimeNow = xTaskGetTickCount();\r
511 \r
512         if( xTimeNow < xLastTime )\r
513         {\r
514                 prvSwitchTimerLists();\r
515                 *pxTimerListsWereSwitched = pdTRUE;\r
516         }\r
517         else\r
518         {\r
519                 *pxTimerListsWereSwitched = pdFALSE;\r
520         }\r
521 \r
522         xLastTime = xTimeNow;\r
523 \r
524         return xTimeNow;\r
525 }\r
526 /*-----------------------------------------------------------*/\r
527 \r
528 static BaseType_t prvInsertTimerInActiveList( Timer_t * const pxTimer, const TickType_t xNextExpiryTime, const TickType_t xTimeNow, const TickType_t xCommandTime )\r
529 {\r
530 BaseType_t xProcessTimerNow = pdFALSE;\r
531 \r
532         listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xNextExpiryTime );\r
533         listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );\r
534 \r
535         if( xNextExpiryTime <= xTimeNow )\r
536         {\r
537                 /* Has the expiry time elapsed between the command to start/reset a\r
538                 timer was issued, and the time the command was processed? */\r
539                 if( ( xTimeNow - xCommandTime ) >= pxTimer->xTimerPeriodInTicks )\r
540                 {\r
541                         /* The time between a command being issued and the command being\r
542                         processed actually exceeds the timers period.  */\r
543                         xProcessTimerNow = pdTRUE;\r
544                 }\r
545                 else\r
546                 {\r
547                         vListInsert( pxOverflowTimerList, &( pxTimer->xTimerListItem ) );\r
548                 }\r
549         }\r
550         else\r
551         {\r
552                 if( ( xTimeNow < xCommandTime ) && ( xNextExpiryTime >= xCommandTime ) )\r
553                 {\r
554                         /* If, since the command was issued, the tick count has overflowed\r
555                         but the expiry time has not, then the timer must have already passed\r
556                         its expiry time and should be processed immediately. */\r
557                         xProcessTimerNow = pdTRUE;\r
558                 }\r
559                 else\r
560                 {\r
561                         vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );\r
562                 }\r
563         }\r
564 \r
565         return xProcessTimerNow;\r
566 }\r
567 /*-----------------------------------------------------------*/\r
568 \r
569 static void     prvProcessReceivedCommands( void )\r
570 {\r
571 DaemonTaskMessage_t xMessage;\r
572 Timer_t *pxTimer;\r
573 BaseType_t xTimerListsWereSwitched, xResult;\r
574 TickType_t xTimeNow;\r
575 \r
576         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
577         {\r
578                 #if ( INCLUDE_xTimerPendFunctionCall == 1 )\r
579                 {\r
580                         /* Negative commands are pended function calls rather than timer\r
581                         commands. */\r
582                         if( xMessage.xMessageID < 0 )\r
583                         {\r
584                                 const CallbackParameters_t * const pxCallback = &( xMessage.u.xCallbackParameters );\r
585 \r
586                                 /* The timer uses the xCallbackParameters member to request a\r
587                                 callback be executed.  Check the callback is not NULL. */\r
588                                 configASSERT( pxCallback );\r
589 \r
590                                 /* Call the function. */\r
591                                 pxCallback->pxCallbackFunction( pxCallback->pvParameter1, pxCallback->ulParameter2 );\r
592                         }\r
593                         else\r
594                         {\r
595                                 mtCOVERAGE_TEST_MARKER();\r
596                         }\r
597                 }\r
598                 #endif /* INCLUDE_xTimerPendFunctionCall */\r
599 \r
600                 /* Commands that are positive are timer commands rather than pended\r
601                 function calls. */\r
602                 if( xMessage.xMessageID >= 0 )\r
603                 {\r
604                         /* The messages uses the xTimerParameters member to work on a\r
605                         software timer. */\r
606                         pxTimer = xMessage.u.xTimerParameters.pxTimer;\r
607 \r
608                         if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )\r
609                         {\r
610                                 /* The timer is in a list, remove it. */\r
611                                 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );\r
612                         }\r
613                         else\r
614                         {\r
615                                 mtCOVERAGE_TEST_MARKER();\r
616                         }\r
617 \r
618                         traceTIMER_COMMAND_RECEIVED( pxTimer, xMessage.xMessageID, xMessage.u.xTimerParameters.xMessageValue );\r
619 \r
620                         /* In this case the xTimerListsWereSwitched parameter is not used, but\r
621                         it must be present in the function call.  prvSampleTimeNow() must be\r
622                         called after the message is received from xTimerQueue so there is no\r
623                         possibility of a higher priority task adding a message to the message\r
624                         queue with a time that is ahead of the timer daemon task (because it\r
625                         pre-empted the timer daemon task after the xTimeNow value was set). */\r
626                         xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );\r
627 \r
628                         switch( xMessage.xMessageID )\r
629                         {\r
630                                 case tmrCOMMAND_START :\r
631                             case tmrCOMMAND_START_FROM_ISR :\r
632                             case tmrCOMMAND_RESET :\r
633                             case tmrCOMMAND_RESET_FROM_ISR :\r
634                                 case tmrCOMMAND_START_DONT_TRACE :\r
635                                         /* Start or restart a timer. */\r
636                                         if( prvInsertTimerInActiveList( pxTimer,  xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow, xMessage.u.xTimerParameters.xMessageValue ) == pdTRUE )\r
637                                         {\r
638                                                 /* The timer expired before it was added to the active\r
639                                                 timer list.  Process it now. */\r
640                                                 pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );\r
641                                                 traceTIMER_EXPIRED( pxTimer );\r
642 \r
643                                                 if( pxTimer->uxAutoReload == ( UBaseType_t ) pdTRUE )\r
644                                                 {\r
645                                                         xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START_DONT_TRACE, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, NULL, tmrNO_DELAY );\r
646                                                         configASSERT( xResult );\r
647                                                         ( void ) xResult;\r
648                                                 }\r
649                                                 else\r
650                                                 {\r
651                                                         mtCOVERAGE_TEST_MARKER();\r
652                                                 }\r
653                                         }\r
654                                         else\r
655                                         {\r
656                                                 mtCOVERAGE_TEST_MARKER();\r
657                                         }\r
658                                         break;\r
659 \r
660                                 case tmrCOMMAND_STOP :\r
661                                 case tmrCOMMAND_STOP_FROM_ISR :\r
662                                         /* The timer has already been removed from the active list.\r
663                                         There is nothing to do here. */\r
664                                         break;\r
665 \r
666                                 case tmrCOMMAND_CHANGE_PERIOD :\r
667                                 case tmrCOMMAND_CHANGE_PERIOD_FROM_ISR :\r
668                                         pxTimer->xTimerPeriodInTicks = xMessage.u.xTimerParameters.xMessageValue;\r
669                                         configASSERT( ( pxTimer->xTimerPeriodInTicks > 0 ) );\r
670 \r
671                                         /* The new period does not really have a reference, and can be\r
672                                         longer or shorter than the old one.  The command time is\r
673                                         therefore set to the current time, and as the period cannot be\r
674                                         zero the next expiry time can only be in the future, meaning\r
675                                         (unlike for the xTimerStart() case above) there is no fail case\r
676                                         that needs to be handled here. */\r
677                                         ( void ) prvInsertTimerInActiveList( pxTimer, ( xTimeNow + pxTimer->xTimerPeriodInTicks ), xTimeNow, xTimeNow );\r
678                                         break;\r
679 \r
680                                 case tmrCOMMAND_DELETE :\r
681                                         /* The timer has already been removed from the active list,\r
682                                         just free up the memory. */\r
683                                         vPortFree( pxTimer );\r
684                                         break;\r
685 \r
686                                 default :\r
687                                         /* Don't expect to get here. */\r
688                                         break;\r
689                         }\r
690                 }\r
691         }\r
692 }\r
693 /*-----------------------------------------------------------*/\r
694 \r
695 static void prvSwitchTimerLists( void )\r
696 {\r
697 TickType_t xNextExpireTime, xReloadTime;\r
698 List_t *pxTemp;\r
699 Timer_t *pxTimer;\r
700 BaseType_t xResult;\r
701 \r
702         /* The tick count has overflowed.  The timer lists must be switched.\r
703         If there are any timers still referenced from the current timer list\r
704         then they must have expired and should be processed before the lists\r
705         are switched. */\r
706         while( listLIST_IS_EMPTY( pxCurrentTimerList ) == pdFALSE )\r
707         {\r
708                 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );\r
709 \r
710                 /* Remove the timer from the list. */\r
711                 pxTimer = ( Timer_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );\r
712                 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );\r
713                 traceTIMER_EXPIRED( pxTimer );\r
714 \r
715                 /* Execute its callback, then send a command to restart the timer if\r
716                 it is an auto-reload timer.  It cannot be restarted here as the lists\r
717                 have not yet been switched. */\r
718                 pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );\r
719 \r
720                 if( pxTimer->uxAutoReload == ( UBaseType_t ) pdTRUE )\r
721                 {\r
722                         /* Calculate the reload value, and if the reload value results in\r
723                         the timer going into the same timer list then it has already expired\r
724                         and the timer should be re-inserted into the current list so it is\r
725                         processed again within this loop.  Otherwise a command should be sent\r
726                         to restart the timer to ensure it is only inserted into a list after\r
727                         the lists have been swapped. */\r
728                         xReloadTime = ( xNextExpireTime + pxTimer->xTimerPeriodInTicks );\r
729                         if( xReloadTime > xNextExpireTime )\r
730                         {\r
731                                 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xReloadTime );\r
732                                 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );\r
733                                 vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );\r
734                         }\r
735                         else\r
736                         {\r
737                                 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START_DONT_TRACE, xNextExpireTime, NULL, tmrNO_DELAY );\r
738                                 configASSERT( xResult );\r
739                                 ( void ) xResult;\r
740                         }\r
741                 }\r
742                 else\r
743                 {\r
744                         mtCOVERAGE_TEST_MARKER();\r
745                 }\r
746         }\r
747 \r
748         pxTemp = pxCurrentTimerList;\r
749         pxCurrentTimerList = pxOverflowTimerList;\r
750         pxOverflowTimerList = pxTemp;\r
751 }\r
752 /*-----------------------------------------------------------*/\r
753 \r
754 static void prvCheckForValidListAndQueue( void )\r
755 {\r
756         /* Check that the list from which active timers are referenced, and the\r
757         queue used to communicate with the timer service, have been\r
758         initialised. */\r
759         taskENTER_CRITICAL();\r
760         {\r
761                 if( xTimerQueue == NULL )\r
762                 {\r
763                         vListInitialise( &xActiveTimerList1 );\r
764                         vListInitialise( &xActiveTimerList2 );\r
765                         pxCurrentTimerList = &xActiveTimerList1;\r
766                         pxOverflowTimerList = &xActiveTimerList2;\r
767                         xTimerQueue = xQueueCreate( ( UBaseType_t ) configTIMER_QUEUE_LENGTH, sizeof( DaemonTaskMessage_t ) );\r
768                         configASSERT( xTimerQueue );\r
769 \r
770                         #if ( configQUEUE_REGISTRY_SIZE > 0 )\r
771                         {\r
772                                 if( xTimerQueue != NULL )\r
773                                 {\r
774                                         vQueueAddToRegistry( xTimerQueue, "TmrQ" );\r
775                                 }\r
776                                 else\r
777                                 {\r
778                                         mtCOVERAGE_TEST_MARKER();\r
779                                 }\r
780                         }\r
781                         #endif /* configQUEUE_REGISTRY_SIZE */\r
782                 }\r
783                 else\r
784                 {\r
785                         mtCOVERAGE_TEST_MARKER();\r
786                 }\r
787         }\r
788         taskEXIT_CRITICAL();\r
789 }\r
790 /*-----------------------------------------------------------*/\r
791 \r
792 BaseType_t xTimerIsTimerActive( TimerHandle_t xTimer )\r
793 {\r
794 BaseType_t xTimerIsInActiveList;\r
795 Timer_t *pxTimer = ( Timer_t * ) xTimer;\r
796 \r
797         /* Is the timer in the list of active timers? */\r
798         taskENTER_CRITICAL();\r
799         {\r
800                 /* Checking to see if it is in the NULL list in effect checks to see if\r
801                 it is referenced from either the current or the overflow timer lists in\r
802                 one go, but the logic has to be reversed, hence the '!'. */\r
803                 xTimerIsInActiveList = !( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) );\r
804         }\r
805         taskEXIT_CRITICAL();\r
806 \r
807         return xTimerIsInActiveList;\r
808 }\r
809 /*-----------------------------------------------------------*/\r
810 \r
811 void *pvTimerGetTimerID( const TimerHandle_t xTimer )\r
812 {\r
813 Timer_t * const pxTimer = ( Timer_t * ) xTimer;\r
814 \r
815         return pxTimer->pvTimerID;\r
816 }\r
817 /*-----------------------------------------------------------*/\r
818 \r
819 #if( INCLUDE_xTimerPendFunctionCall == 1 )\r
820 \r
821         BaseType_t xTimerPendFunctionCallFromISR( PendedFunction_t xFunctionToPend, void *pvParameter1, uint32_t ulParameter2, BaseType_t *pxHigherPriorityTaskWoken )\r
822         {\r
823         DaemonTaskMessage_t xMessage;\r
824         BaseType_t xReturn;\r
825 \r
826                 /* Complete the message with the function parameters and post it to the\r
827                 daemon task. */\r
828                 xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK_FROM_ISR;\r
829                 xMessage.u.xCallbackParameters.pxCallbackFunction = xFunctionToPend;\r
830                 xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;\r
831                 xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;\r
832 \r
833                 xReturn = xQueueSendFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );\r
834                 \r
835                 tracePEND_FUNC_CALL_FROM_ISR( xFunctionToPend, pvParameter1, ulParameter2, xReturn );\r
836 \r
837                 return xReturn;\r
838         }\r
839 \r
840 #endif /* INCLUDE_xTimerPendFunctionCall */\r
841 /*-----------------------------------------------------------*/\r
842 \r
843 #if( INCLUDE_xTimerPendFunctionCall == 1 )\r
844 \r
845         BaseType_t xTimerPendFunctionCall( PendedFunction_t xFunctionToPend, void *pvParameter1, uint32_t ulParameter2, TickType_t xTicksToWait )\r
846         {\r
847         DaemonTaskMessage_t xMessage;\r
848         BaseType_t xReturn;\r
849 \r
850                 /* Complete the message with the function parameters and post it to the\r
851                 daemon task. */\r
852                 xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK;\r
853                 xMessage.u.xCallbackParameters.pxCallbackFunction = xFunctionToPend;\r
854                 xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;\r
855                 xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;\r
856 \r
857                 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xTicksToWait );\r
858 \r
859                 tracePEND_FUNC_CALL( xFunctionToPend, pvParameter1, ulParameter2, xReturn );\r
860                 \r
861                 return xReturn;\r
862         }\r
863 \r
864 #endif /* INCLUDE_xTimerPendFunctionCall */\r
865 /*-----------------------------------------------------------*/\r
866 \r
867 /* This entire source file will be skipped if the application is not configured\r
868 to include software timer functionality.  If you want to include software timer\r
869 functionality then ensure configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */\r
870 #endif /* configUSE_TIMERS == 1 */\r
871 \r
872 \r
873 \r