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