2 FreeRTOS V8.2.3 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
70 /* Standard includes. */
\r
73 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
74 all the API functions to use the MPU wrappers. That should only be done when
\r
75 task.h is included from an application file. */
\r
76 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
78 #include "FreeRTOS.h"
\r
83 #if ( INCLUDE_xTimerPendFunctionCall == 1 ) && ( configUSE_TIMERS == 0 )
\r
84 #error configUSE_TIMERS must be set to 1 to make the xTimerPendFunctionCall() function available.
\r
87 /* Lint e961 and e750 are suppressed as a MISRA exception justified because the
\r
88 MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined for the
\r
89 header files above, but not in this file, in order to generate the correct
\r
90 privileged Vs unprivileged linkage and placement. */
\r
91 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750. */
\r
94 /* This entire source file will be skipped if the application is not configured
\r
95 to include software timer functionality. This #if is closed at the very bottom
\r
96 of this file. If you want to include software timer functionality then ensure
\r
97 configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */
\r
98 #if ( configUSE_TIMERS == 1 )
\r
100 /* Misc definitions. */
\r
101 #define tmrNO_DELAY ( TickType_t ) 0U
\r
103 /* The definition of the timers themselves. */
\r
104 typedef struct tmrTimerControl
\r
106 const char *pcTimerName; /*<< Text name. This is not used by the kernel, it is included simply to make debugging easier. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
\r
107 ListItem_t xTimerListItem; /*<< Standard linked list item as used by all kernel features for event management. */
\r
108 TickType_t xTimerPeriodInTicks;/*<< How quickly and often the timer expires. */
\r
109 UBaseType_t uxAutoReload; /*<< Set to pdTRUE if the timer should be automatically restarted once expired. Set to pdFALSE if the timer is, in effect, a one-shot timer. */
\r
110 void *pvTimerID; /*<< An ID to identify the timer. This allows the timer to be identified when the same callback is used for multiple timers. */
\r
111 TimerCallbackFunction_t pxCallbackFunction; /*<< The function that will be called when the timer expires. */
\r
112 #if( configUSE_TRACE_FACILITY == 1 )
\r
113 UBaseType_t uxTimerNumber; /*<< An ID assigned by trace tools such as FreeRTOS+Trace */
\r
117 /* The old xTIMER name is maintained above then typedefed to the new Timer_t
\r
118 name below to enable the use of older kernel aware debuggers. */
\r
119 typedef xTIMER Timer_t;
\r
121 /* The definition of messages that can be sent and received on the timer queue.
\r
122 Two types of message can be queued - messages that manipulate a software timer,
\r
123 and messages that request the execution of a non-timer related callback. The
\r
124 two message types are defined in two separate structures, xTimerParametersType
\r
125 and xCallbackParametersType respectively. */
\r
126 typedef struct tmrTimerParameters
\r
128 TickType_t xMessageValue; /*<< An optional value used by a subset of commands, for example, when changing the period of a timer. */
\r
129 Timer_t * pxTimer; /*<< The timer to which the command will be applied. */
\r
130 } TimerParameter_t;
\r
133 typedef struct tmrCallbackParameters
\r
135 PendedFunction_t pxCallbackFunction; /* << The callback function to execute. */
\r
136 void *pvParameter1; /* << The value that will be used as the callback functions first parameter. */
\r
137 uint32_t ulParameter2; /* << The value that will be used as the callback functions second parameter. */
\r
138 } CallbackParameters_t;
\r
140 /* The structure that contains the two message types, along with an identifier
\r
141 that is used to determine which message type is valid. */
\r
142 typedef struct tmrTimerQueueMessage
\r
144 BaseType_t xMessageID; /*<< The command being sent to the timer service task. */
\r
147 TimerParameter_t xTimerParameters;
\r
149 /* Don't include xCallbackParameters if it is not going to be used as
\r
150 it makes the structure (and therefore the timer queue) larger. */
\r
151 #if ( INCLUDE_xTimerPendFunctionCall == 1 )
\r
152 CallbackParameters_t xCallbackParameters;
\r
153 #endif /* INCLUDE_xTimerPendFunctionCall */
\r
155 } DaemonTaskMessage_t;
\r
157 /*lint -e956 A manual analysis and inspection has been used to determine which
\r
158 static variables must be declared volatile. */
\r
160 /* The list in which active timers are stored. Timers are referenced in expire
\r
161 time order, with the nearest expiry time at the front of the list. Only the
\r
162 timer service task is allowed to access these lists. */
\r
163 PRIVILEGED_DATA static List_t xActiveTimerList1;
\r
164 PRIVILEGED_DATA static List_t xActiveTimerList2;
\r
165 PRIVILEGED_DATA static List_t *pxCurrentTimerList;
\r
166 PRIVILEGED_DATA static List_t *pxOverflowTimerList;
\r
168 /* A queue that is used to send commands to the timer service task. */
\r
169 PRIVILEGED_DATA static QueueHandle_t xTimerQueue = NULL;
\r
171 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )
\r
173 PRIVILEGED_DATA static TaskHandle_t xTimerTaskHandle = NULL;
\r
179 /*-----------------------------------------------------------*/
\r
182 * Initialise the infrastructure used by the timer service task if it has not
\r
183 * been initialised already.
\r
185 static void prvCheckForValidListAndQueue( void ) PRIVILEGED_FUNCTION;
\r
188 * The timer service task (daemon). Timer functionality is controlled by this
\r
189 * task. Other tasks communicate with the timer service task using the
\r
190 * xTimerQueue queue.
\r
192 static void prvTimerTask( void *pvParameters ) PRIVILEGED_FUNCTION;
\r
195 * Called by the timer service task to interpret and process a command it
\r
196 * received on the timer queue.
\r
198 static void prvProcessReceivedCommands( void ) PRIVILEGED_FUNCTION;
\r
201 * Insert the timer into either xActiveTimerList1, or xActiveTimerList2,
\r
202 * depending on if the expire time causes a timer counter overflow.
\r
204 static BaseType_t prvInsertTimerInActiveList( Timer_t * const pxTimer, const TickType_t xNextExpiryTime, const TickType_t xTimeNow, const TickType_t xCommandTime ) PRIVILEGED_FUNCTION;
\r
207 * An active timer has reached its expire time. Reload the timer if it is an
\r
208 * auto reload timer, then call its callback.
\r
210 static void prvProcessExpiredTimer( const TickType_t xNextExpireTime, const TickType_t xTimeNow ) PRIVILEGED_FUNCTION;
\r
213 * The tick count has overflowed. Switch the timer lists after ensuring the
\r
214 * current timer list does not still reference some timers.
\r
216 static void prvSwitchTimerLists( void ) PRIVILEGED_FUNCTION;
\r
219 * Obtain the current tick count, setting *pxTimerListsWereSwitched to pdTRUE
\r
220 * if a tick count overflow occurred since prvSampleTimeNow() was last called.
\r
222 static TickType_t prvSampleTimeNow( BaseType_t * const pxTimerListsWereSwitched ) PRIVILEGED_FUNCTION;
\r
225 * If the timer list contains any active timers then return the expire time of
\r
226 * the timer that will expire first and set *pxListWasEmpty to false. If the
\r
227 * timer list does not contain any timers then return 0 and set *pxListWasEmpty
\r
230 static TickType_t prvGetNextExpireTime( BaseType_t * const pxListWasEmpty ) PRIVILEGED_FUNCTION;
\r
233 * If a timer has expired, process it. Otherwise, block the timer service task
\r
234 * until either a timer does expire or a command is received.
\r
236 static void prvProcessTimerOrBlockTask( const TickType_t xNextExpireTime, BaseType_t xListWasEmpty ) PRIVILEGED_FUNCTION;
\r
238 /*-----------------------------------------------------------*/
\r
240 BaseType_t xTimerCreateTimerTask( void )
\r
242 BaseType_t xReturn = pdFAIL;
\r
244 /* This function is called when the scheduler is started if
\r
245 configUSE_TIMERS is set to 1. Check that the infrastructure used by the
\r
246 timer service task has been created/initialised. If timers have already
\r
247 been created then the initialisation will already have been performed. */
\r
248 prvCheckForValidListAndQueue();
\r
250 if( xTimerQueue != NULL )
\r
252 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )
\r
254 /* Create the timer task, storing its handle in xTimerTaskHandle so
\r
255 it can be returned by the xTimerGetTimerDaemonTaskHandle() function. */
\r
256 xReturn = xTaskCreate( prvTimerTask, "Tmr Svc", ( uint16_t ) configTIMER_TASK_STACK_DEPTH, NULL, ( ( UBaseType_t ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT, &xTimerTaskHandle );
\r
260 /* Create the timer task without storing its handle. */
\r
261 xReturn = xTaskCreate( prvTimerTask, "Tmr Svc", ( uint16_t ) configTIMER_TASK_STACK_DEPTH, NULL, ( ( UBaseType_t ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT, NULL);
\r
267 mtCOVERAGE_TEST_MARKER();
\r
270 configASSERT( xReturn );
\r
273 /*-----------------------------------------------------------*/
\r
275 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
277 Timer_t *pxNewTimer;
\r
279 /* Allocate the timer structure. */
\r
280 if( xTimerPeriodInTicks == ( TickType_t ) 0U )
\r
286 pxNewTimer = ( Timer_t * ) pvPortMalloc( sizeof( Timer_t ) );
\r
287 if( pxNewTimer != NULL )
\r
289 /* Ensure the infrastructure used by the timer service task has been
\r
290 created/initialised. */
\r
291 prvCheckForValidListAndQueue();
\r
293 /* Initialise the timer structure members using the function parameters. */
\r
294 pxNewTimer->pcTimerName = pcTimerName;
\r
295 pxNewTimer->xTimerPeriodInTicks = xTimerPeriodInTicks;
\r
296 pxNewTimer->uxAutoReload = uxAutoReload;
\r
297 pxNewTimer->pvTimerID = pvTimerID;
\r
298 pxNewTimer->pxCallbackFunction = pxCallbackFunction;
\r
299 vListInitialiseItem( &( pxNewTimer->xTimerListItem ) );
\r
301 traceTIMER_CREATE( pxNewTimer );
\r
305 traceTIMER_CREATE_FAILED();
\r
309 /* 0 is not a valid value for xTimerPeriodInTicks. */
\r
310 configASSERT( ( xTimerPeriodInTicks > 0 ) );
\r
312 return ( TimerHandle_t ) pxNewTimer;
\r
314 /*-----------------------------------------------------------*/
\r
316 BaseType_t xTimerGenericCommand( TimerHandle_t xTimer, const BaseType_t xCommandID, const TickType_t xOptionalValue, BaseType_t * const pxHigherPriorityTaskWoken, const TickType_t xTicksToWait )
\r
318 BaseType_t xReturn = pdFAIL;
\r
319 DaemonTaskMessage_t xMessage;
\r
321 configASSERT( xTimer );
\r
323 /* Send a message to the timer service task to perform a particular action
\r
324 on a particular timer definition. */
\r
325 if( xTimerQueue != NULL )
\r
327 /* Send a command to the timer service task to start the xTimer timer. */
\r
328 xMessage.xMessageID = xCommandID;
\r
329 xMessage.u.xTimerParameters.xMessageValue = xOptionalValue;
\r
330 xMessage.u.xTimerParameters.pxTimer = ( Timer_t * ) xTimer;
\r
332 if( xCommandID < tmrFIRST_FROM_ISR_COMMAND )
\r
334 if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )
\r
336 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xTicksToWait );
\r
340 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, tmrNO_DELAY );
\r
345 xReturn = xQueueSendToBackFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );
\r
348 traceTIMER_COMMAND_SEND( xTimer, xCommandID, xOptionalValue, xReturn );
\r
352 mtCOVERAGE_TEST_MARKER();
\r
357 /*-----------------------------------------------------------*/
\r
359 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )
\r
361 TaskHandle_t xTimerGetTimerDaemonTaskHandle( void )
\r
363 /* If xTimerGetTimerDaemonTaskHandle() is called before the scheduler has been
\r
364 started, then xTimerTaskHandle will be NULL. */
\r
365 configASSERT( ( xTimerTaskHandle != NULL ) );
\r
366 return xTimerTaskHandle;
\r
370 /*-----------------------------------------------------------*/
\r
372 const char * pcTimerGetTimerName( TimerHandle_t xTimer )
\r
374 Timer_t *pxTimer = ( Timer_t * ) xTimer;
\r
376 configASSERT( xTimer );
\r
377 return pxTimer->pcTimerName;
\r
379 /*-----------------------------------------------------------*/
\r
381 static void prvProcessExpiredTimer( const TickType_t xNextExpireTime, const TickType_t xTimeNow )
\r
383 BaseType_t xResult;
\r
384 Timer_t * const pxTimer = ( Timer_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );
\r
386 /* Remove the timer from the list of active timers. A check has already
\r
387 been performed to ensure the list is not empty. */
\r
388 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
\r
389 traceTIMER_EXPIRED( pxTimer );
\r
391 /* If the timer is an auto reload timer then calculate the next
\r
392 expiry time and re-insert the timer in the list of active timers. */
\r
393 if( pxTimer->uxAutoReload == ( UBaseType_t ) pdTRUE )
\r
395 /* The timer is inserted into a list using a time relative to anything
\r
396 other than the current time. It will therefore be inserted into the
\r
397 correct list relative to the time this task thinks it is now. */
\r
398 if( prvInsertTimerInActiveList( pxTimer, ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ), xTimeNow, xNextExpireTime ) == pdTRUE )
\r
400 /* The timer expired before it was added to the active timer
\r
401 list. Reload it now. */
\r
402 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START_DONT_TRACE, xNextExpireTime, NULL, tmrNO_DELAY );
\r
403 configASSERT( xResult );
\r
408 mtCOVERAGE_TEST_MARKER();
\r
413 mtCOVERAGE_TEST_MARKER();
\r
416 /* Call the timer callback. */
\r
417 pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
\r
419 /*-----------------------------------------------------------*/
\r
421 static void prvTimerTask( void *pvParameters )
\r
423 TickType_t xNextExpireTime;
\r
424 BaseType_t xListWasEmpty;
\r
426 /* Just to avoid compiler warnings. */
\r
427 ( void ) pvParameters;
\r
431 /* Query the timers list to see if it contains any timers, and if so,
\r
432 obtain the time at which the next timer will expire. */
\r
433 xNextExpireTime = prvGetNextExpireTime( &xListWasEmpty );
\r
435 /* If a timer has expired, process it. Otherwise, block this task
\r
436 until either a timer does expire, or a command is received. */
\r
437 prvProcessTimerOrBlockTask( xNextExpireTime, xListWasEmpty );
\r
439 /* Empty the command queue. */
\r
440 prvProcessReceivedCommands();
\r
443 /*-----------------------------------------------------------*/
\r
445 static void prvProcessTimerOrBlockTask( const TickType_t xNextExpireTime, BaseType_t xListWasEmpty )
\r
447 TickType_t xTimeNow;
\r
448 BaseType_t xTimerListsWereSwitched;
\r
452 /* Obtain the time now to make an assessment as to whether the timer
\r
453 has expired or not. If obtaining the time causes the lists to switch
\r
454 then don't process this timer as any timers that remained in the list
\r
455 when the lists were switched will have been processed within the
\r
456 prvSampleTimeNow() function. */
\r
457 xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );
\r
458 if( xTimerListsWereSwitched == pdFALSE )
\r
460 /* The tick count has not overflowed, has the timer expired? */
\r
461 if( ( xListWasEmpty == pdFALSE ) && ( xNextExpireTime <= xTimeNow ) )
\r
463 ( void ) xTaskResumeAll();
\r
464 prvProcessExpiredTimer( xNextExpireTime, xTimeNow );
\r
468 /* The tick count has not overflowed, and the next expire
\r
469 time has not been reached yet. This task should therefore
\r
470 block to wait for the next expire time or a command to be
\r
471 received - whichever comes first. The following line cannot
\r
472 be reached unless xNextExpireTime > xTimeNow, except in the
\r
473 case when the current timer list is empty. */
\r
474 if( xListWasEmpty != pdFALSE )
\r
476 /* The current timer list is empty - is the overflow list
\r
478 xListWasEmpty = listLIST_IS_EMPTY( pxOverflowTimerList );
\r
481 vQueueWaitForMessageRestricted( xTimerQueue, ( xNextExpireTime - xTimeNow ), xListWasEmpty );
\r
483 if( xTaskResumeAll() == pdFALSE )
\r
485 /* Yield to wait for either a command to arrive, or the
\r
486 block time to expire. If a command arrived between the
\r
487 critical section being exited and this yield then the yield
\r
488 will not cause the task to block. */
\r
489 portYIELD_WITHIN_API();
\r
493 mtCOVERAGE_TEST_MARKER();
\r
499 ( void ) xTaskResumeAll();
\r
503 /*-----------------------------------------------------------*/
\r
505 static TickType_t prvGetNextExpireTime( BaseType_t * const pxListWasEmpty )
\r
507 TickType_t xNextExpireTime;
\r
509 /* Timers are listed in expiry time order, with the head of the list
\r
510 referencing the task that will expire first. Obtain the time at which
\r
511 the timer with the nearest expiry time will expire. If there are no
\r
512 active timers then just set the next expire time to 0. That will cause
\r
513 this task to unblock when the tick count overflows, at which point the
\r
514 timer lists will be switched and the next expiry time can be
\r
516 *pxListWasEmpty = listLIST_IS_EMPTY( pxCurrentTimerList );
\r
517 if( *pxListWasEmpty == pdFALSE )
\r
519 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );
\r
523 /* Ensure the task unblocks when the tick count rolls over. */
\r
524 xNextExpireTime = ( TickType_t ) 0U;
\r
527 return xNextExpireTime;
\r
529 /*-----------------------------------------------------------*/
\r
531 static TickType_t prvSampleTimeNow( BaseType_t * const pxTimerListsWereSwitched )
\r
533 TickType_t xTimeNow;
\r
534 PRIVILEGED_DATA static TickType_t xLastTime = ( TickType_t ) 0U; /*lint !e956 Variable is only accessible to one task. */
\r
536 xTimeNow = xTaskGetTickCount();
\r
538 if( xTimeNow < xLastTime )
\r
540 prvSwitchTimerLists();
\r
541 *pxTimerListsWereSwitched = pdTRUE;
\r
545 *pxTimerListsWereSwitched = pdFALSE;
\r
548 xLastTime = xTimeNow;
\r
552 /*-----------------------------------------------------------*/
\r
554 static BaseType_t prvInsertTimerInActiveList( Timer_t * const pxTimer, const TickType_t xNextExpiryTime, const TickType_t xTimeNow, const TickType_t xCommandTime )
\r
556 BaseType_t xProcessTimerNow = pdFALSE;
\r
558 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xNextExpiryTime );
\r
559 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );
\r
561 if( xNextExpiryTime <= xTimeNow )
\r
563 /* Has the expiry time elapsed between the command to start/reset a
\r
564 timer was issued, and the time the command was processed? */
\r
565 if( ( xTimeNow - xCommandTime ) >= pxTimer->xTimerPeriodInTicks )
\r
567 /* The time between a command being issued and the command being
\r
568 processed actually exceeds the timers period. */
\r
569 xProcessTimerNow = pdTRUE;
\r
573 vListInsert( pxOverflowTimerList, &( pxTimer->xTimerListItem ) );
\r
578 if( ( xTimeNow < xCommandTime ) && ( xNextExpiryTime >= xCommandTime ) )
\r
580 /* If, since the command was issued, the tick count has overflowed
\r
581 but the expiry time has not, then the timer must have already passed
\r
582 its expiry time and should be processed immediately. */
\r
583 xProcessTimerNow = pdTRUE;
\r
587 vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );
\r
591 return xProcessTimerNow;
\r
593 /*-----------------------------------------------------------*/
\r
595 static void prvProcessReceivedCommands( void )
\r
597 DaemonTaskMessage_t xMessage;
\r
599 BaseType_t xTimerListsWereSwitched, xResult;
\r
600 TickType_t xTimeNow;
\r
602 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
604 #if ( INCLUDE_xTimerPendFunctionCall == 1 )
\r
606 /* Negative commands are pended function calls rather than timer
\r
608 if( xMessage.xMessageID < ( BaseType_t ) 0 )
\r
610 const CallbackParameters_t * const pxCallback = &( xMessage.u.xCallbackParameters );
\r
612 /* The timer uses the xCallbackParameters member to request a
\r
613 callback be executed. Check the callback is not NULL. */
\r
614 configASSERT( pxCallback );
\r
616 /* Call the function. */
\r
617 pxCallback->pxCallbackFunction( pxCallback->pvParameter1, pxCallback->ulParameter2 );
\r
621 mtCOVERAGE_TEST_MARKER();
\r
624 #endif /* INCLUDE_xTimerPendFunctionCall */
\r
626 /* Commands that are positive are timer commands rather than pended
\r
628 if( xMessage.xMessageID >= ( BaseType_t ) 0 )
\r
630 /* The messages uses the xTimerParameters member to work on a
\r
632 pxTimer = xMessage.u.xTimerParameters.pxTimer;
\r
634 if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )
\r
636 /* The timer is in a list, remove it. */
\r
637 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
\r
641 mtCOVERAGE_TEST_MARKER();
\r
644 traceTIMER_COMMAND_RECEIVED( pxTimer, xMessage.xMessageID, xMessage.u.xTimerParameters.xMessageValue );
\r
646 /* In this case the xTimerListsWereSwitched parameter is not used, but
\r
647 it must be present in the function call. prvSampleTimeNow() must be
\r
648 called after the message is received from xTimerQueue so there is no
\r
649 possibility of a higher priority task adding a message to the message
\r
650 queue with a time that is ahead of the timer daemon task (because it
\r
651 pre-empted the timer daemon task after the xTimeNow value was set). */
\r
652 xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );
\r
654 switch( xMessage.xMessageID )
\r
656 case tmrCOMMAND_START :
\r
657 case tmrCOMMAND_START_FROM_ISR :
\r
658 case tmrCOMMAND_RESET :
\r
659 case tmrCOMMAND_RESET_FROM_ISR :
\r
660 case tmrCOMMAND_START_DONT_TRACE :
\r
661 /* Start or restart a timer. */
\r
662 if( prvInsertTimerInActiveList( pxTimer, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow, xMessage.u.xTimerParameters.xMessageValue ) == pdTRUE )
\r
664 /* The timer expired before it was added to the active
\r
665 timer list. Process it now. */
\r
666 pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
\r
667 traceTIMER_EXPIRED( pxTimer );
\r
669 if( pxTimer->uxAutoReload == ( UBaseType_t ) pdTRUE )
\r
671 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START_DONT_TRACE, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, NULL, tmrNO_DELAY );
\r
672 configASSERT( xResult );
\r
677 mtCOVERAGE_TEST_MARKER();
\r
682 mtCOVERAGE_TEST_MARKER();
\r
686 case tmrCOMMAND_STOP :
\r
687 case tmrCOMMAND_STOP_FROM_ISR :
\r
688 /* The timer has already been removed from the active list.
\r
689 There is nothing to do here. */
\r
692 case tmrCOMMAND_CHANGE_PERIOD :
\r
693 case tmrCOMMAND_CHANGE_PERIOD_FROM_ISR :
\r
694 pxTimer->xTimerPeriodInTicks = xMessage.u.xTimerParameters.xMessageValue;
\r
695 configASSERT( ( pxTimer->xTimerPeriodInTicks > 0 ) );
\r
697 /* The new period does not really have a reference, and can be
\r
698 longer or shorter than the old one. The command time is
\r
699 therefore set to the current time, and as the period cannot be
\r
700 zero the next expiry time can only be in the future, meaning
\r
701 (unlike for the xTimerStart() case above) there is no fail case
\r
702 that needs to be handled here. */
\r
703 ( void ) prvInsertTimerInActiveList( pxTimer, ( xTimeNow + pxTimer->xTimerPeriodInTicks ), xTimeNow, xTimeNow );
\r
706 case tmrCOMMAND_DELETE :
\r
707 /* The timer has already been removed from the active list,
\r
708 just free up the memory. */
\r
709 vPortFree( pxTimer );
\r
713 /* Don't expect to get here. */
\r
719 /*-----------------------------------------------------------*/
\r
721 static void prvSwitchTimerLists( void )
\r
723 TickType_t xNextExpireTime, xReloadTime;
\r
726 BaseType_t xResult;
\r
728 /* The tick count has overflowed. The timer lists must be switched.
\r
729 If there are any timers still referenced from the current timer list
\r
730 then they must have expired and should be processed before the lists
\r
732 while( listLIST_IS_EMPTY( pxCurrentTimerList ) == pdFALSE )
\r
734 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );
\r
736 /* Remove the timer from the list. */
\r
737 pxTimer = ( Timer_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );
\r
738 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
\r
739 traceTIMER_EXPIRED( pxTimer );
\r
741 /* Execute its callback, then send a command to restart the timer if
\r
742 it is an auto-reload timer. It cannot be restarted here as the lists
\r
743 have not yet been switched. */
\r
744 pxTimer->pxCallbackFunction( ( TimerHandle_t ) pxTimer );
\r
746 if( pxTimer->uxAutoReload == ( UBaseType_t ) pdTRUE )
\r
748 /* Calculate the reload value, and if the reload value results in
\r
749 the timer going into the same timer list then it has already expired
\r
750 and the timer should be re-inserted into the current list so it is
\r
751 processed again within this loop. Otherwise a command should be sent
\r
752 to restart the timer to ensure it is only inserted into a list after
\r
753 the lists have been swapped. */
\r
754 xReloadTime = ( xNextExpireTime + pxTimer->xTimerPeriodInTicks );
\r
755 if( xReloadTime > xNextExpireTime )
\r
757 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xReloadTime );
\r
758 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );
\r
759 vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );
\r
763 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START_DONT_TRACE, xNextExpireTime, NULL, tmrNO_DELAY );
\r
764 configASSERT( xResult );
\r
770 mtCOVERAGE_TEST_MARKER();
\r
774 pxTemp = pxCurrentTimerList;
\r
775 pxCurrentTimerList = pxOverflowTimerList;
\r
776 pxOverflowTimerList = pxTemp;
\r
778 /*-----------------------------------------------------------*/
\r
780 static void prvCheckForValidListAndQueue( void )
\r
782 /* Check that the list from which active timers are referenced, and the
\r
783 queue used to communicate with the timer service, have been
\r
785 taskENTER_CRITICAL();
\r
787 if( xTimerQueue == NULL )
\r
789 vListInitialise( &xActiveTimerList1 );
\r
790 vListInitialise( &xActiveTimerList2 );
\r
791 pxCurrentTimerList = &xActiveTimerList1;
\r
792 pxOverflowTimerList = &xActiveTimerList2;
\r
793 xTimerQueue = xQueueCreate( ( UBaseType_t ) configTIMER_QUEUE_LENGTH, sizeof( DaemonTaskMessage_t ) );
\r
794 configASSERT( xTimerQueue );
\r
796 #if ( configQUEUE_REGISTRY_SIZE > 0 )
\r
798 if( xTimerQueue != NULL )
\r
800 vQueueAddToRegistry( xTimerQueue, "TmrQ" );
\r
804 mtCOVERAGE_TEST_MARKER();
\r
807 #endif /* configQUEUE_REGISTRY_SIZE */
\r
811 mtCOVERAGE_TEST_MARKER();
\r
814 taskEXIT_CRITICAL();
\r
816 /*-----------------------------------------------------------*/
\r
818 BaseType_t xTimerIsTimerActive( TimerHandle_t xTimer )
\r
820 BaseType_t xTimerIsInActiveList;
\r
821 Timer_t *pxTimer = ( Timer_t * ) xTimer;
\r
823 configASSERT( xTimer );
\r
825 /* Is the timer in the list of active timers? */
\r
826 taskENTER_CRITICAL();
\r
828 /* Checking to see if it is in the NULL list in effect checks to see if
\r
829 it is referenced from either the current or the overflow timer lists in
\r
830 one go, but the logic has to be reversed, hence the '!'. */
\r
831 xTimerIsInActiveList = ( BaseType_t ) !( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) );
\r
833 taskEXIT_CRITICAL();
\r
835 return xTimerIsInActiveList;
\r
836 } /*lint !e818 Can't be pointer to const due to the typedef. */
\r
837 /*-----------------------------------------------------------*/
\r
839 void *pvTimerGetTimerID( const TimerHandle_t xTimer )
\r
841 Timer_t * const pxTimer = ( Timer_t * ) xTimer;
\r
844 configASSERT( xTimer );
\r
846 taskENTER_CRITICAL();
\r
848 pvReturn = pxTimer->pvTimerID;
\r
850 taskEXIT_CRITICAL();
\r
854 /*-----------------------------------------------------------*/
\r
856 void vTimerSetTimerID( TimerHandle_t xTimer, void *pvNewID )
\r
858 Timer_t * const pxTimer = ( Timer_t * ) xTimer;
\r
860 configASSERT( xTimer );
\r
862 taskENTER_CRITICAL();
\r
864 pxTimer->pvTimerID = pvNewID;
\r
866 taskEXIT_CRITICAL();
\r
868 /*-----------------------------------------------------------*/
\r
870 #if( INCLUDE_xTimerPendFunctionCall == 1 )
\r
872 BaseType_t xTimerPendFunctionCallFromISR( PendedFunction_t xFunctionToPend, void *pvParameter1, uint32_t ulParameter2, BaseType_t *pxHigherPriorityTaskWoken )
\r
874 DaemonTaskMessage_t xMessage;
\r
875 BaseType_t xReturn;
\r
877 /* Complete the message with the function parameters and post it to the
\r
879 xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK_FROM_ISR;
\r
880 xMessage.u.xCallbackParameters.pxCallbackFunction = xFunctionToPend;
\r
881 xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;
\r
882 xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;
\r
884 xReturn = xQueueSendFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );
\r
886 tracePEND_FUNC_CALL_FROM_ISR( xFunctionToPend, pvParameter1, ulParameter2, xReturn );
\r
891 #endif /* INCLUDE_xTimerPendFunctionCall */
\r
892 /*-----------------------------------------------------------*/
\r
894 #if( INCLUDE_xTimerPendFunctionCall == 1 )
\r
896 BaseType_t xTimerPendFunctionCall( PendedFunction_t xFunctionToPend, void *pvParameter1, uint32_t ulParameter2, TickType_t xTicksToWait )
\r
898 DaemonTaskMessage_t xMessage;
\r
899 BaseType_t xReturn;
\r
901 /* This function can only be called after a timer has been created or
\r
902 after the scheduler has been started because, until then, the timer
\r
903 queue does not exist. */
\r
904 configASSERT( xTimerQueue );
\r
906 /* Complete the message with the function parameters and post it to the
\r
908 xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK;
\r
909 xMessage.u.xCallbackParameters.pxCallbackFunction = xFunctionToPend;
\r
910 xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;
\r
911 xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;
\r
913 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xTicksToWait );
\r
915 tracePEND_FUNC_CALL( xFunctionToPend, pvParameter1, ulParameter2, xReturn );
\r
920 #endif /* INCLUDE_xTimerPendFunctionCall */
\r
921 /*-----------------------------------------------------------*/
\r
923 /* This entire source file will be skipped if the application is not configured
\r
924 to include software timer functionality. If you want to include software timer
\r
925 functionality then ensure configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */
\r
926 #endif /* configUSE_TIMERS == 1 */
\r