]> git.sur5r.net Git - freertos/blob - Source/tasks.c
Update headers for Version 7.0.0 release.
[freertos] / Source / tasks.c
1 /*\r
2     FreeRTOS V7.0.0 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3         \r
4 \r
5         FreeRTOS supports many tools and architectures. V7.0.0 is sponsored by:\r
6         Atollic AB - Atollic provides professional embedded systems development \r
7         tools for C/C++ development, code analysis and test automation.  \r
8         See http://www.atollic.com\r
9         \r
10 \r
11     ***************************************************************************\r
12      *                                                                       *\r
13      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
14      *    Complete, revised, and edited pdf reference manuals are also       *\r
15      *    available.                                                         *\r
16      *                                                                       *\r
17      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
18      *    ensuring you get running as quickly as possible and with an        *\r
19      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
20      *    the FreeRTOS project to continue with its mission of providing     *\r
21      *    professional grade, cross platform, de facto standard solutions    *\r
22      *    for microcontrollers - completely free of charge!                  *\r
23      *                                                                       *\r
24      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
25      *                                                                       *\r
26      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
27      *                                                                       *\r
28     ***************************************************************************\r
29 \r
30 \r
31     This file is part of the FreeRTOS distribution.\r
32 \r
33     FreeRTOS is free software; you can redistribute it and/or modify it under\r
34     the terms of the GNU General Public License (version 2) as published by the\r
35     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
36     >>>NOTE<<< The modification to the GPL is included to allow you to\r
37     distribute a combined work that includes FreeRTOS without being obliged to\r
38     provide the source code for proprietary components outside of the FreeRTOS\r
39     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
40     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
41     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
42     more details. You should have received a copy of the GNU General Public\r
43     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
44     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
45     by writing to Richard Barry, contact details for whom are available on the\r
46     FreeRTOS WEB site.\r
47 \r
48     1 tab == 4 spaces!\r
49 \r
50     http://www.FreeRTOS.org - Documentation, latest information, license and\r
51     contact details.\r
52 \r
53     http://www.SafeRTOS.com - A version that is certified for use in safety\r
54     critical systems.\r
55 \r
56     http://www.OpenRTOS.com - Commercial support, development, porting,\r
57     licensing and training services.\r
58 */\r
59 \r
60 \r
61 #include <stdio.h>\r
62 #include <stdlib.h>\r
63 #include <string.h>\r
64 \r
65 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
66 all the API functions to use the MPU wrappers.  That should only be done when\r
67 task.h is included from an application file. */\r
68 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
69 \r
70 #include "FreeRTOS.h"\r
71 #include "task.h"\r
72 #include "timers.h"\r
73 #include "StackMacros.h"\r
74 \r
75 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
76 \r
77 /*\r
78  * Macro to define the amount of stack available to the idle task.\r
79  */\r
80 #define tskIDLE_STACK_SIZE      configMINIMAL_STACK_SIZE\r
81 \r
82 /*\r
83  * Task control block.  A task control block (TCB) is allocated to each task,\r
84  * and stores the context of the task.\r
85  */\r
86 typedef struct tskTaskControlBlock\r
87 {\r
88         volatile portSTACK_TYPE *pxTopOfStack;          /*< Points to the location of the last item placed on the tasks stack.  THIS MUST BE THE FIRST MEMBER OF THE STRUCT. */\r
89 \r
90         #if ( portUSING_MPU_WRAPPERS == 1 )\r
91                 xMPU_SETTINGS xMPUSettings;                             /*< The MPU settings are defined as part of the port layer.  THIS MUST BE THE SECOND MEMBER OF THE STRUCT. */\r
92         #endif  \r
93         \r
94         xListItem                               xGenericListItem;       /*< List item used to place the TCB in ready and blocked queues. */\r
95         xListItem                               xEventListItem;         /*< List item used to place the TCB in event lists. */\r
96         unsigned portBASE_TYPE  uxPriority;                     /*< The priority of the task where 0 is the lowest priority. */\r
97         portSTACK_TYPE                  *pxStack;                       /*< Points to the start of the stack. */\r
98         signed char                             pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created.  Facilitates debugging only. */\r
99 \r
100         #if ( portSTACK_GROWTH > 0 )\r
101                 portSTACK_TYPE *pxEndOfStack;                   /*< Used for stack overflow checking on architectures where the stack grows up from low memory. */\r
102         #endif\r
103 \r
104         #if ( portCRITICAL_NESTING_IN_TCB == 1 )\r
105                 unsigned portBASE_TYPE uxCriticalNesting;\r
106         #endif\r
107 \r
108         #if ( configUSE_TRACE_FACILITY == 1 )\r
109                 unsigned portBASE_TYPE  uxTCBNumber;    /*< This is used for tracing the scheduler and making debugging easier only. */\r
110         #endif\r
111 \r
112         #if ( configUSE_MUTEXES == 1 )\r
113                 unsigned portBASE_TYPE uxBasePriority;  /*< The priority last assigned to the task - used by the priority inheritance mechanism. */\r
114         #endif\r
115 \r
116         #if ( configUSE_APPLICATION_TASK_TAG == 1 )\r
117                 pdTASK_HOOK_CODE pxTaskTag;\r
118         #endif\r
119 \r
120         #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
121                 unsigned long ulRunTimeCounter;         /*< Used for calculating how much CPU time each task is utilising. */\r
122         #endif\r
123 \r
124 } tskTCB;\r
125 \r
126 \r
127 /*\r
128  * Some kernel aware debuggers require data to be viewed to be global, rather\r
129  * than file scope.\r
130  */\r
131 #ifdef portREMOVE_STATIC_QUALIFIER\r
132         #define static\r
133 #endif\r
134 \r
135 /*lint -e956 */\r
136 PRIVILEGED_DATA tskTCB * volatile pxCurrentTCB = NULL;\r
137 \r
138 /* Lists for ready and blocked tasks. --------------------*/\r
139 \r
140 PRIVILEGED_DATA static xList pxReadyTasksLists[ configMAX_PRIORITIES ]; /*< Prioritised ready tasks. */\r
141 PRIVILEGED_DATA static xList xDelayedTaskList1;                                                 /*< Delayed tasks. */\r
142 PRIVILEGED_DATA static xList xDelayedTaskList2;                                                 /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */\r
143 PRIVILEGED_DATA static xList * volatile pxDelayedTaskList ;                             /*< Points to the delayed task list currently being used. */\r
144 PRIVILEGED_DATA static xList * volatile pxOverflowDelayedTaskList;              /*< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */\r
145 PRIVILEGED_DATA static xList xPendingReadyList;                                                 /*< Tasks that have been readied while the scheduler was suspended.  They will be moved to the ready queue when the scheduler is resumed. */\r
146 \r
147 #if ( INCLUDE_vTaskDelete == 1 )\r
148 \r
149         PRIVILEGED_DATA static volatile xList xTasksWaitingTermination;         /*< Tasks that have been deleted - but the their memory not yet freed. */\r
150         PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxTasksDeleted = ( unsigned portBASE_TYPE ) 0;\r
151 \r
152 #endif\r
153 \r
154 #if ( INCLUDE_vTaskSuspend == 1 )\r
155 \r
156         PRIVILEGED_DATA static xList xSuspendedTaskList;                                        /*< Tasks that are currently suspended. */\r
157 \r
158 #endif\r
159 \r
160 /* File private variables. --------------------------------*/\r
161 PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxCurrentNumberOfTasks   = ( unsigned portBASE_TYPE ) 0;\r
162 PRIVILEGED_DATA static volatile portTickType xTickCount                                                 = ( portTickType ) 0;\r
163 PRIVILEGED_DATA static unsigned portBASE_TYPE uxTopUsedPriority                                 = tskIDLE_PRIORITY;\r
164 PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxTopReadyPriority               = tskIDLE_PRIORITY;\r
165 PRIVILEGED_DATA static volatile signed portBASE_TYPE xSchedulerRunning                  = pdFALSE;\r
166 PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxSchedulerSuspended             = ( unsigned portBASE_TYPE ) pdFALSE;\r
167 PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxMissedTicks                    = ( unsigned portBASE_TYPE ) 0;\r
168 PRIVILEGED_DATA static volatile portBASE_TYPE xMissedYield                                              = ( portBASE_TYPE ) pdFALSE;\r
169 PRIVILEGED_DATA static volatile portBASE_TYPE xNumOfOverflows                                   = ( portBASE_TYPE ) 0;\r
170 PRIVILEGED_DATA static unsigned portBASE_TYPE uxTaskNumber                                              = ( unsigned portBASE_TYPE ) 0;\r
171 PRIVILEGED_DATA static portTickType xNextTaskUnblockTime                                                = ( portTickType ) portMAX_DELAY;\r
172 \r
173 #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
174 \r
175         PRIVILEGED_DATA static char pcStatsString[ 50 ] ;\r
176         PRIVILEGED_DATA static unsigned long ulTaskSwitchedInTime = 0UL;        /*< Holds the value of a timer/counter the last time a task was switched in. */\r
177         static void prvGenerateRunTimeStatsForTasksInList( const signed char *pcWriteBuffer, xList *pxList, unsigned long ulTotalRunTime ) PRIVILEGED_FUNCTION;\r
178 \r
179 #endif\r
180 \r
181 /* Debugging and trace facilities private variables and macros. ------------*/\r
182 \r
183 /*\r
184  * The value used to fill the stack of a task when the task is created.  This\r
185  * is used purely for checking the high water mark for tasks.\r
186  */\r
187 #define tskSTACK_FILL_BYTE      ( 0xa5U )\r
188 \r
189 /*\r
190  * Macros used by vListTask to indicate which state a task is in.\r
191  */\r
192 #define tskBLOCKED_CHAR         ( ( signed char ) 'B' )\r
193 #define tskREADY_CHAR           ( ( signed char ) 'R' )\r
194 #define tskDELETED_CHAR         ( ( signed char ) 'D' )\r
195 #define tskSUSPENDED_CHAR       ( ( signed char ) 'S' )\r
196 \r
197 /*\r
198  * Macros and private variables used by the trace facility.\r
199  */\r
200 #if ( configUSE_TRACE_FACILITY == 1 )\r
201 \r
202         #define tskSIZE_OF_EACH_TRACE_LINE                      ( ( unsigned long ) ( sizeof( unsigned long ) + sizeof( unsigned long ) ) )\r
203         PRIVILEGED_DATA static volatile signed char * volatile pcTraceBuffer;\r
204         PRIVILEGED_DATA static signed char *pcTraceBufferStart;\r
205         PRIVILEGED_DATA static signed char *pcTraceBufferEnd;\r
206         PRIVILEGED_DATA static signed portBASE_TYPE xTracing = pdFALSE;\r
207         static unsigned portBASE_TYPE uxPreviousTask = 255U;\r
208         PRIVILEGED_DATA static char pcStatusString[ 50 ];\r
209 \r
210 #endif\r
211 \r
212 /*-----------------------------------------------------------*/\r
213 \r
214 /*\r
215  * Macro that writes a trace of scheduler activity to a buffer.  This trace\r
216  * shows which task is running when and is very useful as a debugging tool.\r
217  * As this macro is called each context switch it is a good idea to undefine\r
218  * it if not using the facility.\r
219  */\r
220 #if ( configUSE_TRACE_FACILITY == 1 )\r
221 \r
222         #define vWriteTraceToBuffer()                                                                                                                                   \\r
223         {                                                                                                                                                                                               \\r
224                 if( xTracing )                                                                                                                                                          \\r
225                 {                                                                                                                                                                                       \\r
226                         if( uxPreviousTask != pxCurrentTCB->uxTCBNumber )                                                                               \\r
227                         {                                                                                                                                                                               \\r
228                                 if( ( pcTraceBuffer + tskSIZE_OF_EACH_TRACE_LINE ) < pcTraceBufferEnd )                         \\r
229                                 {                                                                                                                                                                       \\r
230                                         uxPreviousTask = pxCurrentTCB->uxTCBNumber;                                                                             \\r
231                                         *( unsigned long * ) pcTraceBuffer = ( unsigned long ) xTickCount;                              \\r
232                                         pcTraceBuffer += sizeof( unsigned long );                                                                               \\r
233                                         *( unsigned long * ) pcTraceBuffer = ( unsigned long ) uxPreviousTask;                  \\r
234                                         pcTraceBuffer += sizeof( unsigned long );                                                                               \\r
235                                 }                                                                                                                                                                       \\r
236                                 else                                                                                                                                                            \\r
237                                 {                                                                                                                                                                       \\r
238                                         xTracing = pdFALSE;                                                                                                                             \\r
239                                 }                                                                                                                                                                       \\r
240                         }                                                                                                                                                                               \\r
241                 }                                                                                                                                                                                       \\r
242         }\r
243 \r
244 #else\r
245 \r
246         #define vWriteTraceToBuffer()\r
247 \r
248 #endif\r
249 /*-----------------------------------------------------------*/\r
250 \r
251 /*\r
252  * Place the task represented by pxTCB into the appropriate ready queue for\r
253  * the task.  It is inserted at the end of the list.  One quirk of this is\r
254  * that if the task being inserted is at the same priority as the currently\r
255  * executing task, then it will only be rescheduled after the currently\r
256  * executing task has been rescheduled.\r
257  */\r
258 #define prvAddTaskToReadyQueue( pxTCB )                                                                                                                                                                 \\r
259         if( ( pxTCB )->uxPriority > uxTopReadyPriority )                                                                                                                                        \\r
260         {                                                                                                                                                                                                                                       \\r
261                 uxTopReadyPriority = ( pxTCB )->uxPriority;                                                                                                                                             \\r
262         }                                                                                                                                                                                                                                       \\r
263         vListInsertEnd( ( xList * ) &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xGenericListItem ) )\r
264 /*-----------------------------------------------------------*/\r
265 \r
266 /*\r
267  * Macro that looks at the list of tasks that are currently delayed to see if\r
268  * any require waking.\r
269  *\r
270  * Tasks are stored in the queue in the order of their wake time - meaning\r
271  * once one tasks has been found whose timer has not expired we need not look\r
272  * any further down the list.\r
273  */\r
274 #define prvCheckDelayedTasks()                                                                                                                  \\r
275 {                                                                                                                                                                               \\r
276 register tskTCB *pxTCB;                                                                                                                                 \\r
277 portTickType xItemValue;                                                                                                                                \\r
278                                                                                                                                                                                 \\r
279         /* Is the tick count greater than or equal to the wake time of the first                        \\r
280         task referenced from the delayed tasks list? */                                                                         \\r
281         if( xTickCount >= xNextTaskUnblockTime )                                                                                        \\r
282         {                                                                                                                                                                       \\r
283                 for( ;; )                                                                                                                                               \\r
284                 {                                                                                                                                                               \\r
285                         if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )                                         \\r
286                         {                                                                                                                                                       \\r
287                                 /* The delayed list is empty.  Set xNextTaskUnblockTime to the                  \\r
288                                 maximum possible value so it is extremely unlikely that the                             \\r
289                                 if( xTickCount >= xNextTaskUnblockTime ) test will pass next                    \\r
290                                 time through. */                                                                                                                \\r
291                                 xNextTaskUnblockTime = portMAX_DELAY;                                                                   \\r
292                                 break;                                                                                                                                  \\r
293                         }                                                                                                                                                       \\r
294                         else                                                                                                                                            \\r
295                         {                                                                                                                                                       \\r
296                                 /* The delayed list is not empty, get the value of the item at                  \\r
297                                 the head of the delayed list.  This is the time at which the                    \\r
298                                 task at the head of the delayed list should be removed from                             \\r
299                                 the Blocked state. */                                                                                                   \\r
300                                 pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );  \\r
301                                 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) );   \\r
302                                                                                                                                                                                 \\r
303                                 if( xTickCount < xItemValue )                                                                                   \\r
304                                 {                                                                                                                                               \\r
305                                         /* It is not time to unblock this item yet, but the item                        \\r
306                                         value is the time at which the task at the head of the                          \\r
307                                         blocked list should be removed from the Blocked state -                         \\r
308                                         so record the item value in xNextTaskUnblockTime. */                            \\r
309                                         xNextTaskUnblockTime = xItemValue;                                                                      \\r
310                                         break;                                                                                                                          \\r
311                                 }                                                                                                                                               \\r
312                                                                                                                                                                                 \\r
313                                 /* It is time to remove the item from the Blocked state. */                             \\r
314                                 vListRemove( &( pxTCB->xGenericListItem ) );                                                    \\r
315                                                                                                                                                                                 \\r
316                                 /* Is the task waiting on an event also? */                                                             \\r
317                                 if( pxTCB->xEventListItem.pvContainer )                                                                 \\r
318                                 {                                                                                                                                               \\r
319                                         vListRemove( &( pxTCB->xEventListItem ) );                                                      \\r
320                                 }                                                                                                                                               \\r
321                                 prvAddTaskToReadyQueue( pxTCB );                                                                                \\r
322                         }                                                                                                                                                       \\r
323                 }                                                                                                                                                               \\r
324         }                                                                                                                                                                       \\r
325 }\r
326 /*-----------------------------------------------------------*/\r
327 \r
328 /*\r
329  * Several functions take an xTaskHandle parameter that can optionally be NULL,\r
330  * where NULL is used to indicate that the handle of the currently executing\r
331  * task should be used in place of the parameter.  This macro simply checks to\r
332  * see if the parameter is NULL and returns a pointer to the appropriate TCB.\r
333  */\r
334 #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? ( tskTCB * ) pxCurrentTCB : ( tskTCB * ) ( pxHandle ) )\r
335 \r
336 /* Callback function prototypes. --------------------------*/\r
337 extern void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName );\r
338 extern void vApplicationTickHook( void );\r
339                 \r
340 /* File private functions. --------------------------------*/\r
341 \r
342 /*\r
343  * Utility to ready a TCB for a given task.  Mainly just copies the parameters\r
344  * into the TCB structure.\r
345  */\r
346 static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed char * const pcName, unsigned portBASE_TYPE uxPriority, const xMemoryRegion * const xRegions, unsigned short usStackDepth ) PRIVILEGED_FUNCTION;\r
347 \r
348 /*\r
349  * Utility to ready all the lists used by the scheduler.  This is called\r
350  * automatically upon the creation of the first task.\r
351  */\r
352 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;\r
353 \r
354 /*\r
355  * The idle task, which as all tasks is implemented as a never ending loop.\r
356  * The idle task is automatically created and added to the ready lists upon\r
357  * creation of the first user task.\r
358  *\r
359  * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific\r
360  * language extensions.  The equivalent prototype for this function is:\r
361  *\r
362  * void prvIdleTask( void *pvParameters );\r
363  *\r
364  */\r
365 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters );\r
366 \r
367 /*\r
368  * Utility to free all memory allocated by the scheduler to hold a TCB,\r
369  * including the stack pointed to by the TCB.\r
370  *\r
371  * This does not free memory allocated by the task itself (i.e. memory\r
372  * allocated by calls to pvPortMalloc from within the tasks application code).\r
373  */\r
374 #if ( ( INCLUDE_vTaskDelete == 1 ) || ( INCLUDE_vTaskCleanUpResources == 1 ) )\r
375 \r
376         static void prvDeleteTCB( tskTCB *pxTCB ) PRIVILEGED_FUNCTION;\r
377 \r
378 #endif\r
379 \r
380 /*\r
381  * Used only by the idle task.  This checks to see if anything has been placed\r
382  * in the list of tasks waiting to be deleted.  If so the task is cleaned up\r
383  * and its TCB deleted.\r
384  */\r
385 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;\r
386 \r
387 /*\r
388  * The currently executing task is entering the Blocked state.  Add the task to\r
389  * either the current or the overflow delayed task list.\r
390  */\r
391 static void prvAddCurrentTaskToDelayedList( portTickType xTimeToWake ) PRIVILEGED_FUNCTION;\r
392 \r
393 /*\r
394  * Allocates memory from the heap for a TCB and associated stack.  Checks the\r
395  * allocation was successful.\r
396  */\r
397 static tskTCB *prvAllocateTCBAndStack( unsigned short usStackDepth, portSTACK_TYPE *puxStackBuffer ) PRIVILEGED_FUNCTION;\r
398 \r
399 /*\r
400  * Called from vTaskList.  vListTasks details all the tasks currently under\r
401  * control of the scheduler.  The tasks may be in one of a number of lists.\r
402  * prvListTaskWithinSingleList accepts a list and details the tasks from\r
403  * within just that list.\r
404  *\r
405  * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM\r
406  * NORMAL APPLICATION CODE.\r
407  */\r
408 #if ( configUSE_TRACE_FACILITY == 1 )\r
409 \r
410         static void prvListTaskWithinSingleList( const signed char *pcWriteBuffer, xList *pxList, signed char cStatus ) PRIVILEGED_FUNCTION;\r
411 \r
412 #endif\r
413 \r
414 /*\r
415  * When a task is created, the stack of the task is filled with a known value.\r
416  * This function determines the 'high water mark' of the task stack by\r
417  * determining how much of the stack remains at the original preset value.\r
418  */\r
419 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )\r
420 \r
421         static unsigned short usTaskCheckFreeStackSpace( const unsigned char * pucStackByte ) PRIVILEGED_FUNCTION;\r
422 \r
423 #endif\r
424 \r
425 \r
426 /*lint +e956 */\r
427 \r
428 \r
429 \r
430 /*-----------------------------------------------------------\r
431  * TASK CREATION API documented in task.h\r
432  *----------------------------------------------------------*/\r
433 \r
434 signed portBASE_TYPE xTaskGenericCreate( pdTASK_CODE pxTaskCode, const signed char * const pcName, unsigned short usStackDepth, void *pvParameters, unsigned portBASE_TYPE uxPriority, xTaskHandle *pxCreatedTask, portSTACK_TYPE *puxStackBuffer, const xMemoryRegion * const xRegions )\r
435 {\r
436 signed portBASE_TYPE xReturn;\r
437 tskTCB * pxNewTCB;\r
438 \r
439         configASSERT( pxTaskCode );\r
440         configASSERT( ( uxPriority < configMAX_PRIORITIES ) );\r
441 \r
442         /* Allocate the memory required by the TCB and stack for the new task,\r
443         checking that the allocation was successful. */\r
444         pxNewTCB = prvAllocateTCBAndStack( usStackDepth, puxStackBuffer );\r
445 \r
446         if( pxNewTCB != NULL )\r
447         {\r
448                 portSTACK_TYPE *pxTopOfStack;\r
449 \r
450                 #if( portUSING_MPU_WRAPPERS == 1 )\r
451                         /* Should the task be created in privileged mode? */\r
452                         portBASE_TYPE xRunPrivileged;\r
453                         if( ( uxPriority & portPRIVILEGE_BIT ) != 0x00 )\r
454                         {\r
455                                 xRunPrivileged = pdTRUE;\r
456                         }\r
457                         else\r
458                         {\r
459                                 xRunPrivileged = pdFALSE;\r
460                         }\r
461                         uxPriority &= ~portPRIVILEGE_BIT;\r
462                 #endif /* portUSING_MPU_WRAPPERS == 1 */\r
463 \r
464                 /* Calculate the top of stack address.  This depends on whether the\r
465                 stack grows from high memory to low (as per the 80x86) or visa versa.\r
466                 portSTACK_GROWTH is used to make the result positive or negative as\r
467                 required by the port. */\r
468                 #if( portSTACK_GROWTH < 0 )\r
469                 {\r
470                         pxTopOfStack = pxNewTCB->pxStack + ( usStackDepth - ( unsigned short ) 1 );\r
471                         pxTopOfStack = ( portSTACK_TYPE * ) ( ( ( unsigned long ) pxTopOfStack ) & ( ( unsigned long ) ~portBYTE_ALIGNMENT_MASK  ) );\r
472 \r
473                         /* Check the alignment of the calculated top of stack is correct. */\r
474                         configASSERT( ( ( ( unsigned long ) pxTopOfStack & ( unsigned long ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );\r
475                 }\r
476                 #else\r
477                 {\r
478                         pxTopOfStack = pxNewTCB->pxStack;\r
479                         \r
480                         /* Check the alignment of the stack buffer is correct. */\r
481                         configASSERT( ( ( ( unsigned long ) pxNewTCB->pxStack & ( unsigned long ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );\r
482 \r
483                         /* If we want to use stack checking on architectures that use\r
484                         a positive stack growth direction then we also need to store the\r
485                         other extreme of the stack space. */\r
486                         pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( usStackDepth - 1 );\r
487                 }\r
488                 #endif\r
489 \r
490                 /* Setup the newly allocated TCB with the initial state of the task. */\r
491                 prvInitialiseTCBVariables( pxNewTCB, pcName, uxPriority, xRegions, usStackDepth );\r
492 \r
493                 /* Initialize the TCB stack to look as if the task was already running,\r
494                 but had been interrupted by the scheduler.  The return address is set\r
495                 to the start of the task function. Once the stack has been initialised\r
496                 the     top of stack variable is updated. */\r
497                 #if( portUSING_MPU_WRAPPERS == 1 )\r
498                 {\r
499                         pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );\r
500                 }\r
501                 #else\r
502                 {\r
503                         pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );\r
504                 }\r
505                 #endif\r
506 \r
507                 /* Check the alignment of the initialised stack. */\r
508                 configASSERT( ( ( ( unsigned long ) pxNewTCB->pxTopOfStack & ( unsigned long ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );\r
509 \r
510                 if( ( void * ) pxCreatedTask != NULL )\r
511                 {\r
512                         /* Pass the TCB out - in an anonymous way.  The calling function/\r
513                         task can use this as a handle to delete the task later if\r
514                         required.*/\r
515                         *pxCreatedTask = ( xTaskHandle ) pxNewTCB;\r
516                 }\r
517                 \r
518                 /* We are going to manipulate the task queues to add this task to a\r
519                 ready list, so must make sure no interrupts occur. */\r
520                 taskENTER_CRITICAL();\r
521                 {\r
522                         uxCurrentNumberOfTasks++;\r
523                         if( pxCurrentTCB == NULL )\r
524                         {\r
525                                 /* There are no other tasks, or all the other tasks are in\r
526                                 the suspended state - make this the current task. */\r
527                                 pxCurrentTCB =  pxNewTCB;\r
528 \r
529                                 if( uxCurrentNumberOfTasks == ( unsigned portBASE_TYPE ) 1 )\r
530                                 {\r
531                                         /* This is the first task to be created so do the preliminary\r
532                                         initialisation required.  We will not recover if this call\r
533                                         fails, but we will report the failure. */\r
534                                         prvInitialiseTaskLists();\r
535                                 }\r
536                         }\r
537                         else\r
538                         {\r
539                                 /* If the scheduler is not already running, make this task the\r
540                                 current task if it is the highest priority task to be created\r
541                                 so far. */\r
542                                 if( xSchedulerRunning == pdFALSE )\r
543                                 {\r
544                                         if( pxCurrentTCB->uxPriority <= uxPriority )\r
545                                         {\r
546                                                 pxCurrentTCB = pxNewTCB;\r
547                                         }\r
548                                 }\r
549                         }\r
550 \r
551                         /* Remember the top priority to make context switching faster.  Use\r
552                         the priority in pxNewTCB as this has been capped to a valid value. */\r
553                         if( pxNewTCB->uxPriority > uxTopUsedPriority )\r
554                         {\r
555                                 uxTopUsedPriority = pxNewTCB->uxPriority;\r
556                         }\r
557 \r
558                         #if ( configUSE_TRACE_FACILITY == 1 )\r
559                         {\r
560                                 /* Add a counter into the TCB for tracing only. */\r
561                                 pxNewTCB->uxTCBNumber = uxTaskNumber;\r
562                         }\r
563                         #endif\r
564                         uxTaskNumber++;\r
565 \r
566                         prvAddTaskToReadyQueue( pxNewTCB );\r
567 \r
568                         xReturn = pdPASS;\r
569                         traceTASK_CREATE( pxNewTCB );\r
570                 }\r
571                 taskEXIT_CRITICAL();\r
572         }\r
573         else\r
574         {\r
575                 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;\r
576                 traceTASK_CREATE_FAILED();\r
577         }\r
578 \r
579         if( xReturn == pdPASS )\r
580         {\r
581                 if( xSchedulerRunning != pdFALSE )\r
582                 {\r
583                         /* If the created task is of a higher priority than the current task\r
584                         then it should run now. */\r
585                         if( pxCurrentTCB->uxPriority < uxPriority )\r
586                         {\r
587                                 portYIELD_WITHIN_API();\r
588                         }\r
589                 }\r
590         }\r
591 \r
592         return xReturn;\r
593 }\r
594 /*-----------------------------------------------------------*/\r
595 \r
596 #if ( INCLUDE_vTaskDelete == 1 )\r
597 \r
598         void vTaskDelete( xTaskHandle pxTaskToDelete )\r
599         {\r
600         tskTCB *pxTCB;\r
601 \r
602                 taskENTER_CRITICAL();\r
603                 {\r
604                         /* Ensure a yield is performed if the current task is being\r
605                         deleted. */\r
606                         if( pxTaskToDelete == pxCurrentTCB )\r
607                         {\r
608                                 pxTaskToDelete = NULL;\r
609                         }\r
610 \r
611                         /* If null is passed in here then we are deleting ourselves. */\r
612                         pxTCB = prvGetTCBFromHandle( pxTaskToDelete );\r
613 \r
614                         /* Remove task from the ready list and place in the     termination list.\r
615                         This will stop the task from be scheduled.  The idle task will check\r
616                         the termination list and free up any memory allocated by the\r
617                         scheduler for the TCB and stack. */\r
618                         vListRemove( &( pxTCB->xGenericListItem ) );\r
619 \r
620                         /* Is the task waiting on an event also? */\r
621                         if( pxTCB->xEventListItem.pvContainer )\r
622                         {\r
623                                 vListRemove( &( pxTCB->xEventListItem ) );\r
624                         }\r
625 \r
626                         vListInsertEnd( ( xList * ) &xTasksWaitingTermination, &( pxTCB->xGenericListItem ) );\r
627 \r
628                         /* Increment the ucTasksDeleted variable so the idle task knows\r
629                         there is a task that has been deleted and that it should therefore\r
630                         check the xTasksWaitingTermination list. */\r
631                         ++uxTasksDeleted;\r
632 \r
633                         /* Increment the uxTaskNumberVariable also so kernel aware debuggers\r
634                         can detect that the task lists need re-generating. */\r
635                         uxTaskNumber++;\r
636 \r
637                         traceTASK_DELETE( pxTCB );\r
638                 }\r
639                 taskEXIT_CRITICAL();\r
640 \r
641                 /* Force a reschedule if we have just deleted the current task. */\r
642                 if( xSchedulerRunning != pdFALSE )\r
643                 {\r
644                         if( ( void * ) pxTaskToDelete == NULL )\r
645                         {\r
646                                 portYIELD_WITHIN_API();\r
647                         }\r
648                 }\r
649         }\r
650 \r
651 #endif\r
652 \r
653 \r
654 \r
655 \r
656 \r
657 \r
658 /*-----------------------------------------------------------\r
659  * TASK CONTROL API documented in task.h\r
660  *----------------------------------------------------------*/\r
661 \r
662 #if ( INCLUDE_vTaskDelayUntil == 1 )\r
663 \r
664         void vTaskDelayUntil( portTickType * const pxPreviousWakeTime, portTickType xTimeIncrement )\r
665         {\r
666         portTickType xTimeToWake;\r
667         portBASE_TYPE xAlreadyYielded, xShouldDelay = pdFALSE;\r
668 \r
669                 configASSERT( pxPreviousWakeTime );\r
670                 configASSERT( ( xTimeIncrement > 0 ) );\r
671 \r
672                 vTaskSuspendAll();\r
673                 {\r
674                         /* Generate the tick time at which the task wants to wake. */\r
675                         xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;\r
676 \r
677                         if( xTickCount < *pxPreviousWakeTime )\r
678                         {\r
679                                 /* The tick count has overflowed since this function was\r
680                                 lasted called.  In this case the only time we should ever\r
681                                 actually delay is if the wake time has also     overflowed,\r
682                                 and the wake time is greater than the tick time.  When this\r
683                                 is the case it is as if neither time had overflowed. */\r
684                                 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xTickCount ) )\r
685                                 {\r
686                                         xShouldDelay = pdTRUE;\r
687                                 }\r
688                         }\r
689                         else\r
690                         {\r
691                                 /* The tick time has not overflowed.  In this case we will\r
692                                 delay if either the wake time has overflowed, and/or the\r
693                                 tick time is less than the wake time. */\r
694                                 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xTickCount ) )\r
695                                 {\r
696                                         xShouldDelay = pdTRUE;\r
697                                 }\r
698                         }\r
699 \r
700                         /* Update the wake time ready for the next call. */\r
701                         *pxPreviousWakeTime = xTimeToWake;\r
702 \r
703                         if( xShouldDelay != pdFALSE )\r
704                         {\r
705                                 traceTASK_DELAY_UNTIL();\r
706 \r
707                                 /* We must remove ourselves from the ready list before adding\r
708                                 ourselves to the blocked list as the same list item is used for\r
709                                 both lists. */\r
710                                 vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
711                                 prvAddCurrentTaskToDelayedList( xTimeToWake );\r
712                         }\r
713                 }\r
714                 xAlreadyYielded = xTaskResumeAll();\r
715 \r
716                 /* Force a reschedule if xTaskResumeAll has not already done so, we may\r
717                 have put ourselves to sleep. */\r
718                 if( !xAlreadyYielded )\r
719                 {\r
720                         portYIELD_WITHIN_API();\r
721                 }\r
722         }\r
723 \r
724 #endif\r
725 /*-----------------------------------------------------------*/\r
726 \r
727 #if ( INCLUDE_vTaskDelay == 1 )\r
728 \r
729         void vTaskDelay( portTickType xTicksToDelay )\r
730         {\r
731         portTickType xTimeToWake;\r
732         signed portBASE_TYPE xAlreadyYielded = pdFALSE;\r
733 \r
734                 /* A delay time of zero just forces a reschedule. */\r
735                 if( xTicksToDelay > ( portTickType ) 0 )\r
736                 {\r
737                         vTaskSuspendAll();\r
738                         {\r
739                                 traceTASK_DELAY();\r
740 \r
741                                 /* A task that is removed from the event list while the\r
742                                 scheduler is suspended will not get placed in the ready\r
743                                 list or removed from the blocked list until the scheduler\r
744                                 is resumed.\r
745 \r
746                                 This task cannot be in an event list as it is the currently\r
747                                 executing task. */\r
748 \r
749                                 /* Calculate the time to wake - this may overflow but this is\r
750                                 not a problem. */\r
751                                 xTimeToWake = xTickCount + xTicksToDelay;\r
752 \r
753                                 /* We must remove ourselves from the ready list before adding\r
754                                 ourselves to the blocked list as the same list item is used for\r
755                                 both lists. */\r
756                                 vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
757                                 prvAddCurrentTaskToDelayedList( xTimeToWake );\r
758                         }\r
759                         xAlreadyYielded = xTaskResumeAll();\r
760                 }\r
761 \r
762                 /* Force a reschedule if xTaskResumeAll has not already done so, we may\r
763                 have put ourselves to sleep. */\r
764                 if( !xAlreadyYielded )\r
765                 {\r
766                         portYIELD_WITHIN_API();\r
767                 }\r
768         }\r
769 \r
770 #endif\r
771 /*-----------------------------------------------------------*/\r
772 \r
773 #if ( INCLUDE_uxTaskPriorityGet == 1 )\r
774 \r
775         unsigned portBASE_TYPE uxTaskPriorityGet( xTaskHandle pxTask )\r
776         {\r
777         tskTCB *pxTCB;\r
778         unsigned portBASE_TYPE uxReturn;\r
779 \r
780                 taskENTER_CRITICAL();\r
781                 {\r
782                         /* If null is passed in here then we are changing the\r
783                         priority of the calling function. */\r
784                         pxTCB = prvGetTCBFromHandle( pxTask );\r
785                         uxReturn = pxTCB->uxPriority;\r
786                 }\r
787                 taskEXIT_CRITICAL();\r
788 \r
789                 return uxReturn;\r
790         }\r
791 \r
792 #endif\r
793 /*-----------------------------------------------------------*/\r
794 \r
795 #if ( INCLUDE_vTaskPrioritySet == 1 )\r
796 \r
797         void vTaskPrioritySet( xTaskHandle pxTask, unsigned portBASE_TYPE uxNewPriority )\r
798         {\r
799         tskTCB *pxTCB;\r
800         unsigned portBASE_TYPE uxCurrentPriority;\r
801         portBASE_TYPE xYieldRequired = pdFALSE;\r
802 \r
803                 configASSERT( ( uxNewPriority < configMAX_PRIORITIES ) );\r
804 \r
805                 /* Ensure the new priority is valid. */\r
806                 if( uxNewPriority >= configMAX_PRIORITIES )\r
807                 {\r
808                         uxNewPriority = configMAX_PRIORITIES - ( unsigned portBASE_TYPE ) 1U;\r
809                 }\r
810 \r
811                 taskENTER_CRITICAL();\r
812                 {\r
813                         if( pxTask == pxCurrentTCB )\r
814                         {\r
815                                 pxTask = NULL;\r
816                         }\r
817 \r
818                         /* If null is passed in here then we are changing the\r
819                         priority of the calling function. */\r
820                         pxTCB = prvGetTCBFromHandle( pxTask );\r
821 \r
822                         traceTASK_PRIORITY_SET( pxTask, uxNewPriority );\r
823 \r
824                         #if ( configUSE_MUTEXES == 1 )\r
825                         {\r
826                                 uxCurrentPriority = pxTCB->uxBasePriority;\r
827                         }\r
828                         #else\r
829                         {\r
830                                 uxCurrentPriority = pxTCB->uxPriority;\r
831                         }\r
832                         #endif\r
833 \r
834                         if( uxCurrentPriority != uxNewPriority )\r
835                         {\r
836                                 /* The priority change may have readied a task of higher\r
837                                 priority than the calling task. */\r
838                                 if( uxNewPriority > uxCurrentPriority )\r
839                                 {\r
840                                         if( pxTask != NULL )\r
841                                         {\r
842                                                 /* The priority of another task is being raised.  If we\r
843                                                 were raising the priority of the currently running task\r
844                                                 there would be no need to switch as it must have already\r
845                                                 been the highest priority task. */\r
846                                                 xYieldRequired = pdTRUE;\r
847                                         }\r
848                                 }\r
849                                 else if( pxTask == NULL )\r
850                                 {\r
851                                         /* Setting our own priority down means there may now be another\r
852                                         task of higher priority that is ready to execute. */\r
853                                         xYieldRequired = pdTRUE;\r
854                                 }\r
855 \r
856 \r
857 \r
858                                 #if ( configUSE_MUTEXES == 1 )\r
859                                 {\r
860                                         /* Only change the priority being used if the task is not\r
861                                         currently using an inherited priority. */\r
862                                         if( pxTCB->uxBasePriority == pxTCB->uxPriority )\r
863                                         {\r
864                                                 pxTCB->uxPriority = uxNewPriority;\r
865                                         }\r
866 \r
867                                         /* The base priority gets set whatever. */\r
868                                         pxTCB->uxBasePriority = uxNewPriority;\r
869                                 }\r
870                                 #else\r
871                                 {\r
872                                         pxTCB->uxPriority = uxNewPriority;\r
873                                 }\r
874                                 #endif\r
875 \r
876                                 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( configMAX_PRIORITIES - ( portTickType ) uxNewPriority ) );\r
877 \r
878                                 /* If the task is in the blocked or suspended list we need do\r
879                                 nothing more than change it's priority variable. However, if\r
880                                 the task is in a ready list it needs to be removed and placed\r
881                                 in the queue appropriate to its new priority. */\r
882                                 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxCurrentPriority ] ), &( pxTCB->xGenericListItem ) ) )\r
883                                 {\r
884                                         /* The task is currently in its ready list - remove before adding\r
885                                         it to it's new ready list.  As we are in a critical section we\r
886                                         can do this even if the scheduler is suspended. */\r
887                                         vListRemove( &( pxTCB->xGenericListItem ) );\r
888                                         prvAddTaskToReadyQueue( pxTCB );\r
889                                 }\r
890 \r
891                                 if( xYieldRequired == pdTRUE )\r
892                                 {\r
893                                         portYIELD_WITHIN_API();\r
894                                 }\r
895                         }\r
896                 }\r
897                 taskEXIT_CRITICAL();\r
898         }\r
899 \r
900 #endif\r
901 /*-----------------------------------------------------------*/\r
902 \r
903 #if ( INCLUDE_vTaskSuspend == 1 )\r
904 \r
905         void vTaskSuspend( xTaskHandle pxTaskToSuspend )\r
906         {\r
907         tskTCB *pxTCB;\r
908 \r
909                 taskENTER_CRITICAL();\r
910                 {\r
911                         /* Ensure a yield is performed if the current task is being\r
912                         suspended. */\r
913                         if( pxTaskToSuspend == pxCurrentTCB )\r
914                         {\r
915                                 pxTaskToSuspend = NULL;\r
916                         }\r
917 \r
918                         /* If null is passed in here then we are suspending ourselves. */\r
919                         pxTCB = prvGetTCBFromHandle( pxTaskToSuspend );\r
920 \r
921                         traceTASK_SUSPEND( pxTCB );\r
922 \r
923                         /* Remove task from the ready/delayed list and place in the     suspended list. */\r
924                         vListRemove( &( pxTCB->xGenericListItem ) );\r
925 \r
926                         /* Is the task waiting on an event also? */\r
927                         if( pxTCB->xEventListItem.pvContainer )\r
928                         {\r
929                                 vListRemove( &( pxTCB->xEventListItem ) );\r
930                         }\r
931 \r
932                         vListInsertEnd( ( xList * ) &xSuspendedTaskList, &( pxTCB->xGenericListItem ) );\r
933                 }\r
934                 taskEXIT_CRITICAL();\r
935 \r
936                 if( ( void * ) pxTaskToSuspend == NULL )\r
937                 {\r
938                         if( xSchedulerRunning != pdFALSE )\r
939                         {\r
940                                 /* We have just suspended the current task. */\r
941                                 portYIELD_WITHIN_API();\r
942                         }\r
943                         else\r
944                         {\r
945                                 /* The scheduler is not running, but the task that was pointed\r
946                                 to by pxCurrentTCB has just been suspended and pxCurrentTCB\r
947                                 must be adjusted to point to a different task. */\r
948                                 if( uxCurrentNumberOfTasks == ( unsigned portBASE_TYPE ) 1U )\r
949                                 {\r
950                                         /* No other tasks are defined, so set pxCurrentTCB back to\r
951                                         NULL so when the next task is created pxCurrentTCB will\r
952                                         be set to point to it no matter what its relative priority\r
953                                         is. */\r
954                                         pxCurrentTCB = NULL;\r
955                                 }\r
956                                 else\r
957                                 {\r
958                                         vTaskSwitchContext();\r
959                                 }\r
960                         }\r
961                 }\r
962         }\r
963 \r
964 #endif\r
965 /*-----------------------------------------------------------*/\r
966 \r
967 #if ( INCLUDE_vTaskSuspend == 1 )\r
968 \r
969         signed portBASE_TYPE xTaskIsTaskSuspended( xTaskHandle xTask )\r
970         {\r
971         portBASE_TYPE xReturn = pdFALSE;\r
972         const tskTCB * const pxTCB = ( tskTCB * ) xTask;\r
973 \r
974                 /* It does not make sense to check if the calling task is suspended. */\r
975                 configASSERT( xTask );\r
976 \r
977                 /* Is the task we are attempting to resume actually in the\r
978                 suspended list? */\r
979                 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xGenericListItem ) ) != pdFALSE )\r
980                 {\r
981                         /* Has the task already been resumed from within an ISR? */\r
982                         if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdTRUE )\r
983                         {\r
984                                 /* Is it in the suspended list because it is in the\r
985                                 Suspended state?  It is possible to be in the suspended\r
986                                 list because it is blocked on a task with no timeout\r
987                                 specified. */\r
988                                 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) == pdTRUE )\r
989                                 {\r
990                                         xReturn = pdTRUE;\r
991                                 }\r
992                         }\r
993                 }\r
994 \r
995                 return xReturn;\r
996         }\r
997 \r
998 #endif\r
999 /*-----------------------------------------------------------*/\r
1000 \r
1001 #if ( INCLUDE_vTaskSuspend == 1 )\r
1002 \r
1003         void vTaskResume( xTaskHandle pxTaskToResume )\r
1004         {\r
1005         tskTCB *pxTCB;\r
1006 \r
1007                 /* It does not make sense to resume the calling task. */\r
1008                 configASSERT( pxTaskToResume );\r
1009 \r
1010                 /* Remove the task from whichever list it is currently in, and place\r
1011                 it in the ready list. */\r
1012                 pxTCB = ( tskTCB * ) pxTaskToResume;\r
1013 \r
1014                 /* The parameter cannot be NULL as it is impossible to resume the\r
1015                 currently executing task. */\r
1016                 if( ( pxTCB != NULL ) && ( pxTCB != pxCurrentTCB ) )\r
1017                 {\r
1018                         taskENTER_CRITICAL();\r
1019                         {\r
1020                                 if( xTaskIsTaskSuspended( pxTCB ) == pdTRUE )\r
1021                                 {\r
1022                                         traceTASK_RESUME( pxTCB );\r
1023 \r
1024                                         /* As we are in a critical section we can access the ready\r
1025                                         lists even if the scheduler is suspended. */\r
1026                                         vListRemove(  &( pxTCB->xGenericListItem ) );\r
1027                                         prvAddTaskToReadyQueue( pxTCB );\r
1028 \r
1029                                         /* We may have just resumed a higher priority task. */\r
1030                                         if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )\r
1031                                         {\r
1032                                                 /* This yield may not cause the task just resumed to run, but\r
1033                                                 will leave the lists in the correct state for the next yield. */\r
1034                                                 portYIELD_WITHIN_API();\r
1035                                         }\r
1036                                 }\r
1037                         }\r
1038                         taskEXIT_CRITICAL();\r
1039                 }\r
1040         }\r
1041 \r
1042 #endif\r
1043 \r
1044 /*-----------------------------------------------------------*/\r
1045 \r
1046 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )\r
1047 \r
1048         portBASE_TYPE xTaskResumeFromISR( xTaskHandle pxTaskToResume )\r
1049         {\r
1050         portBASE_TYPE xYieldRequired = pdFALSE;\r
1051         tskTCB *pxTCB;\r
1052 \r
1053                 configASSERT( pxTaskToResume );\r
1054 \r
1055                 pxTCB = ( tskTCB * ) pxTaskToResume;\r
1056 \r
1057                 if( xTaskIsTaskSuspended( pxTCB ) == pdTRUE )\r
1058                 {\r
1059                         traceTASK_RESUME_FROM_ISR( pxTCB );\r
1060 \r
1061                         if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )\r
1062                         {\r
1063                                 xYieldRequired = ( pxTCB->uxPriority >= pxCurrentTCB->uxPriority );\r
1064                                 vListRemove(  &( pxTCB->xGenericListItem ) );\r
1065                                 prvAddTaskToReadyQueue( pxTCB );\r
1066                         }\r
1067                         else\r
1068                         {\r
1069                                 /* We cannot access the delayed or ready lists, so will hold this\r
1070                                 task pending until the scheduler is resumed, at which point a\r
1071                                 yield will be performed if necessary. */\r
1072                                 vListInsertEnd( ( xList * ) &( xPendingReadyList ), &( pxTCB->xEventListItem ) );\r
1073                         }\r
1074                 }\r
1075 \r
1076                 return xYieldRequired;\r
1077         }\r
1078 \r
1079 #endif\r
1080 \r
1081 \r
1082 \r
1083 \r
1084 /*-----------------------------------------------------------\r
1085  * PUBLIC SCHEDULER CONTROL documented in task.h\r
1086  *----------------------------------------------------------*/\r
1087 \r
1088 \r
1089 void vTaskStartScheduler( void )\r
1090 {\r
1091 portBASE_TYPE xReturn;\r
1092 \r
1093         /* Add the idle task at the lowest priority. */\r
1094         xReturn = xTaskCreate( prvIdleTask, ( signed char * ) "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), ( xTaskHandle * ) NULL );\r
1095 \r
1096         #if ( configUSE_TIMERS == 1 )\r
1097         {\r
1098                 if( xReturn == pdPASS )\r
1099                 {\r
1100                         xReturn = xTimerCreateTimerTask();\r
1101                 }\r
1102         }\r
1103         #endif\r
1104 \r
1105         if( xReturn == pdPASS )\r
1106         {\r
1107                 /* Interrupts are turned off here, to ensure a tick does not occur\r
1108                 before or during the call to xPortStartScheduler().  The stacks of\r
1109                 the created tasks contain a status word with interrupts switched on\r
1110                 so interrupts will automatically get re-enabled when the first task\r
1111                 starts to run.\r
1112 \r
1113                 STEPPING THROUGH HERE USING A DEBUGGER CAN CAUSE BIG PROBLEMS IF THE\r
1114                 DEBUGGER ALLOWS INTERRUPTS TO BE PROCESSED. */\r
1115                 portDISABLE_INTERRUPTS();\r
1116 \r
1117                 xSchedulerRunning = pdTRUE;\r
1118                 xTickCount = ( portTickType ) 0;\r
1119 \r
1120                 /* If configGENERATE_RUN_TIME_STATS is defined then the following\r
1121                 macro must be defined to configure the timer/counter used to generate\r
1122                 the run time counter time base. */\r
1123                 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();\r
1124                 \r
1125                 /* Setting up the timer tick is hardware specific and thus in the\r
1126                 portable interface. */\r
1127                 if( xPortStartScheduler() )\r
1128                 {\r
1129                         /* Should not reach here as if the scheduler is running the\r
1130                         function will not return. */\r
1131                 }\r
1132                 else\r
1133                 {\r
1134                         /* Should only reach here if a task calls xTaskEndScheduler(). */\r
1135                 }\r
1136         }\r
1137 \r
1138         /* This line will only be reached if the kernel could not be started. */\r
1139         configASSERT( xReturn );\r
1140 }\r
1141 /*-----------------------------------------------------------*/\r
1142 \r
1143 void vTaskEndScheduler( void )\r
1144 {\r
1145         /* Stop the scheduler interrupts and call the portable scheduler end\r
1146         routine so the original ISRs can be restored if necessary.  The port\r
1147         layer must ensure interrupts enable     bit is left in the correct state. */\r
1148         portDISABLE_INTERRUPTS();\r
1149         xSchedulerRunning = pdFALSE;\r
1150         vPortEndScheduler();\r
1151 }\r
1152 /*----------------------------------------------------------*/\r
1153 \r
1154 void vTaskSuspendAll( void )\r
1155 {\r
1156         /* A critical section is not required as the variable is of type\r
1157         portBASE_TYPE. */\r
1158         ++uxSchedulerSuspended;\r
1159 }\r
1160 /*----------------------------------------------------------*/\r
1161 \r
1162 signed portBASE_TYPE xTaskResumeAll( void )\r
1163 {\r
1164 register tskTCB *pxTCB;\r
1165 signed portBASE_TYPE xAlreadyYielded = pdFALSE;\r
1166 \r
1167         /* If uxSchedulerSuspended is zero then this function does not match a\r
1168         previous call to vTaskSuspendAll(). */\r
1169         configASSERT( uxSchedulerSuspended );\r
1170 \r
1171         /* It is possible that an ISR caused a task to be removed from an event\r
1172         list while the scheduler was suspended.  If this was the case then the\r
1173         removed task will have been added to the xPendingReadyList.  Once the\r
1174         scheduler has been resumed it is safe to move all the pending ready\r
1175         tasks from this list into their appropriate ready list. */\r
1176         taskENTER_CRITICAL();\r
1177         {\r
1178                 --uxSchedulerSuspended;\r
1179 \r
1180                 if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )\r
1181                 {\r
1182                         if( uxCurrentNumberOfTasks > ( unsigned portBASE_TYPE ) 0 )\r
1183                         {\r
1184                                 portBASE_TYPE xYieldRequired = pdFALSE;\r
1185 \r
1186                                 /* Move any readied tasks from the pending list into the\r
1187                                 appropriate ready list. */\r
1188                                 while( listLIST_IS_EMPTY( ( xList * ) &xPendingReadyList ) == pdFALSE )\r
1189                                 {\r
1190                                         pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY(  ( ( xList * ) &xPendingReadyList ) );\r
1191                                         vListRemove( &( pxTCB->xEventListItem ) );\r
1192                                         vListRemove( &( pxTCB->xGenericListItem ) );\r
1193                                         prvAddTaskToReadyQueue( pxTCB );\r
1194 \r
1195                                         /* If we have moved a task that has a priority higher than\r
1196                                         the current task then we should yield. */\r
1197                                         if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )\r
1198                                         {\r
1199                                                 xYieldRequired = pdTRUE;\r
1200                                         }\r
1201                                 }\r
1202 \r
1203                                 /* If any ticks occurred while the scheduler was suspended then\r
1204                                 they should be processed now.  This ensures the tick count does not\r
1205                                 slip, and that any delayed tasks are resumed at the correct time. */\r
1206                                 if( uxMissedTicks > ( unsigned portBASE_TYPE ) 0 )\r
1207                                 {\r
1208                                         while( uxMissedTicks > ( unsigned portBASE_TYPE ) 0 )\r
1209                                         {\r
1210                                                 vTaskIncrementTick();\r
1211                                                 --uxMissedTicks;\r
1212                                         }\r
1213 \r
1214                                         /* As we have processed some ticks it is appropriate to yield\r
1215                                         to ensure the highest priority task that is ready to run is\r
1216                                         the task actually running. */\r
1217                                         #if configUSE_PREEMPTION == 1\r
1218                                         {\r
1219                                                 xYieldRequired = pdTRUE;\r
1220                                         }\r
1221                                         #endif\r
1222                                 }\r
1223 \r
1224                                 if( ( xYieldRequired == pdTRUE ) || ( xMissedYield == pdTRUE ) )\r
1225                                 {\r
1226                                         xAlreadyYielded = pdTRUE;\r
1227                                         xMissedYield = pdFALSE;\r
1228                                         portYIELD_WITHIN_API();\r
1229                                 }\r
1230                         }\r
1231                 }\r
1232         }\r
1233         taskEXIT_CRITICAL();\r
1234 \r
1235         return xAlreadyYielded;\r
1236 }\r
1237 \r
1238 \r
1239 \r
1240 \r
1241 \r
1242 \r
1243 /*-----------------------------------------------------------\r
1244  * PUBLIC TASK UTILITIES documented in task.h\r
1245  *----------------------------------------------------------*/\r
1246 \r
1247 \r
1248 \r
1249 portTickType xTaskGetTickCount( void )\r
1250 {\r
1251 portTickType xTicks;\r
1252 \r
1253         /* Critical section required if running on a 16 bit processor. */\r
1254         taskENTER_CRITICAL();\r
1255         {\r
1256                 xTicks = xTickCount;\r
1257         }\r
1258         taskEXIT_CRITICAL();\r
1259 \r
1260         return xTicks;\r
1261 }\r
1262 /*-----------------------------------------------------------*/\r
1263 \r
1264 portTickType xTaskGetTickCountFromISR( void )\r
1265 {\r
1266 portTickType xReturn;\r
1267 unsigned portBASE_TYPE uxSavedInterruptStatus;\r
1268 \r
1269         uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
1270         xReturn = xTickCount;\r
1271         portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );\r
1272 \r
1273         return xReturn;\r
1274 }\r
1275 /*-----------------------------------------------------------*/\r
1276 \r
1277 unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )\r
1278 {\r
1279         /* A critical section is not required because the variables are of type\r
1280         portBASE_TYPE. */\r
1281         return uxCurrentNumberOfTasks;\r
1282 }\r
1283 /*-----------------------------------------------------------*/\r
1284 \r
1285 #if ( configUSE_TRACE_FACILITY == 1 )\r
1286 \r
1287         void vTaskList( signed char *pcWriteBuffer )\r
1288         {\r
1289         unsigned portBASE_TYPE uxQueue;\r
1290 \r
1291                 /* This is a VERY costly function that should be used for debug only.\r
1292                 It leaves interrupts disabled for a LONG time. */\r
1293 \r
1294                 vTaskSuspendAll();\r
1295                 {\r
1296                         /* Run through all the lists that could potentially contain a TCB and\r
1297                         report the task name, state and stack high water mark. */\r
1298 \r
1299                         *pcWriteBuffer = ( signed char ) 0x00;\r
1300                         strcat( ( char * ) pcWriteBuffer, ( const char * ) "\r\n" );\r
1301 \r
1302                         uxQueue = uxTopUsedPriority + ( unsigned portBASE_TYPE ) 1U;\r
1303 \r
1304                         do\r
1305                         {\r
1306                                 uxQueue--;\r
1307 \r
1308                                 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) == pdFALSE )\r
1309                                 {\r
1310                                         prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), tskREADY_CHAR );\r
1311                                 }\r
1312                         }while( uxQueue > ( unsigned short ) tskIDLE_PRIORITY );\r
1313 \r
1314                         if( listLIST_IS_EMPTY( pxDelayedTaskList ) == pdFALSE )\r
1315                         {\r
1316                                 prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, tskBLOCKED_CHAR );\r
1317                         }\r
1318 \r
1319                         if( listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) == pdFALSE )\r
1320                         {\r
1321                                 prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, tskBLOCKED_CHAR );\r
1322                         }\r
1323 \r
1324                         #if( INCLUDE_vTaskDelete == 1 )\r
1325                         {\r
1326                                 if( listLIST_IS_EMPTY( &xTasksWaitingTermination ) == pdFALSE )\r
1327                                 {\r
1328                                         prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &xTasksWaitingTermination, tskDELETED_CHAR );\r
1329                                 }\r
1330                         }\r
1331                         #endif\r
1332 \r
1333                         #if ( INCLUDE_vTaskSuspend == 1 )\r
1334                         {\r
1335                                 if( listLIST_IS_EMPTY( &xSuspendedTaskList ) == pdFALSE )\r
1336                                 {\r
1337                                         prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &xSuspendedTaskList, tskSUSPENDED_CHAR );\r
1338                                 }\r
1339                         }\r
1340                         #endif\r
1341                 }\r
1342                 xTaskResumeAll();\r
1343         }\r
1344 \r
1345 #endif\r
1346 /*----------------------------------------------------------*/\r
1347 \r
1348 #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
1349 \r
1350         void vTaskGetRunTimeStats( signed char *pcWriteBuffer )\r
1351         {\r
1352         unsigned portBASE_TYPE uxQueue;\r
1353         unsigned long ulTotalRunTime;\r
1354 \r
1355                 /* This is a VERY costly function that should be used for debug only.\r
1356                 It leaves interrupts disabled for a LONG time. */\r
1357 \r
1358                 vTaskSuspendAll();\r
1359                 {\r
1360                         #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE\r
1361                                 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );\r
1362                         #else\r
1363                                 ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();\r
1364                         #endif\r
1365 \r
1366                         /* Divide ulTotalRunTime by 100 to make the percentage caluclations\r
1367                         simpler in the prvGenerateRunTimeStatsForTasksInList() function. */\r
1368                         ulTotalRunTime /= 100UL;\r
1369                         \r
1370                         /* Run through all the lists that could potentially contain a TCB,\r
1371                         generating a table of run timer percentages in the provided\r
1372                         buffer. */\r
1373 \r
1374                         *pcWriteBuffer = ( signed char ) 0x00;\r
1375                         strcat( ( char * ) pcWriteBuffer, ( const char * ) "\r\n" );\r
1376 \r
1377                         uxQueue = uxTopUsedPriority + ( unsigned portBASE_TYPE ) 1U;\r
1378 \r
1379                         do\r
1380                         {\r
1381                                 uxQueue--;\r
1382 \r
1383                                 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) == pdFALSE )\r
1384                                 {\r
1385                                         prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), ulTotalRunTime );\r
1386                                 }\r
1387                         }while( uxQueue > ( unsigned short ) tskIDLE_PRIORITY );\r
1388 \r
1389                         if( listLIST_IS_EMPTY( pxDelayedTaskList ) == pdFALSE )\r
1390                         {\r
1391                                 prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, ulTotalRunTime );\r
1392                         }\r
1393 \r
1394                         if( listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) == pdFALSE )\r
1395                         {\r
1396                                 prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, ulTotalRunTime );\r
1397                         }\r
1398 \r
1399                         #if ( INCLUDE_vTaskDelete == 1 )\r
1400                         {\r
1401                                 if( listLIST_IS_EMPTY( &xTasksWaitingTermination ) == pdFALSE )\r
1402                                 {\r
1403                                         prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) &xTasksWaitingTermination, ulTotalRunTime );\r
1404                                 }\r
1405                         }\r
1406                         #endif\r
1407 \r
1408                         #if ( INCLUDE_vTaskSuspend == 1 )\r
1409                         {\r
1410                                 if( listLIST_IS_EMPTY( &xSuspendedTaskList ) == pdFALSE )\r
1411                                 {\r
1412                                         prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) &xSuspendedTaskList, ulTotalRunTime );\r
1413                                 }\r
1414                         }\r
1415                         #endif\r
1416                 }\r
1417                 xTaskResumeAll();\r
1418         }\r
1419 \r
1420 #endif\r
1421 /*----------------------------------------------------------*/\r
1422 \r
1423 #if ( configUSE_TRACE_FACILITY == 1 )\r
1424 \r
1425         void vTaskStartTrace( signed char * pcBuffer, unsigned long ulBufferSize )\r
1426         {\r
1427                 configASSERT( pcBuffer );\r
1428                 configASSERT( ulBufferSize );\r
1429 \r
1430                 taskENTER_CRITICAL();\r
1431                 {\r
1432                         pcTraceBuffer = ( signed char * )pcBuffer;\r
1433                         pcTraceBufferStart = pcBuffer;\r
1434                         pcTraceBufferEnd = pcBuffer + ( ulBufferSize - tskSIZE_OF_EACH_TRACE_LINE );\r
1435                         xTracing = pdTRUE;\r
1436                 }\r
1437                 taskEXIT_CRITICAL();\r
1438         }\r
1439 \r
1440 #endif\r
1441 /*----------------------------------------------------------*/\r
1442 \r
1443 #if ( configUSE_TRACE_FACILITY == 1 )\r
1444 \r
1445         unsigned long ulTaskEndTrace( void )\r
1446         {\r
1447         unsigned long ulBufferLength;\r
1448 \r
1449                 taskENTER_CRITICAL();\r
1450                         xTracing = pdFALSE;\r
1451                 taskEXIT_CRITICAL();\r
1452 \r
1453                 ulBufferLength = ( unsigned long ) ( pcTraceBuffer - pcTraceBufferStart );\r
1454 \r
1455                 return ulBufferLength;\r
1456         }\r
1457 \r
1458 #endif\r
1459 \r
1460 \r
1461 \r
1462 /*-----------------------------------------------------------\r
1463  * SCHEDULER INTERNALS AVAILABLE FOR PORTING PURPOSES\r
1464  * documented in task.h\r
1465  *----------------------------------------------------------*/\r
1466 \r
1467 \r
1468 void vTaskIncrementTick( void )\r
1469 {\r
1470 tskTCB * pxTCB;\r
1471 \r
1472         /* Called by the portable layer each time a tick interrupt occurs.\r
1473         Increments the tick then checks to see if the new tick value will cause any\r
1474         tasks to be unblocked. */\r
1475         if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )\r
1476         {\r
1477                 ++xTickCount;\r
1478                 if( xTickCount == ( portTickType ) 0 )\r
1479                 {\r
1480                         xList *pxTemp;\r
1481 \r
1482                         /* Tick count has overflowed so we need to swap the delay lists.\r
1483                         If there are any items in pxDelayedTaskList here then there is\r
1484                         an error! */\r
1485                         configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) );\r
1486                         \r
1487                         pxTemp = pxDelayedTaskList;\r
1488                         pxDelayedTaskList = pxOverflowDelayedTaskList;\r
1489                         pxOverflowDelayedTaskList = pxTemp;\r
1490                         xNumOfOverflows++;\r
1491         \r
1492                         if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )\r
1493                         {\r
1494                                 /* The new current delayed list is empty.  Set \r
1495                                 xNextTaskUnblockTime to the maximum possible value so it is \r
1496                                 extremely unlikely that the     \r
1497                                 if( xTickCount >= xNextTaskUnblockTime ) test will pass until \r
1498                                 there is an item in the delayed list. */\r
1499                                 xNextTaskUnblockTime = portMAX_DELAY;\r
1500                         }\r
1501                         else\r
1502                         {\r
1503                                 /* The new current delayed list is not empty, get the value of \r
1504                                 the item at the head of the delayed list.  This is the time at \r
1505                                 which the task at the head of the delayed list should be removed \r
1506                                 from the Blocked state. */\r
1507                                 pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );\r
1508                                 xNextTaskUnblockTime = listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) );\r
1509                         }\r
1510                 }\r
1511 \r
1512                 /* See if this tick has made a timeout expire. */\r
1513                 prvCheckDelayedTasks();\r
1514         }\r
1515         else\r
1516         {\r
1517                 ++uxMissedTicks;\r
1518 \r
1519                 /* The tick hook gets called at regular intervals, even if the\r
1520                 scheduler is locked. */\r
1521                 #if ( configUSE_TICK_HOOK == 1 )\r
1522                 {\r
1523                         vApplicationTickHook();\r
1524                 }\r
1525                 #endif\r
1526         }\r
1527 \r
1528         #if ( configUSE_TICK_HOOK == 1 )\r
1529         {\r
1530                 /* Guard against the tick hook being called when the missed tick\r
1531                 count is being unwound (when the scheduler is being unlocked. */\r
1532                 if( uxMissedTicks == ( unsigned portBASE_TYPE ) 0U )\r
1533                 {\r
1534                         vApplicationTickHook();\r
1535                 }\r
1536         }\r
1537         #endif\r
1538 \r
1539         traceTASK_INCREMENT_TICK( xTickCount );\r
1540 }\r
1541 /*-----------------------------------------------------------*/\r
1542 \r
1543 #if ( ( INCLUDE_vTaskCleanUpResources == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )\r
1544 \r
1545         void vTaskCleanUpResources( void )\r
1546         {\r
1547         unsigned short usQueue;\r
1548         volatile tskTCB *pxTCB;\r
1549 \r
1550                 usQueue = ( unsigned short ) uxTopUsedPriority + ( unsigned short ) 1;\r
1551 \r
1552                 /* Remove any TCB's from the ready queues. */\r
1553                 do\r
1554                 {\r
1555                         usQueue--;\r
1556 \r
1557                         while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ usQueue ] ) ) == pdFALSE )\r
1558                         {\r
1559                                 listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &( pxReadyTasksLists[ usQueue ] ) );\r
1560                                 vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );\r
1561 \r
1562                                 prvDeleteTCB( ( tskTCB * ) pxTCB );\r
1563                         }\r
1564                 }while( usQueue > ( unsigned short ) tskIDLE_PRIORITY );\r
1565 \r
1566                 /* Remove any TCB's from the delayed queue. */\r
1567                 while( listLIST_IS_EMPTY( &xDelayedTaskList1 ) == pdFALSE )\r
1568                 {\r
1569                         listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &xDelayedTaskList1 );\r
1570                         vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );\r
1571 \r
1572                         prvDeleteTCB( ( tskTCB * ) pxTCB );\r
1573                 }\r
1574 \r
1575                 /* Remove any TCB's from the overflow delayed queue. */\r
1576                 while( listLIST_IS_EMPTY( &xDelayedTaskList2 ) == pdFALSE )\r
1577                 {\r
1578                         listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &xDelayedTaskList2 );\r
1579                         vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );\r
1580 \r
1581                         prvDeleteTCB( ( tskTCB * ) pxTCB );\r
1582                 }\r
1583 \r
1584                 while( listLIST_IS_EMPTY( &xSuspendedTaskList ) == pdFALSE )\r
1585                 {\r
1586                         listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &xSuspendedTaskList );\r
1587                         vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );\r
1588 \r
1589                         prvDeleteTCB( ( tskTCB * ) pxTCB );\r
1590                 }\r
1591         }\r
1592 \r
1593 #endif\r
1594 /*-----------------------------------------------------------*/\r
1595 \r
1596 #if ( configUSE_APPLICATION_TASK_TAG == 1 )\r
1597 \r
1598         void vTaskSetApplicationTaskTag( xTaskHandle xTask, pdTASK_HOOK_CODE pxHookFunction )\r
1599         {\r
1600         tskTCB *xTCB;\r
1601 \r
1602                 /* If xTask is NULL then we are setting our own task hook. */\r
1603                 if( xTask == NULL )\r
1604                 {\r
1605                         xTCB = ( tskTCB * ) pxCurrentTCB;\r
1606                 }\r
1607                 else\r
1608                 {\r
1609                         xTCB = ( tskTCB * ) xTask;\r
1610                 }\r
1611 \r
1612                 /* Save the hook function in the TCB.  A critical section is required as\r
1613                 the value can be accessed from an interrupt. */\r
1614                 taskENTER_CRITICAL();\r
1615                         xTCB->pxTaskTag = pxHookFunction;\r
1616                 taskEXIT_CRITICAL();\r
1617         }\r
1618 \r
1619 #endif\r
1620 /*-----------------------------------------------------------*/\r
1621 \r
1622 #if ( configUSE_APPLICATION_TASK_TAG == 1 )\r
1623 \r
1624         pdTASK_HOOK_CODE xTaskGetApplicationTaskTag( xTaskHandle xTask )\r
1625         {\r
1626         tskTCB *xTCB;\r
1627         pdTASK_HOOK_CODE xReturn;\r
1628 \r
1629                 /* If xTask is NULL then we are setting our own task hook. */\r
1630                 if( xTask == NULL )\r
1631                 {\r
1632                         xTCB = ( tskTCB * ) pxCurrentTCB;\r
1633                 }\r
1634                 else\r
1635                 {\r
1636                         xTCB = ( tskTCB * ) xTask;\r
1637                 }\r
1638 \r
1639                 /* Save the hook function in the TCB.  A critical section is required as\r
1640                 the value can be accessed from an interrupt. */\r
1641                 taskENTER_CRITICAL();\r
1642                         xReturn = xTCB->pxTaskTag;\r
1643                 taskEXIT_CRITICAL();\r
1644 \r
1645                 return xReturn;\r
1646         }\r
1647 \r
1648 #endif\r
1649 /*-----------------------------------------------------------*/\r
1650 \r
1651 #if ( configUSE_APPLICATION_TASK_TAG == 1 )\r
1652 \r
1653         portBASE_TYPE xTaskCallApplicationTaskHook( xTaskHandle xTask, void *pvParameter )\r
1654         {\r
1655         tskTCB *xTCB;\r
1656         portBASE_TYPE xReturn;\r
1657 \r
1658                 /* If xTask is NULL then we are calling our own task hook. */\r
1659                 if( xTask == NULL )\r
1660                 {\r
1661                         xTCB = ( tskTCB * ) pxCurrentTCB;\r
1662                 }\r
1663                 else\r
1664                 {\r
1665                         xTCB = ( tskTCB * ) xTask;\r
1666                 }\r
1667 \r
1668                 if( xTCB->pxTaskTag != NULL )\r
1669                 {\r
1670                         xReturn = xTCB->pxTaskTag( pvParameter );\r
1671                 }\r
1672                 else\r
1673                 {\r
1674                         xReturn = pdFAIL;\r
1675                 }\r
1676 \r
1677                 return xReturn;\r
1678         }\r
1679 \r
1680 #endif\r
1681 /*-----------------------------------------------------------*/\r
1682 \r
1683 void vTaskSwitchContext( void )\r
1684 {\r
1685         if( uxSchedulerSuspended != ( unsigned portBASE_TYPE ) pdFALSE )\r
1686         {\r
1687                 /* The scheduler is currently suspended - do not allow a context\r
1688                 switch. */\r
1689                 xMissedYield = pdTRUE;\r
1690         }\r
1691         else\r
1692         {\r
1693                 traceTASK_SWITCHED_OUT();\r
1694         \r
1695                 #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
1696                 {\r
1697                         unsigned long ulTempCounter;\r
1698                         \r
1699                                 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE\r
1700                                         portALT_GET_RUN_TIME_COUNTER_VALUE( ulTempCounter );\r
1701                                 #else\r
1702                                         ulTempCounter = portGET_RUN_TIME_COUNTER_VALUE();\r
1703                                 #endif\r
1704         \r
1705                                 /* Add the amount of time the task has been running to the accumulated\r
1706                                 time so far.  The time the task started running was stored in\r
1707                                 ulTaskSwitchedInTime.  Note that there is no overflow protection here\r
1708                                 so count values are only valid until the timer overflows.  Generally\r
1709                                 this will be about 1 hour assuming a 1uS timer increment. */\r
1710                                 pxCurrentTCB->ulRunTimeCounter += ( ulTempCounter - ulTaskSwitchedInTime );\r
1711                                 ulTaskSwitchedInTime = ulTempCounter;\r
1712                 }\r
1713                 #endif\r
1714         \r
1715                 taskFIRST_CHECK_FOR_STACK_OVERFLOW();\r
1716                 taskSECOND_CHECK_FOR_STACK_OVERFLOW();\r
1717         \r
1718                 /* Find the highest priority queue that contains ready tasks. */\r
1719                 while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopReadyPriority ] ) ) )\r
1720                 {\r
1721                         configASSERT( uxTopReadyPriority );\r
1722                         --uxTopReadyPriority;\r
1723                 }\r
1724         \r
1725                 /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the tasks of the\r
1726                 same priority get an equal share of the processor time. */\r
1727                 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopReadyPriority ] ) );\r
1728         \r
1729                 traceTASK_SWITCHED_IN();\r
1730                 vWriteTraceToBuffer();\r
1731         }\r
1732 }\r
1733 /*-----------------------------------------------------------*/\r
1734 \r
1735 void vTaskPlaceOnEventList( const xList * const pxEventList, portTickType xTicksToWait )\r
1736 {\r
1737 portTickType xTimeToWake;\r
1738 \r
1739         configASSERT( pxEventList );\r
1740 \r
1741         /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE\r
1742         SCHEDULER SUSPENDED. */\r
1743 \r
1744         /* Place the event list item of the TCB in the appropriate event list.\r
1745         This is placed in the list in priority order so the highest priority task\r
1746         is the first to be woken by the event. */\r
1747         vListInsert( ( xList * ) pxEventList, ( xListItem * ) &( pxCurrentTCB->xEventListItem ) );\r
1748 \r
1749         /* We must remove ourselves from the ready list before adding ourselves\r
1750         to the blocked list as the same list item is used for both lists.  We have\r
1751         exclusive access to the ready lists as the scheduler is locked. */\r
1752         vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
1753 \r
1754 \r
1755         #if ( INCLUDE_vTaskSuspend == 1 )\r
1756         {\r
1757                 if( xTicksToWait == portMAX_DELAY )\r
1758                 {\r
1759                         /* Add ourselves to the suspended task list instead of a delayed task\r
1760                         list to ensure we are not woken by a timing event.  We will block\r
1761                         indefinitely. */\r
1762                         vListInsertEnd( ( xList * ) &xSuspendedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
1763                 }\r
1764                 else\r
1765                 {\r
1766                         /* Calculate the time at which the task should be woken if the event does\r
1767                         not occur.  This may overflow but this doesn't matter. */\r
1768                         xTimeToWake = xTickCount + xTicksToWait;\r
1769                         prvAddCurrentTaskToDelayedList( xTimeToWake );\r
1770                 }\r
1771         }\r
1772         #else\r
1773         {\r
1774                         /* Calculate the time at which the task should be woken if the event does\r
1775                         not occur.  This may overflow but this doesn't matter. */\r
1776                         xTimeToWake = xTickCount + xTicksToWait;\r
1777                         prvAddCurrentTaskToDelayedList( xTimeToWake );\r
1778         }\r
1779         #endif\r
1780 }\r
1781 /*-----------------------------------------------------------*/\r
1782 \r
1783 #if configUSE_TIMERS == 1\r
1784 \r
1785         void vTaskPlaceOnEventListRestricted( const xList * const pxEventList, portTickType xTicksToWait )\r
1786         {\r
1787         portTickType xTimeToWake;\r
1788 \r
1789                 configASSERT( pxEventList );\r
1790 \r
1791                 /* This function should not be called by application code hence the\r
1792                 'Restricted' in its name.  It is not part of the public API.  It is\r
1793                 designed for use by kernel code, and has special calling requirements -\r
1794                 it should be called from a critical section. */\r
1795 \r
1796         \r
1797                 /* Place the event list item of the TCB in the appropriate event list.\r
1798                 In this case it is assume that this is the only task that is going to\r
1799                 be waiting on this event list, so the faster vListInsertEnd() function\r
1800                 can be used in place of vListInsert. */\r
1801                 vListInsertEnd( ( xList * ) pxEventList, ( xListItem * ) &( pxCurrentTCB->xEventListItem ) );\r
1802 \r
1803                 /* We must remove this task from the ready list before adding it to the\r
1804                 blocked list as the same list item is used for both lists.  This\r
1805                 function is called form a critical section. */\r
1806                 vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
1807 \r
1808                 /* Calculate the time at which the task should be woken if the event does\r
1809                 not occur.  This may overflow but this doesn't matter. */\r
1810                 xTimeToWake = xTickCount + xTicksToWait;\r
1811                 prvAddCurrentTaskToDelayedList( xTimeToWake );\r
1812         }\r
1813         \r
1814 #endif /* configUSE_TIMERS */\r
1815 /*-----------------------------------------------------------*/\r
1816 \r
1817 signed portBASE_TYPE xTaskRemoveFromEventList( const xList * const pxEventList )\r
1818 {\r
1819 tskTCB *pxUnblockedTCB;\r
1820 portBASE_TYPE xReturn;\r
1821 \r
1822         /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE\r
1823         SCHEDULER SUSPENDED.  It can also be called from within an ISR. */\r
1824 \r
1825         /* The event list is sorted in priority order, so we can remove the\r
1826         first in the list, remove the TCB from the delayed list, and add\r
1827         it to the ready list.\r
1828 \r
1829         If an event is for a queue that is locked then this function will never\r
1830         get called - the lock count on the queue will get modified instead.  This\r
1831         means we can always expect exclusive access to the event list here.\r
1832         \r
1833         This function assumes that a check has already been made to ensure that\r
1834         pxEventList is not empty. */\r
1835         pxUnblockedTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );\r
1836         configASSERT( pxUnblockedTCB );\r
1837         vListRemove( &( pxUnblockedTCB->xEventListItem ) );\r
1838 \r
1839         if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )\r
1840         {\r
1841                 vListRemove( &( pxUnblockedTCB->xGenericListItem ) );\r
1842                 prvAddTaskToReadyQueue( pxUnblockedTCB );\r
1843         }\r
1844         else\r
1845         {\r
1846                 /* We cannot access the delayed or ready lists, so will hold this\r
1847                 task pending until the scheduler is resumed. */\r
1848                 vListInsertEnd( ( xList * ) &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );\r
1849         }\r
1850 \r
1851         if( pxUnblockedTCB->uxPriority >= pxCurrentTCB->uxPriority )\r
1852         {\r
1853                 /* Return true if the task removed from the event list has\r
1854                 a higher priority than the calling task.  This allows\r
1855                 the calling task to know if it should force a context\r
1856                 switch now. */\r
1857                 xReturn = pdTRUE;\r
1858         }\r
1859         else\r
1860         {\r
1861                 xReturn = pdFALSE;\r
1862         }\r
1863 \r
1864         return xReturn;\r
1865 }\r
1866 /*-----------------------------------------------------------*/\r
1867 \r
1868 void vTaskSetTimeOutState( xTimeOutType * const pxTimeOut )\r
1869 {\r
1870         configASSERT( pxTimeOut );\r
1871         pxTimeOut->xOverflowCount = xNumOfOverflows;\r
1872         pxTimeOut->xTimeOnEntering = xTickCount;\r
1873 }\r
1874 /*-----------------------------------------------------------*/\r
1875 \r
1876 portBASE_TYPE xTaskCheckForTimeOut( xTimeOutType * const pxTimeOut, portTickType * const pxTicksToWait )\r
1877 {\r
1878 portBASE_TYPE xReturn;\r
1879 \r
1880         configASSERT( pxTimeOut );\r
1881         configASSERT( pxTicksToWait );\r
1882 \r
1883         taskENTER_CRITICAL();\r
1884         {\r
1885                 #if ( INCLUDE_vTaskSuspend == 1 )\r
1886                         /* If INCLUDE_vTaskSuspend is set to 1 and the block time specified is\r
1887                         the maximum block time then the task should block indefinitely, and\r
1888                         therefore never time out. */\r
1889                         if( *pxTicksToWait == portMAX_DELAY )\r
1890                         {\r
1891                                 xReturn = pdFALSE;\r
1892                         }\r
1893                         else /* We are not blocking indefinitely, perform the checks below. */\r
1894                 #endif\r
1895 \r
1896                 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( ( portTickType ) xTickCount >= ( portTickType ) pxTimeOut->xTimeOnEntering ) )\r
1897                 {\r
1898                         /* The tick count is greater than the time at which vTaskSetTimeout()\r
1899                         was called, but has also overflowed since vTaskSetTimeOut() was called.\r
1900                         It must have wrapped all the way around and gone past us again. This\r
1901                         passed since vTaskSetTimeout() was called. */\r
1902                         xReturn = pdTRUE;\r
1903                 }\r
1904                 else if( ( ( portTickType ) ( ( portTickType ) xTickCount - ( portTickType ) pxTimeOut->xTimeOnEntering ) ) < ( portTickType ) *pxTicksToWait )\r
1905                 {\r
1906                         /* Not a genuine timeout. Adjust parameters for time remaining. */\r
1907                         *pxTicksToWait -= ( ( portTickType ) xTickCount - ( portTickType ) pxTimeOut->xTimeOnEntering );\r
1908                         vTaskSetTimeOutState( pxTimeOut );\r
1909                         xReturn = pdFALSE;\r
1910                 }\r
1911                 else\r
1912                 {\r
1913                         xReturn = pdTRUE;\r
1914                 }\r
1915         }\r
1916         taskEXIT_CRITICAL();\r
1917 \r
1918         return xReturn;\r
1919 }\r
1920 /*-----------------------------------------------------------*/\r
1921 \r
1922 void vTaskMissedYield( void )\r
1923 {\r
1924         xMissedYield = pdTRUE;\r
1925 }\r
1926 \r
1927 /*\r
1928  * -----------------------------------------------------------\r
1929  * The Idle task.\r
1930  * ----------------------------------------------------------\r
1931  *\r
1932  * The portTASK_FUNCTION() macro is used to allow port/compiler specific\r
1933  * language extensions.  The equivalent prototype for this function is:\r
1934  *\r
1935  * void prvIdleTask( void *pvParameters );\r
1936  *\r
1937  */\r
1938 static portTASK_FUNCTION( prvIdleTask, pvParameters )\r
1939 {\r
1940         /* Stop warnings. */\r
1941         ( void ) pvParameters;\r
1942 \r
1943         for( ;; )\r
1944         {\r
1945                 /* See if any tasks have been deleted. */\r
1946                 prvCheckTasksWaitingTermination();\r
1947 \r
1948                 #if ( configUSE_PREEMPTION == 0 )\r
1949                 {\r
1950                         /* If we are not using preemption we keep forcing a task switch to\r
1951                         see if any other task has become available.  If we are using\r
1952                         preemption we don't need to do this as any task becoming available\r
1953                         will automatically get the processor anyway. */\r
1954                         taskYIELD();\r
1955                 }\r
1956                 #endif\r
1957 \r
1958                 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )\r
1959                 {\r
1960                         /* When using preemption tasks of equal priority will be\r
1961                         timesliced.  If a task that is sharing the idle priority is ready\r
1962                         to run then the idle task should yield before the end of the\r
1963                         timeslice.\r
1964 \r
1965                         A critical region is not required here as we are just reading from\r
1966                         the list, and an occasional incorrect value will not matter.  If\r
1967                         the ready list at the idle priority contains more than one task\r
1968                         then a task other than the idle task is ready to execute. */\r
1969                         if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( unsigned portBASE_TYPE ) 1 )\r
1970                         {\r
1971                                 taskYIELD();\r
1972                         }\r
1973                 }\r
1974                 #endif\r
1975 \r
1976                 #if ( configUSE_IDLE_HOOK == 1 )\r
1977                 {\r
1978                         extern void vApplicationIdleHook( void );\r
1979 \r
1980                         /* Call the user defined function from within the idle task.  This\r
1981                         allows the application designer to add background functionality\r
1982                         without the overhead of a separate task.\r
1983                         NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,\r
1984                         CALL A FUNCTION THAT MIGHT BLOCK. */\r
1985                         vApplicationIdleHook();\r
1986                 }\r
1987                 #endif\r
1988         }\r
1989 } /*lint !e715 pvParameters is not accessed but all task functions require the same prototype. */\r
1990 \r
1991 \r
1992 \r
1993 \r
1994 \r
1995 \r
1996 \r
1997 /*-----------------------------------------------------------\r
1998  * File private functions documented at the top of the file.\r
1999  *----------------------------------------------------------*/\r
2000 \r
2001 \r
2002 \r
2003 static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed char * const pcName, unsigned portBASE_TYPE uxPriority, const xMemoryRegion * const xRegions, unsigned short usStackDepth )\r
2004 {\r
2005         /* Store the function name in the TCB. */\r
2006         #if configMAX_TASK_NAME_LEN > 1\r
2007         {\r
2008                 /* Don't bring strncpy into the build unnecessarily. */\r
2009                 strncpy( ( char * ) pxTCB->pcTaskName, ( const char * ) pcName, ( unsigned short ) configMAX_TASK_NAME_LEN );\r
2010         }\r
2011         #endif\r
2012         pxTCB->pcTaskName[ ( unsigned short ) configMAX_TASK_NAME_LEN - ( unsigned short ) 1 ] = ( signed char ) '\0';\r
2013 \r
2014         /* This is used as an array index so must ensure it's not too large.  First\r
2015         remove the privilege bit if one is present. */\r
2016         if( uxPriority >= configMAX_PRIORITIES )\r
2017         {\r
2018                 uxPriority = configMAX_PRIORITIES - ( unsigned portBASE_TYPE ) 1U;\r
2019         }\r
2020 \r
2021         pxTCB->uxPriority = uxPriority;\r
2022         #if ( configUSE_MUTEXES == 1 )\r
2023         {\r
2024                 pxTCB->uxBasePriority = uxPriority;\r
2025         }\r
2026         #endif\r
2027 \r
2028         vListInitialiseItem( &( pxTCB->xGenericListItem ) );\r
2029         vListInitialiseItem( &( pxTCB->xEventListItem ) );\r
2030 \r
2031         /* Set the pxTCB as a link back from the xListItem.  This is so we can get\r
2032         back to the containing TCB from a generic item in a list. */\r
2033         listSET_LIST_ITEM_OWNER( &( pxTCB->xGenericListItem ), pxTCB );\r
2034 \r
2035         /* Event lists are always in priority order. */\r
2036         listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );\r
2037         listSET_LIST_ITEM_OWNER( &( pxTCB->xEventListItem ), pxTCB );\r
2038 \r
2039         #if ( portCRITICAL_NESTING_IN_TCB == 1 )\r
2040         {\r
2041                 pxTCB->uxCriticalNesting = ( unsigned portBASE_TYPE ) 0;\r
2042         }\r
2043         #endif\r
2044 \r
2045         #if ( configUSE_APPLICATION_TASK_TAG == 1 )\r
2046         {\r
2047                 pxTCB->pxTaskTag = NULL;\r
2048         }\r
2049         #endif\r
2050 \r
2051         #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
2052         {\r
2053                 pxTCB->ulRunTimeCounter = 0UL;\r
2054         }\r
2055         #endif\r
2056 \r
2057         #if ( portUSING_MPU_WRAPPERS == 1 )\r
2058         {\r
2059                 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, pxTCB->pxStack, usStackDepth );\r
2060         }\r
2061         #else\r
2062         {\r
2063                 ( void ) xRegions;\r
2064                 ( void ) usStackDepth;\r
2065         }\r
2066         #endif\r
2067 }\r
2068 /*-----------------------------------------------------------*/\r
2069 \r
2070 #if ( portUSING_MPU_WRAPPERS == 1 )\r
2071 \r
2072         void vTaskAllocateMPURegions( xTaskHandle xTaskToModify, const xMemoryRegion * const xRegions )\r
2073         {\r
2074         tskTCB *pxTCB;\r
2075         \r
2076                 if( xTaskToModify == pxCurrentTCB )\r
2077                 {\r
2078                         xTaskToModify = NULL;\r
2079                 }\r
2080 \r
2081                 /* If null is passed in here then we are deleting ourselves. */\r
2082                 pxTCB = prvGetTCBFromHandle( xTaskToModify );\r
2083 \r
2084         vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );\r
2085         }\r
2086         /*-----------------------------------------------------------*/\r
2087 #endif\r
2088 \r
2089 static void prvInitialiseTaskLists( void )\r
2090 {\r
2091 unsigned portBASE_TYPE uxPriority;\r
2092 \r
2093         for( uxPriority = ( unsigned portBASE_TYPE ) 0U; uxPriority < configMAX_PRIORITIES; uxPriority++ )\r
2094         {\r
2095                 vListInitialise( ( xList * ) &( pxReadyTasksLists[ uxPriority ] ) );\r
2096         }\r
2097 \r
2098         vListInitialise( ( xList * ) &xDelayedTaskList1 );\r
2099         vListInitialise( ( xList * ) &xDelayedTaskList2 );\r
2100         vListInitialise( ( xList * ) &xPendingReadyList );\r
2101 \r
2102         #if ( INCLUDE_vTaskDelete == 1 )\r
2103         {\r
2104                 vListInitialise( ( xList * ) &xTasksWaitingTermination );\r
2105         }\r
2106         #endif\r
2107 \r
2108         #if ( INCLUDE_vTaskSuspend == 1 )\r
2109         {\r
2110                 vListInitialise( ( xList * ) &xSuspendedTaskList );\r
2111         }\r
2112         #endif\r
2113 \r
2114         /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList\r
2115         using list2. */\r
2116         pxDelayedTaskList = &xDelayedTaskList1;\r
2117         pxOverflowDelayedTaskList = &xDelayedTaskList2;\r
2118 }\r
2119 /*-----------------------------------------------------------*/\r
2120 \r
2121 static void prvCheckTasksWaitingTermination( void )\r
2122 {\r
2123         #if ( INCLUDE_vTaskDelete == 1 )\r
2124         {\r
2125                 portBASE_TYPE xListIsEmpty;\r
2126 \r
2127                 /* ucTasksDeleted is used to prevent vTaskSuspendAll() being called\r
2128                 too often in the idle task. */\r
2129                 if( uxTasksDeleted > ( unsigned portBASE_TYPE ) 0 )\r
2130                 {\r
2131                         vTaskSuspendAll();\r
2132                                 xListIsEmpty = listLIST_IS_EMPTY( &xTasksWaitingTermination );\r
2133                         xTaskResumeAll();\r
2134 \r
2135                         if( xListIsEmpty == pdFALSE )\r
2136                         {\r
2137                                 tskTCB *pxTCB;\r
2138 \r
2139                                 taskENTER_CRITICAL();\r
2140                                 {\r
2141                                         pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( ( ( xList * ) &xTasksWaitingTermination ) );\r
2142                                         vListRemove( &( pxTCB->xGenericListItem ) );\r
2143                                         --uxCurrentNumberOfTasks;\r
2144                                         --uxTasksDeleted;\r
2145                                 }\r
2146                                 taskEXIT_CRITICAL();\r
2147 \r
2148                                 prvDeleteTCB( pxTCB );\r
2149                         }\r
2150                 }\r
2151         }\r
2152         #endif\r
2153 }\r
2154 /*-----------------------------------------------------------*/\r
2155 \r
2156 static void prvAddCurrentTaskToDelayedList( portTickType xTimeToWake )\r
2157 {\r
2158         /* The list item will be inserted in wake time order. */\r
2159         listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );\r
2160 \r
2161         if( xTimeToWake < xTickCount )\r
2162         {\r
2163                 /* Wake time has overflowed.  Place this item in the overflow list. */\r
2164                 vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
2165         }\r
2166         else\r
2167         {\r
2168                 /* The wake time has not overflowed, so we can use the current block list. */\r
2169                 vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
2170 \r
2171                 /* If the task entering the blocked state was placed at the head of the\r
2172                 list of blocked tasks then xNextTaskUnblockTime needs to be updated\r
2173                 too. */\r
2174                 if( xTimeToWake < xNextTaskUnblockTime )\r
2175                 {\r
2176                         xNextTaskUnblockTime = xTimeToWake;\r
2177                 }\r
2178         }\r
2179 }\r
2180 /*-----------------------------------------------------------*/\r
2181 \r
2182 static tskTCB *prvAllocateTCBAndStack( unsigned short usStackDepth, portSTACK_TYPE *puxStackBuffer )\r
2183 {\r
2184 tskTCB *pxNewTCB;\r
2185 \r
2186         /* Allocate space for the TCB.  Where the memory comes from depends on\r
2187         the implementation of the port malloc function. */\r
2188         pxNewTCB = ( tskTCB * ) pvPortMalloc( sizeof( tskTCB ) );\r
2189 \r
2190         if( pxNewTCB != NULL )\r
2191         {\r
2192                 /* Allocate space for the stack used by the task being created.\r
2193                 The base of the stack memory stored in the TCB so the task can\r
2194                 be deleted later if required. */\r
2195                 pxNewTCB->pxStack = ( portSTACK_TYPE * ) pvPortMallocAligned( ( ( ( size_t )usStackDepth ) * sizeof( portSTACK_TYPE ) ), puxStackBuffer );\r
2196 \r
2197                 if( pxNewTCB->pxStack == NULL )\r
2198                 {\r
2199                         /* Could not allocate the stack.  Delete the allocated TCB. */\r
2200                         vPortFree( pxNewTCB );\r
2201                         pxNewTCB = NULL;\r
2202                 }\r
2203                 else\r
2204                 {\r
2205                         /* Just to help debugging. */\r
2206                         memset( pxNewTCB->pxStack, tskSTACK_FILL_BYTE, usStackDepth * sizeof( portSTACK_TYPE ) );\r
2207                 }\r
2208         }\r
2209 \r
2210         return pxNewTCB;\r
2211 }\r
2212 /*-----------------------------------------------------------*/\r
2213 \r
2214 #if ( configUSE_TRACE_FACILITY == 1 )\r
2215 \r
2216         static void prvListTaskWithinSingleList( const signed char *pcWriteBuffer, xList *pxList, signed char cStatus )\r
2217         {\r
2218         volatile tskTCB *pxNextTCB, *pxFirstTCB;\r
2219         unsigned short usStackRemaining;\r
2220 \r
2221                 /* Write the details of all the TCB's in pxList into the buffer. */\r
2222                 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );\r
2223                 do\r
2224                 {\r
2225                         listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );\r
2226                         #if ( portSTACK_GROWTH > 0 )\r
2227                         {\r
2228                                 usStackRemaining = usTaskCheckFreeStackSpace( ( unsigned char * ) pxNextTCB->pxEndOfStack );\r
2229                         }\r
2230                         #else\r
2231                         {\r
2232                                 usStackRemaining = usTaskCheckFreeStackSpace( ( unsigned char * ) pxNextTCB->pxStack );\r
2233                         }\r
2234                         #endif                  \r
2235                         \r
2236                         sprintf( pcStatusString, ( char * ) "%s\t\t%c\t%u\t%u\t%u\r\n", pxNextTCB->pcTaskName, cStatus, ( unsigned int ) pxNextTCB->uxPriority, usStackRemaining, ( unsigned int ) pxNextTCB->uxTCBNumber );\r
2237                         strcat( ( char * ) pcWriteBuffer, ( char * ) pcStatusString );\r
2238 \r
2239                 } while( pxNextTCB != pxFirstTCB );\r
2240         }\r
2241 \r
2242 #endif\r
2243 /*-----------------------------------------------------------*/\r
2244 \r
2245 #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
2246 \r
2247         static void prvGenerateRunTimeStatsForTasksInList( const signed char *pcWriteBuffer, xList *pxList, unsigned long ulTotalRunTime )\r
2248         {\r
2249         volatile tskTCB *pxNextTCB, *pxFirstTCB;\r
2250         unsigned long ulStatsAsPercentage;\r
2251 \r
2252                 /* Write the run time stats of all the TCB's in pxList into the buffer. */\r
2253                 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );\r
2254                 do\r
2255                 {\r
2256                         /* Get next TCB in from the list. */\r
2257                         listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );\r
2258 \r
2259                         /* Divide by zero check. */\r
2260                         if( ulTotalRunTime > 0UL )\r
2261                         {\r
2262                                 /* Has the task run at all? */\r
2263                                 if( pxNextTCB->ulRunTimeCounter == 0 )\r
2264                                 {\r
2265                                         /* The task has used no CPU time at all. */\r
2266                                         sprintf( pcStatsString, ( char * ) "%s\t\t0\t\t0%%\r\n", pxNextTCB->pcTaskName );\r
2267                                 }\r
2268                                 else\r
2269                                 {\r
2270                                         /* What percentage of the total run time has the task used?\r
2271                                         This will always be rounded down to the nearest integer.\r
2272                                         ulTotalRunTime has already been divided by 100. */\r
2273                                         ulStatsAsPercentage = pxNextTCB->ulRunTimeCounter / ulTotalRunTime;\r
2274 \r
2275                                         if( ulStatsAsPercentage > 0UL )\r
2276                                         {\r
2277                                                 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED\r
2278                                                 {\r
2279                                                         sprintf( pcStatsString, ( char * ) "%s\t\t%lu\t\t%lu%%\r\n", pxNextTCB->pcTaskName, pxNextTCB->ulRunTimeCounter, ulStatsAsPercentage );                                                 \r
2280                                                 }\r
2281                                                 #else\r
2282                                                 {\r
2283                                                         /* sizeof( int ) == sizeof( long ) so a smaller\r
2284                                                         printf() library can be used. */\r
2285                                                         sprintf( pcStatsString, ( char * ) "%s\t\t%u\t\t%u%%\r\n", pxNextTCB->pcTaskName, ( unsigned int ) pxNextTCB->ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage );\r
2286                                                 }\r
2287                                                 #endif\r
2288                                         }\r
2289                                         else\r
2290                                         {\r
2291                                                 /* If the percentage is zero here then the task has\r
2292                                                 consumed less than 1% of the total run time. */\r
2293                                                 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED\r
2294                                                 {\r
2295                                                         sprintf( pcStatsString, ( char * ) "%s\t\t%lu\t\t<1%%\r\n", pxNextTCB->pcTaskName, pxNextTCB->ulRunTimeCounter );                                                       \r
2296                                                 }\r
2297                                                 #else\r
2298                                                 {\r
2299                                                         /* sizeof( int ) == sizeof( long ) so a smaller\r
2300                                                         printf() library can be used. */\r
2301                                                         sprintf( pcStatsString, ( char * ) "%s\t\t%u\t\t<1%%\r\n", pxNextTCB->pcTaskName, ( unsigned int ) pxNextTCB->ulRunTimeCounter );\r
2302                                                 }\r
2303                                                 #endif\r
2304                                         }\r
2305                                 }\r
2306 \r
2307                                 strcat( ( char * ) pcWriteBuffer, ( char * ) pcStatsString );\r
2308                         }\r
2309 \r
2310                 } while( pxNextTCB != pxFirstTCB );\r
2311         }\r
2312 \r
2313 #endif\r
2314 /*-----------------------------------------------------------*/\r
2315 \r
2316 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )\r
2317 \r
2318         static unsigned short usTaskCheckFreeStackSpace( const unsigned char * pucStackByte )\r
2319         {\r
2320         register unsigned short usCount = 0;\r
2321 \r
2322                 while( *pucStackByte == tskSTACK_FILL_BYTE )\r
2323                 {\r
2324                         pucStackByte -= portSTACK_GROWTH;\r
2325                         usCount++;\r
2326                 }\r
2327 \r
2328                 usCount /= sizeof( portSTACK_TYPE );\r
2329 \r
2330                 return usCount;\r
2331         }\r
2332 \r
2333 #endif\r
2334 /*-----------------------------------------------------------*/\r
2335 \r
2336 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )\r
2337 \r
2338         unsigned portBASE_TYPE uxTaskGetStackHighWaterMark( xTaskHandle xTask )\r
2339         {\r
2340         tskTCB *pxTCB;\r
2341         unsigned char *pcEndOfStack;\r
2342         unsigned portBASE_TYPE uxReturn;\r
2343 \r
2344                 pxTCB = prvGetTCBFromHandle( xTask );\r
2345 \r
2346                 #if portSTACK_GROWTH < 0\r
2347                 {\r
2348                         pcEndOfStack = ( unsigned char * ) pxTCB->pxStack;\r
2349                 }\r
2350                 #else\r
2351                 {\r
2352                         pcEndOfStack = ( unsigned char * ) pxTCB->pxEndOfStack;\r
2353                 }\r
2354                 #endif\r
2355 \r
2356                 uxReturn = ( unsigned portBASE_TYPE ) usTaskCheckFreeStackSpace( pcEndOfStack );\r
2357 \r
2358                 return uxReturn;\r
2359         }\r
2360 \r
2361 #endif\r
2362 /*-----------------------------------------------------------*/\r
2363 \r
2364 #if ( ( INCLUDE_vTaskDelete == 1 ) || ( INCLUDE_vTaskCleanUpResources == 1 ) )\r
2365 \r
2366         static void prvDeleteTCB( tskTCB *pxTCB )\r
2367         {\r
2368                 /* Free up the memory allocated by the scheduler for the task.  It is up to\r
2369                 the task to free any memory allocated at the application level. */\r
2370                 vPortFreeAligned( pxTCB->pxStack );\r
2371                 vPortFree( pxTCB );\r
2372         }\r
2373 \r
2374 #endif\r
2375 \r
2376 \r
2377 /*-----------------------------------------------------------*/\r
2378 \r
2379 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )\r
2380 \r
2381         xTaskHandle xTaskGetCurrentTaskHandle( void )\r
2382         {\r
2383         xTaskHandle xReturn;\r
2384 \r
2385                 /* A critical section is not required as this is not called from\r
2386                 an interrupt and the current TCB will always be the same for any\r
2387                 individual execution thread. */\r
2388                 xReturn = pxCurrentTCB;\r
2389 \r
2390                 return xReturn;\r
2391         }\r
2392 \r
2393 #endif\r
2394 \r
2395 /*-----------------------------------------------------------*/\r
2396 \r
2397 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )\r
2398 \r
2399         portBASE_TYPE xTaskGetSchedulerState( void )\r
2400         {\r
2401         portBASE_TYPE xReturn;\r
2402 \r
2403                 if( xSchedulerRunning == pdFALSE )\r
2404                 {\r
2405                         xReturn = taskSCHEDULER_NOT_STARTED;\r
2406                 }\r
2407                 else\r
2408                 {\r
2409                         if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )\r
2410                         {\r
2411                                 xReturn = taskSCHEDULER_RUNNING;\r
2412                         }\r
2413                         else\r
2414                         {\r
2415                                 xReturn = taskSCHEDULER_SUSPENDED;\r
2416                         }\r
2417                 }\r
2418 \r
2419                 return xReturn;\r
2420         }\r
2421 \r
2422 #endif\r
2423 /*-----------------------------------------------------------*/\r
2424 \r
2425 #if ( configUSE_MUTEXES == 1 )\r
2426 \r
2427         void vTaskPriorityInherit( xTaskHandle * const pxMutexHolder )\r
2428         {\r
2429         tskTCB * const pxTCB = ( tskTCB * ) pxMutexHolder;\r
2430 \r
2431                 configASSERT( pxMutexHolder );\r
2432 \r
2433                 if( pxTCB->uxPriority < pxCurrentTCB->uxPriority )\r
2434                 {\r
2435                         /* Adjust the mutex holder state to account for its new priority. */\r
2436                         listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) pxCurrentTCB->uxPriority );\r
2437 \r
2438                         /* If the task being modified is in the ready state it will need to\r
2439                         be moved in to a new list. */\r
2440                         if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxTCB->uxPriority ] ), &( pxTCB->xGenericListItem ) ) )\r
2441                         {\r
2442                                 vListRemove( &( pxTCB->xGenericListItem ) );\r
2443 \r
2444                                 /* Inherit the priority before being moved into the new list. */\r
2445                                 pxTCB->uxPriority = pxCurrentTCB->uxPriority;\r
2446                                 prvAddTaskToReadyQueue( pxTCB );\r
2447                         }\r
2448                         else\r
2449                         {\r
2450                                 /* Just inherit the priority. */\r
2451                                 pxTCB->uxPriority = pxCurrentTCB->uxPriority;\r
2452                         }\r
2453                 }\r
2454         }\r
2455 \r
2456 #endif\r
2457 /*-----------------------------------------------------------*/\r
2458 \r
2459 #if ( configUSE_MUTEXES == 1 )\r
2460 \r
2461         void vTaskPriorityDisinherit( xTaskHandle * const pxMutexHolder )\r
2462         {\r
2463         tskTCB * const pxTCB = ( tskTCB * ) pxMutexHolder;\r
2464 \r
2465                 if( pxMutexHolder != NULL )\r
2466                 {\r
2467                         if( pxTCB->uxPriority != pxTCB->uxBasePriority )\r
2468                         {\r
2469                                 /* We must be the running task to be able to give the mutex back.\r
2470                                 Remove ourselves from the ready list we currently appear in. */\r
2471                                 vListRemove( &( pxTCB->xGenericListItem ) );\r
2472 \r
2473                                 /* Disinherit the priority before adding ourselves into the new\r
2474                                 ready list. */\r
2475                                 pxTCB->uxPriority = pxTCB->uxBasePriority;\r
2476                                 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) pxTCB->uxPriority );\r
2477                                 prvAddTaskToReadyQueue( pxTCB );\r
2478                         }\r
2479                 }\r
2480         }\r
2481 \r
2482 #endif\r
2483 /*-----------------------------------------------------------*/\r
2484 \r
2485 #if ( portCRITICAL_NESTING_IN_TCB == 1 )\r
2486 \r
2487         void vTaskEnterCritical( void )\r
2488         {\r
2489                 portDISABLE_INTERRUPTS();\r
2490 \r
2491                 if( xSchedulerRunning != pdFALSE )\r
2492                 {\r
2493                         ( pxCurrentTCB->uxCriticalNesting )++;\r
2494                 }\r
2495         }\r
2496 \r
2497 #endif\r
2498 /*-----------------------------------------------------------*/\r
2499 \r
2500 #if ( portCRITICAL_NESTING_IN_TCB == 1 )\r
2501 \r
2502 void vTaskExitCritical( void )\r
2503 {\r
2504         if( xSchedulerRunning != pdFALSE )\r
2505         {\r
2506                 if( pxCurrentTCB->uxCriticalNesting > 0 )\r
2507                 {\r
2508                         ( pxCurrentTCB->uxCriticalNesting )--;\r
2509 \r
2510                         if( pxCurrentTCB->uxCriticalNesting == 0 )\r
2511                         {\r
2512                                 portENABLE_INTERRUPTS();\r
2513                         }\r
2514                 }\r
2515         }\r
2516 }\r
2517 \r
2518 #endif\r
2519 /*-----------------------------------------------------------*/\r
2520 \r
2521 \r
2522 \r
2523 \r