]> git.sur5r.net Git - freertos/blob - Source/timers.c
Continue work on timers module - work in progress.
[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 /* IDs for commands that can be sent/received on the timer queue. */\r
69 #define tmrCOMMAND_START                0\r
70 \r
71 /* Misc definitions. */\r
72 #define tmrNO_DELAY             ( portTickType ) 0U\r
73 \r
74 /* The definition of the timers themselves. */\r
75 typedef struct tmrTimerControl\r
76 {\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
83 } xTIMER;\r
84 \r
85 /* The definition of messages that can be sent and received on the timer \r
86 queue. */\r
87 typedef struct tmrTimerQueueMessage\r
88 {\r
89         portBASE_TYPE                   xMessageID;\r
90         portTickType                    xMessageValue;\r
91         xTIMER *                                pxTimer;\r
92 } xTIMER_MESSAGE;\r
93 \r
94 \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
99 \r
100 /* A queue that is used to send commands to the timer service task. */\r
101 PRIVILEGED_DATA static xQueueHandle xTimerQueue = NULL;\r
102 \r
103 /*-----------------------------------------------------------*/\r
104 \r
105 /*\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
108  */\r
109 static void prvRemoveTimerFromActiveList( xTIMER *pxTimer ) PRIVILEGED_FUNCTION;\r
110 \r
111 /*\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
114  * running.\r
115  */\r
116 static portBASE_TYPE prvSendMessageToTimerServiceTask( xTIMER_MESSAGE *pxMessage, portTickType xBlockTime ) PRIVILEGED_FUNCTION;\r
117 \r
118 /* \r
119  * Initialise the infrustructure used by the timer service task if it has not\r
120  * been initialised already.\r
121  */\r
122 static void prvCheckForValidListAndQueue( void ) PRIVILEGED_FUNCTION;\r
123 \r
124 /*\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
128  */\r
129 static void prvTimerTask( void *pvParameters ) PRIVILEGED_FUNCTION;\r
130 \r
131 /* \r
132  * The following functions handle the commands that are sent to the timer\r
133  * service task via the xTimerQueue queue.\r
134  */\r
135 static void prvTimerStart( xTIMER *pxTimer ) PRIVILEGED_FUNCTION;\r
136 \r
137 /*\r
138  * Called by the timer service task to interpret and process a command it\r
139  * received on the timer queue. \r
140  */\r
141 static void     prvProcessReceivedCommands( void ) PRIVILEGED_FUNCTION;\r
142 \r
143 /*-----------------------------------------------------------*/\r
144 \r
145 portBASE_TYPE xTimerCreateTimerTask( void )\r
146 {\r
147 portBASE_TYPE xReturn = pdFAIL;\r
148 \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
154 \r
155         if( xTimerQueue != NULL )\r
156         {\r
157                 xReturn = xTaskCreate( prvTimerTask, ( const signed char * ) "Timers", configTIMER_TASK_STACK_DEPTH, NULL, configTIMER_TASK_PRIORITY, NULL );\r
158         }\r
159 \r
160         return xReturn;\r
161 }\r
162 /*-----------------------------------------------------------*/\r
163 \r
164 xTimerHandle xTimerCreate( const signed char *pcTimerName, portTickType xTimerPeriodInTicks, unsigned portBASE_TYPE uxAutoReload, void *pvTimerID, tmrTIMER_CALLBACK pxCallbackFunction )\r
165 {\r
166 xTIMER *pxNewTimer;\r
167 \r
168         /* Allocate the timer structure. */\r
169         pxNewTimer = ( xTIMER * ) pvPortMalloc( sizeof( xTIMER ) );\r
170         if( pxNewTimer != NULL )\r
171         {\r
172                 /* Ensure the infrustructure used by the timer service task has been\r
173                 created/initialised. */\r
174                 prvCheckForValidListAndQueue();\r
175 \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
183         }\r
184 \r
185         return ( xTimerHandle ) pxNewTimer;\r
186 }\r
187 /*-----------------------------------------------------------*/\r
188 \r
189 portBASE_TYPE xTimerStart( xTimerHandle xTimer, portTickType xBlockTime )\r
190 {\r
191 portBASE_TYPE xReturn = pdFAIL;\r
192 xTIMER_MESSAGE xMessage;\r
193 \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
197         {\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
201 \r
202                 prvSendMessageToTimerServiceTask( &xMessage, xBlockTime );\r
203         }\r
204 \r
205         return xReturn;\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 static void prvTimerTask( void *pvParameters )\r
210 {\r
211 portTickType xNextExpireTime, xTimeNow;\r
212 xTIMER *pxTimer;\r
213 \r
214         /* Just to avoid compiler warnings. */\r
215         ( void ) pvParameters;\r
216 \r
217         for( ;; )\r
218         {\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
225                 {\r
226                         xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( &xActiveTimerList );\r
227                 }\r
228                 else\r
229                 {\r
230                         xNextExpireTime = portMAX_DELAY;\r
231                 }\r
232 \r
233                 /* Has the timer expired? */\r
234                 if( xNextExpireTime <= xTaskGetTickCount() )\r
235                 {\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
239 \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
243                         {\r
244                                 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ) );\r
245                                 vListInsert( &xActiveTimerList, &( pxTimer->xTimerListItem ) );\r
246                         }\r
247 \r
248                         /* Call the timer callback. */\r
249                         pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );\r
250                 }\r
251                 else\r
252                 {\r
253                         /* Block this task until the next timer expires, or a command is\r
254                         received. */\r
255                         taskENTER_CRITICAL();\r
256                         {\r
257                                 xTimeNow = xTaskGetTickCount();\r
258                                 if( xTimeNow < xNextExpireTime )\r
259                                 {\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
263                                 }\r
264                         }\r
265                         taskEXIT_CRITICAL();\r
266 \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
270                         task. */\r
271                         portYIELD_WITHIN_API();\r
272 \r
273                         /* Empty the command queue, if it contains any commands. */\r
274                         prvProcessReceivedCommands();\r
275                 }\r
276         }\r
277 }\r
278 /*-----------------------------------------------------------*/\r
279 \r
280 static void     prvProcessReceivedCommands( void )\r
281 {\r
282 xTIMER_MESSAGE xMessage;\r
283 portTickType xTimeToExpire;\r
284 xTIMER *pxTimer;\r
285 \r
286         while( xQueueReceive( xTimerQueue, &xMessage, tmrNO_DELAY ) != pdFAIL )\r
287         {\r
288                 pxTimer = xMessage.pxTimer;\r
289 \r
290                 /* Is the timer already in the list of active timers? */\r
291                 prvRemoveTimerFromActiveList( pxTimer );\r
292 \r
293                 switch( xMessage.xMessageID )\r
294                 {\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
300                                                                                 break;\r
301 \r
302                         default                 :                       /* Don't expect to get here. */\r
303                                                                                 break;\r
304                 }\r
305         }\r
306 }\r
307 /*-----------------------------------------------------------*/\r
308 \r
309 static void prvCheckForValidListAndQueue( void )\r
310 {\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
313         initialised. */\r
314         taskENTER_CRITICAL();\r
315         {\r
316                 if( xTimerQueue == NULL )\r
317                 {\r
318                         vListInitialise( &xActiveTimerList );\r
319                         xTimerQueue = xQueueCreate( configTIMER_QUEUE_LENGTH, sizeof( xTIMER_MESSAGE ) );\r
320                 }\r
321         }\r
322         taskEXIT_CRITICAL();\r
323 }\r
324 /*-----------------------------------------------------------*/\r
325 \r
326 void *pvTimerGetTimerID( xTimerHandle xTimer )\r
327 {\r
328 xTIMER *pxTimer = ( xTIMER * ) xTimer;\r
329 \r
330         return pxTimer->pvTimerID;\r
331 }\r
332 /*-----------------------------------------------------------*/\r
333 \r
334 static void prvRemoveTimerFromActiveList( xTIMER *pxTimer )\r
335 {\r
336         /* Is the timer already in the list of active timers? */\r
337         if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )\r
338         {\r
339                 /* The timer is in the list, remove it. */\r
340                 vListRemove( &( pxTimer->xTimerListItem ) );\r
341         }\r
342 }\r
343 /*-----------------------------------------------------------*/\r
344 \r
345 static portBASE_TYPE prvSendMessageToTimerServiceTask( xTIMER_MESSAGE *pxMessage, portTickType xBlockTime )\r
346 {\r
347 portBASE_TYPE xReturn;\r
348 \r
349         if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )\r
350         {\r
351                 xReturn = xQueueSendToBack( xTimerQueue, pxMessage, xBlockTime );\r
352         }\r
353         else\r
354         {\r
355                 xReturn = xQueueSendToBack( xTimerQueue, pxMessage, tmrNO_DELAY );\r
356         }\r
357 \r
358         return xReturn;\r
359 }\r
360 /*-----------------------------------------------------------*/\r
361 \r
362 \r
363 \r
364 \r
365 \r
366 \r
367 \r
368 portBASE_TYPE xTimerIsTimerActive( xTimerHandle xTimer )\r
369 {\r
370         return pdFALSE;\r
371 }\r
372 \r
373 void vTimerStop( xTimerHandle xTimer )\r
374 {\r
375 }\r
376 \r
377 \r
378 void vTimerChangePeriod( xTimerHandle xTimer )\r
379 {\r
380 }\r
381 \r
382 void vTimerDelete( xTimerHandle xTimer )\r
383 {\r
384 }\r
385 /*-----------------------------------------------------------*/\r