2 FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS books - available as PDF or paperback *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
54 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
55 all the API functions to use the MPU wrappers. That should only be done when
\r
56 task.h is included from an application file. */
\r
57 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
59 #include "FreeRTOS.h"
\r
64 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
66 /* Misc definitions. */
\r
67 #define tmrNO_DELAY ( portTickType ) 0U
\r
69 /* The definition of the timers themselves. */
\r
70 typedef struct tmrTimerControl
\r
72 const signed char *pcTimerName; /*<< Text name. This is not used by the kernel, it is included simply to make debugging easier. */
\r
73 xListItem xTimerListItem; /*<< Standard linked list item as used by all kernel features for event management. */
\r
74 portTickType xTimerPeriodInTicks;/*<< How quickly and often the timer expires. */
\r
75 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
76 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
77 tmrTIMER_CALLBACK pxCallbackFunction; /*<< The function that will be called when the timer expires. */
\r
80 /* The definition of messages that can be sent and received on the timer
\r
82 typedef struct tmrTimerQueueMessage
\r
84 portBASE_TYPE xMessageID; /*<< The command being sent to the timer service task. */
\r
85 portTickType xMessageValue; /*<< An optional value used by a subset of commands, for example, when changing the period of a timer. */
\r
86 xTIMER * pxTimer; /*<< The timer to which the command will be applied. */
\r
90 /* The list in which active timers are stored. Timers are referenced in expire
\r
91 time order, with the nearest expiry time at the front of the list. Only the
\r
92 timer service task is allowed to access xActiveTimerList. */
\r
93 PRIVILEGED_DATA static xList xActiveTimerList1;
\r
94 PRIVILEGED_DATA static xList xActiveTimerList2;
\r
95 PRIVILEGED_DATA static xList *pxCurrentTimerList;
\r
96 PRIVILEGED_DATA static xList *pxOverflowTimerList;
\r
98 /* A queue that is used to send commands to the timer service task. */
\r
99 PRIVILEGED_DATA static xQueueHandle xTimerQueue = NULL;
\r
101 /*-----------------------------------------------------------*/
\r
104 * Initialise the infrastructure used by the timer service task if it has not
\r
105 * been initialised already.
\r
107 static void prvCheckForValidListAndQueue( void ) PRIVILEGED_FUNCTION;
\r
110 * The timer service task (daemon). Timer functionality is controlled by this
\r
111 * task. Other tasks communicate with the timer service task using the
\r
112 * xTimerQueue queue.
\r
114 static void prvTimerTask( void *pvParameters ) PRIVILEGED_FUNCTION;
\r
117 * Called by the timer service task to interpret and process a command it
\r
118 * received on the timer queue.
\r
120 static void prvProcessReceivedCommands( void ) PRIVILEGED_FUNCTION;
\r
123 * Insert the timer into either xActiveTimerList1, or xActiveTimerList2,
\r
124 * depending on if the expire time causes a timer counter overflow.
\r
126 static void prvInsertTimerInActiveList( xTIMER *pxTimer, portTickType xNextExpiryTime, portTickType xTimeNow ) PRIVILEGED_FUNCTION;
\r
129 * An active timer has reached its expire time. Reload the timer if it is an
\r
130 * auto reload timer, then call its callback.
\r
132 static void prvProcessExpiredTimer( portTickType xNextExpireTime, portTickType xTimeNow ) PRIVILEGED_FUNCTION;
\r
135 * The tick count has overflowed. Switch the timer lists after ensuring the
\r
136 * current timer list does not still reference some timers.
\r
138 static void prvSwitchTimerLists( portTickType xTimeNow, portTickType xLastTime ) PRIVILEGED_FUNCTION;
\r
141 * Obtain the current tick count, setting *pxTimerListsWereSwitched to pdTRUE
\r
142 * if a tick count overflow occurred since prvSampleTimeNow() was last called.
\r
144 static portTickType prvSampleTimeNow( portBASE_TYPE *pxTimerListsWereSwitched ) PRIVILEGED_FUNCTION;
\r
147 * If the timer list contains any active timers then return the expire time of
\r
148 * the timer that will expire first and set *pxListWasEmpty to false. If the
\r
149 * timer list does not contain any timers then return 0 and set *pxListWasEmpty
\r
152 static portTickType prvLookForExpiredTimer( portBASE_TYPE *pxListWasEmpty ) PRIVILEGED_FUNCTION;
\r
155 * If a timer has expired, process it. Otherwise, block the timer service task
\r
156 * until either a timer does expire or a command is received.
\r
158 static void prvProcessTimerOrBlockTask( portTickType xNextExpireTime, portBASE_TYPE xListWasEmpty ) PRIVILEGED_FUNCTION;
\r
160 /*-----------------------------------------------------------*/
\r
162 portBASE_TYPE xTimerCreateTimerTask( void )
\r
164 portBASE_TYPE xReturn = pdFAIL;
\r
166 /* This function is called when the scheduler is started if
\r
167 configUSE_TIMERS is set to 1. Check that the infrastructure used by the
\r
168 timer service task has been created/initialised. If timers have already
\r
169 been created then the initialisation will already have been performed. */
\r
170 prvCheckForValidListAndQueue();
\r
172 if( xTimerQueue != NULL )
\r
174 xReturn = xTaskCreate( prvTimerTask, ( const signed char * ) "Tmr Svc", configTIMER_TASK_STACK_DEPTH, NULL, configTIMER_TASK_PRIORITY, NULL);
\r
179 /*-----------------------------------------------------------*/
\r
181 xTimerHandle xTimerCreate( const signed char *pcTimerName, portTickType xTimerPeriodInTicks, unsigned portBASE_TYPE uxAutoReload, void *pvTimerID, tmrTIMER_CALLBACK pxCallbackFunction )
\r
183 xTIMER *pxNewTimer;
\r
185 /* Allocate the timer structure. */
\r
186 pxNewTimer = ( xTIMER * ) pvPortMalloc( sizeof( xTIMER ) );
\r
187 if( pxNewTimer != NULL )
\r
189 /* Ensure the infrastructure used by the timer service task has been
\r
190 created/initialised. */
\r
191 prvCheckForValidListAndQueue();
\r
193 /* Initialise the timer structure members using the function parameters. */
\r
194 pxNewTimer->pcTimerName = pcTimerName;
\r
195 pxNewTimer->xTimerPeriodInTicks = xTimerPeriodInTicks;
\r
196 pxNewTimer->uxAutoReload = uxAutoReload;
\r
197 pxNewTimer->pvTimerID = pvTimerID;
\r
198 pxNewTimer->pxCallbackFunction = pxCallbackFunction;
\r
199 vListInitialiseItem( &( pxNewTimer->xTimerListItem ) );
\r
202 return ( xTimerHandle ) pxNewTimer;
\r
204 /*-----------------------------------------------------------*/
\r
206 portBASE_TYPE xTimerGenericCommand( xTimerHandle xTimer, portBASE_TYPE xCommandID, portTickType xOptionalValue, portBASE_TYPE *pxHigherPriorityTaskWoken, portTickType xBlockTime )
\r
208 portBASE_TYPE xReturn = pdFAIL;
\r
209 xTIMER_MESSAGE xMessage;
\r
211 /* Send a message to the timer service task to perform a particular action
\r
212 on a particular timer definition. */
\r
213 if( xTimerQueue != NULL )
\r
215 /* Send a command to the timer service task to start the xTimer timer. */
\r
216 xMessage.xMessageID = xCommandID;
\r
217 xMessage.xMessageValue = xOptionalValue;
\r
218 xMessage.pxTimer = ( xTIMER * ) xTimer;
\r
220 if( pxHigherPriorityTaskWoken == NULL )
\r
222 if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )
\r
224 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xBlockTime );
\r
228 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, tmrNO_DELAY );
\r
233 xReturn = xQueueSendToBackFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );
\r
239 /*-----------------------------------------------------------*/
\r
241 static void prvProcessExpiredTimer( portTickType xNextExpireTime, portTickType xTimeNow )
\r
245 /* Remove the timer from the list of active timers. A check has already
\r
246 been performed to ensure the list is not empty. */
\r
247 pxTimer = ( xTIMER * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );
\r
248 vListRemove( &( pxTimer->xTimerListItem ) );
\r
250 /* If the timer is an auto reload timer then calculate the next
\r
251 expiry time and re-insert the timer in the list of active timers. */
\r
252 if( pxTimer->uxAutoReload == pdTRUE )
\r
254 /* This is the only time a timer is inserted into a list using
\r
255 a time relative to anything other than the current time. It
\r
256 will therefore be inserted into the correct list relative to
\r
257 the time this task thinks it is now, even if a command to
\r
258 switch lists due to a tick count overflow is already waiting in
\r
259 the timer queue. */
\r
260 prvInsertTimerInActiveList( pxTimer, ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ), xTimeNow );
\r
263 /* Call the timer callback. */
\r
264 pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );
\r
266 /*-----------------------------------------------------------*/
\r
268 static void prvTimerTask( void *pvParameters )
\r
270 portTickType xNextExpireTime;
\r
271 portBASE_TYPE xListWasEmpty;
\r
273 /* Just to avoid compiler warnings. */
\r
274 ( void ) pvParameters;
\r
278 /* Query the timers list to see if it contains any timers, and if so,
\r
279 obtain the time at which the next timer will expire. */
\r
280 xNextExpireTime = prvLookForExpiredTimer( &xListWasEmpty );
\r
282 /* If a timer has expired, process it. Otherwise, block this task
\r
283 until either a timer does expire, or a command is received. */
\r
284 prvProcessTimerOrBlockTask( xNextExpireTime, xListWasEmpty );
\r
286 /* Empty the command queue. */
\r
287 prvProcessReceivedCommands();
\r
290 /*-----------------------------------------------------------*/
\r
292 static void prvProcessTimerOrBlockTask( portTickType xNextExpireTime, portBASE_TYPE xListWasEmpty )
\r
294 portTickType xTimeNow;
\r
295 portBASE_TYPE xTimerListsWereSwitched;
\r
299 /* Obtain the time now to make an assessment as to whether the timer
\r
300 has expired or not. If obtaining the time causes the lists to switch
\r
301 then don't process this timer as any timers that remained in the list
\r
302 when the lists were switched will have been processed within the
\r
303 prvSampelTimeNow() function. */
\r
304 xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );
\r
305 if( xTimerListsWereSwitched == pdFALSE )
\r
307 /* The tick count has not overflowed, has the timer expired? */
\r
308 if( ( xListWasEmpty == pdFALSE ) && ( xNextExpireTime <= xTimeNow ) )
\r
310 prvProcessExpiredTimer( xNextExpireTime, xTimeNow );
\r
314 /* The tick count has not overflowed, and the next expire
\r
315 time has not been reached yet. This task should therefore
\r
316 block to wait for the next expire time or a command to be
\r
317 received - whichever comes first. The following line cannot
\r
318 be reached unless xNextExpireTime > xTimeNow, except in the
\r
319 case when the current timer list is empty. */
\r
320 vQueueWaitForMessageRestricted( xTimerQueue, ( xNextExpireTime - xTimeNow ) );
\r
324 if( xTaskResumeAll() == pdFALSE )
\r
326 /* Yield to wait for either a command to arrive, or the block time
\r
327 to expire. If a command arrived between the critical section being
\r
328 exited and this yield then the yield will not cause the task
\r
330 portYIELD_WITHIN_API();
\r
333 /*-----------------------------------------------------------*/
\r
335 static portTickType prvLookForExpiredTimer( portBASE_TYPE *pxListWasEmpty )
\r
337 portTickType xNextExpireTime;
\r
339 /* Timers are listed in expiry time order, with the head of the list
\r
340 referencing the task that will expire first. Obtain the time at which
\r
341 the timer with the nearest expiry time will expire. If there are no
\r
342 active timers then just set the next expire time to 0. That will cause
\r
343 this task to unblock when the tick count overflows, at which point the
\r
344 timer lists will be switched and the next expiry time can be
\r
346 *pxListWasEmpty = listLIST_IS_EMPTY( pxCurrentTimerList );
\r
347 if( *pxListWasEmpty == pdFALSE )
\r
349 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );
\r
353 /* Ensure the task unblocks when the tick count rolls over. */
\r
354 xNextExpireTime = ( portTickType ) 0U;
\r
357 return xNextExpireTime;
\r
359 /*-----------------------------------------------------------*/
\r
361 static portTickType prvSampleTimeNow( portBASE_TYPE *pxTimerListsWereSwitched )
\r
363 portTickType xTimeNow;
\r
364 static portTickType xLastTime = ( portTickType ) 0U;
\r
366 xTimeNow = xTaskGetTickCount();
\r
368 if( xTimeNow < xLastTime )
\r
370 prvSwitchTimerLists( xTimeNow, xLastTime );
\r
371 *pxTimerListsWereSwitched = pdTRUE;
\r
375 *pxTimerListsWereSwitched = pdFALSE;
\r
378 xLastTime = xTimeNow;
\r
382 /*-----------------------------------------------------------*/
\r
384 static void prvInsertTimerInActiveList( xTIMER *pxTimer, portTickType xNextExpiryTime, portTickType xTimeNow )
\r
386 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xNextExpiryTime );
\r
387 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );
\r
389 if( xNextExpiryTime < xTimeNow )
\r
391 vListInsert( pxOverflowTimerList, &( pxTimer->xTimerListItem ) );
\r
395 vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );
\r
398 /*-----------------------------------------------------------*/
\r
400 static void prvProcessReceivedCommands( void )
\r
402 xTIMER_MESSAGE xMessage;
\r
404 portBASE_TYPE xTimerListsWereSwitched;
\r
405 portTickType xTimeNow;
\r
407 /* In this case the xTimerListsWereSwitched parameter is not used, but it
\r
408 must be present in the function call. */
\r
409 xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );
\r
411 while( xQueueReceive( xTimerQueue, &xMessage, tmrNO_DELAY ) != pdFAIL )
\r
413 pxTimer = xMessage.pxTimer;
\r
415 /* Is the timer already in a list of active timers? When the command
\r
416 is trmCOMMAND_PROCESS_TIMER_OVERFLOW, the timer will be NULL as the
\r
417 command is to the task rather than to an individual timer. */
\r
418 if( pxTimer != NULL )
\r
420 if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )
\r
422 /* The timer is in a list, remove it. */
\r
423 vListRemove( &( pxTimer->xTimerListItem ) );
\r
427 switch( xMessage.xMessageID )
\r
429 case tmrCOMMAND_START :
\r
430 /* Start or restart a timer. */
\r
431 prvInsertTimerInActiveList( pxTimer, xTimeNow + pxTimer->xTimerPeriodInTicks, xTimeNow );
\r
434 case tmrCOMMAND_STOP :
\r
435 /* The timer has already been removed from the active list.
\r
436 There is nothing to do here. */
\r
439 case tmrCOMMAND_CHANGE_PERIOD :
\r
440 pxTimer->xTimerPeriodInTicks = xMessage.xMessageValue;
\r
441 prvInsertTimerInActiveList( pxTimer, ( xTimeNow + pxTimer->xTimerPeriodInTicks ), xTimeNow );
\r
444 case tmrCOMMAND_DELETE :
\r
445 /* The timer has already been removed from the active list,
\r
446 just free up the memory. */
\r
447 vPortFree( pxTimer );
\r
451 /* Don't expect to get here. */
\r
456 /*-----------------------------------------------------------*/
\r
458 static void prvSwitchTimerLists( portTickType xTimeNow, portTickType xLastTime )
\r
460 portTickType xNextExpireTime;
\r
463 /* Remove compiler warnings if configASSERT() is not defined. */
\r
464 ( void ) xLastTime;
\r
466 /* The tick count has overflowed. The timer lists must be switched.
\r
467 If there are any timers still referenced from the current timer list
\r
468 then they must have expired and should be processed before the lists
\r
470 while( listLIST_IS_EMPTY( pxCurrentTimerList ) == pdFALSE )
\r
472 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );
\r
473 configASSERT( ( xNextExpireTime >= xLastTime ) );
\r
474 prvProcessExpiredTimer( xNextExpireTime, xTimeNow );
\r
477 pxTemp = pxCurrentTimerList;
\r
478 pxCurrentTimerList = pxOverflowTimerList;
\r
479 pxOverflowTimerList = pxTemp;
\r
481 /*-----------------------------------------------------------*/
\r
483 static void prvCheckForValidListAndQueue( void )
\r
485 /* Check that the list from which active timers are referenced, and the
\r
486 queue used to communicate with the timer service, have been
\r
488 taskENTER_CRITICAL();
\r
490 if( xTimerQueue == NULL )
\r
492 vListInitialise( &xActiveTimerList1 );
\r
493 vListInitialise( &xActiveTimerList2 );
\r
494 pxCurrentTimerList = &xActiveTimerList1;
\r
495 pxOverflowTimerList = &xActiveTimerList2;
\r
496 xTimerQueue = xQueueCreate( configTIMER_QUEUE_LENGTH, sizeof( xTIMER_MESSAGE ) );
\r
499 taskEXIT_CRITICAL();
\r
501 /*-----------------------------------------------------------*/
\r
503 portBASE_TYPE xTimerIsTimerActive( xTimerHandle xTimer )
\r
505 portBASE_TYPE xTimerIsInActiveList;
\r
506 xTIMER *pxTimer = ( xTIMER * ) xTimer;
\r
508 /* Is the timer in the list of active timers? */
\r
509 taskENTER_CRITICAL();
\r
511 /* Checking to see if it is in the NULL list in effect checks to see if
\r
512 it is referenced from either the current or the overflow timer lists in
\r
513 one go, but the logic has to be reversed, hence the '!'. */
\r
514 xTimerIsInActiveList = !( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) );
\r
516 taskEXIT_CRITICAL();
\r
518 return xTimerIsInActiveList;
\r
520 /*-----------------------------------------------------------*/
\r
522 void *pvTimerGetTimerID( xTimerHandle xTimer )
\r
524 xTIMER *pxTimer = ( xTIMER * ) xTimer;
\r
526 return pxTimer->pvTimerID;
\r
528 /*-----------------------------------------------------------*/
\r