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 * Called by the timer service task to interpret and process a command it
\r
117 * received on the timer queue.
\r
119 static void prvProcessReceivedCommands( void ) PRIVILEGED_FUNCTION;
\r
121 /*-----------------------------------------------------------*/
\r
123 portBASE_TYPE xTimerCreateTimerTask( void )
\r
125 portBASE_TYPE xReturn = pdFAIL;
\r
127 /* This function is called when the scheduler is started if
\r
128 configUSE_TIMERS is set to 1. Check that the infrustructure used by the
\r
129 timer service task has been created/initialised. If timers have already
\r
130 been created then the initialisation will already have been performed. */
\r
131 prvCheckForValidListAndQueue();
\r
133 if( xTimerQueue != NULL )
\r
135 xReturn = xTaskCreate( prvTimerTask, ( const signed char * ) "Timer Service", configTIMER_TASK_STACK_DEPTH, NULL, configTIMER_TASK_PRIORITY, NULL );
\r
140 /*-----------------------------------------------------------*/
\r
142 xTimerHandle xTimerCreate( const signed char *pcTimerName, portTickType xTimerPeriodInTicks, unsigned portBASE_TYPE uxAutoReload, void *pvTimerID, tmrTIMER_CALLBACK pxCallbackFunction )
\r
144 xTIMER *pxNewTimer;
\r
146 /* Allocate the timer structure. */
\r
147 pxNewTimer = ( xTIMER * ) pvPortMalloc( sizeof( xTIMER ) );
\r
148 if( pxNewTimer != NULL )
\r
150 /* Ensure the infrustructure used by the timer service task has been
\r
151 created/initialised. */
\r
152 prvCheckForValidListAndQueue();
\r
154 /* Initialise the timer structure members using the function parameters. */
\r
155 pxNewTimer->pcTimerName = pcTimerName;
\r
156 pxNewTimer->xTimerPeriodInTicks = xTimerPeriodInTicks;
\r
157 pxNewTimer->uxAutoReload = uxAutoReload;
\r
158 pxNewTimer->pvTimerID = pvTimerID;
\r
159 pxNewTimer->pxCallbackFunction = pxCallbackFunction;
\r
160 vListInitialiseItem( &( pxNewTimer->xTimerListItem ) );
\r
163 return ( xTimerHandle ) pxNewTimer;
\r
165 /*-----------------------------------------------------------*/
\r
167 portBASE_TYPE xTimerGenericCommand( xTimerHandle xTimer, portBASE_TYPE xCommandID, portTickType xOptionalValue, portTickType xBlockTime )
\r
169 portBASE_TYPE xReturn = pdFAIL;
\r
170 xTIMER_MESSAGE xMessage;
\r
172 /* Send a message to the timer service task to perform a particular action
\r
173 on a particular timer definition. */
\r
174 if( xTimerQueue != NULL )
\r
176 /* Send a command to the timer service task to start the xTimer timer. */
\r
177 xMessage.xMessageID = xCommandID;
\r
178 xMessage.xMessageValue = xOptionalValue;
\r
179 xMessage.pxTimer = ( xTIMER * ) xTimer;
\r
181 if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )
\r
183 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xBlockTime );
\r
187 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, tmrNO_DELAY );
\r
193 /*-----------------------------------------------------------*/
\r
195 static void prvTimerTask( void *pvParameters )
\r
197 portTickType xNextExpireTime, xTimeNow;
\r
200 /* Just to avoid compiler warnings. */
\r
201 ( void ) pvParameters;
\r
205 /* Timers are listed in expiry time order, with the head of the list
\r
206 referencing the task that will expire first. Obtain the time at which
\r
207 the timer with the nearest expiry time will expire. If there are no
\r
208 active timers then just set the next expire time to the maximum possible
\r
209 time to ensure this task does not run unnecessarily. */
\r
210 if( listLIST_IS_EMPTY( &xActiveTimerList ) == pdFALSE )
\r
212 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( &xActiveTimerList );
\r
216 xNextExpireTime = portMAX_DELAY;
\r
219 /* Has the timer expired? */
\r
220 if( xNextExpireTime <= xTaskGetTickCount() )
\r
222 /* Remove the timer from the list of active timers. */
\r
223 pxTimer = listGET_OWNER_OF_HEAD_ENTRY( &xActiveTimerList );
\r
224 vListRemove( &( pxTimer->xTimerListItem ) );
\r
226 /* If the timer is an autoreload timer then calculate the next
\r
227 expiry time and re-insert the timer in the list of active timers. */
\r
228 if( pxTimer->uxAutoReload == pdTRUE )
\r
230 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ) );
\r
231 vListInsert( &xActiveTimerList, &( pxTimer->xTimerListItem ) );
\r
234 /* Call the timer callback. */
\r
235 pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );
\r
239 /* Block this task until the next timer expires, or a command is
\r
241 taskENTER_CRITICAL();
\r
243 xTimeNow = xTaskGetTickCount();
\r
244 if( xTimeNow < xNextExpireTime )
\r
246 /* This is a simple fast function - a yield will not be
\r
247 performed until after this critical section exits. */
\r
248 vQueueWaitForMessageRestricted( xTimerQueue, ( xNextExpireTime - xTimeNow ) );
\r
251 taskEXIT_CRITICAL();
\r
253 /* Yield to wait for either a command to arrive, or the block time
\r
254 to expire. If a command arrived between the critical section being
\r
255 exited and this yeild then the yield will just return to the same
\r
257 portYIELD_WITHIN_API();
\r
259 /* Empty the command queue, if it contains any commands. */
\r
260 prvProcessReceivedCommands();
\r
264 /*-----------------------------------------------------------*/
\r
266 static void prvProcessReceivedCommands( void )
\r
268 xTIMER_MESSAGE xMessage;
\r
269 portTickType xTimeToExpire;
\r
272 while( xQueueReceive( xTimerQueue, &xMessage, tmrNO_DELAY ) != pdFAIL )
\r
274 pxTimer = xMessage.pxTimer;
\r
276 /* Is the timer already in the list of active timers? */
\r
277 if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )
\r
279 /* The timer is in the list, remove it. */
\r
280 vListRemove( &( pxTimer->xTimerListItem ) );
\r
283 switch( xMessage.xMessageID )
\r
285 case tmrCOMMAND_START :
\r
286 /* Start or restart a timer. */
\r
287 xTimeToExpire = xTaskGetTickCount() + pxTimer->xTimerPeriodInTicks;
\r
288 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xTimeToExpire );
\r
289 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );
\r
290 vListInsert( &xActiveTimerList, &( pxTimer->xTimerListItem ) );
\r
293 case tmrCOMMAND_STOP :
\r
294 /* The timer has already been removed from the active list.
\r
295 There is nothing to do here. */
\r
298 case tmrCOMMAND_CHANGE_PERIOD :
\r
299 pxTimer->xTimerPeriodInTicks = xMessage.xMessageValue;
\r
300 xTimeToExpire = xTaskGetTickCount() + pxTimer->xTimerPeriodInTicks;
\r
301 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xTimeToExpire );
\r
302 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );
\r
303 vListInsert( &xActiveTimerList, &( pxTimer->xTimerListItem ) );
\r
306 case tmrCOMMAND_DELETE :
\r
307 /* The timer has already been removed from the active list,
\r
308 just free up the memory. */
\r
309 vPortFree( pxTimer );
\r
313 /* Don't expect to get here. */
\r
318 /*-----------------------------------------------------------*/
\r
320 static void prvCheckForValidListAndQueue( void )
\r
322 /* Check that the list from which active timers are referenced, and the
\r
323 queue used to communicate with the timer service, have been
\r
325 taskENTER_CRITICAL();
\r
327 if( xTimerQueue == NULL )
\r
329 vListInitialise( &xActiveTimerList );
\r
330 xTimerQueue = xQueueCreate( configTIMER_QUEUE_LENGTH, sizeof( xTIMER_MESSAGE ) );
\r
333 taskEXIT_CRITICAL();
\r
335 /*-----------------------------------------------------------*/
\r
337 portBASE_TYPE xTimerIsTimerActive( xTimerHandle xTimer )
\r
339 portBASE_TYPE xTimerIsInActiveList;
\r
340 xTIMER *pxTimer = ( xTIMER * ) xTimer;
\r
342 /* Is the timer in the list of active timers? */
\r
343 taskENTER_CRITICAL();
\r
345 xTimerIsInActiveList = listIS_CONTAINED_WITHIN( &xActiveTimerList, &( pxTimer->xTimerListItem ) );
\r
347 taskEXIT_CRITICAL();
\r
349 return xTimerIsInActiveList;
\r
351 /*-----------------------------------------------------------*/
\r
353 void *pvTimerGetTimerID( xTimerHandle xTimer )
\r
355 xTIMER *pxTimer = ( xTIMER * ) xTimer;
\r
357 return pxTimer->pvTimerID;
\r
359 /*-----------------------------------------------------------*/
\r