]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - os/7.4.0/src/os/timers.c
Remove the name of the platform from the OS folder name
[pes-rpp/rpp-lib.git] / os / 7.4.0 / src / os / timers.c
1 /*
2     FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.
3
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6
7     ***************************************************************************
8      *                                                                       *
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *
10      *    Complete, revised, and edited pdf reference manuals are also       *
11      *    available.                                                         *
12      *                                                                       *
13      *    Purchasing FreeRTOS documentation will not only help you, by       *
14      *    ensuring you get running as quickly as possible and with an        *
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *
16      *    the FreeRTOS project to continue with its mission of providing     *
17      *    professional grade, cross platform, de facto standard solutions    *
18      *    for microcontrollers - completely free of charge!                  *
19      *                                                                       *
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *
21      *                                                                       *
22      *    Thank you for using FreeRTOS, and thank you for your support!      *
23      *                                                                       *
24     ***************************************************************************
25
26
27     This file is part of the FreeRTOS distribution.
28
29     FreeRTOS is free software; you can redistribute it and/or modify it under
30     the terms of the GNU General Public License (version 2) as published by the
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
32
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
34     distribute a combined work that includes FreeRTOS without being obliged to
35     provide the source code for proprietary components outside of the FreeRTOS
36     kernel.
37
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
41     details. You should have received a copy of the GNU General Public License
42     and the FreeRTOS license exception along with FreeRTOS; if not itcan be
43     viewed here: http://www.freertos.org/a00114.html and also obtained by
44     writing to Real Time Engineers Ltd., contact details for whom are available
45     on the FreeRTOS WEB site.
46
47     1 tab == 4 spaces!
48
49     ***************************************************************************
50      *                                                                       *
51      *    Having a problem?  Start by reading the FAQ "My application does   *
52      *    not run, what could be wrong?"                                     *
53      *                                                                       *
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *
55      *                                                                       *
56     ***************************************************************************
57
58
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions,
60     license and Real Time Engineers Ltd. contact details.
61
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new
64     fully thread aware and reentrant UDP/IP stack.
65
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
67     Integrity Systems, who sell the code with commercial support,
68     indemnification and middleware, under the OpenRTOS brand.
69
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
71     engineered and independently SIL3 certified version for use in safety and
72     mission critical applications that require provable dependability.
73 */
74
75 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
76 all the API functions to use the MPU wrappers.  That should only be done when
77 task.h is included from an application file. */
78 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
79
80 #include "os/FreeRTOS.h"
81 #include "os/task.h"
82 #include "os/queue.h"
83 #include "os/timers.h"
84
85 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
86
87 /* This entire source file will be skipped if the application is not configured
88 to include software timer functionality.  This #if is closed at the very bottom
89 of this file.  If you want to include software timer functionality then ensure
90 configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */
91 #if ( configUSE_TIMERS == 1 )
92
93 /* Misc definitions. */
94 #define tmrNO_DELAY     ( portTickType ) 0U
95
96 /* The definition of the timers themselves. */
97 typedef struct tmrTimerControl
98 {
99     const signed char       *pcTimerName;       /*<< Text name.  This is not used by the kernel, it is included simply to make debugging easier. */
100     xListItem               xTimerListItem;     /*<< Standard linked list item as used by all kernel features for event management. */
101     portTickType            xTimerPeriodInTicks;/*<< How quickly and often the timer expires. */
102     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. */
103     void                    *pvTimerID;         /*<< An ID to identify the timer.  This allows the timer to be identified when the same callback is used for multiple timers. */
104     tmrTIMER_CALLBACK       pxCallbackFunction; /*<< The function that will be called when the timer expires. */
105 } xTIMER;
106
107 /* The definition of messages that can be sent and received on the timer
108 queue. */
109 typedef struct tmrTimerQueueMessage
110 {
111     portBASE_TYPE           xMessageID;         /*<< The command being sent to the timer service task. */
112     portTickType            xMessageValue;      /*<< An optional value used by a subset of commands, for example, when changing the period of a timer. */
113     xTIMER *                pxTimer;            /*<< The timer to which the command will be applied. */
114 } xTIMER_MESSAGE;
115
116 #pragma SET_DATA_SECTION(".kernelBSS")
117
118 /* The list in which active timers are stored.  Timers are referenced in expire
119 time order, with the nearest expiry time at the front of the list.  Only the
120 timer service task is allowed to access xActiveTimerList. */
121 PRIVILEGED_DATA static xList xActiveTimerList1;
122 PRIVILEGED_DATA static xList xActiveTimerList2;
123 PRIVILEGED_DATA static xList *pxCurrentTimerList;
124 PRIVILEGED_DATA static xList *pxOverflowTimerList;
125
126 /* A queue that is used to send commands to the timer service task. */
127 PRIVILEGED_DATA static xQueueHandle xTimerQueue = NULL;
128
129 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )
130
131     PRIVILEGED_DATA static xTaskHandle xTimerTaskHandle = NULL;
132
133 #endif
134
135 #pragma SET_DATA_SECTION()
136
137 /*-----------------------------------------------------------*/
138
139 /*
140  * Initialise the infrastructure used by the timer service task if it has not
141  * been initialised already.
142  */
143 static void prvCheckForValidListAndQueue( void ) PRIVILEGED_FUNCTION;
144
145 /*
146  * The timer service task (daemon).  Timer functionality is controlled by this
147  * task.  Other tasks communicate with the timer service task using the
148  * xTimerQueue queue.
149  */
150 static void prvTimerTask( void *pvParameters ) PRIVILEGED_FUNCTION;
151
152 /*
153  * Called by the timer service task to interpret and process a command it
154  * received on the timer queue.
155  */
156 static void prvProcessReceivedCommands( void ) PRIVILEGED_FUNCTION;
157
158 /*
159  * Insert the timer into either xActiveTimerList1, or xActiveTimerList2,
160  * depending on if the expire time causes a timer counter overflow.
161  */
162 static portBASE_TYPE prvInsertTimerInActiveList( xTIMER *pxTimer, portTickType xNextExpiryTime, portTickType xTimeNow, portTickType xCommandTime ) PRIVILEGED_FUNCTION;
163
164 /*
165  * An active timer has reached its expire time.  Reload the timer if it is an
166  * auto reload timer, then call its callback.
167  */
168 static void prvProcessExpiredTimer( portTickType xNextExpireTime, portTickType xTimeNow ) PRIVILEGED_FUNCTION;
169
170 /*
171  * The tick count has overflowed.  Switch the timer lists after ensuring the
172  * current timer list does not still reference some timers.
173  */
174 static void prvSwitchTimerLists( portTickType xLastTime ) PRIVILEGED_FUNCTION;
175
176 /*
177  * Obtain the current tick count, setting *pxTimerListsWereSwitched to pdTRUE
178  * if a tick count overflow occurred since prvSampleTimeNow() was last called.
179  */
180 static portTickType prvSampleTimeNow( portBASE_TYPE *pxTimerListsWereSwitched ) PRIVILEGED_FUNCTION;
181
182 /*
183  * If the timer list contains any active timers then return the expire time of
184  * the timer that will expire first and set *pxListWasEmpty to false.  If the
185  * timer list does not contain any timers then return 0 and set *pxListWasEmpty
186  * to pdTRUE.
187  */
188 static portTickType prvGetNextExpireTime( portBASE_TYPE *pxListWasEmpty ) PRIVILEGED_FUNCTION;
189
190 /*
191  * If a timer has expired, process it.  Otherwise, block the timer service task
192  * until either a timer does expire or a command is received.
193  */
194 static void prvProcessTimerOrBlockTask( portTickType xNextExpireTime, portBASE_TYPE xListWasEmpty ) PRIVILEGED_FUNCTION;
195
196 /*-----------------------------------------------------------*/
197
198 #pragma SET_CODE_SECTION(".kernelTEXT")
199
200 portBASE_TYPE xTimerCreateTimerTask( void )
201 {
202 portBASE_TYPE xReturn = pdFAIL;
203
204     /* This function is called when the scheduler is started if
205     configUSE_TIMERS is set to 1.  Check that the infrastructure used by the
206     timer service task has been created/initialised.  If timers have already
207     been created then the initialisation will already have been performed. */
208     prvCheckForValidListAndQueue();
209
210     if( xTimerQueue != NULL )
211     {
212         #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )
213         {
214             /* Create the timer task, storing its handle in xTimerTaskHandle so
215             it can be returned by the xTimerGetTimerDaemonTaskHandle() function. */
216             xReturn = xTaskCreate( prvTimerTask, ( const signed char * ) "Tmr Svc", ( unsigned short ) configTIMER_TASK_STACK_DEPTH, NULL, ( ( unsigned portBASE_TYPE ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT, &xTimerTaskHandle );
217         }
218         #else
219         {
220             /* Create the timer task without storing its handle. */
221             xReturn = xTaskCreate( prvTimerTask, ( const signed char * ) "Tmr Svc", ( unsigned short ) configTIMER_TASK_STACK_DEPTH, NULL, ( ( unsigned portBASE_TYPE ) configTIMER_TASK_PRIORITY ) | portPRIVILEGE_BIT, NULL);
222         }
223         #endif
224     }
225
226     configASSERT( xReturn );
227     return xReturn;
228 }
229 /*-----------------------------------------------------------*/
230
231 xTimerHandle xTimerCreate( const signed char * const pcTimerName, portTickType xTimerPeriodInTicks, unsigned portBASE_TYPE uxAutoReload, void *pvTimerID, tmrTIMER_CALLBACK pxCallbackFunction )
232 {
233 xTIMER *pxNewTimer;
234
235     /* Allocate the timer structure. */
236     if( xTimerPeriodInTicks == ( portTickType ) 0U )
237     {
238         pxNewTimer = NULL;
239         configASSERT( ( xTimerPeriodInTicks > 0 ) );
240     }
241     else
242     {
243         pxNewTimer = ( xTIMER * ) pvPortMalloc( sizeof( xTIMER ) );
244         if( pxNewTimer != NULL )
245         {
246             /* Ensure the infrastructure used by the timer service task has been
247             created/initialised. */
248             prvCheckForValidListAndQueue();
249
250             /* Initialise the timer structure members using the function parameters. */
251             pxNewTimer->pcTimerName = pcTimerName;
252             pxNewTimer->xTimerPeriodInTicks = xTimerPeriodInTicks;
253             pxNewTimer->uxAutoReload = uxAutoReload;
254             pxNewTimer->pvTimerID = pvTimerID;
255             pxNewTimer->pxCallbackFunction = pxCallbackFunction;
256             vListInitialiseItem( &( pxNewTimer->xTimerListItem ) );
257
258             traceTIMER_CREATE( pxNewTimer );
259         }
260         else
261         {
262             traceTIMER_CREATE_FAILED();
263         }
264     }
265
266     return ( xTimerHandle ) pxNewTimer;
267 }
268 /*-----------------------------------------------------------*/
269
270 portBASE_TYPE xTimerGenericCommand( xTimerHandle xTimer, portBASE_TYPE xCommandID, portTickType xOptionalValue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portTickType xBlockTime )
271 {
272 portBASE_TYPE xReturn = pdFAIL;
273 xTIMER_MESSAGE xMessage;
274
275     /* Send a message to the timer service task to perform a particular action
276     on a particular timer definition. */
277     if( xTimerQueue != NULL )
278     {
279         /* Send a command to the timer service task to start the xTimer timer. */
280         xMessage.xMessageID = xCommandID;
281         xMessage.xMessageValue = xOptionalValue;
282         xMessage.pxTimer = ( xTIMER * ) xTimer;
283
284         if( pxHigherPriorityTaskWoken == NULL )
285         {
286             if( xTaskGetSchedulerState() == taskSCHEDULER_RUNNING )
287             {
288                 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, xBlockTime );
289             }
290             else
291             {
292                 xReturn = xQueueSendToBack( xTimerQueue, &xMessage, tmrNO_DELAY );
293             }
294         }
295         else
296         {
297             xReturn = xQueueSendToBackFromISR( xTimerQueue, &xMessage, pxHigherPriorityTaskWoken );
298         }
299
300         traceTIMER_COMMAND_SEND( xTimer, xCommandID, xOptionalValue, xReturn );
301     }
302
303     return xReturn;
304 }
305
306 #pragma SET_CODE_SECTION()
307
308 /*-----------------------------------------------------------*/
309
310 #if ( INCLUDE_xTimerGetTimerDaemonTaskHandle == 1 )
311
312     xTaskHandle xTimerGetTimerDaemonTaskHandle( void )
313     {
314         /* If xTimerGetTimerDaemonTaskHandle() is called before the scheduler has been
315         started, then xTimerTaskHandle will be NULL. */
316         configASSERT( ( xTimerTaskHandle != NULL ) );
317         return xTimerTaskHandle;
318     }
319
320 #endif
321 /*-----------------------------------------------------------*/
322
323 #pragma SET_CODE_SECTION(".kernelTEXT")
324
325 static void prvProcessExpiredTimer( portTickType xNextExpireTime, portTickType xTimeNow )
326 {
327 xTIMER *pxTimer;
328 portBASE_TYPE xResult;
329
330     /* Remove the timer from the list of active timers.  A check has already
331     been performed to ensure the list is not empty. */
332     pxTimer = ( xTIMER * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );
333     uxListRemove( &( pxTimer->xTimerListItem ) );
334     traceTIMER_EXPIRED( pxTimer );
335
336     /* If the timer is an auto reload timer then calculate the next
337     expiry time and re-insert the timer in the list of active timers. */
338     if( pxTimer->uxAutoReload == ( unsigned portBASE_TYPE ) pdTRUE )
339     {
340         /* This is the only time a timer is inserted into a list using
341         a time relative to anything other than the current time.  It
342         will therefore be inserted into the correct list relative to
343         the time this task thinks it is now, even if a command to
344         switch lists due to a tick count overflow is already waiting in
345         the timer queue. */
346         if( prvInsertTimerInActiveList( pxTimer, ( xNextExpireTime + pxTimer->xTimerPeriodInTicks ), xTimeNow, xNextExpireTime ) == pdTRUE )
347         {
348             /* The timer expired before it was added to the active timer
349             list.  Reload it now.  */
350             xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START, xNextExpireTime, NULL, tmrNO_DELAY );
351             configASSERT( xResult );
352             ( void ) xResult;
353         }
354     }
355
356     /* Call the timer callback. */
357     pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );
358 }
359 /*-----------------------------------------------------------*/
360
361 #pragma TASK( prvTimerTask )
362
363 static void prvTimerTask( void *pvParameters )
364 {
365 portTickType xNextExpireTime;
366 portBASE_TYPE xListWasEmpty;
367
368     /* Just to avoid compiler warnings. */
369     ( void ) pvParameters;
370
371     for( ;; )
372     {
373         /* Query the timers list to see if it contains any timers, and if so,
374         obtain the time at which the next timer will expire. */
375         xNextExpireTime = prvGetNextExpireTime( &xListWasEmpty );
376
377         /* If a timer has expired, process it.  Otherwise, block this task
378         until either a timer does expire, or a command is received. */
379         prvProcessTimerOrBlockTask( xNextExpireTime, xListWasEmpty );
380
381         /* Empty the command queue. */
382         prvProcessReceivedCommands();
383     }
384 }
385 /*-----------------------------------------------------------*/
386
387 static void prvProcessTimerOrBlockTask( portTickType xNextExpireTime, portBASE_TYPE xListWasEmpty )
388 {
389 portTickType xTimeNow;
390 portBASE_TYPE xTimerListsWereSwitched;
391
392     vTaskSuspendAll();
393     {
394         /* Obtain the time now to make an assessment as to whether the timer
395         has expired or not.  If obtaining the time causes the lists to switch
396         then don't process this timer as any timers that remained in the list
397         when the lists were switched will have been processed within the
398         prvSampelTimeNow() function. */
399         xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );
400         if( xTimerListsWereSwitched == pdFALSE )
401         {
402             /* The tick count has not overflowed, has the timer expired? */
403             if( ( xListWasEmpty == pdFALSE ) && ( xNextExpireTime <= xTimeNow ) )
404             {
405                 xTaskResumeAll();
406                 prvProcessExpiredTimer( xNextExpireTime, xTimeNow );
407             }
408             else
409             {
410                 /* The tick count has not overflowed, and the next expire
411                 time has not been reached yet.  This task should therefore
412                 block to wait for the next expire time or a command to be
413                 received - whichever comes first.  The following line cannot
414                 be reached unless xNextExpireTime > xTimeNow, except in the
415                 case when the current timer list is empty. */
416                 vQueueWaitForMessageRestricted( xTimerQueue, ( xNextExpireTime - xTimeNow ) );
417
418                 if( xTaskResumeAll() == pdFALSE )
419                 {
420                     /* Yield to wait for either a command to arrive, or the block time
421                     to expire.  If a command arrived between the critical section being
422                     exited and this yield then the yield will not cause the task
423                     to block. */
424                     portYIELD_WITHIN_API();
425                 }
426             }
427         }
428         else
429         {
430             xTaskResumeAll();
431         }
432     }
433 }
434 /*-----------------------------------------------------------*/
435
436 static portTickType prvGetNextExpireTime( portBASE_TYPE *pxListWasEmpty )
437 {
438 portTickType xNextExpireTime;
439
440     /* Timers are listed in expiry time order, with the head of the list
441     referencing the task that will expire first.  Obtain the time at which
442     the timer with the nearest expiry time will expire.  If there are no
443     active timers then just set the next expire time to 0.  That will cause
444     this task to unblock when the tick count overflows, at which point the
445     timer lists will be switched and the next expiry time can be
446     re-assessed.  */
447     *pxListWasEmpty = listLIST_IS_EMPTY( pxCurrentTimerList );
448     if( *pxListWasEmpty == pdFALSE )
449     {
450         xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );
451     }
452     else
453     {
454         /* Ensure the task unblocks when the tick count rolls over. */
455         xNextExpireTime = ( portTickType ) 0U;
456     }
457
458     return xNextExpireTime;
459 }
460 /*-----------------------------------------------------------*/
461
462
463 #pragma SET_DATA_SECTION(".kernelBSS")
464
465 static portTickType prvSampleTimeNow( portBASE_TYPE *pxTimerListsWereSwitched )
466 {
467 portTickType xTimeNow;
468 PRIVILEGED_DATA static portTickType xLastTime = ( portTickType ) 0U;
469
470     xTimeNow = xTaskGetTickCount();
471
472     if( xTimeNow < xLastTime )
473     {
474         prvSwitchTimerLists( xLastTime );
475         *pxTimerListsWereSwitched = pdTRUE;
476     }
477     else
478     {
479         *pxTimerListsWereSwitched = pdFALSE;
480     }
481
482     xLastTime = xTimeNow;
483
484     return xTimeNow;
485 }
486
487 #pragma SET_DATA_SECTION()
488 /*-----------------------------------------------------------*/
489
490 static portBASE_TYPE prvInsertTimerInActiveList( xTIMER *pxTimer, portTickType xNextExpiryTime, portTickType xTimeNow, portTickType xCommandTime )
491 {
492 portBASE_TYPE xProcessTimerNow = pdFALSE;
493
494     listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xNextExpiryTime );
495     listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );
496
497     if( xNextExpiryTime <= xTimeNow )
498     {
499         /* Has the expiry time elapsed between the command to start/reset a
500         timer was issued, and the time the command was processed? */
501         if( ( ( portTickType ) ( xTimeNow - xCommandTime ) ) >= pxTimer->xTimerPeriodInTicks )
502         {
503             /* The time between a command being issued and the command being
504             processed actually exceeds the timers period.  */
505             xProcessTimerNow = pdTRUE;
506         }
507         else
508         {
509             vListInsert( pxOverflowTimerList, &( pxTimer->xTimerListItem ) );
510         }
511     }
512     else
513     {
514         if( ( xTimeNow < xCommandTime ) && ( xNextExpiryTime >= xCommandTime ) )
515         {
516             /* If, since the command was issued, the tick count has overflowed
517             but the expiry time has not, then the timer must have already passed
518             its expiry time and should be processed immediately. */
519             xProcessTimerNow = pdTRUE;
520         }
521         else
522         {
523             vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );
524         }
525     }
526
527     return xProcessTimerNow;
528 }
529 /*-----------------------------------------------------------*/
530
531 static void prvProcessReceivedCommands( void )
532 {
533 xTIMER_MESSAGE xMessage;
534 xTIMER *pxTimer;
535 portBASE_TYPE xTimerListsWereSwitched, xResult;
536 portTickType xTimeNow;
537
538     while( xQueueReceive( xTimerQueue, &xMessage, tmrNO_DELAY ) != pdFAIL )
539     {
540         pxTimer = xMessage.pxTimer;
541
542         if( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) == pdFALSE )
543         {
544             /* The timer is in a list, remove it. */
545             uxListRemove( &( pxTimer->xTimerListItem ) );
546         }
547
548         traceTIMER_COMMAND_RECEIVED( pxTimer, xMessage.xMessageID, xMessage.xMessageValue );
549
550         /* In this case the xTimerListsWereSwitched parameter is not used, but
551         it must be present in the function call.  prvSampleTimeNow() must be
552         called after the message is received from xTimerQueue so there is no
553         possibility of a higher priority task adding a message to the message
554         queue with a time that is ahead of the timer daemon task (because it
555         pre-empted the timer daemon task after the xTimeNow value was set). */
556         xTimeNow = prvSampleTimeNow( &xTimerListsWereSwitched );
557
558         switch( xMessage.xMessageID )
559         {
560             case tmrCOMMAND_START :
561                 /* Start or restart a timer. */
562                 if( prvInsertTimerInActiveList( pxTimer,  xMessage.xMessageValue + pxTimer->xTimerPeriodInTicks, xTimeNow, xMessage.xMessageValue ) == pdTRUE )
563                 {
564                     /* The timer expired before it was added to the active timer
565                     list.  Process it now. */
566                     pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );
567
568                     if( pxTimer->uxAutoReload == ( unsigned portBASE_TYPE ) pdTRUE )
569                     {
570                         xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START, xMessage.xMessageValue + pxTimer->xTimerPeriodInTicks, NULL, tmrNO_DELAY );
571                         configASSERT( xResult );
572                         ( void ) xResult;
573                     }
574                 }
575                 break;
576
577             case tmrCOMMAND_STOP :
578                 /* The timer has already been removed from the active list.
579                 There is nothing to do here. */
580                 break;
581
582             case tmrCOMMAND_CHANGE_PERIOD :
583                 pxTimer->xTimerPeriodInTicks = xMessage.xMessageValue;
584                 configASSERT( ( pxTimer->xTimerPeriodInTicks > 0 ) );
585                 prvInsertTimerInActiveList( pxTimer, ( xTimeNow + pxTimer->xTimerPeriodInTicks ), xTimeNow, xTimeNow );
586                 break;
587
588             case tmrCOMMAND_DELETE :
589                 /* The timer has already been removed from the active list,
590                 just free up the memory. */
591                 vPortFree( pxTimer );
592                 break;
593
594             default :
595                 /* Don't expect to get here. */
596                 break;
597         }
598     }
599 }
600 /*-----------------------------------------------------------*/
601
602 static void prvSwitchTimerLists( portTickType xLastTime )
603 {
604 portTickType xNextExpireTime, xReloadTime;
605 xList *pxTemp;
606 xTIMER *pxTimer;
607 portBASE_TYPE xResult;
608
609     /* Remove compiler warnings if configASSERT() is not defined. */
610     ( void ) xLastTime;
611
612     /* The tick count has overflowed.  The timer lists must be switched.
613     If there are any timers still referenced from the current timer list
614     then they must have expired and should be processed before the lists
615     are switched. */
616     while( listLIST_IS_EMPTY( pxCurrentTimerList ) == pdFALSE )
617     {
618         xNextExpireTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxCurrentTimerList );
619
620         /* Remove the timer from the list. */
621         pxTimer = ( xTIMER * ) listGET_OWNER_OF_HEAD_ENTRY( pxCurrentTimerList );
622         uxListRemove( &( pxTimer->xTimerListItem ) );
623
624         /* Execute its callback, then send a command to restart the timer if
625         it is an auto-reload timer.  It cannot be restarted here as the lists
626         have not yet been switched. */
627         pxTimer->pxCallbackFunction( ( xTimerHandle ) pxTimer );
628
629         if( pxTimer->uxAutoReload == ( unsigned portBASE_TYPE ) pdTRUE )
630         {
631             /* Calculate the reload value, and if the reload value results in
632             the timer going into the same timer list then it has already expired
633             and the timer should be re-inserted into the current list so it is
634             processed again within this loop.  Otherwise a command should be sent
635             to restart the timer to ensure it is only inserted into a list after
636             the lists have been swapped. */
637             xReloadTime = ( xNextExpireTime + pxTimer->xTimerPeriodInTicks );
638             if( xReloadTime > xNextExpireTime )
639             {
640                 listSET_LIST_ITEM_VALUE( &( pxTimer->xTimerListItem ), xReloadTime );
641                 listSET_LIST_ITEM_OWNER( &( pxTimer->xTimerListItem ), pxTimer );
642                 vListInsert( pxCurrentTimerList, &( pxTimer->xTimerListItem ) );
643             }
644             else
645             {
646                 xResult = xTimerGenericCommand( pxTimer, tmrCOMMAND_START, xNextExpireTime, NULL, tmrNO_DELAY );
647                 configASSERT( xResult );
648                 ( void ) xResult;
649             }
650         }
651     }
652
653     pxTemp = pxCurrentTimerList;
654     pxCurrentTimerList = pxOverflowTimerList;
655     pxOverflowTimerList = pxTemp;
656 }
657 /*-----------------------------------------------------------*/
658
659 static void prvCheckForValidListAndQueue( void )
660 {
661     /* Check that the list from which active timers are referenced, and the
662     queue used to communicate with the timer service, have been
663     initialised. */
664     taskENTER_CRITICAL();
665     {
666         if( xTimerQueue == NULL )
667         {
668             vListInitialise( &xActiveTimerList1 );
669             vListInitialise( &xActiveTimerList2 );
670             pxCurrentTimerList = &xActiveTimerList1;
671             pxOverflowTimerList = &xActiveTimerList2;
672             xTimerQueue = xQueueCreate( ( unsigned portBASE_TYPE ) configTIMER_QUEUE_LENGTH, sizeof( xTIMER_MESSAGE ) );
673         }
674     }
675     taskEXIT_CRITICAL();
676 }
677 /*-----------------------------------------------------------*/
678
679 portBASE_TYPE xTimerIsTimerActive( xTimerHandle xTimer )
680 {
681 portBASE_TYPE xTimerIsInActiveList;
682 xTIMER *pxTimer = ( xTIMER * ) xTimer;
683
684     /* Is the timer in the list of active timers? */
685     taskENTER_CRITICAL();
686     {
687         /* Checking to see if it is in the NULL list in effect checks to see if
688         it is referenced from either the current or the overflow timer lists in
689         one go, but the logic has to be reversed, hence the '!'. */
690         xTimerIsInActiveList = !( listIS_CONTAINED_WITHIN( NULL, &( pxTimer->xTimerListItem ) ) );
691     }
692     taskEXIT_CRITICAL();
693
694     return xTimerIsInActiveList;
695 }
696 /*-----------------------------------------------------------*/
697
698 void *pvTimerGetTimerID( xTimerHandle xTimer )
699 {
700 xTIMER *pxTimer = ( xTIMER * ) xTimer;
701
702     return pxTimer->pvTimerID;
703 }
704 /*-----------------------------------------------------------*/
705
706 /* This entire source file will be skipped if the application is not configured
707 to include software timer functionality.  If you want to include software timer
708 functionality then ensure configUSE_TIMERS is set to 1 in FreeRTOSConfig.h. */
709 #endif /* configUSE_TIMERS == 1 */
710
711
712
713