2 FreeRTOS V7.6.0 - Copyright (C) 2013 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_xTimerPendCallbackFromISR == 1 ) && ( configUSE_TIMERS == 0 )
\r
80 #error configUSE_TIMERS must be set to 1 to make the INCLUDE_xTimerPendCallbackFromISR() 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 ( portTickType ) 0U
\r
99 /* The definition of the timers themselves. */
\r
100 typedef struct tmrTimerControl
\r
102 const signed char *pcTimerName; /*<< Text name. This is not used by the kernel, it is included simply to make debugging easier. */
\r
103 xListItem xTimerListItem; /*<< Standard linked list item as used by all kernel features for event management. */
\r
104 portTickType xTimerPeriodInTicks;/*<< How quickly and often the timer expires. */
\r
105 unsigned portBASE_TYPE 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 tmrTIMER_CALLBACK pxCallbackFunction; /*<< The function that will be called when the timer expires. */
\r
110 /* The definition of messages that can be sent and received on the timer queue.
\r
111 Two types of message can be queued - messages that manipulate a software timer,
\r
112 and messages that request the execution of a non-timer related callback. The
\r
113 two message types are defined in two separate structures, xTimerParametersType
\r
114 and xCallbackParametersType respectively. */
\r
115 typedef struct tmrTimerParameters
\r
117 portTickType xMessageValue; /*<< An optional value used by a subset of commands, for example, when changing the period of a timer. */
\r
118 xTIMER * pxTimer; /*<< The timer to which the command will be applied. */
\r
119 } xTimerParametersType;
\r
122 typedef struct tmrCallbackParameters
\r
124 pdAPPLICATION_CALLBACK_CODE pxCallbackFunction; /* << The callback function to execute. */
\r
125 void *pvParameter1; /* << The value that will be used as the callback functions first parameter. */
\r
126 unsigned long ulParameter2; /* << The value that will be used as the callback functions second parameter. */
\r
127 } xCallbackParametersType;
\r
129 /* The structure that contains the two message types, along with an identifier
\r
130 that is used to determine which message type is valid. */
\r
131 typedef struct tmrTimerQueueMessage
\r
133 portBASE_TYPE xMessageID; /*<< The command being sent to the timer service task. */
\r
136 xTimerParametersType xTimerParameters;
\r
138 /* Don't include xCallbackParameters if it is not going to be used as
\r
139 it makes the structure (and therefore the timer queue) larger. */
\r
140 #if ( INCLUDE_xTimerPendCallbackFromISR == 1 )
\r
141 xCallbackParametersType xCallbackParameters;
\r
142 #endif /* INCLUDE_xTimerPendCallbackFromISR */
\r
144 } xDAEMON_TASK_MESSAGE;
\r
146 /*lint -e956 A manual analysis and inspection has been used to determine which
\r
147 static variables must be declared volatile. */
\r
149 /* The list in which active timers are stored. Timers are referenced in expire
\r
150 time order, with the nearest expiry time at the front of the list. Only the
\r
151 timer service task is allowed to access these lists. */
\r
152 PRIVILEGED_DATA static xList xActiveTimerList1;
\r
153 PRIVILEGED_DATA static xList xActiveTimerList2;
\r
154 PRIVILEGED_DATA static xList *pxCurrentTimerList;
\r
155 PRIVILEGED_DATA static xList *pxOverflowTimerList;
\r
157 /* A queue that is used to send commands to the timer service task. */
\r
158 PRIVILEGED_DATA static xQueueHandle xTimerQueue = NULL;
\r
160 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )
\r
162 PRIVILEGED_DATA static xTaskHandle xTimerTaskHandle = NULL;
\r
168 /*-----------------------------------------------------------*/
\r
171 * Initialise the infrastructure used by the timer service task if it has not
\r
172 * been initialised already.
\r
174 static void prvCheckForValidListAndQueue( void ) PRIVILEGED_FUNCTION;
\r
177 * The timer service task (daemon). Timer functionality is controlled by this
\r
178 * task. Other tasks communicate with the timer service task using the
\r
179 * xTimerQueue queue.
\r
181 static void prvTimerTask( void *pvParameters ) PRIVILEGED_FUNCTION;
\r
184 * Called by the timer service task to interpret and process a command it
\r
185 * received on the timer queue.
\r
187 static void prvProcessReceivedCommands( void ) PRIVILEGED_FUNCTION;
\r
190 * Insert the timer into either xActiveTimerList1, or xActiveTimerList2,
\r
191 * depending on if the expire time causes a timer counter overflow.
\r
193 static portBASE_TYPE prvInsertTimerInActiveList( xTIMER * const pxTimer, const portTickType xNextExpiryTime, const portTickType xTimeNow, const portTickType xCommandTime ) PRIVILEGED_FUNCTION;
\r
196 * An active timer has reached its expire time. Reload the timer if it is an
\r
197 * auto reload timer, then call its callback.
\r
199 static void prvProcessExpiredTimer( const portTickType xNextExpireTime, const portTickType xTimeNow ) PRIVILEGED_FUNCTION;
\r
202 * The tick count has overflowed. Switch the timer lists after ensuring the
\r
203 * current timer list does not still reference some timers.
\r
205 static void prvSwitchTimerLists( void ) PRIVILEGED_FUNCTION;
\r
208 * Obtain the current tick count, setting *pxTimerListsWereSwitched to pdTRUE
\r
209 * if a tick count overflow occurred since prvSampleTimeNow() was last called.
\r
211 static portTickType prvSampleTimeNow( portBASE_TYPE *pxTimerListsWereSwitched ) PRIVILEGED_FUNCTION;
\r
214 * If the timer list contains any active timers then return the expire time of
\r
215 * the timer that will expire first and set *pxListWasEmpty to false. If the
\r
216 * timer list does not contain any timers then return 0 and set *pxListWasEmpty
\r
219 static portTickType prvGetNextExpireTime( portBASE_TYPE *pxListWasEmpty ) PRIVILEGED_FUNCTION;
\r
222 * If a timer has expired, process it. Otherwise, block the timer service task
\r
223 * until either a timer does expire or a command is received.
\r
225 static void prvProcessTimerOrBlockTask( const portTickType xNextExpireTime, const portBASE_TYPE xListWasEmpty ) PRIVILEGED_FUNCTION;
\r
227 /*-----------------------------------------------------------*/
\r
229 portBASE_TYPE xTimerCreateTimerTask( void )
\r
231 portBASE_TYPE xReturn = pdFAIL;
\r
233 /* This function is called when the scheduler is started if
\r
234 configUSE_TIMERS is set to 1. Check that the infrastructure used by the
\r
235 timer service task has been created/initialised. If timers have already
\r
236 been created then the initialisation will already have been performed. */
\r
237 prvCheckForValidListAndQueue();
\r
239 if( xTimerQueue != NULL )
\r
241 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )
\r
243 /* Create the timer task, storing its handle in xTimerTaskHandle so
\r
244 it can be returned by the xTimerGetTimerDaemonTaskHandle() function. */
\r
245 xReturn = xTaskCreate( prvTimerTask, ( signed char * ) "Tmr Svc", ( unsigned short ) configTIMER_TASK_STACK_DEPTH, NULL, ( ( unsigned portBASE_TYPE ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT, &xTimerTaskHandle );
\r
249 /* Create the timer task without storing its handle. */
\r
250 xReturn = xTaskCreate( prvTimerTask, ( signed char * ) "Tmr Svc", ( unsigned short ) configTIMER_TASK_STACK_DEPTH, NULL, ( ( unsigned portBASE_TYPE ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT, NULL);
\r
255 configASSERT( xReturn );
\r
258 /*-----------------------------------------------------------*/
\r
260 xTimerHandle xTimerCreate( const signed char * const pcTimerName, portTickType xTimerPeriodInTicks, unsigned portBASE_TYPE uxAutoReload, void *pvTimerID, tmrTIMER_CALLBACK pxCallbackFunction )
\r
262 xTIMER *pxNewTimer;
\r
264 /* Allocate the timer structure. */
\r
265 if( xTimerPeriodInTicks == ( portTickType ) 0U )
\r
271 pxNewTimer = ( xTIMER * ) pvPortMalloc( sizeof( xTIMER ) );
\r
272 if( pxNewTimer != NULL )
\r
274 /* Ensure the infrastructure used by the timer service task has been
\r
275 created/initialised. */
\r
276 prvCheckForValidListAndQueue();
\r
278 /* Initialise the timer structure members using the function parameters. */
\r
279 pxNewTimer->pcTimerName = pcTimerName;
\r
280 pxNewTimer->xTimerPeriodInTicks = xTimerPeriodInTicks;
\r
281 pxNewTimer->uxAutoReload = uxAutoReload;
\r
282 pxNewTimer->pvTimerID = pvTimerID;
\r
283 pxNewTimer->pxCallbackFunction = pxCallbackFunction;
\r
284 vListInitialiseItem( &( pxNewTimer->xTimerListItem ) );
\r
286 traceTIMER_CREATE( pxNewTimer );
\r
290 traceTIMER_CREATE_FAILED();
\r
294 /* 0 is not a valid value for xTimerPeriodInTicks. */
\r
295 configASSERT( ( xTimerPeriodInTicks > 0 ) );
\r
297 return ( xTimerHandle ) pxNewTimer;
\r
299 /*-----------------------------------------------------------*/
\r
301 portBASE_TYPE xTimerGenericCommand( xTimerHandle xTimer, portBASE_TYPE xCommandID, portTickType xOptionalValue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portTickType xBlockTime )
\r
303 portBASE_TYPE xReturn = pdFAIL;
\r
304 xDAEMON_TASK_MESSAGE xMessage;
\r
306 /* Send a message to the timer service task to perform a particular action
\r
307 on a particular timer definition. */
\r
308 if( xTimerQueue != NULL )
\r
310 /* Send a command to the timer service task to start the xTimer timer. */
\r
311 xMessage.xMessageID = xCommandID;
\r
312 xMessage.u.xTimerParameters.xMessageValue = xOptionalValue;
\r
313 xMessage.u.xTimerParameters.pxTimer = ( xTIMER * ) xTimer;
\r
315 if( pxHigherPriorityTaskWoken == NULL )
\r
317 if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )
\r
319 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xBlockTime );
\r
323 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, tmrNO_DELAY );
\r
328 xReturn = xQueueSendToBackFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );
\r
331 traceTIMER_COMMAND_SEND( xTimer, xCommandID, xOptionalValue, xReturn );
\r
336 /*-----------------------------------------------------------*/
\r
338 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )
\r
340 xTaskHandle xTimerGetTimerDaemonTaskHandle( void )
\r
342 /* If xTimerGetTimerDaemonTaskHandle() is called before the scheduler has been
\r
343 started, then xTimerTaskHandle will be NULL. */
\r
344 configASSERT( ( xTimerTaskHandle != NULL ) );
\r
345 return xTimerTaskHandle;
\r
349 /*-----------------------------------------------------------*/
\r
351 static void prvProcessExpiredTimer( const portTickType xNextExpireTime, const portTickType xTimeNow )
\r
353 portBASE_TYPE xResult;
\r
354 xTIMER * const pxTimer = ( xTIMER * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );
\r
356 /* Remove the timer from the list of active timers. A check has already
\r
357 been performed to ensure the list is not empty. */
\r
358 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
\r
359 traceTIMER_EXPIRED( pxTimer );
\r
361 /* If the timer is an auto reload timer then calculate the next
\r
362 expiry time and re-insert the timer in the list of active timers. */
\r
363 if( pxTimer->uxAutoReload == ( unsigned portBASE_TYPE ) pdTRUE )
\r
365 /* The timer is inserted into a list using a time relative to anything
\r
366 other than the current time. It will therefore be inserted into the
\r
367 correct list relative to the time this task thinks it is now. */
\r
368 if( prvInsertTimerInActiveList( pxTimer, ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ), xTimeNow, xNextExpireTime ) == pdTRUE )
\r
370 /* The timer expired before it was added to the active timer
\r
371 list. Reload it now. */
\r
372 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START, xNextExpireTime, NULL, tmrNO_DELAY );
\r
373 configASSERT( xResult );
\r
378 /* Call the timer callback. */
\r
379 pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );
\r
381 /*-----------------------------------------------------------*/
\r
383 static void prvTimerTask( void *pvParameters )
\r
385 portTickType xNextExpireTime;
\r
386 portBASE_TYPE xListWasEmpty;
\r
388 /* Just to avoid compiler warnings. */
\r
389 ( void ) pvParameters;
\r
393 /* Query the timers list to see if it contains any timers, and if so,
\r
394 obtain the time at which the next timer will expire. */
\r
395 xNextExpireTime = prvGetNextExpireTime( &xListWasEmpty );
\r
397 /* If a timer has expired, process it. Otherwise, block this task
\r
398 until either a timer does expire, or a command is received. */
\r
399 prvProcessTimerOrBlockTask( xNextExpireTime, xListWasEmpty );
\r
401 /* Empty the command queue. */
\r
402 prvProcessReceivedCommands();
\r
405 /*-----------------------------------------------------------*/
\r
407 static void prvProcessTimerOrBlockTask( const portTickType xNextExpireTime, const portBASE_TYPE xListWasEmpty )
\r
409 portTickType xTimeNow;
\r
410 portBASE_TYPE xTimerListsWereSwitched;
\r
414 /* Obtain the time now to make an assessment as to whether the timer
\r
415 has expired or not. If obtaining the time causes the lists to switch
\r
416 then don't process this timer as any timers that remained in the list
\r
417 when the lists were switched will have been processed within the
\r
418 prvSampleTimeNow() function. */
\r
419 xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );
\r
420 if( xTimerListsWereSwitched == pdFALSE )
\r
422 /* The tick count has not overflowed, has the timer expired? */
\r
423 if( ( xListWasEmpty == pdFALSE ) && ( xNextExpireTime <= xTimeNow ) )
\r
425 ( void ) xTaskResumeAll();
\r
426 prvProcessExpiredTimer( xNextExpireTime, xTimeNow );
\r
430 /* The tick count has not overflowed, and the next expire
\r
431 time has not been reached yet. This task should therefore
\r
432 block to wait for the next expire time or a command to be
\r
433 received - whichever comes first. The following line cannot
\r
434 be reached unless xNextExpireTime > xTimeNow, except in the
\r
435 case when the current timer list is empty. */
\r
436 vQueueWaitForMessageRestricted( xTimerQueue, ( xNextExpireTime - xTimeNow ) );
\r
438 if( xTaskResumeAll() == pdFALSE )
\r
440 /* Yield to wait for either a command to arrive, or the block time
\r
441 to expire. If a command arrived between the critical section being
\r
442 exited and this yield then the yield will not cause the task
\r
444 portYIELD_WITHIN_API();
\r
450 ( void ) xTaskResumeAll();
\r
454 /*-----------------------------------------------------------*/
\r
456 static portTickType prvGetNextExpireTime( portBASE_TYPE *pxListWasEmpty )
\r
458 portTickType xNextExpireTime;
\r
460 /* Timers are listed in expiry time order, with the head of the list
\r
461 referencing the task that will expire first. Obtain the time at which
\r
462 the timer with the nearest expiry time will expire. If there are no
\r
463 active timers then just set the next expire time to 0. That will cause
\r
464 this task to unblock when the tick count overflows, at which point the
\r
465 timer lists will be switched and the next expiry time can be
\r
467 *pxListWasEmpty = listLIST_IS_EMPTY( pxCurrentTimerList );
\r
468 if( *pxListWasEmpty == pdFALSE )
\r
470 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );
\r
474 /* Ensure the task unblocks when the tick count rolls over. */
\r
475 xNextExpireTime = ( portTickType ) 0U;
\r
478 return xNextExpireTime;
\r
480 /*-----------------------------------------------------------*/
\r
482 static portTickType prvSampleTimeNow( portBASE_TYPE *pxTimerListsWereSwitched )
\r
484 portTickType xTimeNow;
\r
485 PRIVILEGED_DATA static portTickType xLastTime = ( portTickType ) 0U; /*lint !e956 Variable is only accessible to one task. */
\r
487 xTimeNow = xTaskGetTickCount();
\r
489 if( xTimeNow < xLastTime )
\r
491 prvSwitchTimerLists();
\r
492 *pxTimerListsWereSwitched = pdTRUE;
\r
496 *pxTimerListsWereSwitched = pdFALSE;
\r
499 xLastTime = xTimeNow;
\r
503 /*-----------------------------------------------------------*/
\r
505 static portBASE_TYPE prvInsertTimerInActiveList( xTIMER * const pxTimer, const portTickType xNextExpiryTime, const portTickType xTimeNow, const portTickType xCommandTime )
\r
507 portBASE_TYPE xProcessTimerNow = pdFALSE;
\r
509 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xNextExpiryTime );
\r
510 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );
\r
512 if( xNextExpiryTime <= xTimeNow )
\r
514 /* Has the expiry time elapsed between the command to start/reset a
\r
515 timer was issued, and the time the command was processed? */
\r
516 if( ( xTimeNow - xCommandTime ) >= pxTimer->xTimerPeriodInTicks )
\r
518 /* The time between a command being issued and the command being
\r
519 processed actually exceeds the timers period. */
\r
520 xProcessTimerNow = pdTRUE;
\r
524 vListInsert( pxOverflowTimerList, &( pxTimer->xTimerListItem ) );
\r
529 if( ( xTimeNow < xCommandTime ) && ( xNextExpiryTime >= xCommandTime ) )
\r
531 /* If, since the command was issued, the tick count has overflowed
\r
532 but the expiry time has not, then the timer must have already passed
\r
533 its expiry time and should be processed immediately. */
\r
534 xProcessTimerNow = pdTRUE;
\r
538 vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );
\r
542 return xProcessTimerNow;
\r
544 /*-----------------------------------------------------------*/
\r
546 static void prvProcessReceivedCommands( void )
\r
548 xDAEMON_TASK_MESSAGE xMessage;
\r
550 portBASE_TYPE xTimerListsWereSwitched, xResult;
\r
551 portTickType xTimeNow;
\r
553 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
555 #if ( INCLUDE_xTimerPendCallbackFromISR == 1 )
\r
557 if( xMessage.xMessageID == tmrCOMMAND_EXECUTE_CALLBACK )
\r
559 const xCallbackParametersType * const pxCallback = &( xMessage.u.xCallbackParameters );
\r
561 /* The timer uses the xCallbackParameters member to request a
\r
562 callback be executed. Check the callback is not NULL. */
\r
563 configASSERT( pxCallback );
\r
565 /* Call the function. */
\r
566 pxCallback->pxCallbackFunction( pxCallback->pvParameter1, pxCallback->ulParameter2 );
\r
569 #endif /* INCLUDE_xTimerPendCallbackFromISR */
\r
571 if( xMessage.xMessageID != tmrCOMMAND_EXECUTE_CALLBACK )
\r
573 /* The messages uses the xTimerParameters member to work on a
\r
575 pxTimer = xMessage.u.xTimerParameters.pxTimer;
\r
577 if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )
\r
579 /* The timer is in a list, remove it. */
\r
580 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
\r
583 traceTIMER_COMMAND_RECEIVED( pxTimer, xMessage.xMessageID, xMessage.u.xTimerParameters.xMessageValue );
\r
585 /* In this case the xTimerListsWereSwitched parameter is not used, but
\r
586 it must be present in the function call. prvSampleTimeNow() must be
\r
587 called after the message is received from xTimerQueue so there is no
\r
588 possibility of a higher priority task adding a message to the message
\r
589 queue with a time that is ahead of the timer daemon task (because it
\r
590 pre-empted the timer daemon task after the xTimeNow value was set). */
\r
591 xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );
\r
593 switch( xMessage.xMessageID )
\r
595 case tmrCOMMAND_START :
\r
596 /* Start or restart a timer. */
\r
597 if( prvInsertTimerInActiveList( pxTimer, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow, xMessage.u.xTimerParameters.xMessageValue ) == pdTRUE )
\r
599 /* The timer expired before it was added to the active
\r
600 timer list. Process it now. */
\r
601 pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );
\r
602 traceTIMER_EXPIRED( pxTimer );
\r
604 if( pxTimer->uxAutoReload == ( unsigned portBASE_TYPE ) pdTRUE )
\r
606 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START, xMessage.u.xTimerParameters.xMessageValue + pxTimer->xTimerPeriodInTicks, NULL, tmrNO_DELAY );
\r
607 configASSERT( xResult );
\r
613 case tmrCOMMAND_STOP :
\r
614 /* The timer has already been removed from the active list.
\r
615 There is nothing to do here. */
\r
618 case tmrCOMMAND_CHANGE_PERIOD :
\r
619 pxTimer->xTimerPeriodInTicks = xMessage.u.xTimerParameters.xMessageValue;
\r
620 configASSERT( ( pxTimer->xTimerPeriodInTicks > 0 ) );
\r
622 /* The new period does not really have a reference, and can be
\r
623 longer or shorter than the old one. The command time is
\r
624 therefore set to the current time, and as the period cannot be
\r
625 zero the next expiry time can only be in the future, meaning
\r
626 (unlike for the xTimerStart() case above) there is no fail case
\r
627 that needs to be handled here. */
\r
628 ( void ) prvInsertTimerInActiveList( pxTimer, ( xTimeNow + pxTimer->xTimerPeriodInTicks ), xTimeNow, xTimeNow );
\r
631 case tmrCOMMAND_DELETE :
\r
632 /* The timer has already been removed from the active list,
\r
633 just free up the memory. */
\r
634 vPortFree( pxTimer );
\r
638 /* Don't expect to get here. */
\r
644 /*-----------------------------------------------------------*/
\r
646 static void prvSwitchTimerLists( void )
\r
648 portTickType xNextExpireTime, xReloadTime;
\r
651 portBASE_TYPE xResult;
\r
653 /* The tick count has overflowed. The timer lists must be switched.
\r
654 If there are any timers still referenced from the current timer list
\r
655 then they must have expired and should be processed before the lists
\r
657 while( listLIST_IS_EMPTY( pxCurrentTimerList ) == pdFALSE )
\r
659 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );
\r
661 /* Remove the timer from the list. */
\r
662 pxTimer = ( xTIMER * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );
\r
663 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
\r
664 traceTIMER_EXPIRED( pxTimer );
\r
666 /* Execute its callback, then send a command to restart the timer if
\r
667 it is an auto-reload timer. It cannot be restarted here as the lists
\r
668 have not yet been switched. */
\r
669 pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );
\r
671 if( pxTimer->uxAutoReload == ( unsigned portBASE_TYPE ) pdTRUE )
\r
673 /* Calculate the reload value, and if the reload value results in
\r
674 the timer going into the same timer list then it has already expired
\r
675 and the timer should be re-inserted into the current list so it is
\r
676 processed again within this loop. Otherwise a command should be sent
\r
677 to restart the timer to ensure it is only inserted into a list after
\r
678 the lists have been swapped. */
\r
679 xReloadTime = ( xNextExpireTime + pxTimer->xTimerPeriodInTicks );
\r
680 if( xReloadTime > xNextExpireTime )
\r
682 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xReloadTime );
\r
683 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );
\r
684 vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );
\r
688 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START, xNextExpireTime, NULL, tmrNO_DELAY );
\r
689 configASSERT( xResult );
\r
695 pxTemp = pxCurrentTimerList;
\r
696 pxCurrentTimerList = pxOverflowTimerList;
\r
697 pxOverflowTimerList = pxTemp;
\r
699 /*-----------------------------------------------------------*/
\r
701 static void prvCheckForValidListAndQueue( void )
\r
703 /* Check that the list from which active timers are referenced, and the
\r
704 queue used to communicate with the timer service, have been
\r
706 taskENTER_CRITICAL();
\r
708 if( xTimerQueue == NULL )
\r
710 vListInitialise( &xActiveTimerList1 );
\r
711 vListInitialise( &xActiveTimerList2 );
\r
712 pxCurrentTimerList = &xActiveTimerList1;
\r
713 pxOverflowTimerList = &xActiveTimerList2;
\r
714 xTimerQueue = xQueueCreate( ( unsigned portBASE_TYPE ) configTIMER_QUEUE_LENGTH, sizeof( xDAEMON_TASK_MESSAGE ) );
\r
715 configASSERT( xTimerQueue );
\r
717 #if ( configQUEUE_REGISTRY_SIZE > 0 )
\r
719 if( xTimerQueue != NULL )
\r
721 vQueueAddToRegistry( xTimerQueue, ( signed char * ) "TmrQ" );
\r
724 #endif /* configQUEUE_REGISTRY_SIZE */
\r
727 taskEXIT_CRITICAL();
\r
729 /*-----------------------------------------------------------*/
\r
731 portBASE_TYPE xTimerIsTimerActive( xTimerHandle xTimer )
\r
733 portBASE_TYPE xTimerIsInActiveList;
\r
734 xTIMER *pxTimer = ( xTIMER * ) xTimer;
\r
736 /* Is the timer in the list of active timers? */
\r
737 taskENTER_CRITICAL();
\r
739 /* Checking to see if it is in the NULL list in effect checks to see if
\r
740 it is referenced from either the current or the overflow timer lists in
\r
741 one go, but the logic has to be reversed, hence the '!'. */
\r
742 xTimerIsInActiveList = !( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) );
\r
744 taskEXIT_CRITICAL();
\r
746 return xTimerIsInActiveList;
\r
748 /*-----------------------------------------------------------*/
\r
750 void *pvTimerGetTimerID( const xTimerHandle xTimer )
\r
752 xTIMER * const pxTimer = ( xTIMER * const ) xTimer;
\r
754 return pxTimer->pvTimerID;
\r
756 /*-----------------------------------------------------------*/
\r
758 #if( INCLUDE_xTimerPendCallbackFromISR == 1 )
\r
760 portBASE_TYPE xTimerPendCallbackFromISR( pdAPPLICATION_CALLBACK_CODE pvCallbackFunction, void *pvParameter1, unsigned long ulParameter2, portBASE_TYPE *pxHigherPriorityTaskWoken )
\r
762 xDAEMON_TASK_MESSAGE xMessage;
\r
763 portBASE_TYPE xReturn;
\r
765 /* Complete the message with the function parameters and post it to the
\r
767 xMessage.xMessageID = tmrCOMMAND_EXECUTE_CALLBACK;
\r
768 xMessage.u.xCallbackParameters.pxCallbackFunction = pvCallbackFunction;
\r
769 xMessage.u.xCallbackParameters.pvParameter1 = pvParameter1;
\r
770 xMessage.u.xCallbackParameters.ulParameter2 = ulParameter2;
\r
772 xReturn = xQueueSendFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );
\r
777 #endif /* INCLUDE_xTimerPendCallbackFromISR */
\r
778 /*-----------------------------------------------------------*/
\r
780 /* This entire source file will be skipped if the application is not configured
\r
781 to include software timer functionality. If you want to include software timer
\r
782 functionality then ensure configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */
\r
783 #endif /* configUSE_TIMERS == 1 */
\r