2 FreeRTOS V7.2.0 - 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 & ( ~portPRIVILEGE_BIT ) ) < 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_eTaskStateGet == 1 )
\r
738 eTaskState eTaskStateGet( xTaskHandle pxTask )
\r
740 eTaskState eReturn;
\r
741 xList *pxStateList;
\r
744 pxTCB = ( tskTCB * ) pxTask;
\r
746 if( pxTCB == pxCurrentTCB )
\r
748 /* The task calling this function is querying its own state. */
\r
749 eReturn = eRunning;
\r
753 taskENTER_CRITICAL();
\r
755 pxStateList = ( xList * ) listLIST_ITEM_CONTAINED( &( pxTCB->xGenericListItem ) );
\r
757 taskEXIT_CRITICAL();
\r
759 if( ( pxStateList == pxDelayedTaskList ) || ( pxStateList == pxOverflowDelayedTaskList ) )
\r
761 /* The task being queried is referenced from one of the Blocked
\r
763 eReturn = eBlocked;
\r
766 #if ( INCLUDE_vTaskSuspend == 1 )
\r
767 else if( pxStateList == &xSuspendedTaskList )
\r
769 /* The task being queried is referenced from the suspended
\r
771 eReturn = eSuspended;
\r
775 #if ( INCLUDE_vTaskDelete == 1 )
\r
776 else if( pxStateList == &xTasksWaitingTermination )
\r
778 /* The task being queried is referenced from the deleted
\r
780 eReturn = eDeleted;
\r
786 /* If the task is not in any other state, it must be in the
\r
787 Ready (including pending ready) state. */
\r
796 /*-----------------------------------------------------------*/
\r
798 #if ( INCLUDE_uxTaskPriorityGet == 1 )
\r
800 unsigned portBASE_TYPE uxTaskPriorityGet( xTaskHandle pxTask )
\r
803 unsigned portBASE_TYPE uxReturn;
\r
805 taskENTER_CRITICAL();
\r
807 /* If null is passed in here then we are changing the
\r
808 priority of the calling function. */
\r
809 pxTCB = prvGetTCBFromHandle( pxTask );
\r
810 uxReturn = pxTCB->uxPriority;
\r
812 taskEXIT_CRITICAL();
\r
818 /*-----------------------------------------------------------*/
\r
820 #if ( INCLUDE_vTaskPrioritySet == 1 )
\r
822 void vTaskPrioritySet( xTaskHandle pxTask, unsigned portBASE_TYPE uxNewPriority )
\r
825 unsigned portBASE_TYPE uxCurrentPriority;
\r
826 portBASE_TYPE xYieldRequired = pdFALSE;
\r
828 configASSERT( ( uxNewPriority < configMAX_PRIORITIES ) );
\r
830 /* Ensure the new priority is valid. */
\r
831 if( uxNewPriority >= configMAX_PRIORITIES )
\r
833 uxNewPriority = configMAX_PRIORITIES - ( unsigned portBASE_TYPE ) 1U;
\r
836 taskENTER_CRITICAL();
\r
838 if( pxTask == pxCurrentTCB )
\r
843 /* If null is passed in here then we are changing the
\r
844 priority of the calling function. */
\r
845 pxTCB = prvGetTCBFromHandle( pxTask );
\r
847 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
\r
849 #if ( configUSE_MUTEXES == 1 )
\r
851 uxCurrentPriority = pxTCB->uxBasePriority;
\r
855 uxCurrentPriority = pxTCB->uxPriority;
\r
859 if( uxCurrentPriority != uxNewPriority )
\r
861 /* The priority change may have readied a task of higher
\r
862 priority than the calling task. */
\r
863 if( uxNewPriority > uxCurrentPriority )
\r
865 if( pxTask != NULL )
\r
867 /* The priority of another task is being raised. If we
\r
868 were raising the priority of the currently running task
\r
869 there would be no need to switch as it must have already
\r
870 been the highest priority task. */
\r
871 xYieldRequired = pdTRUE;
\r
874 else if( pxTask == NULL )
\r
876 /* Setting our own priority down means there may now be another
\r
877 task of higher priority that is ready to execute. */
\r
878 xYieldRequired = pdTRUE;
\r
883 #if ( configUSE_MUTEXES == 1 )
\r
885 /* Only change the priority being used if the task is not
\r
886 currently using an inherited priority. */
\r
887 if( pxTCB->uxBasePriority == pxTCB->uxPriority )
\r
889 pxTCB->uxPriority = uxNewPriority;
\r
892 /* The base priority gets set whatever. */
\r
893 pxTCB->uxBasePriority = uxNewPriority;
\r
897 pxTCB->uxPriority = uxNewPriority;
\r
901 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( configMAX_PRIORITIES - ( portTickType ) uxNewPriority ) );
\r
903 /* If the task is in the blocked or suspended list we need do
\r
904 nothing more than change it's priority variable. However, if
\r
905 the task is in a ready list it needs to be removed and placed
\r
906 in the queue appropriate to its new priority. */
\r
907 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxCurrentPriority ] ), &( pxTCB->xGenericListItem ) ) )
\r
909 /* The task is currently in its ready list - remove before adding
\r
910 it to it's new ready list. As we are in a critical section we
\r
911 can do this even if the scheduler is suspended. */
\r
912 vListRemove( &( pxTCB->xGenericListItem ) );
\r
913 prvAddTaskToReadyQueue( pxTCB );
\r
916 if( xYieldRequired == pdTRUE )
\r
918 portYIELD_WITHIN_API();
\r
922 taskEXIT_CRITICAL();
\r
926 /*-----------------------------------------------------------*/
\r
928 #if ( INCLUDE_vTaskSuspend == 1 )
\r
930 void vTaskSuspend( xTaskHandle pxTaskToSuspend )
\r
934 taskENTER_CRITICAL();
\r
936 /* Ensure a yield is performed if the current task is being
\r
938 if( pxTaskToSuspend == pxCurrentTCB )
\r
940 pxTaskToSuspend = NULL;
\r
943 /* If null is passed in here then we are suspending ourselves. */
\r
944 pxTCB = prvGetTCBFromHandle( pxTaskToSuspend );
\r
946 traceTASK_SUSPEND( pxTCB );
\r
948 /* Remove task from the ready/delayed list and place in the suspended list. */
\r
949 vListRemove( &( pxTCB->xGenericListItem ) );
\r
951 /* Is the task waiting on an event also? */
\r
952 if( pxTCB->xEventListItem.pvContainer != NULL )
\r
954 vListRemove( &( pxTCB->xEventListItem ) );
\r
957 vListInsertEnd( ( xList * ) &xSuspendedTaskList, &( pxTCB->xGenericListItem ) );
\r
959 taskEXIT_CRITICAL();
\r
961 if( ( void * ) pxTaskToSuspend == NULL )
\r
963 if( xSchedulerRunning != pdFALSE )
\r
965 /* We have just suspended the current task. */
\r
966 portYIELD_WITHIN_API();
\r
970 /* The scheduler is not running, but the task that was pointed
\r
971 to by pxCurrentTCB has just been suspended and pxCurrentTCB
\r
972 must be adjusted to point to a different task. */
\r
973 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks )
\r
975 /* No other tasks are ready, so set pxCurrentTCB back to
\r
976 NULL so when the next task is created pxCurrentTCB will
\r
977 be set to point to it no matter what its relative priority
\r
979 pxCurrentTCB = NULL;
\r
983 vTaskSwitchContext();
\r
990 /*-----------------------------------------------------------*/
\r
992 #if ( INCLUDE_vTaskSuspend == 1 )
\r
994 signed portBASE_TYPE xTaskIsTaskSuspended( xTaskHandle xTask )
\r
996 portBASE_TYPE xReturn = pdFALSE;
\r
997 const tskTCB * const pxTCB = ( tskTCB * ) xTask;
\r
999 /* It does not make sense to check if the calling task is suspended. */
\r
1000 configASSERT( xTask );
\r
1002 /* Is the task we are attempting to resume actually in the
\r
1003 suspended list? */
\r
1004 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xGenericListItem ) ) != pdFALSE )
\r
1006 /* Has the task already been resumed from within an ISR? */
\r
1007 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdTRUE )
\r
1009 /* Is it in the suspended list because it is in the
\r
1010 Suspended state? It is possible to be in the suspended
\r
1011 list because it is blocked on a task with no timeout
\r
1013 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) == pdTRUE )
\r
1024 /*-----------------------------------------------------------*/
\r
1026 #if ( INCLUDE_vTaskSuspend == 1 )
\r
1028 void vTaskResume( xTaskHandle pxTaskToResume )
\r
1032 /* It does not make sense to resume the calling task. */
\r
1033 configASSERT( pxTaskToResume );
\r
1035 /* Remove the task from whichever list it is currently in, and place
\r
1036 it in the ready list. */
\r
1037 pxTCB = ( tskTCB * ) pxTaskToResume;
\r
1039 /* The parameter cannot be NULL as it is impossible to resume the
\r
1040 currently executing task. */
\r
1041 if( ( pxTCB != NULL ) && ( pxTCB != pxCurrentTCB ) )
\r
1043 taskENTER_CRITICAL();
\r
1045 if( xTaskIsTaskSuspended( pxTCB ) == pdTRUE )
\r
1047 traceTASK_RESUME( pxTCB );
\r
1049 /* As we are in a critical section we can access the ready
\r
1050 lists even if the scheduler is suspended. */
\r
1051 vListRemove( &( pxTCB->xGenericListItem ) );
\r
1052 prvAddTaskToReadyQueue( pxTCB );
\r
1054 /* We may have just resumed a higher priority task. */
\r
1055 if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
\r
1057 /* This yield may not cause the task just resumed to run, but
\r
1058 will leave the lists in the correct state for the next yield. */
\r
1059 portYIELD_WITHIN_API();
\r
1063 taskEXIT_CRITICAL();
\r
1069 /*-----------------------------------------------------------*/
\r
1071 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
\r
1073 portBASE_TYPE xTaskResumeFromISR( xTaskHandle pxTaskToResume )
\r
1075 portBASE_TYPE xYieldRequired = pdFALSE;
\r
1077 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
1079 configASSERT( pxTaskToResume );
\r
1081 pxTCB = ( tskTCB * ) pxTaskToResume;
\r
1083 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
1085 if( xTaskIsTaskSuspended( pxTCB ) == pdTRUE )
\r
1087 traceTASK_RESUME_FROM_ISR( pxTCB );
\r
1089 if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
\r
1091 xYieldRequired = ( pxTCB->uxPriority >= pxCurrentTCB->uxPriority );
\r
1092 vListRemove( &( pxTCB->xGenericListItem ) );
\r
1093 prvAddTaskToReadyQueue( pxTCB );
\r
1097 /* We cannot access the delayed or ready lists, so will hold this
\r
1098 task pending until the scheduler is resumed, at which point a
\r
1099 yield will be performed if necessary. */
\r
1100 vListInsertEnd( ( xList * ) &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
\r
1104 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1106 return xYieldRequired;
\r
1114 /*-----------------------------------------------------------
\r
1115 * PUBLIC SCHEDULER CONTROL documented in task.h
\r
1116 *----------------------------------------------------------*/
\r
1119 void vTaskStartScheduler( void )
\r
1121 portBASE_TYPE xReturn;
\r
1123 /* Add the idle task at the lowest priority. */
\r
1124 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
\r
1126 /* Create the idle task, storing its handle in xIdleTaskHandle so it can
\r
1127 be returned by the xTaskGetIdleTaskHandle() function. */
\r
1128 xReturn = xTaskCreate( prvIdleTask, ( signed char * ) "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), &xIdleTaskHandle );
\r
1132 /* Create the idle task without storing its handle. */
\r
1133 xReturn = xTaskCreate( prvIdleTask, ( signed char * ) "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), NULL );
\r
1137 #if ( configUSE_TIMERS == 1 )
\r
1139 if( xReturn == pdPASS )
\r
1141 xReturn = xTimerCreateTimerTask();
\r
1146 if( xReturn == pdPASS )
\r
1148 /* Interrupts are turned off here, to ensure a tick does not occur
\r
1149 before or during the call to xPortStartScheduler(). The stacks of
\r
1150 the created tasks contain a status word with interrupts switched on
\r
1151 so interrupts will automatically get re-enabled when the first task
\r
1154 STEPPING THROUGH HERE USING A DEBUGGER CAN CAUSE BIG PROBLEMS IF THE
\r
1155 DEBUGGER ALLOWS INTERRUPTS TO BE PROCESSED. */
\r
1156 portDISABLE_INTERRUPTS();
\r
1158 xSchedulerRunning = pdTRUE;
\r
1159 xTickCount = ( portTickType ) 0U;
\r
1161 /* If configGENERATE_RUN_TIME_STATS is defined then the following
\r
1162 macro must be defined to configure the timer/counter used to generate
\r
1163 the run time counter time base. */
\r
1164 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
\r
1166 /* Setting up the timer tick is hardware specific and thus in the
\r
1167 portable interface. */
\r
1168 if( xPortStartScheduler() != pdFALSE )
\r
1170 /* Should not reach here as if the scheduler is running the
\r
1171 function will not return. */
\r
1175 /* Should only reach here if a task calls xTaskEndScheduler(). */
\r
1179 /* This line will only be reached if the kernel could not be started. */
\r
1180 configASSERT( xReturn );
\r
1182 /*-----------------------------------------------------------*/
\r
1184 void vTaskEndScheduler( void )
\r
1186 /* Stop the scheduler interrupts and call the portable scheduler end
\r
1187 routine so the original ISRs can be restored if necessary. The port
\r
1188 layer must ensure interrupts enable bit is left in the correct state. */
\r
1189 portDISABLE_INTERRUPTS();
\r
1190 xSchedulerRunning = pdFALSE;
\r
1191 vPortEndScheduler();
\r
1193 /*----------------------------------------------------------*/
\r
1195 void vTaskSuspendAll( void )
\r
1197 /* A critical section is not required as the variable is of type
\r
1199 ++uxSchedulerSuspended;
\r
1201 /*----------------------------------------------------------*/
\r
1203 signed portBASE_TYPE xTaskResumeAll( void )
\r
1205 register tskTCB *pxTCB;
\r
1206 signed portBASE_TYPE xAlreadyYielded = pdFALSE;
\r
1208 /* If uxSchedulerSuspended is zero then this function does not match a
\r
1209 previous call to vTaskSuspendAll(). */
\r
1210 configASSERT( uxSchedulerSuspended );
\r
1212 /* It is possible that an ISR caused a task to be removed from an event
\r
1213 list while the scheduler was suspended. If this was the case then the
\r
1214 removed task will have been added to the xPendingReadyList. Once the
\r
1215 scheduler has been resumed it is safe to move all the pending ready
\r
1216 tasks from this list into their appropriate ready list. */
\r
1217 taskENTER_CRITICAL();
\r
1219 --uxSchedulerSuspended;
\r
1221 if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
\r
1223 if( uxCurrentNumberOfTasks > ( unsigned portBASE_TYPE ) 0U )
\r
1225 portBASE_TYPE xYieldRequired = pdFALSE;
\r
1227 /* Move any readied tasks from the pending list into the
\r
1228 appropriate ready list. */
\r
1229 while( listLIST_IS_EMPTY( ( xList * ) &xPendingReadyList ) == pdFALSE )
\r
1231 pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( ( ( xList * ) &xPendingReadyList ) );
\r
1232 vListRemove( &( pxTCB->xEventListItem ) );
\r
1233 vListRemove( &( pxTCB->xGenericListItem ) );
\r
1234 prvAddTaskToReadyQueue( pxTCB );
\r
1236 /* If we have moved a task that has a priority higher than
\r
1237 the current task then we should yield. */
\r
1238 if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
\r
1240 xYieldRequired = pdTRUE;
\r
1244 /* If any ticks occurred while the scheduler was suspended then
\r
1245 they should be processed now. This ensures the tick count does not
\r
1246 slip, and that any delayed tasks are resumed at the correct time. */
\r
1247 if( uxMissedTicks > ( unsigned portBASE_TYPE ) 0U )
\r
1249 while( uxMissedTicks > ( unsigned portBASE_TYPE ) 0U )
\r
1251 vTaskIncrementTick();
\r
1255 /* As we have processed some ticks it is appropriate to yield
\r
1256 to ensure the highest priority task that is ready to run is
\r
1257 the task actually running. */
\r
1258 #if configUSE_PREEMPTION == 1
\r
1260 xYieldRequired = pdTRUE;
\r
1265 if( ( xYieldRequired == pdTRUE ) || ( xMissedYield == pdTRUE ) )
\r
1267 xAlreadyYielded = pdTRUE;
\r
1268 xMissedYield = pdFALSE;
\r
1269 portYIELD_WITHIN_API();
\r
1274 taskEXIT_CRITICAL();
\r
1276 return xAlreadyYielded;
\r
1284 /*-----------------------------------------------------------
\r
1285 * PUBLIC TASK UTILITIES documented in task.h
\r
1286 *----------------------------------------------------------*/
\r
1290 portTickType xTaskGetTickCount( void )
\r
1292 portTickType xTicks;
\r
1294 /* Critical section required if running on a 16 bit processor. */
\r
1295 taskENTER_CRITICAL();
\r
1297 xTicks = xTickCount;
\r
1299 taskEXIT_CRITICAL();
\r
1303 /*-----------------------------------------------------------*/
\r
1305 portTickType xTaskGetTickCountFromISR( void )
\r
1307 portTickType xReturn;
\r
1308 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
1310 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
1311 xReturn = xTickCount;
\r
1312 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1316 /*-----------------------------------------------------------*/
\r
1318 unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
\r
1320 /* A critical section is not required because the variables are of type
\r
1322 return uxCurrentNumberOfTasks;
\r
1324 /*-----------------------------------------------------------*/
\r
1326 #if ( INCLUDE_pcTaskGetTaskName == 1 )
\r
1328 signed char *pcTaskGetTaskName( xTaskHandle xTaskToQuery )
\r
1332 /* If null is passed in here then the name of the calling task is being queried. */
\r
1333 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
\r
1334 configASSERT( pxTCB );
\r
1335 return &( pxTCB->pcTaskName[ 0 ] );
\r
1339 /*-----------------------------------------------------------*/
\r
1341 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1343 void vTaskList( signed char *pcWriteBuffer )
\r
1345 unsigned portBASE_TYPE uxQueue;
\r
1347 /* This is a VERY costly function that should be used for debug only.
\r
1348 It leaves interrupts disabled for a LONG time. */
\r
1350 vTaskSuspendAll();
\r
1352 /* Run through all the lists that could potentially contain a TCB and
\r
1353 report the task name, state and stack high water mark. */
\r
1355 *pcWriteBuffer = ( signed char ) 0x00;
\r
1356 strcat( ( char * ) pcWriteBuffer, ( const char * ) "\r\n" );
\r
1358 uxQueue = uxTopUsedPriority + ( unsigned portBASE_TYPE ) 1U;
\r
1364 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) == pdFALSE )
\r
1366 prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), tskREADY_CHAR );
\r
1368 }while( uxQueue > ( unsigned short ) tskIDLE_PRIORITY );
\r
1370 if( listLIST_IS_EMPTY( pxDelayedTaskList ) == pdFALSE )
\r
1372 prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, tskBLOCKED_CHAR );
\r
1375 if( listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) == pdFALSE )
\r
1377 prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, tskBLOCKED_CHAR );
\r
1380 #if( INCLUDE_vTaskDelete == 1 )
\r
1382 if( listLIST_IS_EMPTY( &xTasksWaitingTermination ) == pdFALSE )
\r
1384 prvListTaskWithinSingleList( pcWriteBuffer, &xTasksWaitingTermination, tskDELETED_CHAR );
\r
1389 #if ( INCLUDE_vTaskSuspend == 1 )
\r
1391 if( listLIST_IS_EMPTY( &xSuspendedTaskList ) == pdFALSE )
\r
1393 prvListTaskWithinSingleList( pcWriteBuffer, &xSuspendedTaskList, tskSUSPENDED_CHAR );
\r
1402 /*----------------------------------------------------------*/
\r
1404 #if ( configGENERATE_RUN_TIME_STATS == 1 )
\r
1406 void vTaskGetRunTimeStats( signed char *pcWriteBuffer )
\r
1408 unsigned portBASE_TYPE uxQueue;
\r
1409 unsigned long ulTotalRunTime;
\r
1411 /* This is a VERY costly function that should be used for debug only.
\r
1412 It leaves interrupts disabled for a LONG time. */
\r
1414 vTaskSuspendAll();
\r
1416 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
\r
1417 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );
\r
1419 ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
\r
1422 /* Divide ulTotalRunTime by 100 to make the percentage caluclations
\r
1423 simpler in the prvGenerateRunTimeStatsForTasksInList() function. */
\r
1424 ulTotalRunTime /= 100UL;
\r
1426 /* Run through all the lists that could potentially contain a TCB,
\r
1427 generating a table of run timer percentages in the provided
\r
1430 *pcWriteBuffer = ( signed char ) 0x00;
\r
1431 strcat( ( char * ) pcWriteBuffer, ( const char * ) "\r\n" );
\r
1433 uxQueue = uxTopUsedPriority + ( unsigned portBASE_TYPE ) 1U;
\r
1439 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) == pdFALSE )
\r
1441 prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), ulTotalRunTime );
\r
1443 }while( uxQueue > ( unsigned short ) tskIDLE_PRIORITY );
\r
1445 if( listLIST_IS_EMPTY( pxDelayedTaskList ) == pdFALSE )
\r
1447 prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, ulTotalRunTime );
\r
1450 if( listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) == pdFALSE )
\r
1452 prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, ulTotalRunTime );
\r
1455 #if ( INCLUDE_vTaskDelete == 1 )
\r
1457 if( listLIST_IS_EMPTY( &xTasksWaitingTermination ) == pdFALSE )
\r
1459 prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, &xTasksWaitingTermination, ulTotalRunTime );
\r
1464 #if ( INCLUDE_vTaskSuspend == 1 )
\r
1466 if( listLIST_IS_EMPTY( &xSuspendedTaskList ) == pdFALSE )
\r
1468 prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, &xSuspendedTaskList, ulTotalRunTime );
\r
1477 /*----------------------------------------------------------*/
\r
1479 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
\r
1481 xTaskHandle xTaskGetIdleTaskHandle( void )
\r
1483 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
\r
1484 started, then xIdleTaskHandle will be NULL. */
\r
1485 configASSERT( ( xIdleTaskHandle != NULL ) );
\r
1486 return xIdleTaskHandle;
\r
1491 /*-----------------------------------------------------------
\r
1492 * SCHEDULER INTERNALS AVAILABLE FOR PORTING PURPOSES
\r
1493 * documented in task.h
\r
1494 *----------------------------------------------------------*/
\r
1496 void vTaskIncrementTick( void )
\r
1500 /* Called by the portable layer each time a tick interrupt occurs.
\r
1501 Increments the tick then checks to see if the new tick value will cause any
\r
1502 tasks to be unblocked. */
\r
1503 if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
\r
1506 if( xTickCount == ( portTickType ) 0U )
\r
1510 /* Tick count has overflowed so we need to swap the delay lists.
\r
1511 If there are any items in pxDelayedTaskList here then there is
\r
1513 configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) );
\r
1515 pxTemp = pxDelayedTaskList;
\r
1516 pxDelayedTaskList = pxOverflowDelayedTaskList;
\r
1517 pxOverflowDelayedTaskList = pxTemp;
\r
1518 xNumOfOverflows++;
\r
1520 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
\r
1522 /* The new current delayed list is empty. Set
\r
1523 xNextTaskUnblockTime to the maximum possible value so it is
\r
1524 extremely unlikely that the
\r
1525 if( xTickCount >= xNextTaskUnblockTime ) test will pass until
\r
1526 there is an item in the delayed list. */
\r
1527 xNextTaskUnblockTime = portMAX_DELAY;
\r
1531 /* The new current delayed list is not empty, get the value of
\r
1532 the item at the head of the delayed list. This is the time at
\r
1533 which the task at the head of the delayed list should be removed
\r
1534 from the Blocked state. */
\r
1535 pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );
\r
1536 xNextTaskUnblockTime = listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) );
\r
1540 /* See if this tick has made a timeout expire. */
\r
1541 prvCheckDelayedTasks();
\r
1547 /* The tick hook gets called at regular intervals, even if the
\r
1548 scheduler is locked. */
\r
1549 #if ( configUSE_TICK_HOOK == 1 )
\r
1551 vApplicationTickHook();
\r
1556 #if ( configUSE_TICK_HOOK == 1 )
\r
1558 /* Guard against the tick hook being called when the missed tick
\r
1559 count is being unwound (when the scheduler is being unlocked. */
\r
1560 if( uxMissedTicks == ( unsigned portBASE_TYPE ) 0U )
\r
1562 vApplicationTickHook();
\r
1567 traceTASK_INCREMENT_TICK( xTickCount );
\r
1569 /*-----------------------------------------------------------*/
\r
1571 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
\r
1573 void vTaskSetApplicationTaskTag( xTaskHandle xTask, pdTASK_HOOK_CODE pxHookFunction )
\r
1577 /* If xTask is NULL then we are setting our own task hook. */
\r
1578 if( xTask == NULL )
\r
1580 xTCB = ( tskTCB * ) pxCurrentTCB;
\r
1584 xTCB = ( tskTCB * ) xTask;
\r
1587 /* Save the hook function in the TCB. A critical section is required as
\r
1588 the value can be accessed from an interrupt. */
\r
1589 taskENTER_CRITICAL();
\r
1590 xTCB->pxTaskTag = pxHookFunction;
\r
1591 taskEXIT_CRITICAL();
\r
1595 /*-----------------------------------------------------------*/
\r
1597 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
\r
1599 pdTASK_HOOK_CODE xTaskGetApplicationTaskTag( xTaskHandle xTask )
\r
1602 pdTASK_HOOK_CODE xReturn;
\r
1604 /* If xTask is NULL then we are setting our own task hook. */
\r
1605 if( xTask == NULL )
\r
1607 xTCB = ( tskTCB * ) pxCurrentTCB;
\r
1611 xTCB = ( tskTCB * ) xTask;
\r
1614 /* Save the hook function in the TCB. A critical section is required as
\r
1615 the value can be accessed from an interrupt. */
\r
1616 taskENTER_CRITICAL();
\r
1617 xReturn = xTCB->pxTaskTag;
\r
1618 taskEXIT_CRITICAL();
\r
1624 /*-----------------------------------------------------------*/
\r
1626 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
\r
1628 portBASE_TYPE xTaskCallApplicationTaskHook( xTaskHandle xTask, void *pvParameter )
\r
1631 portBASE_TYPE xReturn;
\r
1633 /* If xTask is NULL then we are calling our own task hook. */
\r
1634 if( xTask == NULL )
\r
1636 xTCB = ( tskTCB * ) pxCurrentTCB;
\r
1640 xTCB = ( tskTCB * ) xTask;
\r
1643 if( xTCB->pxTaskTag != NULL )
\r
1645 xReturn = xTCB->pxTaskTag( pvParameter );
\r
1656 /*-----------------------------------------------------------*/
\r
1658 void vTaskSwitchContext( void )
\r
1660 if( uxSchedulerSuspended != ( unsigned portBASE_TYPE ) pdFALSE )
\r
1662 /* The scheduler is currently suspended - do not allow a context
\r
1664 xMissedYield = pdTRUE;
\r
1668 traceTASK_SWITCHED_OUT();
\r
1670 #if ( configGENERATE_RUN_TIME_STATS == 1 )
\r
1672 unsigned long ulTempCounter;
\r
1674 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
\r
1675 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTempCounter );
\r
1677 ulTempCounter = portGET_RUN_TIME_COUNTER_VALUE();
\r
1680 /* Add the amount of time the task has been running to the accumulated
\r
1681 time so far. The time the task started running was stored in
\r
1682 ulTaskSwitchedInTime. Note that there is no overflow protection here
\r
1683 so count values are only valid until the timer overflows. Generally
\r
1684 this will be about 1 hour assuming a 1uS timer increment. */
\r
1685 pxCurrentTCB->ulRunTimeCounter += ( ulTempCounter - ulTaskSwitchedInTime );
\r
1686 ulTaskSwitchedInTime = ulTempCounter;
\r
1690 taskFIRST_CHECK_FOR_STACK_OVERFLOW();
\r
1691 taskSECOND_CHECK_FOR_STACK_OVERFLOW();
\r
1693 /* Find the highest priority queue that contains ready tasks. */
\r
1694 while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopReadyPriority ] ) ) )
\r
1696 configASSERT( uxTopReadyPriority );
\r
1697 --uxTopReadyPriority;
\r
1700 /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the tasks of the
\r
1701 same priority get an equal share of the processor time. */
\r
1702 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopReadyPriority ] ) );
\r
1704 traceTASK_SWITCHED_IN();
\r
1707 /*-----------------------------------------------------------*/
\r
1709 void vTaskPlaceOnEventList( const xList * const pxEventList, portTickType xTicksToWait )
\r
1711 portTickType xTimeToWake;
\r
1713 configASSERT( pxEventList );
\r
1715 /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE
\r
1716 SCHEDULER SUSPENDED. */
\r
1718 /* Place the event list item of the TCB in the appropriate event list.
\r
1719 This is placed in the list in priority order so the highest priority task
\r
1720 is the first to be woken by the event. */
\r
1721 vListInsert( ( xList * ) pxEventList, ( xListItem * ) &( pxCurrentTCB->xEventListItem ) );
\r
1723 /* We must remove ourselves from the ready list before adding ourselves
\r
1724 to the blocked list as the same list item is used for both lists. We have
\r
1725 exclusive access to the ready lists as the scheduler is locked. */
\r
1726 vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
\r
1729 #if ( INCLUDE_vTaskSuspend == 1 )
\r
1731 if( xTicksToWait == portMAX_DELAY )
\r
1733 /* Add ourselves to the suspended task list instead of a delayed task
\r
1734 list to ensure we are not woken by a timing event. We will block
\r
1736 vListInsertEnd( ( xList * ) &xSuspendedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
\r
1740 /* Calculate the time at which the task should be woken if the event does
\r
1741 not occur. This may overflow but this doesn't matter. */
\r
1742 xTimeToWake = xTickCount + xTicksToWait;
\r
1743 prvAddCurrentTaskToDelayedList( xTimeToWake );
\r
1748 /* Calculate the time at which the task should be woken if the event does
\r
1749 not occur. This may overflow but this doesn't matter. */
\r
1750 xTimeToWake = xTickCount + xTicksToWait;
\r
1751 prvAddCurrentTaskToDelayedList( xTimeToWake );
\r
1755 /*-----------------------------------------------------------*/
\r
1757 #if configUSE_TIMERS == 1
\r
1759 void vTaskPlaceOnEventListRestricted( const xList * const pxEventList, portTickType xTicksToWait )
\r
1761 portTickType xTimeToWake;
\r
1763 configASSERT( pxEventList );
\r
1765 /* This function should not be called by application code hence the
\r
1766 'Restricted' in its name. It is not part of the public API. It is
\r
1767 designed for use by kernel code, and has special calling requirements -
\r
1768 it should be called from a critical section. */
\r
1771 /* Place the event list item of the TCB in the appropriate event list.
\r
1772 In this case it is assume that this is the only task that is going to
\r
1773 be waiting on this event list, so the faster vListInsertEnd() function
\r
1774 can be used in place of vListInsert. */
\r
1775 vListInsertEnd( ( xList * ) pxEventList, ( xListItem * ) &( pxCurrentTCB->xEventListItem ) );
\r
1777 /* We must remove this task from the ready list before adding it to the
\r
1778 blocked list as the same list item is used for both lists. This
\r
1779 function is called form a critical section. */
\r
1780 vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
\r
1782 /* Calculate the time at which the task should be woken if the event does
\r
1783 not occur. This may overflow but this doesn't matter. */
\r
1784 xTimeToWake = xTickCount + xTicksToWait;
\r
1785 prvAddCurrentTaskToDelayedList( xTimeToWake );
\r
1788 #endif /* configUSE_TIMERS */
\r
1789 /*-----------------------------------------------------------*/
\r
1791 signed portBASE_TYPE xTaskRemoveFromEventList( const xList * const pxEventList )
\r
1793 tskTCB *pxUnblockedTCB;
\r
1794 portBASE_TYPE xReturn;
\r
1796 /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE
\r
1797 SCHEDULER SUSPENDED. It can also be called from within an ISR. */
\r
1799 /* The event list is sorted in priority order, so we can remove the
\r
1800 first in the list, remove the TCB from the delayed list, and add
\r
1801 it to the ready list.
\r
1803 If an event is for a queue that is locked then this function will never
\r
1804 get called - the lock count on the queue will get modified instead. This
\r
1805 means we can always expect exclusive access to the event list here.
\r
1807 This function assumes that a check has already been made to ensure that
\r
1808 pxEventList is not empty. */
\r
1809 pxUnblockedTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
\r
1810 configASSERT( pxUnblockedTCB );
\r
1811 vListRemove( &( pxUnblockedTCB->xEventListItem ) );
\r
1813 if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
\r
1815 vListRemove( &( pxUnblockedTCB->xGenericListItem ) );
\r
1816 prvAddTaskToReadyQueue( pxUnblockedTCB );
\r
1820 /* We cannot access the delayed or ready lists, so will hold this
\r
1821 task pending until the scheduler is resumed. */
\r
1822 vListInsertEnd( ( xList * ) &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
\r
1825 if( pxUnblockedTCB->uxPriority >= pxCurrentTCB->uxPriority )
\r
1827 /* Return true if the task removed from the event list has
\r
1828 a higher priority than the calling task. This allows
\r
1829 the calling task to know if it should force a context
\r
1835 xReturn = pdFALSE;
\r
1840 /*-----------------------------------------------------------*/
\r
1842 void vTaskSetTimeOutState( xTimeOutType * const pxTimeOut )
\r
1844 configASSERT( pxTimeOut );
\r
1845 pxTimeOut->xOverflowCount = xNumOfOverflows;
\r
1846 pxTimeOut->xTimeOnEntering = xTickCount;
\r
1848 /*-----------------------------------------------------------*/
\r
1850 portBASE_TYPE xTaskCheckForTimeOut( xTimeOutType * const pxTimeOut, portTickType * const pxTicksToWait )
\r
1852 portBASE_TYPE xReturn;
\r
1854 configASSERT( pxTimeOut );
\r
1855 configASSERT( pxTicksToWait );
\r
1857 taskENTER_CRITICAL();
\r
1859 #if ( INCLUDE_vTaskSuspend == 1 )
\r
1860 /* If INCLUDE_vTaskSuspend is set to 1 and the block time specified is
\r
1861 the maximum block time then the task should block indefinitely, and
\r
1862 therefore never time out. */
\r
1863 if( *pxTicksToWait == portMAX_DELAY )
\r
1865 xReturn = pdFALSE;
\r
1867 else /* We are not blocking indefinitely, perform the checks below. */
\r
1870 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( ( portTickType ) xTickCount >= ( portTickType ) pxTimeOut->xTimeOnEntering ) )
\r
1872 /* The tick count is greater than the time at which vTaskSetTimeout()
\r
1873 was called, but has also overflowed since vTaskSetTimeOut() was called.
\r
1874 It must have wrapped all the way around and gone past us again. This
\r
1875 passed since vTaskSetTimeout() was called. */
\r
1878 else if( ( ( portTickType ) ( ( portTickType ) xTickCount - ( portTickType ) pxTimeOut->xTimeOnEntering ) ) < ( portTickType ) *pxTicksToWait )
\r
1880 /* Not a genuine timeout. Adjust parameters for time remaining. */
\r
1881 *pxTicksToWait -= ( ( portTickType ) xTickCount - ( portTickType ) pxTimeOut->xTimeOnEntering );
\r
1882 vTaskSetTimeOutState( pxTimeOut );
\r
1883 xReturn = pdFALSE;
\r
1890 taskEXIT_CRITICAL();
\r
1894 /*-----------------------------------------------------------*/
\r
1896 void vTaskMissedYield( void )
\r
1898 xMissedYield = pdTRUE;
\r
1900 /*-----------------------------------------------------------*/
\r
1902 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1903 unsigned portBASE_TYPE uxTaskGetTaskNumber( xTaskHandle xTask )
\r
1905 unsigned portBASE_TYPE uxReturn;
\r
1908 if( xTask != NULL )
\r
1910 pxTCB = ( tskTCB * ) xTask;
\r
1911 uxReturn = pxTCB->uxTaskNumber;
\r
1921 /*-----------------------------------------------------------*/
\r
1923 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1924 void vTaskSetTaskNumber( xTaskHandle xTask, unsigned portBASE_TYPE uxHandle )
\r
1928 if( xTask != NULL )
\r
1930 pxTCB = ( tskTCB * ) xTask;
\r
1931 pxTCB->uxTaskNumber = uxHandle;
\r
1938 * -----------------------------------------------------------
\r
1940 * ----------------------------------------------------------
\r
1942 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
\r
1943 * language extensions. The equivalent prototype for this function is:
\r
1945 * void prvIdleTask( void *pvParameters );
\r
1948 static portTASK_FUNCTION( prvIdleTask, pvParameters )
\r
1950 /* Stop warnings. */
\r
1951 ( void ) pvParameters;
\r
1955 /* See if any tasks have been deleted. */
\r
1956 prvCheckTasksWaitingTermination();
\r
1958 #if ( configUSE_PREEMPTION == 0 )
\r
1960 /* If we are not using preemption we keep forcing a task switch to
\r
1961 see if any other task has become available. If we are using
\r
1962 preemption we don't need to do this as any task becoming available
\r
1963 will automatically get the processor anyway. */
\r
1968 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
\r
1970 /* When using preemption tasks of equal priority will be
\r
1971 timesliced. If a task that is sharing the idle priority is ready
\r
1972 to run then the idle task should yield before the end of the
\r
1975 A critical region is not required here as we are just reading from
\r
1976 the list, and an occasional incorrect value will not matter. If
\r
1977 the ready list at the idle priority contains more than one task
\r
1978 then a task other than the idle task is ready to execute. */
\r
1979 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( unsigned portBASE_TYPE ) 1 )
\r
1986 #if ( configUSE_IDLE_HOOK == 1 )
\r
1988 extern void vApplicationIdleHook( void );
\r
1990 /* Call the user defined function from within the idle task. This
\r
1991 allows the application designer to add background functionality
\r
1992 without the overhead of a separate task.
\r
1993 NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
\r
1994 CALL A FUNCTION THAT MIGHT BLOCK. */
\r
1995 vApplicationIdleHook();
\r
1999 } /*lint !e715 pvParameters is not accessed but all task functions require the same prototype. */
\r
2007 /*-----------------------------------------------------------
\r
2008 * File private functions documented at the top of the file.
\r
2009 *----------------------------------------------------------*/
\r
2013 static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed char * const pcName, unsigned portBASE_TYPE uxPriority, const xMemoryRegion * const xRegions, unsigned short usStackDepth )
\r
2015 /* Store the function name in the TCB. */
\r
2016 #if configMAX_TASK_NAME_LEN > 1
\r
2018 /* Don't bring strncpy into the build unnecessarily. */
\r
2019 strncpy( ( char * ) pxTCB->pcTaskName, ( const char * ) pcName, ( unsigned short ) configMAX_TASK_NAME_LEN );
\r
2022 pxTCB->pcTaskName[ ( unsigned short ) configMAX_TASK_NAME_LEN - ( unsigned short ) 1 ] = ( signed char ) '\0';
\r
2024 /* This is used as an array index so must ensure it's not too large. First
\r
2025 remove the privilege bit if one is present. */
\r
2026 if( uxPriority >= configMAX_PRIORITIES )
\r
2028 uxPriority = configMAX_PRIORITIES - ( unsigned portBASE_TYPE ) 1U;
\r
2031 pxTCB->uxPriority = uxPriority;
\r
2032 #if ( configUSE_MUTEXES == 1 )
\r
2034 pxTCB->uxBasePriority = uxPriority;
\r
2038 vListInitialiseItem( &( pxTCB->xGenericListItem ) );
\r
2039 vListInitialiseItem( &( pxTCB->xEventListItem ) );
\r
2041 /* Set the pxTCB as a link back from the xListItem. This is so we can get
\r
2042 back to the containing TCB from a generic item in a list. */
\r
2043 listSET_LIST_ITEM_OWNER( &( pxTCB->xGenericListItem ), pxTCB );
\r
2045 /* Event lists are always in priority order. */
\r
2046 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );
\r
2047 listSET_LIST_ITEM_OWNER( &( pxTCB->xEventListItem ), pxTCB );
\r
2049 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
\r
2051 pxTCB->uxCriticalNesting = ( unsigned portBASE_TYPE ) 0U;
\r
2055 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
\r
2057 pxTCB->pxTaskTag = NULL;
\r
2061 #if ( configGENERATE_RUN_TIME_STATS == 1 )
\r
2063 pxTCB->ulRunTimeCounter = 0UL;
\r
2067 #if ( portUSING_MPU_WRAPPERS == 1 )
\r
2069 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, pxTCB->pxStack, usStackDepth );
\r
2073 ( void ) xRegions;
\r
2074 ( void ) usStackDepth;
\r
2078 /*-----------------------------------------------------------*/
\r
2080 #if ( portUSING_MPU_WRAPPERS == 1 )
\r
2082 void vTaskAllocateMPURegions( xTaskHandle xTaskToModify, const xMemoryRegion * const xRegions )
\r
2086 if( xTaskToModify == pxCurrentTCB )
\r
2088 xTaskToModify = NULL;
\r
2091 /* If null is passed in here then we are deleting ourselves. */
\r
2092 pxTCB = prvGetTCBFromHandle( xTaskToModify );
\r
2094 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
\r
2096 /*-----------------------------------------------------------*/
\r
2099 static void prvInitialiseTaskLists( void )
\r
2101 unsigned portBASE_TYPE uxPriority;
\r
2103 for( uxPriority = ( unsigned portBASE_TYPE ) 0U; uxPriority < configMAX_PRIORITIES; uxPriority++ )
\r
2105 vListInitialise( ( xList * ) &( pxReadyTasksLists[ uxPriority ] ) );
\r
2108 vListInitialise( ( xList * ) &xDelayedTaskList1 );
\r
2109 vListInitialise( ( xList * ) &xDelayedTaskList2 );
\r
2110 vListInitialise( ( xList * ) &xPendingReadyList );
\r
2112 #if ( INCLUDE_vTaskDelete == 1 )
\r
2114 vListInitialise( ( xList * ) &xTasksWaitingTermination );
\r
2118 #if ( INCLUDE_vTaskSuspend == 1 )
\r
2120 vListInitialise( ( xList * ) &xSuspendedTaskList );
\r
2124 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
\r
2126 pxDelayedTaskList = &xDelayedTaskList1;
\r
2127 pxOverflowDelayedTaskList = &xDelayedTaskList2;
\r
2129 /*-----------------------------------------------------------*/
\r
2131 static void prvCheckTasksWaitingTermination( void )
\r
2133 #if ( INCLUDE_vTaskDelete == 1 )
\r
2135 portBASE_TYPE xListIsEmpty;
\r
2137 /* ucTasksDeleted is used to prevent vTaskSuspendAll() being called
\r
2138 too often in the idle task. */
\r
2139 if( uxTasksDeleted > ( unsigned portBASE_TYPE ) 0U )
\r
2141 vTaskSuspendAll();
\r
2142 xListIsEmpty = listLIST_IS_EMPTY( &xTasksWaitingTermination );
\r
2145 if( xListIsEmpty == pdFALSE )
\r
2149 taskENTER_CRITICAL();
\r
2151 pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( ( ( xList * ) &xTasksWaitingTermination ) );
\r
2152 vListRemove( &( pxTCB->xGenericListItem ) );
\r
2153 --uxCurrentNumberOfTasks;
\r
2156 taskEXIT_CRITICAL();
\r
2158 prvDeleteTCB( pxTCB );
\r
2164 /*-----------------------------------------------------------*/
\r
2166 static void prvAddCurrentTaskToDelayedList( portTickType xTimeToWake )
\r
2168 /* The list item will be inserted in wake time order. */
\r
2169 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );
\r
2171 if( xTimeToWake < xTickCount )
\r
2173 /* Wake time has overflowed. Place this item in the overflow list. */
\r
2174 vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
\r
2178 /* The wake time has not overflowed, so we can use the current block list. */
\r
2179 vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
\r
2181 /* If the task entering the blocked state was placed at the head of the
\r
2182 list of blocked tasks then xNextTaskUnblockTime needs to be updated
\r
2184 if( xTimeToWake < xNextTaskUnblockTime )
\r
2186 xNextTaskUnblockTime = xTimeToWake;
\r
2190 /*-----------------------------------------------------------*/
\r
2192 static tskTCB *prvAllocateTCBAndStack( unsigned short usStackDepth, portSTACK_TYPE *puxStackBuffer )
\r
2196 /* Allocate space for the TCB. Where the memory comes from depends on
\r
2197 the implementation of the port malloc function. */
\r
2198 pxNewTCB = ( tskTCB * ) pvPortMalloc( sizeof( tskTCB ) );
\r
2200 if( pxNewTCB != NULL )
\r
2202 /* Allocate space for the stack used by the task being created.
\r
2203 The base of the stack memory stored in the TCB so the task can
\r
2204 be deleted later if required. */
\r
2205 pxNewTCB->pxStack = ( portSTACK_TYPE * ) pvPortMallocAligned( ( ( ( size_t )usStackDepth ) * sizeof( portSTACK_TYPE ) ), puxStackBuffer );
\r
2207 if( pxNewTCB->pxStack == NULL )
\r
2209 /* Could not allocate the stack. Delete the allocated TCB. */
\r
2210 vPortFree( pxNewTCB );
\r
2215 /* Just to help debugging. */
\r
2216 memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) usStackDepth * sizeof( portSTACK_TYPE ) );
\r
2222 /*-----------------------------------------------------------*/
\r
2224 #if ( configUSE_TRACE_FACILITY == 1 )
\r
2226 static void prvListTaskWithinSingleList( const signed char *pcWriteBuffer, xList *pxList, signed char cStatus )
\r
2228 volatile tskTCB *pxNextTCB, *pxFirstTCB;
\r
2229 unsigned short usStackRemaining;
\r
2230 PRIVILEGED_DATA static char pcStatusString[ configMAX_TASK_NAME_LEN + 30 ];
\r
2232 /* Write the details of all the TCB's in pxList into the buffer. */
\r
2233 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
\r
2236 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
\r
2237 #if ( portSTACK_GROWTH > 0 )
\r
2239 usStackRemaining = usTaskCheckFreeStackSpace( ( unsigned char * ) pxNextTCB->pxEndOfStack );
\r
2243 usStackRemaining = usTaskCheckFreeStackSpace( ( unsigned char * ) pxNextTCB->pxStack );
\r
2247 sprintf( pcStatusString, ( char * ) "%s\t\t%c\t%u\t%u\t%u\r\n", pxNextTCB->pcTaskName, cStatus, ( unsigned int ) pxNextTCB->uxPriority, usStackRemaining, ( unsigned int ) pxNextTCB->uxTCBNumber );
\r
2248 strcat( ( char * ) pcWriteBuffer, ( char * ) pcStatusString );
\r
2250 } while( pxNextTCB != pxFirstTCB );
\r
2254 /*-----------------------------------------------------------*/
\r
2256 #if ( configGENERATE_RUN_TIME_STATS == 1 )
\r
2258 static void prvGenerateRunTimeStatsForTasksInList( const signed char *pcWriteBuffer, xList *pxList, unsigned long ulTotalRunTime )
\r
2260 volatile tskTCB *pxNextTCB, *pxFirstTCB;
\r
2261 unsigned long ulStatsAsPercentage;
\r
2263 /* Write the run time stats of all the TCB's in pxList into the buffer. */
\r
2264 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
\r
2267 /* Get next TCB in from the list. */
\r
2268 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
\r
2270 /* Divide by zero check. */
\r
2271 if( ulTotalRunTime > 0UL )
\r
2273 /* Has the task run at all? */
\r
2274 if( pxNextTCB->ulRunTimeCounter == 0UL )
\r
2276 /* The task has used no CPU time at all. */
\r
2277 sprintf( pcStatsString, ( char * ) "%s\t\t0\t\t0%%\r\n", pxNextTCB->pcTaskName );
\r
2281 /* What percentage of the total run time has the task used?
\r
2282 This will always be rounded down to the nearest integer.
\r
2283 ulTotalRunTime has already been divided by 100. */
\r
2284 ulStatsAsPercentage = pxNextTCB->ulRunTimeCounter / ulTotalRunTime;
\r
2286 if( ulStatsAsPercentage > 0UL )
\r
2288 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
\r
2290 sprintf( pcStatsString, ( char * ) "%s\t\t%lu\t\t%lu%%\r\n", pxNextTCB->pcTaskName, pxNextTCB->ulRunTimeCounter, ulStatsAsPercentage );
\r
2294 /* sizeof( int ) == sizeof( long ) so a smaller
\r
2295 printf() library can be used. */
\r
2296 sprintf( pcStatsString, ( char * ) "%s\t\t%u\t\t%u%%\r\n", pxNextTCB->pcTaskName, ( unsigned int ) pxNextTCB->ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage );
\r
2302 /* If the percentage is zero here then the task has
\r
2303 consumed less than 1% of the total run time. */
\r
2304 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
\r
2306 sprintf( pcStatsString, ( char * ) "%s\t\t%lu\t\t<1%%\r\n", pxNextTCB->pcTaskName, pxNextTCB->ulRunTimeCounter );
\r
2310 /* sizeof( int ) == sizeof( long ) so a smaller
\r
2311 printf() library can be used. */
\r
2312 sprintf( pcStatsString, ( char * ) "%s\t\t%u\t\t<1%%\r\n", pxNextTCB->pcTaskName, ( unsigned int ) pxNextTCB->ulRunTimeCounter );
\r
2318 strcat( ( char * ) pcWriteBuffer, ( char * ) pcStatsString );
\r
2321 } while( pxNextTCB != pxFirstTCB );
\r
2325 /*-----------------------------------------------------------*/
\r
2327 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )
\r
2329 static unsigned short usTaskCheckFreeStackSpace( const unsigned char * pucStackByte )
\r
2331 register unsigned short usCount = 0U;
\r
2333 while( *pucStackByte == tskSTACK_FILL_BYTE )
\r
2335 pucStackByte -= portSTACK_GROWTH;
\r
2339 usCount /= sizeof( portSTACK_TYPE );
\r
2345 /*-----------------------------------------------------------*/
\r
2347 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
\r
2349 unsigned portBASE_TYPE uxTaskGetStackHighWaterMark( xTaskHandle xTask )
\r
2352 unsigned char *pcEndOfStack;
\r
2353 unsigned portBASE_TYPE uxReturn;
\r
2355 pxTCB = prvGetTCBFromHandle( xTask );
\r
2357 #if portSTACK_GROWTH < 0
\r
2359 pcEndOfStack = ( unsigned char * ) pxTCB->pxStack;
\r
2363 pcEndOfStack = ( unsigned char * ) pxTCB->pxEndOfStack;
\r
2367 uxReturn = ( unsigned portBASE_TYPE ) usTaskCheckFreeStackSpace( pcEndOfStack );
\r
2373 /*-----------------------------------------------------------*/
\r
2375 #if ( INCLUDE_vTaskDelete == 1 )
\r
2377 static void prvDeleteTCB( tskTCB *pxTCB )
\r
2379 /* This call is required specifically for the TriCore port. It must be
\r
2380 above the vPortFree() calls. The call is also used by ports/demos that
\r
2381 want to allocate and clean RAM statically. */
\r
2382 portCLEAN_UP_TCB( pxTCB );
\r
2384 /* Free up the memory allocated by the scheduler for the task. It is up to
\r
2385 the task to free any memory allocated at the application level. */
\r
2386 vPortFreeAligned( pxTCB->pxStack );
\r
2387 vPortFree( pxTCB );
\r
2393 /*-----------------------------------------------------------*/
\r
2395 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
\r
2397 xTaskHandle xTaskGetCurrentTaskHandle( void )
\r
2399 xTaskHandle xReturn;
\r
2401 /* A critical section is not required as this is not called from
\r
2402 an interrupt and the current TCB will always be the same for any
\r
2403 individual execution thread. */
\r
2404 xReturn = pxCurrentTCB;
\r
2411 /*-----------------------------------------------------------*/
\r
2413 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
\r
2415 portBASE_TYPE xTaskGetSchedulerState( void )
\r
2417 portBASE_TYPE xReturn;
\r
2419 if( xSchedulerRunning == pdFALSE )
\r
2421 xReturn = taskSCHEDULER_NOT_STARTED;
\r
2425 if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
\r
2427 xReturn = taskSCHEDULER_RUNNING;
\r
2431 xReturn = taskSCHEDULER_SUSPENDED;
\r
2439 /*-----------------------------------------------------------*/
\r
2441 #if ( configUSE_MUTEXES == 1 )
\r
2443 void vTaskPriorityInherit( xTaskHandle * const pxMutexHolder )
\r
2445 tskTCB * const pxTCB = ( tskTCB * ) pxMutexHolder;
\r
2447 /* If the mutex was given back by an interrupt while the queue was
\r
2448 locked then the mutex holder might now be NULL. */
\r
2449 if( pxMutexHolder != NULL )
\r
2451 if( pxTCB->uxPriority < pxCurrentTCB->uxPriority )
\r
2453 /* Adjust the mutex holder state to account for its new priority. */
\r
2454 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) pxCurrentTCB->uxPriority );
\r
2456 /* If the task being modified is in the ready state it will need to
\r
2457 be moved in to a new list. */
\r
2458 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxTCB->uxPriority ] ), &( pxTCB->xGenericListItem ) ) != pdFALSE )
\r
2460 vListRemove( &( pxTCB->xGenericListItem ) );
\r
2462 /* Inherit the priority before being moved into the new list. */
\r
2463 pxTCB->uxPriority = pxCurrentTCB->uxPriority;
\r
2464 prvAddTaskToReadyQueue( pxTCB );
\r
2468 /* Just inherit the priority. */
\r
2469 pxTCB->uxPriority = pxCurrentTCB->uxPriority;
\r
2472 traceTASK_PRIORITY_INHERIT( pxTCB, pxCurrentTCB->uxPriority );
\r
2478 /*-----------------------------------------------------------*/
\r
2480 #if ( configUSE_MUTEXES == 1 )
\r
2482 void vTaskPriorityDisinherit( xTaskHandle * const pxMutexHolder )
\r
2484 tskTCB * const pxTCB = ( tskTCB * ) pxMutexHolder;
\r
2486 if( pxMutexHolder != NULL )
\r
2488 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
\r
2490 /* We must be the running task to be able to give the mutex back.
\r
2491 Remove ourselves from the ready list we currently appear in. */
\r
2492 vListRemove( &( pxTCB->xGenericListItem ) );
\r
2494 /* Disinherit the priority before adding the task into the new
\r
2496 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
\r
2497 pxTCB->uxPriority = pxTCB->uxBasePriority;
\r
2498 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) pxTCB->uxPriority );
\r
2499 prvAddTaskToReadyQueue( pxTCB );
\r
2505 /*-----------------------------------------------------------*/
\r
2507 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
\r
2509 void vTaskEnterCritical( void )
\r
2511 portDISABLE_INTERRUPTS();
\r
2513 if( xSchedulerRunning != pdFALSE )
\r
2515 ( pxCurrentTCB->uxCriticalNesting )++;
\r
2520 /*-----------------------------------------------------------*/
\r
2522 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
\r
2524 void vTaskExitCritical( void )
\r
2526 if( xSchedulerRunning != pdFALSE )
\r
2528 if( pxCurrentTCB->uxCriticalNesting > 0U )
\r
2530 ( pxCurrentTCB->uxCriticalNesting )--;
\r
2532 if( pxCurrentTCB->uxCriticalNesting == 0U )
\r
2534 portENABLE_INTERRUPTS();
\r
2541 /*-----------------------------------------------------------*/
\r