]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/timers.c
Add coverage test markers.
[freertos] / FreeRTOS / Source / timers.c
1 /*\r
2     FreeRTOS V7.6.0 - Copyright (C) 2013 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_xTimerPendCallbackFromISR == 1 ) && ( configUSE_TIMERS == 0 )\r
80         #error configUSE_TIMERS must be set to 1 to make the INCLUDE_xTimerPendCallbackFromISR() 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             ( portTickType ) 0U\r
98 \r
99 /* The definition of the timers themselves. */\r
100 typedef struct tmrTimerControl\r
101 {\r
102         const signed char               *pcTimerName;           /*<< Text name.  This is not used by the kernel, it is included simply to make debugging easier. */\r
103         xListItem                               xTimerListItem;         /*<< Standard linked list item as used by all kernel features for event management. */\r
104         portTickType                    xTimerPeriodInTicks;/*<< How quickly and often the timer expires. */\r
105         unsigned portBASE_TYPE  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         tmrTIMER_CALLBACK               pxCallbackFunction;     /*<< The function that will be called when the timer expires. */\r
108 } xTIMER;\r
109 \r
110 /* The definition of messages that can be sent and received on the timer queue.\r
111 Two types of message can be queued - messages that manipulate a software timer,\r
112 and messages that request the execution of a non-timer related callback.  The\r
113 two message types are defined in two separate structures, xTimerParametersType\r
114 and xCallbackParametersType respectively. */\r
115 typedef struct tmrTimerParameters\r
116 {\r
117         portTickType                    xMessageValue;          /*<< An optional value used by a subset of commands, for example, when changing the period of a timer. */\r
118         xTIMER *                                pxTimer;                        /*<< The timer to which the command will be applied. */\r
119 } xTimerParametersType;\r
120 \r
121 \r
122 typedef struct tmrCallbackParameters\r
123 {\r
124         pdAPPLICATION_CALLBACK_CODE     pxCallbackFunction; /* << The callback function to execute. */\r
125         void *pvParameter1;                                                             /* << The value that will be used as the callback functions first parameter. */\r
126         unsigned long ulParameter2;                                             /* << The value that will be used as the callback functions second parameter. */\r
127 } xCallbackParametersType;\r
128 \r
129 /* The structure that contains the two message types, along with an identifier\r
130 that is used to determine which message type is valid. */\r
131 typedef struct tmrTimerQueueMessage\r
132 {\r
133         portBASE_TYPE                   xMessageID;                     /*<< The command being sent to the timer service task. */\r
134         union\r
135         {\r
136                 xTimerParametersType xTimerParameters;\r
137 \r
138                 /* Don't include xCallbackParameters if it is not going to be used as\r
139                 it makes the structure (and therefore the timer queue) larger. */\r
140                 #if ( INCLUDE_xTimerPendCallbackFromISR == 1 )\r
141                         xCallbackParametersType xCallbackParameters;\r
142                 #endif /* INCLUDE_xTimerPendCallbackFromISR */\r
143         } u;\r
144 } xDAEMON_TASK_MESSAGE;\r
145 \r
146 /*lint -e956 A manual analysis and inspection has been used to determine which\r
147 static variables must be declared volatile. */\r
148 \r
149 /* The list in which active timers are stored.  Timers are referenced in expire\r
150 time order, with the nearest expiry time at the front of the list.  Only the\r
151 timer service task is allowed to access these lists. */\r
152 PRIVILEGED_DATA static xList xActiveTimerList1;\r
153 PRIVILEGED_DATA static xList xActiveTimerList2;\r
154 PRIVILEGED_DATA static xList *pxCurrentTimerList;\r
155 PRIVILEGED_DATA static xList *pxOverflowTimerList;\r
156 \r
157 /* A queue that is used to send commands to the timer service task. */\r
158 PRIVILEGED_DATA static xQueueHandle xTimerQueue = NULL;\r
159 \r
160 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )\r
161 \r
162         PRIVILEGED_DATA static xTaskHandle xTimerTaskHandle = NULL;\r
163 \r
164 #endif\r
165 \r
166 /*lint +e956 */\r
167 \r
168 /*-----------------------------------------------------------*/\r
169 \r
170 /*\r
171  * Initialise the infrastructure used by the timer service task if it has not\r
172  * been initialised already.\r
173  */\r
174 static void prvCheckForValidListAndQueue( void ) PRIVILEGED_FUNCTION;\r
175 \r
176 /*\r
177  * The timer service task (daemon).  Timer functionality is controlled by this\r
178  * task.  Other tasks communicate with the timer service task using the\r
179  * xTimerQueue queue.\r
180  */\r
181 static void prvTimerTask( void *pvParameters ) PRIVILEGED_FUNCTION;\r
182 \r
183 /*\r
184  * Called by the timer service task to interpret and process a command it\r
185  * received on the timer queue.\r
186  */\r
187 static void     prvProcessReceivedCommands( void ) PRIVILEGED_FUNCTION;\r
188 \r
189 /*\r
190  * Insert the timer into either xActiveTimerList1, or xActiveTimerList2,\r
191  * depending on if the expire time causes a timer counter overflow.\r
192  */\r
193 static portBASE_TYPE prvInsertTimerInActiveList( xTIMER * const pxTimer, const portTickType xNextExpiryTime, const portTickType xTimeNow, const portTickType xCommandTime ) PRIVILEGED_FUNCTION;\r
194 \r
195 /*\r
196  * An active timer has reached its expire time.  Reload the timer if it is an\r
197  * auto reload timer, then call its callback.\r
198  */\r
199 static void prvProcessExpiredTimer( const portTickType xNextExpireTime, const portTickType xTimeNow ) PRIVILEGED_FUNCTION;\r
200 \r
201 /*\r
202  * The tick count has overflowed.  Switch the timer lists after ensuring the\r
203  * current timer list does not still reference some timers.\r
204  */\r
205 static void prvSwitchTimerLists( void ) PRIVILEGED_FUNCTION;\r
206 \r
207 /*\r
208  * Obtain the current tick count, setting *pxTimerListsWereSwitched to pdTRUE\r
209  * if a tick count overflow occurred since prvSampleTimeNow() was last called.\r
210  */\r
211 static portTickType prvSampleTimeNow( portBASE_TYPE *pxTimerListsWereSwitched ) PRIVILEGED_FUNCTION;\r
212 \r
213 /*\r
214  * If the timer list contains any active timers then return the expire time of\r
215  * the timer that will expire first and set *pxListWasEmpty to false.  If the\r
216  * timer list does not contain any timers then return 0 and set *pxListWasEmpty\r
217  * to pdTRUE.\r
218  */\r
219 static portTickType prvGetNextExpireTime( portBASE_TYPE *pxListWasEmpty ) PRIVILEGED_FUNCTION;\r
220 \r
221 /*\r
222  * If a timer has expired, process it.  Otherwise, block the timer service task\r
223  * until either a timer does expire or a command is received.\r
224  */\r
225 static void prvProcessTimerOrBlockTask( const portTickType xNextExpireTime, const portBASE_TYPE xListWasEmpty ) PRIVILEGED_FUNCTION;\r
226 \r
227 /*-----------------------------------------------------------*/\r
228 \r
229 portBASE_TYPE xTimerCreateTimerTask( void )\r
230 {\r
231 portBASE_TYPE xReturn = pdFAIL;\r
232 \r
233         /* This function is called when the scheduler is started if\r
234         configUSE_TIMERS is set to 1.  Check that the infrastructure used by the\r
235         timer service task has been created/initialised.  If timers have already\r
236         been created then the initialisation will already have been performed. */\r
237         prvCheckForValidListAndQueue();\r
238 \r
239         if( xTimerQueue != NULL )\r
240         {\r
241                 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )\r
242                 {\r
243                         /* Create the timer task, storing its handle in xTimerTaskHandle so\r
244                         it can be returned by the xTimerGetTimerDaemonTaskHandle() function. */\r
245                         xReturn = xTaskCreate( prvTimerTask, ( signed char * ) "Tmr Svc", ( unsigned short ) configTIMER_TASK_STACK_DEPTH, NULL, ( ( unsigned portBASE_TYPE ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT, &xTimerTaskHandle );\r
246                 }\r
247                 #else\r
248                 {\r
249                         /* Create the timer task without storing its handle. */\r
250                         xReturn = xTaskCreate( prvTimerTask, ( signed char * ) "Tmr Svc", ( unsigned short ) configTIMER_TASK_STACK_DEPTH, NULL, ( ( unsigned portBASE_TYPE ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT, NULL);\r
251                 }\r
252                 #endif\r
253         }\r
254         else\r
255         {\r
256                 mtCOVERAGE_TEST_MARKER();\r
257         }\r
258 \r
259         configASSERT( xReturn );\r
260         return xReturn;\r
261 }\r
262 /*-----------------------------------------------------------*/\r
263 \r
264 xTimerHandle xTimerCreate( const signed char * const pcTimerName, portTickType xTimerPeriodInTicks, unsigned portBASE_TYPE uxAutoReload, void *pvTimerID, tmrTIMER_CALLBACK pxCallbackFunction )\r
265 {\r
266 xTIMER *pxNewTimer;\r
267 \r
268         /* Allocate the timer structure. */\r
269         if( xTimerPeriodInTicks == ( portTickType ) 0U )\r
270         {\r
271                 pxNewTimer = NULL;\r
272         }\r
273         else\r
274         {\r
275                 pxNewTimer = ( xTIMER * ) pvPortMalloc( sizeof( xTIMER ) );\r
276                 if( pxNewTimer != NULL )\r
277                 {\r
278                         /* Ensure the infrastructure used by the timer service task has been\r
279                         created/initialised. */\r
280                         prvCheckForValidListAndQueue();\r
281 \r
282                         /* Initialise the timer structure members using the function parameters. */\r
283                         pxNewTimer->pcTimerName = pcTimerName;\r
284                         pxNewTimer->xTimerPeriodInTicks = xTimerPeriodInTicks;\r
285                         pxNewTimer->uxAutoReload = uxAutoReload;\r
286                         pxNewTimer->pvTimerID = pvTimerID;\r
287                         pxNewTimer->pxCallbackFunction = pxCallbackFunction;\r
288                         vListInitialiseItem( &( pxNewTimer->xTimerListItem ) );\r
289 \r
290                         traceTIMER_CREATE( pxNewTimer );\r
291                 }\r
292                 else\r
293                 {\r
294                         traceTIMER_CREATE_FAILED();\r
295                 }\r
296         }\r
297 \r
298         /* 0 is not a valid value for xTimerPeriodInTicks. */\r
299         configASSERT( ( xTimerPeriodInTicks > 0 ) );\r
300 \r
301         return ( xTimerHandle ) pxNewTimer;\r
302 }\r
303 /*-----------------------------------------------------------*/\r
304 \r
305 portBASE_TYPE xTimerGenericCommand( xTimerHandle xTimer, portBASE_TYPE xCommandID, portTickType xOptionalValue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portTickType xBlockTime )\r
306 {\r
307 portBASE_TYPE xReturn = pdFAIL;\r
308 xDAEMON_TASK_MESSAGE xMessage;\r
309 \r
310         /* Send a message to the timer service task to perform a particular action\r
311         on a particular timer definition. */\r
312         if( xTimerQueue != NULL )\r
313         {\r
314                 /* Send a command to the timer service task to start the xTimer timer. */\r
315                 xMessage.xMessageID = xCommandID;\r
316                 xMessage.u.xTimerParameters.xMessageValue = xOptionalValue;\r
317                 xMessage.u.xTimerParameters.pxTimer = ( xTIMER * ) xTimer;\r
318 \r
319                 if( pxHigherPriorityTaskWoken == NULL )\r
320                 {\r
321                         if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )\r
322                         {\r
323                                 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xBlockTime );\r
324                         }\r
325                         else\r
326                         {\r
327                                 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, tmrNO_DELAY );\r
328                         }\r
329                 }\r
330                 else\r
331                 {\r
332                         xReturn = xQueueSendToBackFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );\r
333                 }\r
334 \r
335                 traceTIMER_COMMAND_SEND( xTimer, xCommandID, xOptionalValue, xReturn );\r
336         }\r
337         else\r
338         {\r
339                 mtCOVERAGE_TEST_MARKER();\r
340         }\r
341 \r
342         return xReturn;\r
343 }\r
344 /*-----------------------------------------------------------*/\r
345 \r
346 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )\r
347 \r
348         xTaskHandle xTimerGetTimerDaemonTaskHandle( void )\r
349         {\r
350                 /* If xTimerGetTimerDaemonTaskHandle() is called before the scheduler has been\r
351                 started, then xTimerTaskHandle will be NULL. */\r
352                 configASSERT( ( xTimerTaskHandle != NULL ) );\r
353                 return xTimerTaskHandle;\r
354         }\r
355 \r
356 #endif\r
357 /*-----------------------------------------------------------*/\r
358 \r
359 static void prvProcessExpiredTimer( const portTickType xNextExpireTime, const portTickType xTimeNow )\r
360 {\r
361 portBASE_TYPE xResult;\r
362 xTIMER * const pxTimer = ( xTIMER * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );\r
363 \r
364         /* Remove the timer from the list of active timers.  A check has already\r
365         been performed to ensure the list is not empty. */\r
366         ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );\r
367         traceTIMER_EXPIRED( pxTimer );\r
368 \r
369         /* If the timer is an auto reload timer then calculate the next\r
370         expiry time and re-insert the timer in the list of active timers. */\r
371         if( pxTimer->uxAutoReload == ( unsigned portBASE_TYPE ) pdTRUE )\r
372         {\r
373                 /* The timer is inserted into a list using a time relative to anything\r
374                 other than the current time.  It will therefore be inserted into the\r
375                 correct list relative to the time this task thinks it is now. */\r
376                 if( prvInsertTimerInActiveList( pxTimer, ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ), xTimeNow, xNextExpireTime ) == pdTRUE )\r
377                 {\r
378                         /* The timer expired before it was added to the active timer\r
379                         list.  Reload it now.  */\r
380                         xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START, xNextExpireTime, NULL, tmrNO_DELAY );\r
381                         configASSERT( xResult );\r
382                         ( void ) xResult;\r
383                 }\r
384                 else\r
385                 {\r
386                         mtCOVERAGE_TEST_MARKER();\r
387                 }\r
388         }\r
389         else\r
390         {\r
391                 mtCOVERAGE_TEST_MARKER();\r
392         }\r
393 \r
394         /* Call the timer callback. */\r
395         pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );\r
396 }\r
397 /*-----------------------------------------------------------*/\r
398 \r
399 static void prvTimerTask( void *pvParameters )\r
400 {\r
401 portTickType xNextExpireTime;\r
402 portBASE_TYPE xListWasEmpty;\r
403 \r
404         /* Just to avoid compiler warnings. */\r
405         ( void ) pvParameters;\r
406 \r
407         for( ;; )\r
408         {\r
409                 /* Query the timers list to see if it contains any timers, and if so,\r
410                 obtain the time at which the next timer will expire. */\r
411                 xNextExpireTime = prvGetNextExpireTime( &xListWasEmpty );\r
412 \r
413                 /* If a timer has expired, process it.  Otherwise, block this task\r
414                 until either a timer does expire, or a command is received. */\r
415                 prvProcessTimerOrBlockTask( xNextExpireTime, xListWasEmpty );\r
416 \r
417                 /* Empty the command queue. */\r
418                 prvProcessReceivedCommands();\r
419         }\r
420 }\r
421 /*-----------------------------------------------------------*/\r
422 \r
423 static void prvProcessTimerOrBlockTask( const portTickType xNextExpireTime, const portBASE_TYPE xListWasEmpty )\r
424 {\r
425 portTickType xTimeNow;\r
426 portBASE_TYPE xTimerListsWereSwitched;\r
427 \r
428         vTaskSuspendAll();\r
429         {\r
430                 /* Obtain the time now to make an assessment as to whether the timer\r
431                 has expired or not.  If obtaining the time causes the lists to switch\r
432                 then don't process this timer as any timers that remained in the list\r
433                 when the lists were switched will have been processed within the\r
434                 prvSampleTimeNow() function. */\r
435                 xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );\r
436                 if( xTimerListsWereSwitched == pdFALSE )\r
437                 {\r
438                         /* The tick count has not overflowed, has the timer expired? */\r
439                         if( ( xListWasEmpty == pdFALSE ) && ( xNextExpireTime <= xTimeNow ) )\r
440                         {\r
441                                 ( void ) xTaskResumeAll();\r
442                                 prvProcessExpiredTimer( xNextExpireTime, xTimeNow );\r
443                         }\r
444                         else\r
445                         {\r
446                                 /* The tick count has not overflowed, and the next expire\r
447                                 time has not been reached yet.  This task should therefore\r
448                                 block to wait for the next expire time or a command to be\r
449                                 received - whichever comes first.  The following line cannot\r
450                                 be reached unless xNextExpireTime > xTimeNow, except in the\r
451                                 case when the current timer list is empty. */\r
452                                 vQueueWaitForMessageRestricted( xTimerQueue, ( xNextExpireTime - xTimeNow ) );\r
453 \r
454                                 if( xTaskResumeAll() == pdFALSE )\r
455                                 {\r
456                                         /* Yield to wait for either a command to arrive, or the block time\r
457                                         to expire.  If a command arrived between the critical section being\r
458                                         exited and this yield then the yield will not cause the task\r
459                                         to block. */\r
460                                         portYIELD_WITHIN_API();\r
461                                 }\r
462                                 else\r
463                                 {\r
464                                         mtCOVERAGE_TEST_MARKER();\r
465                                 }\r
466                         }\r
467                 }\r
468                 else\r
469                 {\r
470                         ( void ) xTaskResumeAll();\r
471                 }\r
472         }\r
473 }\r
474 /*-----------------------------------------------------------*/\r
475 \r
476 static portTickType prvGetNextExpireTime( portBASE_TYPE *pxListWasEmpty )\r
477 {\r
478 portTickType xNextExpireTime;\r
479 \r
480         /* Timers are listed in expiry time order, with the head of the list\r
481         referencing the task that will expire first.  Obtain the time at which\r
482         the timer with the nearest expiry time will expire.  If there are no\r
483         active timers then just set the next expire time to 0.  That will cause\r
484         this task to unblock when the tick count overflows, at which point the\r
485         timer lists will be switched and the next expiry time can be\r
486         re-assessed.  */\r
487         *pxListWasEmpty = listLIST_IS_EMPTY( pxCurrentTimerList );\r
488         if( *pxListWasEmpty == pdFALSE )\r
489         {\r
490                 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );\r
491         }\r
492         else\r
493         {\r
494                 /* Ensure the task unblocks when the tick count rolls over. */\r
495                 xNextExpireTime = ( portTickType ) 0U;\r
496         }\r
497 \r
498         return xNextExpireTime;\r
499 }\r
500 /*-----------------------------------------------------------*/\r
501 \r
502 static portTickType prvSampleTimeNow( portBASE_TYPE *pxTimerListsWereSwitched )\r
503 {\r
504 portTickType xTimeNow;\r
505 PRIVILEGED_DATA static portTickType xLastTime = ( portTickType ) 0U; /*lint !e956 Variable is only accessible to one task. */\r
506 \r
507         xTimeNow = xTaskGetTickCount();\r
508 \r
509         if( xTimeNow < xLastTime )\r
510         {\r
511                 prvSwitchTimerLists();\r
512                 *pxTimerListsWereSwitched = pdTRUE;\r
513         }\r
514         else\r
515         {\r
516                 *pxTimerListsWereSwitched = pdFALSE;\r
517         }\r
518 \r
519         xLastTime = xTimeNow;\r
520 \r
521         return xTimeNow;\r
522 }\r
523 /*-----------------------------------------------------------*/\r
524 \r
525 static portBASE_TYPE prvInsertTimerInActiveList( xTIMER * const pxTimer, const portTickType xNextExpiryTime, const portTickType xTimeNow, const portTickType xCommandTime )\r
526 {\r
527 portBASE_TYPE xProcessTimerNow = pdFALSE;\r
528 \r
529         listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xNextExpiryTime );\r
530         listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );\r
531 \r
532         if( xNextExpiryTime <= xTimeNow )\r
533         {\r
534                 /* Has the expiry time elapsed between the command to start/reset a\r
535                 timer was issued, and the time the command was processed? */\r
536                 if( ( xTimeNow - xCommandTime ) >= pxTimer->xTimerPeriodInTicks )\r
537                 {\r
538                         /* The time between a command being issued and the command being\r
539                         processed actually exceeds the timers period.  */\r
540                         xProcessTimerNow = pdTRUE;\r
541                 }\r
542                 else\r
543                 {\r
544                         vListInsert( pxOverflowTimerList, &( pxTimer->xTimerListItem ) );\r
545                 }\r
546         }\r
547         else\r
548         {\r
549                 if( ( xTimeNow < xCommandTime ) && ( xNextExpiryTime >= xCommandTime ) )\r
550                 {\r
551                         /* If, since the command was issued, the tick count has overflowed\r
552                         but the expiry time has not, then the timer must have already passed\r
553                         its expiry time and should be processed immediately. */\r
554                         xProcessTimerNow = pdTRUE;\r
555                 }\r
556                 else\r
557                 {\r
558                         vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );\r
559                 }\r
560         }\r
561 \r
562         return xProcessTimerNow;\r
563 }\r
564 /*-----------------------------------------------------------*/\r
565 \r
566 static void     prvProcessReceivedCommands( void )\r
567 {\r
568 xDAEMON_TASK_MESSAGE xMessage;\r
569 xTIMER *pxTimer;\r
570 portBASE_TYPE xTimerListsWereSwitched, xResult;\r
571 portTickType xTimeNow;\r
572 \r
573         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
574         {\r
575                 #if ( INCLUDE_xTimerPendCallbackFromISR == 1 )\r
576                 {\r
577                         if( xMessage.xMessageID == tmrCOMMAND_EXECUTE_CALLBACK )\r
578                         {\r
579                                 const xCallbackParametersType * const pxCallback = &( xMessage.u.xCallbackParameters );\r
580 \r
581                                 /* The timer uses the xCallbackParameters member to request a\r
582                                 callback be executed.  Check the callback is not NULL. */\r
583                                 configASSERT( pxCallback );\r
584 \r
585                                 /* Call the function. */\r
586                                 pxCallback->pxCallbackFunction( pxCallback->pvParameter1, pxCallback->ulParameter2 );\r
587                         }\r
588                         else\r
589                         {\r
590                                 mtCOVERAGE_TEST_MARKER();\r
591                         }\r
592                 }\r
593                 #endif /* INCLUDE_xTimerPendCallbackFromISR */\r
594 \r
595                 if( xMessage.xMessageID != tmrCOMMAND_EXECUTE_CALLBACK )\r
596                 {\r
597                         /* The messages uses the xTimerParameters member to work on a\r
598                         software timer. */\r
599                         pxTimer = xMessage.u.xTimerParameters.pxTimer;\r
600 \r
601                         if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )\r
602                         {\r
603                                 /* The timer is in a list, remove it. */\r
604                                 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );\r
605                         }\r
606                         else\r
607                         {\r
608                                 mtCOVERAGE_TEST_MARKER();\r
609                         }\r
610 \r
611                         traceTIMER_COMMAND_RECEIVED( pxTimer, xMessage.xMessageID, xMessage.u.xTimerParameters.xMessageValue );\r
612 \r
613                         /* In this case the xTimerListsWereSwitched parameter is not used, but\r
614                         it must be present in the function call.  prvSampleTimeNow() must be\r
615                         called after the message is received from xTimerQueue so there is no\r
616                         possibility of a higher priority task adding a message to the message\r
617                         queue with a time that is ahead of the timer daemon task (because it\r
618                         pre-empted the timer daemon task after the xTimeNow value was set). */\r
619                         xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );\r
620 \r
621                         switch( xMessage.xMessageID )\r
622                         {\r
623                                 case tmrCOMMAND_START :\r
624                                         /* Start or restart a timer. */\r
625                                         if( prvInsertTimerInActiveList( pxTimer,  xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow, xMessage.u.xTimerParameters.xMessageValue ) == pdTRUE )\r
626                                         {\r
627                                                 /* The timer expired before it was added to the active\r
628                                                 timer list.  Process it now. */\r
629                                                 pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );\r
630                                                 traceTIMER_EXPIRED( pxTimer );\r
631 \r
632                                                 if( pxTimer->uxAutoReload == ( unsigned portBASE_TYPE ) pdTRUE )\r
633                                                 {\r
634                                                         xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, NULL, tmrNO_DELAY );\r
635                                                         configASSERT( xResult );\r
636                                                         ( void ) xResult;\r
637                                                 }\r
638                                                 else\r
639                                                 {\r
640                                                         mtCOVERAGE_TEST_MARKER();\r
641                                                 }\r
642                                         }\r
643                                         else\r
644                                         {\r
645                                                 mtCOVERAGE_TEST_MARKER();\r
646                                         }\r
647                                         break;\r
648 \r
649                                 case tmrCOMMAND_STOP :\r
650                                         /* The timer has already been removed from the active list.\r
651                                         There is nothing to do here. */\r
652                                         break;\r
653 \r
654                                 case tmrCOMMAND_CHANGE_PERIOD :\r
655                                         pxTimer->xTimerPeriodInTicks = xMessage.u.xTimerParameters.xMessageValue;\r
656                                         configASSERT( ( pxTimer->xTimerPeriodInTicks > 0 ) );\r
657 \r
658                                         /* The new period does not really have a reference, and can be\r
659                                         longer or shorter than the old one.  The command time is\r
660                                         therefore set to the current time, and as the period cannot be\r
661                                         zero the next expiry time can only be in the future, meaning\r
662                                         (unlike for the xTimerStart() case above) there is no fail case\r
663                                         that needs to be handled here. */\r
664                                         ( void ) prvInsertTimerInActiveList( pxTimer, ( xTimeNow + pxTimer->xTimerPeriodInTicks ), xTimeNow, xTimeNow );\r
665                                         break;\r
666 \r
667                                 case tmrCOMMAND_DELETE :\r
668                                         /* The timer has already been removed from the active list,\r
669                                         just free up the memory. */\r
670                                         vPortFree( pxTimer );\r
671                                         break;\r
672 \r
673                                 default :\r
674                                         /* Don't expect to get here. */\r
675                                         break;\r
676                         }\r
677                 }\r
678         }\r
679 }\r
680 /*-----------------------------------------------------------*/\r
681 \r
682 static void prvSwitchTimerLists( void )\r
683 {\r
684 portTickType xNextExpireTime, xReloadTime;\r
685 xList *pxTemp;\r
686 xTIMER *pxTimer;\r
687 portBASE_TYPE xResult;\r
688 \r
689         /* The tick count has overflowed.  The timer lists must be switched.\r
690         If there are any timers still referenced from the current timer list\r
691         then they must have expired and should be processed before the lists\r
692         are switched. */\r
693         while( listLIST_IS_EMPTY( pxCurrentTimerList ) == pdFALSE )\r
694         {\r
695                 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );\r
696 \r
697                 /* Remove the timer from the list. */\r
698                 pxTimer = ( xTIMER * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );\r
699                 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );\r
700                 traceTIMER_EXPIRED( pxTimer );\r
701 \r
702                 /* Execute its callback, then send a command to restart the timer if\r
703                 it is an auto-reload timer.  It cannot be restarted here as the lists\r
704                 have not yet been switched. */\r
705                 pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );\r
706 \r
707                 if( pxTimer->uxAutoReload == ( unsigned portBASE_TYPE ) pdTRUE )\r
708                 {\r
709                         /* Calculate the reload value, and if the reload value results in\r
710                         the timer going into the same timer list then it has already expired\r
711                         and the timer should be re-inserted into the current list so it is\r
712                         processed again within this loop.  Otherwise a command should be sent\r
713                         to restart the timer to ensure it is only inserted into a list after\r
714                         the lists have been swapped. */\r
715                         xReloadTime = ( xNextExpireTime + pxTimer->xTimerPeriodInTicks );\r
716                         if( xReloadTime > xNextExpireTime )\r
717                         {\r
718                                 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xReloadTime );\r
719                                 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );\r
720                                 vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );\r
721                         }\r
722                         else\r
723                         {\r
724                                 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START, xNextExpireTime, NULL, tmrNO_DELAY );\r
725                                 configASSERT( xResult );\r
726                                 ( void ) xResult;\r
727                         }\r
728                 }\r
729                 else\r
730                 {\r
731                         mtCOVERAGE_TEST_MARKER();\r
732                 }\r
733         }\r
734 \r
735         pxTemp = pxCurrentTimerList;\r
736         pxCurrentTimerList = pxOverflowTimerList;\r
737         pxOverflowTimerList = pxTemp;\r
738 }\r
739 /*-----------------------------------------------------------*/\r
740 \r
741 static void prvCheckForValidListAndQueue( void )\r
742 {\r
743         /* Check that the list from which active timers are referenced, and the\r
744         queue used to communicate with the timer service, have been\r
745         initialised. */\r
746         taskENTER_CRITICAL();\r
747         {\r
748                 if( xTimerQueue == NULL )\r
749                 {\r
750                         vListInitialise( &xActiveTimerList1 );\r
751                         vListInitialise( &xActiveTimerList2 );\r
752                         pxCurrentTimerList = &xActiveTimerList1;\r
753                         pxOverflowTimerList = &xActiveTimerList2;\r
754                         xTimerQueue = xQueueCreate( ( unsigned portBASE_TYPE ) configTIMER_QUEUE_LENGTH, sizeof( xDAEMON_TASK_MESSAGE ) );\r
755                         configASSERT( xTimerQueue );\r
756 \r
757                         #if ( configQUEUE_REGISTRY_SIZE > 0 )\r
758                         {\r
759                                 if( xTimerQueue != NULL )\r
760                                 {\r
761                                         vQueueAddToRegistry( xTimerQueue, ( signed char * ) "TmrQ" );\r
762                                 }\r
763                                 else\r
764                                 {\r
765                                         mtCOVERAGE_TEST_MARKER();\r
766                                 }\r
767                         }\r
768                         #endif /* configQUEUE_REGISTRY_SIZE */\r
769                 }\r
770                 else\r
771                 {\r
772                         mtCOVERAGE_TEST_MARKER();\r
773                 }\r
774         }\r
775         taskEXIT_CRITICAL();\r
776 }\r
777 /*-----------------------------------------------------------*/\r
778 \r
779 portBASE_TYPE xTimerIsTimerActive( xTimerHandle xTimer )\r
780 {\r
781 portBASE_TYPE xTimerIsInActiveList;\r
782 xTIMER *pxTimer = ( xTIMER * ) xTimer;\r
783 \r
784         /* Is the timer in the list of active timers? */\r
785         taskENTER_CRITICAL();\r
786         {\r
787                 /* Checking to see if it is in the NULL list in effect checks to see if\r
788                 it is referenced from either the current or the overflow timer lists in\r
789                 one go, but the logic has to be reversed, hence the '!'. */\r
790                 xTimerIsInActiveList = !( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) );\r
791         }\r
792         taskEXIT_CRITICAL();\r
793 \r
794         return xTimerIsInActiveList;\r
795 }\r
796 /*-----------------------------------------------------------*/\r
797 \r
798 void *pvTimerGetTimerID( const xTimerHandle xTimer )\r
799 {\r
800 xTIMER * const pxTimer = ( xTIMER * ) xTimer;\r
801 \r
802         return pxTimer->pvTimerID;\r
803 }\r
804 /*-----------------------------------------------------------*/\r
805 \r
806 #if( INCLUDE_xTimerPendCallbackFromISR == 1 )\r
807 \r
808         portBASE_TYPE xTimerPendCallbackFromISR( pdAPPLICATION_CALLBACK_CODE pvCallbackFunction, void *pvParameter1, unsigned long ulParameter2, portBASE_TYPE *pxHigherPriorityTaskWoken )\r
809         {\r
810         xDAEMON_TASK_MESSAGE xMessage;\r
811         portBASE_TYPE xReturn;\r
812 \r
813                 /* Complete the message with the function parameters and post it to the\r
814                 daemon task. */\r
815                 xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK;\r
816                 xMessage.u.xCallbackParameters.pxCallbackFunction = pvCallbackFunction;\r
817                 xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;\r
818                 xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;\r
819 \r
820                 xReturn = xQueueSendFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );\r
821 \r
822                 return xReturn;\r
823         }\r
824 \r
825 #endif /* INCLUDE_xTimerPendCallbackFromISR */\r
826 /*-----------------------------------------------------------*/\r
827 \r
828 /* This entire source file will be skipped if the application is not configured\r
829 to include software timer functionality.  If you want to include software timer\r
830 functionality then ensure configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */\r
831 #endif /* configUSE_TIMERS == 1 */\r
832 \r
833 \r
834 \r