]> git.sur5r.net Git - freertos/blob - Demo/CORTEX_M4_ATSAM4S_Atmel_Studio/src/asf/thirdparty/FreeRTOS/tasks.c
Start to re-arrange files to include FreeRTOS+ in main download.
[freertos] / Demo / CORTEX_M4_ATSAM4S_Atmel_Studio / src / asf / thirdparty / FreeRTOS / tasks.c
1 /*\r
2     FreeRTOS V7.1.1 - 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 < 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_uxTaskPriorityGet == 1 )\r
737 \r
738         unsigned portBASE_TYPE uxTaskPriorityGet( xTaskHandle pxTask )\r
739         {\r
740         tskTCB *pxTCB;\r
741         unsigned portBASE_TYPE uxReturn;\r
742 \r
743                 taskENTER_CRITICAL();\r
744                 {\r
745                         /* If null is passed in here then we are changing the\r
746                         priority of the calling function. */\r
747                         pxTCB = prvGetTCBFromHandle( pxTask );\r
748                         uxReturn = pxTCB->uxPriority;\r
749                 }\r
750                 taskEXIT_CRITICAL();\r
751 \r
752                 return uxReturn;\r
753         }\r
754 \r
755 #endif\r
756 /*-----------------------------------------------------------*/\r
757 \r
758 #if ( INCLUDE_vTaskPrioritySet == 1 )\r
759 \r
760         void vTaskPrioritySet( xTaskHandle pxTask, unsigned portBASE_TYPE uxNewPriority )\r
761         {\r
762         tskTCB *pxTCB;\r
763         unsigned portBASE_TYPE uxCurrentPriority;\r
764         portBASE_TYPE xYieldRequired = pdFALSE;\r
765 \r
766                 configASSERT( ( uxNewPriority < configMAX_PRIORITIES ) );\r
767 \r
768                 /* Ensure the new priority is valid. */\r
769                 if( uxNewPriority >= configMAX_PRIORITIES )\r
770                 {\r
771                         uxNewPriority = configMAX_PRIORITIES - ( unsigned portBASE_TYPE ) 1U;\r
772                 }\r
773 \r
774                 taskENTER_CRITICAL();\r
775                 {\r
776                         if( pxTask == pxCurrentTCB )\r
777                         {\r
778                                 pxTask = NULL;\r
779                         }\r
780 \r
781                         /* If null is passed in here then we are changing the\r
782                         priority of the calling function. */\r
783                         pxTCB = prvGetTCBFromHandle( pxTask );\r
784 \r
785                         traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );\r
786 \r
787                         #if ( configUSE_MUTEXES == 1 )\r
788                         {\r
789                                 uxCurrentPriority = pxTCB->uxBasePriority;\r
790                         }\r
791                         #else\r
792                         {\r
793                                 uxCurrentPriority = pxTCB->uxPriority;\r
794                         }\r
795                         #endif\r
796 \r
797                         if( uxCurrentPriority != uxNewPriority )\r
798                         {\r
799                                 /* The priority change may have readied a task of higher\r
800                                 priority than the calling task. */\r
801                                 if( uxNewPriority > uxCurrentPriority )\r
802                                 {\r
803                                         if( pxTask != NULL )\r
804                                         {\r
805                                                 /* The priority of another task is being raised.  If we\r
806                                                 were raising the priority of the currently running task\r
807                                                 there would be no need to switch as it must have already\r
808                                                 been the highest priority task. */\r
809                                                 xYieldRequired = pdTRUE;\r
810                                         }\r
811                                 }\r
812                                 else if( pxTask == NULL )\r
813                                 {\r
814                                         /* Setting our own priority down means there may now be another\r
815                                         task of higher priority that is ready to execute. */\r
816                                         xYieldRequired = pdTRUE;\r
817                                 }\r
818 \r
819 \r
820 \r
821                                 #if ( configUSE_MUTEXES == 1 )\r
822                                 {\r
823                                         /* Only change the priority being used if the task is not\r
824                                         currently using an inherited priority. */\r
825                                         if( pxTCB->uxBasePriority == pxTCB->uxPriority )\r
826                                         {\r
827                                                 pxTCB->uxPriority = uxNewPriority;\r
828                                         }\r
829 \r
830                                         /* The base priority gets set whatever. */\r
831                                         pxTCB->uxBasePriority = uxNewPriority;\r
832                                 }\r
833                                 #else\r
834                                 {\r
835                                         pxTCB->uxPriority = uxNewPriority;\r
836                                 }\r
837                                 #endif\r
838 \r
839                                 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( configMAX_PRIORITIES - ( portTickType ) uxNewPriority ) );\r
840 \r
841                                 /* If the task is in the blocked or suspended list we need do\r
842                                 nothing more than change it's priority variable. However, if\r
843                                 the task is in a ready list it needs to be removed and placed\r
844                                 in the queue appropriate to its new priority. */\r
845                                 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxCurrentPriority ] ), &( pxTCB->xGenericListItem ) ) )\r
846                                 {\r
847                                         /* The task is currently in its ready list - remove before adding\r
848                                         it to it's new ready list.  As we are in a critical section we\r
849                                         can do this even if the scheduler is suspended. */\r
850                                         vListRemove( &( pxTCB->xGenericListItem ) );\r
851                                         prvAddTaskToReadyQueue( pxTCB );\r
852                                 }\r
853 \r
854                                 if( xYieldRequired == pdTRUE )\r
855                                 {\r
856                                         portYIELD_WITHIN_API();\r
857                                 }\r
858                         }\r
859                 }\r
860                 taskEXIT_CRITICAL();\r
861         }\r
862 \r
863 #endif\r
864 /*-----------------------------------------------------------*/\r
865 \r
866 #if ( INCLUDE_vTaskSuspend == 1 )\r
867 \r
868         void vTaskSuspend( xTaskHandle pxTaskToSuspend )\r
869         {\r
870         tskTCB *pxTCB;\r
871 \r
872                 taskENTER_CRITICAL();\r
873                 {\r
874                         /* Ensure a yield is performed if the current task is being\r
875                         suspended. */\r
876                         if( pxTaskToSuspend == pxCurrentTCB )\r
877                         {\r
878                                 pxTaskToSuspend = NULL;\r
879                         }\r
880 \r
881                         /* If null is passed in here then we are suspending ourselves. */\r
882                         pxTCB = prvGetTCBFromHandle( pxTaskToSuspend );\r
883 \r
884                         traceTASK_SUSPEND( pxTCB );\r
885 \r
886                         /* Remove task from the ready/delayed list and place in the     suspended list. */\r
887                         vListRemove( &( pxTCB->xGenericListItem ) );\r
888 \r
889                         /* Is the task waiting on an event also? */\r
890                         if( pxTCB->xEventListItem.pvContainer != NULL )\r
891                         {\r
892                                 vListRemove( &( pxTCB->xEventListItem ) );\r
893                         }\r
894 \r
895                         vListInsertEnd( ( xList * ) &xSuspendedTaskList, &( pxTCB->xGenericListItem ) );\r
896                 }\r
897                 taskEXIT_CRITICAL();\r
898 \r
899                 if( ( void * ) pxTaskToSuspend == NULL )\r
900                 {\r
901                         if( xSchedulerRunning != pdFALSE )\r
902                         {\r
903                                 /* We have just suspended the current task. */\r
904                                 portYIELD_WITHIN_API();\r
905                         }\r
906                         else\r
907                         {\r
908                                 /* The scheduler is not running, but the task that was pointed\r
909                                 to by pxCurrentTCB has just been suspended and pxCurrentTCB\r
910                                 must be adjusted to point to a different task. */\r
911                                 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks )\r
912                                 {\r
913                                         /* No other tasks are ready, so set pxCurrentTCB back to\r
914                                         NULL so when the next task is created pxCurrentTCB will\r
915                                         be set to point to it no matter what its relative priority\r
916                                         is. */\r
917                                         pxCurrentTCB = NULL;\r
918                                 }\r
919                                 else\r
920                                 {\r
921                                         vTaskSwitchContext();\r
922                                 }\r
923                         }\r
924                 }\r
925         }\r
926 \r
927 #endif\r
928 /*-----------------------------------------------------------*/\r
929 \r
930 #if ( INCLUDE_vTaskSuspend == 1 )\r
931 \r
932         signed portBASE_TYPE xTaskIsTaskSuspended( xTaskHandle xTask )\r
933         {\r
934         portBASE_TYPE xReturn = pdFALSE;\r
935         const tskTCB * const pxTCB = ( tskTCB * ) xTask;\r
936 \r
937                 /* It does not make sense to check if the calling task is suspended. */\r
938                 configASSERT( xTask );\r
939 \r
940                 /* Is the task we are attempting to resume actually in the\r
941                 suspended list? */\r
942                 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xGenericListItem ) ) != pdFALSE )\r
943                 {\r
944                         /* Has the task already been resumed from within an ISR? */\r
945                         if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdTRUE )\r
946                         {\r
947                                 /* Is it in the suspended list because it is in the\r
948                                 Suspended state?  It is possible to be in the suspended\r
949                                 list because it is blocked on a task with no timeout\r
950                                 specified. */\r
951                                 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) == pdTRUE )\r
952                                 {\r
953                                         xReturn = pdTRUE;\r
954                                 }\r
955                         }\r
956                 }\r
957 \r
958                 return xReturn;\r
959         }\r
960 \r
961 #endif\r
962 /*-----------------------------------------------------------*/\r
963 \r
964 #if ( INCLUDE_vTaskSuspend == 1 )\r
965 \r
966         void vTaskResume( xTaskHandle pxTaskToResume )\r
967         {\r
968         tskTCB *pxTCB;\r
969 \r
970                 /* It does not make sense to resume the calling task. */\r
971                 configASSERT( pxTaskToResume );\r
972 \r
973                 /* Remove the task from whichever list it is currently in, and place\r
974                 it in the ready list. */\r
975                 pxTCB = ( tskTCB * ) pxTaskToResume;\r
976 \r
977                 /* The parameter cannot be NULL as it is impossible to resume the\r
978                 currently executing task. */\r
979                 if( ( pxTCB != NULL ) && ( pxTCB != pxCurrentTCB ) )\r
980                 {\r
981                         taskENTER_CRITICAL();\r
982                         {\r
983                                 if( xTaskIsTaskSuspended( pxTCB ) == pdTRUE )\r
984                                 {\r
985                                         traceTASK_RESUME( pxTCB );\r
986 \r
987                                         /* As we are in a critical section we can access the ready\r
988                                         lists even if the scheduler is suspended. */\r
989                                         vListRemove(  &( pxTCB->xGenericListItem ) );\r
990                                         prvAddTaskToReadyQueue( pxTCB );\r
991 \r
992                                         /* We may have just resumed a higher priority task. */\r
993                                         if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )\r
994                                         {\r
995                                                 /* This yield may not cause the task just resumed to run, but\r
996                                                 will leave the lists in the correct state for the next yield. */\r
997                                                 portYIELD_WITHIN_API();\r
998                                         }\r
999                                 }\r
1000                         }\r
1001                         taskEXIT_CRITICAL();\r
1002                 }\r
1003         }\r
1004 \r
1005 #endif\r
1006 \r
1007 /*-----------------------------------------------------------*/\r
1008 \r
1009 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )\r
1010 \r
1011         portBASE_TYPE xTaskResumeFromISR( xTaskHandle pxTaskToResume )\r
1012         {\r
1013         portBASE_TYPE xYieldRequired = pdFALSE;\r
1014         tskTCB *pxTCB;\r
1015         unsigned portBASE_TYPE uxSavedInterruptStatus;\r
1016 \r
1017                 configASSERT( pxTaskToResume );\r
1018 \r
1019                 pxTCB = ( tskTCB * ) pxTaskToResume;\r
1020 \r
1021                 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
1022                 {\r
1023                         if( xTaskIsTaskSuspended( pxTCB ) == pdTRUE )\r
1024                         {\r
1025                                 traceTASK_RESUME_FROM_ISR( pxTCB );\r
1026 \r
1027                                 if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )\r
1028                                 {\r
1029                                         xYieldRequired = ( pxTCB->uxPriority >= pxCurrentTCB->uxPriority );\r
1030                                         vListRemove(  &( pxTCB->xGenericListItem ) );\r
1031                                         prvAddTaskToReadyQueue( pxTCB );\r
1032                                 }\r
1033                                 else\r
1034                                 {\r
1035                                         /* We cannot access the delayed or ready lists, so will hold this\r
1036                                         task pending until the scheduler is resumed, at which point a\r
1037                                         yield will be performed if necessary. */\r
1038                                         vListInsertEnd( ( xList * ) &( xPendingReadyList ), &( pxTCB->xEventListItem ) );\r
1039                                 }\r
1040                         }\r
1041                 }\r
1042                 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );\r
1043 \r
1044                 return xYieldRequired;\r
1045         }\r
1046 \r
1047 #endif\r
1048 \r
1049 \r
1050 \r
1051 \r
1052 /*-----------------------------------------------------------\r
1053  * PUBLIC SCHEDULER CONTROL documented in task.h\r
1054  *----------------------------------------------------------*/\r
1055 \r
1056 \r
1057 void vTaskStartScheduler( void )\r
1058 {\r
1059 portBASE_TYPE xReturn;\r
1060 \r
1061         /* Add the idle task at the lowest priority. */\r
1062         #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )\r
1063         {\r
1064                 /* Create the idle task, storing its handle in xIdleTaskHandle so it can\r
1065                 be returned by the xTaskGetIdleTaskHandle() function. */\r
1066                 xReturn = xTaskCreate( prvIdleTask, ( signed char * ) "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), &xIdleTaskHandle );\r
1067         }\r
1068         #else\r
1069         {\r
1070                 /* Create the idle task without storing its handle. */\r
1071                 xReturn = xTaskCreate( prvIdleTask, ( signed char * ) "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), NULL );\r
1072         }\r
1073         #endif\r
1074 \r
1075         #if ( configUSE_TIMERS == 1 )\r
1076         {\r
1077                 if( xReturn == pdPASS )\r
1078                 {\r
1079                         xReturn = xTimerCreateTimerTask();\r
1080                 }\r
1081         }\r
1082         #endif\r
1083 \r
1084         if( xReturn == pdPASS )\r
1085         {\r
1086                 /* Interrupts are turned off here, to ensure a tick does not occur\r
1087                 before or during the call to xPortStartScheduler().  The stacks of\r
1088                 the created tasks contain a status word with interrupts switched on\r
1089                 so interrupts will automatically get re-enabled when the first task\r
1090                 starts to run.\r
1091 \r
1092                 STEPPING THROUGH HERE USING A DEBUGGER CAN CAUSE BIG PROBLEMS IF THE\r
1093                 DEBUGGER ALLOWS INTERRUPTS TO BE PROCESSED. */\r
1094                 portDISABLE_INTERRUPTS();\r
1095 \r
1096                 xSchedulerRunning = pdTRUE;\r
1097                 xTickCount = ( portTickType ) 0U;\r
1098 \r
1099                 /* If configGENERATE_RUN_TIME_STATS is defined then the following\r
1100                 macro must be defined to configure the timer/counter used to generate\r
1101                 the run time counter time base. */\r
1102                 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();\r
1103                 \r
1104                 /* Setting up the timer tick is hardware specific and thus in the\r
1105                 portable interface. */\r
1106                 if( xPortStartScheduler() != pdFALSE )\r
1107                 {\r
1108                         /* Should not reach here as if the scheduler is running the\r
1109                         function will not return. */\r
1110                 }\r
1111                 else\r
1112                 {\r
1113                         /* Should only reach here if a task calls xTaskEndScheduler(). */\r
1114                 }\r
1115         }\r
1116 \r
1117         /* This line will only be reached if the kernel could not be started. */\r
1118         configASSERT( xReturn );\r
1119 }\r
1120 /*-----------------------------------------------------------*/\r
1121 \r
1122 void vTaskEndScheduler( void )\r
1123 {\r
1124         /* Stop the scheduler interrupts and call the portable scheduler end\r
1125         routine so the original ISRs can be restored if necessary.  The port\r
1126         layer must ensure interrupts enable     bit is left in the correct state. */\r
1127         portDISABLE_INTERRUPTS();\r
1128         xSchedulerRunning = pdFALSE;\r
1129         vPortEndScheduler();\r
1130 }\r
1131 /*----------------------------------------------------------*/\r
1132 \r
1133 void vTaskSuspendAll( void )\r
1134 {\r
1135         /* A critical section is not required as the variable is of type\r
1136         portBASE_TYPE. */\r
1137         ++uxSchedulerSuspended;\r
1138 }\r
1139 /*----------------------------------------------------------*/\r
1140 \r
1141 signed portBASE_TYPE xTaskResumeAll( void )\r
1142 {\r
1143 register tskTCB *pxTCB;\r
1144 signed portBASE_TYPE xAlreadyYielded = pdFALSE;\r
1145 \r
1146         /* If uxSchedulerSuspended is zero then this function does not match a\r
1147         previous call to vTaskSuspendAll(). */\r
1148         configASSERT( uxSchedulerSuspended );\r
1149 \r
1150         /* It is possible that an ISR caused a task to be removed from an event\r
1151         list while the scheduler was suspended.  If this was the case then the\r
1152         removed task will have been added to the xPendingReadyList.  Once the\r
1153         scheduler has been resumed it is safe to move all the pending ready\r
1154         tasks from this list into their appropriate ready list. */\r
1155         taskENTER_CRITICAL();\r
1156         {\r
1157                 --uxSchedulerSuspended;\r
1158 \r
1159                 if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )\r
1160                 {\r
1161                         if( uxCurrentNumberOfTasks > ( unsigned portBASE_TYPE ) 0U )\r
1162                         {\r
1163                                 portBASE_TYPE xYieldRequired = pdFALSE;\r
1164 \r
1165                                 /* Move any readied tasks from the pending list into the\r
1166                                 appropriate ready list. */\r
1167                                 while( listLIST_IS_EMPTY( ( xList * ) &xPendingReadyList ) == pdFALSE )\r
1168                                 {\r
1169                                         pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY(  ( ( xList * ) &xPendingReadyList ) );\r
1170                                         vListRemove( &( pxTCB->xEventListItem ) );\r
1171                                         vListRemove( &( pxTCB->xGenericListItem ) );\r
1172                                         prvAddTaskToReadyQueue( pxTCB );\r
1173 \r
1174                                         /* If we have moved a task that has a priority higher than\r
1175                                         the current task then we should yield. */\r
1176                                         if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )\r
1177                                         {\r
1178                                                 xYieldRequired = pdTRUE;\r
1179                                         }\r
1180                                 }\r
1181 \r
1182                                 /* If any ticks occurred while the scheduler was suspended then\r
1183                                 they should be processed now.  This ensures the tick count does not\r
1184                                 slip, and that any delayed tasks are resumed at the correct time. */\r
1185                                 if( uxMissedTicks > ( unsigned portBASE_TYPE ) 0U )\r
1186                                 {\r
1187                                         while( uxMissedTicks > ( unsigned portBASE_TYPE ) 0U )\r
1188                                         {\r
1189                                                 vTaskIncrementTick();\r
1190                                                 --uxMissedTicks;\r
1191                                         }\r
1192 \r
1193                                         /* As we have processed some ticks it is appropriate to yield\r
1194                                         to ensure the highest priority task that is ready to run is\r
1195                                         the task actually running. */\r
1196                                         #if configUSE_PREEMPTION == 1\r
1197                                         {\r
1198                                                 xYieldRequired = pdTRUE;\r
1199                                         }\r
1200                                         #endif\r
1201                                 }\r
1202 \r
1203                                 if( ( xYieldRequired == pdTRUE ) || ( xMissedYield == pdTRUE ) )\r
1204                                 {\r
1205                                         xAlreadyYielded = pdTRUE;\r
1206                                         xMissedYield = pdFALSE;\r
1207                                         portYIELD_WITHIN_API();\r
1208                                 }\r
1209                         }\r
1210                 }\r
1211         }\r
1212         taskEXIT_CRITICAL();\r
1213 \r
1214         return xAlreadyYielded;\r
1215 }\r
1216 \r
1217 \r
1218 \r
1219 \r
1220 \r
1221 \r
1222 /*-----------------------------------------------------------\r
1223  * PUBLIC TASK UTILITIES documented in task.h\r
1224  *----------------------------------------------------------*/\r
1225 \r
1226 \r
1227 \r
1228 portTickType xTaskGetTickCount( void )\r
1229 {\r
1230 portTickType xTicks;\r
1231 \r
1232         /* Critical section required if running on a 16 bit processor. */\r
1233         taskENTER_CRITICAL();\r
1234         {\r
1235                 xTicks = xTickCount;\r
1236         }\r
1237         taskEXIT_CRITICAL();\r
1238 \r
1239         return xTicks;\r
1240 }\r
1241 /*-----------------------------------------------------------*/\r
1242 \r
1243 portTickType xTaskGetTickCountFromISR( void )\r
1244 {\r
1245 portTickType xReturn;\r
1246 unsigned portBASE_TYPE uxSavedInterruptStatus;\r
1247 \r
1248         uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();\r
1249         xReturn = xTickCount;\r
1250         portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );\r
1251 \r
1252         return xReturn;\r
1253 }\r
1254 /*-----------------------------------------------------------*/\r
1255 \r
1256 unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )\r
1257 {\r
1258         /* A critical section is not required because the variables are of type\r
1259         portBASE_TYPE. */\r
1260         return uxCurrentNumberOfTasks;\r
1261 }\r
1262 /*-----------------------------------------------------------*/\r
1263 \r
1264 #if ( INCLUDE_pcTaskGetTaskName == 1 )\r
1265 \r
1266         signed char *pcTaskGetTaskName( xTaskHandle xTaskToQuery )\r
1267         {\r
1268         tskTCB *pxTCB;\r
1269 \r
1270                 /* If null is passed in here then the name of the calling task is being queried. */\r
1271                 pxTCB = prvGetTCBFromHandle( xTaskToQuery );\r
1272                 configASSERT( pxTCB );\r
1273                 return &( pxTCB->pcTaskName[ 0 ] );\r
1274         }\r
1275 \r
1276 #endif\r
1277 /*-----------------------------------------------------------*/\r
1278 \r
1279 #if ( configUSE_TRACE_FACILITY == 1 )\r
1280 \r
1281         void vTaskList( signed char *pcWriteBuffer )\r
1282         {\r
1283         unsigned portBASE_TYPE uxQueue;\r
1284 \r
1285                 /* This is a VERY costly function that should be used for debug only.\r
1286                 It leaves interrupts disabled for a LONG time. */\r
1287 \r
1288                 vTaskSuspendAll();\r
1289                 {\r
1290                         /* Run through all the lists that could potentially contain a TCB and\r
1291                         report the task name, state and stack high water mark. */\r
1292 \r
1293                         *pcWriteBuffer = ( signed char ) 0x00;\r
1294                         strcat( ( char * ) pcWriteBuffer, ( const char * ) "\r\n" );\r
1295 \r
1296                         uxQueue = uxTopUsedPriority + ( unsigned portBASE_TYPE ) 1U;\r
1297 \r
1298                         do\r
1299                         {\r
1300                                 uxQueue--;\r
1301 \r
1302                                 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) == pdFALSE )\r
1303                                 {\r
1304                                         prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), tskREADY_CHAR );\r
1305                                 }\r
1306                         }while( uxQueue > ( unsigned short ) tskIDLE_PRIORITY );\r
1307 \r
1308                         if( listLIST_IS_EMPTY( pxDelayedTaskList ) == pdFALSE )\r
1309                         {\r
1310                                 prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, tskBLOCKED_CHAR );\r
1311                         }\r
1312 \r
1313                         if( listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) == pdFALSE )\r
1314                         {\r
1315                                 prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, tskBLOCKED_CHAR );\r
1316                         }\r
1317 \r
1318                         #if( INCLUDE_vTaskDelete == 1 )\r
1319                         {\r
1320                                 if( listLIST_IS_EMPTY( &xTasksWaitingTermination ) == pdFALSE )\r
1321                                 {\r
1322                                         prvListTaskWithinSingleList( pcWriteBuffer, &xTasksWaitingTermination, tskDELETED_CHAR );\r
1323                                 }\r
1324                         }\r
1325                         #endif\r
1326 \r
1327                         #if ( INCLUDE_vTaskSuspend == 1 )\r
1328                         {\r
1329                                 if( listLIST_IS_EMPTY( &xSuspendedTaskList ) == pdFALSE )\r
1330                                 {\r
1331                                         prvListTaskWithinSingleList( pcWriteBuffer, &xSuspendedTaskList, tskSUSPENDED_CHAR );\r
1332                                 }\r
1333                         }\r
1334                         #endif\r
1335                 }\r
1336                 xTaskResumeAll();\r
1337         }\r
1338 \r
1339 #endif\r
1340 /*----------------------------------------------------------*/\r
1341 \r
1342 #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
1343 \r
1344         void vTaskGetRunTimeStats( signed char *pcWriteBuffer )\r
1345         {\r
1346         unsigned portBASE_TYPE uxQueue;\r
1347         unsigned long ulTotalRunTime;\r
1348 \r
1349                 /* This is a VERY costly function that should be used for debug only.\r
1350                 It leaves interrupts disabled for a LONG time. */\r
1351 \r
1352                 vTaskSuspendAll();\r
1353                 {\r
1354                         #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE\r
1355                                 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );\r
1356                         #else\r
1357                                 ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();\r
1358                         #endif\r
1359 \r
1360                         /* Divide ulTotalRunTime by 100 to make the percentage caluclations\r
1361                         simpler in the prvGenerateRunTimeStatsForTasksInList() function. */\r
1362                         ulTotalRunTime /= 100UL;\r
1363                         \r
1364                         /* Run through all the lists that could potentially contain a TCB,\r
1365                         generating a table of run timer percentages in the provided\r
1366                         buffer. */\r
1367 \r
1368                         *pcWriteBuffer = ( signed char ) 0x00;\r
1369                         strcat( ( char * ) pcWriteBuffer, ( const char * ) "\r\n" );\r
1370 \r
1371                         uxQueue = uxTopUsedPriority + ( unsigned portBASE_TYPE ) 1U;\r
1372 \r
1373                         do\r
1374                         {\r
1375                                 uxQueue--;\r
1376 \r
1377                                 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) == pdFALSE )\r
1378                                 {\r
1379                                         prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), ulTotalRunTime );\r
1380                                 }\r
1381                         }while( uxQueue > ( unsigned short ) tskIDLE_PRIORITY );\r
1382 \r
1383                         if( listLIST_IS_EMPTY( pxDelayedTaskList ) == pdFALSE )\r
1384                         {\r
1385                                 prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, ulTotalRunTime );\r
1386                         }\r
1387 \r
1388                         if( listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) == pdFALSE )\r
1389                         {\r
1390                                 prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, ulTotalRunTime );\r
1391                         }\r
1392 \r
1393                         #if ( INCLUDE_vTaskDelete == 1 )\r
1394                         {\r
1395                                 if( listLIST_IS_EMPTY( &xTasksWaitingTermination ) == pdFALSE )\r
1396                                 {\r
1397                                         prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, &xTasksWaitingTermination, ulTotalRunTime );\r
1398                                 }\r
1399                         }\r
1400                         #endif\r
1401 \r
1402                         #if ( INCLUDE_vTaskSuspend == 1 )\r
1403                         {\r
1404                                 if( listLIST_IS_EMPTY( &xSuspendedTaskList ) == pdFALSE )\r
1405                                 {\r
1406                                         prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, &xSuspendedTaskList, ulTotalRunTime );\r
1407                                 }\r
1408                         }\r
1409                         #endif\r
1410                 }\r
1411                 xTaskResumeAll();\r
1412         }\r
1413 \r
1414 #endif\r
1415 /*----------------------------------------------------------*/\r
1416 \r
1417 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )\r
1418 \r
1419         xTaskHandle xTaskGetIdleTaskHandle( void )\r
1420         {\r
1421                 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been\r
1422                 started, then xIdleTaskHandle will be NULL. */\r
1423                 configASSERT( ( xIdleTaskHandle != NULL ) );\r
1424                 return xIdleTaskHandle;\r
1425         }\r
1426         \r
1427 #endif\r
1428 \r
1429 /*-----------------------------------------------------------\r
1430  * SCHEDULER INTERNALS AVAILABLE FOR PORTING PURPOSES\r
1431  * documented in task.h\r
1432  *----------------------------------------------------------*/\r
1433 \r
1434 void vTaskIncrementTick( void )\r
1435 {\r
1436 tskTCB * pxTCB;\r
1437 \r
1438         /* Called by the portable layer each time a tick interrupt occurs.\r
1439         Increments the tick then checks to see if the new tick value will cause any\r
1440         tasks to be unblocked. */\r
1441         if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )\r
1442         {\r
1443                 ++xTickCount;\r
1444                 if( xTickCount == ( portTickType ) 0U )\r
1445                 {\r
1446                         xList *pxTemp;\r
1447 \r
1448                         /* Tick count has overflowed so we need to swap the delay lists.\r
1449                         If there are any items in pxDelayedTaskList here then there is\r
1450                         an error! */\r
1451                         configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) );\r
1452                         \r
1453                         pxTemp = pxDelayedTaskList;\r
1454                         pxDelayedTaskList = pxOverflowDelayedTaskList;\r
1455                         pxOverflowDelayedTaskList = pxTemp;\r
1456                         xNumOfOverflows++;\r
1457         \r
1458                         if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )\r
1459                         {\r
1460                                 /* The new current delayed list is empty.  Set\r
1461                                 xNextTaskUnblockTime to the maximum possible value so it is\r
1462                                 extremely unlikely that the     \r
1463                                 if( xTickCount >= xNextTaskUnblockTime ) test will pass until\r
1464                                 there is an item in the delayed list. */\r
1465                                 xNextTaskUnblockTime = portMAX_DELAY;\r
1466                         }\r
1467                         else\r
1468                         {\r
1469                                 /* The new current delayed list is not empty, get the value of\r
1470                                 the item at the head of the delayed list.  This is the time at\r
1471                                 which the task at the head of the delayed list should be removed\r
1472                                 from the Blocked state. */\r
1473                                 pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );\r
1474                                 xNextTaskUnblockTime = listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) );\r
1475                         }\r
1476                 }\r
1477 \r
1478                 /* See if this tick has made a timeout expire. */\r
1479                 prvCheckDelayedTasks();\r
1480         }\r
1481         else\r
1482         {\r
1483                 ++uxMissedTicks;\r
1484 \r
1485                 /* The tick hook gets called at regular intervals, even if the\r
1486                 scheduler is locked. */\r
1487                 #if ( configUSE_TICK_HOOK == 1 )\r
1488                 {\r
1489                         vApplicationTickHook();\r
1490                 }\r
1491                 #endif\r
1492         }\r
1493 \r
1494         #if ( configUSE_TICK_HOOK == 1 )\r
1495         {\r
1496                 /* Guard against the tick hook being called when the missed tick\r
1497                 count is being unwound (when the scheduler is being unlocked. */\r
1498                 if( uxMissedTicks == ( unsigned portBASE_TYPE ) 0U )\r
1499                 {\r
1500                         vApplicationTickHook();\r
1501                 }\r
1502         }\r
1503         #endif\r
1504 \r
1505         traceTASK_INCREMENT_TICK( xTickCount );\r
1506 }\r
1507 /*-----------------------------------------------------------*/\r
1508 \r
1509 #if ( configUSE_APPLICATION_TASK_TAG == 1 )\r
1510 \r
1511         void vTaskSetApplicationTaskTag( xTaskHandle xTask, pdTASK_HOOK_CODE pxHookFunction )\r
1512         {\r
1513         tskTCB *xTCB;\r
1514 \r
1515                 /* If xTask is NULL then we are setting our own task hook. */\r
1516                 if( xTask == NULL )\r
1517                 {\r
1518                         xTCB = ( tskTCB * ) pxCurrentTCB;\r
1519                 }\r
1520                 else\r
1521                 {\r
1522                         xTCB = ( tskTCB * ) xTask;\r
1523                 }\r
1524 \r
1525                 /* Save the hook function in the TCB.  A critical section is required as\r
1526                 the value can be accessed from an interrupt. */\r
1527                 taskENTER_CRITICAL();\r
1528                         xTCB->pxTaskTag = pxHookFunction;\r
1529                 taskEXIT_CRITICAL();\r
1530         }\r
1531 \r
1532 #endif\r
1533 /*-----------------------------------------------------------*/\r
1534 \r
1535 #if ( configUSE_APPLICATION_TASK_TAG == 1 )\r
1536 \r
1537         pdTASK_HOOK_CODE xTaskGetApplicationTaskTag( xTaskHandle xTask )\r
1538         {\r
1539         tskTCB *xTCB;\r
1540         pdTASK_HOOK_CODE xReturn;\r
1541 \r
1542                 /* If xTask is NULL then we are setting our own task hook. */\r
1543                 if( xTask == NULL )\r
1544                 {\r
1545                         xTCB = ( tskTCB * ) pxCurrentTCB;\r
1546                 }\r
1547                 else\r
1548                 {\r
1549                         xTCB = ( tskTCB * ) xTask;\r
1550                 }\r
1551 \r
1552                 /* Save the hook function in the TCB.  A critical section is required as\r
1553                 the value can be accessed from an interrupt. */\r
1554                 taskENTER_CRITICAL();\r
1555                         xReturn = xTCB->pxTaskTag;\r
1556                 taskEXIT_CRITICAL();\r
1557 \r
1558                 return xReturn;\r
1559         }\r
1560 \r
1561 #endif\r
1562 /*-----------------------------------------------------------*/\r
1563 \r
1564 #if ( configUSE_APPLICATION_TASK_TAG == 1 )\r
1565 \r
1566         portBASE_TYPE xTaskCallApplicationTaskHook( xTaskHandle xTask, void *pvParameter )\r
1567         {\r
1568         tskTCB *xTCB;\r
1569         portBASE_TYPE xReturn;\r
1570 \r
1571                 /* If xTask is NULL then we are calling our own task hook. */\r
1572                 if( xTask == NULL )\r
1573                 {\r
1574                         xTCB = ( tskTCB * ) pxCurrentTCB;\r
1575                 }\r
1576                 else\r
1577                 {\r
1578                         xTCB = ( tskTCB * ) xTask;\r
1579                 }\r
1580 \r
1581                 if( xTCB->pxTaskTag != NULL )\r
1582                 {\r
1583                         xReturn = xTCB->pxTaskTag( pvParameter );\r
1584                 }\r
1585                 else\r
1586                 {\r
1587                         xReturn = pdFAIL;\r
1588                 }\r
1589 \r
1590                 return xReturn;\r
1591         }\r
1592 \r
1593 #endif\r
1594 /*-----------------------------------------------------------*/\r
1595 \r
1596 void vTaskSwitchContext( void )\r
1597 {\r
1598         if( uxSchedulerSuspended != ( unsigned portBASE_TYPE ) pdFALSE )\r
1599         {\r
1600                 /* The scheduler is currently suspended - do not allow a context\r
1601                 switch. */\r
1602                 xMissedYield = pdTRUE;\r
1603         }\r
1604         else\r
1605         {\r
1606                 traceTASK_SWITCHED_OUT();\r
1607         \r
1608                 #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
1609                 {\r
1610                         unsigned long ulTempCounter;\r
1611                         \r
1612                                 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE\r
1613                                         portALT_GET_RUN_TIME_COUNTER_VALUE( ulTempCounter );\r
1614                                 #else\r
1615                                         ulTempCounter = portGET_RUN_TIME_COUNTER_VALUE();\r
1616                                 #endif\r
1617         \r
1618                                 /* Add the amount of time the task has been running to the accumulated\r
1619                                 time so far.  The time the task started running was stored in\r
1620                                 ulTaskSwitchedInTime.  Note that there is no overflow protection here\r
1621                                 so count values are only valid until the timer overflows.  Generally\r
1622                                 this will be about 1 hour assuming a 1uS timer increment. */\r
1623                                 pxCurrentTCB->ulRunTimeCounter += ( ulTempCounter - ulTaskSwitchedInTime );\r
1624                                 ulTaskSwitchedInTime = ulTempCounter;\r
1625                 }\r
1626                 #endif\r
1627         \r
1628                 taskFIRST_CHECK_FOR_STACK_OVERFLOW();\r
1629                 taskSECOND_CHECK_FOR_STACK_OVERFLOW();\r
1630         \r
1631                 /* Find the highest priority queue that contains ready tasks. */\r
1632                 while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopReadyPriority ] ) ) )\r
1633                 {\r
1634                         configASSERT( uxTopReadyPriority );\r
1635                         --uxTopReadyPriority;\r
1636                 }\r
1637         \r
1638                 /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the tasks of the\r
1639                 same priority get an equal share of the processor time. */\r
1640                 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopReadyPriority ] ) );\r
1641         \r
1642                 traceTASK_SWITCHED_IN();\r
1643         }\r
1644 }\r
1645 /*-----------------------------------------------------------*/\r
1646 \r
1647 void vTaskPlaceOnEventList( const xList * const pxEventList, portTickType xTicksToWait )\r
1648 {\r
1649 portTickType xTimeToWake;\r
1650 \r
1651         configASSERT( pxEventList );\r
1652 \r
1653         /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE\r
1654         SCHEDULER SUSPENDED. */\r
1655 \r
1656         /* Place the event list item of the TCB in the appropriate event list.\r
1657         This is placed in the list in priority order so the highest priority task\r
1658         is the first to be woken by the event. */\r
1659         vListInsert( ( xList * ) pxEventList, ( xListItem * ) &( pxCurrentTCB->xEventListItem ) );\r
1660 \r
1661         /* We must remove ourselves from the ready list before adding ourselves\r
1662         to the blocked list as the same list item is used for both lists.  We have\r
1663         exclusive access to the ready lists as the scheduler is locked. */\r
1664         vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
1665 \r
1666 \r
1667         #if ( INCLUDE_vTaskSuspend == 1 )\r
1668         {\r
1669                 if( xTicksToWait == portMAX_DELAY )\r
1670                 {\r
1671                         /* Add ourselves to the suspended task list instead of a delayed task\r
1672                         list to ensure we are not woken by a timing event.  We will block\r
1673                         indefinitely. */\r
1674                         vListInsertEnd( ( xList * ) &xSuspendedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
1675                 }\r
1676                 else\r
1677                 {\r
1678                         /* Calculate the time at which the task should be woken if the event does\r
1679                         not occur.  This may overflow but this doesn't matter. */\r
1680                         xTimeToWake = xTickCount + xTicksToWait;\r
1681                         prvAddCurrentTaskToDelayedList( xTimeToWake );\r
1682                 }\r
1683         }\r
1684         #else\r
1685         {\r
1686                         /* Calculate the time at which the task should be woken if the event does\r
1687                         not occur.  This may overflow but this doesn't matter. */\r
1688                         xTimeToWake = xTickCount + xTicksToWait;\r
1689                         prvAddCurrentTaskToDelayedList( xTimeToWake );\r
1690         }\r
1691         #endif\r
1692 }\r
1693 /*-----------------------------------------------------------*/\r
1694 \r
1695 #if configUSE_TIMERS == 1\r
1696 \r
1697         void vTaskPlaceOnEventListRestricted( const xList * const pxEventList, portTickType xTicksToWait )\r
1698         {\r
1699         portTickType xTimeToWake;\r
1700 \r
1701                 configASSERT( pxEventList );\r
1702 \r
1703                 /* This function should not be called by application code hence the\r
1704                 'Restricted' in its name.  It is not part of the public API.  It is\r
1705                 designed for use by kernel code, and has special calling requirements -\r
1706                 it should be called from a critical section. */\r
1707 \r
1708         \r
1709                 /* Place the event list item of the TCB in the appropriate event list.\r
1710                 In this case it is assume that this is the only task that is going to\r
1711                 be waiting on this event list, so the faster vListInsertEnd() function\r
1712                 can be used in place of vListInsert. */\r
1713                 vListInsertEnd( ( xList * ) pxEventList, ( xListItem * ) &( pxCurrentTCB->xEventListItem ) );\r
1714 \r
1715                 /* We must remove this task from the ready list before adding it to the\r
1716                 blocked list as the same list item is used for both lists.  This\r
1717                 function is called form a critical section. */\r
1718                 vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
1719 \r
1720                 /* Calculate the time at which the task should be woken if the event does\r
1721                 not occur.  This may overflow but this doesn't matter. */\r
1722                 xTimeToWake = xTickCount + xTicksToWait;\r
1723                 prvAddCurrentTaskToDelayedList( xTimeToWake );\r
1724         }\r
1725         \r
1726 #endif /* configUSE_TIMERS */\r
1727 /*-----------------------------------------------------------*/\r
1728 \r
1729 signed portBASE_TYPE xTaskRemoveFromEventList( const xList * const pxEventList )\r
1730 {\r
1731 tskTCB *pxUnblockedTCB;\r
1732 portBASE_TYPE xReturn;\r
1733 \r
1734         /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE\r
1735         SCHEDULER SUSPENDED.  It can also be called from within an ISR. */\r
1736 \r
1737         /* The event list is sorted in priority order, so we can remove the\r
1738         first in the list, remove the TCB from the delayed list, and add\r
1739         it to the ready list.\r
1740 \r
1741         If an event is for a queue that is locked then this function will never\r
1742         get called - the lock count on the queue will get modified instead.  This\r
1743         means we can always expect exclusive access to the event list here.\r
1744         \r
1745         This function assumes that a check has already been made to ensure that\r
1746         pxEventList is not empty. */\r
1747         pxUnblockedTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );\r
1748         configASSERT( pxUnblockedTCB );\r
1749         vListRemove( &( pxUnblockedTCB->xEventListItem ) );\r
1750 \r
1751         if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )\r
1752         {\r
1753                 vListRemove( &( pxUnblockedTCB->xGenericListItem ) );\r
1754                 prvAddTaskToReadyQueue( pxUnblockedTCB );\r
1755         }\r
1756         else\r
1757         {\r
1758                 /* We cannot access the delayed or ready lists, so will hold this\r
1759                 task pending until the scheduler is resumed. */\r
1760                 vListInsertEnd( ( xList * ) &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );\r
1761         }\r
1762 \r
1763         if( pxUnblockedTCB->uxPriority >= pxCurrentTCB->uxPriority )\r
1764         {\r
1765                 /* Return true if the task removed from the event list has\r
1766                 a higher priority than the calling task.  This allows\r
1767                 the calling task to know if it should force a context\r
1768                 switch now. */\r
1769                 xReturn = pdTRUE;\r
1770         }\r
1771         else\r
1772         {\r
1773                 xReturn = pdFALSE;\r
1774         }\r
1775 \r
1776         return xReturn;\r
1777 }\r
1778 /*-----------------------------------------------------------*/\r
1779 \r
1780 void vTaskSetTimeOutState( xTimeOutType * const pxTimeOut )\r
1781 {\r
1782         configASSERT( pxTimeOut );\r
1783         pxTimeOut->xOverflowCount = xNumOfOverflows;\r
1784         pxTimeOut->xTimeOnEntering = xTickCount;\r
1785 }\r
1786 /*-----------------------------------------------------------*/\r
1787 \r
1788 portBASE_TYPE xTaskCheckForTimeOut( xTimeOutType * const pxTimeOut, portTickType * const pxTicksToWait )\r
1789 {\r
1790 portBASE_TYPE xReturn;\r
1791 \r
1792         configASSERT( pxTimeOut );\r
1793         configASSERT( pxTicksToWait );\r
1794 \r
1795         taskENTER_CRITICAL();\r
1796         {\r
1797                 #if ( INCLUDE_vTaskSuspend == 1 )\r
1798                         /* If INCLUDE_vTaskSuspend is set to 1 and the block time specified is\r
1799                         the maximum block time then the task should block indefinitely, and\r
1800                         therefore never time out. */\r
1801                         if( *pxTicksToWait == portMAX_DELAY )\r
1802                         {\r
1803                                 xReturn = pdFALSE;\r
1804                         }\r
1805                         else /* We are not blocking indefinitely, perform the checks below. */\r
1806                 #endif\r
1807 \r
1808                 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( ( portTickType ) xTickCount >= ( portTickType ) pxTimeOut->xTimeOnEntering ) )\r
1809                 {\r
1810                         /* The tick count is greater than the time at which vTaskSetTimeout()\r
1811                         was called, but has also overflowed since vTaskSetTimeOut() was called.\r
1812                         It must have wrapped all the way around and gone past us again. This\r
1813                         passed since vTaskSetTimeout() was called. */\r
1814                         xReturn = pdTRUE;\r
1815                 }\r
1816                 else if( ( ( portTickType ) ( ( portTickType ) xTickCount - ( portTickType ) pxTimeOut->xTimeOnEntering ) ) < ( portTickType ) *pxTicksToWait )\r
1817                 {\r
1818                         /* Not a genuine timeout. Adjust parameters for time remaining. */\r
1819                         *pxTicksToWait -= ( ( portTickType ) xTickCount - ( portTickType ) pxTimeOut->xTimeOnEntering );\r
1820                         vTaskSetTimeOutState( pxTimeOut );\r
1821                         xReturn = pdFALSE;\r
1822                 }\r
1823                 else\r
1824                 {\r
1825                         xReturn = pdTRUE;\r
1826                 }\r
1827         }\r
1828         taskEXIT_CRITICAL();\r
1829 \r
1830         return xReturn;\r
1831 }\r
1832 /*-----------------------------------------------------------*/\r
1833 \r
1834 void vTaskMissedYield( void )\r
1835 {\r
1836         xMissedYield = pdTRUE;\r
1837 }\r
1838 /*-----------------------------------------------------------*/\r
1839 \r
1840 #if ( configUSE_TRACE_FACILITY == 1 )\r
1841         unsigned portBASE_TYPE uxTaskGetTaskNumber( xTaskHandle xTask )\r
1842         {\r
1843         unsigned portBASE_TYPE uxReturn;\r
1844         tskTCB *pxTCB;\r
1845         \r
1846                 if( xTask != NULL )\r
1847                 {\r
1848                         pxTCB = ( tskTCB * ) xTask;\r
1849                         uxReturn = pxTCB->uxTaskNumber;\r
1850                 }\r
1851                 else\r
1852                 {\r
1853                         uxReturn = 0U;\r
1854                 }\r
1855                 \r
1856                 return uxReturn;\r
1857         }\r
1858 #endif\r
1859 /*-----------------------------------------------------------*/\r
1860 \r
1861 #if ( configUSE_TRACE_FACILITY == 1 )\r
1862         void vTaskSetTaskNumber( xTaskHandle xTask, unsigned portBASE_TYPE uxHandle )\r
1863         {\r
1864         tskTCB *pxTCB;\r
1865         \r
1866                 if( xTask != NULL )\r
1867                 {\r
1868                         pxTCB = ( tskTCB * ) xTask;\r
1869                         pxTCB->uxTaskNumber = uxHandle;\r
1870                 }\r
1871         }\r
1872 #endif\r
1873 \r
1874 \r
1875 /*\r
1876  * -----------------------------------------------------------\r
1877  * The Idle task.\r
1878  * ----------------------------------------------------------\r
1879  *\r
1880  * The portTASK_FUNCTION() macro is used to allow port/compiler specific\r
1881  * language extensions.  The equivalent prototype for this function is:\r
1882  *\r
1883  * void prvIdleTask( void *pvParameters );\r
1884  *\r
1885  */\r
1886 static portTASK_FUNCTION( prvIdleTask, pvParameters )\r
1887 {\r
1888         /* Stop warnings. */\r
1889         ( void ) pvParameters;\r
1890 \r
1891         for( ;; )\r
1892         {\r
1893                 /* See if any tasks have been deleted. */\r
1894                 prvCheckTasksWaitingTermination();\r
1895 \r
1896                 #if ( configUSE_PREEMPTION == 0 )\r
1897                 {\r
1898                         /* If we are not using preemption we keep forcing a task switch to\r
1899                         see if any other task has become available.  If we are using\r
1900                         preemption we don't need to do this as any task becoming available\r
1901                         will automatically get the processor anyway. */\r
1902                         taskYIELD();\r
1903                 }\r
1904                 #endif\r
1905 \r
1906                 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )\r
1907                 {\r
1908                         /* When using preemption tasks of equal priority will be\r
1909                         timesliced.  If a task that is sharing the idle priority is ready\r
1910                         to run then the idle task should yield before the end of the\r
1911                         timeslice.\r
1912 \r
1913                         A critical region is not required here as we are just reading from\r
1914                         the list, and an occasional incorrect value will not matter.  If\r
1915                         the ready list at the idle priority contains more than one task\r
1916                         then a task other than the idle task is ready to execute. */\r
1917                         if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( unsigned portBASE_TYPE ) 1 )\r
1918                         {\r
1919                                 taskYIELD();\r
1920                         }\r
1921                 }\r
1922                 #endif\r
1923 \r
1924                 #if ( configUSE_IDLE_HOOK == 1 )\r
1925                 {\r
1926                         extern void vApplicationIdleHook( void );\r
1927 \r
1928                         /* Call the user defined function from within the idle task.  This\r
1929                         allows the application designer to add background functionality\r
1930                         without the overhead of a separate task.\r
1931                         NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,\r
1932                         CALL A FUNCTION THAT MIGHT BLOCK. */\r
1933                         vApplicationIdleHook();\r
1934                 }\r
1935                 #endif\r
1936         }\r
1937 } /*lint !e715 pvParameters is not accessed but all task functions require the same prototype. */\r
1938 \r
1939 \r
1940 \r
1941 \r
1942 \r
1943 \r
1944 \r
1945 /*-----------------------------------------------------------\r
1946  * File private functions documented at the top of the file.\r
1947  *----------------------------------------------------------*/\r
1948 \r
1949 \r
1950 \r
1951 static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed char * const pcName, unsigned portBASE_TYPE uxPriority, const xMemoryRegion * const xRegions, unsigned short usStackDepth )\r
1952 {\r
1953         /* Store the function name in the TCB. */\r
1954         #if configMAX_TASK_NAME_LEN > 1\r
1955         {\r
1956                 /* Don't bring strncpy into the build unnecessarily. */\r
1957                 strncpy( ( char * ) pxTCB->pcTaskName, ( const char * ) pcName, ( unsigned short ) configMAX_TASK_NAME_LEN );\r
1958         }\r
1959         #endif\r
1960         pxTCB->pcTaskName[ ( unsigned short ) configMAX_TASK_NAME_LEN - ( unsigned short ) 1 ] = ( signed char ) '\0';\r
1961 \r
1962         /* This is used as an array index so must ensure it's not too large.  First\r
1963         remove the privilege bit if one is present. */\r
1964         if( uxPriority >= configMAX_PRIORITIES )\r
1965         {\r
1966                 uxPriority = configMAX_PRIORITIES - ( unsigned portBASE_TYPE ) 1U;\r
1967         }\r
1968 \r
1969         pxTCB->uxPriority = uxPriority;\r
1970         #if ( configUSE_MUTEXES == 1 )\r
1971         {\r
1972                 pxTCB->uxBasePriority = uxPriority;\r
1973         }\r
1974         #endif\r
1975 \r
1976         vListInitialiseItem( &( pxTCB->xGenericListItem ) );\r
1977         vListInitialiseItem( &( pxTCB->xEventListItem ) );\r
1978 \r
1979         /* Set the pxTCB as a link back from the xListItem.  This is so we can get\r
1980         back to the containing TCB from a generic item in a list. */\r
1981         listSET_LIST_ITEM_OWNER( &( pxTCB->xGenericListItem ), pxTCB );\r
1982 \r
1983         /* Event lists are always in priority order. */\r
1984         listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );\r
1985         listSET_LIST_ITEM_OWNER( &( pxTCB->xEventListItem ), pxTCB );\r
1986 \r
1987         #if ( portCRITICAL_NESTING_IN_TCB == 1 )\r
1988         {\r
1989                 pxTCB->uxCriticalNesting = ( unsigned portBASE_TYPE ) 0U;\r
1990         }\r
1991         #endif\r
1992 \r
1993         #if ( configUSE_APPLICATION_TASK_TAG == 1 )\r
1994         {\r
1995                 pxTCB->pxTaskTag = NULL;\r
1996         }\r
1997         #endif\r
1998 \r
1999         #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
2000         {\r
2001                 pxTCB->ulRunTimeCounter = 0UL;\r
2002         }\r
2003         #endif\r
2004 \r
2005         #if ( portUSING_MPU_WRAPPERS == 1 )\r
2006         {\r
2007                 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, pxTCB->pxStack, usStackDepth );\r
2008         }\r
2009         #else\r
2010         {\r
2011                 ( void ) xRegions;\r
2012                 ( void ) usStackDepth;\r
2013         }\r
2014         #endif\r
2015 }\r
2016 /*-----------------------------------------------------------*/\r
2017 \r
2018 #if ( portUSING_MPU_WRAPPERS == 1 )\r
2019 \r
2020         void vTaskAllocateMPURegions( xTaskHandle xTaskToModify, const xMemoryRegion * const xRegions )\r
2021         {\r
2022         tskTCB *pxTCB;\r
2023         \r
2024                 if( xTaskToModify == pxCurrentTCB )\r
2025                 {\r
2026                         xTaskToModify = NULL;\r
2027                 }\r
2028 \r
2029                 /* If null is passed in here then we are deleting ourselves. */\r
2030                 pxTCB = prvGetTCBFromHandle( xTaskToModify );\r
2031 \r
2032         vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );\r
2033         }\r
2034         /*-----------------------------------------------------------*/\r
2035 #endif\r
2036 \r
2037 static void prvInitialiseTaskLists( void )\r
2038 {\r
2039 unsigned portBASE_TYPE uxPriority;\r
2040 \r
2041         for( uxPriority = ( unsigned portBASE_TYPE ) 0U; uxPriority < configMAX_PRIORITIES; uxPriority++ )\r
2042         {\r
2043                 vListInitialise( ( xList * ) &( pxReadyTasksLists[ uxPriority ] ) );\r
2044         }\r
2045 \r
2046         vListInitialise( ( xList * ) &xDelayedTaskList1 );\r
2047         vListInitialise( ( xList * ) &xDelayedTaskList2 );\r
2048         vListInitialise( ( xList * ) &xPendingReadyList );\r
2049 \r
2050         #if ( INCLUDE_vTaskDelete == 1 )\r
2051         {\r
2052                 vListInitialise( ( xList * ) &xTasksWaitingTermination );\r
2053         }\r
2054         #endif\r
2055 \r
2056         #if ( INCLUDE_vTaskSuspend == 1 )\r
2057         {\r
2058                 vListInitialise( ( xList * ) &xSuspendedTaskList );\r
2059         }\r
2060         #endif\r
2061 \r
2062         /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList\r
2063         using list2. */\r
2064         pxDelayedTaskList = &xDelayedTaskList1;\r
2065         pxOverflowDelayedTaskList = &xDelayedTaskList2;\r
2066 }\r
2067 /*-----------------------------------------------------------*/\r
2068 \r
2069 static void prvCheckTasksWaitingTermination( void )\r
2070 {\r
2071         #if ( INCLUDE_vTaskDelete == 1 )\r
2072         {\r
2073                 portBASE_TYPE xListIsEmpty;\r
2074 \r
2075                 /* ucTasksDeleted is used to prevent vTaskSuspendAll() being called\r
2076                 too often in the idle task. */\r
2077                 if( uxTasksDeleted > ( unsigned portBASE_TYPE ) 0U )\r
2078                 {\r
2079                         vTaskSuspendAll();\r
2080                                 xListIsEmpty = listLIST_IS_EMPTY( &xTasksWaitingTermination );\r
2081                         xTaskResumeAll();\r
2082 \r
2083                         if( xListIsEmpty == pdFALSE )\r
2084                         {\r
2085                                 tskTCB *pxTCB;\r
2086 \r
2087                                 taskENTER_CRITICAL();\r
2088                                 {\r
2089                                         pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( ( ( xList * ) &xTasksWaitingTermination ) );\r
2090                                         vListRemove( &( pxTCB->xGenericListItem ) );\r
2091                                         --uxCurrentNumberOfTasks;\r
2092                                         --uxTasksDeleted;\r
2093                                 }\r
2094                                 taskEXIT_CRITICAL();\r
2095 \r
2096                                 prvDeleteTCB( pxTCB );\r
2097                         }\r
2098                 }\r
2099         }\r
2100         #endif\r
2101 }\r
2102 /*-----------------------------------------------------------*/\r
2103 \r
2104 static void prvAddCurrentTaskToDelayedList( portTickType xTimeToWake )\r
2105 {\r
2106         /* The list item will be inserted in wake time order. */\r
2107         listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );\r
2108 \r
2109         if( xTimeToWake < xTickCount )\r
2110         {\r
2111                 /* Wake time has overflowed.  Place this item in the overflow list. */\r
2112                 vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
2113         }\r
2114         else\r
2115         {\r
2116                 /* The wake time has not overflowed, so we can use the current block list. */\r
2117                 vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );\r
2118 \r
2119                 /* If the task entering the blocked state was placed at the head of the\r
2120                 list of blocked tasks then xNextTaskUnblockTime needs to be updated\r
2121                 too. */\r
2122                 if( xTimeToWake < xNextTaskUnblockTime )\r
2123                 {\r
2124                         xNextTaskUnblockTime = xTimeToWake;\r
2125                 }\r
2126         }\r
2127 }\r
2128 /*-----------------------------------------------------------*/\r
2129 \r
2130 static tskTCB *prvAllocateTCBAndStack( unsigned short usStackDepth, portSTACK_TYPE *puxStackBuffer )\r
2131 {\r
2132 tskTCB *pxNewTCB;\r
2133 \r
2134         /* Allocate space for the TCB.  Where the memory comes from depends on\r
2135         the implementation of the port malloc function. */\r
2136         pxNewTCB = ( tskTCB * ) pvPortMalloc( sizeof( tskTCB ) );\r
2137 \r
2138         if( pxNewTCB != NULL )\r
2139         {\r
2140                 /* Allocate space for the stack used by the task being created.\r
2141                 The base of the stack memory stored in the TCB so the task can\r
2142                 be deleted later if required. */\r
2143                 pxNewTCB->pxStack = ( portSTACK_TYPE * ) pvPortMallocAligned( ( ( ( size_t )usStackDepth ) * sizeof( portSTACK_TYPE ) ), puxStackBuffer );\r
2144 \r
2145                 if( pxNewTCB->pxStack == NULL )\r
2146                 {\r
2147                         /* Could not allocate the stack.  Delete the allocated TCB. */\r
2148                         vPortFree( pxNewTCB );\r
2149                         pxNewTCB = NULL;\r
2150                 }\r
2151                 else\r
2152                 {\r
2153                         /* Just to help debugging. */\r
2154                         memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) usStackDepth * sizeof( portSTACK_TYPE ) );\r
2155                 }\r
2156         }\r
2157 \r
2158         return pxNewTCB;\r
2159 }\r
2160 /*-----------------------------------------------------------*/\r
2161 \r
2162 #if ( configUSE_TRACE_FACILITY == 1 )\r
2163 \r
2164         static void prvListTaskWithinSingleList( const signed char *pcWriteBuffer, xList *pxList, signed char cStatus )\r
2165         {\r
2166         volatile tskTCB *pxNextTCB, *pxFirstTCB;\r
2167         unsigned short usStackRemaining;\r
2168         PRIVILEGED_DATA static char pcStatusString[ configMAX_TASK_NAME_LEN + 30 ];\r
2169 \r
2170                 /* Write the details of all the TCB's in pxList into the buffer. */\r
2171                 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );\r
2172                 do\r
2173                 {\r
2174                         listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );\r
2175                         #if ( portSTACK_GROWTH > 0 )\r
2176                         {\r
2177                                 usStackRemaining = usTaskCheckFreeStackSpace( ( unsigned char * ) pxNextTCB->pxEndOfStack );\r
2178                         }\r
2179                         #else\r
2180                         {\r
2181                                 usStackRemaining = usTaskCheckFreeStackSpace( ( unsigned char * ) pxNextTCB->pxStack );\r
2182                         }\r
2183                         #endif                  \r
2184                         \r
2185                         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
2186                         strcat( ( char * ) pcWriteBuffer, ( char * ) pcStatusString );\r
2187 \r
2188                 } while( pxNextTCB != pxFirstTCB );\r
2189         }\r
2190 \r
2191 #endif\r
2192 /*-----------------------------------------------------------*/\r
2193 \r
2194 #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
2195 \r
2196         static void prvGenerateRunTimeStatsForTasksInList( const signed char *pcWriteBuffer, xList *pxList, unsigned long ulTotalRunTime )\r
2197         {\r
2198         volatile tskTCB *pxNextTCB, *pxFirstTCB;\r
2199         unsigned long ulStatsAsPercentage;\r
2200 \r
2201                 /* Write the run time stats of all the TCB's in pxList into the buffer. */\r
2202                 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );\r
2203                 do\r
2204                 {\r
2205                         /* Get next TCB in from the list. */\r
2206                         listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );\r
2207 \r
2208                         /* Divide by zero check. */\r
2209                         if( ulTotalRunTime > 0UL )\r
2210                         {\r
2211                                 /* Has the task run at all? */\r
2212                                 if( pxNextTCB->ulRunTimeCounter == 0UL )\r
2213                                 {\r
2214                                         /* The task has used no CPU time at all. */\r
2215                                         sprintf( pcStatsString, ( char * ) "%s\t\t0\t\t0%%\r\n", pxNextTCB->pcTaskName );\r
2216                                 }\r
2217                                 else\r
2218                                 {\r
2219                                         /* What percentage of the total run time has the task used?\r
2220                                         This will always be rounded down to the nearest integer.\r
2221                                         ulTotalRunTime has already been divided by 100. */\r
2222                                         ulStatsAsPercentage = pxNextTCB->ulRunTimeCounter / ulTotalRunTime;\r
2223 \r
2224                                         if( ulStatsAsPercentage > 0UL )\r
2225                                         {\r
2226                                                 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED\r
2227                                                 {\r
2228                                                         sprintf( pcStatsString, ( char * ) "%s\t\t%lu\t\t%lu%%\r\n", pxNextTCB->pcTaskName, pxNextTCB->ulRunTimeCounter, ulStatsAsPercentage );                                                 \r
2229                                                 }\r
2230                                                 #else\r
2231                                                 {\r
2232                                                         /* sizeof( int ) == sizeof( long ) so a smaller\r
2233                                                         printf() library can be used. */\r
2234                                                         sprintf( pcStatsString, ( char * ) "%s\t\t%u\t\t%u%%\r\n", pxNextTCB->pcTaskName, ( unsigned int ) pxNextTCB->ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage );\r
2235                                                 }\r
2236                                                 #endif\r
2237                                         }\r
2238                                         else\r
2239                                         {\r
2240                                                 /* If the percentage is zero here then the task has\r
2241                                                 consumed less than 1% of the total run time. */\r
2242                                                 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED\r
2243                                                 {\r
2244                                                         sprintf( pcStatsString, ( char * ) "%s\t\t%lu\t\t<1%%\r\n", pxNextTCB->pcTaskName, pxNextTCB->ulRunTimeCounter );                                                       \r
2245                                                 }\r
2246                                                 #else\r
2247                                                 {\r
2248                                                         /* sizeof( int ) == sizeof( long ) so a smaller\r
2249                                                         printf() library can be used. */\r
2250                                                         sprintf( pcStatsString, ( char * ) "%s\t\t%u\t\t<1%%\r\n", pxNextTCB->pcTaskName, ( unsigned int ) pxNextTCB->ulRunTimeCounter );\r
2251                                                 }\r
2252                                                 #endif\r
2253                                         }\r
2254                                 }\r
2255 \r
2256                                 strcat( ( char * ) pcWriteBuffer, ( char * ) pcStatsString );\r
2257                         }\r
2258 \r
2259                 } while( pxNextTCB != pxFirstTCB );\r
2260         }\r
2261 \r
2262 #endif\r
2263 /*-----------------------------------------------------------*/\r
2264 \r
2265 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )\r
2266 \r
2267         static unsigned short usTaskCheckFreeStackSpace( const unsigned char * pucStackByte )\r
2268         {\r
2269         register unsigned short usCount = 0U;\r
2270 \r
2271                 while( *pucStackByte == tskSTACK_FILL_BYTE )\r
2272                 {\r
2273                         pucStackByte -= portSTACK_GROWTH;\r
2274                         usCount++;\r
2275                 }\r
2276 \r
2277                 usCount /= sizeof( portSTACK_TYPE );\r
2278 \r
2279                 return usCount;\r
2280         }\r
2281 \r
2282 #endif\r
2283 /*-----------------------------------------------------------*/\r
2284 \r
2285 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )\r
2286 \r
2287         unsigned portBASE_TYPE uxTaskGetStackHighWaterMark( xTaskHandle xTask )\r
2288         {\r
2289         tskTCB *pxTCB;\r
2290         unsigned char *pcEndOfStack;\r
2291         unsigned portBASE_TYPE uxReturn;\r
2292 \r
2293                 pxTCB = prvGetTCBFromHandle( xTask );\r
2294 \r
2295                 #if portSTACK_GROWTH < 0\r
2296                 {\r
2297                         pcEndOfStack = ( unsigned char * ) pxTCB->pxStack;\r
2298                 }\r
2299                 #else\r
2300                 {\r
2301                         pcEndOfStack = ( unsigned char * ) pxTCB->pxEndOfStack;\r
2302                 }\r
2303                 #endif\r
2304 \r
2305                 uxReturn = ( unsigned portBASE_TYPE ) usTaskCheckFreeStackSpace( pcEndOfStack );\r
2306 \r
2307                 return uxReturn;\r
2308         }\r
2309 \r
2310 #endif\r
2311 /*-----------------------------------------------------------*/\r
2312 \r
2313 #if ( INCLUDE_vTaskDelete == 1 )\r
2314 \r
2315         static void prvDeleteTCB( tskTCB *pxTCB )\r
2316         {\r
2317                 /* This call is required specifically for the TriCore port.  It must be\r
2318                 above the vPortFree() calls.  The call is also used by ports/demos that\r
2319                 want to allocate and clean RAM statically. */\r
2320                 portCLEAN_UP_TCB( pxTCB );\r
2321 \r
2322                 /* Free up the memory allocated by the scheduler for the task.  It is up to\r
2323                 the task to free any memory allocated at the application level. */\r
2324                 vPortFreeAligned( pxTCB->pxStack );\r
2325                 vPortFree( pxTCB );\r
2326         }\r
2327 \r
2328 #endif\r
2329 \r
2330 \r
2331 /*-----------------------------------------------------------*/\r
2332 \r
2333 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )\r
2334 \r
2335         xTaskHandle xTaskGetCurrentTaskHandle( void )\r
2336         {\r
2337         xTaskHandle xReturn;\r
2338 \r
2339                 /* A critical section is not required as this is not called from\r
2340                 an interrupt and the current TCB will always be the same for any\r
2341                 individual execution thread. */\r
2342                 xReturn = pxCurrentTCB;\r
2343 \r
2344                 return xReturn;\r
2345         }\r
2346 \r
2347 #endif\r
2348 \r
2349 /*-----------------------------------------------------------*/\r
2350 \r
2351 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )\r
2352 \r
2353         portBASE_TYPE xTaskGetSchedulerState( void )\r
2354         {\r
2355         portBASE_TYPE xReturn;\r
2356 \r
2357                 if( xSchedulerRunning == pdFALSE )\r
2358                 {\r
2359                         xReturn = taskSCHEDULER_NOT_STARTED;\r
2360                 }\r
2361                 else\r
2362                 {\r
2363                         if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )\r
2364                         {\r
2365                                 xReturn = taskSCHEDULER_RUNNING;\r
2366                         }\r
2367                         else\r
2368                         {\r
2369                                 xReturn = taskSCHEDULER_SUSPENDED;\r
2370                         }\r
2371                 }\r
2372 \r
2373                 return xReturn;\r
2374         }\r
2375 \r
2376 #endif\r
2377 /*-----------------------------------------------------------*/\r
2378 \r
2379 #if ( configUSE_MUTEXES == 1 )\r
2380 \r
2381         void vTaskPriorityInherit( xTaskHandle * const pxMutexHolder )\r
2382         {\r
2383         tskTCB * const pxTCB = ( tskTCB * ) pxMutexHolder;\r
2384 \r
2385                 configASSERT( pxMutexHolder );\r
2386 \r
2387                 if( pxTCB->uxPriority < pxCurrentTCB->uxPriority )\r
2388                 {\r
2389                         /* Adjust the mutex holder state to account for its new priority. */\r
2390                         listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) pxCurrentTCB->uxPriority );\r
2391 \r
2392                         /* If the task being modified is in the ready state it will need to\r
2393                         be moved in to a new list. */\r
2394                         if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxTCB->uxPriority ] ), &( pxTCB->xGenericListItem ) ) != pdFALSE )\r
2395                         {\r
2396                                 vListRemove( &( pxTCB->xGenericListItem ) );\r
2397 \r
2398                                 /* Inherit the priority before being moved into the new list. */\r
2399                                 pxTCB->uxPriority = pxCurrentTCB->uxPriority;\r
2400                                 prvAddTaskToReadyQueue( pxTCB );\r
2401                         }\r
2402                         else\r
2403                         {\r
2404                                 /* Just inherit the priority. */\r
2405                                 pxTCB->uxPriority = pxCurrentTCB->uxPriority;\r
2406                         }\r
2407 \r
2408                         traceTASK_PRIORITY_INHERIT( pxTCB, pxCurrentTCB->uxPriority );\r
2409                 }\r
2410         }\r
2411 \r
2412 #endif\r
2413 /*-----------------------------------------------------------*/\r
2414 \r
2415 #if ( configUSE_MUTEXES == 1 )\r
2416 \r
2417         void vTaskPriorityDisinherit( xTaskHandle * const pxMutexHolder )\r
2418         {\r
2419         tskTCB * const pxTCB = ( tskTCB * ) pxMutexHolder;\r
2420 \r
2421                 if( pxMutexHolder != NULL )\r
2422                 {\r
2423                         if( pxTCB->uxPriority != pxTCB->uxBasePriority )\r
2424                         {\r
2425                                 /* We must be the running task to be able to give the mutex back.\r
2426                                 Remove ourselves from the ready list we currently appear in. */\r
2427                                 vListRemove( &( pxTCB->xGenericListItem ) );\r
2428 \r
2429                                 /* Disinherit the priority before adding the task into the new\r
2430                                 ready list. */\r
2431                                 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );\r
2432                                 pxTCB->uxPriority = pxTCB->uxBasePriority;\r
2433                                 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) pxTCB->uxPriority );\r
2434                                 prvAddTaskToReadyQueue( pxTCB );\r
2435                         }\r
2436                 }\r
2437         }\r
2438 \r
2439 #endif\r
2440 /*-----------------------------------------------------------*/\r
2441 \r
2442 #if ( portCRITICAL_NESTING_IN_TCB == 1 )\r
2443 \r
2444         void vTaskEnterCritical( void )\r
2445         {\r
2446                 portDISABLE_INTERRUPTS();\r
2447 \r
2448                 if( xSchedulerRunning != pdFALSE )\r
2449                 {\r
2450                         ( pxCurrentTCB->uxCriticalNesting )++;\r
2451                 }\r
2452         }\r
2453 \r
2454 #endif\r
2455 /*-----------------------------------------------------------*/\r
2456 \r
2457 #if ( portCRITICAL_NESTING_IN_TCB == 1 )\r
2458 \r
2459 void vTaskExitCritical( void )\r
2460 {\r
2461         if( xSchedulerRunning != pdFALSE )\r
2462         {\r
2463                 if( pxCurrentTCB->uxCriticalNesting > 0U )\r
2464                 {\r
2465                         ( pxCurrentTCB->uxCriticalNesting )--;\r
2466 \r
2467                         if( pxCurrentTCB->uxCriticalNesting == 0U )\r
2468                         {\r
2469                                 portENABLE_INTERRUPTS();\r
2470                         }\r
2471                 }\r
2472         }\r
2473 }\r
2474 \r
2475 #endif\r
2476 /*-----------------------------------------------------------*/\r
2477 \r
2478 \r
2479 \r
2480 \r