2 FreeRTOS V8.0.0 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 /* Standard includes. */
\r
69 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
70 all the API functions to use the MPU wrappers. That should only be done when
\r
71 task.h is included from an application file. */
\r
72 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
74 #include "FreeRTOS.h"
\r
79 #if ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 0 )
\r
80 #error configUSE_TIMERS must be set to 1 to make the xTimerPendFunctionCall() function available.
\r
83 /* Lint e961 and e750 are suppressed as a MISRA exception justified because the
\r
84 MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined for the
\r
85 header files above, but not in this file, in order to generate the correct
\r
86 privileged Vs unprivileged linkage and placement. */
\r
87 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750. */
\r
90 /* This entire source file will be skipped if the application is not configured
\r
91 to include software timer functionality. This #if is closed at the very bottom
\r
92 of this file. If you want to include software timer functionality then ensure
\r
93 configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */
\r
94 #if ( configUSE_TIMERS == 1 )
\r
96 /* Misc definitions. */
\r
97 #define tmrNO_DELAY ( TickType_t ) 0U
\r
99 /* The definition of the timers themselves. */
\r
100 typedef struct tmrTimerControl
\r
102 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
103 ListItem_t xTimerListItem; /*<< Standard linked list item as used by all kernel features for event management. */
\r
104 TickType_t xTimerPeriodInTicks;/*<< How quickly and often the timer expires. */
\r
105 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
106 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
107 TimerCallbackFunction_t pxCallbackFunction; /*<< The function that will be called when the timer expires. */
\r
108 #if( configUSE_TRACE_FACILITY == 1 )
\r
109 UBaseType_t uxTimerNumber; /*<< An ID assigned by trace tools such as FreeRTOS+Trace */
\r
113 /* The old xTIMER name is maintained above then typedefed to the new Timer_t
\r
114 name below to enable the use of older kernel aware debuggers. */
\r
115 typedef xTIMER Timer_t;
\r
117 /* The definition of messages that can be sent and received on the timer queue.
\r
118 Two types of message can be queued - messages that manipulate a software timer,
\r
119 and messages that request the execution of a non-timer related callback. The
\r
120 two message types are defined in two separate structures, xTimerParametersType
\r
121 and xCallbackParametersType respectively. */
\r
122 typedef struct tmrTimerParameters
\r
124 TickType_t xMessageValue; /*<< An optional value used by a subset of commands, for example, when changing the period of a timer. */
\r
125 Timer_t * pxTimer; /*<< The timer to which the command will be applied. */
\r
126 } TimerParameter_t;
\r
129 typedef struct tmrCallbackParameters
\r
131 PendedFunction_t pxCallbackFunction; /* << The callback function to execute. */
\r
132 void *pvParameter1; /* << The value that will be used as the callback functions first parameter. */
\r
133 uint32_t ulParameter2; /* << The value that will be used as the callback functions second parameter. */
\r
134 } CallbackParameters_t;
\r
136 /* The structure that contains the two message types, along with an identifier
\r
137 that is used to determine which message type is valid. */
\r
138 typedef struct tmrTimerQueueMessage
\r
140 BaseType_t xMessageID; /*<< The command being sent to the timer service task. */
\r
143 TimerParameter_t xTimerParameters;
\r
145 /* Don't include xCallbackParameters if it is not going to be used as
\r
146 it makes the structure (and therefore the timer queue) larger. */
\r
147 #if ( INCLUDE_xTimerPendFunctionCall == 1 )
\r
148 CallbackParameters_t xCallbackParameters;
\r
149 #endif /* INCLUDE_xTimerPendFunctionCall */
\r
151 } DaemonTaskMessage_t;
\r
153 /*lint -e956 A manual analysis and inspection has been used to determine which
\r
154 static variables must be declared volatile. */
\r
156 /* The list in which active timers are stored. Timers are referenced in expire
\r
157 time order, with the nearest expiry time at the front of the list. Only the
\r
158 timer service task is allowed to access these lists. */
\r
159 PRIVILEGED_DATA static List_t xActiveTimerList1;
\r
160 PRIVILEGED_DATA static List_t xActiveTimerList2;
\r
161 PRIVILEGED_DATA static List_t *pxCurrentTimerList;
\r
162 PRIVILEGED_DATA static List_t *pxOverflowTimerList;
\r
164 /* A queue that is used to send commands to the timer service task. */
\r
165 PRIVILEGED_DATA static QueueHandle_t xTimerQueue = NULL;
\r
167 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )
\r
169 PRIVILEGED_DATA static TaskHandle_t xTimerTaskHandle = NULL;
\r
175 /*-----------------------------------------------------------*/
\r
178 * Initialise the infrastructure used by the timer service task if it has not
\r
179 * been initialised already.
\r
181 static void prvCheckForValidListAndQueue( void ) PRIVILEGED_FUNCTION;
\r
184 * The timer service task (daemon). Timer functionality is controlled by this
\r
185 * task. Other tasks communicate with the timer service task using the
\r
186 * xTimerQueue queue.
\r
188 static void prvTimerTask( void *pvParameters ) PRIVILEGED_FUNCTION;
\r
191 * Called by the timer service task to interpret and process a command it
\r
192 * received on the timer queue.
\r
194 static void prvProcessReceivedCommands( void ) PRIVILEGED_FUNCTION;
\r
197 * Insert the timer into either xActiveTimerList1, or xActiveTimerList2,
\r
198 * depending on if the expire time causes a timer counter overflow.
\r
200 static BaseType_t prvInsertTimerInActiveList( Timer_t * const pxTimer, const TickType_t xNextExpiryTime, const TickType_t xTimeNow, const TickType_t xCommandTime ) PRIVILEGED_FUNCTION;
\r
203 * An active timer has reached its expire time. Reload the timer if it is an
\r
204 * auto reload timer, then call its callback.
\r
206 static void prvProcessExpiredTimer( const TickType_t xNextExpireTime, const TickType_t xTimeNow ) PRIVILEGED_FUNCTION;
\r
209 * The tick count has overflowed. Switch the timer lists after ensuring the
\r
210 * current timer list does not still reference some timers.
\r
212 static void prvSwitchTimerLists( void ) PRIVILEGED_FUNCTION;
\r
215 * Obtain the current tick count, setting *pxTimerListsWereSwitched to pdTRUE
\r
216 * if a tick count overflow occurred since prvSampleTimeNow() was last called.
\r
218 static TickType_t prvSampleTimeNow( BaseType_t * const pxTimerListsWereSwitched ) PRIVILEGED_FUNCTION;
\r
221 * If the timer list contains any active timers then return the expire time of
\r
222 * the timer that will expire first and set *pxListWasEmpty to false. If the
\r
223 * timer list does not contain any timers then return 0 and set *pxListWasEmpty
\r
226 static TickType_t prvGetNextExpireTime( BaseType_t * const pxListWasEmpty ) PRIVILEGED_FUNCTION;
\r
229 * If a timer has expired, process it. Otherwise, block the timer service task
\r
230 * until either a timer does expire or a command is received.
\r
232 static void prvProcessTimerOrBlockTask( const TickType_t xNextExpireTime, const BaseType_t xListWasEmpty ) PRIVILEGED_FUNCTION;
\r
234 /*-----------------------------------------------------------*/
\r
236 BaseType_t xTimerCreateTimerTask( void )
\r
238 BaseType_t xReturn = pdFAIL;
\r
240 /* This function is called when the scheduler is started if
\r
241 configUSE_TIMERS is set to 1. Check that the infrastructure used by the
\r
242 timer service task has been created/initialised. If timers have already
\r
243 been created then the initialisation will already have been performed. */
\r
244 prvCheckForValidListAndQueue();
\r
246 if( xTimerQueue != NULL )
\r
248 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )
\r
250 /* Create the timer task, storing its handle in xTimerTaskHandle so
\r
251 it can be returned by the xTimerGetTimerDaemonTaskHandle() function. */
\r
252 xReturn = xTaskCreate( prvTimerTask, "Tmr Svc", ( uint16_t ) configTIMER_TASK_STACK_DEPTH, NULL, ( ( UBaseType_t ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT, &xTimerTaskHandle );
\r
256 /* Create the timer task without storing its handle. */
\r
257 xReturn = xTaskCreate( prvTimerTask, "Tmr Svc", ( uint16_t ) configTIMER_TASK_STACK_DEPTH, NULL, ( ( UBaseType_t ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT, NULL);
\r
263 mtCOVERAGE_TEST_MARKER();
\r
266 configASSERT( xReturn );
\r
269 /*-----------------------------------------------------------*/
\r
271 TimerHandle_t xTimerCreate( const char * const pcTimerName, const TickType_t xTimerPeriodInTicks, const UBaseType_t uxAutoReload, void * const pvTimerID, TimerCallbackFunction_t pxCallbackFunction ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
\r
273 Timer_t *pxNewTimer;
\r
275 /* Allocate the timer structure. */
\r
276 if( xTimerPeriodInTicks == ( TickType_t ) 0U )
\r
282 pxNewTimer = ( Timer_t * ) pvPortMalloc( sizeof( Timer_t ) );
\r
283 if( pxNewTimer != NULL )
\r
285 /* Ensure the infrastructure used by the timer service task has been
\r
286 created/initialised. */
\r
287 prvCheckForValidListAndQueue();
\r
289 /* Initialise the timer structure members using the function parameters. */
\r
290 pxNewTimer->pcTimerName = pcTimerName;
\r
291 pxNewTimer->xTimerPeriodInTicks = xTimerPeriodInTicks;
\r
292 pxNewTimer->uxAutoReload = uxAutoReload;
\r
293 pxNewTimer->pvTimerID = pvTimerID;
\r
294 pxNewTimer->pxCallbackFunction = pxCallbackFunction;
\r
295 vListInitialiseItem( &( pxNewTimer->xTimerListItem ) );
\r
297 traceTIMER_CREATE( pxNewTimer );
\r
301 traceTIMER_CREATE_FAILED();
\r
305 /* 0 is not a valid value for xTimerPeriodInTicks. */
\r
306 configASSERT( ( xTimerPeriodInTicks > 0 ) );
\r
308 return ( TimerHandle_t ) pxNewTimer;
\r
310 /*-----------------------------------------------------------*/
\r
312 BaseType_t xTimerGenericCommand( TimerHandle_t xTimer, const BaseType_t xCommandID, const TickType_t xOptionalValue, BaseType_t * const pxHigherPriorityTaskWoken, const TickType_t xTicksToWait )
\r
314 BaseType_t xReturn = pdFAIL;
\r
315 DaemonTaskMessage_t xMessage;
\r
317 /* Send a message to the timer service task to perform a particular action
\r
318 on a particular timer definition. */
\r
319 if( xTimerQueue != NULL )
\r
321 /* Send a command to the timer service task to start the xTimer timer. */
\r
322 xMessage.xMessageID = xCommandID;
\r
323 xMessage.u.xTimerParameters.xMessageValue = xOptionalValue;
\r
324 xMessage.u.xTimerParameters.pxTimer = ( Timer_t * ) xTimer;
\r
326 if( xCommandID < tmrFIRST_FROM_ISR_COMMAND )
\r
328 if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )
\r
330 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xTicksToWait );
\r
334 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, tmrNO_DELAY );
\r
339 xReturn = xQueueSendToBackFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );
\r
342 traceTIMER_COMMAND_SEND( xTimer, xCommandID, xOptionalValue, xReturn );
\r
346 mtCOVERAGE_TEST_MARKER();
\r
351 /*-----------------------------------------------------------*/
\r
353 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )
\r
355 TaskHandle_t xTimerGetTimerDaemonTaskHandle( void )
\r
357 /* If xTimerGetTimerDaemonTaskHandle() is called before the scheduler has been
\r
358 started, then xTimerTaskHandle will be NULL. */
\r
359 configASSERT( ( xTimerTaskHandle != NULL ) );
\r
360 return xTimerTaskHandle;
\r
364 /*-----------------------------------------------------------*/
\r
366 static void prvProcessExpiredTimer( const TickType_t xNextExpireTime, const TickType_t xTimeNow )
\r
368 BaseType_t xResult;
\r
369 Timer_t * const pxTimer = ( Timer_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );
\r
371 /* Remove the timer from the list of active timers. A check has already
\r
372 been performed to ensure the list is not empty. */
\r
373 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
\r
374 traceTIMER_EXPIRED( pxTimer );
\r
376 /* If the timer is an auto reload timer then calculate the next
\r
377 expiry time and re-insert the timer in the list of active timers. */
\r
378 if( pxTimer->uxAutoReload == ( UBaseType_t ) pdTRUE )
\r
380 /* The timer is inserted into a list using a time relative to anything
\r
381 other than the current time. It will therefore be inserted into the
\r
382 correct list relative to the time this task thinks it is now. */
\r
383 if( prvInsertTimerInActiveList( pxTimer, ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ), xTimeNow, xNextExpireTime ) == pdTRUE )
\r
385 /* The timer expired before it was added to the active timer
\r
386 list. Reload it now. */
\r
387 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START_DONT_TRACE, xNextExpireTime, NULL, tmrNO_DELAY );
\r
388 configASSERT( xResult );
\r
393 mtCOVERAGE_TEST_MARKER();
\r
398 mtCOVERAGE_TEST_MARKER();
\r
401 /* Call the timer callback. */
\r
402 pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
\r
404 /*-----------------------------------------------------------*/
\r
406 static void prvTimerTask( void *pvParameters )
\r
408 TickType_t xNextExpireTime;
\r
409 BaseType_t xListWasEmpty;
\r
411 /* Just to avoid compiler warnings. */
\r
412 ( void ) pvParameters;
\r
416 /* Query the timers list to see if it contains any timers, and if so,
\r
417 obtain the time at which the next timer will expire. */
\r
418 xNextExpireTime = prvGetNextExpireTime( &xListWasEmpty );
\r
420 /* If a timer has expired, process it. Otherwise, block this task
\r
421 until either a timer does expire, or a command is received. */
\r
422 prvProcessTimerOrBlockTask( xNextExpireTime, xListWasEmpty );
\r
424 /* Empty the command queue. */
\r
425 prvProcessReceivedCommands();
\r
428 /*-----------------------------------------------------------*/
\r
430 static void prvProcessTimerOrBlockTask( const TickType_t xNextExpireTime, const BaseType_t xListWasEmpty )
\r
432 TickType_t xTimeNow;
\r
433 BaseType_t xTimerListsWereSwitched;
\r
437 /* Obtain the time now to make an assessment as to whether the timer
\r
438 has expired or not. If obtaining the time causes the lists to switch
\r
439 then don't process this timer as any timers that remained in the list
\r
440 when the lists were switched will have been processed within the
\r
441 prvSampleTimeNow() function. */
\r
442 xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );
\r
443 if( xTimerListsWereSwitched == pdFALSE )
\r
445 /* The tick count has not overflowed, has the timer expired? */
\r
446 if( ( xListWasEmpty == pdFALSE ) && ( xNextExpireTime <= xTimeNow ) )
\r
448 ( void ) xTaskResumeAll();
\r
449 prvProcessExpiredTimer( xNextExpireTime, xTimeNow );
\r
453 /* The tick count has not overflowed, and the next expire
\r
454 time has not been reached yet. This task should therefore
\r
455 block to wait for the next expire time or a command to be
\r
456 received - whichever comes first. The following line cannot
\r
457 be reached unless xNextExpireTime > xTimeNow, except in the
\r
458 case when the current timer list is empty. */
\r
459 vQueueWaitForMessageRestricted( xTimerQueue, ( xNextExpireTime - xTimeNow ) );
\r
461 if( xTaskResumeAll() == pdFALSE )
\r
463 /* Yield to wait for either a command to arrive, or the block time
\r
464 to expire. If a command arrived between the critical section being
\r
465 exited and this yield then the yield will not cause the task
\r
467 portYIELD_WITHIN_API();
\r
471 mtCOVERAGE_TEST_MARKER();
\r
477 ( void ) xTaskResumeAll();
\r
481 /*-----------------------------------------------------------*/
\r
483 static TickType_t prvGetNextExpireTime( BaseType_t * const pxListWasEmpty )
\r
485 TickType_t xNextExpireTime;
\r
487 /* Timers are listed in expiry time order, with the head of the list
\r
488 referencing the task that will expire first. Obtain the time at which
\r
489 the timer with the nearest expiry time will expire. If there are no
\r
490 active timers then just set the next expire time to 0. That will cause
\r
491 this task to unblock when the tick count overflows, at which point the
\r
492 timer lists will be switched and the next expiry time can be
\r
494 *pxListWasEmpty = listLIST_IS_EMPTY( pxCurrentTimerList );
\r
495 if( *pxListWasEmpty == pdFALSE )
\r
497 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );
\r
501 /* Ensure the task unblocks when the tick count rolls over. */
\r
502 xNextExpireTime = ( TickType_t ) 0U;
\r
505 return xNextExpireTime;
\r
507 /*-----------------------------------------------------------*/
\r
509 static TickType_t prvSampleTimeNow( BaseType_t * const pxTimerListsWereSwitched )
\r
511 TickType_t xTimeNow;
\r
512 PRIVILEGED_DATA static TickType_t xLastTime = ( TickType_t ) 0U; /*lint !e956 Variable is only accessible to one task. */
\r
514 xTimeNow = xTaskGetTickCount();
\r
516 if( xTimeNow < xLastTime )
\r
518 prvSwitchTimerLists();
\r
519 *pxTimerListsWereSwitched = pdTRUE;
\r
523 *pxTimerListsWereSwitched = pdFALSE;
\r
526 xLastTime = xTimeNow;
\r
530 /*-----------------------------------------------------------*/
\r
532 static BaseType_t prvInsertTimerInActiveList( Timer_t * const pxTimer, const TickType_t xNextExpiryTime, const TickType_t xTimeNow, const TickType_t xCommandTime )
\r
534 BaseType_t xProcessTimerNow = pdFALSE;
\r
536 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xNextExpiryTime );
\r
537 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );
\r
539 if( xNextExpiryTime <= xTimeNow )
\r
541 /* Has the expiry time elapsed between the command to start/reset a
\r
542 timer was issued, and the time the command was processed? */
\r
543 if( ( xTimeNow - xCommandTime ) >= pxTimer->xTimerPeriodInTicks )
\r
545 /* The time between a command being issued and the command being
\r
546 processed actually exceeds the timers period. */
\r
547 xProcessTimerNow = pdTRUE;
\r
551 vListInsert( pxOverflowTimerList, &( pxTimer->xTimerListItem ) );
\r
556 if( ( xTimeNow < xCommandTime ) && ( xNextExpiryTime >= xCommandTime ) )
\r
558 /* If, since the command was issued, the tick count has overflowed
\r
559 but the expiry time has not, then the timer must have already passed
\r
560 its expiry time and should be processed immediately. */
\r
561 xProcessTimerNow = pdTRUE;
\r
565 vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );
\r
569 return xProcessTimerNow;
\r
571 /*-----------------------------------------------------------*/
\r
573 static void prvProcessReceivedCommands( void )
\r
575 DaemonTaskMessage_t xMessage;
\r
577 BaseType_t xTimerListsWereSwitched, xResult;
\r
578 TickType_t xTimeNow;
\r
580 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
582 #if ( INCLUDE_xTimerPendFunctionCall == 1 )
\r
584 /* Negative commands are pended function calls rather than timer
\r
586 if( xMessage.xMessageID < 0 )
\r
588 const CallbackParameters_t * const pxCallback = &( xMessage.u.xCallbackParameters );
\r
590 /* The timer uses the xCallbackParameters member to request a
\r
591 callback be executed. Check the callback is not NULL. */
\r
592 configASSERT( pxCallback );
\r
594 /* Call the function. */
\r
595 pxCallback->pxCallbackFunction( pxCallback->pvParameter1, pxCallback->ulParameter2 );
\r
599 mtCOVERAGE_TEST_MARKER();
\r
602 #endif /* INCLUDE_xTimerPendFunctionCall */
\r
604 /* Commands that are positive are timer commands rather than pended
\r
606 if( xMessage.xMessageID >= ( BaseType_t ) 0 )
\r
608 /* The messages uses the xTimerParameters member to work on a
\r
610 pxTimer = xMessage.u.xTimerParameters.pxTimer;
\r
612 if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )
\r
614 /* The timer is in a list, remove it. */
\r
615 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
\r
619 mtCOVERAGE_TEST_MARKER();
\r
622 traceTIMER_COMMAND_RECEIVED( pxTimer, xMessage.xMessageID, xMessage.u.xTimerParameters.xMessageValue );
\r
624 /* In this case the xTimerListsWereSwitched parameter is not used, but
\r
625 it must be present in the function call. prvSampleTimeNow() must be
\r
626 called after the message is received from xTimerQueue so there is no
\r
627 possibility of a higher priority task adding a message to the message
\r
628 queue with a time that is ahead of the timer daemon task (because it
\r
629 pre-empted the timer daemon task after the xTimeNow value was set). */
\r
630 xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );
\r
632 switch( xMessage.xMessageID )
\r
634 case tmrCOMMAND_START :
\r
635 case tmrCOMMAND_START_FROM_ISR :
\r
636 case tmrCOMMAND_RESET :
\r
637 case tmrCOMMAND_RESET_FROM_ISR :
\r
638 case tmrCOMMAND_START_DONT_TRACE :
\r
639 /* Start or restart a timer. */
\r
640 if( prvInsertTimerInActiveList( pxTimer, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow, xMessage.u.xTimerParameters.xMessageValue ) == pdTRUE )
\r
642 /* The timer expired before it was added to the active
\r
643 timer list. Process it now. */
\r
644 pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
\r
645 traceTIMER_EXPIRED( pxTimer );
\r
647 if( pxTimer->uxAutoReload == ( UBaseType_t ) pdTRUE )
\r
649 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START_DONT_TRACE, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, NULL, tmrNO_DELAY );
\r
650 configASSERT( xResult );
\r
655 mtCOVERAGE_TEST_MARKER();
\r
660 mtCOVERAGE_TEST_MARKER();
\r
664 case tmrCOMMAND_STOP :
\r
665 case tmrCOMMAND_STOP_FROM_ISR :
\r
666 /* The timer has already been removed from the active list.
\r
667 There is nothing to do here. */
\r
670 case tmrCOMMAND_CHANGE_PERIOD :
\r
671 case tmrCOMMAND_CHANGE_PERIOD_FROM_ISR :
\r
672 pxTimer->xTimerPeriodInTicks = xMessage.u.xTimerParameters.xMessageValue;
\r
673 configASSERT( ( pxTimer->xTimerPeriodInTicks > 0 ) );
\r
675 /* The new period does not really have a reference, and can be
\r
676 longer or shorter than the old one. The command time is
\r
677 therefore set to the current time, and as the period cannot be
\r
678 zero the next expiry time can only be in the future, meaning
\r
679 (unlike for the xTimerStart() case above) there is no fail case
\r
680 that needs to be handled here. */
\r
681 ( void ) prvInsertTimerInActiveList( pxTimer, ( xTimeNow + pxTimer->xTimerPeriodInTicks ), xTimeNow, xTimeNow );
\r
684 case tmrCOMMAND_DELETE :
\r
685 /* The timer has already been removed from the active list,
\r
686 just free up the memory. */
\r
687 vPortFree( pxTimer );
\r
691 /* Don't expect to get here. */
\r
697 /*-----------------------------------------------------------*/
\r
699 static void prvSwitchTimerLists( void )
\r
701 TickType_t xNextExpireTime, xReloadTime;
\r
704 BaseType_t xResult;
\r
706 /* The tick count has overflowed. The timer lists must be switched.
\r
707 If there are any timers still referenced from the current timer list
\r
708 then they must have expired and should be processed before the lists
\r
710 while( listLIST_IS_EMPTY( pxCurrentTimerList ) == pdFALSE )
\r
712 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );
\r
714 /* Remove the timer from the list. */
\r
715 pxTimer = ( Timer_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );
\r
716 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
\r
717 traceTIMER_EXPIRED( pxTimer );
\r
719 /* Execute its callback, then send a command to restart the timer if
\r
720 it is an auto-reload timer. It cannot be restarted here as the lists
\r
721 have not yet been switched. */
\r
722 pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
\r
724 if( pxTimer->uxAutoReload == ( UBaseType_t ) pdTRUE )
\r
726 /* Calculate the reload value, and if the reload value results in
\r
727 the timer going into the same timer list then it has already expired
\r
728 and the timer should be re-inserted into the current list so it is
\r
729 processed again within this loop. Otherwise a command should be sent
\r
730 to restart the timer to ensure it is only inserted into a list after
\r
731 the lists have been swapped. */
\r
732 xReloadTime = ( xNextExpireTime + pxTimer->xTimerPeriodInTicks );
\r
733 if( xReloadTime > xNextExpireTime )
\r
735 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xReloadTime );
\r
736 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );
\r
737 vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );
\r
741 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START_DONT_TRACE, xNextExpireTime, NULL, tmrNO_DELAY );
\r
742 configASSERT( xResult );
\r
748 mtCOVERAGE_TEST_MARKER();
\r
752 pxTemp = pxCurrentTimerList;
\r
753 pxCurrentTimerList = pxOverflowTimerList;
\r
754 pxOverflowTimerList = pxTemp;
\r
756 /*-----------------------------------------------------------*/
\r
758 static void prvCheckForValidListAndQueue( void )
\r
760 /* Check that the list from which active timers are referenced, and the
\r
761 queue used to communicate with the timer service, have been
\r
763 taskENTER_CRITICAL();
\r
765 if( xTimerQueue == NULL )
\r
767 vListInitialise( &xActiveTimerList1 );
\r
768 vListInitialise( &xActiveTimerList2 );
\r
769 pxCurrentTimerList = &xActiveTimerList1;
\r
770 pxOverflowTimerList = &xActiveTimerList2;
\r
771 xTimerQueue = xQueueCreate( ( UBaseType_t ) configTIMER_QUEUE_LENGTH, sizeof( DaemonTaskMessage_t ) );
\r
772 configASSERT( xTimerQueue );
\r
774 #if ( configQUEUE_REGISTRY_SIZE > 0 )
\r
776 if( xTimerQueue != NULL )
\r
778 vQueueAddToRegistry( xTimerQueue, "TmrQ" );
\r
782 mtCOVERAGE_TEST_MARKER();
\r
785 #endif /* configQUEUE_REGISTRY_SIZE */
\r
789 mtCOVERAGE_TEST_MARKER();
\r
792 taskEXIT_CRITICAL();
\r
794 /*-----------------------------------------------------------*/
\r
796 BaseType_t xTimerIsTimerActive( TimerHandle_t xTimer )
\r
798 BaseType_t xTimerIsInActiveList;
\r
799 Timer_t *pxTimer = ( Timer_t * ) xTimer;
\r
801 /* Is the timer in the list of active timers? */
\r
802 taskENTER_CRITICAL();
\r
804 /* Checking to see if it is in the NULL list in effect checks to see if
\r
805 it is referenced from either the current or the overflow timer lists in
\r
806 one go, but the logic has to be reversed, hence the '!'. */
\r
807 xTimerIsInActiveList = !( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) );
\r
809 taskEXIT_CRITICAL();
\r
811 return xTimerIsInActiveList;
\r
812 } /*lint !e818 Can't be pointer to const due to the typedef. */
\r
813 /*-----------------------------------------------------------*/
\r
815 void *pvTimerGetTimerID( const TimerHandle_t xTimer )
\r
817 Timer_t * const pxTimer = ( Timer_t * ) xTimer;
\r
819 return pxTimer->pvTimerID;
\r
821 /*-----------------------------------------------------------*/
\r
823 #if( INCLUDE_xTimerPendFunctionCall == 1 )
\r
825 BaseType_t xTimerPendFunctionCallFromISR( PendedFunction_t xFunctionToPend, void *pvParameter1, uint32_t ulParameter2, BaseType_t *pxHigherPriorityTaskWoken )
\r
827 DaemonTaskMessage_t xMessage;
\r
828 BaseType_t xReturn;
\r
830 /* Complete the message with the function parameters and post it to the
\r
832 xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK_FROM_ISR;
\r
833 xMessage.u.xCallbackParameters.pxCallbackFunction = xFunctionToPend;
\r
834 xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;
\r
835 xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;
\r
837 xReturn = xQueueSendFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );
\r
839 tracePEND_FUNC_CALL_FROM_ISR( xFunctionToPend, pvParameter1, ulParameter2, xReturn );
\r
844 #endif /* INCLUDE_xTimerPendFunctionCall */
\r
845 /*-----------------------------------------------------------*/
\r
847 #if( INCLUDE_xTimerPendFunctionCall == 1 )
\r
849 BaseType_t xTimerPendFunctionCall( PendedFunction_t xFunctionToPend, void *pvParameter1, uint32_t ulParameter2, TickType_t xTicksToWait )
\r
851 DaemonTaskMessage_t xMessage;
\r
852 BaseType_t xReturn;
\r
854 /* Complete the message with the function parameters and post it to the
\r
856 xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK;
\r
857 xMessage.u.xCallbackParameters.pxCallbackFunction = xFunctionToPend;
\r
858 xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;
\r
859 xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;
\r
861 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xTicksToWait );
\r
863 tracePEND_FUNC_CALL( xFunctionToPend, pvParameter1, ulParameter2, xReturn );
\r
868 #endif /* INCLUDE_xTimerPendFunctionCall */
\r
869 /*-----------------------------------------------------------*/
\r
871 /* This entire source file will be skipped if the application is not configured
\r
872 to include software timer functionality. If you want to include software timer
\r
873 functionality then ensure configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */
\r
874 #endif /* configUSE_TIMERS == 1 */
\r