]> git.sur5r.net Git - freertos/blob - Source/timers.c
Commit before update to include overflow list functionality.
[freertos] / Source / timers.c
1 /* Need a method of switching to an overflow list. _RB_*/\r
2 \r
3 /*\r
4     FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
5 \r
6     ***************************************************************************\r
7     *                                                                         *\r
8     * If you are:                                                             *\r
9     *                                                                         *\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
14     *                                                                         *\r
15     * then take a look at the FreeRTOS books - available as PDF or paperback  *\r
16     *                                                                         *\r
17     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
18     *                  http://www.FreeRTOS.org/Documentation                  *\r
19     *                                                                         *\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
24     *                                                                         *\r
25     ***************************************************************************\r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \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
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45 \r
46     http://www.FreeRTOS.org - Documentation, latest information, license and\r
47     contact details.\r
48 \r
49     http://www.SafeRTOS.com - A version that is certified for use in safety\r
50     critical systems.\r
51 \r
52     http://www.OpenRTOS.com - Commercial support, development, porting,\r
53     licensing and training services.\r
54 */\r
55 \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
60 \r
61 #include "FreeRTOS.h"\r
62 #include "task.h"\r
63 #include "queue.h"\r
64 #include "timers.h"\r
65 \r
66 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
67 \r
68 /* Misc definitions. */\r
69 #define tmrNO_DELAY             ( portTickType ) 0U\r
70 \r
71 /* The definition of the timers themselves. */\r
72 typedef struct tmrTimerControl\r
73 {\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
80 } xTIMER;\r
81 \r
82 /* The definition of messages that can be sent and received on the timer\r
83 queue. */\r
84 typedef struct tmrTimerQueueMessage\r
85 {\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
89 } xTIMER_MESSAGE;\r
90 \r
91 \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
96 \r
97 /* A queue that is used to send commands to the timer service task. */\r
98 PRIVILEGED_DATA static xQueueHandle xTimerQueue = NULL;\r
99 \r
100 /*-----------------------------------------------------------*/\r
101 \r
102 /*\r
103  * Initialise the infrustructure used by the timer service task if it has not\r
104  * been initialised already.\r
105  */\r
106 static void prvCheckForValidListAndQueue( void ) PRIVILEGED_FUNCTION;\r
107 \r
108 /*\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
112  */\r
113 static void prvTimerTask( void *pvParameters ) PRIVILEGED_FUNCTION;\r
114 \r
115 /*\r
116  * Called by the timer service task to interpret and process a command it\r
117  * received on the timer queue.\r
118  */\r
119 static void     prvProcessReceivedCommands( void ) PRIVILEGED_FUNCTION;\r
120 \r
121 /*-----------------------------------------------------------*/\r
122 \r
123 portBASE_TYPE xTimerCreateTimerTask( void )\r
124 {\r
125 portBASE_TYPE xReturn = pdFAIL;\r
126 \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
132 \r
133         if( xTimerQueue != NULL )\r
134         {\r
135                 xReturn = xTaskCreate( prvTimerTask, ( const signed char * ) "Timer Service", configTIMER_TASK_STACK_DEPTH, NULL, configTIMER_TASK_PRIORITY, NULL );\r
136         }\r
137 \r
138         return xReturn;\r
139 }\r
140 /*-----------------------------------------------------------*/\r
141 \r
142 xTimerHandle xTimerCreate( const signed char *pcTimerName, portTickType xTimerPeriodInTicks, unsigned portBASE_TYPE uxAutoReload, void *pvTimerID, tmrTIMER_CALLBACK pxCallbackFunction )\r
143 {\r
144 xTIMER *pxNewTimer;\r
145 \r
146         /* Allocate the timer structure. */\r
147         pxNewTimer = ( xTIMER * ) pvPortMalloc( sizeof( xTIMER ) );\r
148         if( pxNewTimer != NULL )\r
149         {\r
150                 /* Ensure the infrustructure used by the timer service task has been\r
151                 created/initialised. */\r
152                 prvCheckForValidListAndQueue();\r
153 \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
161         }\r
162 \r
163         return ( xTimerHandle ) pxNewTimer;\r
164 }\r
165 /*-----------------------------------------------------------*/\r
166 \r
167 portBASE_TYPE xTimerGenericCommand( xTimerHandle xTimer, portBASE_TYPE xCommandID, portTickType xOptionalValue, portTickType xBlockTime )\r
168 {\r
169 portBASE_TYPE xReturn = pdFAIL;\r
170 xTIMER_MESSAGE xMessage;\r
171 \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
175         {\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
180 \r
181                 if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )\r
182                 {\r
183                         xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xBlockTime );\r
184                 }\r
185                 else\r
186                 {\r
187                         xReturn = xQueueSendToBack( xTimerQueue, &xMessage, tmrNO_DELAY );\r
188                 }\r
189         }\r
190 \r
191         return xReturn;\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 static void prvTimerTask( void *pvParameters )\r
196 {\r
197 portTickType xNextExpireTime, xTimeNow;\r
198 xTIMER *pxTimer;\r
199 \r
200         /* Just to avoid compiler warnings. */\r
201         ( void ) pvParameters;\r
202 \r
203         for( ;; )\r
204         {\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
211                 {\r
212                         xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( &xActiveTimerList );\r
213                 }\r
214                 else\r
215                 {\r
216                         xNextExpireTime = portMAX_DELAY;\r
217                 }\r
218 \r
219                 /* Has the timer expired? */\r
220                 if( xNextExpireTime <= xTaskGetTickCount() )\r
221                 {\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
225 \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
229                         {\r
230                                 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ) );\r
231                                 vListInsert( &xActiveTimerList, &( pxTimer->xTimerListItem ) );\r
232                         }\r
233 \r
234                         /* Call the timer callback. */\r
235                         pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );\r
236                 }\r
237                 else\r
238                 {\r
239                         /* Block this task until the next timer expires, or a command is\r
240                         received. */\r
241                         taskENTER_CRITICAL();\r
242                         {\r
243                                 xTimeNow = xTaskGetTickCount();\r
244                                 if( xTimeNow < xNextExpireTime )\r
245                                 {\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
249                                 }\r
250                         }\r
251                         taskEXIT_CRITICAL();\r
252 \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
256                         task. */\r
257                         portYIELD_WITHIN_API();\r
258 \r
259                         /* Empty the command queue, if it contains any commands. */\r
260                         prvProcessReceivedCommands();\r
261                 }\r
262         }\r
263 }\r
264 /*-----------------------------------------------------------*/\r
265 \r
266 static void     prvProcessReceivedCommands( void )\r
267 {\r
268 xTIMER_MESSAGE xMessage;\r
269 portTickType xTimeToExpire;\r
270 xTIMER *pxTimer;\r
271 \r
272         while( xQueueReceive( xTimerQueue, &xMessage, tmrNO_DELAY ) != pdFAIL )\r
273         {\r
274                 pxTimer = xMessage.pxTimer;\r
275 \r
276                 /* Is the timer already in the list of active timers? */\r
277                 if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )\r
278                 {\r
279                         /* The timer is in the list, remove it. */\r
280                         vListRemove( &( pxTimer->xTimerListItem ) );\r
281                 }\r
282 \r
283                 switch( xMessage.xMessageID )\r
284                 {\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
291                                 break;\r
292 \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
296                                 break;\r
297 \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
304                                 break;\r
305 \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
310                                 break;\r
311 \r
312                         default :                       \r
313                                 /* Don't expect to get here. */\r
314                                 break;\r
315                 }\r
316         }\r
317 }\r
318 /*-----------------------------------------------------------*/\r
319 \r
320 static void prvCheckForValidListAndQueue( void )\r
321 {\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
324         initialised. */\r
325         taskENTER_CRITICAL();\r
326         {\r
327                 if( xTimerQueue == NULL )\r
328                 {\r
329                         vListInitialise( &xActiveTimerList );\r
330                         xTimerQueue = xQueueCreate( configTIMER_QUEUE_LENGTH, sizeof( xTIMER_MESSAGE ) );\r
331                 }\r
332         }\r
333         taskEXIT_CRITICAL();\r
334 }\r
335 /*-----------------------------------------------------------*/\r
336 \r
337 portBASE_TYPE xTimerIsTimerActive( xTimerHandle xTimer )\r
338 {\r
339 portBASE_TYPE xTimerIsInActiveList;\r
340 xTIMER *pxTimer = ( xTIMER * ) xTimer;\r
341 \r
342         /* Is the timer in the list of active timers? */\r
343         taskENTER_CRITICAL();\r
344         {\r
345                 xTimerIsInActiveList = listIS_CONTAINED_WITHIN( &xActiveTimerList, &( pxTimer->xTimerListItem ) );\r
346         }\r
347         taskEXIT_CRITICAL();\r
348 \r
349         return xTimerIsInActiveList;\r
350 }\r
351 /*-----------------------------------------------------------*/\r
352 \r
353 void *pvTimerGetTimerID( xTimerHandle xTimer )\r
354 {\r
355 xTIMER *pxTimer = ( xTIMER * ) xTimer;\r
356 \r
357         return pxTimer->pvTimerID;\r
358 }\r
359 /*-----------------------------------------------------------*/\r
360 \r