2 * FreeRTOS Kernel V10.4.6
\r
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * SPDX-License-Identifier: MIT
\r
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
8 * this software and associated documentation files (the "Software"), to deal in
\r
9 * the Software without restriction, including without limitation the rights to
\r
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
11 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
12 * subject to the following conditions:
\r
14 * The above copyright notice and this permission notice shall be included in all
\r
15 * copies or substantial portions of the Software.
\r
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
24 * https://www.FreeRTOS.org
\r
25 * https://github.com/FreeRTOS
\r
29 /* Standard includes. */
\r
32 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
33 * all the API functions to use the MPU wrappers. That should only be done when
\r
34 * task.h is included from an application file. */
\r
35 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
37 #include "FreeRTOS.h"
\r
42 #if ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 0 )
\r
43 #error configUSE_TIMERS must be set to 1 to make the xTimerPendFunctionCall() function available.
\r
46 /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
\r
47 * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
\r
48 * for the header files above, but not in this file, in order to generate the
\r
49 * correct privileged Vs unprivileged linkage and placement. */
\r
50 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e9021 !e961 !e750. */
\r
53 /* This entire source file will be skipped if the application is not configured
\r
54 * to include software timer functionality. This #if is closed at the very bottom
\r
55 * of this file. If you want to include software timer functionality then ensure
\r
56 * configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */
\r
57 #if ( configUSE_TIMERS == 1 )
\r
59 /* Misc definitions. */
\r
60 #define tmrNO_DELAY ( ( TickType_t ) 0U )
\r
61 #define tmrMAX_TIME_BEFORE_OVERFLOW ( ( TickType_t ) -1 )
\r
63 /* The name assigned to the timer service task. This can be overridden by
\r
64 * defining trmTIMER_SERVICE_TASK_NAME in FreeRTOSConfig.h. */
\r
65 #ifndef configTIMER_SERVICE_TASK_NAME
\r
66 #define configTIMER_SERVICE_TASK_NAME "Tmr Svc"
\r
69 /* Bit definitions used in the ucStatus member of a timer structure. */
\r
70 #define tmrSTATUS_IS_ACTIVE ( ( uint8_t ) 0x01 )
\r
71 #define tmrSTATUS_IS_STATICALLY_ALLOCATED ( ( uint8_t ) 0x02 )
\r
72 #define tmrSTATUS_IS_AUTORELOAD ( ( uint8_t ) 0x04 )
\r
74 /* The definition of the timers themselves. */
\r
75 typedef struct tmrTimerControl /* The old naming convention is used to prevent breaking kernel aware debuggers. */
\r
77 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
78 ListItem_t xTimerListItem; /*<< Standard linked list item as used by all kernel features for event management. */
\r
79 TickType_t xTimerPeriodInTicks; /*<< How quickly and often the timer expires. */
\r
80 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
81 TimerCallbackFunction_t pxCallbackFunction; /*<< The function that will be called when the timer expires. */
\r
82 #if ( configUSE_TRACE_FACILITY == 1 )
\r
83 UBaseType_t uxTimerNumber; /*<< An ID assigned by trace tools such as FreeRTOS+Trace */
\r
85 uint8_t ucStatus; /*<< Holds bits to say if the timer was statically allocated or not, and if it is active or not. */
\r
88 /* The old xTIMER name is maintained above then typedefed to the new Timer_t
\r
89 * name below to enable the use of older kernel aware debuggers. */
\r
90 typedef xTIMER Timer_t;
\r
92 /* The definition of messages that can be sent and received on the timer queue.
\r
93 * Two types of message can be queued - messages that manipulate a software timer,
\r
94 * and messages that request the execution of a non-timer related callback. The
\r
95 * two message types are defined in two separate structures, xTimerParametersType
\r
96 * and xCallbackParametersType respectively. */
\r
97 typedef struct tmrTimerParameters
\r
99 TickType_t xMessageValue; /*<< An optional value used by a subset of commands, for example, when changing the period of a timer. */
\r
100 Timer_t * pxTimer; /*<< The timer to which the command will be applied. */
\r
101 } TimerParameter_t;
\r
104 typedef struct tmrCallbackParameters
\r
106 PendedFunction_t pxCallbackFunction; /* << The callback function to execute. */
\r
107 void * pvParameter1; /* << The value that will be used as the callback functions first parameter. */
\r
108 uint32_t ulParameter2; /* << The value that will be used as the callback functions second parameter. */
\r
109 } CallbackParameters_t;
\r
111 /* The structure that contains the two message types, along with an identifier
\r
112 * that is used to determine which message type is valid. */
\r
113 typedef struct tmrTimerQueueMessage
\r
115 BaseType_t xMessageID; /*<< The command being sent to the timer service task. */
\r
118 TimerParameter_t xTimerParameters;
\r
120 /* Don't include xCallbackParameters if it is not going to be used as
\r
121 * it makes the structure (and therefore the timer queue) larger. */
\r
122 #if ( INCLUDE_xTimerPendFunctionCall == 1 )
\r
123 CallbackParameters_t xCallbackParameters;
\r
124 #endif /* INCLUDE_xTimerPendFunctionCall */
\r
126 } DaemonTaskMessage_t;
\r
128 /*lint -save -e956 A manual analysis and inspection has been used to determine
\r
129 * which static variables must be declared volatile. */
\r
131 /* The list in which active timers are stored. Timers are referenced in expire
\r
132 * time order, with the nearest expiry time at the front of the list. Only the
\r
133 * timer service task is allowed to access these lists.
\r
134 * xActiveTimerList1 and xActiveTimerList2 could be at function scope but that
\r
135 * breaks some kernel aware debuggers, and debuggers that reply on removing the
\r
136 * static qualifier. */
\r
137 PRIVILEGED_DATA static List_t xActiveTimerList1;
\r
138 PRIVILEGED_DATA static List_t xActiveTimerList2;
\r
139 PRIVILEGED_DATA static List_t * pxCurrentTimerList;
\r
140 PRIVILEGED_DATA static List_t * pxOverflowTimerList;
\r
142 /* A queue that is used to send commands to the timer service task. */
\r
143 PRIVILEGED_DATA static QueueHandle_t xTimerQueue = NULL;
\r
144 PRIVILEGED_DATA static TaskHandle_t xTimerTaskHandle = NULL;
\r
148 /*-----------------------------------------------------------*/
\r
151 * Initialise the infrastructure used by the timer service task if it has not
\r
152 * been initialised already.
\r
154 static void prvCheckForValidListAndQueue( void ) PRIVILEGED_FUNCTION;
\r
157 * The timer service task (daemon). Timer functionality is controlled by this
\r
158 * task. Other tasks communicate with the timer service task using the
\r
159 * xTimerQueue queue.
\r
161 static portTASK_FUNCTION_PROTO( prvTimerTask, pvParameters ) PRIVILEGED_FUNCTION;
\r
164 * Called by the timer service task to interpret and process a command it
\r
165 * received on the timer queue.
\r
167 static void prvProcessReceivedCommands( void ) PRIVILEGED_FUNCTION;
\r
170 * Insert the timer into either xActiveTimerList1, or xActiveTimerList2,
\r
171 * depending on if the expire time causes a timer counter overflow.
\r
173 static BaseType_t prvInsertTimerInActiveList( Timer_t * const pxTimer,
\r
174 const TickType_t xNextExpiryTime,
\r
175 const TickType_t xTimeNow,
\r
176 const TickType_t xCommandTime ) PRIVILEGED_FUNCTION;
\r
179 * Reload the specified auto-reload timer. If the reloading is backlogged,
\r
180 * clear the backlog, calling the callback for each additional reload. When
\r
181 * this function returns, the next expiry time is after xTimeNow.
\r
183 static void prvReloadTimer( Timer_t * const pxTimer,
\r
184 TickType_t xExpiredTime,
\r
185 const TickType_t xTimeNow ) PRIVILEGED_FUNCTION;
\r
188 * An active timer has reached its expire time. Reload the timer if it is an
\r
189 * auto-reload timer, then call its callback.
\r
191 static void prvProcessExpiredTimer( const TickType_t xNextExpireTime,
\r
192 const TickType_t xTimeNow ) PRIVILEGED_FUNCTION;
\r
195 * The tick count has overflowed. Switch the timer lists after ensuring the
\r
196 * current timer list does not still reference some timers.
\r
198 static void prvSwitchTimerLists( void ) PRIVILEGED_FUNCTION;
\r
201 * Obtain the current tick count, setting *pxTimerListsWereSwitched to pdTRUE
\r
202 * if a tick count overflow occurred since prvSampleTimeNow() was last called.
\r
204 static TickType_t prvSampleTimeNow( BaseType_t * const pxTimerListsWereSwitched ) PRIVILEGED_FUNCTION;
\r
207 * If the timer list contains any active timers then return the expire time of
\r
208 * the timer that will expire first and set *pxListWasEmpty to false. If the
\r
209 * timer list does not contain any timers then return 0 and set *pxListWasEmpty
\r
212 static TickType_t prvGetNextExpireTime( BaseType_t * const pxListWasEmpty ) PRIVILEGED_FUNCTION;
\r
215 * If a timer has expired, process it. Otherwise, block the timer service task
\r
216 * until either a timer does expire or a command is received.
\r
218 static void prvProcessTimerOrBlockTask( const TickType_t xNextExpireTime,
\r
219 BaseType_t xListWasEmpty ) PRIVILEGED_FUNCTION;
\r
222 * Called after a Timer_t structure has been allocated either statically or
\r
223 * dynamically to fill in the structure's members.
\r
225 static void prvInitialiseNewTimer( const char * const pcTimerName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
\r
226 const TickType_t xTimerPeriodInTicks,
\r
227 const UBaseType_t uxAutoReload,
\r
228 void * const pvTimerID,
\r
229 TimerCallbackFunction_t pxCallbackFunction,
\r
230 Timer_t * pxNewTimer ) PRIVILEGED_FUNCTION;
\r
231 /*-----------------------------------------------------------*/
\r
233 BaseType_t xTimerCreateTimerTask( void )
\r
235 BaseType_t xReturn = pdFAIL;
\r
237 /* This function is called when the scheduler is started if
\r
238 * configUSE_TIMERS is set to 1. Check that the infrastructure used by the
\r
239 * timer service task has been created/initialised. If timers have already
\r
240 * been created then the initialisation will already have been performed. */
\r
241 prvCheckForValidListAndQueue();
\r
243 if( xTimerQueue != NULL )
\r
245 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
\r
247 StaticTask_t * pxTimerTaskTCBBuffer = NULL;
\r
248 StackType_t * pxTimerTaskStackBuffer = NULL;
\r
249 uint32_t ulTimerTaskStackSize;
\r
251 vApplicationGetTimerTaskMemory( &pxTimerTaskTCBBuffer, &pxTimerTaskStackBuffer, &ulTimerTaskStackSize );
\r
252 xTimerTaskHandle = xTaskCreateStatic( prvTimerTask,
\r
253 configTIMER_SERVICE_TASK_NAME,
\r
254 ulTimerTaskStackSize,
\r
256 ( ( UBaseType_t ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT,
\r
257 pxTimerTaskStackBuffer,
\r
258 pxTimerTaskTCBBuffer );
\r
260 if( xTimerTaskHandle != NULL )
\r
265 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
\r
267 xReturn = xTaskCreate( prvTimerTask,
\r
268 configTIMER_SERVICE_TASK_NAME,
\r
269 configTIMER_TASK_STACK_DEPTH,
\r
271 ( ( UBaseType_t ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT,
\r
272 &xTimerTaskHandle );
\r
274 #endif /* configSUPPORT_STATIC_ALLOCATION */
\r
278 mtCOVERAGE_TEST_MARKER();
\r
281 configASSERT( xReturn );
\r
284 /*-----------------------------------------------------------*/
\r
286 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
288 TimerHandle_t xTimerCreate( const char * const pcTimerName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
\r
289 const TickType_t xTimerPeriodInTicks,
\r
290 const UBaseType_t uxAutoReload,
\r
291 void * const pvTimerID,
\r
292 TimerCallbackFunction_t pxCallbackFunction )
\r
294 Timer_t * pxNewTimer;
\r
296 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
298 if( pxNewTimer != NULL )
\r
300 /* Status is thus far zero as the timer is not created statically
\r
301 * and has not been started. The auto-reload bit may get set in
\r
302 * prvInitialiseNewTimer. */
\r
303 pxNewTimer->ucStatus = 0x00;
\r
304 prvInitialiseNewTimer( pcTimerName, xTimerPeriodInTicks, uxAutoReload, pvTimerID, pxCallbackFunction, pxNewTimer );
\r
310 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
\r
311 /*-----------------------------------------------------------*/
\r
313 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
\r
315 TimerHandle_t xTimerCreateStatic( const char * const pcTimerName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
\r
316 const TickType_t xTimerPeriodInTicks,
\r
317 const UBaseType_t uxAutoReload,
\r
318 void * const pvTimerID,
\r
319 TimerCallbackFunction_t pxCallbackFunction,
\r
320 StaticTimer_t * pxTimerBuffer )
\r
322 Timer_t * pxNewTimer;
\r
324 #if ( configASSERT_DEFINED == 1 )
\r
326 /* Sanity check that the size of the structure used to declare a
\r
327 * variable of type StaticTimer_t equals the size of the real timer
\r
329 volatile size_t xSize = sizeof( StaticTimer_t );
\r
330 configASSERT( xSize == sizeof( Timer_t ) );
\r
331 ( void ) xSize; /* Keeps lint quiet when configASSERT() is not defined. */
\r
333 #endif /* configASSERT_DEFINED */
\r
335 /* A pointer to a StaticTimer_t structure MUST be provided, use it. */
\r
336 configASSERT( pxTimerBuffer );
\r
337 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
339 if( pxNewTimer != NULL )
\r
341 /* Timers can be created statically or dynamically so note this
\r
342 * timer was created statically in case it is later deleted. The
\r
343 * auto-reload bit may get set in prvInitialiseNewTimer(). */
\r
344 pxNewTimer->ucStatus = tmrSTATUS_IS_STATICALLY_ALLOCATED;
\r
346 prvInitialiseNewTimer( pcTimerName, xTimerPeriodInTicks, uxAutoReload, pvTimerID, pxCallbackFunction, pxNewTimer );
\r
352 #endif /* configSUPPORT_STATIC_ALLOCATION */
\r
353 /*-----------------------------------------------------------*/
\r
355 static void prvInitialiseNewTimer( const char * const pcTimerName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
\r
356 const TickType_t xTimerPeriodInTicks,
\r
357 const UBaseType_t uxAutoReload,
\r
358 void * const pvTimerID,
\r
359 TimerCallbackFunction_t pxCallbackFunction,
\r
360 Timer_t * pxNewTimer )
\r
362 /* 0 is not a valid value for xTimerPeriodInTicks. */
\r
363 configASSERT( ( xTimerPeriodInTicks > 0 ) );
\r
365 /* Ensure the infrastructure used by the timer service task has been
\r
366 * created/initialised. */
\r
367 prvCheckForValidListAndQueue();
\r
369 /* Initialise the timer structure members using the function
\r
371 pxNewTimer->pcTimerName = pcTimerName;
\r
372 pxNewTimer->xTimerPeriodInTicks = xTimerPeriodInTicks;
\r
373 pxNewTimer->pvTimerID = pvTimerID;
\r
374 pxNewTimer->pxCallbackFunction = pxCallbackFunction;
\r
375 vListInitialiseItem( &( pxNewTimer->xTimerListItem ) );
\r
377 if( uxAutoReload != pdFALSE )
\r
379 pxNewTimer->ucStatus |= tmrSTATUS_IS_AUTORELOAD;
\r
382 traceTIMER_CREATE( pxNewTimer );
\r
384 /*-----------------------------------------------------------*/
\r
386 BaseType_t xTimerGenericCommand( TimerHandle_t xTimer,
\r
387 const BaseType_t xCommandID,
\r
388 const TickType_t xOptionalValue,
\r
389 BaseType_t * const pxHigherPriorityTaskWoken,
\r
390 const TickType_t xTicksToWait )
\r
392 BaseType_t xReturn = pdFAIL;
\r
393 DaemonTaskMessage_t xMessage;
\r
395 configASSERT( xTimer );
\r
397 /* Send a message to the timer service task to perform a particular action
\r
398 * on a particular timer definition. */
\r
399 if( xTimerQueue != NULL )
\r
401 /* Send a command to the timer service task to start the xTimer timer. */
\r
402 xMessage.xMessageID = xCommandID;
\r
403 xMessage.u.xTimerParameters.xMessageValue = xOptionalValue;
\r
404 xMessage.u.xTimerParameters.pxTimer = xTimer;
\r
406 if( xCommandID < tmrFIRST_FROM_ISR_COMMAND )
\r
408 if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )
\r
410 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xTicksToWait );
\r
414 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, tmrNO_DELAY );
\r
419 xReturn = xQueueSendToBackFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );
\r
422 traceTIMER_COMMAND_SEND( xTimer, xCommandID, xOptionalValue, xReturn );
\r
426 mtCOVERAGE_TEST_MARKER();
\r
431 /*-----------------------------------------------------------*/
\r
433 TaskHandle_t xTimerGetTimerDaemonTaskHandle( void )
\r
435 /* If xTimerGetTimerDaemonTaskHandle() is called before the scheduler has been
\r
436 * started, then xTimerTaskHandle will be NULL. */
\r
437 configASSERT( ( xTimerTaskHandle != NULL ) );
\r
438 return xTimerTaskHandle;
\r
440 /*-----------------------------------------------------------*/
\r
442 TickType_t xTimerGetPeriod( TimerHandle_t xTimer )
\r
444 Timer_t * pxTimer = xTimer;
\r
446 configASSERT( xTimer );
\r
447 return pxTimer->xTimerPeriodInTicks;
\r
449 /*-----------------------------------------------------------*/
\r
451 void vTimerSetReloadMode( TimerHandle_t xTimer,
\r
452 const UBaseType_t uxAutoReload )
\r
454 Timer_t * pxTimer = xTimer;
\r
456 configASSERT( xTimer );
\r
457 taskENTER_CRITICAL();
\r
459 if( uxAutoReload != pdFALSE )
\r
461 pxTimer->ucStatus |= tmrSTATUS_IS_AUTORELOAD;
\r
465 pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_AUTORELOAD );
\r
468 taskEXIT_CRITICAL();
\r
470 /*-----------------------------------------------------------*/
\r
472 UBaseType_t uxTimerGetReloadMode( TimerHandle_t xTimer )
\r
474 Timer_t * pxTimer = xTimer;
\r
475 UBaseType_t uxReturn;
\r
477 configASSERT( xTimer );
\r
478 taskENTER_CRITICAL();
\r
480 if( ( pxTimer->ucStatus & tmrSTATUS_IS_AUTORELOAD ) == 0 )
\r
482 /* Not an auto-reload timer. */
\r
483 uxReturn = ( UBaseType_t ) pdFALSE;
\r
487 /* Is an auto-reload timer. */
\r
488 uxReturn = ( UBaseType_t ) pdTRUE;
\r
491 taskEXIT_CRITICAL();
\r
495 /*-----------------------------------------------------------*/
\r
497 TickType_t xTimerGetExpiryTime( TimerHandle_t xTimer )
\r
499 Timer_t * pxTimer = xTimer;
\r
500 TickType_t xReturn;
\r
502 configASSERT( xTimer );
\r
503 xReturn = listGET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ) );
\r
506 /*-----------------------------------------------------------*/
\r
508 const char * pcTimerGetName( TimerHandle_t xTimer ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
\r
510 Timer_t * pxTimer = xTimer;
\r
512 configASSERT( xTimer );
\r
513 return pxTimer->pcTimerName;
\r
515 /*-----------------------------------------------------------*/
\r
517 static void prvReloadTimer( Timer_t * const pxTimer,
\r
518 TickType_t xExpiredTime,
\r
519 const TickType_t xTimeNow )
\r
521 /* Insert the timer into the appropriate list for the next expiry time.
\r
522 * If the next expiry time has already passed, advance the expiry time,
\r
523 * call the callback function, and try again. */
\r
524 while( prvInsertTimerInActiveList( pxTimer, ( xExpiredTime + pxTimer->xTimerPeriodInTicks ), xTimeNow, xExpiredTime ) != pdFALSE )
\r
526 /* Advance the expiry time. */
\r
527 xExpiredTime += pxTimer->xTimerPeriodInTicks;
\r
529 /* Call the timer callback. */
\r
530 traceTIMER_EXPIRED( pxTimer );
\r
531 pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
\r
534 /*-----------------------------------------------------------*/
\r
536 static void prvProcessExpiredTimer( const TickType_t xNextExpireTime,
\r
537 const TickType_t xTimeNow )
\r
539 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
541 /* Remove the timer from the list of active timers. A check has already
\r
542 * been performed to ensure the list is not empty. */
\r
544 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
\r
546 /* If the timer is an auto-reload timer then calculate the next
\r
547 * expiry time and re-insert the timer in the list of active timers. */
\r
548 if( ( pxTimer->ucStatus & tmrSTATUS_IS_AUTORELOAD ) != 0 )
\r
550 prvReloadTimer( pxTimer, xNextExpireTime, xTimeNow );
\r
554 pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_ACTIVE );
\r
557 /* Call the timer callback. */
\r
558 traceTIMER_EXPIRED( pxTimer );
\r
559 pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
\r
561 /*-----------------------------------------------------------*/
\r
563 static portTASK_FUNCTION( prvTimerTask, pvParameters )
\r
565 TickType_t xNextExpireTime;
\r
566 BaseType_t xListWasEmpty;
\r
568 /* Just to avoid compiler warnings. */
\r
569 ( void ) pvParameters;
\r
571 #if ( configUSE_DAEMON_TASK_STARTUP_HOOK == 1 )
\r
573 extern void vApplicationDaemonTaskStartupHook( void );
\r
575 /* Allow the application writer to execute some code in the context of
\r
576 * this task at the point the task starts executing. This is useful if the
\r
577 * application includes initialisation code that would benefit from
\r
578 * executing after the scheduler has been started. */
\r
579 vApplicationDaemonTaskStartupHook();
\r
581 #endif /* configUSE_DAEMON_TASK_STARTUP_HOOK */
\r
585 /* Query the timers list to see if it contains any timers, and if so,
\r
586 * obtain the time at which the next timer will expire. */
\r
587 xNextExpireTime = prvGetNextExpireTime( &xListWasEmpty );
\r
589 /* If a timer has expired, process it. Otherwise, block this task
\r
590 * until either a timer does expire, or a command is received. */
\r
591 prvProcessTimerOrBlockTask( xNextExpireTime, xListWasEmpty );
\r
593 /* Empty the command queue. */
\r
594 prvProcessReceivedCommands();
\r
597 /*-----------------------------------------------------------*/
\r
599 static void prvProcessTimerOrBlockTask( const TickType_t xNextExpireTime,
\r
600 BaseType_t xListWasEmpty )
\r
602 TickType_t xTimeNow;
\r
603 BaseType_t xTimerListsWereSwitched;
\r
607 /* Obtain the time now to make an assessment as to whether the timer
\r
608 * has expired or not. If obtaining the time causes the lists to switch
\r
609 * then don't process this timer as any timers that remained in the list
\r
610 * when the lists were switched will have been processed within the
\r
611 * prvSampleTimeNow() function. */
\r
612 xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );
\r
614 if( xTimerListsWereSwitched == pdFALSE )
\r
616 /* The tick count has not overflowed, has the timer expired? */
\r
617 if( ( xListWasEmpty == pdFALSE ) && ( xNextExpireTime <= xTimeNow ) )
\r
619 ( void ) xTaskResumeAll();
\r
620 prvProcessExpiredTimer( xNextExpireTime, xTimeNow );
\r
624 /* The tick count has not overflowed, and the next expire
\r
625 * time has not been reached yet. This task should therefore
\r
626 * block to wait for the next expire time or a command to be
\r
627 * received - whichever comes first. The following line cannot
\r
628 * be reached unless xNextExpireTime > xTimeNow, except in the
\r
629 * case when the current timer list is empty. */
\r
630 if( xListWasEmpty != pdFALSE )
\r
632 /* The current timer list is empty - is the overflow list
\r
634 xListWasEmpty = listLIST_IS_EMPTY( pxOverflowTimerList );
\r
637 vQueueWaitForMessageRestricted( xTimerQueue, ( xNextExpireTime - xTimeNow ), xListWasEmpty );
\r
639 if( xTaskResumeAll() == pdFALSE )
\r
641 /* Yield to wait for either a command to arrive, or the
\r
642 * block time to expire. If a command arrived between the
\r
643 * critical section being exited and this yield then the yield
\r
644 * will not cause the task to block. */
\r
645 portYIELD_WITHIN_API();
\r
649 mtCOVERAGE_TEST_MARKER();
\r
655 ( void ) xTaskResumeAll();
\r
659 /*-----------------------------------------------------------*/
\r
661 static TickType_t prvGetNextExpireTime( BaseType_t * const pxListWasEmpty )
\r
663 TickType_t xNextExpireTime;
\r
665 /* Timers are listed in expiry time order, with the head of the list
\r
666 * referencing the task that will expire first. Obtain the time at which
\r
667 * the timer with the nearest expiry time will expire. If there are no
\r
668 * active timers then just set the next expire time to 0. That will cause
\r
669 * this task to unblock when the tick count overflows, at which point the
\r
670 * timer lists will be switched and the next expiry time can be
\r
672 *pxListWasEmpty = listLIST_IS_EMPTY( pxCurrentTimerList );
\r
674 if( *pxListWasEmpty == pdFALSE )
\r
676 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );
\r
680 /* Ensure the task unblocks when the tick count rolls over. */
\r
681 xNextExpireTime = ( TickType_t ) 0U;
\r
684 return xNextExpireTime;
\r
686 /*-----------------------------------------------------------*/
\r
688 static TickType_t prvSampleTimeNow( BaseType_t * const pxTimerListsWereSwitched )
\r
690 TickType_t xTimeNow;
\r
691 PRIVILEGED_DATA static TickType_t xLastTime = ( TickType_t ) 0U; /*lint !e956 Variable is only accessible to one task. */
\r
693 xTimeNow = xTaskGetTickCount();
\r
695 if( xTimeNow < xLastTime )
\r
697 prvSwitchTimerLists();
\r
698 *pxTimerListsWereSwitched = pdTRUE;
\r
702 *pxTimerListsWereSwitched = pdFALSE;
\r
705 xLastTime = xTimeNow;
\r
709 /*-----------------------------------------------------------*/
\r
711 static BaseType_t prvInsertTimerInActiveList( Timer_t * const pxTimer,
\r
712 const TickType_t xNextExpiryTime,
\r
713 const TickType_t xTimeNow,
\r
714 const TickType_t xCommandTime )
\r
716 BaseType_t xProcessTimerNow = pdFALSE;
\r
718 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xNextExpiryTime );
\r
719 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );
\r
721 if( xNextExpiryTime <= xTimeNow )
\r
723 /* Has the expiry time elapsed between the command to start/reset a
\r
724 * timer was issued, and the time the command was processed? */
\r
725 if( ( ( TickType_t ) ( xTimeNow - xCommandTime ) ) >= pxTimer->xTimerPeriodInTicks ) /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
\r
727 /* The time between a command being issued and the command being
\r
728 * processed actually exceeds the timers period. */
\r
729 xProcessTimerNow = pdTRUE;
\r
733 vListInsert( pxOverflowTimerList, &( pxTimer->xTimerListItem ) );
\r
738 if( ( xTimeNow < xCommandTime ) && ( xNextExpiryTime >= xCommandTime ) )
\r
740 /* If, since the command was issued, the tick count has overflowed
\r
741 * but the expiry time has not, then the timer must have already passed
\r
742 * its expiry time and should be processed immediately. */
\r
743 xProcessTimerNow = pdTRUE;
\r
747 vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );
\r
751 return xProcessTimerNow;
\r
753 /*-----------------------------------------------------------*/
\r
755 static void prvProcessReceivedCommands( void )
\r
757 DaemonTaskMessage_t xMessage;
\r
759 BaseType_t xTimerListsWereSwitched;
\r
760 TickType_t xTimeNow;
\r
762 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
764 #if ( INCLUDE_xTimerPendFunctionCall == 1 )
\r
766 /* Negative commands are pended function calls rather than timer
\r
768 if( xMessage.xMessageID < ( BaseType_t ) 0 )
\r
770 const CallbackParameters_t * const pxCallback = &( xMessage.u.xCallbackParameters );
\r
772 /* The timer uses the xCallbackParameters member to request a
\r
773 * callback be executed. Check the callback is not NULL. */
\r
774 configASSERT( pxCallback );
\r
776 /* Call the function. */
\r
777 pxCallback->pxCallbackFunction( pxCallback->pvParameter1, pxCallback->ulParameter2 );
\r
781 mtCOVERAGE_TEST_MARKER();
\r
784 #endif /* INCLUDE_xTimerPendFunctionCall */
\r
786 /* Commands that are positive are timer commands rather than pended
\r
787 * function calls. */
\r
788 if( xMessage.xMessageID >= ( BaseType_t ) 0 )
\r
790 /* The messages uses the xTimerParameters member to work on a
\r
791 * software timer. */
\r
792 pxTimer = xMessage.u.xTimerParameters.pxTimer;
\r
794 if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE ) /*lint !e961. The cast is only redundant when NULL is passed into the macro. */
\r
796 /* The timer is in a list, remove it. */
\r
797 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
\r
801 mtCOVERAGE_TEST_MARKER();
\r
804 traceTIMER_COMMAND_RECEIVED( pxTimer, xMessage.xMessageID, xMessage.u.xTimerParameters.xMessageValue );
\r
806 /* In this case the xTimerListsWereSwitched parameter is not used, but
\r
807 * it must be present in the function call. prvSampleTimeNow() must be
\r
808 * called after the message is received from xTimerQueue so there is no
\r
809 * possibility of a higher priority task adding a message to the message
\r
810 * queue with a time that is ahead of the timer daemon task (because it
\r
811 * pre-empted the timer daemon task after the xTimeNow value was set). */
\r
812 xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );
\r
814 switch( xMessage.xMessageID )
\r
816 case tmrCOMMAND_START:
\r
817 case tmrCOMMAND_START_FROM_ISR:
\r
818 case tmrCOMMAND_RESET:
\r
819 case tmrCOMMAND_RESET_FROM_ISR:
\r
820 /* Start or restart a timer. */
\r
821 pxTimer->ucStatus |= tmrSTATUS_IS_ACTIVE;
\r
823 if( prvInsertTimerInActiveList( pxTimer, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow, xMessage.u.xTimerParameters.xMessageValue ) != pdFALSE )
\r
825 /* The timer expired before it was added to the active
\r
826 * timer list. Process it now. */
\r
827 if( ( pxTimer->ucStatus & tmrSTATUS_IS_AUTORELOAD ) != 0 )
\r
829 prvReloadTimer( pxTimer, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow );
\r
833 pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_ACTIVE );
\r
836 /* Call the timer callback. */
\r
837 traceTIMER_EXPIRED( pxTimer );
\r
838 pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
\r
842 mtCOVERAGE_TEST_MARKER();
\r
847 case tmrCOMMAND_STOP:
\r
848 case tmrCOMMAND_STOP_FROM_ISR:
\r
849 /* The timer has already been removed from the active list. */
\r
850 pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_ACTIVE );
\r
853 case tmrCOMMAND_CHANGE_PERIOD:
\r
854 case tmrCOMMAND_CHANGE_PERIOD_FROM_ISR:
\r
855 pxTimer->ucStatus |= tmrSTATUS_IS_ACTIVE;
\r
856 pxTimer->xTimerPeriodInTicks = xMessage.u.xTimerParameters.xMessageValue;
\r
857 configASSERT( ( pxTimer->xTimerPeriodInTicks > 0 ) );
\r
859 /* The new period does not really have a reference, and can
\r
860 * be longer or shorter than the old one. The command time is
\r
861 * therefore set to the current time, and as the period cannot
\r
862 * be zero the next expiry time can only be in the future,
\r
863 * meaning (unlike for the xTimerStart() case above) there is
\r
864 * no fail case that needs to be handled here. */
\r
865 ( void ) prvInsertTimerInActiveList( pxTimer, ( xTimeNow + pxTimer->xTimerPeriodInTicks ), xTimeNow, xTimeNow );
\r
868 case tmrCOMMAND_DELETE:
\r
869 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
871 /* The timer has already been removed from the active list,
\r
872 * just free up the memory if the memory was dynamically
\r
874 if( ( pxTimer->ucStatus & tmrSTATUS_IS_STATICALLY_ALLOCATED ) == ( uint8_t ) 0 )
\r
876 vPortFree( pxTimer );
\r
880 pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_ACTIVE );
\r
883 #else /* if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) */
\r
885 /* If dynamic allocation is not enabled, the memory
\r
886 * could not have been dynamically allocated. So there is
\r
887 * no need to free the memory - just mark the timer as
\r
889 pxTimer->ucStatus &= ( ( uint8_t ) ~tmrSTATUS_IS_ACTIVE );
\r
891 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
\r
895 /* Don't expect to get here. */
\r
901 /*-----------------------------------------------------------*/
\r
903 static void prvSwitchTimerLists( void )
\r
905 TickType_t xNextExpireTime;
\r
908 /* The tick count has overflowed. The timer lists must be switched.
\r
909 * If there are any timers still referenced from the current timer list
\r
910 * then they must have expired and should be processed before the lists
\r
912 while( listLIST_IS_EMPTY( pxCurrentTimerList ) == pdFALSE )
\r
914 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );
\r
916 /* Process the expired timer. For auto-reload timers, be careful to
\r
917 * process only expirations that occur on the current list. Further
\r
918 * expirations must wait until after the lists are switched. */
\r
919 prvProcessExpiredTimer( xNextExpireTime, tmrMAX_TIME_BEFORE_OVERFLOW );
\r
922 pxTemp = pxCurrentTimerList;
\r
923 pxCurrentTimerList = pxOverflowTimerList;
\r
924 pxOverflowTimerList = pxTemp;
\r
926 /*-----------------------------------------------------------*/
\r
928 static void prvCheckForValidListAndQueue( void )
\r
930 /* Check that the list from which active timers are referenced, and the
\r
931 * queue used to communicate with the timer service, have been
\r
933 taskENTER_CRITICAL();
\r
935 if( xTimerQueue == NULL )
\r
937 vListInitialise( &xActiveTimerList1 );
\r
938 vListInitialise( &xActiveTimerList2 );
\r
939 pxCurrentTimerList = &xActiveTimerList1;
\r
940 pxOverflowTimerList = &xActiveTimerList2;
\r
942 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
\r
944 /* The timer queue is allocated statically in case
\r
945 * configSUPPORT_DYNAMIC_ALLOCATION is 0. */
\r
946 PRIVILEGED_DATA static StaticQueue_t xStaticTimerQueue; /*lint !e956 Ok to declare in this manner to prevent additional conditional compilation guards in other locations. */
\r
947 PRIVILEGED_DATA 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
949 xTimerQueue = xQueueCreateStatic( ( UBaseType_t ) configTIMER_QUEUE_LENGTH, ( UBaseType_t ) sizeof( DaemonTaskMessage_t ), &( ucStaticTimerQueueStorage[ 0 ] ), &xStaticTimerQueue );
\r
953 xTimerQueue = xQueueCreate( ( UBaseType_t ) configTIMER_QUEUE_LENGTH, sizeof( DaemonTaskMessage_t ) );
\r
955 #endif /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
\r
957 #if ( configQUEUE_REGISTRY_SIZE > 0 )
\r
959 if( xTimerQueue != NULL )
\r
961 vQueueAddToRegistry( xTimerQueue, "TmrQ" );
\r
965 mtCOVERAGE_TEST_MARKER();
\r
968 #endif /* configQUEUE_REGISTRY_SIZE */
\r
972 mtCOVERAGE_TEST_MARKER();
\r
975 taskEXIT_CRITICAL();
\r
977 /*-----------------------------------------------------------*/
\r
979 BaseType_t xTimerIsTimerActive( TimerHandle_t xTimer )
\r
981 BaseType_t xReturn;
\r
982 Timer_t * pxTimer = xTimer;
\r
984 configASSERT( xTimer );
\r
986 /* Is the timer in the list of active timers? */
\r
987 taskENTER_CRITICAL();
\r
989 if( ( pxTimer->ucStatus & tmrSTATUS_IS_ACTIVE ) == 0 )
\r
998 taskEXIT_CRITICAL();
\r
1001 } /*lint !e818 Can't be pointer to const due to the typedef. */
\r
1002 /*-----------------------------------------------------------*/
\r
1004 void * pvTimerGetTimerID( const TimerHandle_t xTimer )
\r
1006 Timer_t * const pxTimer = xTimer;
\r
1009 configASSERT( xTimer );
\r
1011 taskENTER_CRITICAL();
\r
1013 pvReturn = pxTimer->pvTimerID;
\r
1015 taskEXIT_CRITICAL();
\r
1019 /*-----------------------------------------------------------*/
\r
1021 void vTimerSetTimerID( TimerHandle_t xTimer,
\r
1024 Timer_t * const pxTimer = xTimer;
\r
1026 configASSERT( xTimer );
\r
1028 taskENTER_CRITICAL();
\r
1030 pxTimer->pvTimerID = pvNewID;
\r
1032 taskEXIT_CRITICAL();
\r
1034 /*-----------------------------------------------------------*/
\r
1036 #if ( INCLUDE_xTimerPendFunctionCall == 1 )
\r
1038 BaseType_t xTimerPendFunctionCallFromISR( PendedFunction_t xFunctionToPend,
\r
1039 void * pvParameter1,
\r
1040 uint32_t ulParameter2,
\r
1041 BaseType_t * pxHigherPriorityTaskWoken )
\r
1043 DaemonTaskMessage_t xMessage;
\r
1044 BaseType_t xReturn;
\r
1046 /* Complete the message with the function parameters and post it to the
\r
1048 xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK_FROM_ISR;
\r
1049 xMessage.u.xCallbackParameters.pxCallbackFunction = xFunctionToPend;
\r
1050 xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;
\r
1051 xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;
\r
1053 xReturn = xQueueSendFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );
\r
1055 tracePEND_FUNC_CALL_FROM_ISR( xFunctionToPend, pvParameter1, ulParameter2, xReturn );
\r
1060 #endif /* INCLUDE_xTimerPendFunctionCall */
\r
1061 /*-----------------------------------------------------------*/
\r
1063 #if ( INCLUDE_xTimerPendFunctionCall == 1 )
\r
1065 BaseType_t xTimerPendFunctionCall( PendedFunction_t xFunctionToPend,
\r
1066 void * pvParameter1,
\r
1067 uint32_t ulParameter2,
\r
1068 TickType_t xTicksToWait )
\r
1070 DaemonTaskMessage_t xMessage;
\r
1071 BaseType_t xReturn;
\r
1073 /* This function can only be called after a timer has been created or
\r
1074 * after the scheduler has been started because, until then, the timer
\r
1075 * queue does not exist. */
\r
1076 configASSERT( xTimerQueue );
\r
1078 /* Complete the message with the function parameters and post it to the
\r
1080 xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK;
\r
1081 xMessage.u.xCallbackParameters.pxCallbackFunction = xFunctionToPend;
\r
1082 xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;
\r
1083 xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;
\r
1085 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xTicksToWait );
\r
1087 tracePEND_FUNC_CALL( xFunctionToPend, pvParameter1, ulParameter2, xReturn );
\r
1092 #endif /* INCLUDE_xTimerPendFunctionCall */
\r
1093 /*-----------------------------------------------------------*/
\r
1095 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1097 UBaseType_t uxTimerGetTimerNumber( TimerHandle_t xTimer )
\r
1099 return ( ( Timer_t * ) xTimer )->uxTimerNumber;
\r
1102 #endif /* configUSE_TRACE_FACILITY */
\r
1103 /*-----------------------------------------------------------*/
\r
1105 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1107 void vTimerSetTimerNumber( TimerHandle_t xTimer,
\r
1108 UBaseType_t uxTimerNumber )
\r
1110 ( ( Timer_t * ) xTimer )->uxTimerNumber = uxTimerNumber;
\r
1113 #endif /* configUSE_TRACE_FACILITY */
\r
1114 /*-----------------------------------------------------------*/
\r
1116 /* This entire source file will be skipped if the application is not configured
\r
1117 * to include software timer functionality. If you want to include software timer
\r
1118 * functionality then ensure configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */
\r
1119 #endif /* configUSE_TIMERS == 1 */
\r