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