changing circuitry to disable RTC, update initialization to match
[fw/openalt] / FreeRTOS / croutine.c
1 /*
2         FreeRTOS.org V4.4.0 - Copyright (C) 2003-2007 Richard Barry.
3
4         This file is part of the FreeRTOS.org distribution.
5
6         FreeRTOS.org is free software; you can redistribute it and/or modify
7         it under the terms of the GNU General Public License as published by
8         the Free Software Foundation; either version 2 of the License, or
9         (at your option) any later version.
10
11         FreeRTOS.org is distributed in the hope that it will be useful,
12         but WITHOUT ANY WARRANTY; without even the implied warranty of
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14         GNU General Public License for more details.
15
16         You should have received a copy of the GNU General Public License
17         along with FreeRTOS.org; if not, write to the Free Software
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20         A special exception to the GPL can be applied should you wish to distribute
21         a combined work that includes FreeRTOS.org, without being obliged to provide
22         the source code for any proprietary components.  See the licensing section
23         of http://www.FreeRTOS.org for full details of how and when the exception
24         can be applied.
25
26         ***************************************************************************
27         See http://www.FreeRTOS.org for documentation, latest information, license
28         and contact details.  Please ensure to read the configuration and relevant
29         port sections of the online documentation.
30
31         Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along
32         with commercial development and support options.
33         ***************************************************************************
34 */
35
36 #include "FreeRTOS.h"
37 #include "task.h"
38 #include "croutine.h"
39
40 /* Lists for ready and blocked co-routines. --------------------*/
41 static xList pxReadyCoRoutineLists[ configMAX_CO_ROUTINE_PRIORITIES ];  /*< Prioritised ready co-routines. */
42 static xList xDelayedCoRoutineList1;                                                                    /*< Delayed co-routines. */
43 static xList xDelayedCoRoutineList2;                                                                    /*< Delayed co-routines (two lists are used - one for delays that have overflowed the current tick count. */
44 static xList * pxDelayedCoRoutineList;                                                                  /*< Points to the delayed co-routine list currently being used. */
45 static xList * pxOverflowDelayedCoRoutineList;                                                  /*< Points to the delayed co-routine list currently being used to hold co-routines that have overflowed the current tick count. */
46 static xList xPendingReadyList;                                                                                 /*< Holds co-routines that have been readied by an external event.  They cannot be added directly to the ready lists as the ready lists cannot be accessed by interrupts. */
47
48 /* Other file private variables. --------------------------------*/
49 corCRCB * pxCurrentCoRoutine = NULL;
50 static unsigned portBASE_TYPE uxTopCoRoutineReadyPriority = 0;
51 static portTickType xCoRoutineTickCount = 0;
52
53 /* The initial state of the co-routine when it is created. */
54 #define corINITIAL_STATE        ( 0 )
55
56 /*
57  * Place the co-routine represented by pxCRCB into the appropriate ready queue
58  * for the priority.  It is inserted at the end of the list.
59  *
60  * This macro accesses the co-routine ready lists and therefore must not be
61  * used from within an ISR.
62  */
63 #define prvAddCoRoutineToReadyQueue( pxCRCB )                                                                                                                                           \
64 {                                                                                                                                                                                                                                       \
65         if( pxCRCB->uxPriority > uxTopCoRoutineReadyPriority )                                                                                                                  \
66         {                                                                                                                                                                                                                               \
67                 uxTopCoRoutineReadyPriority = pxCRCB->uxPriority;                                                                                                                       \
68         }                                                                                                                                                                                                                               \
69         vListInsertEnd( ( xList * ) &( pxReadyCoRoutineLists[ pxCRCB->uxPriority ] ), &( pxCRCB->xGenericListItem ) );  \
70 }       
71
72 /*
73  * Utility to ready all the lists used by the scheduler.  This is called
74  * automatically upon the creation of the first co-routine.
75  */
76 static void prvInitialiseCoRoutineLists( void );
77
78 /*
79  * Co-routines that are readied by an interrupt cannot be placed directly into
80  * the ready lists (there is no mutual exclusion).  Instead they are placed in
81  * in the pending ready list in order that they can later be moved to the ready
82  * list by the co-routine scheduler.
83  */
84 static inline void prvCheckPendingReadyList( void );
85
86 /*
87  * Macro that looks at the list of co-routines that are currently delayed to
88  * see if any require waking.
89  *
90  * Co-routines are stored in the queue in the order of their wake time -
91  * meaning once one co-routine has been found whose timer has not expired
92  * we need not look any further down the list.
93  */
94 static inline void prvCheckDelayedList( void );
95
96 /*-----------------------------------------------------------*/
97
98 signed portBASE_TYPE xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, unsigned portBASE_TYPE uxPriority, unsigned portBASE_TYPE uxIndex )
99 {
100 signed portBASE_TYPE xReturn;
101 corCRCB *pxCoRoutine;
102
103         /* Allocate the memory that will store the co-routine control block. */
104         pxCoRoutine = ( corCRCB * ) pvPortMalloc( sizeof( corCRCB ) );
105         if( pxCoRoutine )
106         {
107                 /* If pxCurrentCoRoutine is NULL then this is the first co-routine to
108                 be created and the co-routine data structures need initialising. */
109                 if( pxCurrentCoRoutine == NULL )
110                 {
111                         pxCurrentCoRoutine = pxCoRoutine;
112                         prvInitialiseCoRoutineLists();
113                 }
114
115                 /* Check the priority is within limits. */
116                 if( uxPriority >= configMAX_CO_ROUTINE_PRIORITIES )
117                 {
118                         uxPriority = configMAX_CO_ROUTINE_PRIORITIES - 1;
119                 }
120
121                 /* Fill out the co-routine control block from the function parameters. */
122                 pxCoRoutine->uxState = corINITIAL_STATE;
123                 pxCoRoutine->uxPriority = uxPriority;
124                 pxCoRoutine->uxIndex = uxIndex;
125                 pxCoRoutine->pxCoRoutineFunction = pxCoRoutineCode;
126
127                 /* Initialise all the other co-routine control block parameters. */
128                 vListInitialiseItem( &( pxCoRoutine->xGenericListItem ) );
129                 vListInitialiseItem( &( pxCoRoutine->xEventListItem ) );
130
131                 /* Set the co-routine control block as a link back from the xListItem.
132                 This is so we can get back to the containing CRCB from a generic item
133                 in a list. */
134                 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xGenericListItem ), pxCoRoutine );
135                 listSET_LIST_ITEM_OWNER( &( pxCoRoutine->xEventListItem ), pxCoRoutine );
136         
137                 /* Event lists are always in priority order. */
138                 listSET_LIST_ITEM_VALUE( &( pxCoRoutine->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );
139                 
140                 /* Now the co-routine has been initialised it can be added to the ready
141                 list at the correct priority. */
142                 prvAddCoRoutineToReadyQueue( pxCoRoutine );
143
144                 xReturn = pdPASS;
145         }
146         else
147         {               
148                 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
149         }
150         
151         return xReturn; 
152 }
153 /*-----------------------------------------------------------*/
154
155 void vCoRoutineAddToDelayedList( portTickType xTicksToDelay, xList *pxEventList )
156 {
157 portTickType xTimeToWake;
158
159         /* Calculate the time to wake - this may overflow but this is
160         not a problem. */
161         xTimeToWake = xCoRoutineTickCount + xTicksToDelay;
162
163         /* We must remove ourselves from the ready list before adding
164         ourselves to the blocked list as the same list item is used for
165         both lists. */
166         vListRemove( ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
167
168         /* The list item will be inserted in wake time order. */
169         listSET_LIST_ITEM_VALUE( &( pxCurrentCoRoutine->xGenericListItem ), xTimeToWake );
170
171         if( xTimeToWake < xCoRoutineTickCount )
172         {
173                 /* Wake time has overflowed.  Place this item in the
174                 overflow list. */
175                 vListInsert( ( xList * ) pxOverflowDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
176         }
177         else
178         {
179                 /* The wake time has not overflowed, so we can use the
180                 current block list. */
181                 vListInsert( ( xList * ) pxDelayedCoRoutineList, ( xListItem * ) &( pxCurrentCoRoutine->xGenericListItem ) );
182         }
183
184         if( pxEventList )
185         {
186                 /* Also add the co-routine to an event list.  If this is done then the
187                 function must be called with interrupts disabled. */
188                 vListInsert( pxEventList, &( pxCurrentCoRoutine->xEventListItem ) );
189         }
190 }
191 /*-----------------------------------------------------------*/
192
193 static inline void prvCheckPendingReadyList( void )
194 {
195         /* Are there any co-routines waiting to get moved to the ready list?  These
196         are co-routines that have been readied by an ISR.  The ISR cannot access
197         the     ready lists itself. */
198         while( !listLIST_IS_EMPTY( &xPendingReadyList ) )
199         {
200                 corCRCB *pxUnblockedCRCB;
201
202                 /* The pending ready list can be accessed by an ISR. */
203                 portDISABLE_INTERRUPTS();
204                 {       
205                         pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( (&xPendingReadyList) );                    
206                         vListRemove( &( pxUnblockedCRCB->xEventListItem ) );
207                 }
208                 portENABLE_INTERRUPTS();
209
210                 vListRemove( &( pxUnblockedCRCB->xGenericListItem ) );
211                 prvAddCoRoutineToReadyQueue( pxUnblockedCRCB ); 
212         }
213 }
214 /*-----------------------------------------------------------*/
215
216 static inline void prvCheckDelayedList( void )
217 {
218 static portTickType xLastTickCount, xPassedTicks;
219 corCRCB *pxCRCB;
220
221         xPassedTicks = xTaskGetTickCount() - xLastTickCount;
222         while( xPassedTicks )
223         {
224                 xCoRoutineTickCount++;
225                 xPassedTicks--;
226
227                 /* If the tick count has overflowed we need to swap the ready lists. */
228                 if( xCoRoutineTickCount == 0 )
229                 {
230                         xList * pxTemp;
231
232                         /* Tick count has overflowed so we need to swap the delay lists.  If there are
233                         any items in pxDelayedCoRoutineList here then there is an error! */
234                         pxTemp = pxDelayedCoRoutineList;
235                         pxDelayedCoRoutineList = pxOverflowDelayedCoRoutineList;
236                         pxOverflowDelayedCoRoutineList = pxTemp;
237                 }
238
239                 /* See if this tick has made a timeout expire. */
240                 while( ( pxCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedCoRoutineList ) ) != NULL )
241                 {       
242                         if( xCoRoutineTickCount < listGET_LIST_ITEM_VALUE( &( pxCRCB->xGenericListItem ) ) )                            
243                         {                       
244                                 /* Timeout not yet expired. */                                                                                                                                                  
245                                 break;                                                                                                                                                          
246                         }                                                                                                                                                                               
247
248                         portDISABLE_INTERRUPTS();
249                         {
250                                 /* The event could have occurred just before this critical
251                                 section.  If this is the case then the generic list item will
252                                 have been moved to the pending ready list and the following
253                                 line is still valid.  Also the pvContainer parameter will have
254                                 been set to NULL so the following lines are also valid. */
255                                 vListRemove( &( pxCRCB->xGenericListItem ) );                                                                                   
256
257                                 /* Is the co-routine waiting on an event also? */                                                                                               
258                                 if( pxCRCB->xEventListItem.pvContainer )                                                                                                        
259                                 {                                                                                                                       
260                                         vListRemove( &( pxCRCB->xEventListItem ) );                                                                                     
261                                 }
262                         }
263                         portENABLE_INTERRUPTS();
264
265                         prvAddCoRoutineToReadyQueue( pxCRCB );                                                                                                  
266                 }                                                                                                                                                                                                       
267         }
268
269         xLastTickCount = xCoRoutineTickCount;
270 }
271 /*-----------------------------------------------------------*/
272
273 void vCoRoutineSchedule( void )
274 {
275         /* See if any co-routines readied by events need moving to the ready lists. */
276         prvCheckPendingReadyList();
277
278         /* See if any delayed co-routines have timed out. */
279         prvCheckDelayedList();
280
281         /* Find the highest priority queue that contains ready co-routines. */
282         while( listLIST_IS_EMPTY( &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) ) )
283         {
284                 if( uxTopCoRoutineReadyPriority == 0 )
285                 {
286                         /* No more co-routines to check. */
287                         return;
288                 }
289                 --uxTopCoRoutineReadyPriority;
290         }
291
292         /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the co-routines
293          of the same priority get an equal share of the processor time. */
294         listGET_OWNER_OF_NEXT_ENTRY( pxCurrentCoRoutine, &( pxReadyCoRoutineLists[ uxTopCoRoutineReadyPriority ] ) );
295
296         /* Call the co-routine. */
297         ( pxCurrentCoRoutine->pxCoRoutineFunction )( pxCurrentCoRoutine, pxCurrentCoRoutine->uxIndex );
298
299         return;
300 }
301 /*-----------------------------------------------------------*/
302
303 static void prvInitialiseCoRoutineLists( void )
304 {
305 unsigned portBASE_TYPE uxPriority;
306
307         for( uxPriority = 0; uxPriority < configMAX_CO_ROUTINE_PRIORITIES; uxPriority++ )
308         {
309                 vListInitialise( ( xList * ) &( pxReadyCoRoutineLists[ uxPriority ] ) );
310         }
311
312         vListInitialise( ( xList * ) &xDelayedCoRoutineList1 );
313         vListInitialise( ( xList * ) &xDelayedCoRoutineList2 );
314         vListInitialise( ( xList * ) &xPendingReadyList );
315
316         /* Start with pxDelayedCoRoutineList using list1 and the
317         pxOverflowDelayedCoRoutineList using list2. */
318         pxDelayedCoRoutineList = &xDelayedCoRoutineList1;
319         pxOverflowDelayedCoRoutineList = &xDelayedCoRoutineList2;
320 }
321 /*-----------------------------------------------------------*/
322
323 signed portBASE_TYPE xCoRoutineRemoveFromEventList( const xList *pxEventList )
324 {
325 corCRCB *pxUnblockedCRCB;
326 signed portBASE_TYPE xReturn;
327
328         /* This function is called from within an interrupt.  It can only access
329         event lists and the pending ready list. */
330         pxUnblockedCRCB = ( corCRCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
331         vListRemove( &( pxUnblockedCRCB->xEventListItem ) );
332         vListInsertEnd( ( xList * ) &( xPendingReadyList ), &( pxUnblockedCRCB->xEventListItem ) );
333
334         if( pxUnblockedCRCB->uxPriority >= pxCurrentCoRoutine->uxPriority )
335         {
336                 xReturn = pdTRUE;
337         }
338         else
339         {
340                 xReturn = pdFALSE;
341         }
342
343         return xReturn;
344 }
345