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 /* IDs for commands that can be sent/received on the timer queue. */
\r
69 #define tmrCOMMAND_START 0
\r
71 /* Misc definitions. */
\r
72 #define tmrNO_DELAY ( portTickType ) 0U
\r
74 /* The definition of the timers themselves. */
\r
75 typedef struct tmrTimerControl
\r
77 const signed char *pcTimerName; /*<< Text name. This is not used by the kernel, it is included simply to make debugging easier. */
\r
78 xListItem xTimerListItem; /*<< Standard linked list item as used by all kernel features for event management. */
\r
79 portTickType xTimerPeriodInTicks;/*<< How quickly and often the timer expires. */
\r
80 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
81 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
82 tmrTIMER_CALLBACK pxCallbackFunction; /*<< The function that will be called when the timer expires. */
\r
85 /* The definition of messages that can be sent and received on the timer
\r
87 typedef struct tmrTimerQueueMessage
\r
89 portBASE_TYPE xMessageID;
\r
90 portTickType xMessageValue;
\r
95 /* The list in which active timers are stored. Timers are referenced in expire
\r
96 time order, with the nearest expiry time at the front of the list. Only the
\r
97 timer service task is allowed to access xActiveTimerList. */
\r
98 PRIVILEGED_DATA static xList xActiveTimerList;
\r
100 /* A queue that is used to send commands to the timer service task. */
\r
101 PRIVILEGED_DATA static xQueueHandle xTimerQueue = NULL;
\r
103 /*-----------------------------------------------------------*/
\r
106 * Called when a timer is about to be modified. If the timer is already in the
\r
107 * list of active timers then it is removed prior to the modification.
\r
109 static void prvRemoveTimerFromActiveList( xTIMER *pxTimer ) PRIVILEGED_FUNCTION;
\r
112 * Send pxMessage to xTimerQueue using a block time of xBlockTime if the
\r
113 * scheduler is running, or a block time of zero if the scheduler is not
\r
116 static portBASE_TYPE prvSendMessageToTimerServiceTask( xTIMER_MESSAGE *pxMessage, portTickType xBlockTime ) PRIVILEGED_FUNCTION;
\r
119 * Initialise the infrustructure used by the timer service task if it has not
\r
120 * been initialised already.
\r
122 static void prvCheckForValidListAndQueue( void ) PRIVILEGED_FUNCTION;
\r
125 * The timer service task (daemon). Timer functionality is controlled by this
\r
126 * task. Other tasks communicate with the timer service task using the
\r
127 * xTimerQueue queue.
\r
129 static void prvTimerTask( void *pvParameters ) PRIVILEGED_FUNCTION;
\r
132 * The following functions handle the commands that are sent to the timer
\r
133 * service task via the xTimerQueue queue.
\r
135 static void prvTimerStart( xTIMER *pxTimer ) PRIVILEGED_FUNCTION;
\r
138 * Called by the timer service task to interpret and process a command it
\r
139 * received on the timer queue.
\r
141 static void prvProcessReceivedCommands( void ) PRIVILEGED_FUNCTION;
\r
143 /*-----------------------------------------------------------*/
\r
145 portBASE_TYPE xTimerCreateTimerTask( void )
\r
147 portBASE_TYPE xReturn = pdFAIL;
\r
149 /* This function is called when the scheduler is started if
\r
150 configUSE_TIMERS is set to 1. Check that the infrustructure used by the
\r
151 timer service task has been created/initialised. If timers have already
\r
152 been created then the initialisation will already have been performed. */
\r
153 prvCheckForValidListAndQueue();
\r
155 if( xTimerQueue != NULL )
\r
157 xReturn = xTaskCreate( prvTimerTask, ( const signed char * ) "Timers", configTIMER_TASK_STACK_DEPTH, NULL, configTIMER_TASK_PRIORITY, NULL );
\r
162 /*-----------------------------------------------------------*/
\r
164 xTimerHandle xTimerCreate( const signed char *pcTimerName, portTickType xTimerPeriodInTicks, unsigned portBASE_TYPE uxAutoReload, void *pvTimerID, tmrTIMER_CALLBACK pxCallbackFunction )
\r
166 xTIMER *pxNewTimer;
\r
168 /* Allocate the timer structure. */
\r
169 pxNewTimer = ( xTIMER * ) pvPortMalloc( sizeof( xTIMER ) );
\r
170 if( pxNewTimer != NULL )
\r
172 /* Ensure the infrustructure used by the timer service task has been
\r
173 created/initialised. */
\r
174 prvCheckForValidListAndQueue();
\r
176 /* Initialise the timer structure members using the function parameters. */
\r
177 pxNewTimer->pcTimerName = pcTimerName;
\r
178 pxNewTimer->xTimerPeriodInTicks = xTimerPeriodInTicks;
\r
179 pxNewTimer->uxAutoReload = uxAutoReload;
\r
180 pxNewTimer->pvTimerID = pvTimerID;
\r
181 pxNewTimer->pxCallbackFunction = pxCallbackFunction;
\r
182 vListInitialiseItem( &( pxNewTimer->xTimerListItem ) );
\r
185 return ( xTimerHandle ) pxNewTimer;
\r
187 /*-----------------------------------------------------------*/
\r
189 portBASE_TYPE xTimerStart( xTimerHandle xTimer, portTickType xBlockTime )
\r
191 portBASE_TYPE xReturn = pdFAIL;
\r
192 xTIMER_MESSAGE xMessage;
\r
194 /* A timer cannot be started unless it is created, and creating a timer
\r
195 will have resulted in the timer queue also being created. */
\r
196 if( xTimerQueue != NULL )
\r
198 /* Send a command to the timer service task to start the xTimer timer. */
\r
199 xMessage.xMessageID = tmrCOMMAND_START;
\r
200 xMessage.pxTimer = ( xTIMER * ) xTimer;
\r
202 prvSendMessageToTimerServiceTask( &xMessage, xBlockTime );
\r
207 /*-----------------------------------------------------------*/
\r
209 static void prvTimerTask( void *pvParameters )
\r
211 portTickType xNextExpireTime, xTimeNow;
\r
214 /* Just to avoid compiler warnings. */
\r
215 ( void ) pvParameters;
\r
219 /* Timers are listed in expiry time order, with the head of the list
\r
220 referencing the task that will expire first. Obtain the time at which
\r
221 the timer with the nearest expiry time will expire. If there are no
\r
222 active timers then just set the next expire time to the maximum possible
\r
223 time to ensure this task does not run unnecessarily. */
\r
224 if( listLIST_IS_EMPTY( &xActiveTimerList ) == pdFALSE )
\r
226 xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( &xActiveTimerList );
\r
230 xNextExpireTime = portMAX_DELAY;
\r
233 /* Has the timer expired? */
\r
234 if( xNextExpireTime <= xTaskGetTickCount() )
\r
236 /* Remove the timer from the list of active timers. */
\r
237 pxTimer = listGET_OWNER_OF_HEAD_ENTRY( &xActiveTimerList );
\r
238 vListRemove( &( pxTimer->xTimerListItem ) );
\r
240 /* If the timer is an autoreload timer then calculate the next
\r
241 expiry time and re-insert the timer in the list of active timers. */
\r
242 if( pxTimer->uxAutoReload == pdTRUE )
\r
244 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ) );
\r
245 vListInsert( &xActiveTimerList, &( pxTimer->xTimerListItem ) );
\r
248 /* Call the timer callback. */
\r
249 pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );
\r
253 /* Block this task until the next timer expires, or a command is
\r
255 taskENTER_CRITICAL();
\r
257 xTimeNow = xTaskGetTickCount();
\r
258 if( xTimeNow < xNextExpireTime )
\r
260 /* This is a simple fast function - a yield will not be
\r
261 performed until after this critical section exits. */
\r
262 vQueueWaitForMessageRestricted( xTimerQueue, ( xNextExpireTime - xTimeNow ) );
\r
265 taskEXIT_CRITICAL();
\r
267 /* Yield to wait for either a command to arrive, or the block time
\r
268 to expire. If a command arrived between the critical section being
\r
269 exited and this yeild then the yield will just return to the same
\r
271 portYIELD_WITHIN_API();
\r
273 /* Empty the command queue, if it contains any commands. */
\r
274 prvProcessReceivedCommands();
\r
278 /*-----------------------------------------------------------*/
\r
280 static void prvProcessReceivedCommands( void )
\r
282 xTIMER_MESSAGE xMessage;
\r
283 portTickType xTimeToExpire;
\r
286 while( xQueueReceive( xTimerQueue, &xMessage, tmrNO_DELAY ) != pdFAIL )
\r
288 pxTimer = xMessage.pxTimer;
\r
290 /* Is the timer already in the list of active timers? */
\r
291 prvRemoveTimerFromActiveList( pxTimer );
\r
293 switch( xMessage.xMessageID )
\r
295 case tmrCOMMAND_START : /* Start or restart a timer. */
\r
296 xTimeToExpire = xTaskGetTickCount() + pxTimer->xTimerPeriodInTicks;
\r
297 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xTimeToExpire );
\r
298 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );
\r
299 vListInsert( &xActiveTimerList, &( pxTimer->xTimerListItem ) );
\r
302 default : /* Don't expect to get here. */
\r
307 /*-----------------------------------------------------------*/
\r
309 static void prvCheckForValidListAndQueue( void )
\r
311 /* Check that the list from which active timers are referenced, and the
\r
312 queue used to communicate with the timer service, have been
\r
314 taskENTER_CRITICAL();
\r
316 if( xTimerQueue == NULL )
\r
318 vListInitialise( &xActiveTimerList );
\r
319 xTimerQueue = xQueueCreate( configTIMER_QUEUE_LENGTH, sizeof( xTIMER_MESSAGE ) );
\r
322 taskEXIT_CRITICAL();
\r
324 /*-----------------------------------------------------------*/
\r
326 void *pvTimerGetTimerID( xTimerHandle xTimer )
\r
328 xTIMER *pxTimer = ( xTIMER * ) xTimer;
\r
330 return pxTimer->pvTimerID;
\r
332 /*-----------------------------------------------------------*/
\r
334 static void prvRemoveTimerFromActiveList( xTIMER *pxTimer )
\r
336 /* Is the timer already in the list of active timers? */
\r
337 if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )
\r
339 /* The timer is in the list, remove it. */
\r
340 vListRemove( &( pxTimer->xTimerListItem ) );
\r
343 /*-----------------------------------------------------------*/
\r
345 static portBASE_TYPE prvSendMessageToTimerServiceTask( xTIMER_MESSAGE *pxMessage, portTickType xBlockTime )
\r
347 portBASE_TYPE xReturn;
\r
349 if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )
\r
351 xReturn = xQueueSendToBack( xTimerQueue, pxMessage, xBlockTime );
\r
355 xReturn = xQueueSendToBack( xTimerQueue, pxMessage, tmrNO_DELAY );
\r
360 /*-----------------------------------------------------------*/
\r
368 portBASE_TYPE xTimerIsTimerActive( xTimerHandle xTimer )
\r
373 void vTimerStop( xTimerHandle xTimer )
\r
378 void vTimerChangePeriod( xTimerHandle xTimer )
\r
382 void vTimerDelete( xTimerHandle xTimer )
\r
385 /*-----------------------------------------------------------*/
\r