]> git.sur5r.net Git - freertos/blob - Source/timers.c
Continue work on the timers module.
[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  * The following functions handle the commands that are sent to the timer\r
117  * service task via the xTimerQueue queue.\r
118  */\r
119 static void prvTimerStart( xTIMER *pxTimer ) PRIVILEGED_FUNCTION;\r
120 \r
121 /*\r
122  * Called by the timer service task to interpret and process a command it\r
123  * received on the timer queue. \r
124  */\r
125 static void     prvProcessReceivedCommands( void ) PRIVILEGED_FUNCTION;\r
126 \r
127 /*-----------------------------------------------------------*/\r
128 \r
129 portBASE_TYPE xTimerCreateTimerTask( void )\r
130 {\r
131 portBASE_TYPE xReturn = pdFAIL;\r
132 \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
138 \r
139         if( xTimerQueue != NULL )\r
140         {\r
141                 xReturn = xTaskCreate( prvTimerTask, ( const signed char * ) "Timers", configTIMER_TASK_STACK_DEPTH, NULL, configTIMER_TASK_PRIORITY, NULL );\r
142         }\r
143 \r
144         return xReturn;\r
145 }\r
146 /*-----------------------------------------------------------*/\r
147 \r
148 xTimerHandle xTimerCreate( const signed char *pcTimerName, portTickType xTimerPeriodInTicks, unsigned portBASE_TYPE uxAutoReload, void *pvTimerID, tmrTIMER_CALLBACK pxCallbackFunction )\r
149 {\r
150 xTIMER *pxNewTimer;\r
151 \r
152         /* Allocate the timer structure. */\r
153         pxNewTimer = ( xTIMER * ) pvPortMalloc( sizeof( xTIMER ) );\r
154         if( pxNewTimer != NULL )\r
155         {\r
156                 /* Ensure the infrustructure used by the timer service task has been\r
157                 created/initialised. */\r
158                 prvCheckForValidListAndQueue();\r
159 \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
167         }\r
168 \r
169         return ( xTimerHandle ) pxNewTimer;\r
170 }\r
171 /*-----------------------------------------------------------*/\r
172 \r
173 portBASE_TYPE xTimerGenericCommand( xTimerHandle xTimer, portBASE_TYPE xCommandID, portTickType xOptionalValue, portTickType xBlockTime )\r
174 {\r
175 portBASE_TYPE xReturn = pdFAIL;\r
176 xTIMER_MESSAGE xMessage;\r
177 \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
181         {\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
186 \r
187                 if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )\r
188                 {\r
189                         xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xBlockTime );\r
190                 }\r
191                 else\r
192                 {\r
193                         xReturn = xQueueSendToBack( xTimerQueue, &xMessage, tmrNO_DELAY );\r
194                 }\r
195         }\r
196 \r
197         return xReturn;\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r
201 static void prvTimerTask( void *pvParameters )\r
202 {\r
203 portTickType xNextExpireTime, xTimeNow;\r
204 xTIMER *pxTimer;\r
205 \r
206         /* Just to avoid compiler warnings. */\r
207         ( void ) pvParameters;\r
208 \r
209         for( ;; )\r
210         {\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
217                 {\r
218                         xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( &xActiveTimerList );\r
219                 }\r
220                 else\r
221                 {\r
222                         xNextExpireTime = portMAX_DELAY;\r
223                 }\r
224 \r
225                 /* Has the timer expired? */\r
226                 if( xNextExpireTime <= xTaskGetTickCount() )\r
227                 {\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
231 \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
235                         {\r
236                                 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ) );\r
237                                 vListInsert( &xActiveTimerList, &( pxTimer->xTimerListItem ) );\r
238                         }\r
239 \r
240                         /* Call the timer callback. */\r
241                         pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );\r
242                 }\r
243                 else\r
244                 {\r
245                         /* Block this task until the next timer expires, or a command is\r
246                         received. */\r
247                         taskENTER_CRITICAL();\r
248                         {\r
249                                 xTimeNow = xTaskGetTickCount();\r
250                                 if( xTimeNow < xNextExpireTime )\r
251                                 {\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
255                                 }\r
256                         }\r
257                         taskEXIT_CRITICAL();\r
258 \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
262                         task. */\r
263                         portYIELD_WITHIN_API();\r
264 \r
265                         /* Empty the command queue, if it contains any commands. */\r
266                         prvProcessReceivedCommands();\r
267                 }\r
268         }\r
269 }\r
270 /*-----------------------------------------------------------*/\r
271 \r
272 static void     prvProcessReceivedCommands( void )\r
273 {\r
274 xTIMER_MESSAGE xMessage;\r
275 portTickType xTimeToExpire;\r
276 xTIMER *pxTimer;\r
277 \r
278         while( xQueueReceive( xTimerQueue, &xMessage, tmrNO_DELAY ) != pdFAIL )\r
279         {\r
280                 pxTimer = xMessage.pxTimer;\r
281 \r
282                 /* Is the timer already in the list of active timers? */\r
283                 if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )\r
284                 {\r
285                         /* The timer is in the list, remove it. */\r
286                         vListRemove( &( pxTimer->xTimerListItem ) );\r
287                 }\r
288 \r
289                 switch( xMessage.xMessageID )\r
290                 {\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
297                                 break;\r
298 \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
302                                 break;\r
303 \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
310                                 break;\r
311 \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
316                                 break;\r
317 \r
318                         default :                       \r
319                                 /* Don't expect to get here. */\r
320                                 break;\r
321                 }\r
322         }\r
323 }\r
324 /*-----------------------------------------------------------*/\r
325 \r
326 static void prvCheckForValidListAndQueue( void )\r
327 {\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
330         initialised. */\r
331         taskENTER_CRITICAL();\r
332         {\r
333                 if( xTimerQueue == NULL )\r
334                 {\r
335                         vListInitialise( &xActiveTimerList );\r
336                         xTimerQueue = xQueueCreate( configTIMER_QUEUE_LENGTH, sizeof( xTIMER_MESSAGE ) );\r
337                 }\r
338         }\r
339         taskEXIT_CRITICAL();\r
340 }\r
341 /*-----------------------------------------------------------*/\r
342 \r
343 portBASE_TYPE xTimerIsTimerActive( xTimerHandle xTimer )\r
344 {\r
345 portBASE_TYPE xTimerIsInActiveList;\r
346 xTIMER *pxTimer = ( xTIMER * ) xTimer;\r
347 \r
348         /* Is the timer in the list of active timers? */\r
349         taskENTER_CRITICAL();\r
350         {\r
351                 xTimerIsInActiveList = listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) );\r
352         }\r
353         taskEXIT_CRITICAL();\r
354 \r
355         return xTimerIsInActiveList;\r
356 }\r
357 /*-----------------------------------------------------------*/\r
358 \r
359 void *pvTimerGetTimerID( xTimerHandle xTimer )\r
360 {\r
361 xTIMER *pxTimer = ( xTIMER * ) xTimer;\r
362 \r
363         return pxTimer->pvTimerID;\r
364 }\r
365 /*-----------------------------------------------------------*/\r
366 \r