2 FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
65 /* Standard includes. */
\r
68 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
69 all the API functions to use the MPU wrappers. That should only be done when
\r
70 task.h is included from an application file. */
\r
71 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
73 #include "FreeRTOS.h"
\r
78 /* Lint e961 and e750 are suppressed as a MISRA exception justified because the
\r
79 MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined for the
\r
80 header files above, but not in this file, in order to generate the correct
\r
81 privileged Vs unprivileged linkage and placement. */
\r
82 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750. */
\r
85 /* This entire source file will be skipped if the application is not configured
\r
86 to include software timer functionality. This #if is closed at the very bottom
\r
87 of this file. If you want to include software timer functionality then ensure
\r
88 configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */
\r
89 #if ( configUSE_TIMERS == 1 )
\r
91 /* Misc definitions. */
\r
92 #define tmrNO_DELAY ( portTickType ) 0U
\r
94 /* The definition of the timers themselves. */
\r
95 typedef struct tmrTimerControl
\r
97 const signed char *pcTimerName; /*<< Text name. This is not used by the kernel, it is included simply to make debugging easier. */
\r
98 xListItem xTimerListItem; /*<< Standard linked list item as used by all kernel features for event management. */
\r
99 portTickType xTimerPeriodInTicks;/*<< How quickly and often the timer expires. */
\r
100 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
101 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
102 tmrTIMER_CALLBACK pxCallbackFunction; /*<< The function that will be called when the timer expires. */
\r
105 /* The definition of messages that can be sent and received on the timer
\r
107 typedef struct tmrTimerQueueMessage
\r
109 portBASE_TYPE xMessageID; /*<< The command being sent to the timer service task. */
\r
110 portTickType xMessageValue; /*<< An optional value used by a subset of commands, for example, when changing the period of a timer. */
\r
111 xTIMER * pxTimer; /*<< The timer to which the command will be applied. */
\r
114 /*lint -e956 A manual analysis and inspection has been used to determine which
\r
115 static variables must be declared volatile. */
\r
117 /* The list in which active timers are stored. Timers are referenced in expire
\r
118 time order, with the nearest expiry time at the front of the list. Only the
\r
119 timer service task is allowed to access xActiveTimerList. */
\r
120 PRIVILEGED_DATA static xList xActiveTimerList1;
\r
121 PRIVILEGED_DATA static xList xActiveTimerList2;
\r
122 PRIVILEGED_DATA static xList *pxCurrentTimerList;
\r
123 PRIVILEGED_DATA static xList *pxOverflowTimerList;
\r
125 /* A queue that is used to send commands to the timer service task. */
\r
126 PRIVILEGED_DATA static xQueueHandle xTimerQueue = NULL;
\r
128 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )
\r
130 PRIVILEGED_DATA static xTaskHandle xTimerTaskHandle = NULL;
\r
136 /*-----------------------------------------------------------*/
\r
139 * Initialise the infrastructure used by the timer service task if it has not
\r
140 * been initialised already.
\r
142 static void prvCheckForValidListAndQueue( void ) PRIVILEGED_FUNCTION;
\r
145 * The timer service task (daemon). Timer functionality is controlled by this
\r
146 * task. Other tasks communicate with the timer service task using the
\r
147 * xTimerQueue queue.
\r
149 static void prvTimerTask( void *pvParameters ) PRIVILEGED_FUNCTION;
\r
152 * Called by the timer service task to interpret and process a command it
\r
153 * received on the timer queue.
\r
155 static void prvProcessReceivedCommands( void ) PRIVILEGED_FUNCTION;
\r
158 * Insert the timer into either xActiveTimerList1, or xActiveTimerList2,
\r
159 * depending on if the expire time causes a timer counter overflow.
\r
161 static portBASE_TYPE prvInsertTimerInActiveList( xTIMER *pxTimer, portTickType xNextExpiryTime, portTickType xTimeNow, portTickType xCommandTime ) PRIVILEGED_FUNCTION;
\r
164 * An active timer has reached its expire time. Reload the timer if it is an
\r
165 * auto reload timer, then call its callback.
\r
167 static void prvProcessExpiredTimer( portTickType xNextExpireTime, portTickType xTimeNow ) PRIVILEGED_FUNCTION;
\r
170 * The tick count has overflowed. Switch the timer lists after ensuring the
\r
171 * current timer list does not still reference some timers.
\r
173 static void prvSwitchTimerLists( portTickType xLastTime ) PRIVILEGED_FUNCTION;
\r
176 * Obtain the current tick count, setting *pxTimerListsWereSwitched to pdTRUE
\r
177 * if a tick count overflow occurred since prvSampleTimeNow() was last called.
\r
179 static portTickType prvSampleTimeNow( portBASE_TYPE *pxTimerListsWereSwitched ) PRIVILEGED_FUNCTION;
\r
182 * If the timer list contains any active timers then return the expire time of
\r
183 * the timer that will expire first and set *pxListWasEmpty to false. If the
\r
184 * timer list does not contain any timers then return 0 and set *pxListWasEmpty
\r
187 static portTickType prvGetNextExpireTime( portBASE_TYPE *pxListWasEmpty ) PRIVILEGED_FUNCTION;
\r
190 * If a timer has expired, process it. Otherwise, block the timer service task
\r
191 * until either a timer does expire or a command is received.
\r
193 static void prvProcessTimerOrBlockTask( portTickType xNextExpireTime, portBASE_TYPE xListWasEmpty ) PRIVILEGED_FUNCTION;
\r
195 /*-----------------------------------------------------------*/
\r
197 portBASE_TYPE xTimerCreateTimerTask( void )
\r
199 portBASE_TYPE xReturn = pdFAIL;
\r
201 /* This function is called when the scheduler is started if
\r
202 configUSE_TIMERS is set to 1. Check that the infrastructure used by the
\r
203 timer service task has been created/initialised. If timers have already
\r
204 been created then the initialisation will already have been performed. */
\r
205 prvCheckForValidListAndQueue();
\r
207 if( xTimerQueue != NULL )
\r
209 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )
\r
211 /* Create the timer task, storing its handle in xTimerTaskHandle so
\r
212 it can be returned by the xTimerGetTimerDaemonTaskHandle() function. */
\r
213 xReturn = xTaskCreate( prvTimerTask, ( const signed char * ) "Tmr Svc", ( unsigned short ) configTIMER_TASK_STACK_DEPTH, NULL, ( ( unsigned portBASE_TYPE ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT, &xTimerTaskHandle );
\r
217 /* Create the timer task without storing its handle. */
\r
218 xReturn = xTaskCreate( prvTimerTask, ( const signed char * ) "Tmr Svc", ( unsigned short ) configTIMER_TASK_STACK_DEPTH, NULL, ( ( unsigned portBASE_TYPE ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT, NULL);
\r
223 configASSERT( xReturn );
\r
226 /*-----------------------------------------------------------*/
\r
228 xTimerHandle xTimerCreate( const signed char * const pcTimerName, portTickType xTimerPeriodInTicks, unsigned portBASE_TYPE uxAutoReload, void *pvTimerID, tmrTIMER_CALLBACK pxCallbackFunction )
\r
230 xTIMER *pxNewTimer;
\r
232 /* Allocate the timer structure. */
\r
233 if( xTimerPeriodInTicks == ( portTickType ) 0U )
\r
236 configASSERT( ( xTimerPeriodInTicks > 0 ) );
\r
240 pxNewTimer = ( xTIMER * ) pvPortMalloc( sizeof( xTIMER ) );
\r
241 if( pxNewTimer != NULL )
\r
243 /* Ensure the infrastructure used by the timer service task has been
\r
244 created/initialised. */
\r
245 prvCheckForValidListAndQueue();
\r
247 /* Initialise the timer structure members using the function parameters. */
\r
248 pxNewTimer->pcTimerName = pcTimerName;
\r
249 pxNewTimer->xTimerPeriodInTicks = xTimerPeriodInTicks;
\r
250 pxNewTimer->uxAutoReload = uxAutoReload;
\r
251 pxNewTimer->pvTimerID = pvTimerID;
\r
252 pxNewTimer->pxCallbackFunction = pxCallbackFunction;
\r
253 vListInitialiseItem( &( pxNewTimer->xTimerListItem ) );
\r
255 traceTIMER_CREATE( pxNewTimer );
\r
259 traceTIMER_CREATE_FAILED();
\r
263 return ( xTimerHandle ) pxNewTimer;
\r
265 /*-----------------------------------------------------------*/
\r
267 portBASE_TYPE xTimerGenericCommand( xTimerHandle xTimer, portBASE_TYPE xCommandID, portTickType xOptionalValue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portTickType xBlockTime )
\r
269 portBASE_TYPE xReturn = pdFAIL;
\r
270 xTIMER_MESSAGE xMessage;
\r
272 /* Send a message to the timer service task to perform a particular action
\r
273 on a particular timer definition. */
\r
274 if( xTimerQueue != NULL )
\r
276 /* Send a command to the timer service task to start the xTimer timer. */
\r
277 xMessage.xMessageID = xCommandID;
\r
278 xMessage.xMessageValue = xOptionalValue;
\r
279 xMessage.pxTimer = ( xTIMER * ) xTimer;
\r
281 if( pxHigherPriorityTaskWoken == NULL )
\r
283 if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )
\r
285 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xBlockTime );
\r
289 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, tmrNO_DELAY );
\r
294 xReturn = xQueueSendToBackFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );
\r
297 traceTIMER_COMMAND_SEND( xTimer, xCommandID, xOptionalValue, xReturn );
\r
302 /*-----------------------------------------------------------*/
\r
304 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )
\r
306 xTaskHandle xTimerGetTimerDaemonTaskHandle( void )
\r
308 /* If xTimerGetTimerDaemonTaskHandle() is called before the scheduler has been
\r
309 started, then xTimerTaskHandle will be NULL. */
\r
310 configASSERT( ( xTimerTaskHandle != NULL ) );
\r
311 return xTimerTaskHandle;
\r
315 /*-----------------------------------------------------------*/
\r
317 static void prvProcessExpiredTimer( portTickType xNextExpireTime, portTickType xTimeNow )
\r
320 portBASE_TYPE xResult;
\r
322 /* Remove the timer from the list of active timers. A check has already
\r
323 been performed to ensure the list is not empty. */
\r
324 pxTimer = ( xTIMER * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );
\r
325 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
\r
326 traceTIMER_EXPIRED( pxTimer );
\r
328 /* If the timer is an auto reload timer then calculate the next
\r
329 expiry time and re-insert the timer in the list of active timers. */
\r
330 if( pxTimer->uxAutoReload == ( unsigned portBASE_TYPE ) pdTRUE )
\r
332 /* This is the only time a timer is inserted into a list using
\r
333 a time relative to anything other than the current time. It
\r
334 will therefore be inserted into the correct list relative to
\r
335 the time this task thinks it is now, even if a command to
\r
336 switch lists due to a tick count overflow is already waiting in
\r
337 the timer queue. */
\r
338 if( prvInsertTimerInActiveList( pxTimer, ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ), xTimeNow, xNextExpireTime ) == pdTRUE )
\r
340 /* The timer expired before it was added to the active timer
\r
341 list. Reload it now. */
\r
342 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START, xNextExpireTime, NULL, tmrNO_DELAY );
\r
343 configASSERT( xResult );
\r
348 /* Call the timer callback. */
\r
349 pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );
\r
351 /*-----------------------------------------------------------*/
\r
353 static void prvTimerTask( void *pvParameters )
\r
355 portTickType xNextExpireTime;
\r
356 portBASE_TYPE xListWasEmpty;
\r
358 /* Just to avoid compiler warnings. */
\r
359 ( void ) pvParameters;
\r
363 /* Query the timers list to see if it contains any timers, and if so,
\r
364 obtain the time at which the next timer will expire. */
\r
365 xNextExpireTime = prvGetNextExpireTime( &xListWasEmpty );
\r
367 /* If a timer has expired, process it. Otherwise, block this task
\r
368 until either a timer does expire, or a command is received. */
\r
369 prvProcessTimerOrBlockTask( xNextExpireTime, xListWasEmpty );
\r
371 /* Empty the command queue. */
\r
372 prvProcessReceivedCommands();
\r
375 /*-----------------------------------------------------------*/
\r
377 static void prvProcessTimerOrBlockTask( portTickType xNextExpireTime, portBASE_TYPE xListWasEmpty )
\r
379 portTickType xTimeNow;
\r
380 portBASE_TYPE xTimerListsWereSwitched;
\r
384 /* Obtain the time now to make an assessment as to whether the timer
\r
385 has expired or not. If obtaining the time causes the lists to switch
\r
386 then don't process this timer as any timers that remained in the list
\r
387 when the lists were switched will have been processed within the
\r
388 prvSampelTimeNow() function. */
\r
389 xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );
\r
390 if( xTimerListsWereSwitched == pdFALSE )
\r
392 /* The tick count has not overflowed, has the timer expired? */
\r
393 if( ( xListWasEmpty == pdFALSE ) && ( xNextExpireTime <= xTimeNow ) )
\r
395 ( void ) xTaskResumeAll();
\r
396 prvProcessExpiredTimer( xNextExpireTime, xTimeNow );
\r
400 /* The tick count has not overflowed, and the next expire
\r
401 time has not been reached yet. This task should therefore
\r
402 block to wait for the next expire time or a command to be
\r
403 received - whichever comes first. The following line cannot
\r
404 be reached unless xNextExpireTime > xTimeNow, except in the
\r
405 case when the current timer list is empty. */
\r
406 vQueueWaitForMessageRestricted( xTimerQueue, ( xNextExpireTime - xTimeNow ) );
\r
408 if( xTaskResumeAll() == pdFALSE )
\r
410 /* Yield to wait for either a command to arrive, or the block time
\r
411 to expire. If a command arrived between the critical section being
\r
412 exited and this yield then the yield will not cause the task
\r
414 portYIELD_WITHIN_API();
\r
420 ( void ) xTaskResumeAll();
\r
424 /*-----------------------------------------------------------*/
\r
426 static portTickType prvGetNextExpireTime( portBASE_TYPE *pxListWasEmpty )
\r
428 portTickType xNextExpireTime;
\r
430 /* Timers are listed in expiry time order, with the head of the list
\r
431 referencing the task that will expire first. Obtain the time at which
\r
432 the timer with the nearest expiry time will expire. If there are no
\r
433 active timers then just set the next expire time to 0. That will cause
\r
434 this task to unblock when the tick count overflows, at which point the
\r
435 timer lists will be switched and the next expiry time can be
\r
437 *pxListWasEmpty = listLIST_IS_EMPTY( pxCurrentTimerList );
\r
438 if( *pxListWasEmpty == pdFALSE )
\r
440 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );
\r
444 /* Ensure the task unblocks when the tick count rolls over. */
\r
445 xNextExpireTime = ( portTickType ) 0U;
\r
448 return xNextExpireTime;
\r
450 /*-----------------------------------------------------------*/
\r
452 static portTickType prvSampleTimeNow( portBASE_TYPE *pxTimerListsWereSwitched )
\r
454 portTickType xTimeNow;
\r
455 PRIVILEGED_DATA static portTickType xLastTime = ( portTickType ) 0U; /*lint !e956 Variable is only accessible to one task. */
\r
457 xTimeNow = xTaskGetTickCount();
\r
459 if( xTimeNow < xLastTime )
\r
461 prvSwitchTimerLists( xLastTime );
\r
462 *pxTimerListsWereSwitched = pdTRUE;
\r
466 *pxTimerListsWereSwitched = pdFALSE;
\r
469 xLastTime = xTimeNow;
\r
473 /*-----------------------------------------------------------*/
\r
475 static portBASE_TYPE prvInsertTimerInActiveList( xTIMER *pxTimer, portTickType xNextExpiryTime, portTickType xTimeNow, portTickType xCommandTime )
\r
477 portBASE_TYPE xProcessTimerNow = pdFALSE;
\r
479 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xNextExpiryTime );
\r
480 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );
\r
482 if( xNextExpiryTime <= xTimeNow )
\r
484 /* Has the expiry time elapsed between the command to start/reset a
\r
485 timer was issued, and the time the command was processed? */
\r
486 if( ( xTimeNow - xCommandTime ) >= pxTimer->xTimerPeriodInTicks )
\r
488 /* The time between a command being issued and the command being
\r
489 processed actually exceeds the timers period. */
\r
490 xProcessTimerNow = pdTRUE;
\r
494 vListInsert( pxOverflowTimerList, &( pxTimer->xTimerListItem ) );
\r
499 if( ( xTimeNow < xCommandTime ) && ( xNextExpiryTime >= xCommandTime ) )
\r
501 /* If, since the command was issued, the tick count has overflowed
\r
502 but the expiry time has not, then the timer must have already passed
\r
503 its expiry time and should be processed immediately. */
\r
504 xProcessTimerNow = pdTRUE;
\r
508 vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );
\r
512 return xProcessTimerNow;
\r
514 /*-----------------------------------------------------------*/
\r
516 static void prvProcessReceivedCommands( void )
\r
518 xTIMER_MESSAGE xMessage;
\r
520 portBASE_TYPE xTimerListsWereSwitched, xResult;
\r
521 portTickType xTimeNow;
\r
523 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
525 pxTimer = xMessage.pxTimer;
\r
527 if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )
\r
529 /* The timer is in a list, remove it. */
\r
530 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
\r
533 traceTIMER_COMMAND_RECEIVED( pxTimer, xMessage.xMessageID, xMessage.xMessageValue );
\r
535 /* In this case the xTimerListsWereSwitched parameter is not used, but
\r
536 it must be present in the function call. prvSampleTimeNow() must be
\r
537 called after the message is received from xTimerQueue so there is no
\r
538 possibility of a higher priority task adding a message to the message
\r
539 queue with a time that is ahead of the timer daemon task (because it
\r
540 pre-empted the timer daemon task after the xTimeNow value was set). */
\r
541 xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );
\r
543 switch( xMessage.xMessageID )
\r
545 case tmrCOMMAND_START :
\r
546 /* Start or restart a timer. */
\r
547 if( prvInsertTimerInActiveList( pxTimer, xMessage.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow, xMessage.xMessageValue ) == pdTRUE )
\r
549 /* The timer expired before it was added to the active timer
\r
550 list. Process it now. */
\r
551 pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );
\r
553 if( pxTimer->uxAutoReload == ( unsigned portBASE_TYPE ) pdTRUE )
\r
555 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START, xMessage.xMessageValue + pxTimer->xTimerPeriodInTicks, NULL, tmrNO_DELAY );
\r
556 configASSERT( xResult );
\r
562 case tmrCOMMAND_STOP :
\r
563 /* The timer has already been removed from the active list.
\r
564 There is nothing to do here. */
\r
567 case tmrCOMMAND_CHANGE_PERIOD :
\r
568 pxTimer->xTimerPeriodInTicks = xMessage.xMessageValue;
\r
569 configASSERT( ( pxTimer->xTimerPeriodInTicks > 0 ) );
\r
570 ( void ) prvInsertTimerInActiveList( pxTimer, ( xTimeNow + pxTimer->xTimerPeriodInTicks ), xTimeNow, xTimeNow );
\r
573 case tmrCOMMAND_DELETE :
\r
574 /* The timer has already been removed from the active list,
\r
575 just free up the memory. */
\r
576 vPortFree( pxTimer );
\r
580 /* Don't expect to get here. */
\r
585 /*-----------------------------------------------------------*/
\r
587 static void prvSwitchTimerLists( portTickType xLastTime )
\r
589 portTickType xNextExpireTime, xReloadTime;
\r
592 portBASE_TYPE xResult;
\r
594 /* Remove compiler warnings if configASSERT() is not defined. */
\r
595 ( void ) xLastTime;
\r
597 /* The tick count has overflowed. The timer lists must be switched.
\r
598 If there are any timers still referenced from the current timer list
\r
599 then they must have expired and should be processed before the lists
\r
601 while( listLIST_IS_EMPTY( pxCurrentTimerList ) == pdFALSE )
\r
603 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );
\r
605 /* Remove the timer from the list. */
\r
606 pxTimer = ( xTIMER * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );
\r
607 ( void ) uxListRemove( &( pxTimer->xTimerListItem ) );
\r
609 /* Execute its callback, then send a command to restart the timer if
\r
610 it is an auto-reload timer. It cannot be restarted here as the lists
\r
611 have not yet been switched. */
\r
612 pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );
\r
614 if( pxTimer->uxAutoReload == ( unsigned portBASE_TYPE ) pdTRUE )
\r
616 /* Calculate the reload value, and if the reload value results in
\r
617 the timer going into the same timer list then it has already expired
\r
618 and the timer should be re-inserted into the current list so it is
\r
619 processed again within this loop. Otherwise a command should be sent
\r
620 to restart the timer to ensure it is only inserted into a list after
\r
621 the lists have been swapped. */
\r
622 xReloadTime = ( xNextExpireTime + pxTimer->xTimerPeriodInTicks );
\r
623 if( xReloadTime > xNextExpireTime )
\r
625 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xReloadTime );
\r
626 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );
\r
627 vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );
\r
631 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START, xNextExpireTime, NULL, tmrNO_DELAY );
\r
632 configASSERT( xResult );
\r
638 pxTemp = pxCurrentTimerList;
\r
639 pxCurrentTimerList = pxOverflowTimerList;
\r
640 pxOverflowTimerList = pxTemp;
\r
642 /*-----------------------------------------------------------*/
\r
644 static void prvCheckForValidListAndQueue( void )
\r
646 /* Check that the list from which active timers are referenced, and the
\r
647 queue used to communicate with the timer service, have been
\r
649 taskENTER_CRITICAL();
\r
651 if( xTimerQueue == NULL )
\r
653 vListInitialise( &xActiveTimerList1 );
\r
654 vListInitialise( &xActiveTimerList2 );
\r
655 pxCurrentTimerList = &xActiveTimerList1;
\r
656 pxOverflowTimerList = &xActiveTimerList2;
\r
657 xTimerQueue = xQueueCreate( ( unsigned portBASE_TYPE ) configTIMER_QUEUE_LENGTH, sizeof( xTIMER_MESSAGE ) );
\r
660 taskEXIT_CRITICAL();
\r
662 /*-----------------------------------------------------------*/
\r
664 portBASE_TYPE xTimerIsTimerActive( xTimerHandle xTimer )
\r
666 portBASE_TYPE xTimerIsInActiveList;
\r
667 xTIMER *pxTimer = ( xTIMER * ) xTimer;
\r
669 /* Is the timer in the list of active timers? */
\r
670 taskENTER_CRITICAL();
\r
672 /* Checking to see if it is in the NULL list in effect checks to see if
\r
673 it is referenced from either the current or the overflow timer lists in
\r
674 one go, but the logic has to be reversed, hence the '!'. */
\r
675 xTimerIsInActiveList = !( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) );
\r
677 taskEXIT_CRITICAL();
\r
679 return xTimerIsInActiveList;
\r
681 /*-----------------------------------------------------------*/
\r
683 void *pvTimerGetTimerID( xTimerHandle xTimer )
\r
685 xTIMER *pxTimer = ( xTIMER * ) xTimer;
\r
687 return pxTimer->pvTimerID;
\r
689 /*-----------------------------------------------------------*/
\r
691 /* This entire source file will be skipped if the application is not configured
\r
692 to include software timer functionality. If you want to include software timer
\r
693 functionality then ensure configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */
\r
694 #endif /* configUSE_TIMERS == 1 */
\r