1 /* Need a method of switching to an overflow list. _RB_*/
\r
4 FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
6 ***************************************************************************
\r
10 * + New to FreeRTOS, *
\r
11 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
12 * + Looking for basic training, *
\r
13 * + Wanting to improve your FreeRTOS skills and productivity *
\r
15 * then take a look at the FreeRTOS books - available as PDF or paperback *
\r
17 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
18 * http://www.FreeRTOS.org/Documentation *
\r
20 * A pdf reference manual is also available. Both are usually delivered *
\r
21 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
22 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
23 * exceptional circumstances). Thank you for your support! *
\r
25 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 ***NOTE*** The exception to the GPL is included to allow you to distribute
\r
33 a combined work that includes FreeRTOS without being obliged to provide the
\r
34 source code for proprietary components outside of the FreeRTOS kernel.
\r
35 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
36 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
37 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
49 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
52 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
53 licensing and training services.
\r
56 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
57 all the API functions to use the MPU wrappers. That should only be done when
\r
58 task.h is included from an application file. */
\r
59 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
61 #include "FreeRTOS.h"
\r
66 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
68 /* Misc definitions. */
\r
69 #define tmrNO_DELAY ( portTickType ) 0U
\r
71 /* The definition of the timers themselves. */
\r
72 typedef struct tmrTimerControl
\r
74 const signed char *pcTimerName; /*<< Text name. This is not used by the kernel, it is included simply to make debugging easier. */
\r
75 xListItem xTimerListItem; /*<< Standard linked list item as used by all kernel features for event management. */
\r
76 portTickType xTimerPeriodInTicks;/*<< How quickly and often the timer expires. */
\r
77 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
78 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
79 tmrTIMER_CALLBACK pxCallbackFunction; /*<< The function that will be called when the timer expires. */
\r
82 /* The definition of messages that can be sent and received on the timer
\r
84 typedef struct tmrTimerQueueMessage
\r
86 portBASE_TYPE xMessageID; /*<< The command being sent to the timer service task. */
\r
87 portTickType xMessageValue; /*<< An optional value used by a subset of commands, for example, when chaning the period of a timer. */
\r
88 xTIMER * pxTimer; /*<< The timer to which the command will be applied. */
\r
92 /* The list in which active timers are stored. Timers are referenced in expire
\r
93 time order, with the nearest expiry time at the front of the list. Only the
\r
94 timer service task is allowed to access xActiveTimerList. */
\r
95 PRIVILEGED_DATA static xList xActiveTimerList;
\r
97 /* A queue that is used to send commands to the timer service task. */
\r
98 PRIVILEGED_DATA static xQueueHandle xTimerQueue = NULL;
\r
100 /*-----------------------------------------------------------*/
\r
103 * Initialise the infrustructure used by the timer service task if it has not
\r
104 * been initialised already.
\r
106 static void prvCheckForValidListAndQueue( void ) PRIVILEGED_FUNCTION;
\r
109 * The timer service task (daemon). Timer functionality is controlled by this
\r
110 * task. Other tasks communicate with the timer service task using the
\r
111 * xTimerQueue queue.
\r
113 static void prvTimerTask( void *pvParameters ) PRIVILEGED_FUNCTION;
\r
116 * The following functions handle the commands that are sent to the timer
\r
117 * service task via the xTimerQueue queue.
\r
119 static void prvTimerStart( xTIMER *pxTimer ) PRIVILEGED_FUNCTION;
\r
122 * Called by the timer service task to interpret and process a command it
\r
123 * received on the timer queue.
\r
125 static void prvProcessReceivedCommands( void ) PRIVILEGED_FUNCTION;
\r
127 /*-----------------------------------------------------------*/
\r
129 portBASE_TYPE xTimerCreateTimerTask( void )
\r
131 portBASE_TYPE xReturn = pdFAIL;
\r
133 /* This function is called when the scheduler is started if
\r
134 configUSE_TIMERS is set to 1. Check that the infrustructure used by the
\r
135 timer service task has been created/initialised. If timers have already
\r
136 been created then the initialisation will already have been performed. */
\r
137 prvCheckForValidListAndQueue();
\r
139 if( xTimerQueue != NULL )
\r
141 xReturn = xTaskCreate( prvTimerTask, ( const signed char * ) "Timers", configTIMER_TASK_STACK_DEPTH, NULL, configTIMER_TASK_PRIORITY, NULL );
\r
146 /*-----------------------------------------------------------*/
\r
148 xTimerHandle xTimerCreate( const signed char *pcTimerName, portTickType xTimerPeriodInTicks, unsigned portBASE_TYPE uxAutoReload, void *pvTimerID, tmrTIMER_CALLBACK pxCallbackFunction )
\r
150 xTIMER *pxNewTimer;
\r
152 /* Allocate the timer structure. */
\r
153 pxNewTimer = ( xTIMER * ) pvPortMalloc( sizeof( xTIMER ) );
\r
154 if( pxNewTimer != NULL )
\r
156 /* Ensure the infrustructure used by the timer service task has been
\r
157 created/initialised. */
\r
158 prvCheckForValidListAndQueue();
\r
160 /* Initialise the timer structure members using the function parameters. */
\r
161 pxNewTimer->pcTimerName = pcTimerName;
\r
162 pxNewTimer->xTimerPeriodInTicks = xTimerPeriodInTicks;
\r
163 pxNewTimer->uxAutoReload = uxAutoReload;
\r
164 pxNewTimer->pvTimerID = pvTimerID;
\r
165 pxNewTimer->pxCallbackFunction = pxCallbackFunction;
\r
166 vListInitialiseItem( &( pxNewTimer->xTimerListItem ) );
\r
169 return ( xTimerHandle ) pxNewTimer;
\r
171 /*-----------------------------------------------------------*/
\r
173 portBASE_TYPE xTimerGenericCommand( xTimerHandle xTimer, portBASE_TYPE xCommandID, portTickType xOptionalValue, portTickType xBlockTime )
\r
175 portBASE_TYPE xReturn = pdFAIL;
\r
176 xTIMER_MESSAGE xMessage;
\r
178 /* Send a message to the timer service task to perform a particular action
\r
179 on a particular timer definition. */
\r
180 if( xTimerQueue != NULL )
\r
182 /* Send a command to the timer service task to start the xTimer timer. */
\r
183 xMessage.xMessageID = xCommandID;
\r
184 xMessage.xMessageValue = xOptionalValue;
\r
185 xMessage.pxTimer = ( xTIMER * ) xTimer;
\r
187 if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )
\r
189 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xBlockTime );
\r
193 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, tmrNO_DELAY );
\r
199 /*-----------------------------------------------------------*/
\r
201 static void prvTimerTask( void *pvParameters )
\r
203 portTickType xNextExpireTime, xTimeNow;
\r
206 /* Just to avoid compiler warnings. */
\r
207 ( void ) pvParameters;
\r
211 /* Timers are listed in expiry time order, with the head of the list
\r
212 referencing the task that will expire first. Obtain the time at which
\r
213 the timer with the nearest expiry time will expire. If there are no
\r
214 active timers then just set the next expire time to the maximum possible
\r
215 time to ensure this task does not run unnecessarily. */
\r
216 if( listLIST_IS_EMPTY( &xActiveTimerList ) == pdFALSE )
\r
218 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( &xActiveTimerList );
\r
222 xNextExpireTime = portMAX_DELAY;
\r
225 /* Has the timer expired? */
\r
226 if( xNextExpireTime <= xTaskGetTickCount() )
\r
228 /* Remove the timer from the list of active timers. */
\r
229 pxTimer = listGET_OWNER_OF_HEAD_ENTRY( &xActiveTimerList );
\r
230 vListRemove( &( pxTimer->xTimerListItem ) );
\r
232 /* If the timer is an autoreload timer then calculate the next
\r
233 expiry time and re-insert the timer in the list of active timers. */
\r
234 if( pxTimer->uxAutoReload == pdTRUE )
\r
236 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ) );
\r
237 vListInsert( &xActiveTimerList, &( pxTimer->xTimerListItem ) );
\r
240 /* Call the timer callback. */
\r
241 pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );
\r
245 /* Block this task until the next timer expires, or a command is
\r
247 taskENTER_CRITICAL();
\r
249 xTimeNow = xTaskGetTickCount();
\r
250 if( xTimeNow < xNextExpireTime )
\r
252 /* This is a simple fast function - a yield will not be
\r
253 performed until after this critical section exits. */
\r
254 vQueueWaitForMessageRestricted( xTimerQueue, ( xNextExpireTime - xTimeNow ) );
\r
257 taskEXIT_CRITICAL();
\r
259 /* Yield to wait for either a command to arrive, or the block time
\r
260 to expire. If a command arrived between the critical section being
\r
261 exited and this yeild then the yield will just return to the same
\r
263 portYIELD_WITHIN_API();
\r
265 /* Empty the command queue, if it contains any commands. */
\r
266 prvProcessReceivedCommands();
\r
270 /*-----------------------------------------------------------*/
\r
272 static void prvProcessReceivedCommands( void )
\r
274 xTIMER_MESSAGE xMessage;
\r
275 portTickType xTimeToExpire;
\r
278 while( xQueueReceive( xTimerQueue, &xMessage, tmrNO_DELAY ) != pdFAIL )
\r
280 pxTimer = xMessage.pxTimer;
\r
282 /* Is the timer already in the list of active timers? */
\r
283 if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )
\r
285 /* The timer is in the list, remove it. */
\r
286 vListRemove( &( pxTimer->xTimerListItem ) );
\r
289 switch( xMessage.xMessageID )
\r
291 case tmrCOMMAND_START :
\r
292 /* Start or restart a timer. */
\r
293 xTimeToExpire = xTaskGetTickCount() + pxTimer->xTimerPeriodInTicks;
\r
294 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xTimeToExpire );
\r
295 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );
\r
296 vListInsert( &xActiveTimerList, &( pxTimer->xTimerListItem ) );
\r
299 case tmrCOMMAND_STOP :
\r
300 /* The timer has already been removed from the active list.
\r
301 There is nothing to do here. */
\r
304 case tmrCOMMAND_CHANGE_PERIOD :
\r
305 pxTimer->xTimerPeriodInTicks = xMessage.xMessageValue;
\r
306 xTimeToExpire = xTaskGetTickCount() + pxTimer->xTimerPeriodInTicks;
\r
307 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xTimeToExpire );
\r
308 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );
\r
309 vListInsert( &xActiveTimerList, &( pxTimer->xTimerListItem ) );
\r
312 case tmrCOMMAND_DELETE :
\r
313 /* The timer has already been removed from the active list,
\r
314 just free up the memory. */
\r
315 vPortFree( pxTimer );
\r
319 /* Don't expect to get here. */
\r
324 /*-----------------------------------------------------------*/
\r
326 static void prvCheckForValidListAndQueue( void )
\r
328 /* Check that the list from which active timers are referenced, and the
\r
329 queue used to communicate with the timer service, have been
\r
331 taskENTER_CRITICAL();
\r
333 if( xTimerQueue == NULL )
\r
335 vListInitialise( &xActiveTimerList );
\r
336 xTimerQueue = xQueueCreate( configTIMER_QUEUE_LENGTH, sizeof( xTIMER_MESSAGE ) );
\r
339 taskEXIT_CRITICAL();
\r
341 /*-----------------------------------------------------------*/
\r
343 portBASE_TYPE xTimerIsTimerActive( xTimerHandle xTimer )
\r
345 portBASE_TYPE xTimerIsInActiveList;
\r
346 xTIMER *pxTimer = ( xTIMER * ) xTimer;
\r
348 /* Is the timer in the list of active timers? */
\r
349 taskENTER_CRITICAL();
\r
351 xTimerIsInActiveList = listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) );
\r
353 taskEXIT_CRITICAL();
\r
355 return xTimerIsInActiveList;
\r
357 /*-----------------------------------------------------------*/
\r
359 void *pvTimerGetTimerID( xTimerHandle xTimer )
\r
361 xTIMER *pxTimer = ( xTIMER * ) xTimer;
\r
363 return pxTimer->pvTimerID;
\r
365 /*-----------------------------------------------------------*/
\r