2 * FreeRTOS Kernel V10.0.1
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /* Standard includes. */
\r
31 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
32 all the API functions to use the MPU wrappers. That should only be done when
\r
33 task.h is included from an application file. */
\r
34 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
36 #include "FreeRTOS.h"
\r
41 #if ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 0 )
\r
42 #error configUSE_TIMERS must be set to 1 to make the xTimerPendFunctionCall() function available.
\r
45 /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
\r
46 because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
\r
47 for the header files above, but not in this file, in order to generate the
\r
48 correct privileged Vs unprivileged linkage and placement. */
\r
49 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e9021 !e961 !e750. */
\r
52 /* This entire source file will be skipped if the application is not configured
\r
53 to include software timer functionality. This #if is closed at the very bottom
\r
54 of this file. If you want to include software timer functionality then ensure
\r
55 configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */
\r
56 #if ( configUSE_TIMERS == 1 )
\r
58 /* Misc definitions. */
\r
59 #define tmrNO_DELAY ( TickType_t ) 0U
\r
61 /* The name assigned to the timer service task. This can be overridden by
\r
62 defining trmTIMER_SERVICE_TASK_NAME in FreeRTOSConfig.h. */
\r
63 #ifndef configTIMER_SERVICE_TASK_NAME
\r
64 #define configTIMER_SERVICE_TASK_NAME "Tmr Svc"
\r
67 /* The definition of the timers themselves. */
\r
68 typedef struct TimerDef_t
\r
70 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
71 ListItem_t xTimerListItem; /*<< Standard linked list item as used by all kernel features for event management. */
\r
72 TickType_t xTimerPeriodInTicks;/*<< How quickly and often the timer expires. */
\r
73 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
74 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
75 TimerCallbackFunction_t pxCallbackFunction; /*<< The function that will be called when the timer expires. */
\r
76 #if( configUSE_TRACE_FACILITY == 1 )
\r
77 UBaseType_t uxTimerNumber; /*<< An ID assigned by trace tools such as FreeRTOS+Trace */
\r
80 #if( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
\r
81 uint8_t ucStaticallyAllocated; /*<< Set to pdTRUE if the timer was created statically so no attempt is made to free the memory again if the timer is later deleted. */
\r
85 /* The old xTIMER name is maintained above then typedefed to the new Timer_t
\r
86 name below to enable the use of older kernel aware debuggers. */
\r
87 typedef xTIMER Timer_t;
\r
89 /* The definition of messages that can be sent and received on the timer queue.
\r
90 Two types of message can be queued - messages that manipulate a software timer,
\r
91 and messages that request the execution of a non-timer related callback. The
\r
92 two message types are defined in two separate structures, xTimerParametersType
\r
93 and xCallbackParametersType respectively. */
\r
94 typedef struct tmrTimerParameters
\r
96 TickType_t xMessageValue; /*<< An optional value used by a subset of commands, for example, when changing the period of a timer. */
\r
97 Timer_t * pxTimer; /*<< The timer to which the command will be applied. */
\r
101 typedef struct tmrCallbackParameters
\r
103 PendedFunction_t pxCallbackFunction; /* << The callback function to execute. */
\r
104 void *pvParameter1; /* << The value that will be used as the callback functions first parameter. */
\r
105 uint32_t ulParameter2; /* << The value that will be used as the callback functions second parameter. */
\r
106 } CallbackParameters_t;
\r
108 /* The structure that contains the two message types, along with an identifier
\r
109 that is used to determine which message type is valid. */
\r
110 typedef struct tmrTimerQueueMessage
\r
112 BaseType_t xMessageID; /*<< The command being sent to the timer service task. */
\r
115 TimerParameter_t xTimerParameters;
\r
117 /* Don't include xCallbackParameters if it is not going to be used as
\r
118 it makes the structure (and therefore the timer queue) larger. */
\r
119 #if ( INCLUDE_xTimerPendFunctionCall == 1 )
\r
120 CallbackParameters_t xCallbackParameters;
\r
121 #endif /* INCLUDE_xTimerPendFunctionCall */
\r
123 } DaemonTaskMessage_t;
\r
125 /*lint -save -e956 A manual analysis and inspection has been used to determine
\r
126 which static variables must be declared volatile. */
\r
128 /* The list in which active timers are stored. Timers are referenced in expire
\r
129 time order, with the nearest expiry time at the front of the list. Only the
\r
130 timer service task is allowed to access these lists. */
\r
131 PRIVILEGED_DATA static List_t *pxCurrentTimerList;
\r
132 PRIVILEGED_DATA static List_t *pxOverflowTimerList;
\r
134 /* A queue that is used to send commands to the timer service task. */
\r
135 PRIVILEGED_DATA static QueueHandle_t xTimerQueue = NULL;
\r
136 PRIVILEGED_DATA static TaskHandle_t xTimerTaskHandle = NULL;
\r
140 /*-----------------------------------------------------------*/
\r
142 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
\r
144 /* If static allocation is supported then the application must provide the
\r
145 following callback function - which enables the application to optionally
\r
146 provide the memory that will be used by the timer task as the task's stack
\r
148 extern void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize );
\r
153 * Initialise the infrastructure used by the timer service task if it has not
\r
154 * been initialised already.
\r
156 static void prvCheckForValidListAndQueue( void ) PRIVILEGED_FUNCTION;
\r
159 * The timer service task (daemon). Timer functionality is controlled by this
\r
160 * task. Other tasks communicate with the timer service task using the
\r
161 * xTimerQueue queue.
\r
163 static void prvTimerTask( void *pvParameters ) PRIVILEGED_FUNCTION;
\r
166 * Called by the timer service task to interpret and process a command it
\r
167 * received on the timer queue.
\r
169 static void prvProcessReceivedCommands( void ) PRIVILEGED_FUNCTION;
\r
172 * Insert the timer into either xActiveTimerList1, or xActiveTimerList2,
\r
173 * depending on if the expire time causes a timer counter overflow.
\r
175 static BaseType_t prvInsertTimerInActiveList( Timer_t * const pxTimer, const TickType_t xNextExpiryTime, const TickType_t xTimeNow, const TickType_t xCommandTime ) PRIVILEGED_FUNCTION;
\r
178 * An active timer has reached its expire time. Reload the timer if it is an
\r
179 * auto reload timer, then call its callback.
\r
181 static void prvProcessExpiredTimer( const TickType_t xNextExpireTime, const TickType_t xTimeNow ) PRIVILEGED_FUNCTION;
\r
184 * The tick count has overflowed. Switch the timer lists after ensuring the
\r
185 * current timer list does not still reference some timers.
\r
187 static void prvSwitchTimerLists( void ) PRIVILEGED_FUNCTION;
\r
190 * Obtain the current tick count, setting *pxTimerListsWereSwitched to pdTRUE
\r
191 * if a tick count overflow occurred since prvSampleTimeNow() was last called.
\r
193 static TickType_t prvSampleTimeNow( BaseType_t * const pxTimerListsWereSwitched ) PRIVILEGED_FUNCTION;
\r
196 * If the timer list contains any active timers then return the expire time of
\r
197 * the timer that will expire first and set *pxListWasEmpty to false. If the
\r
198 * timer list does not contain any timers then return 0 and set *pxListWasEmpty
\r
201 static TickType_t prvGetNextExpireTime( BaseType_t * const pxListWasEmpty ) PRIVILEGED_FUNCTION;
\r
204 * If a timer has expired, process it. Otherwise, block the timer service task
\r
205 * until either a timer does expire or a command is received.
\r
207 static void prvProcessTimerOrBlockTask( const TickType_t xNextExpireTime, BaseType_t xListWasEmpty ) PRIVILEGED_FUNCTION;
\r
210 * Called after a Timer_t structure has been allocated either statically or
\r
211 * dynamically to fill in the structure's members.
\r
213 static void prvInitialiseNewTimer( const char * const pcTimerName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
\r
214 const TickType_t xTimerPeriodInTicks,
\r
215 const UBaseType_t uxAutoReload,
\r
216 void * const pvTimerID,
\r
217 TimerCallbackFunction_t pxCallbackFunction,
\r
218 Timer_t *pxNewTimer ) PRIVILEGED_FUNCTION;
\r
219 /*-----------------------------------------------------------*/
\r
221 BaseType_t xTimerCreateTimerTask( void )
\r
223 BaseType_t xReturn = pdFAIL;
\r
225 /* This function is called when the scheduler is started if
\r
226 configUSE_TIMERS is set to 1. Check that the infrastructure used by the
\r
227 timer service task has been created/initialised. If timers have already
\r
228 been created then the initialisation will already have been performed. */
\r
229 prvCheckForValidListAndQueue();
\r
231 if( xTimerQueue != NULL )
\r
233 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
\r
235 StaticTask_t *pxTimerTaskTCBBuffer = NULL;
\r
236 StackType_t *pxTimerTaskStackBuffer = NULL;
\r
237 uint32_t ulTimerTaskStackSize;
\r
239 vApplicationGetTimerTaskMemory( &pxTimerTaskTCBBuffer, &pxTimerTaskStackBuffer, &ulTimerTaskStackSize );
\r
240 xTimerTaskHandle = xTaskCreateStatic( prvTimerTask,
\r
241 configTIMER_SERVICE_TASK_NAME,
\r
242 ulTimerTaskStackSize,
\r
244 ( ( UBaseType_t ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT,
\r
245 pxTimerTaskStackBuffer,
\r
246 pxTimerTaskTCBBuffer );
\r
248 if( xTimerTaskHandle != NULL )
\r
255 xReturn = xTaskCreate( prvTimerTask,
\r
256 configTIMER_SERVICE_TASK_NAME,
\r
257 configTIMER_TASK_STACK_DEPTH,
\r
259 ( ( UBaseType_t ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT,
\r
260 &xTimerTaskHandle );
\r
262 #endif /* configSUPPORT_STATIC_ALLOCATION */
\r
266 mtCOVERAGE_TEST_MARKER();
\r
269 configASSERT( xReturn );
\r
272 /*-----------------------------------------------------------*/
\r
274 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
276 TimerHandle_t xTimerCreate( const char * const pcTimerName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
\r
277 const TickType_t xTimerPeriodInTicks,
\r
278 const UBaseType_t uxAutoReload,
\r
279 void * const pvTimerID,
\r
280 TimerCallbackFunction_t pxCallbackFunction )
\r
282 Timer_t *pxNewTimer;
\r
284 pxNewTimer = ( Timer_t * ) pvPortMalloc( sizeof( Timer_t ) ); /*lint !e9087 !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack, and the first member of Timer_t is always a pointer to the timer's mame. */
\r
286 if( pxNewTimer != NULL )
\r
288 prvInitialiseNewTimer( pcTimerName, xTimerPeriodInTicks, uxAutoReload, pvTimerID, pxCallbackFunction, pxNewTimer );
\r
290 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
\r
292 /* Timers can be created statically or dynamically, so note this
\r
293 timer was created dynamically in case the timer is later
\r
295 pxNewTimer->ucStaticallyAllocated = pdFALSE;
\r
297 #endif /* configSUPPORT_STATIC_ALLOCATION */
\r
303 #endif /* configSUPPORT_STATIC_ALLOCATION */
\r
304 /*-----------------------------------------------------------*/
\r
306 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
\r
308 TimerHandle_t xTimerCreateStatic( const char * const pcTimerName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
\r
309 const TickType_t xTimerPeriodInTicks,
\r
310 const UBaseType_t uxAutoReload,
\r
311 void * const pvTimerID,
\r
312 TimerCallbackFunction_t pxCallbackFunction,
\r
313 StaticTimer_t *pxTimerBuffer )
\r
315 Timer_t *pxNewTimer;
\r
317 #if( configASSERT_DEFINED == 1 )
\r
319 /* Sanity check that the size of the structure used to declare a
\r
320 variable of type StaticTimer_t equals the size of the real timer
\r
322 volatile size_t xSize = sizeof( StaticTimer_t );
\r
323 configASSERT( xSize == sizeof( Timer_t ) );
\r
324 ( void ) xSize; /* Keeps lint quiet when configASSERT() is not defined. */
\r
326 #endif /* configASSERT_DEFINED */
\r
328 /* A pointer to a StaticTimer_t structure MUST be provided, use it. */
\r
329 configASSERT( pxTimerBuffer );
\r
330 pxNewTimer = ( Timer_t * ) pxTimerBuffer; /*lint !e740 !e9087 StaticTimer_t is a pointer to a Timer_t, so guaranteed to be aligned and sized correctly (checked by an assert()), so this is safe. */
\r
332 if( pxNewTimer != NULL )
\r
334 prvInitialiseNewTimer( pcTimerName, xTimerPeriodInTicks, uxAutoReload, pvTimerID, pxCallbackFunction, pxNewTimer );
\r
336 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
338 /* Timers can be created statically or dynamically so note this
\r
339 timer was created statically in case it is later deleted. */
\r
340 pxNewTimer->ucStaticallyAllocated = pdTRUE;
\r
342 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
\r
348 #endif /* configSUPPORT_STATIC_ALLOCATION */
\r
349 /*-----------------------------------------------------------*/
\r
351 static void prvInitialiseNewTimer( const char * const pcTimerName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
\r
352 const TickType_t xTimerPeriodInTicks,
\r
353 const UBaseType_t uxAutoReload,
\r
354 void * const pvTimerID,
\r
355 TimerCallbackFunction_t pxCallbackFunction,
\r
356 Timer_t *pxNewTimer )
\r
358 /* 0 is not a valid value for xTimerPeriodInTicks. */
\r
359 configASSERT( ( xTimerPeriodInTicks > 0 ) );
\r
361 if( pxNewTimer != NULL )
\r
363 /* Ensure the infrastructure used by the timer service task has been
\r
364 created/initialised. */
\r
365 prvCheckForValidListAndQueue();
\r
367 /* Initialise the timer structure members using the function
\r
369 pxNewTimer->pcTimerName = pcTimerName;
\r
370 pxNewTimer->xTimerPeriodInTicks = xTimerPeriodInTicks;
\r
371 pxNewTimer->uxAutoReload = uxAutoReload;
\r
372 pxNewTimer->pvTimerID = pvTimerID;
\r
373 pxNewTimer->pxCallbackFunction = pxCallbackFunction;
\r
374 vListInitialiseItem( &( pxNewTimer->xTimerListItem ) );
\r
375 traceTIMER_CREATE( pxNewTimer );
\r
378 /*-----------------------------------------------------------*/
\r
380 BaseType_t xTimerGenericCommand( TimerHandle_t xTimer, const BaseType_t xCommandID, const TickType_t xOptionalValue, BaseType_t * const pxHigherPriorityTaskWoken, const TickType_t xTicksToWait )
\r
382 BaseType_t xReturn = pdFAIL;
\r
383 DaemonTaskMessage_t xMessage;
\r
385 configASSERT( xTimer );
\r
387 /* Send a message to the timer service task to perform a particular action
\r
388 on a particular timer definition. */
\r
389 if( xTimerQueue != NULL )
\r
391 /* Send a command to the timer service task to start the xTimer timer. */
\r
392 xMessage.xMessageID = xCommandID;
\r
393 xMessage.u.xTimerParameters.xMessageValue = xOptionalValue;
\r
394 xMessage.u.xTimerParameters.pxTimer = xTimer;
\r
396 if( xCommandID < tmrFIRST_FROM_ISR_COMMAND )
\r
398 if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )
\r
400 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xTicksToWait );
\r
404 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, tmrNO_DELAY );
\r
409 xReturn = xQueueSendToBackFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );
\r
412 traceTIMER_COMMAND_SEND( xTimer, xCommandID, xOptionalValue, xReturn );
\r
416 mtCOVERAGE_TEST_MARKER();
\r
421 /*-----------------------------------------------------------*/
\r
423 TaskHandle_t xTimerGetTimerDaemonTaskHandle( void )
\r
425 /* If xTimerGetTimerDaemonTaskHandle() is called before the scheduler has been
\r
426 started, then xTimerTaskHandle will be NULL. */
\r
427 configASSERT( ( xTimerTaskHandle != NULL ) );
\r
428 return xTimerTaskHandle;
\r
430 /*-----------------------------------------------------------*/
\r
432 TickType_t xTimerGetPeriod( TimerHandle_t xTimer )
\r
434 Timer_t *pxTimer = xTimer;
\r
436 configASSERT( xTimer );
\r
437 return pxTimer->xTimerPeriodInTicks;
\r
439 /*-----------------------------------------------------------*/
\r
441 TickType_t xTimerGetExpiryTime( TimerHandle_t xTimer )
\r
443 Timer_t * pxTimer = xTimer;
\r
444 TickType_t xReturn;
\r
446 configASSERT( xTimer );
\r
447 xReturn = listGET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ) );
\r
450 /*-----------------------------------------------------------*/
\r
452 const char * pcTimerGetName( TimerHandle_t xTimer ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
\r
454 Timer_t *pxTimer = xTimer;
\r
456 configASSERT( xTimer );
\r
457 return pxTimer->pcTimerName;
\r
459 /*-----------------------------------------------------------*/
\r
461 static void prvProcessExpiredTimer( const TickType_t xNextExpireTime, const TickType_t xTimeNow )
\r
463 BaseType_t xResult;
\r
464 Timer_t * const pxTimer = ( Timer_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList ); /*lint !e9087 !e9079 void * is used as this macro is used with tasks and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
\r
466 /* Remove the timer from the list of active timers. A check has already
\r
467 been performed to ensure the list is not empty. */
\r
468 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
\r
469 traceTIMER_EXPIRED( pxTimer );
\r
471 /* If the timer is an auto reload timer then calculate the next
\r
472 expiry time and re-insert the timer in the list of active timers. */
\r
473 if( pxTimer->uxAutoReload == ( UBaseType_t ) pdTRUE )
\r
475 /* The timer is inserted into a list using a time relative to anything
\r
476 other than the current time. It will therefore be inserted into the
\r
477 correct list relative to the time this task thinks it is now. */
\r
478 if( prvInsertTimerInActiveList( pxTimer, ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ), xTimeNow, xNextExpireTime ) != pdFALSE )
\r
480 /* The timer expired before it was added to the active timer
\r
481 list. Reload it now. */
\r
482 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START_DONT_TRACE, xNextExpireTime, NULL, tmrNO_DELAY );
\r
483 configASSERT( xResult );
\r
488 mtCOVERAGE_TEST_MARKER();
\r
493 mtCOVERAGE_TEST_MARKER();
\r
496 /* Call the timer callback. */
\r
497 pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
\r
499 /*-----------------------------------------------------------*/
\r
501 static void prvTimerTask( void *pvParameters )
\r
503 TickType_t xNextExpireTime;
\r
504 BaseType_t xListWasEmpty;
\r
506 /* Just to avoid compiler warnings. */
\r
507 ( void ) pvParameters;
\r
509 #if( configUSE_DAEMON_TASK_STARTUP_HOOK == 1 )
\r
511 extern void vApplicationDaemonTaskStartupHook( void );
\r
513 /* Allow the application writer to execute some code in the context of
\r
514 this task at the point the task starts executing. This is useful if the
\r
515 application includes initialisation code that would benefit from
\r
516 executing after the scheduler has been started. */
\r
517 vApplicationDaemonTaskStartupHook();
\r
519 #endif /* configUSE_DAEMON_TASK_STARTUP_HOOK */
\r
523 /* Query the timers list to see if it contains any timers, and if so,
\r
524 obtain the time at which the next timer will expire. */
\r
525 xNextExpireTime = prvGetNextExpireTime( &xListWasEmpty );
\r
527 /* If a timer has expired, process it. Otherwise, block this task
\r
528 until either a timer does expire, or a command is received. */
\r
529 prvProcessTimerOrBlockTask( xNextExpireTime, xListWasEmpty );
\r
531 /* Empty the command queue. */
\r
532 prvProcessReceivedCommands();
\r
535 /*-----------------------------------------------------------*/
\r
537 static void prvProcessTimerOrBlockTask( const TickType_t xNextExpireTime, BaseType_t xListWasEmpty )
\r
539 TickType_t xTimeNow;
\r
540 BaseType_t xTimerListsWereSwitched;
\r
544 /* Obtain the time now to make an assessment as to whether the timer
\r
545 has expired or not. If obtaining the time causes the lists to switch
\r
546 then don't process this timer as any timers that remained in the list
\r
547 when the lists were switched will have been processed within the
\r
548 prvSampleTimeNow() function. */
\r
549 xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );
\r
550 if( xTimerListsWereSwitched == pdFALSE )
\r
552 /* The tick count has not overflowed, has the timer expired? */
\r
553 if( ( xListWasEmpty == pdFALSE ) && ( xNextExpireTime <= xTimeNow ) )
\r
555 ( void ) xTaskResumeAll();
\r
556 prvProcessExpiredTimer( xNextExpireTime, xTimeNow );
\r
560 /* The tick count has not overflowed, and the next expire
\r
561 time has not been reached yet. This task should therefore
\r
562 block to wait for the next expire time or a command to be
\r
563 received - whichever comes first. The following line cannot
\r
564 be reached unless xNextExpireTime > xTimeNow, except in the
\r
565 case when the current timer list is empty. */
\r
566 if( xListWasEmpty != pdFALSE )
\r
568 /* The current timer list is empty - is the overflow list
\r
570 xListWasEmpty = listLIST_IS_EMPTY( pxOverflowTimerList );
\r
573 vQueueWaitForMessageRestricted( xTimerQueue, ( xNextExpireTime - xTimeNow ), xListWasEmpty );
\r
575 if( xTaskResumeAll() == pdFALSE )
\r
577 /* Yield to wait for either a command to arrive, or the
\r
578 block time to expire. If a command arrived between the
\r
579 critical section being exited and this yield then the yield
\r
580 will not cause the task to block. */
\r
581 portYIELD_WITHIN_API();
\r
585 mtCOVERAGE_TEST_MARKER();
\r
591 ( void ) xTaskResumeAll();
\r
595 /*-----------------------------------------------------------*/
\r
597 static TickType_t prvGetNextExpireTime( BaseType_t * const pxListWasEmpty )
\r
599 TickType_t xNextExpireTime;
\r
601 /* Timers are listed in expiry time order, with the head of the list
\r
602 referencing the task that will expire first. Obtain the time at which
\r
603 the timer with the nearest expiry time will expire. If there are no
\r
604 active timers then just set the next expire time to 0. That will cause
\r
605 this task to unblock when the tick count overflows, at which point the
\r
606 timer lists will be switched and the next expiry time can be
\r
608 *pxListWasEmpty = listLIST_IS_EMPTY( pxCurrentTimerList );
\r
609 if( *pxListWasEmpty == pdFALSE )
\r
611 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );
\r
615 /* Ensure the task unblocks when the tick count rolls over. */
\r
616 xNextExpireTime = ( TickType_t ) 0U;
\r
619 return xNextExpireTime;
\r
621 /*-----------------------------------------------------------*/
\r
623 static TickType_t prvSampleTimeNow( BaseType_t * const pxTimerListsWereSwitched )
\r
625 TickType_t xTimeNow;
\r
626 PRIVILEGED_DATA static TickType_t xLastTime = ( TickType_t ) 0U; /*lint !e956 Variable is only accessible to one task. */
\r
628 xTimeNow = xTaskGetTickCount();
\r
630 if( xTimeNow < xLastTime )
\r
632 prvSwitchTimerLists();
\r
633 *pxTimerListsWereSwitched = pdTRUE;
\r
637 *pxTimerListsWereSwitched = pdFALSE;
\r
640 xLastTime = xTimeNow;
\r
644 /*-----------------------------------------------------------*/
\r
646 static BaseType_t prvInsertTimerInActiveList( Timer_t * const pxTimer, const TickType_t xNextExpiryTime, const TickType_t xTimeNow, const TickType_t xCommandTime )
\r
648 BaseType_t xProcessTimerNow = pdFALSE;
\r
650 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xNextExpiryTime );
\r
651 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );
\r
653 if( xNextExpiryTime <= xTimeNow )
\r
655 /* Has the expiry time elapsed between the command to start/reset a
\r
656 timer was issued, and the time the command was processed? */
\r
657 if( ( ( TickType_t ) ( xTimeNow - xCommandTime ) ) >= pxTimer->xTimerPeriodInTicks ) /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
\r
659 /* The time between a command being issued and the command being
\r
660 processed actually exceeds the timers period. */
\r
661 xProcessTimerNow = pdTRUE;
\r
665 vListInsert( pxOverflowTimerList, &( pxTimer->xTimerListItem ) );
\r
670 if( ( xTimeNow < xCommandTime ) && ( xNextExpiryTime >= xCommandTime ) )
\r
672 /* If, since the command was issued, the tick count has overflowed
\r
673 but the expiry time has not, then the timer must have already passed
\r
674 its expiry time and should be processed immediately. */
\r
675 xProcessTimerNow = pdTRUE;
\r
679 vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );
\r
683 return xProcessTimerNow;
\r
685 /*-----------------------------------------------------------*/
\r
687 static void prvProcessReceivedCommands( void )
\r
689 DaemonTaskMessage_t xMessage;
\r
691 BaseType_t xTimerListsWereSwitched, xResult;
\r
692 TickType_t xTimeNow;
\r
694 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
696 #if ( INCLUDE_xTimerPendFunctionCall == 1 )
\r
698 /* Negative commands are pended function calls rather than timer
\r
700 if( xMessage.xMessageID < ( BaseType_t ) 0 )
\r
702 const CallbackParameters_t * const pxCallback = &( xMessage.u.xCallbackParameters );
\r
704 /* The timer uses the xCallbackParameters member to request a
\r
705 callback be executed. Check the callback is not NULL. */
\r
706 configASSERT( pxCallback );
\r
708 /* Call the function. */
\r
709 pxCallback->pxCallbackFunction( pxCallback->pvParameter1, pxCallback->ulParameter2 );
\r
713 mtCOVERAGE_TEST_MARKER();
\r
716 #endif /* INCLUDE_xTimerPendFunctionCall */
\r
718 /* Commands that are positive are timer commands rather than pended
\r
720 if( xMessage.xMessageID >= ( BaseType_t ) 0 )
\r
722 /* The messages uses the xTimerParameters member to work on a
\r
724 pxTimer = xMessage.u.xTimerParameters.pxTimer;
\r
726 if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE ) /*lint !e961. The cast is only redundant when NULL is passed into the macro. */
\r
728 /* The timer is in a list, remove it. */
\r
729 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
\r
733 mtCOVERAGE_TEST_MARKER();
\r
736 traceTIMER_COMMAND_RECEIVED( pxTimer, xMessage.xMessageID, xMessage.u.xTimerParameters.xMessageValue );
\r
738 /* In this case the xTimerListsWereSwitched parameter is not used, but
\r
739 it must be present in the function call. prvSampleTimeNow() must be
\r
740 called after the message is received from xTimerQueue so there is no
\r
741 possibility of a higher priority task adding a message to the message
\r
742 queue with a time that is ahead of the timer daemon task (because it
\r
743 pre-empted the timer daemon task after the xTimeNow value was set). */
\r
744 xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );
\r
746 switch( xMessage.xMessageID )
\r
748 case tmrCOMMAND_START :
\r
749 case tmrCOMMAND_START_FROM_ISR :
\r
750 case tmrCOMMAND_RESET :
\r
751 case tmrCOMMAND_RESET_FROM_ISR :
\r
752 case tmrCOMMAND_START_DONT_TRACE :
\r
753 /* Start or restart a timer. */
\r
754 if( prvInsertTimerInActiveList( pxTimer, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow, xMessage.u.xTimerParameters.xMessageValue ) != pdFALSE )
\r
756 /* The timer expired before it was added to the active
\r
757 timer list. Process it now. */
\r
758 pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
\r
759 traceTIMER_EXPIRED( pxTimer );
\r
761 if( pxTimer->uxAutoReload == ( UBaseType_t ) pdTRUE )
\r
763 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START_DONT_TRACE, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, NULL, tmrNO_DELAY );
\r
764 configASSERT( xResult );
\r
769 mtCOVERAGE_TEST_MARKER();
\r
774 mtCOVERAGE_TEST_MARKER();
\r
778 case tmrCOMMAND_STOP :
\r
779 case tmrCOMMAND_STOP_FROM_ISR :
\r
780 /* The timer has already been removed from the active list.
\r
781 There is nothing to do here. */
\r
784 case tmrCOMMAND_CHANGE_PERIOD :
\r
785 case tmrCOMMAND_CHANGE_PERIOD_FROM_ISR :
\r
786 pxTimer->xTimerPeriodInTicks = xMessage.u.xTimerParameters.xMessageValue;
\r
787 configASSERT( ( pxTimer->xTimerPeriodInTicks > 0 ) );
\r
789 /* The new period does not really have a reference, and can
\r
790 be longer or shorter than the old one. The command time is
\r
791 therefore set to the current time, and as the period cannot
\r
792 be zero the next expiry time can only be in the future,
\r
793 meaning (unlike for the xTimerStart() case above) there is
\r
794 no fail case that needs to be handled here. */
\r
795 ( void ) prvInsertTimerInActiveList( pxTimer, ( xTimeNow + pxTimer->xTimerPeriodInTicks ), xTimeNow, xTimeNow );
\r
798 case tmrCOMMAND_DELETE :
\r
799 /* The timer has already been removed from the active list,
\r
800 just free up the memory if the memory was dynamically
\r
802 #if( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )
\r
804 /* The timer can only have been allocated dynamically -
\r
806 vPortFree( pxTimer );
\r
808 #elif( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
\r
810 /* The timer could have been allocated statically or
\r
811 dynamically, so check before attempting to free the
\r
813 if( pxTimer->ucStaticallyAllocated == ( uint8_t ) pdFALSE )
\r
815 vPortFree( pxTimer );
\r
819 mtCOVERAGE_TEST_MARKER();
\r
822 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
\r
826 /* Don't expect to get here. */
\r
832 /*-----------------------------------------------------------*/
\r
834 static void prvSwitchTimerLists( void )
\r
836 TickType_t xNextExpireTime, xReloadTime;
\r
839 BaseType_t xResult;
\r
841 /* The tick count has overflowed. The timer lists must be switched.
\r
842 If there are any timers still referenced from the current timer list
\r
843 then they must have expired and should be processed before the lists
\r
845 while( listLIST_IS_EMPTY( pxCurrentTimerList ) == pdFALSE )
\r
847 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );
\r
849 /* Remove the timer from the list. */
\r
850 pxTimer = ( Timer_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList ); /*lint !e9087 !e9079 void * is used as this macro is used with tasks and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
\r
851 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
\r
852 traceTIMER_EXPIRED( pxTimer );
\r
854 /* Execute its callback, then send a command to restart the timer if
\r
855 it is an auto-reload timer. It cannot be restarted here as the lists
\r
856 have not yet been switched. */
\r
857 pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
\r
859 if( pxTimer->uxAutoReload == ( UBaseType_t ) pdTRUE )
\r
861 /* Calculate the reload value, and if the reload value results in
\r
862 the timer going into the same timer list then it has already expired
\r
863 and the timer should be re-inserted into the current list so it is
\r
864 processed again within this loop. Otherwise a command should be sent
\r
865 to restart the timer to ensure it is only inserted into a list after
\r
866 the lists have been swapped. */
\r
867 xReloadTime = ( xNextExpireTime + pxTimer->xTimerPeriodInTicks );
\r
868 if( xReloadTime > xNextExpireTime )
\r
870 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xReloadTime );
\r
871 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );
\r
872 vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );
\r
876 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START_DONT_TRACE, xNextExpireTime, NULL, tmrNO_DELAY );
\r
877 configASSERT( xResult );
\r
883 mtCOVERAGE_TEST_MARKER();
\r
887 pxTemp = pxCurrentTimerList;
\r
888 pxCurrentTimerList = pxOverflowTimerList;
\r
889 pxOverflowTimerList = pxTemp;
\r
891 /*-----------------------------------------------------------*/
\r
893 static void prvCheckForValidListAndQueue( void )
\r
895 PRIVILEGED_DATA static List_t xActiveTimerList1;
\r
896 PRIVILEGED_DATA static List_t xActiveTimerList2;
\r
898 /* Check that the list from which active timers are referenced, and the
\r
899 queue used to communicate with the timer service, have been
\r
901 taskENTER_CRITICAL();
\r
903 if( xTimerQueue == NULL )
\r
905 vListInitialise( &xActiveTimerList1 );
\r
906 vListInitialise( &xActiveTimerList2 );
\r
907 pxCurrentTimerList = &xActiveTimerList1;
\r
908 pxOverflowTimerList = &xActiveTimerList2;
\r
910 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
\r
912 /* The timer queue is allocated statically in case
\r
913 configSUPPORT_DYNAMIC_ALLOCATION is 0. */
\r
914 static StaticQueue_t xStaticTimerQueue; /*lint !e956 Ok to declare in this manner to prevent additional conditional compilation guards in other locations. */
\r
915 static uint8_t ucStaticTimerQueueStorage[ ( size_t ) configTIMER_QUEUE_LENGTH * sizeof( DaemonTaskMessage_t ) ]; /*lint !e956 Ok to declare in this manner to prevent additional conditional compilation guards in other locations. */
\r
917 xTimerQueue = xQueueCreateStatic( ( UBaseType_t ) configTIMER_QUEUE_LENGTH, ( UBaseType_t ) sizeof( DaemonTaskMessage_t ), &( ucStaticTimerQueueStorage[ 0 ] ), &xStaticTimerQueue );
\r
921 xTimerQueue = xQueueCreate( ( UBaseType_t ) configTIMER_QUEUE_LENGTH, sizeof( DaemonTaskMessage_t ) );
\r
925 #if ( configQUEUE_REGISTRY_SIZE > 0 )
\r
927 if( xTimerQueue != NULL )
\r
929 vQueueAddToRegistry( xTimerQueue, "TmrQ" );
\r
933 mtCOVERAGE_TEST_MARKER();
\r
936 #endif /* configQUEUE_REGISTRY_SIZE */
\r
940 mtCOVERAGE_TEST_MARKER();
\r
943 taskEXIT_CRITICAL();
\r
945 /*-----------------------------------------------------------*/
\r
947 BaseType_t xTimerIsTimerActive( TimerHandle_t xTimer )
\r
949 BaseType_t xTimerIsInActiveList;
\r
950 Timer_t *pxTimer = xTimer;
\r
952 configASSERT( xTimer );
\r
954 /* Is the timer in the list of active timers? */
\r
955 taskENTER_CRITICAL();
\r
957 /* Checking to see if it is in the NULL list in effect checks to see if
\r
958 it is referenced from either the current or the overflow timer lists in
\r
959 one go, but the logic has to be reversed, hence the '!'. */
\r
960 if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdTRUE )
\r
962 xTimerIsInActiveList = pdFALSE;
\r
966 xTimerIsInActiveList = pdTRUE;
\r
969 taskEXIT_CRITICAL();
\r
971 return xTimerIsInActiveList;
\r
972 } /*lint !e818 Can't be pointer to const due to the typedef. */
\r
973 /*-----------------------------------------------------------*/
\r
975 void *pvTimerGetTimerID( const TimerHandle_t xTimer )
\r
977 Timer_t * const pxTimer = xTimer;
\r
980 configASSERT( xTimer );
\r
982 taskENTER_CRITICAL();
\r
984 pvReturn = pxTimer->pvTimerID;
\r
986 taskEXIT_CRITICAL();
\r
990 /*-----------------------------------------------------------*/
\r
992 void vTimerSetTimerID( TimerHandle_t xTimer, void *pvNewID )
\r
994 Timer_t * const pxTimer = xTimer;
\r
996 configASSERT( xTimer );
\r
998 taskENTER_CRITICAL();
\r
1000 pxTimer->pvTimerID = pvNewID;
\r
1002 taskEXIT_CRITICAL();
\r
1004 /*-----------------------------------------------------------*/
\r
1006 #if( INCLUDE_xTimerPendFunctionCall == 1 )
\r
1008 BaseType_t xTimerPendFunctionCallFromISR( PendedFunction_t xFunctionToPend, void *pvParameter1, uint32_t ulParameter2, BaseType_t *pxHigherPriorityTaskWoken )
\r
1010 DaemonTaskMessage_t xMessage;
\r
1011 BaseType_t xReturn;
\r
1013 /* Complete the message with the function parameters and post it to the
\r
1015 xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK_FROM_ISR;
\r
1016 xMessage.u.xCallbackParameters.pxCallbackFunction = xFunctionToPend;
\r
1017 xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;
\r
1018 xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;
\r
1020 xReturn = xQueueSendFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );
\r
1022 tracePEND_FUNC_CALL_FROM_ISR( xFunctionToPend, pvParameter1, ulParameter2, xReturn );
\r
1027 #endif /* INCLUDE_xTimerPendFunctionCall */
\r
1028 /*-----------------------------------------------------------*/
\r
1030 #if( INCLUDE_xTimerPendFunctionCall == 1 )
\r
1032 BaseType_t xTimerPendFunctionCall( PendedFunction_t xFunctionToPend, void *pvParameter1, uint32_t ulParameter2, TickType_t xTicksToWait )
\r
1034 DaemonTaskMessage_t xMessage;
\r
1035 BaseType_t xReturn;
\r
1037 /* This function can only be called after a timer has been created or
\r
1038 after the scheduler has been started because, until then, the timer
\r
1039 queue does not exist. */
\r
1040 configASSERT( xTimerQueue );
\r
1042 /* Complete the message with the function parameters and post it to the
\r
1044 xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK;
\r
1045 xMessage.u.xCallbackParameters.pxCallbackFunction = xFunctionToPend;
\r
1046 xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;
\r
1047 xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;
\r
1049 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xTicksToWait );
\r
1051 tracePEND_FUNC_CALL( xFunctionToPend, pvParameter1, ulParameter2, xReturn );
\r
1056 #endif /* INCLUDE_xTimerPendFunctionCall */
\r
1057 /*-----------------------------------------------------------*/
\r
1059 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1061 UBaseType_t uxTimerGetTimerNumber( TimerHandle_t xTimer )
\r
1063 return ( ( Timer_t * ) xTimer )->uxTimerNumber;
\r
1066 #endif /* configUSE_TRACE_FACILITY */
\r
1067 /*-----------------------------------------------------------*/
\r
1069 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1071 void vTimerSetTimerNumber( TimerHandle_t xTimer, UBaseType_t uxTimerNumber )
\r
1073 ( ( Timer_t * ) xTimer )->uxTimerNumber = uxTimerNumber;
\r
1076 #endif /* configUSE_TRACE_FACILITY */
\r
1077 /*-----------------------------------------------------------*/
\r
1079 /* This entire source file will be skipped if the application is not configured
\r
1080 to include software timer functionality. If you want to include software timer
\r
1081 functionality then ensure configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */
\r
1082 #endif /* configUSE_TIMERS == 1 */
\r