2 FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\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
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\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
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\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
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
77 #include "FreeRTOS.h"
\r
80 #include "StackMacros.h"
\r
82 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
85 * Macro to define the amount of stack available to the idle task.
\r
87 #define tskIDLE_STACK_SIZE configMINIMAL_STACK_SIZE
\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
93 typedef struct tskTaskControlBlock
\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
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
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
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
111 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
\r
112 unsigned portBASE_TYPE uxCriticalNesting;
\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
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
124 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
\r
125 pdTASK_HOOK_CODE pxTaskTag;
\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
136 * Some kernel aware debuggers require data to be viewed to be global, rather
\r
139 #ifdef portREMOVE_STATIC_QUALIFIER
\r
144 PRIVILEGED_DATA tskTCB * volatile pxCurrentTCB = NULL;
\r
146 /* Lists for ready and blocked tasks. --------------------*/
\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
155 #if ( INCLUDE_vTaskDelete == 1 )
\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
162 #if ( INCLUDE_vTaskSuspend == 1 )
\r
164 PRIVILEGED_DATA static xList xSuspendedTaskList; /*< Tasks that are currently suspended. */
\r
168 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
\r
170 PRIVILEGED_DATA static xTaskHandle xIdleTaskHandle = NULL;
\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
187 #if ( configGENERATE_RUN_TIME_STATS == 1 )
\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
195 /* Debugging and trace facilities private variables and macros. ------------*/
\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
201 #define tskSTACK_FILL_BYTE ( 0xa5U )
\r
204 * Macros used by vListTask to indicate which state a task is in.
\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
211 /*-----------------------------------------------------------*/
\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
220 #define prvAddTaskToReadyQueue( pxTCB ) \
\r
221 traceMOVED_TASK_TO_READY_STATE( pxTCB ) \
\r
222 if( ( pxTCB )->uxPriority > uxTopReadyPriority ) \
\r
224 uxTopReadyPriority = ( pxTCB )->uxPriority; \
\r
226 vListInsertEnd( ( xList * ) &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xGenericListItem ) )
\r
227 /*-----------------------------------------------------------*/
\r
230 * Macro that looks at the list of tasks that are currently delayed to see if
\r
231 * any require waking.
\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
237 #define prvCheckDelayedTasks() \
\r
239 portTickType xItemValue; \
\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
247 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE ) \
\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
253 xNextTaskUnblockTime = portMAX_DELAY; \
\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
265 if( xTickCount < xItemValue ) \
\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
275 /* It is time to remove the item from the Blocked state. */ \
\r
276 vListRemove( &( pxTCB->xGenericListItem ) ); \
\r
278 /* Is the task waiting on an event also? */ \
\r
279 if( pxTCB->xEventListItem.pvContainer != NULL ) \
\r
281 vListRemove( &( pxTCB->xEventListItem ) ); \
\r
283 prvAddTaskToReadyQueue( pxTCB ); \
\r
288 /*-----------------------------------------------------------*/
\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
296 #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? ( tskTCB * ) pxCurrentTCB : ( tskTCB * ) ( pxHandle ) )
\r
298 /* Callback function prototypes. --------------------------*/
\r
299 extern void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName );
\r
300 extern void vApplicationTickHook( void );
\r
302 /* File private functions. --------------------------------*/
\r
305 * Utility to ready a TCB for a given task. Mainly just copies the parameters
\r
306 * into the TCB structure.
\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
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
314 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
\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
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
324 * void prvIdleTask( void *pvParameters );
\r
327 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters );
\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
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
336 #if ( INCLUDE_vTaskDelete == 1 )
\r
338 static void prvDeleteTCB( tskTCB *pxTCB ) PRIVILEGED_FUNCTION;
\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
347 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
\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
353 static void prvAddCurrentTaskToDelayedList( portTickType xTimeToWake ) PRIVILEGED_FUNCTION;
\r
356 * Allocates memory from the heap for a TCB and associated stack. Checks the
\r
357 * allocation was successful.
\r
359 static tskTCB *prvAllocateTCBAndStack( unsigned short usStackDepth, portSTACK_TYPE *puxStackBuffer ) PRIVILEGED_FUNCTION;
\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
367 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
\r
368 * NORMAL APPLICATION CODE.
\r
370 #if ( configUSE_TRACE_FACILITY == 1 )
\r
372 static void prvListTaskWithinSingleList( const signed char *pcWriteBuffer, xList *pxList, signed char cStatus ) PRIVILEGED_FUNCTION;
\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
381 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )
\r
383 static unsigned short usTaskCheckFreeStackSpace( const unsigned char * pucStackByte ) PRIVILEGED_FUNCTION;
\r
392 /*-----------------------------------------------------------
\r
393 * TASK CREATION API documented in task.h
\r
394 *----------------------------------------------------------*/
\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
398 signed portBASE_TYPE xReturn;
\r
401 configASSERT( pxTaskCode );
\r
402 configASSERT( ( uxPriority < configMAX_PRIORITIES ) );
\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
408 if( pxNewTCB != NULL )
\r
410 portSTACK_TYPE *pxTopOfStack;
\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
417 xRunPrivileged = pdTRUE;
\r
421 xRunPrivileged = pdFALSE;
\r
423 uxPriority &= ~portPRIVILEGE_BIT;
\r
424 #endif /* portUSING_MPU_WRAPPERS == 1 */
\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
432 pxTopOfStack = pxNewTCB->pxStack + ( usStackDepth - ( unsigned short ) 1 );
\r
433 pxTopOfStack = ( portSTACK_TYPE * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ( portPOINTER_SIZE_TYPE ) ~portBYTE_ALIGNMENT_MASK ) );
\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
440 pxTopOfStack = pxNewTCB->pxStack;
\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
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
452 /* Setup the newly allocated TCB with the initial state of the task. */
\r
453 prvInitialiseTCBVariables( pxNewTCB, pcName, uxPriority, xRegions, usStackDepth );
\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
461 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
\r
465 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
\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
472 if( ( void * ) pxCreatedTask != NULL )
\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
477 *pxCreatedTask = ( xTaskHandle ) pxNewTCB;
\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
484 uxCurrentNumberOfTasks++;
\r
485 if( pxCurrentTCB == NULL )
\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
491 if( uxCurrentNumberOfTasks == ( unsigned portBASE_TYPE ) 1 )
\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
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
504 if( xSchedulerRunning == pdFALSE )
\r
506 if( pxCurrentTCB->uxPriority <= uxPriority )
\r
508 pxCurrentTCB = pxNewTCB;
\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
517 uxTopUsedPriority = pxNewTCB->uxPriority;
\r
520 #if ( configUSE_TRACE_FACILITY == 1 )
\r
522 /* Add a counter into the TCB for tracing only. */
\r
523 pxNewTCB->uxTCBNumber = uxTaskNumber;
\r
528 prvAddTaskToReadyQueue( pxNewTCB );
\r
531 portSETUP_TCB( pxNewTCB );
\r
532 traceTASK_CREATE( pxNewTCB );
\r
534 taskEXIT_CRITICAL();
\r
538 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
\r
539 traceTASK_CREATE_FAILED();
\r
542 if( xReturn == pdPASS )
\r
544 if( xSchedulerRunning != pdFALSE )
\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
550 portYIELD_WITHIN_API();
\r
557 /*-----------------------------------------------------------*/
\r
559 #if ( INCLUDE_vTaskDelete == 1 )
\r
561 void vTaskDelete( xTaskHandle pxTaskToDelete )
\r
565 taskENTER_CRITICAL();
\r
567 /* Ensure a yield is performed if the current task is being
\r
569 if( pxTaskToDelete == pxCurrentTCB )
\r
571 pxTaskToDelete = NULL;
\r
574 /* If null is passed in here then we are deleting ourselves. */
\r
575 pxTCB = prvGetTCBFromHandle( pxTaskToDelete );
\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
583 /* Is the task waiting on an event also? */
\r
584 if( pxTCB->xEventListItem.pvContainer != NULL )
\r
586 vListRemove( &( pxTCB->xEventListItem ) );
\r
589 vListInsertEnd( ( xList * ) &xTasksWaitingTermination, &( pxTCB->xGenericListItem ) );
\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
596 /* Increment the uxTaskNumberVariable also so kernel aware debuggers
\r
597 can detect that the task lists need re-generating. */
\r
600 traceTASK_DELETE( pxTCB );
\r
602 taskEXIT_CRITICAL();
\r
604 /* Force a reschedule if we have just deleted the current task. */
\r
605 if( xSchedulerRunning != pdFALSE )
\r
607 if( ( void * ) pxTaskToDelete == NULL )
\r
609 portYIELD_WITHIN_API();
\r
621 /*-----------------------------------------------------------
\r
622 * TASK CONTROL API documented in task.h
\r
623 *----------------------------------------------------------*/
\r
625 #if ( INCLUDE_vTaskDelayUntil == 1 )
\r
627 void vTaskDelayUntil( portTickType * const pxPreviousWakeTime, portTickType xTimeIncrement )
\r
629 portTickType xTimeToWake;
\r
630 portBASE_TYPE xAlreadyYielded, xShouldDelay = pdFALSE;
\r
632 configASSERT( pxPreviousWakeTime );
\r
633 configASSERT( ( xTimeIncrement > 0U ) );
\r
637 /* Generate the tick time at which the task wants to wake. */
\r
638 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
\r
640 if( xTickCount < *pxPreviousWakeTime )
\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
649 xShouldDelay = pdTRUE;
\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
659 xShouldDelay = pdTRUE;
\r
663 /* Update the wake time ready for the next call. */
\r
664 *pxPreviousWakeTime = xTimeToWake;
\r
666 if( xShouldDelay != pdFALSE )
\r
668 traceTASK_DELAY_UNTIL();
\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
673 vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
\r
674 prvAddCurrentTaskToDelayedList( xTimeToWake );
\r
677 xAlreadyYielded = xTaskResumeAll();
\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
683 portYIELD_WITHIN_API();
\r
688 /*-----------------------------------------------------------*/
\r
690 #if ( INCLUDE_vTaskDelay == 1 )
\r
692 void vTaskDelay( portTickType xTicksToDelay )
\r
694 portTickType xTimeToWake;
\r
695 signed portBASE_TYPE xAlreadyYielded = pdFALSE;
\r
697 /* A delay time of zero just forces a reschedule. */
\r
698 if( xTicksToDelay > ( portTickType ) 0U )
\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
709 This task cannot be in an event list as it is the currently
\r
712 /* Calculate the time to wake - this may overflow but this is
\r
714 xTimeToWake = xTickCount + xTicksToDelay;
\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
719 vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
\r
720 prvAddCurrentTaskToDelayedList( xTimeToWake );
\r
722 xAlreadyYielded = xTaskResumeAll();
\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
729 portYIELD_WITHIN_API();
\r
734 /*-----------------------------------------------------------*/
\r
736 #if ( INCLUDE_uxTaskPriorityGet == 1 )
\r
738 unsigned portBASE_TYPE uxTaskPriorityGet( xTaskHandle pxTask )
\r
741 unsigned portBASE_TYPE uxReturn;
\r
743 taskENTER_CRITICAL();
\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
750 taskEXIT_CRITICAL();
\r
756 /*-----------------------------------------------------------*/
\r
758 #if ( INCLUDE_vTaskPrioritySet == 1 )
\r
760 void vTaskPrioritySet( xTaskHandle pxTask, unsigned portBASE_TYPE uxNewPriority )
\r
763 unsigned portBASE_TYPE uxCurrentPriority;
\r
764 portBASE_TYPE xYieldRequired = pdFALSE;
\r
766 configASSERT( ( uxNewPriority < configMAX_PRIORITIES ) );
\r
768 /* Ensure the new priority is valid. */
\r
769 if( uxNewPriority >= configMAX_PRIORITIES )
\r
771 uxNewPriority = configMAX_PRIORITIES - ( unsigned portBASE_TYPE ) 1U;
\r
774 taskENTER_CRITICAL();
\r
776 if( pxTask == pxCurrentTCB )
\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
785 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
\r
787 #if ( configUSE_MUTEXES == 1 )
\r
789 uxCurrentPriority = pxTCB->uxBasePriority;
\r
793 uxCurrentPriority = pxTCB->uxPriority;
\r
797 if( uxCurrentPriority != uxNewPriority )
\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
803 if( pxTask != NULL )
\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
812 else if( pxTask == NULL )
\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
821 #if ( configUSE_MUTEXES == 1 )
\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
827 pxTCB->uxPriority = uxNewPriority;
\r
830 /* The base priority gets set whatever. */
\r
831 pxTCB->uxBasePriority = uxNewPriority;
\r
835 pxTCB->uxPriority = uxNewPriority;
\r
839 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( configMAX_PRIORITIES - ( portTickType ) uxNewPriority ) );
\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
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
854 if( xYieldRequired == pdTRUE )
\r
856 portYIELD_WITHIN_API();
\r
860 taskEXIT_CRITICAL();
\r
864 /*-----------------------------------------------------------*/
\r
866 #if ( INCLUDE_vTaskSuspend == 1 )
\r
868 void vTaskSuspend( xTaskHandle pxTaskToSuspend )
\r
872 taskENTER_CRITICAL();
\r
874 /* Ensure a yield is performed if the current task is being
\r
876 if( pxTaskToSuspend == pxCurrentTCB )
\r
878 pxTaskToSuspend = NULL;
\r
881 /* If null is passed in here then we are suspending ourselves. */
\r
882 pxTCB = prvGetTCBFromHandle( pxTaskToSuspend );
\r
884 traceTASK_SUSPEND( pxTCB );
\r
886 /* Remove task from the ready/delayed list and place in the suspended list. */
\r
887 vListRemove( &( pxTCB->xGenericListItem ) );
\r
889 /* Is the task waiting on an event also? */
\r
890 if( pxTCB->xEventListItem.pvContainer != NULL )
\r
892 vListRemove( &( pxTCB->xEventListItem ) );
\r
895 vListInsertEnd( ( xList * ) &xSuspendedTaskList, &( pxTCB->xGenericListItem ) );
\r
897 taskEXIT_CRITICAL();
\r
899 if( ( void * ) pxTaskToSuspend == NULL )
\r
901 if( xSchedulerRunning != pdFALSE )
\r
903 /* We have just suspended the current task. */
\r
904 portYIELD_WITHIN_API();
\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
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
917 pxCurrentTCB = NULL;
\r
921 vTaskSwitchContext();
\r
928 /*-----------------------------------------------------------*/
\r
930 #if ( INCLUDE_vTaskSuspend == 1 )
\r
932 signed portBASE_TYPE xTaskIsTaskSuspended( xTaskHandle xTask )
\r
934 portBASE_TYPE xReturn = pdFALSE;
\r
935 const tskTCB * const pxTCB = ( tskTCB * ) xTask;
\r
937 /* It does not make sense to check if the calling task is suspended. */
\r
938 configASSERT( xTask );
\r
940 /* Is the task we are attempting to resume actually in the
\r
942 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xGenericListItem ) ) != pdFALSE )
\r
944 /* Has the task already been resumed from within an ISR? */
\r
945 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdTRUE )
\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
951 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) == pdTRUE )
\r
962 /*-----------------------------------------------------------*/
\r
964 #if ( INCLUDE_vTaskSuspend == 1 )
\r
966 void vTaskResume( xTaskHandle pxTaskToResume )
\r
970 /* It does not make sense to resume the calling task. */
\r
971 configASSERT( pxTaskToResume );
\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
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
981 taskENTER_CRITICAL();
\r
983 if( xTaskIsTaskSuspended( pxTCB ) == pdTRUE )
\r
985 traceTASK_RESUME( pxTCB );
\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
992 /* We may have just resumed a higher priority task. */
\r
993 if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
\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
1001 taskEXIT_CRITICAL();
\r
1007 /*-----------------------------------------------------------*/
\r
1009 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
\r
1011 portBASE_TYPE xTaskResumeFromISR( xTaskHandle pxTaskToResume )
\r
1013 portBASE_TYPE xYieldRequired = pdFALSE;
\r
1015 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
1017 configASSERT( pxTaskToResume );
\r
1019 pxTCB = ( tskTCB * ) pxTaskToResume;
\r
1021 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
1023 if( xTaskIsTaskSuspended( pxTCB ) == pdTRUE )
\r
1025 traceTASK_RESUME_FROM_ISR( pxTCB );
\r
1027 if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
\r
1029 xYieldRequired = ( pxTCB->uxPriority >= pxCurrentTCB->uxPriority );
\r
1030 vListRemove( &( pxTCB->xGenericListItem ) );
\r
1031 prvAddTaskToReadyQueue( pxTCB );
\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
1042 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1044 return xYieldRequired;
\r
1052 /*-----------------------------------------------------------
\r
1053 * PUBLIC SCHEDULER CONTROL documented in task.h
\r
1054 *----------------------------------------------------------*/
\r
1057 void vTaskStartScheduler( void )
\r
1059 portBASE_TYPE xReturn;
\r
1061 /* Add the idle task at the lowest priority. */
\r
1062 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
\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
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
1075 #if ( configUSE_TIMERS == 1 )
\r
1077 if( xReturn == pdPASS )
\r
1079 xReturn = xTimerCreateTimerTask();
\r
1084 if( xReturn == pdPASS )
\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
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
1096 xSchedulerRunning = pdTRUE;
\r
1097 xTickCount = ( portTickType ) 0U;
\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
1104 /* Setting up the timer tick is hardware specific and thus in the
\r
1105 portable interface. */
\r
1106 if( xPortStartScheduler() != pdFALSE )
\r
1108 /* Should not reach here as if the scheduler is running the
\r
1109 function will not return. */
\r
1113 /* Should only reach here if a task calls xTaskEndScheduler(). */
\r
1117 /* This line will only be reached if the kernel could not be started. */
\r
1118 configASSERT( xReturn );
\r
1120 /*-----------------------------------------------------------*/
\r
1122 void vTaskEndScheduler( void )
\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
1131 /*----------------------------------------------------------*/
\r
1133 void vTaskSuspendAll( void )
\r
1135 /* A critical section is not required as the variable is of type
\r
1137 ++uxSchedulerSuspended;
\r
1139 /*----------------------------------------------------------*/
\r
1141 signed portBASE_TYPE xTaskResumeAll( void )
\r
1143 register tskTCB *pxTCB;
\r
1144 signed portBASE_TYPE xAlreadyYielded = pdFALSE;
\r
1146 /* If uxSchedulerSuspended is zero then this function does not match a
\r
1147 previous call to vTaskSuspendAll(). */
\r
1148 configASSERT( uxSchedulerSuspended );
\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
1157 --uxSchedulerSuspended;
\r
1159 if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
\r
1161 if( uxCurrentNumberOfTasks > ( unsigned portBASE_TYPE ) 0U )
\r
1163 portBASE_TYPE xYieldRequired = pdFALSE;
\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
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
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
1178 xYieldRequired = pdTRUE;
\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
1187 while( uxMissedTicks > ( unsigned portBASE_TYPE ) 0U )
\r
1189 vTaskIncrementTick();
\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
1198 xYieldRequired = pdTRUE;
\r
1203 if( ( xYieldRequired == pdTRUE ) || ( xMissedYield == pdTRUE ) )
\r
1205 xAlreadyYielded = pdTRUE;
\r
1206 xMissedYield = pdFALSE;
\r
1207 portYIELD_WITHIN_API();
\r
1212 taskEXIT_CRITICAL();
\r
1214 return xAlreadyYielded;
\r
1222 /*-----------------------------------------------------------
\r
1223 * PUBLIC TASK UTILITIES documented in task.h
\r
1224 *----------------------------------------------------------*/
\r
1228 portTickType xTaskGetTickCount( void )
\r
1230 portTickType xTicks;
\r
1232 /* Critical section required if running on a 16 bit processor. */
\r
1233 taskENTER_CRITICAL();
\r
1235 xTicks = xTickCount;
\r
1237 taskEXIT_CRITICAL();
\r
1241 /*-----------------------------------------------------------*/
\r
1243 portTickType xTaskGetTickCountFromISR( void )
\r
1245 portTickType xReturn;
\r
1246 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
1248 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
1249 xReturn = xTickCount;
\r
1250 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1254 /*-----------------------------------------------------------*/
\r
1256 unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
\r
1258 /* A critical section is not required because the variables are of type
\r
1260 return uxCurrentNumberOfTasks;
\r
1262 /*-----------------------------------------------------------*/
\r
1264 #if ( INCLUDE_pcTaskGetTaskName == 1 )
\r
1266 signed char *pcTaskGetTaskName( xTaskHandle xTaskToQuery )
\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
1277 /*-----------------------------------------------------------*/
\r
1279 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1281 void vTaskList( signed char *pcWriteBuffer )
\r
1283 unsigned portBASE_TYPE uxQueue;
\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
1288 vTaskSuspendAll();
\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
1293 *pcWriteBuffer = ( signed char ) 0x00;
\r
1294 strcat( ( char * ) pcWriteBuffer, ( const char * ) "\r\n" );
\r
1296 uxQueue = uxTopUsedPriority + ( unsigned portBASE_TYPE ) 1U;
\r
1302 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) == pdFALSE )
\r
1304 prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), tskREADY_CHAR );
\r
1306 }while( uxQueue > ( unsigned short ) tskIDLE_PRIORITY );
\r
1308 if( listLIST_IS_EMPTY( pxDelayedTaskList ) == pdFALSE )
\r
1310 prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, tskBLOCKED_CHAR );
\r
1313 if( listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) == pdFALSE )
\r
1315 prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, tskBLOCKED_CHAR );
\r
1318 #if( INCLUDE_vTaskDelete == 1 )
\r
1320 if( listLIST_IS_EMPTY( &xTasksWaitingTermination ) == pdFALSE )
\r
1322 prvListTaskWithinSingleList( pcWriteBuffer, &xTasksWaitingTermination, tskDELETED_CHAR );
\r
1327 #if ( INCLUDE_vTaskSuspend == 1 )
\r
1329 if( listLIST_IS_EMPTY( &xSuspendedTaskList ) == pdFALSE )
\r
1331 prvListTaskWithinSingleList( pcWriteBuffer, &xSuspendedTaskList, tskSUSPENDED_CHAR );
\r
1340 /*----------------------------------------------------------*/
\r
1342 #if ( configGENERATE_RUN_TIME_STATS == 1 )
\r
1344 void vTaskGetRunTimeStats( signed char *pcWriteBuffer )
\r
1346 unsigned portBASE_TYPE uxQueue;
\r
1347 unsigned long ulTotalRunTime;
\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
1352 vTaskSuspendAll();
\r
1354 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
\r
1355 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );
\r
1357 ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
\r
1360 /* Divide ulTotalRunTime by 100 to make the percentage caluclations
\r
1361 simpler in the prvGenerateRunTimeStatsForTasksInList() function. */
\r
1362 ulTotalRunTime /= 100UL;
\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
1368 *pcWriteBuffer = ( signed char ) 0x00;
\r
1369 strcat( ( char * ) pcWriteBuffer, ( const char * ) "\r\n" );
\r
1371 uxQueue = uxTopUsedPriority + ( unsigned portBASE_TYPE ) 1U;
\r
1377 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) == pdFALSE )
\r
1379 prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), ulTotalRunTime );
\r
1381 }while( uxQueue > ( unsigned short ) tskIDLE_PRIORITY );
\r
1383 if( listLIST_IS_EMPTY( pxDelayedTaskList ) == pdFALSE )
\r
1385 prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, ulTotalRunTime );
\r
1388 if( listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) == pdFALSE )
\r
1390 prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, ulTotalRunTime );
\r
1393 #if ( INCLUDE_vTaskDelete == 1 )
\r
1395 if( listLIST_IS_EMPTY( &xTasksWaitingTermination ) == pdFALSE )
\r
1397 prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, &xTasksWaitingTermination, ulTotalRunTime );
\r
1402 #if ( INCLUDE_vTaskSuspend == 1 )
\r
1404 if( listLIST_IS_EMPTY( &xSuspendedTaskList ) == pdFALSE )
\r
1406 prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, &xSuspendedTaskList, ulTotalRunTime );
\r
1415 /*----------------------------------------------------------*/
\r
1417 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
\r
1419 xTaskHandle xTaskGetIdleTaskHandle( void )
\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
1429 /*-----------------------------------------------------------
\r
1430 * SCHEDULER INTERNALS AVAILABLE FOR PORTING PURPOSES
\r
1431 * documented in task.h
\r
1432 *----------------------------------------------------------*/
\r
1434 void vTaskIncrementTick( void )
\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
1444 if( xTickCount == ( portTickType ) 0U )
\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
1451 configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) );
\r
1453 pxTemp = pxDelayedTaskList;
\r
1454 pxDelayedTaskList = pxOverflowDelayedTaskList;
\r
1455 pxOverflowDelayedTaskList = pxTemp;
\r
1456 xNumOfOverflows++;
\r
1458 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
\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
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
1478 /* See if this tick has made a timeout expire. */
\r
1479 prvCheckDelayedTasks();
\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
1489 vApplicationTickHook();
\r
1494 #if ( configUSE_TICK_HOOK == 1 )
\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
1500 vApplicationTickHook();
\r
1505 traceTASK_INCREMENT_TICK( xTickCount );
\r
1507 /*-----------------------------------------------------------*/
\r
1509 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
\r
1511 void vTaskSetApplicationTaskTag( xTaskHandle xTask, pdTASK_HOOK_CODE pxHookFunction )
\r
1515 /* If xTask is NULL then we are setting our own task hook. */
\r
1516 if( xTask == NULL )
\r
1518 xTCB = ( tskTCB * ) pxCurrentTCB;
\r
1522 xTCB = ( tskTCB * ) xTask;
\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
1533 /*-----------------------------------------------------------*/
\r
1535 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
\r
1537 pdTASK_HOOK_CODE xTaskGetApplicationTaskTag( xTaskHandle xTask )
\r
1540 pdTASK_HOOK_CODE xReturn;
\r
1542 /* If xTask is NULL then we are setting our own task hook. */
\r
1543 if( xTask == NULL )
\r
1545 xTCB = ( tskTCB * ) pxCurrentTCB;
\r
1549 xTCB = ( tskTCB * ) xTask;
\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
1562 /*-----------------------------------------------------------*/
\r
1564 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
\r
1566 portBASE_TYPE xTaskCallApplicationTaskHook( xTaskHandle xTask, void *pvParameter )
\r
1569 portBASE_TYPE xReturn;
\r
1571 /* If xTask is NULL then we are calling our own task hook. */
\r
1572 if( xTask == NULL )
\r
1574 xTCB = ( tskTCB * ) pxCurrentTCB;
\r
1578 xTCB = ( tskTCB * ) xTask;
\r
1581 if( xTCB->pxTaskTag != NULL )
\r
1583 xReturn = xTCB->pxTaskTag( pvParameter );
\r
1594 /*-----------------------------------------------------------*/
\r
1596 void vTaskSwitchContext( void )
\r
1598 if( uxSchedulerSuspended != ( unsigned portBASE_TYPE ) pdFALSE )
\r
1600 /* The scheduler is currently suspended - do not allow a context
\r
1602 xMissedYield = pdTRUE;
\r
1606 traceTASK_SWITCHED_OUT();
\r
1608 #if ( configGENERATE_RUN_TIME_STATS == 1 )
\r
1610 unsigned long ulTempCounter;
\r
1612 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
\r
1613 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTempCounter );
\r
1615 ulTempCounter = portGET_RUN_TIME_COUNTER_VALUE();
\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
1628 taskFIRST_CHECK_FOR_STACK_OVERFLOW();
\r
1629 taskSECOND_CHECK_FOR_STACK_OVERFLOW();
\r
1631 /* Find the highest priority queue that contains ready tasks. */
\r
1632 while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopReadyPriority ] ) ) )
\r
1634 configASSERT( uxTopReadyPriority );
\r
1635 --uxTopReadyPriority;
\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
1642 traceTASK_SWITCHED_IN();
\r
1645 /*-----------------------------------------------------------*/
\r
1647 void vTaskPlaceOnEventList( const xList * const pxEventList, portTickType xTicksToWait )
\r
1649 portTickType xTimeToWake;
\r
1651 configASSERT( pxEventList );
\r
1653 /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE
\r
1654 SCHEDULER SUSPENDED. */
\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
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
1667 #if ( INCLUDE_vTaskSuspend == 1 )
\r
1669 if( xTicksToWait == portMAX_DELAY )
\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
1674 vListInsertEnd( ( xList * ) &xSuspendedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
\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
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
1693 /*-----------------------------------------------------------*/
\r
1695 #if configUSE_TIMERS == 1
\r
1697 void vTaskPlaceOnEventListRestricted( const xList * const pxEventList, portTickType xTicksToWait )
\r
1699 portTickType xTimeToWake;
\r
1701 configASSERT( pxEventList );
\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
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
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
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
1726 #endif /* configUSE_TIMERS */
\r
1727 /*-----------------------------------------------------------*/
\r
1729 signed portBASE_TYPE xTaskRemoveFromEventList( const xList * const pxEventList )
\r
1731 tskTCB *pxUnblockedTCB;
\r
1732 portBASE_TYPE xReturn;
\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
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
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
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
1751 if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
\r
1753 vListRemove( &( pxUnblockedTCB->xGenericListItem ) );
\r
1754 prvAddTaskToReadyQueue( pxUnblockedTCB );
\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
1763 if( pxUnblockedTCB->uxPriority >= pxCurrentTCB->uxPriority )
\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
1773 xReturn = pdFALSE;
\r
1778 /*-----------------------------------------------------------*/
\r
1780 void vTaskSetTimeOutState( xTimeOutType * const pxTimeOut )
\r
1782 configASSERT( pxTimeOut );
\r
1783 pxTimeOut->xOverflowCount = xNumOfOverflows;
\r
1784 pxTimeOut->xTimeOnEntering = xTickCount;
\r
1786 /*-----------------------------------------------------------*/
\r
1788 portBASE_TYPE xTaskCheckForTimeOut( xTimeOutType * const pxTimeOut, portTickType * const pxTicksToWait )
\r
1790 portBASE_TYPE xReturn;
\r
1792 configASSERT( pxTimeOut );
\r
1793 configASSERT( pxTicksToWait );
\r
1795 taskENTER_CRITICAL();
\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
1803 xReturn = pdFALSE;
\r
1805 else /* We are not blocking indefinitely, perform the checks below. */
\r
1808 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( ( portTickType ) xTickCount >= ( portTickType ) pxTimeOut->xTimeOnEntering ) )
\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
1816 else if( ( ( portTickType ) ( ( portTickType ) xTickCount - ( portTickType ) pxTimeOut->xTimeOnEntering ) ) < ( portTickType ) *pxTicksToWait )
\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
1828 taskEXIT_CRITICAL();
\r
1832 /*-----------------------------------------------------------*/
\r
1834 void vTaskMissedYield( void )
\r
1836 xMissedYield = pdTRUE;
\r
1838 /*-----------------------------------------------------------*/
\r
1840 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1841 unsigned portBASE_TYPE uxTaskGetTaskNumber( xTaskHandle xTask )
\r
1843 unsigned portBASE_TYPE uxReturn;
\r
1846 if( xTask != NULL )
\r
1848 pxTCB = ( tskTCB * ) xTask;
\r
1849 uxReturn = pxTCB->uxTaskNumber;
\r
1859 /*-----------------------------------------------------------*/
\r
1861 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1862 void vTaskSetTaskNumber( xTaskHandle xTask, unsigned portBASE_TYPE uxHandle )
\r
1866 if( xTask != NULL )
\r
1868 pxTCB = ( tskTCB * ) xTask;
\r
1869 pxTCB->uxTaskNumber = uxHandle;
\r
1876 * -----------------------------------------------------------
\r
1878 * ----------------------------------------------------------
\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
1883 * void prvIdleTask( void *pvParameters );
\r
1886 static portTASK_FUNCTION( prvIdleTask, pvParameters )
\r
1888 /* Stop warnings. */
\r
1889 ( void ) pvParameters;
\r
1893 /* See if any tasks have been deleted. */
\r
1894 prvCheckTasksWaitingTermination();
\r
1896 #if ( configUSE_PREEMPTION == 0 )
\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
1906 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
\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
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
1924 #if ( configUSE_IDLE_HOOK == 1 )
\r
1926 extern void vApplicationIdleHook( void );
\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
1937 } /*lint !e715 pvParameters is not accessed but all task functions require the same prototype. */
\r
1945 /*-----------------------------------------------------------
\r
1946 * File private functions documented at the top of the file.
\r
1947 *----------------------------------------------------------*/
\r
1951 static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed char * const pcName, unsigned portBASE_TYPE uxPriority, const xMemoryRegion * const xRegions, unsigned short usStackDepth )
\r
1953 /* Store the function name in the TCB. */
\r
1954 #if configMAX_TASK_NAME_LEN > 1
\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
1960 pxTCB->pcTaskName[ ( unsigned short ) configMAX_TASK_NAME_LEN - ( unsigned short ) 1 ] = ( signed char ) '\0';
\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
1966 uxPriority = configMAX_PRIORITIES - ( unsigned portBASE_TYPE ) 1U;
\r
1969 pxTCB->uxPriority = uxPriority;
\r
1970 #if ( configUSE_MUTEXES == 1 )
\r
1972 pxTCB->uxBasePriority = uxPriority;
\r
1976 vListInitialiseItem( &( pxTCB->xGenericListItem ) );
\r
1977 vListInitialiseItem( &( pxTCB->xEventListItem ) );
\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
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
1987 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
\r
1989 pxTCB->uxCriticalNesting = ( unsigned portBASE_TYPE ) 0U;
\r
1993 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
\r
1995 pxTCB->pxTaskTag = NULL;
\r
1999 #if ( configGENERATE_RUN_TIME_STATS == 1 )
\r
2001 pxTCB->ulRunTimeCounter = 0UL;
\r
2005 #if ( portUSING_MPU_WRAPPERS == 1 )
\r
2007 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, pxTCB->pxStack, usStackDepth );
\r
2011 ( void ) xRegions;
\r
2012 ( void ) usStackDepth;
\r
2016 /*-----------------------------------------------------------*/
\r
2018 #if ( portUSING_MPU_WRAPPERS == 1 )
\r
2020 void vTaskAllocateMPURegions( xTaskHandle xTaskToModify, const xMemoryRegion * const xRegions )
\r
2024 if( xTaskToModify == pxCurrentTCB )
\r
2026 xTaskToModify = NULL;
\r
2029 /* If null is passed in here then we are deleting ourselves. */
\r
2030 pxTCB = prvGetTCBFromHandle( xTaskToModify );
\r
2032 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
\r
2034 /*-----------------------------------------------------------*/
\r
2037 static void prvInitialiseTaskLists( void )
\r
2039 unsigned portBASE_TYPE uxPriority;
\r
2041 for( uxPriority = ( unsigned portBASE_TYPE ) 0U; uxPriority < configMAX_PRIORITIES; uxPriority++ )
\r
2043 vListInitialise( ( xList * ) &( pxReadyTasksLists[ uxPriority ] ) );
\r
2046 vListInitialise( ( xList * ) &xDelayedTaskList1 );
\r
2047 vListInitialise( ( xList * ) &xDelayedTaskList2 );
\r
2048 vListInitialise( ( xList * ) &xPendingReadyList );
\r
2050 #if ( INCLUDE_vTaskDelete == 1 )
\r
2052 vListInitialise( ( xList * ) &xTasksWaitingTermination );
\r
2056 #if ( INCLUDE_vTaskSuspend == 1 )
\r
2058 vListInitialise( ( xList * ) &xSuspendedTaskList );
\r
2062 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
\r
2064 pxDelayedTaskList = &xDelayedTaskList1;
\r
2065 pxOverflowDelayedTaskList = &xDelayedTaskList2;
\r
2067 /*-----------------------------------------------------------*/
\r
2069 static void prvCheckTasksWaitingTermination( void )
\r
2071 #if ( INCLUDE_vTaskDelete == 1 )
\r
2073 portBASE_TYPE xListIsEmpty;
\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
2079 vTaskSuspendAll();
\r
2080 xListIsEmpty = listLIST_IS_EMPTY( &xTasksWaitingTermination );
\r
2083 if( xListIsEmpty == pdFALSE )
\r
2087 taskENTER_CRITICAL();
\r
2089 pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( ( ( xList * ) &xTasksWaitingTermination ) );
\r
2090 vListRemove( &( pxTCB->xGenericListItem ) );
\r
2091 --uxCurrentNumberOfTasks;
\r
2094 taskEXIT_CRITICAL();
\r
2096 prvDeleteTCB( pxTCB );
\r
2102 /*-----------------------------------------------------------*/
\r
2104 static void prvAddCurrentTaskToDelayedList( portTickType xTimeToWake )
\r
2106 /* The list item will be inserted in wake time order. */
\r
2107 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );
\r
2109 if( xTimeToWake < xTickCount )
\r
2111 /* Wake time has overflowed. Place this item in the overflow list. */
\r
2112 vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
\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
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
2122 if( xTimeToWake < xNextTaskUnblockTime )
\r
2124 xNextTaskUnblockTime = xTimeToWake;
\r
2128 /*-----------------------------------------------------------*/
\r
2130 static tskTCB *prvAllocateTCBAndStack( unsigned short usStackDepth, portSTACK_TYPE *puxStackBuffer )
\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
2138 if( pxNewTCB != NULL )
\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
2145 if( pxNewTCB->pxStack == NULL )
\r
2147 /* Could not allocate the stack. Delete the allocated TCB. */
\r
2148 vPortFree( pxNewTCB );
\r
2153 /* Just to help debugging. */
\r
2154 memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) usStackDepth * sizeof( portSTACK_TYPE ) );
\r
2160 /*-----------------------------------------------------------*/
\r
2162 #if ( configUSE_TRACE_FACILITY == 1 )
\r
2164 static void prvListTaskWithinSingleList( const signed char *pcWriteBuffer, xList *pxList, signed char cStatus )
\r
2166 volatile tskTCB *pxNextTCB, *pxFirstTCB;
\r
2167 unsigned short usStackRemaining;
\r
2168 PRIVILEGED_DATA static char pcStatusString[ configMAX_TASK_NAME_LEN + 30 ];
\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
2174 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
\r
2175 #if ( portSTACK_GROWTH > 0 )
\r
2177 usStackRemaining = usTaskCheckFreeStackSpace( ( unsigned char * ) pxNextTCB->pxEndOfStack );
\r
2181 usStackRemaining = usTaskCheckFreeStackSpace( ( unsigned char * ) pxNextTCB->pxStack );
\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
2188 } while( pxNextTCB != pxFirstTCB );
\r
2192 /*-----------------------------------------------------------*/
\r
2194 #if ( configGENERATE_RUN_TIME_STATS == 1 )
\r
2196 static void prvGenerateRunTimeStatsForTasksInList( const signed char *pcWriteBuffer, xList *pxList, unsigned long ulTotalRunTime )
\r
2198 volatile tskTCB *pxNextTCB, *pxFirstTCB;
\r
2199 unsigned long ulStatsAsPercentage;
\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
2205 /* Get next TCB in from the list. */
\r
2206 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
\r
2208 /* Divide by zero check. */
\r
2209 if( ulTotalRunTime > 0UL )
\r
2211 /* Has the task run at all? */
\r
2212 if( pxNextTCB->ulRunTimeCounter == 0UL )
\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
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
2224 if( ulStatsAsPercentage > 0UL )
\r
2226 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
\r
2228 sprintf( pcStatsString, ( char * ) "%s\t\t%lu\t\t%lu%%\r\n", pxNextTCB->pcTaskName, pxNextTCB->ulRunTimeCounter, ulStatsAsPercentage );
\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
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
2244 sprintf( pcStatsString, ( char * ) "%s\t\t%lu\t\t<1%%\r\n", pxNextTCB->pcTaskName, pxNextTCB->ulRunTimeCounter );
\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
2256 strcat( ( char * ) pcWriteBuffer, ( char * ) pcStatsString );
\r
2259 } while( pxNextTCB != pxFirstTCB );
\r
2263 /*-----------------------------------------------------------*/
\r
2265 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )
\r
2267 static unsigned short usTaskCheckFreeStackSpace( const unsigned char * pucStackByte )
\r
2269 register unsigned short usCount = 0U;
\r
2271 while( *pucStackByte == tskSTACK_FILL_BYTE )
\r
2273 pucStackByte -= portSTACK_GROWTH;
\r
2277 usCount /= sizeof( portSTACK_TYPE );
\r
2283 /*-----------------------------------------------------------*/
\r
2285 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
\r
2287 unsigned portBASE_TYPE uxTaskGetStackHighWaterMark( xTaskHandle xTask )
\r
2290 unsigned char *pcEndOfStack;
\r
2291 unsigned portBASE_TYPE uxReturn;
\r
2293 pxTCB = prvGetTCBFromHandle( xTask );
\r
2295 #if portSTACK_GROWTH < 0
\r
2297 pcEndOfStack = ( unsigned char * ) pxTCB->pxStack;
\r
2301 pcEndOfStack = ( unsigned char * ) pxTCB->pxEndOfStack;
\r
2305 uxReturn = ( unsigned portBASE_TYPE ) usTaskCheckFreeStackSpace( pcEndOfStack );
\r
2311 /*-----------------------------------------------------------*/
\r
2313 #if ( INCLUDE_vTaskDelete == 1 )
\r
2315 static void prvDeleteTCB( tskTCB *pxTCB )
\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
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
2331 /*-----------------------------------------------------------*/
\r
2333 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
\r
2335 xTaskHandle xTaskGetCurrentTaskHandle( void )
\r
2337 xTaskHandle xReturn;
\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
2349 /*-----------------------------------------------------------*/
\r
2351 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
\r
2353 portBASE_TYPE xTaskGetSchedulerState( void )
\r
2355 portBASE_TYPE xReturn;
\r
2357 if( xSchedulerRunning == pdFALSE )
\r
2359 xReturn = taskSCHEDULER_NOT_STARTED;
\r
2363 if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
\r
2365 xReturn = taskSCHEDULER_RUNNING;
\r
2369 xReturn = taskSCHEDULER_SUSPENDED;
\r
2377 /*-----------------------------------------------------------*/
\r
2379 #if ( configUSE_MUTEXES == 1 )
\r
2381 void vTaskPriorityInherit( xTaskHandle * const pxMutexHolder )
\r
2383 tskTCB * const pxTCB = ( tskTCB * ) pxMutexHolder;
\r
2385 configASSERT( pxMutexHolder );
\r
2387 if( pxTCB->uxPriority < pxCurrentTCB->uxPriority )
\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
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
2396 vListRemove( &( pxTCB->xGenericListItem ) );
\r
2398 /* Inherit the priority before being moved into the new list. */
\r
2399 pxTCB->uxPriority = pxCurrentTCB->uxPriority;
\r
2400 prvAddTaskToReadyQueue( pxTCB );
\r
2404 /* Just inherit the priority. */
\r
2405 pxTCB->uxPriority = pxCurrentTCB->uxPriority;
\r
2408 traceTASK_PRIORITY_INHERIT( pxTCB, pxCurrentTCB->uxPriority );
\r
2413 /*-----------------------------------------------------------*/
\r
2415 #if ( configUSE_MUTEXES == 1 )
\r
2417 void vTaskPriorityDisinherit( xTaskHandle * const pxMutexHolder )
\r
2419 tskTCB * const pxTCB = ( tskTCB * ) pxMutexHolder;
\r
2421 if( pxMutexHolder != NULL )
\r
2423 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
\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
2429 /* Disinherit the priority before adding the task into the new
\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
2440 /*-----------------------------------------------------------*/
\r
2442 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
\r
2444 void vTaskEnterCritical( void )
\r
2446 portDISABLE_INTERRUPTS();
\r
2448 if( xSchedulerRunning != pdFALSE )
\r
2450 ( pxCurrentTCB->uxCriticalNesting )++;
\r
2455 /*-----------------------------------------------------------*/
\r
2457 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
\r
2459 void vTaskExitCritical( void )
\r
2461 if( xSchedulerRunning != pdFALSE )
\r
2463 if( pxCurrentTCB->uxCriticalNesting > 0U )
\r
2465 ( pxCurrentTCB->uxCriticalNesting )--;
\r
2467 if( pxCurrentTCB->uxCriticalNesting == 0U )
\r
2469 portENABLE_INTERRUPTS();
\r
2476 /*-----------------------------------------------------------*/
\r