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