]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/timers.c
Ensure the code builds when configSUPPORT_STATIC_ALLOCATION is 0.
[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 \r
116         #if( configSUPPORT_STATIC_ALLOCATION == 1 )\r
117                 uint8_t                         ucStaticallyAllocated; /*<< Set to pdTRUE if the timer was created from a StaticTimer_t structure, and pdFALSE if the timer structure was allocated dynamically. */\r
118         #endif\r
119 } xTIMER;\r
120 \r
121 /* The old xTIMER name is maintained above then typedefed to the new Timer_t\r
122 name below to enable the use of older kernel aware debuggers. */\r
123 typedef xTIMER Timer_t;\r
124 \r
125 /* The definition of messages that can be sent and received on the timer queue.\r
126 Two types of message can be queued - messages that manipulate a software timer,\r
127 and messages that request the execution of a non-timer related callback.  The\r
128 two message types are defined in two separate structures, xTimerParametersType\r
129 and xCallbackParametersType respectively. */\r
130 typedef struct tmrTimerParameters\r
131 {\r
132         TickType_t                      xMessageValue;          /*<< An optional value used by a subset of commands, for example, when changing the period of a timer. */\r
133         Timer_t *                       pxTimer;                        /*<< The timer to which the command will be applied. */\r
134 } TimerParameter_t;\r
135 \r
136 \r
137 typedef struct tmrCallbackParameters\r
138 {\r
139         PendedFunction_t        pxCallbackFunction;     /* << The callback function to execute. */\r
140         void *pvParameter1;                                             /* << The value that will be used as the callback functions first parameter. */\r
141         uint32_t ulParameter2;                                  /* << The value that will be used as the callback functions second parameter. */\r
142 } CallbackParameters_t;\r
143 \r
144 /* The structure that contains the two message types, along with an identifier\r
145 that is used to determine which message type is valid. */\r
146 typedef struct tmrTimerQueueMessage\r
147 {\r
148         BaseType_t                      xMessageID;                     /*<< The command being sent to the timer service task. */\r
149         union\r
150         {\r
151                 TimerParameter_t xTimerParameters;\r
152 \r
153                 /* Don't include xCallbackParameters if it is not going to be used as\r
154                 it makes the structure (and therefore the timer queue) larger. */\r
155                 #if ( INCLUDE_xTimerPendFunctionCall == 1 )\r
156                         CallbackParameters_t xCallbackParameters;\r
157                 #endif /* INCLUDE_xTimerPendFunctionCall */\r
158         } u;\r
159 } DaemonTaskMessage_t;\r
160 \r
161 /*lint -e956 A manual analysis and inspection has been used to determine which\r
162 static variables must be declared volatile. */\r
163 \r
164 /* The list in which active timers are stored.  Timers are referenced in expire\r
165 time order, with the nearest expiry time at the front of the list.  Only the\r
166 timer service task is allowed to access these lists. */\r
167 PRIVILEGED_DATA static List_t xActiveTimerList1;\r
168 PRIVILEGED_DATA static List_t xActiveTimerList2;\r
169 PRIVILEGED_DATA static List_t *pxCurrentTimerList;\r
170 PRIVILEGED_DATA static List_t *pxOverflowTimerList;\r
171 \r
172 /* A queue that is used to send commands to the timer service task. */\r
173 PRIVILEGED_DATA static QueueHandle_t xTimerQueue = NULL;\r
174 \r
175 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )\r
176 \r
177         PRIVILEGED_DATA static TaskHandle_t xTimerTaskHandle = NULL;\r
178 \r
179 #endif\r
180 \r
181 /*lint +e956 */\r
182 \r
183 /*-----------------------------------------------------------*/\r
184 \r
185 #if( configSUPPORT_STATIC_ALLOCATION == 1 )\r
186 \r
187         /* If static allocation is supported then the application must provide the\r
188         following callback function - which enables the application to optionally\r
189         provide the memory that will be used by the timer task as the task's stack\r
190         and TCB. */\r
191         extern void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint16_t *pusTimerTaskStackSize );\r
192 \r
193 #endif\r
194 \r
195 /*\r
196  * Initialise the infrastructure used by the timer service task if it has not\r
197  * been initialised already.\r
198  */\r
199 static void prvCheckForValidListAndQueue( void ) PRIVILEGED_FUNCTION;\r
200 \r
201 /*\r
202  * The timer service task (daemon).  Timer functionality is controlled by this\r
203  * task.  Other tasks communicate with the timer service task using the\r
204  * xTimerQueue queue.\r
205  */\r
206 static void prvTimerTask( void *pvParameters ) PRIVILEGED_FUNCTION;\r
207 \r
208 /*\r
209  * Called by the timer service task to interpret and process a command it\r
210  * received on the timer queue.\r
211  */\r
212 static void prvProcessReceivedCommands( void ) PRIVILEGED_FUNCTION;\r
213 \r
214 /*\r
215  * Insert the timer into either xActiveTimerList1, or xActiveTimerList2,\r
216  * depending on if the expire time causes a timer counter overflow.\r
217  */\r
218 static BaseType_t prvInsertTimerInActiveList( Timer_t * const pxTimer, const TickType_t xNextExpiryTime, const TickType_t xTimeNow, const TickType_t xCommandTime ) PRIVILEGED_FUNCTION;\r
219 \r
220 /*\r
221  * An active timer has reached its expire time.  Reload the timer if it is an\r
222  * auto reload timer, then call its callback.\r
223  */\r
224 static void prvProcessExpiredTimer( const TickType_t xNextExpireTime, const TickType_t xTimeNow ) PRIVILEGED_FUNCTION;\r
225 \r
226 /*\r
227  * The tick count has overflowed.  Switch the timer lists after ensuring the\r
228  * current timer list does not still reference some timers.\r
229  */\r
230 static void prvSwitchTimerLists( void ) PRIVILEGED_FUNCTION;\r
231 \r
232 /*\r
233  * Obtain the current tick count, setting *pxTimerListsWereSwitched to pdTRUE\r
234  * if a tick count overflow occurred since prvSampleTimeNow() was last called.\r
235  */\r
236 static TickType_t prvSampleTimeNow( BaseType_t * const pxTimerListsWereSwitched ) PRIVILEGED_FUNCTION;\r
237 \r
238 /*\r
239  * If the timer list contains any active timers then return the expire time of\r
240  * the timer that will expire first and set *pxListWasEmpty to false.  If the\r
241  * timer list does not contain any timers then return 0 and set *pxListWasEmpty\r
242  * to pdTRUE.\r
243  */\r
244 static TickType_t prvGetNextExpireTime( BaseType_t * const pxListWasEmpty ) PRIVILEGED_FUNCTION;\r
245 \r
246 /*\r
247  * If a timer has expired, process it.  Otherwise, block the timer service task\r
248  * until either a timer does expire or a command is received.\r
249  */\r
250 static void prvProcessTimerOrBlockTask( const TickType_t xNextExpireTime, BaseType_t xListWasEmpty ) PRIVILEGED_FUNCTION;\r
251 \r
252 /*-----------------------------------------------------------*/\r
253 \r
254 BaseType_t xTimerCreateTimerTask( void )\r
255 {\r
256 BaseType_t xReturn = pdFAIL;\r
257 StaticTask_t *pxTimerTaskTCBBuffer = NULL;\r
258 StackType_t *pxTimerTaskStackBuffer = NULL;\r
259 uint16_t usTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;\r
260 \r
261 \r
262         /* This function is called when the scheduler is started if\r
263         configUSE_TIMERS is set to 1.  Check that the infrastructure used by the\r
264         timer service task has been created/initialised.  If timers have already\r
265         been created then the initialisation will already have been performed. */\r
266         prvCheckForValidListAndQueue();\r
267 \r
268         if( xTimerQueue != NULL )\r
269         {\r
270 \r
271                 #if( configSUPPORT_STATIC_ALLOCATION == 1 )\r
272                 {\r
273                         vApplicationGetTimerTaskMemory( &pxTimerTaskTCBBuffer, &pxTimerTaskStackBuffer, &usTimerTaskStackSize );\r
274                 }\r
275                 #endif /* configSUPPORT_STATIC_ALLOCATION */\r
276 \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 = xTaskGenericCreate( prvTimerTask, "Tmr Svc", usTimerTaskStackSize, NULL, ( ( UBaseType_t ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT, &xTimerTaskHandle, pxTimerTaskStackBuffer, pxTimerTaskTCBBuffer, NULL );\r
283                 }\r
284                 #else\r
285                 {\r
286                         /* Create the timer task without storing its handle. */\r
287                         xReturn = xTaskGenericCreate( prvTimerTask, "Tmr Svc", usTimerTaskStackSize, NULL, ( ( UBaseType_t ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT, NULL, pxTimerTaskStackBuffer, pxTimerTaskTCBBuffer, 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 xTimerGenericCreate( const char * const pcTimerName, const TickType_t xTimerPeriodInTicks, const UBaseType_t uxAutoReload, void * const pvTimerID, TimerCallbackFunction_t pxCallbackFunction, StaticTimer_t *pxTimerBuffer ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */\r
302 {\r
303 Timer_t *pxNewTimer;\r
304 \r
305         #if( ( configASSERT_DEFINED == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )\r
306         {\r
307                 /* Sanity check that the size of the structure used to declare a\r
308                 variable of type StaticTimer_t equals the size of the real timer\r
309                 structures. */\r
310                 volatile size_t xSize = sizeof( StaticTimer_t );\r
311                 configASSERT( xSize == sizeof( Timer_t ) );\r
312         }\r
313         #endif /* configASSERT_DEFINED */\r
314 \r
315         /* Allocate the timer structure. */\r
316         if( xTimerPeriodInTicks == ( TickType_t ) 0U )\r
317         {\r
318                 pxNewTimer = NULL;\r
319         }\r
320         else\r
321         {\r
322                 /* If the user passed in a statically allocated timer structure then use\r
323                 it, otherwise allocate the structure dynamically. */\r
324                 if( pxTimerBuffer == NULL )\r
325                 {\r
326                         pxNewTimer = ( Timer_t * ) pvPortMalloc( sizeof( Timer_t ) );\r
327                 }\r
328                 else\r
329                 {\r
330                         pxNewTimer = ( Timer_t * ) pxTimerBuffer;\r
331                 }\r
332 \r
333                 if( pxNewTimer != NULL )\r
334                 {\r
335                         /* Ensure the infrastructure used by the timer service task has been\r
336                         created/initialised. */\r
337                         prvCheckForValidListAndQueue();\r
338 \r
339                         /* Initialise the timer structure members using the function\r
340                         parameters. */\r
341                         pxNewTimer->pcTimerName = pcTimerName;\r
342                         pxNewTimer->xTimerPeriodInTicks = xTimerPeriodInTicks;\r
343                         pxNewTimer->uxAutoReload = uxAutoReload;\r
344                         pxNewTimer->pvTimerID = pvTimerID;\r
345                         pxNewTimer->pxCallbackFunction = pxCallbackFunction;\r
346                         vListInitialiseItem( &( pxNewTimer->xTimerListItem ) );\r
347 \r
348                         #if( configSUPPORT_STATIC_ALLOCATION == 1 )\r
349                         {\r
350                                 if( pxTimerBuffer == NULL )\r
351                                 {\r
352                                         pxNewTimer->ucStaticallyAllocated = pdFALSE;\r
353                                 }\r
354                                 else\r
355                                 {\r
356                                         pxNewTimer->ucStaticallyAllocated = pdTRUE;\r
357                                 }\r
358                         }\r
359                         #endif /* configSUPPORT_STATIC_ALLOCATION */\r
360 \r
361                         traceTIMER_CREATE( pxNewTimer );\r
362                 }\r
363                 else\r
364                 {\r
365                         traceTIMER_CREATE_FAILED();\r
366                 }\r
367         }\r
368 \r
369         /* 0 is not a valid value for xTimerPeriodInTicks. */\r
370         configASSERT( ( xTimerPeriodInTicks > 0 ) );\r
371 \r
372         return ( TimerHandle_t ) pxNewTimer;\r
373 }\r
374 /*-----------------------------------------------------------*/\r
375 \r
376 BaseType_t xTimerGenericCommand( TimerHandle_t xTimer, const BaseType_t xCommandID, const TickType_t xOptionalValue, BaseType_t * const pxHigherPriorityTaskWoken, const TickType_t xTicksToWait )\r
377 {\r
378 BaseType_t xReturn = pdFAIL;\r
379 DaemonTaskMessage_t xMessage;\r
380 \r
381         configASSERT( xTimer );\r
382 \r
383         /* Send a message to the timer service task to perform a particular action\r
384         on a particular timer definition. */\r
385         if( xTimerQueue != NULL )\r
386         {\r
387                 /* Send a command to the timer service task to start the xTimer timer. */\r
388                 xMessage.xMessageID = xCommandID;\r
389                 xMessage.u.xTimerParameters.xMessageValue = xOptionalValue;\r
390                 xMessage.u.xTimerParameters.pxTimer = ( Timer_t * ) xTimer;\r
391 \r
392                 if( xCommandID < tmrFIRST_FROM_ISR_COMMAND )\r
393                 {\r
394                         if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )\r
395                         {\r
396                                 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xTicksToWait );\r
397                         }\r
398                         else\r
399                         {\r
400                                 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, tmrNO_DELAY );\r
401                         }\r
402                 }\r
403                 else\r
404                 {\r
405                         xReturn = xQueueSendToBackFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );\r
406                 }\r
407 \r
408                 traceTIMER_COMMAND_SEND( xTimer, xCommandID, xOptionalValue, xReturn );\r
409         }\r
410         else\r
411         {\r
412                 mtCOVERAGE_TEST_MARKER();\r
413         }\r
414 \r
415         return xReturn;\r
416 }\r
417 /*-----------------------------------------------------------*/\r
418 \r
419 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )\r
420 \r
421         TaskHandle_t xTimerGetTimerDaemonTaskHandle( void )\r
422         {\r
423                 /* If xTimerGetTimerDaemonTaskHandle() is called before the scheduler has been\r
424                 started, then xTimerTaskHandle will be NULL. */\r
425                 configASSERT( ( xTimerTaskHandle != NULL ) );\r
426                 return xTimerTaskHandle;\r
427         }\r
428 \r
429 #endif\r
430 /*-----------------------------------------------------------*/\r
431 \r
432 const char * pcTimerGetTimerName( TimerHandle_t xTimer )\r
433 {\r
434 Timer_t *pxTimer = ( Timer_t * ) xTimer;\r
435 \r
436         configASSERT( xTimer );\r
437         return pxTimer->pcTimerName;\r
438 }\r
439 /*-----------------------------------------------------------*/\r
440 \r
441 static void prvProcessExpiredTimer( const TickType_t xNextExpireTime, const TickType_t xTimeNow )\r
442 {\r
443 BaseType_t xResult;\r
444 Timer_t * const pxTimer = ( Timer_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );\r
445 \r
446         /* Remove the timer from the list of active timers.  A check has already\r
447         been performed to ensure the list is not empty. */\r
448         ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );\r
449         traceTIMER_EXPIRED( pxTimer );\r
450 \r
451         /* If the timer is an auto reload timer then calculate the next\r
452         expiry time and re-insert the timer in the list of active timers. */\r
453         if( pxTimer->uxAutoReload == ( UBaseType_t ) pdTRUE )\r
454         {\r
455                 /* The timer is inserted into a list using a time relative to anything\r
456                 other than the current time.  It will therefore be inserted into the\r
457                 correct list relative to the time this task thinks it is now. */\r
458                 if( prvInsertTimerInActiveList( pxTimer, ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ), xTimeNow, xNextExpireTime ) == pdTRUE )\r
459                 {\r
460                         /* The timer expired before it was added to the active timer\r
461                         list.  Reload it now.  */\r
462                         xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START_DONT_TRACE, xNextExpireTime, NULL, tmrNO_DELAY );\r
463                         configASSERT( xResult );\r
464                         ( void ) xResult;\r
465                 }\r
466                 else\r
467                 {\r
468                         mtCOVERAGE_TEST_MARKER();\r
469                 }\r
470         }\r
471         else\r
472         {\r
473                 mtCOVERAGE_TEST_MARKER();\r
474         }\r
475 \r
476         /* Call the timer callback. */\r
477         pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );\r
478 }\r
479 /*-----------------------------------------------------------*/\r
480 \r
481 static void prvTimerTask( void *pvParameters )\r
482 {\r
483 TickType_t xNextExpireTime;\r
484 BaseType_t xListWasEmpty;\r
485 \r
486         /* Just to avoid compiler warnings. */\r
487         ( void ) pvParameters;\r
488 \r
489         for( ;; )\r
490         {\r
491                 /* Query the timers list to see if it contains any timers, and if so,\r
492                 obtain the time at which the next timer will expire. */\r
493                 xNextExpireTime = prvGetNextExpireTime( &xListWasEmpty );\r
494 \r
495                 /* If a timer has expired, process it.  Otherwise, block this task\r
496                 until either a timer does expire, or a command is received. */\r
497                 prvProcessTimerOrBlockTask( xNextExpireTime, xListWasEmpty );\r
498 \r
499                 /* Empty the command queue. */\r
500                 prvProcessReceivedCommands();\r
501         }\r
502 }\r
503 /*-----------------------------------------------------------*/\r
504 \r
505 static void prvProcessTimerOrBlockTask( const TickType_t xNextExpireTime, BaseType_t xListWasEmpty )\r
506 {\r
507 TickType_t xTimeNow;\r
508 BaseType_t xTimerListsWereSwitched;\r
509 \r
510         vTaskSuspendAll();\r
511         {\r
512                 /* Obtain the time now to make an assessment as to whether the timer\r
513                 has expired or not.  If obtaining the time causes the lists to switch\r
514                 then don't process this timer as any timers that remained in the list\r
515                 when the lists were switched will have been processed within the\r
516                 prvSampleTimeNow() function. */\r
517                 xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );\r
518                 if( xTimerListsWereSwitched == pdFALSE )\r
519                 {\r
520                         /* The tick count has not overflowed, has the timer expired? */\r
521                         if( ( xListWasEmpty == pdFALSE ) && ( xNextExpireTime <= xTimeNow ) )\r
522                         {\r
523                                 ( void ) xTaskResumeAll();\r
524                                 prvProcessExpiredTimer( xNextExpireTime, xTimeNow );\r
525                         }\r
526                         else\r
527                         {\r
528                                 /* The tick count has not overflowed, and the next expire\r
529                                 time has not been reached yet.  This task should therefore\r
530                                 block to wait for the next expire time or a command to be\r
531                                 received - whichever comes first.  The following line cannot\r
532                                 be reached unless xNextExpireTime > xTimeNow, except in the\r
533                                 case when the current timer list is empty. */\r
534                                 if( xListWasEmpty != pdFALSE )\r
535                                 {\r
536                                         /* The current timer list is empty - is the overflow list\r
537                                         also empty? */\r
538                                         xListWasEmpty = listLIST_IS_EMPTY( pxOverflowTimerList );\r
539                                 }\r
540 \r
541                                 vQueueWaitForMessageRestricted( xTimerQueue, ( xNextExpireTime - xTimeNow ), xListWasEmpty );\r
542 \r
543                                 if( xTaskResumeAll() == pdFALSE )\r
544                                 {\r
545                                         /* Yield to wait for either a command to arrive, or the\r
546                                         block time to expire.  If a command arrived between the\r
547                                         critical section being exited and this yield then the yield\r
548                                         will not cause the task to block. */\r
549                                         portYIELD_WITHIN_API();\r
550                                 }\r
551                                 else\r
552                                 {\r
553                                         mtCOVERAGE_TEST_MARKER();\r
554                                 }\r
555                         }\r
556                 }\r
557                 else\r
558                 {\r
559                         ( void ) xTaskResumeAll();\r
560                 }\r
561         }\r
562 }\r
563 /*-----------------------------------------------------------*/\r
564 \r
565 static TickType_t prvGetNextExpireTime( BaseType_t * const pxListWasEmpty )\r
566 {\r
567 TickType_t xNextExpireTime;\r
568 \r
569         /* Timers are listed in expiry time order, with the head of the list\r
570         referencing the task that will expire first.  Obtain the time at which\r
571         the timer with the nearest expiry time will expire.  If there are no\r
572         active timers then just set the next expire time to 0.  That will cause\r
573         this task to unblock when the tick count overflows, at which point the\r
574         timer lists will be switched and the next expiry time can be\r
575         re-assessed.  */\r
576         *pxListWasEmpty = listLIST_IS_EMPTY( pxCurrentTimerList );\r
577         if( *pxListWasEmpty == pdFALSE )\r
578         {\r
579                 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );\r
580         }\r
581         else\r
582         {\r
583                 /* Ensure the task unblocks when the tick count rolls over. */\r
584                 xNextExpireTime = ( TickType_t ) 0U;\r
585         }\r
586 \r
587         return xNextExpireTime;\r
588 }\r
589 /*-----------------------------------------------------------*/\r
590 \r
591 static TickType_t prvSampleTimeNow( BaseType_t * const pxTimerListsWereSwitched )\r
592 {\r
593 TickType_t xTimeNow;\r
594 PRIVILEGED_DATA static TickType_t xLastTime = ( TickType_t ) 0U; /*lint !e956 Variable is only accessible to one task. */\r
595 \r
596         xTimeNow = xTaskGetTickCount();\r
597 \r
598         if( xTimeNow < xLastTime )\r
599         {\r
600                 prvSwitchTimerLists();\r
601                 *pxTimerListsWereSwitched = pdTRUE;\r
602         }\r
603         else\r
604         {\r
605                 *pxTimerListsWereSwitched = pdFALSE;\r
606         }\r
607 \r
608         xLastTime = xTimeNow;\r
609 \r
610         return xTimeNow;\r
611 }\r
612 /*-----------------------------------------------------------*/\r
613 \r
614 static BaseType_t prvInsertTimerInActiveList( Timer_t * const pxTimer, const TickType_t xNextExpiryTime, const TickType_t xTimeNow, const TickType_t xCommandTime )\r
615 {\r
616 BaseType_t xProcessTimerNow = pdFALSE;\r
617 \r
618         listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xNextExpiryTime );\r
619         listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );\r
620 \r
621         if( xNextExpiryTime <= xTimeNow )\r
622         {\r
623                 /* Has the expiry time elapsed between the command to start/reset a\r
624                 timer was issued, and the time the command was processed? */\r
625                 if( ( xTimeNow - xCommandTime ) >= pxTimer->xTimerPeriodInTicks )\r
626                 {\r
627                         /* The time between a command being issued and the command being\r
628                         processed actually exceeds the timers period.  */\r
629                         xProcessTimerNow = pdTRUE;\r
630                 }\r
631                 else\r
632                 {\r
633                         vListInsert( pxOverflowTimerList, &( pxTimer->xTimerListItem ) );\r
634                 }\r
635         }\r
636         else\r
637         {\r
638                 if( ( xTimeNow < xCommandTime ) && ( xNextExpiryTime >= xCommandTime ) )\r
639                 {\r
640                         /* If, since the command was issued, the tick count has overflowed\r
641                         but the expiry time has not, then the timer must have already passed\r
642                         its expiry time and should be processed immediately. */\r
643                         xProcessTimerNow = pdTRUE;\r
644                 }\r
645                 else\r
646                 {\r
647                         vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );\r
648                 }\r
649         }\r
650 \r
651         return xProcessTimerNow;\r
652 }\r
653 /*-----------------------------------------------------------*/\r
654 \r
655 static void     prvProcessReceivedCommands( void )\r
656 {\r
657 DaemonTaskMessage_t xMessage;\r
658 Timer_t *pxTimer;\r
659 BaseType_t xTimerListsWereSwitched, xResult;\r
660 TickType_t xTimeNow;\r
661 \r
662         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
663         {\r
664                 #if ( INCLUDE_xTimerPendFunctionCall == 1 )\r
665                 {\r
666                         /* Negative commands are pended function calls rather than timer\r
667                         commands. */\r
668                         if( xMessage.xMessageID < ( BaseType_t ) 0 )\r
669                         {\r
670                                 const CallbackParameters_t * const pxCallback = &( xMessage.u.xCallbackParameters );\r
671 \r
672                                 /* The timer uses the xCallbackParameters member to request a\r
673                                 callback be executed.  Check the callback is not NULL. */\r
674                                 configASSERT( pxCallback );\r
675 \r
676                                 /* Call the function. */\r
677                                 pxCallback->pxCallbackFunction( pxCallback->pvParameter1, pxCallback->ulParameter2 );\r
678                         }\r
679                         else\r
680                         {\r
681                                 mtCOVERAGE_TEST_MARKER();\r
682                         }\r
683                 }\r
684                 #endif /* INCLUDE_xTimerPendFunctionCall */\r
685 \r
686                 /* Commands that are positive are timer commands rather than pended\r
687                 function calls. */\r
688                 if( xMessage.xMessageID >= ( BaseType_t ) 0 )\r
689                 {\r
690                         /* The messages uses the xTimerParameters member to work on a\r
691                         software timer. */\r
692                         pxTimer = xMessage.u.xTimerParameters.pxTimer;\r
693 \r
694                         if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )\r
695                         {\r
696                                 /* The timer is in a list, remove it. */\r
697                                 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );\r
698                         }\r
699                         else\r
700                         {\r
701                                 mtCOVERAGE_TEST_MARKER();\r
702                         }\r
703 \r
704                         traceTIMER_COMMAND_RECEIVED( pxTimer, xMessage.xMessageID, xMessage.u.xTimerParameters.xMessageValue );\r
705 \r
706                         /* In this case the xTimerListsWereSwitched parameter is not used, but\r
707                         it must be present in the function call.  prvSampleTimeNow() must be\r
708                         called after the message is received from xTimerQueue so there is no\r
709                         possibility of a higher priority task adding a message to the message\r
710                         queue with a time that is ahead of the timer daemon task (because it\r
711                         pre-empted the timer daemon task after the xTimeNow value was set). */\r
712                         xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );\r
713 \r
714                         switch( xMessage.xMessageID )\r
715                         {\r
716                                 case tmrCOMMAND_START :\r
717                             case tmrCOMMAND_START_FROM_ISR :\r
718                             case tmrCOMMAND_RESET :\r
719                             case tmrCOMMAND_RESET_FROM_ISR :\r
720                                 case tmrCOMMAND_START_DONT_TRACE :\r
721                                         /* Start or restart a timer. */\r
722                                         if( prvInsertTimerInActiveList( pxTimer,  xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow, xMessage.u.xTimerParameters.xMessageValue ) == pdTRUE )\r
723                                         {\r
724                                                 /* The timer expired before it was added to the active\r
725                                                 timer list.  Process it now. */\r
726                                                 pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );\r
727                                                 traceTIMER_EXPIRED( pxTimer );\r
728 \r
729                                                 if( pxTimer->uxAutoReload == ( UBaseType_t ) pdTRUE )\r
730                                                 {\r
731                                                         xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START_DONT_TRACE, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, NULL, tmrNO_DELAY );\r
732                                                         configASSERT( xResult );\r
733                                                         ( void ) xResult;\r
734                                                 }\r
735                                                 else\r
736                                                 {\r
737                                                         mtCOVERAGE_TEST_MARKER();\r
738                                                 }\r
739                                         }\r
740                                         else\r
741                                         {\r
742                                                 mtCOVERAGE_TEST_MARKER();\r
743                                         }\r
744                                         break;\r
745 \r
746                                 case tmrCOMMAND_STOP :\r
747                                 case tmrCOMMAND_STOP_FROM_ISR :\r
748                                         /* The timer has already been removed from the active list.\r
749                                         There is nothing to do here. */\r
750                                         break;\r
751 \r
752                                 case tmrCOMMAND_CHANGE_PERIOD :\r
753                                 case tmrCOMMAND_CHANGE_PERIOD_FROM_ISR :\r
754                                         pxTimer->xTimerPeriodInTicks = xMessage.u.xTimerParameters.xMessageValue;\r
755                                         configASSERT( ( pxTimer->xTimerPeriodInTicks > 0 ) );\r
756 \r
757                                         /* The new period does not really have a reference, and can\r
758                                         be longer or shorter than the old one.  The command time is\r
759                                         therefore set to the current time, and as the period cannot\r
760                                         be zero the next expiry time can only be in the future,\r
761                                         meaning (unlike for the xTimerStart() case above) there is\r
762                                         no fail case that needs to be handled here. */\r
763                                         ( void ) prvInsertTimerInActiveList( pxTimer, ( xTimeNow + pxTimer->xTimerPeriodInTicks ), xTimeNow, xTimeNow );\r
764                                         break;\r
765 \r
766                                 case tmrCOMMAND_DELETE :\r
767                                         /* The timer has already been removed from the active list,\r
768                                         just free up the memory if the memory was dynamically\r
769                                         allocated. */\r
770                                         #if( configSUPPORT_STATIC_ALLOCATION == 1 )\r
771                                         {\r
772                                                 if( pxTimer->ucStaticallyAllocated == pdFALSE )\r
773                                                 {\r
774                                                         vPortFree( pxTimer );\r
775                                                 }\r
776                                                 else\r
777                                                 {\r
778                                                         mtCOVERAGE_TEST_MARKER();\r
779                                                 }\r
780                                         }\r
781                                         #else\r
782                                         {\r
783                                                 vPortFree( pxTimer );\r
784                                         }\r
785                                         #endif /* configSUPPORT_STATIC_ALLOCATION */\r
786                                         break;\r
787 \r
788                                 default :\r
789                                         /* Don't expect to get here. */\r
790                                         break;\r
791                         }\r
792                 }\r
793         }\r
794 }\r
795 /*-----------------------------------------------------------*/\r
796 \r
797 static void prvSwitchTimerLists( void )\r
798 {\r
799 TickType_t xNextExpireTime, xReloadTime;\r
800 List_t *pxTemp;\r
801 Timer_t *pxTimer;\r
802 BaseType_t xResult;\r
803 \r
804         /* The tick count has overflowed.  The timer lists must be switched.\r
805         If there are any timers still referenced from the current timer list\r
806         then they must have expired and should be processed before the lists\r
807         are switched. */\r
808         while( listLIST_IS_EMPTY( pxCurrentTimerList ) == pdFALSE )\r
809         {\r
810                 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );\r
811 \r
812                 /* Remove the timer from the list. */\r
813                 pxTimer = ( Timer_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );\r
814                 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );\r
815                 traceTIMER_EXPIRED( pxTimer );\r
816 \r
817                 /* Execute its callback, then send a command to restart the timer if\r
818                 it is an auto-reload timer.  It cannot be restarted here as the lists\r
819                 have not yet been switched. */\r
820                 pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );\r
821 \r
822                 if( pxTimer->uxAutoReload == ( UBaseType_t ) pdTRUE )\r
823                 {\r
824                         /* Calculate the reload value, and if the reload value results in\r
825                         the timer going into the same timer list then it has already expired\r
826                         and the timer should be re-inserted into the current list so it is\r
827                         processed again within this loop.  Otherwise a command should be sent\r
828                         to restart the timer to ensure it is only inserted into a list after\r
829                         the lists have been swapped. */\r
830                         xReloadTime = ( xNextExpireTime + pxTimer->xTimerPeriodInTicks );\r
831                         if( xReloadTime > xNextExpireTime )\r
832                         {\r
833                                 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xReloadTime );\r
834                                 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );\r
835                                 vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );\r
836                         }\r
837                         else\r
838                         {\r
839                                 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START_DONT_TRACE, xNextExpireTime, NULL, tmrNO_DELAY );\r
840                                 configASSERT( xResult );\r
841                                 ( void ) xResult;\r
842                         }\r
843                 }\r
844                 else\r
845                 {\r
846                         mtCOVERAGE_TEST_MARKER();\r
847                 }\r
848         }\r
849 \r
850         pxTemp = pxCurrentTimerList;\r
851         pxCurrentTimerList = pxOverflowTimerList;\r
852         pxOverflowTimerList = pxTemp;\r
853 }\r
854 /*-----------------------------------------------------------*/\r
855 \r
856 static void prvCheckForValidListAndQueue( void )\r
857 {\r
858         /* Check that the list from which active timers are referenced, and the\r
859         queue used to communicate with the timer service, have been\r
860         initialised. */\r
861         taskENTER_CRITICAL();\r
862         {\r
863                 if( xTimerQueue == NULL )\r
864                 {\r
865                         vListInitialise( &xActiveTimerList1 );\r
866                         vListInitialise( &xActiveTimerList2 );\r
867                         pxCurrentTimerList = &xActiveTimerList1;\r
868                         pxOverflowTimerList = &xActiveTimerList2;\r
869                         xTimerQueue = xQueueCreate( ( UBaseType_t ) configTIMER_QUEUE_LENGTH, sizeof( DaemonTaskMessage_t ) );\r
870                         configASSERT( xTimerQueue );\r
871 \r
872                         #if ( configQUEUE_REGISTRY_SIZE > 0 )\r
873                         {\r
874                                 if( xTimerQueue != NULL )\r
875                                 {\r
876                                         vQueueAddToRegistry( xTimerQueue, "TmrQ" );\r
877                                 }\r
878                                 else\r
879                                 {\r
880                                         mtCOVERAGE_TEST_MARKER();\r
881                                 }\r
882                         }\r
883                         #endif /* configQUEUE_REGISTRY_SIZE */\r
884                 }\r
885                 else\r
886                 {\r
887                         mtCOVERAGE_TEST_MARKER();\r
888                 }\r
889         }\r
890         taskEXIT_CRITICAL();\r
891 }\r
892 /*-----------------------------------------------------------*/\r
893 \r
894 BaseType_t xTimerIsTimerActive( TimerHandle_t xTimer )\r
895 {\r
896 BaseType_t xTimerIsInActiveList;\r
897 Timer_t *pxTimer = ( Timer_t * ) xTimer;\r
898 \r
899         configASSERT( xTimer );\r
900 \r
901         /* Is the timer in the list of active timers? */\r
902         taskENTER_CRITICAL();\r
903         {\r
904                 /* Checking to see if it is in the NULL list in effect checks to see if\r
905                 it is referenced from either the current or the overflow timer lists in\r
906                 one go, but the logic has to be reversed, hence the '!'. */\r
907                 xTimerIsInActiveList = ( BaseType_t ) !( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) );\r
908         }\r
909         taskEXIT_CRITICAL();\r
910 \r
911         return xTimerIsInActiveList;\r
912 } /*lint !e818 Can't be pointer to const due to the typedef. */\r
913 /*-----------------------------------------------------------*/\r
914 \r
915 void *pvTimerGetTimerID( const TimerHandle_t xTimer )\r
916 {\r
917 Timer_t * const pxTimer = ( Timer_t * ) xTimer;\r
918 void *pvReturn;\r
919 \r
920         configASSERT( xTimer );\r
921 \r
922         taskENTER_CRITICAL();\r
923         {\r
924                 pvReturn = pxTimer->pvTimerID;\r
925         }\r
926         taskEXIT_CRITICAL();\r
927 \r
928         return pvReturn;\r
929 }\r
930 /*-----------------------------------------------------------*/\r
931 \r
932 void vTimerSetTimerID( TimerHandle_t xTimer, void *pvNewID )\r
933 {\r
934 Timer_t * const pxTimer = ( Timer_t * ) xTimer;\r
935 \r
936         configASSERT( xTimer );\r
937 \r
938         taskENTER_CRITICAL();\r
939         {\r
940                 pxTimer->pvTimerID = pvNewID;\r
941         }\r
942         taskEXIT_CRITICAL();\r
943 }\r
944 /*-----------------------------------------------------------*/\r
945 \r
946 #if( INCLUDE_xTimerPendFunctionCall == 1 )\r
947 \r
948         BaseType_t xTimerPendFunctionCallFromISR( PendedFunction_t xFunctionToPend, void *pvParameter1, uint32_t ulParameter2, BaseType_t *pxHigherPriorityTaskWoken )\r
949         {\r
950         DaemonTaskMessage_t xMessage;\r
951         BaseType_t xReturn;\r
952 \r
953                 /* Complete the message with the function parameters and post it to the\r
954                 daemon task. */\r
955                 xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK_FROM_ISR;\r
956                 xMessage.u.xCallbackParameters.pxCallbackFunction = xFunctionToPend;\r
957                 xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;\r
958                 xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;\r
959 \r
960                 xReturn = xQueueSendFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );\r
961 \r
962                 tracePEND_FUNC_CALL_FROM_ISR( xFunctionToPend, pvParameter1, ulParameter2, xReturn );\r
963 \r
964                 return xReturn;\r
965         }\r
966 \r
967 #endif /* INCLUDE_xTimerPendFunctionCall */\r
968 /*-----------------------------------------------------------*/\r
969 \r
970 #if( INCLUDE_xTimerPendFunctionCall == 1 )\r
971 \r
972         BaseType_t xTimerPendFunctionCall( PendedFunction_t xFunctionToPend, void *pvParameter1, uint32_t ulParameter2, TickType_t xTicksToWait )\r
973         {\r
974         DaemonTaskMessage_t xMessage;\r
975         BaseType_t xReturn;\r
976 \r
977                 /* This function can only be called after a timer has been created or\r
978                 after the scheduler has been started because, until then, the timer\r
979                 queue does not exist. */\r
980                 configASSERT( xTimerQueue );\r
981 \r
982                 /* Complete the message with the function parameters and post it to the\r
983                 daemon task. */\r
984                 xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK;\r
985                 xMessage.u.xCallbackParameters.pxCallbackFunction = xFunctionToPend;\r
986                 xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;\r
987                 xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;\r
988 \r
989                 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xTicksToWait );\r
990 \r
991                 tracePEND_FUNC_CALL( xFunctionToPend, pvParameter1, ulParameter2, xReturn );\r
992 \r
993                 return xReturn;\r
994         }\r
995 \r
996 #endif /* INCLUDE_xTimerPendFunctionCall */\r
997 /*-----------------------------------------------------------*/\r
998 \r
999 /* This entire source file will be skipped if the application is not configured\r
1000 to include software timer functionality.  If you want to include software timer\r
1001 functionality then ensure configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */\r
1002 #endif /* configUSE_TIMERS == 1 */\r
1003 \r
1004 \r
1005 \r