2 FreeRTOS V7.0.0 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 FreeRTOS supports many tools and architectures. V7.0.0 is sponsored by:
\r
6 Atollic AB - Atollic provides professional embedded systems development
\r
7 tools for C/C++ development, code analysis and test automation.
\r
8 See http://www.atollic.com
\r
11 ***************************************************************************
\r
13 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
14 * Complete, revised, and edited pdf reference manuals are also *
\r
17 * Purchasing FreeRTOS documentation will not only help you, by *
\r
18 * ensuring you get running as quickly as possible and with an *
\r
19 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
20 * the FreeRTOS project to continue with its mission of providing *
\r
21 * professional grade, cross platform, de facto standard solutions *
\r
22 * for microcontrollers - completely free of charge! *
\r
24 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
26 * Thank you for using FreeRTOS, and thank you for your support! *
\r
28 ***************************************************************************
\r
31 This file is part of the FreeRTOS distribution.
\r
33 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
34 the terms of the GNU General Public License (version 2) as published by the
\r
35 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
36 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
37 distribute a combined work that includes FreeRTOS without being obliged to
\r
38 provide the source code for proprietary components outside of the FreeRTOS
\r
39 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
40 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
41 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
42 more details. You should have received a copy of the GNU General Public
\r
43 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
44 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
45 by writing to Richard Barry, contact details for whom are available on the
\r
50 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
53 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
56 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
57 licensing and training services.
\r
65 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
66 all the API functions to use the MPU wrappers. That should only be done when
\r
67 task.h is included from an application file. */
\r
68 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
70 #include "FreeRTOS.h"
\r
73 #include "StackMacros.h"
\r
75 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
78 * Macro to define the amount of stack available to the idle task.
\r
80 #define tskIDLE_STACK_SIZE configMINIMAL_STACK_SIZE
\r
83 * Task control block. A task control block (TCB) is allocated to each task,
\r
84 * and stores the context of the task.
\r
86 typedef struct tskTaskControlBlock
\r
88 volatile portSTACK_TYPE *pxTopOfStack; /*< Points to the location of the last item placed on the tasks stack. THIS MUST BE THE FIRST MEMBER OF THE STRUCT. */
\r
90 #if ( portUSING_MPU_WRAPPERS == 1 )
\r
91 xMPU_SETTINGS xMPUSettings; /*< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE STRUCT. */
\r
94 xListItem xGenericListItem; /*< List item used to place the TCB in ready and blocked queues. */
\r
95 xListItem xEventListItem; /*< List item used to place the TCB in event lists. */
\r
96 unsigned portBASE_TYPE uxPriority; /*< The priority of the task where 0 is the lowest priority. */
\r
97 portSTACK_TYPE *pxStack; /*< Points to the start of the stack. */
\r
98 signed char pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created. Facilitates debugging only. */
\r
100 #if ( portSTACK_GROWTH > 0 )
\r
101 portSTACK_TYPE *pxEndOfStack; /*< Used for stack overflow checking on architectures where the stack grows up from low memory. */
\r
104 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
\r
105 unsigned portBASE_TYPE uxCriticalNesting;
\r
108 #if ( configUSE_TRACE_FACILITY == 1 )
\r
109 unsigned portBASE_TYPE uxTCBNumber; /*< This is used for tracing the scheduler and making debugging easier only. */
\r
112 #if ( configUSE_MUTEXES == 1 )
\r
113 unsigned portBASE_TYPE uxBasePriority; /*< The priority last assigned to the task - used by the priority inheritance mechanism. */
\r
116 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
\r
117 pdTASK_HOOK_CODE pxTaskTag;
\r
120 #if ( configGENERATE_RUN_TIME_STATS == 1 )
\r
121 unsigned long ulRunTimeCounter; /*< Used for calculating how much CPU time each task is utilising. */
\r
128 * Some kernel aware debuggers require data to be viewed to be global, rather
\r
131 #ifdef portREMOVE_STATIC_QUALIFIER
\r
136 PRIVILEGED_DATA tskTCB * volatile pxCurrentTCB = NULL;
\r
138 /* Lists for ready and blocked tasks. --------------------*/
\r
140 PRIVILEGED_DATA static xList pxReadyTasksLists[ configMAX_PRIORITIES ]; /*< Prioritised ready tasks. */
\r
141 PRIVILEGED_DATA static xList xDelayedTaskList1; /*< Delayed tasks. */
\r
142 PRIVILEGED_DATA static xList xDelayedTaskList2; /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
\r
143 PRIVILEGED_DATA static xList * volatile pxDelayedTaskList ; /*< Points to the delayed task list currently being used. */
\r
144 PRIVILEGED_DATA static xList * volatile pxOverflowDelayedTaskList; /*< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */
\r
145 PRIVILEGED_DATA static xList xPendingReadyList; /*< Tasks that have been readied while the scheduler was suspended. They will be moved to the ready queue when the scheduler is resumed. */
\r
147 #if ( INCLUDE_vTaskDelete == 1 )
\r
149 PRIVILEGED_DATA static volatile xList xTasksWaitingTermination; /*< Tasks that have been deleted - but the their memory not yet freed. */
\r
150 PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxTasksDeleted = ( unsigned portBASE_TYPE ) 0;
\r
154 #if ( INCLUDE_vTaskSuspend == 1 )
\r
156 PRIVILEGED_DATA static xList xSuspendedTaskList; /*< Tasks that are currently suspended. */
\r
160 /* File private variables. --------------------------------*/
\r
161 PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxCurrentNumberOfTasks = ( unsigned portBASE_TYPE ) 0;
\r
162 PRIVILEGED_DATA static volatile portTickType xTickCount = ( portTickType ) 0;
\r
163 PRIVILEGED_DATA static unsigned portBASE_TYPE uxTopUsedPriority = tskIDLE_PRIORITY;
\r
164 PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxTopReadyPriority = tskIDLE_PRIORITY;
\r
165 PRIVILEGED_DATA static volatile signed portBASE_TYPE xSchedulerRunning = pdFALSE;
\r
166 PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxSchedulerSuspended = ( unsigned portBASE_TYPE ) pdFALSE;
\r
167 PRIVILEGED_DATA static volatile unsigned portBASE_TYPE uxMissedTicks = ( unsigned portBASE_TYPE ) 0;
\r
168 PRIVILEGED_DATA static volatile portBASE_TYPE xMissedYield = ( portBASE_TYPE ) pdFALSE;
\r
169 PRIVILEGED_DATA static volatile portBASE_TYPE xNumOfOverflows = ( portBASE_TYPE ) 0;
\r
170 PRIVILEGED_DATA static unsigned portBASE_TYPE uxTaskNumber = ( unsigned portBASE_TYPE ) 0;
\r
171 PRIVILEGED_DATA static portTickType xNextTaskUnblockTime = ( portTickType ) portMAX_DELAY;
\r
173 #if ( configGENERATE_RUN_TIME_STATS == 1 )
\r
175 PRIVILEGED_DATA static char pcStatsString[ 50 ] ;
\r
176 PRIVILEGED_DATA static unsigned long ulTaskSwitchedInTime = 0UL; /*< Holds the value of a timer/counter the last time a task was switched in. */
\r
177 static void prvGenerateRunTimeStatsForTasksInList( const signed char *pcWriteBuffer, xList *pxList, unsigned long ulTotalRunTime ) PRIVILEGED_FUNCTION;
\r
181 /* Debugging and trace facilities private variables and macros. ------------*/
\r
184 * The value used to fill the stack of a task when the task is created. This
\r
185 * is used purely for checking the high water mark for tasks.
\r
187 #define tskSTACK_FILL_BYTE ( 0xa5U )
\r
190 * Macros used by vListTask to indicate which state a task is in.
\r
192 #define tskBLOCKED_CHAR ( ( signed char ) 'B' )
\r
193 #define tskREADY_CHAR ( ( signed char ) 'R' )
\r
194 #define tskDELETED_CHAR ( ( signed char ) 'D' )
\r
195 #define tskSUSPENDED_CHAR ( ( signed char ) 'S' )
\r
198 * Macros and private variables used by the trace facility.
\r
200 #if ( configUSE_TRACE_FACILITY == 1 )
\r
202 #define tskSIZE_OF_EACH_TRACE_LINE ( ( unsigned long ) ( sizeof( unsigned long ) + sizeof( unsigned long ) ) )
\r
203 PRIVILEGED_DATA static volatile signed char * volatile pcTraceBuffer;
\r
204 PRIVILEGED_DATA static signed char *pcTraceBufferStart;
\r
205 PRIVILEGED_DATA static signed char *pcTraceBufferEnd;
\r
206 PRIVILEGED_DATA static signed portBASE_TYPE xTracing = pdFALSE;
\r
207 static unsigned portBASE_TYPE uxPreviousTask = 255U;
\r
208 PRIVILEGED_DATA static char pcStatusString[ 50 ];
\r
212 /*-----------------------------------------------------------*/
\r
215 * Macro that writes a trace of scheduler activity to a buffer. This trace
\r
216 * shows which task is running when and is very useful as a debugging tool.
\r
217 * As this macro is called each context switch it is a good idea to undefine
\r
218 * it if not using the facility.
\r
220 #if ( configUSE_TRACE_FACILITY == 1 )
\r
222 #define vWriteTraceToBuffer() \
\r
226 if( uxPreviousTask != pxCurrentTCB->uxTCBNumber ) \
\r
228 if( ( pcTraceBuffer + tskSIZE_OF_EACH_TRACE_LINE ) < pcTraceBufferEnd ) \
\r
230 uxPreviousTask = pxCurrentTCB->uxTCBNumber; \
\r
231 *( unsigned long * ) pcTraceBuffer = ( unsigned long ) xTickCount; \
\r
232 pcTraceBuffer += sizeof( unsigned long ); \
\r
233 *( unsigned long * ) pcTraceBuffer = ( unsigned long ) uxPreviousTask; \
\r
234 pcTraceBuffer += sizeof( unsigned long ); \
\r
238 xTracing = pdFALSE; \
\r
246 #define vWriteTraceToBuffer()
\r
249 /*-----------------------------------------------------------*/
\r
252 * Place the task represented by pxTCB into the appropriate ready queue for
\r
253 * the task. It is inserted at the end of the list. One quirk of this is
\r
254 * that if the task being inserted is at the same priority as the currently
\r
255 * executing task, then it will only be rescheduled after the currently
\r
256 * executing task has been rescheduled.
\r
258 #define prvAddTaskToReadyQueue( pxTCB ) \
\r
259 if( ( pxTCB )->uxPriority > uxTopReadyPriority ) \
\r
261 uxTopReadyPriority = ( pxTCB )->uxPriority; \
\r
263 vListInsertEnd( ( xList * ) &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xGenericListItem ) )
\r
264 /*-----------------------------------------------------------*/
\r
267 * Macro that looks at the list of tasks that are currently delayed to see if
\r
268 * any require waking.
\r
270 * Tasks are stored in the queue in the order of their wake time - meaning
\r
271 * once one tasks has been found whose timer has not expired we need not look
\r
272 * any further down the list.
\r
274 #define prvCheckDelayedTasks() \
\r
276 register tskTCB *pxTCB; \
\r
277 portTickType xItemValue; \
\r
279 /* Is the tick count greater than or equal to the wake time of the first \
\r
280 task referenced from the delayed tasks list? */ \
\r
281 if( xTickCount >= xNextTaskUnblockTime ) \
\r
285 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE ) \
\r
287 /* The delayed list is empty. Set xNextTaskUnblockTime to the \
\r
288 maximum possible value so it is extremely unlikely that the \
\r
289 if( xTickCount >= xNextTaskUnblockTime ) test will pass next \
\r
291 xNextTaskUnblockTime = portMAX_DELAY; \
\r
296 /* The delayed list is not empty, get the value of the item at \
\r
297 the head of the delayed list. This is the time at which the \
\r
298 task at the head of the delayed list should be removed from \
\r
299 the Blocked state. */ \
\r
300 pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList ); \
\r
301 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) ); \
\r
303 if( xTickCount < xItemValue ) \
\r
305 /* It is not time to unblock this item yet, but the item \
\r
306 value is the time at which the task at the head of the \
\r
307 blocked list should be removed from the Blocked state - \
\r
308 so record the item value in xNextTaskUnblockTime. */ \
\r
309 xNextTaskUnblockTime = xItemValue; \
\r
313 /* It is time to remove the item from the Blocked state. */ \
\r
314 vListRemove( &( pxTCB->xGenericListItem ) ); \
\r
316 /* Is the task waiting on an event also? */ \
\r
317 if( pxTCB->xEventListItem.pvContainer ) \
\r
319 vListRemove( &( pxTCB->xEventListItem ) ); \
\r
321 prvAddTaskToReadyQueue( pxTCB ); \
\r
326 /*-----------------------------------------------------------*/
\r
329 * Several functions take an xTaskHandle parameter that can optionally be NULL,
\r
330 * where NULL is used to indicate that the handle of the currently executing
\r
331 * task should be used in place of the parameter. This macro simply checks to
\r
332 * see if the parameter is NULL and returns a pointer to the appropriate TCB.
\r
334 #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? ( tskTCB * ) pxCurrentTCB : ( tskTCB * ) ( pxHandle ) )
\r
336 /* Callback function prototypes. --------------------------*/
\r
337 extern void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName );
\r
338 extern void vApplicationTickHook( void );
\r
340 /* File private functions. --------------------------------*/
\r
343 * Utility to ready a TCB for a given task. Mainly just copies the parameters
\r
344 * into the TCB structure.
\r
346 static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed char * const pcName, unsigned portBASE_TYPE uxPriority, const xMemoryRegion * const xRegions, unsigned short usStackDepth ) PRIVILEGED_FUNCTION;
\r
349 * Utility to ready all the lists used by the scheduler. This is called
\r
350 * automatically upon the creation of the first task.
\r
352 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
\r
355 * The idle task, which as all tasks is implemented as a never ending loop.
\r
356 * The idle task is automatically created and added to the ready lists upon
\r
357 * creation of the first user task.
\r
359 * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
\r
360 * language extensions. The equivalent prototype for this function is:
\r
362 * void prvIdleTask( void *pvParameters );
\r
365 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters );
\r
368 * Utility to free all memory allocated by the scheduler to hold a TCB,
\r
369 * including the stack pointed to by the TCB.
\r
371 * This does not free memory allocated by the task itself (i.e. memory
\r
372 * allocated by calls to pvPortMalloc from within the tasks application code).
\r
374 #if ( ( INCLUDE_vTaskDelete == 1 ) || ( INCLUDE_vTaskCleanUpResources == 1 ) )
\r
376 static void prvDeleteTCB( tskTCB *pxTCB ) PRIVILEGED_FUNCTION;
\r
381 * Used only by the idle task. This checks to see if anything has been placed
\r
382 * in the list of tasks waiting to be deleted. If so the task is cleaned up
\r
383 * and its TCB deleted.
\r
385 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
\r
388 * The currently executing task is entering the Blocked state. Add the task to
\r
389 * either the current or the overflow delayed task list.
\r
391 static void prvAddCurrentTaskToDelayedList( portTickType xTimeToWake ) PRIVILEGED_FUNCTION;
\r
394 * Allocates memory from the heap for a TCB and associated stack. Checks the
\r
395 * allocation was successful.
\r
397 static tskTCB *prvAllocateTCBAndStack( unsigned short usStackDepth, portSTACK_TYPE *puxStackBuffer ) PRIVILEGED_FUNCTION;
\r
400 * Called from vTaskList. vListTasks details all the tasks currently under
\r
401 * control of the scheduler. The tasks may be in one of a number of lists.
\r
402 * prvListTaskWithinSingleList accepts a list and details the tasks from
\r
403 * within just that list.
\r
405 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
\r
406 * NORMAL APPLICATION CODE.
\r
408 #if ( configUSE_TRACE_FACILITY == 1 )
\r
410 static void prvListTaskWithinSingleList( const signed char *pcWriteBuffer, xList *pxList, signed char cStatus ) PRIVILEGED_FUNCTION;
\r
415 * When a task is created, the stack of the task is filled with a known value.
\r
416 * This function determines the 'high water mark' of the task stack by
\r
417 * determining how much of the stack remains at the original preset value.
\r
419 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )
\r
421 static unsigned short usTaskCheckFreeStackSpace( const unsigned char * pucStackByte ) PRIVILEGED_FUNCTION;
\r
430 /*-----------------------------------------------------------
\r
431 * TASK CREATION API documented in task.h
\r
432 *----------------------------------------------------------*/
\r
434 signed portBASE_TYPE xTaskGenericCreate( pdTASK_CODE pxTaskCode, const signed char * const pcName, unsigned short usStackDepth, void *pvParameters, unsigned portBASE_TYPE uxPriority, xTaskHandle *pxCreatedTask, portSTACK_TYPE *puxStackBuffer, const xMemoryRegion * const xRegions )
\r
436 signed portBASE_TYPE xReturn;
\r
439 configASSERT( pxTaskCode );
\r
440 configASSERT( ( uxPriority < configMAX_PRIORITIES ) );
\r
442 /* Allocate the memory required by the TCB and stack for the new task,
\r
443 checking that the allocation was successful. */
\r
444 pxNewTCB = prvAllocateTCBAndStack( usStackDepth, puxStackBuffer );
\r
446 if( pxNewTCB != NULL )
\r
448 portSTACK_TYPE *pxTopOfStack;
\r
450 #if( portUSING_MPU_WRAPPERS == 1 )
\r
451 /* Should the task be created in privileged mode? */
\r
452 portBASE_TYPE xRunPrivileged;
\r
453 if( ( uxPriority & portPRIVILEGE_BIT ) != 0x00 )
\r
455 xRunPrivileged = pdTRUE;
\r
459 xRunPrivileged = pdFALSE;
\r
461 uxPriority &= ~portPRIVILEGE_BIT;
\r
462 #endif /* portUSING_MPU_WRAPPERS == 1 */
\r
464 /* Calculate the top of stack address. This depends on whether the
\r
465 stack grows from high memory to low (as per the 80x86) or visa versa.
\r
466 portSTACK_GROWTH is used to make the result positive or negative as
\r
467 required by the port. */
\r
468 #if( portSTACK_GROWTH < 0 )
\r
470 pxTopOfStack = pxNewTCB->pxStack + ( usStackDepth - ( unsigned short ) 1 );
\r
471 pxTopOfStack = ( portSTACK_TYPE * ) ( ( ( unsigned long ) pxTopOfStack ) & ( ( unsigned long ) ~portBYTE_ALIGNMENT_MASK ) );
\r
473 /* Check the alignment of the calculated top of stack is correct. */
\r
474 configASSERT( ( ( ( unsigned long ) pxTopOfStack & ( unsigned long ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
\r
478 pxTopOfStack = pxNewTCB->pxStack;
\r
480 /* Check the alignment of the stack buffer is correct. */
\r
481 configASSERT( ( ( ( unsigned long ) pxNewTCB->pxStack & ( unsigned long ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
\r
483 /* If we want to use stack checking on architectures that use
\r
484 a positive stack growth direction then we also need to store the
\r
485 other extreme of the stack space. */
\r
486 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( usStackDepth - 1 );
\r
490 /* Setup the newly allocated TCB with the initial state of the task. */
\r
491 prvInitialiseTCBVariables( pxNewTCB, pcName, uxPriority, xRegions, usStackDepth );
\r
493 /* Initialize the TCB stack to look as if the task was already running,
\r
494 but had been interrupted by the scheduler. The return address is set
\r
495 to the start of the task function. Once the stack has been initialised
\r
496 the top of stack variable is updated. */
\r
497 #if( portUSING_MPU_WRAPPERS == 1 )
\r
499 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
\r
503 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
\r
507 /* Check the alignment of the initialised stack. */
\r
508 configASSERT( ( ( ( unsigned long ) pxNewTCB->pxTopOfStack & ( unsigned long ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
\r
510 if( ( void * ) pxCreatedTask != NULL )
\r
512 /* Pass the TCB out - in an anonymous way. The calling function/
\r
513 task can use this as a handle to delete the task later if
\r
515 *pxCreatedTask = ( xTaskHandle ) pxNewTCB;
\r
518 /* We are going to manipulate the task queues to add this task to a
\r
519 ready list, so must make sure no interrupts occur. */
\r
520 taskENTER_CRITICAL();
\r
522 uxCurrentNumberOfTasks++;
\r
523 if( pxCurrentTCB == NULL )
\r
525 /* There are no other tasks, or all the other tasks are in
\r
526 the suspended state - make this the current task. */
\r
527 pxCurrentTCB = pxNewTCB;
\r
529 if( uxCurrentNumberOfTasks == ( unsigned portBASE_TYPE ) 1 )
\r
531 /* This is the first task to be created so do the preliminary
\r
532 initialisation required. We will not recover if this call
\r
533 fails, but we will report the failure. */
\r
534 prvInitialiseTaskLists();
\r
539 /* If the scheduler is not already running, make this task the
\r
540 current task if it is the highest priority task to be created
\r
542 if( xSchedulerRunning == pdFALSE )
\r
544 if( pxCurrentTCB->uxPriority <= uxPriority )
\r
546 pxCurrentTCB = pxNewTCB;
\r
551 /* Remember the top priority to make context switching faster. Use
\r
552 the priority in pxNewTCB as this has been capped to a valid value. */
\r
553 if( pxNewTCB->uxPriority > uxTopUsedPriority )
\r
555 uxTopUsedPriority = pxNewTCB->uxPriority;
\r
558 #if ( configUSE_TRACE_FACILITY == 1 )
\r
560 /* Add a counter into the TCB for tracing only. */
\r
561 pxNewTCB->uxTCBNumber = uxTaskNumber;
\r
566 prvAddTaskToReadyQueue( pxNewTCB );
\r
569 traceTASK_CREATE( pxNewTCB );
\r
571 taskEXIT_CRITICAL();
\r
575 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
\r
576 traceTASK_CREATE_FAILED();
\r
579 if( xReturn == pdPASS )
\r
581 if( xSchedulerRunning != pdFALSE )
\r
583 /* If the created task is of a higher priority than the current task
\r
584 then it should run now. */
\r
585 if( pxCurrentTCB->uxPriority < uxPriority )
\r
587 portYIELD_WITHIN_API();
\r
594 /*-----------------------------------------------------------*/
\r
596 #if ( INCLUDE_vTaskDelete == 1 )
\r
598 void vTaskDelete( xTaskHandle pxTaskToDelete )
\r
602 taskENTER_CRITICAL();
\r
604 /* Ensure a yield is performed if the current task is being
\r
606 if( pxTaskToDelete == pxCurrentTCB )
\r
608 pxTaskToDelete = NULL;
\r
611 /* If null is passed in here then we are deleting ourselves. */
\r
612 pxTCB = prvGetTCBFromHandle( pxTaskToDelete );
\r
614 /* Remove task from the ready list and place in the termination list.
\r
615 This will stop the task from be scheduled. The idle task will check
\r
616 the termination list and free up any memory allocated by the
\r
617 scheduler for the TCB and stack. */
\r
618 vListRemove( &( pxTCB->xGenericListItem ) );
\r
620 /* Is the task waiting on an event also? */
\r
621 if( pxTCB->xEventListItem.pvContainer )
\r
623 vListRemove( &( pxTCB->xEventListItem ) );
\r
626 vListInsertEnd( ( xList * ) &xTasksWaitingTermination, &( pxTCB->xGenericListItem ) );
\r
628 /* Increment the ucTasksDeleted variable so the idle task knows
\r
629 there is a task that has been deleted and that it should therefore
\r
630 check the xTasksWaitingTermination list. */
\r
633 /* Increment the uxTaskNumberVariable also so kernel aware debuggers
\r
634 can detect that the task lists need re-generating. */
\r
637 traceTASK_DELETE( pxTCB );
\r
639 taskEXIT_CRITICAL();
\r
641 /* Force a reschedule if we have just deleted the current task. */
\r
642 if( xSchedulerRunning != pdFALSE )
\r
644 if( ( void * ) pxTaskToDelete == NULL )
\r
646 portYIELD_WITHIN_API();
\r
658 /*-----------------------------------------------------------
\r
659 * TASK CONTROL API documented in task.h
\r
660 *----------------------------------------------------------*/
\r
662 #if ( INCLUDE_vTaskDelayUntil == 1 )
\r
664 void vTaskDelayUntil( portTickType * const pxPreviousWakeTime, portTickType xTimeIncrement )
\r
666 portTickType xTimeToWake;
\r
667 portBASE_TYPE xAlreadyYielded, xShouldDelay = pdFALSE;
\r
669 configASSERT( pxPreviousWakeTime );
\r
670 configASSERT( ( xTimeIncrement > 0 ) );
\r
674 /* Generate the tick time at which the task wants to wake. */
\r
675 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
\r
677 if( xTickCount < *pxPreviousWakeTime )
\r
679 /* The tick count has overflowed since this function was
\r
680 lasted called. In this case the only time we should ever
\r
681 actually delay is if the wake time has also overflowed,
\r
682 and the wake time is greater than the tick time. When this
\r
683 is the case it is as if neither time had overflowed. */
\r
684 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xTickCount ) )
\r
686 xShouldDelay = pdTRUE;
\r
691 /* The tick time has not overflowed. In this case we will
\r
692 delay if either the wake time has overflowed, and/or the
\r
693 tick time is less than the wake time. */
\r
694 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xTickCount ) )
\r
696 xShouldDelay = pdTRUE;
\r
700 /* Update the wake time ready for the next call. */
\r
701 *pxPreviousWakeTime = xTimeToWake;
\r
703 if( xShouldDelay != pdFALSE )
\r
705 traceTASK_DELAY_UNTIL();
\r
707 /* We must remove ourselves from the ready list before adding
\r
708 ourselves to the blocked list as the same list item is used for
\r
710 vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
\r
711 prvAddCurrentTaskToDelayedList( xTimeToWake );
\r
714 xAlreadyYielded = xTaskResumeAll();
\r
716 /* Force a reschedule if xTaskResumeAll has not already done so, we may
\r
717 have put ourselves to sleep. */
\r
718 if( !xAlreadyYielded )
\r
720 portYIELD_WITHIN_API();
\r
725 /*-----------------------------------------------------------*/
\r
727 #if ( INCLUDE_vTaskDelay == 1 )
\r
729 void vTaskDelay( portTickType xTicksToDelay )
\r
731 portTickType xTimeToWake;
\r
732 signed portBASE_TYPE xAlreadyYielded = pdFALSE;
\r
734 /* A delay time of zero just forces a reschedule. */
\r
735 if( xTicksToDelay > ( portTickType ) 0 )
\r
741 /* A task that is removed from the event list while the
\r
742 scheduler is suspended will not get placed in the ready
\r
743 list or removed from the blocked list until the scheduler
\r
746 This task cannot be in an event list as it is the currently
\r
749 /* Calculate the time to wake - this may overflow but this is
\r
751 xTimeToWake = xTickCount + xTicksToDelay;
\r
753 /* We must remove ourselves from the ready list before adding
\r
754 ourselves to the blocked list as the same list item is used for
\r
756 vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
\r
757 prvAddCurrentTaskToDelayedList( xTimeToWake );
\r
759 xAlreadyYielded = xTaskResumeAll();
\r
762 /* Force a reschedule if xTaskResumeAll has not already done so, we may
\r
763 have put ourselves to sleep. */
\r
764 if( !xAlreadyYielded )
\r
766 portYIELD_WITHIN_API();
\r
771 /*-----------------------------------------------------------*/
\r
773 #if ( INCLUDE_uxTaskPriorityGet == 1 )
\r
775 unsigned portBASE_TYPE uxTaskPriorityGet( xTaskHandle pxTask )
\r
778 unsigned portBASE_TYPE uxReturn;
\r
780 taskENTER_CRITICAL();
\r
782 /* If null is passed in here then we are changing the
\r
783 priority of the calling function. */
\r
784 pxTCB = prvGetTCBFromHandle( pxTask );
\r
785 uxReturn = pxTCB->uxPriority;
\r
787 taskEXIT_CRITICAL();
\r
793 /*-----------------------------------------------------------*/
\r
795 #if ( INCLUDE_vTaskPrioritySet == 1 )
\r
797 void vTaskPrioritySet( xTaskHandle pxTask, unsigned portBASE_TYPE uxNewPriority )
\r
800 unsigned portBASE_TYPE uxCurrentPriority;
\r
801 portBASE_TYPE xYieldRequired = pdFALSE;
\r
803 configASSERT( ( uxNewPriority < configMAX_PRIORITIES ) );
\r
805 /* Ensure the new priority is valid. */
\r
806 if( uxNewPriority >= configMAX_PRIORITIES )
\r
808 uxNewPriority = configMAX_PRIORITIES - ( unsigned portBASE_TYPE ) 1U;
\r
811 taskENTER_CRITICAL();
\r
813 if( pxTask == pxCurrentTCB )
\r
818 /* If null is passed in here then we are changing the
\r
819 priority of the calling function. */
\r
820 pxTCB = prvGetTCBFromHandle( pxTask );
\r
822 traceTASK_PRIORITY_SET( pxTask, uxNewPriority );
\r
824 #if ( configUSE_MUTEXES == 1 )
\r
826 uxCurrentPriority = pxTCB->uxBasePriority;
\r
830 uxCurrentPriority = pxTCB->uxPriority;
\r
834 if( uxCurrentPriority != uxNewPriority )
\r
836 /* The priority change may have readied a task of higher
\r
837 priority than the calling task. */
\r
838 if( uxNewPriority > uxCurrentPriority )
\r
840 if( pxTask != NULL )
\r
842 /* The priority of another task is being raised. If we
\r
843 were raising the priority of the currently running task
\r
844 there would be no need to switch as it must have already
\r
845 been the highest priority task. */
\r
846 xYieldRequired = pdTRUE;
\r
849 else if( pxTask == NULL )
\r
851 /* Setting our own priority down means there may now be another
\r
852 task of higher priority that is ready to execute. */
\r
853 xYieldRequired = pdTRUE;
\r
858 #if ( configUSE_MUTEXES == 1 )
\r
860 /* Only change the priority being used if the task is not
\r
861 currently using an inherited priority. */
\r
862 if( pxTCB->uxBasePriority == pxTCB->uxPriority )
\r
864 pxTCB->uxPriority = uxNewPriority;
\r
867 /* The base priority gets set whatever. */
\r
868 pxTCB->uxBasePriority = uxNewPriority;
\r
872 pxTCB->uxPriority = uxNewPriority;
\r
876 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( configMAX_PRIORITIES - ( portTickType ) uxNewPriority ) );
\r
878 /* If the task is in the blocked or suspended list we need do
\r
879 nothing more than change it's priority variable. However, if
\r
880 the task is in a ready list it needs to be removed and placed
\r
881 in the queue appropriate to its new priority. */
\r
882 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxCurrentPriority ] ), &( pxTCB->xGenericListItem ) ) )
\r
884 /* The task is currently in its ready list - remove before adding
\r
885 it to it's new ready list. As we are in a critical section we
\r
886 can do this even if the scheduler is suspended. */
\r
887 vListRemove( &( pxTCB->xGenericListItem ) );
\r
888 prvAddTaskToReadyQueue( pxTCB );
\r
891 if( xYieldRequired == pdTRUE )
\r
893 portYIELD_WITHIN_API();
\r
897 taskEXIT_CRITICAL();
\r
901 /*-----------------------------------------------------------*/
\r
903 #if ( INCLUDE_vTaskSuspend == 1 )
\r
905 void vTaskSuspend( xTaskHandle pxTaskToSuspend )
\r
909 taskENTER_CRITICAL();
\r
911 /* Ensure a yield is performed if the current task is being
\r
913 if( pxTaskToSuspend == pxCurrentTCB )
\r
915 pxTaskToSuspend = NULL;
\r
918 /* If null is passed in here then we are suspending ourselves. */
\r
919 pxTCB = prvGetTCBFromHandle( pxTaskToSuspend );
\r
921 traceTASK_SUSPEND( pxTCB );
\r
923 /* Remove task from the ready/delayed list and place in the suspended list. */
\r
924 vListRemove( &( pxTCB->xGenericListItem ) );
\r
926 /* Is the task waiting on an event also? */
\r
927 if( pxTCB->xEventListItem.pvContainer )
\r
929 vListRemove( &( pxTCB->xEventListItem ) );
\r
932 vListInsertEnd( ( xList * ) &xSuspendedTaskList, &( pxTCB->xGenericListItem ) );
\r
934 taskEXIT_CRITICAL();
\r
936 if( ( void * ) pxTaskToSuspend == NULL )
\r
938 if( xSchedulerRunning != pdFALSE )
\r
940 /* We have just suspended the current task. */
\r
941 portYIELD_WITHIN_API();
\r
945 /* The scheduler is not running, but the task that was pointed
\r
946 to by pxCurrentTCB has just been suspended and pxCurrentTCB
\r
947 must be adjusted to point to a different task. */
\r
948 if( uxCurrentNumberOfTasks == ( unsigned portBASE_TYPE ) 1U )
\r
950 /* No other tasks are defined, so set pxCurrentTCB back to
\r
951 NULL so when the next task is created pxCurrentTCB will
\r
952 be set to point to it no matter what its relative priority
\r
954 pxCurrentTCB = NULL;
\r
958 vTaskSwitchContext();
\r
965 /*-----------------------------------------------------------*/
\r
967 #if ( INCLUDE_vTaskSuspend == 1 )
\r
969 signed portBASE_TYPE xTaskIsTaskSuspended( xTaskHandle xTask )
\r
971 portBASE_TYPE xReturn = pdFALSE;
\r
972 const tskTCB * const pxTCB = ( tskTCB * ) xTask;
\r
974 /* It does not make sense to check if the calling task is suspended. */
\r
975 configASSERT( xTask );
\r
977 /* Is the task we are attempting to resume actually in the
\r
979 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xGenericListItem ) ) != pdFALSE )
\r
981 /* Has the task already been resumed from within an ISR? */
\r
982 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdTRUE )
\r
984 /* Is it in the suspended list because it is in the
\r
985 Suspended state? It is possible to be in the suspended
\r
986 list because it is blocked on a task with no timeout
\r
988 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) == pdTRUE )
\r
999 /*-----------------------------------------------------------*/
\r
1001 #if ( INCLUDE_vTaskSuspend == 1 )
\r
1003 void vTaskResume( xTaskHandle pxTaskToResume )
\r
1007 /* It does not make sense to resume the calling task. */
\r
1008 configASSERT( pxTaskToResume );
\r
1010 /* Remove the task from whichever list it is currently in, and place
\r
1011 it in the ready list. */
\r
1012 pxTCB = ( tskTCB * ) pxTaskToResume;
\r
1014 /* The parameter cannot be NULL as it is impossible to resume the
\r
1015 currently executing task. */
\r
1016 if( ( pxTCB != NULL ) && ( pxTCB != pxCurrentTCB ) )
\r
1018 taskENTER_CRITICAL();
\r
1020 if( xTaskIsTaskSuspended( pxTCB ) == pdTRUE )
\r
1022 traceTASK_RESUME( pxTCB );
\r
1024 /* As we are in a critical section we can access the ready
\r
1025 lists even if the scheduler is suspended. */
\r
1026 vListRemove( &( pxTCB->xGenericListItem ) );
\r
1027 prvAddTaskToReadyQueue( pxTCB );
\r
1029 /* We may have just resumed a higher priority task. */
\r
1030 if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
\r
1032 /* This yield may not cause the task just resumed to run, but
\r
1033 will leave the lists in the correct state for the next yield. */
\r
1034 portYIELD_WITHIN_API();
\r
1038 taskEXIT_CRITICAL();
\r
1044 /*-----------------------------------------------------------*/
\r
1046 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
\r
1048 portBASE_TYPE xTaskResumeFromISR( xTaskHandle pxTaskToResume )
\r
1050 portBASE_TYPE xYieldRequired = pdFALSE;
\r
1053 configASSERT( pxTaskToResume );
\r
1055 pxTCB = ( tskTCB * ) pxTaskToResume;
\r
1057 if( xTaskIsTaskSuspended( pxTCB ) == pdTRUE )
\r
1059 traceTASK_RESUME_FROM_ISR( pxTCB );
\r
1061 if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
\r
1063 xYieldRequired = ( pxTCB->uxPriority >= pxCurrentTCB->uxPriority );
\r
1064 vListRemove( &( pxTCB->xGenericListItem ) );
\r
1065 prvAddTaskToReadyQueue( pxTCB );
\r
1069 /* We cannot access the delayed or ready lists, so will hold this
\r
1070 task pending until the scheduler is resumed, at which point a
\r
1071 yield will be performed if necessary. */
\r
1072 vListInsertEnd( ( xList * ) &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
\r
1076 return xYieldRequired;
\r
1084 /*-----------------------------------------------------------
\r
1085 * PUBLIC SCHEDULER CONTROL documented in task.h
\r
1086 *----------------------------------------------------------*/
\r
1089 void vTaskStartScheduler( void )
\r
1091 portBASE_TYPE xReturn;
\r
1093 /* Add the idle task at the lowest priority. */
\r
1094 xReturn = xTaskCreate( prvIdleTask, ( signed char * ) "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), ( xTaskHandle * ) NULL );
\r
1096 #if ( configUSE_TIMERS == 1 )
\r
1098 if( xReturn == pdPASS )
\r
1100 xReturn = xTimerCreateTimerTask();
\r
1105 if( xReturn == pdPASS )
\r
1107 /* Interrupts are turned off here, to ensure a tick does not occur
\r
1108 before or during the call to xPortStartScheduler(). The stacks of
\r
1109 the created tasks contain a status word with interrupts switched on
\r
1110 so interrupts will automatically get re-enabled when the first task
\r
1113 STEPPING THROUGH HERE USING A DEBUGGER CAN CAUSE BIG PROBLEMS IF THE
\r
1114 DEBUGGER ALLOWS INTERRUPTS TO BE PROCESSED. */
\r
1115 portDISABLE_INTERRUPTS();
\r
1117 xSchedulerRunning = pdTRUE;
\r
1118 xTickCount = ( portTickType ) 0;
\r
1120 /* If configGENERATE_RUN_TIME_STATS is defined then the following
\r
1121 macro must be defined to configure the timer/counter used to generate
\r
1122 the run time counter time base. */
\r
1123 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
\r
1125 /* Setting up the timer tick is hardware specific and thus in the
\r
1126 portable interface. */
\r
1127 if( xPortStartScheduler() )
\r
1129 /* Should not reach here as if the scheduler is running the
\r
1130 function will not return. */
\r
1134 /* Should only reach here if a task calls xTaskEndScheduler(). */
\r
1138 /* This line will only be reached if the kernel could not be started. */
\r
1139 configASSERT( xReturn );
\r
1141 /*-----------------------------------------------------------*/
\r
1143 void vTaskEndScheduler( void )
\r
1145 /* Stop the scheduler interrupts and call the portable scheduler end
\r
1146 routine so the original ISRs can be restored if necessary. The port
\r
1147 layer must ensure interrupts enable bit is left in the correct state. */
\r
1148 portDISABLE_INTERRUPTS();
\r
1149 xSchedulerRunning = pdFALSE;
\r
1150 vPortEndScheduler();
\r
1152 /*----------------------------------------------------------*/
\r
1154 void vTaskSuspendAll( void )
\r
1156 /* A critical section is not required as the variable is of type
\r
1158 ++uxSchedulerSuspended;
\r
1160 /*----------------------------------------------------------*/
\r
1162 signed portBASE_TYPE xTaskResumeAll( void )
\r
1164 register tskTCB *pxTCB;
\r
1165 signed portBASE_TYPE xAlreadyYielded = pdFALSE;
\r
1167 /* If uxSchedulerSuspended is zero then this function does not match a
\r
1168 previous call to vTaskSuspendAll(). */
\r
1169 configASSERT( uxSchedulerSuspended );
\r
1171 /* It is possible that an ISR caused a task to be removed from an event
\r
1172 list while the scheduler was suspended. If this was the case then the
\r
1173 removed task will have been added to the xPendingReadyList. Once the
\r
1174 scheduler has been resumed it is safe to move all the pending ready
\r
1175 tasks from this list into their appropriate ready list. */
\r
1176 taskENTER_CRITICAL();
\r
1178 --uxSchedulerSuspended;
\r
1180 if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
\r
1182 if( uxCurrentNumberOfTasks > ( unsigned portBASE_TYPE ) 0 )
\r
1184 portBASE_TYPE xYieldRequired = pdFALSE;
\r
1186 /* Move any readied tasks from the pending list into the
\r
1187 appropriate ready list. */
\r
1188 while( listLIST_IS_EMPTY( ( xList * ) &xPendingReadyList ) == pdFALSE )
\r
1190 pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( ( ( xList * ) &xPendingReadyList ) );
\r
1191 vListRemove( &( pxTCB->xEventListItem ) );
\r
1192 vListRemove( &( pxTCB->xGenericListItem ) );
\r
1193 prvAddTaskToReadyQueue( pxTCB );
\r
1195 /* If we have moved a task that has a priority higher than
\r
1196 the current task then we should yield. */
\r
1197 if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
\r
1199 xYieldRequired = pdTRUE;
\r
1203 /* If any ticks occurred while the scheduler was suspended then
\r
1204 they should be processed now. This ensures the tick count does not
\r
1205 slip, and that any delayed tasks are resumed at the correct time. */
\r
1206 if( uxMissedTicks > ( unsigned portBASE_TYPE ) 0 )
\r
1208 while( uxMissedTicks > ( unsigned portBASE_TYPE ) 0 )
\r
1210 vTaskIncrementTick();
\r
1214 /* As we have processed some ticks it is appropriate to yield
\r
1215 to ensure the highest priority task that is ready to run is
\r
1216 the task actually running. */
\r
1217 #if configUSE_PREEMPTION == 1
\r
1219 xYieldRequired = pdTRUE;
\r
1224 if( ( xYieldRequired == pdTRUE ) || ( xMissedYield == pdTRUE ) )
\r
1226 xAlreadyYielded = pdTRUE;
\r
1227 xMissedYield = pdFALSE;
\r
1228 portYIELD_WITHIN_API();
\r
1233 taskEXIT_CRITICAL();
\r
1235 return xAlreadyYielded;
\r
1243 /*-----------------------------------------------------------
\r
1244 * PUBLIC TASK UTILITIES documented in task.h
\r
1245 *----------------------------------------------------------*/
\r
1249 portTickType xTaskGetTickCount( void )
\r
1251 portTickType xTicks;
\r
1253 /* Critical section required if running on a 16 bit processor. */
\r
1254 taskENTER_CRITICAL();
\r
1256 xTicks = xTickCount;
\r
1258 taskEXIT_CRITICAL();
\r
1262 /*-----------------------------------------------------------*/
\r
1264 portTickType xTaskGetTickCountFromISR( void )
\r
1266 portTickType xReturn;
\r
1267 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
1269 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
1270 xReturn = xTickCount;
\r
1271 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1275 /*-----------------------------------------------------------*/
\r
1277 unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
\r
1279 /* A critical section is not required because the variables are of type
\r
1281 return uxCurrentNumberOfTasks;
\r
1283 /*-----------------------------------------------------------*/
\r
1285 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1287 void vTaskList( signed char *pcWriteBuffer )
\r
1289 unsigned portBASE_TYPE uxQueue;
\r
1291 /* This is a VERY costly function that should be used for debug only.
\r
1292 It leaves interrupts disabled for a LONG time. */
\r
1294 vTaskSuspendAll();
\r
1296 /* Run through all the lists that could potentially contain a TCB and
\r
1297 report the task name, state and stack high water mark. */
\r
1299 *pcWriteBuffer = ( signed char ) 0x00;
\r
1300 strcat( ( char * ) pcWriteBuffer, ( const char * ) "\r\n" );
\r
1302 uxQueue = uxTopUsedPriority + ( unsigned portBASE_TYPE ) 1U;
\r
1308 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) == pdFALSE )
\r
1310 prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), tskREADY_CHAR );
\r
1312 }while( uxQueue > ( unsigned short ) tskIDLE_PRIORITY );
\r
1314 if( listLIST_IS_EMPTY( pxDelayedTaskList ) == pdFALSE )
\r
1316 prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, tskBLOCKED_CHAR );
\r
1319 if( listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) == pdFALSE )
\r
1321 prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, tskBLOCKED_CHAR );
\r
1324 #if( INCLUDE_vTaskDelete == 1 )
\r
1326 if( listLIST_IS_EMPTY( &xTasksWaitingTermination ) == pdFALSE )
\r
1328 prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &xTasksWaitingTermination, tskDELETED_CHAR );
\r
1333 #if ( INCLUDE_vTaskSuspend == 1 )
\r
1335 if( listLIST_IS_EMPTY( &xSuspendedTaskList ) == pdFALSE )
\r
1337 prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &xSuspendedTaskList, tskSUSPENDED_CHAR );
\r
1346 /*----------------------------------------------------------*/
\r
1348 #if ( configGENERATE_RUN_TIME_STATS == 1 )
\r
1350 void vTaskGetRunTimeStats( signed char *pcWriteBuffer )
\r
1352 unsigned portBASE_TYPE uxQueue;
\r
1353 unsigned long ulTotalRunTime;
\r
1355 /* This is a VERY costly function that should be used for debug only.
\r
1356 It leaves interrupts disabled for a LONG time. */
\r
1358 vTaskSuspendAll();
\r
1360 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
\r
1361 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );
\r
1363 ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
\r
1366 /* Divide ulTotalRunTime by 100 to make the percentage caluclations
\r
1367 simpler in the prvGenerateRunTimeStatsForTasksInList() function. */
\r
1368 ulTotalRunTime /= 100UL;
\r
1370 /* Run through all the lists that could potentially contain a TCB,
\r
1371 generating a table of run timer percentages in the provided
\r
1374 *pcWriteBuffer = ( signed char ) 0x00;
\r
1375 strcat( ( char * ) pcWriteBuffer, ( const char * ) "\r\n" );
\r
1377 uxQueue = uxTopUsedPriority + ( unsigned portBASE_TYPE ) 1U;
\r
1383 if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) == pdFALSE )
\r
1385 prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), ulTotalRunTime );
\r
1387 }while( uxQueue > ( unsigned short ) tskIDLE_PRIORITY );
\r
1389 if( listLIST_IS_EMPTY( pxDelayedTaskList ) == pdFALSE )
\r
1391 prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, ulTotalRunTime );
\r
1394 if( listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) == pdFALSE )
\r
1396 prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, ulTotalRunTime );
\r
1399 #if ( INCLUDE_vTaskDelete == 1 )
\r
1401 if( listLIST_IS_EMPTY( &xTasksWaitingTermination ) == pdFALSE )
\r
1403 prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) &xTasksWaitingTermination, ulTotalRunTime );
\r
1408 #if ( INCLUDE_vTaskSuspend == 1 )
\r
1410 if( listLIST_IS_EMPTY( &xSuspendedTaskList ) == pdFALSE )
\r
1412 prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) &xSuspendedTaskList, ulTotalRunTime );
\r
1421 /*----------------------------------------------------------*/
\r
1423 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1425 void vTaskStartTrace( signed char * pcBuffer, unsigned long ulBufferSize )
\r
1427 configASSERT( pcBuffer );
\r
1428 configASSERT( ulBufferSize );
\r
1430 taskENTER_CRITICAL();
\r
1432 pcTraceBuffer = ( signed char * )pcBuffer;
\r
1433 pcTraceBufferStart = pcBuffer;
\r
1434 pcTraceBufferEnd = pcBuffer + ( ulBufferSize - tskSIZE_OF_EACH_TRACE_LINE );
\r
1435 xTracing = pdTRUE;
\r
1437 taskEXIT_CRITICAL();
\r
1441 /*----------------------------------------------------------*/
\r
1443 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1445 unsigned long ulTaskEndTrace( void )
\r
1447 unsigned long ulBufferLength;
\r
1449 taskENTER_CRITICAL();
\r
1450 xTracing = pdFALSE;
\r
1451 taskEXIT_CRITICAL();
\r
1453 ulBufferLength = ( unsigned long ) ( pcTraceBuffer - pcTraceBufferStart );
\r
1455 return ulBufferLength;
\r
1462 /*-----------------------------------------------------------
\r
1463 * SCHEDULER INTERNALS AVAILABLE FOR PORTING PURPOSES
\r
1464 * documented in task.h
\r
1465 *----------------------------------------------------------*/
\r
1468 void vTaskIncrementTick( void )
\r
1472 /* Called by the portable layer each time a tick interrupt occurs.
\r
1473 Increments the tick then checks to see if the new tick value will cause any
\r
1474 tasks to be unblocked. */
\r
1475 if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
\r
1478 if( xTickCount == ( portTickType ) 0 )
\r
1482 /* Tick count has overflowed so we need to swap the delay lists.
\r
1483 If there are any items in pxDelayedTaskList here then there is
\r
1485 configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) );
\r
1487 pxTemp = pxDelayedTaskList;
\r
1488 pxDelayedTaskList = pxOverflowDelayedTaskList;
\r
1489 pxOverflowDelayedTaskList = pxTemp;
\r
1490 xNumOfOverflows++;
\r
1492 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
\r
1494 /* The new current delayed list is empty. Set
\r
1495 xNextTaskUnblockTime to the maximum possible value so it is
\r
1496 extremely unlikely that the
\r
1497 if( xTickCount >= xNextTaskUnblockTime ) test will pass until
\r
1498 there is an item in the delayed list. */
\r
1499 xNextTaskUnblockTime = portMAX_DELAY;
\r
1503 /* The new current delayed list is not empty, get the value of
\r
1504 the item at the head of the delayed list. This is the time at
\r
1505 which the task at the head of the delayed list should be removed
\r
1506 from the Blocked state. */
\r
1507 pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );
\r
1508 xNextTaskUnblockTime = listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) );
\r
1512 /* See if this tick has made a timeout expire. */
\r
1513 prvCheckDelayedTasks();
\r
1519 /* The tick hook gets called at regular intervals, even if the
\r
1520 scheduler is locked. */
\r
1521 #if ( configUSE_TICK_HOOK == 1 )
\r
1523 vApplicationTickHook();
\r
1528 #if ( configUSE_TICK_HOOK == 1 )
\r
1530 /* Guard against the tick hook being called when the missed tick
\r
1531 count is being unwound (when the scheduler is being unlocked. */
\r
1532 if( uxMissedTicks == ( unsigned portBASE_TYPE ) 0U )
\r
1534 vApplicationTickHook();
\r
1539 traceTASK_INCREMENT_TICK( xTickCount );
\r
1541 /*-----------------------------------------------------------*/
\r
1543 #if ( ( INCLUDE_vTaskCleanUpResources == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
\r
1545 void vTaskCleanUpResources( void )
\r
1547 unsigned short usQueue;
\r
1548 volatile tskTCB *pxTCB;
\r
1550 usQueue = ( unsigned short ) uxTopUsedPriority + ( unsigned short ) 1;
\r
1552 /* Remove any TCB's from the ready queues. */
\r
1557 while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ usQueue ] ) ) == pdFALSE )
\r
1559 listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &( pxReadyTasksLists[ usQueue ] ) );
\r
1560 vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );
\r
1562 prvDeleteTCB( ( tskTCB * ) pxTCB );
\r
1564 }while( usQueue > ( unsigned short ) tskIDLE_PRIORITY );
\r
1566 /* Remove any TCB's from the delayed queue. */
\r
1567 while( listLIST_IS_EMPTY( &xDelayedTaskList1 ) == pdFALSE )
\r
1569 listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &xDelayedTaskList1 );
\r
1570 vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );
\r
1572 prvDeleteTCB( ( tskTCB * ) pxTCB );
\r
1575 /* Remove any TCB's from the overflow delayed queue. */
\r
1576 while( listLIST_IS_EMPTY( &xDelayedTaskList2 ) == pdFALSE )
\r
1578 listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &xDelayedTaskList2 );
\r
1579 vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );
\r
1581 prvDeleteTCB( ( tskTCB * ) pxTCB );
\r
1584 while( listLIST_IS_EMPTY( &xSuspendedTaskList ) == pdFALSE )
\r
1586 listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &xSuspendedTaskList );
\r
1587 vListRemove( ( xListItem * ) &( pxTCB->xGenericListItem ) );
\r
1589 prvDeleteTCB( ( tskTCB * ) pxTCB );
\r
1594 /*-----------------------------------------------------------*/
\r
1596 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
\r
1598 void vTaskSetApplicationTaskTag( xTaskHandle xTask, pdTASK_HOOK_CODE pxHookFunction )
\r
1602 /* If xTask is NULL then we are setting our own task hook. */
\r
1603 if( xTask == NULL )
\r
1605 xTCB = ( tskTCB * ) pxCurrentTCB;
\r
1609 xTCB = ( tskTCB * ) xTask;
\r
1612 /* Save the hook function in the TCB. A critical section is required as
\r
1613 the value can be accessed from an interrupt. */
\r
1614 taskENTER_CRITICAL();
\r
1615 xTCB->pxTaskTag = pxHookFunction;
\r
1616 taskEXIT_CRITICAL();
\r
1620 /*-----------------------------------------------------------*/
\r
1622 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
\r
1624 pdTASK_HOOK_CODE xTaskGetApplicationTaskTag( xTaskHandle xTask )
\r
1627 pdTASK_HOOK_CODE xReturn;
\r
1629 /* If xTask is NULL then we are setting our own task hook. */
\r
1630 if( xTask == NULL )
\r
1632 xTCB = ( tskTCB * ) pxCurrentTCB;
\r
1636 xTCB = ( tskTCB * ) xTask;
\r
1639 /* Save the hook function in the TCB. A critical section is required as
\r
1640 the value can be accessed from an interrupt. */
\r
1641 taskENTER_CRITICAL();
\r
1642 xReturn = xTCB->pxTaskTag;
\r
1643 taskEXIT_CRITICAL();
\r
1649 /*-----------------------------------------------------------*/
\r
1651 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
\r
1653 portBASE_TYPE xTaskCallApplicationTaskHook( xTaskHandle xTask, void *pvParameter )
\r
1656 portBASE_TYPE xReturn;
\r
1658 /* If xTask is NULL then we are calling our own task hook. */
\r
1659 if( xTask == NULL )
\r
1661 xTCB = ( tskTCB * ) pxCurrentTCB;
\r
1665 xTCB = ( tskTCB * ) xTask;
\r
1668 if( xTCB->pxTaskTag != NULL )
\r
1670 xReturn = xTCB->pxTaskTag( pvParameter );
\r
1681 /*-----------------------------------------------------------*/
\r
1683 void vTaskSwitchContext( void )
\r
1685 if( uxSchedulerSuspended != ( unsigned portBASE_TYPE ) pdFALSE )
\r
1687 /* The scheduler is currently suspended - do not allow a context
\r
1689 xMissedYield = pdTRUE;
\r
1693 traceTASK_SWITCHED_OUT();
\r
1695 #if ( configGENERATE_RUN_TIME_STATS == 1 )
\r
1697 unsigned long ulTempCounter;
\r
1699 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
\r
1700 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTempCounter );
\r
1702 ulTempCounter = portGET_RUN_TIME_COUNTER_VALUE();
\r
1705 /* Add the amount of time the task has been running to the accumulated
\r
1706 time so far. The time the task started running was stored in
\r
1707 ulTaskSwitchedInTime. Note that there is no overflow protection here
\r
1708 so count values are only valid until the timer overflows. Generally
\r
1709 this will be about 1 hour assuming a 1uS timer increment. */
\r
1710 pxCurrentTCB->ulRunTimeCounter += ( ulTempCounter - ulTaskSwitchedInTime );
\r
1711 ulTaskSwitchedInTime = ulTempCounter;
\r
1715 taskFIRST_CHECK_FOR_STACK_OVERFLOW();
\r
1716 taskSECOND_CHECK_FOR_STACK_OVERFLOW();
\r
1718 /* Find the highest priority queue that contains ready tasks. */
\r
1719 while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopReadyPriority ] ) ) )
\r
1721 configASSERT( uxTopReadyPriority );
\r
1722 --uxTopReadyPriority;
\r
1725 /* listGET_OWNER_OF_NEXT_ENTRY walks through the list, so the tasks of the
\r
1726 same priority get an equal share of the processor time. */
\r
1727 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopReadyPriority ] ) );
\r
1729 traceTASK_SWITCHED_IN();
\r
1730 vWriteTraceToBuffer();
\r
1733 /*-----------------------------------------------------------*/
\r
1735 void vTaskPlaceOnEventList( const xList * const pxEventList, portTickType xTicksToWait )
\r
1737 portTickType xTimeToWake;
\r
1739 configASSERT( pxEventList );
\r
1741 /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE
\r
1742 SCHEDULER SUSPENDED. */
\r
1744 /* Place the event list item of the TCB in the appropriate event list.
\r
1745 This is placed in the list in priority order so the highest priority task
\r
1746 is the first to be woken by the event. */
\r
1747 vListInsert( ( xList * ) pxEventList, ( xListItem * ) &( pxCurrentTCB->xEventListItem ) );
\r
1749 /* We must remove ourselves from the ready list before adding ourselves
\r
1750 to the blocked list as the same list item is used for both lists. We have
\r
1751 exclusive access to the ready lists as the scheduler is locked. */
\r
1752 vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
\r
1755 #if ( INCLUDE_vTaskSuspend == 1 )
\r
1757 if( xTicksToWait == portMAX_DELAY )
\r
1759 /* Add ourselves to the suspended task list instead of a delayed task
\r
1760 list to ensure we are not woken by a timing event. We will block
\r
1762 vListInsertEnd( ( xList * ) &xSuspendedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
\r
1766 /* Calculate the time at which the task should be woken if the event does
\r
1767 not occur. This may overflow but this doesn't matter. */
\r
1768 xTimeToWake = xTickCount + xTicksToWait;
\r
1769 prvAddCurrentTaskToDelayedList( xTimeToWake );
\r
1774 /* Calculate the time at which the task should be woken if the event does
\r
1775 not occur. This may overflow but this doesn't matter. */
\r
1776 xTimeToWake = xTickCount + xTicksToWait;
\r
1777 prvAddCurrentTaskToDelayedList( xTimeToWake );
\r
1781 /*-----------------------------------------------------------*/
\r
1783 #if configUSE_TIMERS == 1
\r
1785 void vTaskPlaceOnEventListRestricted( const xList * const pxEventList, portTickType xTicksToWait )
\r
1787 portTickType xTimeToWake;
\r
1789 configASSERT( pxEventList );
\r
1791 /* This function should not be called by application code hence the
\r
1792 'Restricted' in its name. It is not part of the public API. It is
\r
1793 designed for use by kernel code, and has special calling requirements -
\r
1794 it should be called from a critical section. */
\r
1797 /* Place the event list item of the TCB in the appropriate event list.
\r
1798 In this case it is assume that this is the only task that is going to
\r
1799 be waiting on this event list, so the faster vListInsertEnd() function
\r
1800 can be used in place of vListInsert. */
\r
1801 vListInsertEnd( ( xList * ) pxEventList, ( xListItem * ) &( pxCurrentTCB->xEventListItem ) );
\r
1803 /* We must remove this task from the ready list before adding it to the
\r
1804 blocked list as the same list item is used for both lists. This
\r
1805 function is called form a critical section. */
\r
1806 vListRemove( ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
\r
1808 /* Calculate the time at which the task should be woken if the event does
\r
1809 not occur. This may overflow but this doesn't matter. */
\r
1810 xTimeToWake = xTickCount + xTicksToWait;
\r
1811 prvAddCurrentTaskToDelayedList( xTimeToWake );
\r
1814 #endif /* configUSE_TIMERS */
\r
1815 /*-----------------------------------------------------------*/
\r
1817 signed portBASE_TYPE xTaskRemoveFromEventList( const xList * const pxEventList )
\r
1819 tskTCB *pxUnblockedTCB;
\r
1820 portBASE_TYPE xReturn;
\r
1822 /* THIS FUNCTION MUST BE CALLED WITH INTERRUPTS DISABLED OR THE
\r
1823 SCHEDULER SUSPENDED. It can also be called from within an ISR. */
\r
1825 /* The event list is sorted in priority order, so we can remove the
\r
1826 first in the list, remove the TCB from the delayed list, and add
\r
1827 it to the ready list.
\r
1829 If an event is for a queue that is locked then this function will never
\r
1830 get called - the lock count on the queue will get modified instead. This
\r
1831 means we can always expect exclusive access to the event list here.
\r
1833 This function assumes that a check has already been made to ensure that
\r
1834 pxEventList is not empty. */
\r
1835 pxUnblockedTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
\r
1836 configASSERT( pxUnblockedTCB );
\r
1837 vListRemove( &( pxUnblockedTCB->xEventListItem ) );
\r
1839 if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
\r
1841 vListRemove( &( pxUnblockedTCB->xGenericListItem ) );
\r
1842 prvAddTaskToReadyQueue( pxUnblockedTCB );
\r
1846 /* We cannot access the delayed or ready lists, so will hold this
\r
1847 task pending until the scheduler is resumed. */
\r
1848 vListInsertEnd( ( xList * ) &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
\r
1851 if( pxUnblockedTCB->uxPriority >= pxCurrentTCB->uxPriority )
\r
1853 /* Return true if the task removed from the event list has
\r
1854 a higher priority than the calling task. This allows
\r
1855 the calling task to know if it should force a context
\r
1861 xReturn = pdFALSE;
\r
1866 /*-----------------------------------------------------------*/
\r
1868 void vTaskSetTimeOutState( xTimeOutType * const pxTimeOut )
\r
1870 configASSERT( pxTimeOut );
\r
1871 pxTimeOut->xOverflowCount = xNumOfOverflows;
\r
1872 pxTimeOut->xTimeOnEntering = xTickCount;
\r
1874 /*-----------------------------------------------------------*/
\r
1876 portBASE_TYPE xTaskCheckForTimeOut( xTimeOutType * const pxTimeOut, portTickType * const pxTicksToWait )
\r
1878 portBASE_TYPE xReturn;
\r
1880 configASSERT( pxTimeOut );
\r
1881 configASSERT( pxTicksToWait );
\r
1883 taskENTER_CRITICAL();
\r
1885 #if ( INCLUDE_vTaskSuspend == 1 )
\r
1886 /* If INCLUDE_vTaskSuspend is set to 1 and the block time specified is
\r
1887 the maximum block time then the task should block indefinitely, and
\r
1888 therefore never time out. */
\r
1889 if( *pxTicksToWait == portMAX_DELAY )
\r
1891 xReturn = pdFALSE;
\r
1893 else /* We are not blocking indefinitely, perform the checks below. */
\r
1896 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( ( portTickType ) xTickCount >= ( portTickType ) pxTimeOut->xTimeOnEntering ) )
\r
1898 /* The tick count is greater than the time at which vTaskSetTimeout()
\r
1899 was called, but has also overflowed since vTaskSetTimeOut() was called.
\r
1900 It must have wrapped all the way around and gone past us again. This
\r
1901 passed since vTaskSetTimeout() was called. */
\r
1904 else if( ( ( portTickType ) ( ( portTickType ) xTickCount - ( portTickType ) pxTimeOut->xTimeOnEntering ) ) < ( portTickType ) *pxTicksToWait )
\r
1906 /* Not a genuine timeout. Adjust parameters for time remaining. */
\r
1907 *pxTicksToWait -= ( ( portTickType ) xTickCount - ( portTickType ) pxTimeOut->xTimeOnEntering );
\r
1908 vTaskSetTimeOutState( pxTimeOut );
\r
1909 xReturn = pdFALSE;
\r
1916 taskEXIT_CRITICAL();
\r
1920 /*-----------------------------------------------------------*/
\r
1922 void vTaskMissedYield( void )
\r
1924 xMissedYield = pdTRUE;
\r
1928 * -----------------------------------------------------------
\r
1930 * ----------------------------------------------------------
\r
1932 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
\r
1933 * language extensions. The equivalent prototype for this function is:
\r
1935 * void prvIdleTask( void *pvParameters );
\r
1938 static portTASK_FUNCTION( prvIdleTask, pvParameters )
\r
1940 /* Stop warnings. */
\r
1941 ( void ) pvParameters;
\r
1945 /* See if any tasks have been deleted. */
\r
1946 prvCheckTasksWaitingTermination();
\r
1948 #if ( configUSE_PREEMPTION == 0 )
\r
1950 /* If we are not using preemption we keep forcing a task switch to
\r
1951 see if any other task has become available. If we are using
\r
1952 preemption we don't need to do this as any task becoming available
\r
1953 will automatically get the processor anyway. */
\r
1958 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
\r
1960 /* When using preemption tasks of equal priority will be
\r
1961 timesliced. If a task that is sharing the idle priority is ready
\r
1962 to run then the idle task should yield before the end of the
\r
1965 A critical region is not required here as we are just reading from
\r
1966 the list, and an occasional incorrect value will not matter. If
\r
1967 the ready list at the idle priority contains more than one task
\r
1968 then a task other than the idle task is ready to execute. */
\r
1969 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( unsigned portBASE_TYPE ) 1 )
\r
1976 #if ( configUSE_IDLE_HOOK == 1 )
\r
1978 extern void vApplicationIdleHook( void );
\r
1980 /* Call the user defined function from within the idle task. This
\r
1981 allows the application designer to add background functionality
\r
1982 without the overhead of a separate task.
\r
1983 NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
\r
1984 CALL A FUNCTION THAT MIGHT BLOCK. */
\r
1985 vApplicationIdleHook();
\r
1989 } /*lint !e715 pvParameters is not accessed but all task functions require the same prototype. */
\r
1997 /*-----------------------------------------------------------
\r
1998 * File private functions documented at the top of the file.
\r
1999 *----------------------------------------------------------*/
\r
2003 static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed char * const pcName, unsigned portBASE_TYPE uxPriority, const xMemoryRegion * const xRegions, unsigned short usStackDepth )
\r
2005 /* Store the function name in the TCB. */
\r
2006 #if configMAX_TASK_NAME_LEN > 1
\r
2008 /* Don't bring strncpy into the build unnecessarily. */
\r
2009 strncpy( ( char * ) pxTCB->pcTaskName, ( const char * ) pcName, ( unsigned short ) configMAX_TASK_NAME_LEN );
\r
2012 pxTCB->pcTaskName[ ( unsigned short ) configMAX_TASK_NAME_LEN - ( unsigned short ) 1 ] = ( signed char ) '\0';
\r
2014 /* This is used as an array index so must ensure it's not too large. First
\r
2015 remove the privilege bit if one is present. */
\r
2016 if( uxPriority >= configMAX_PRIORITIES )
\r
2018 uxPriority = configMAX_PRIORITIES - ( unsigned portBASE_TYPE ) 1U;
\r
2021 pxTCB->uxPriority = uxPriority;
\r
2022 #if ( configUSE_MUTEXES == 1 )
\r
2024 pxTCB->uxBasePriority = uxPriority;
\r
2028 vListInitialiseItem( &( pxTCB->xGenericListItem ) );
\r
2029 vListInitialiseItem( &( pxTCB->xEventListItem ) );
\r
2031 /* Set the pxTCB as a link back from the xListItem. This is so we can get
\r
2032 back to the containing TCB from a generic item in a list. */
\r
2033 listSET_LIST_ITEM_OWNER( &( pxTCB->xGenericListItem ), pxTCB );
\r
2035 /* Event lists are always in priority order. */
\r
2036 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) uxPriority );
\r
2037 listSET_LIST_ITEM_OWNER( &( pxTCB->xEventListItem ), pxTCB );
\r
2039 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
\r
2041 pxTCB->uxCriticalNesting = ( unsigned portBASE_TYPE ) 0;
\r
2045 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
\r
2047 pxTCB->pxTaskTag = NULL;
\r
2051 #if ( configGENERATE_RUN_TIME_STATS == 1 )
\r
2053 pxTCB->ulRunTimeCounter = 0UL;
\r
2057 #if ( portUSING_MPU_WRAPPERS == 1 )
\r
2059 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, pxTCB->pxStack, usStackDepth );
\r
2063 ( void ) xRegions;
\r
2064 ( void ) usStackDepth;
\r
2068 /*-----------------------------------------------------------*/
\r
2070 #if ( portUSING_MPU_WRAPPERS == 1 )
\r
2072 void vTaskAllocateMPURegions( xTaskHandle xTaskToModify, const xMemoryRegion * const xRegions )
\r
2076 if( xTaskToModify == pxCurrentTCB )
\r
2078 xTaskToModify = NULL;
\r
2081 /* If null is passed in here then we are deleting ourselves. */
\r
2082 pxTCB = prvGetTCBFromHandle( xTaskToModify );
\r
2084 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
\r
2086 /*-----------------------------------------------------------*/
\r
2089 static void prvInitialiseTaskLists( void )
\r
2091 unsigned portBASE_TYPE uxPriority;
\r
2093 for( uxPriority = ( unsigned portBASE_TYPE ) 0U; uxPriority < configMAX_PRIORITIES; uxPriority++ )
\r
2095 vListInitialise( ( xList * ) &( pxReadyTasksLists[ uxPriority ] ) );
\r
2098 vListInitialise( ( xList * ) &xDelayedTaskList1 );
\r
2099 vListInitialise( ( xList * ) &xDelayedTaskList2 );
\r
2100 vListInitialise( ( xList * ) &xPendingReadyList );
\r
2102 #if ( INCLUDE_vTaskDelete == 1 )
\r
2104 vListInitialise( ( xList * ) &xTasksWaitingTermination );
\r
2108 #if ( INCLUDE_vTaskSuspend == 1 )
\r
2110 vListInitialise( ( xList * ) &xSuspendedTaskList );
\r
2114 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
\r
2116 pxDelayedTaskList = &xDelayedTaskList1;
\r
2117 pxOverflowDelayedTaskList = &xDelayedTaskList2;
\r
2119 /*-----------------------------------------------------------*/
\r
2121 static void prvCheckTasksWaitingTermination( void )
\r
2123 #if ( INCLUDE_vTaskDelete == 1 )
\r
2125 portBASE_TYPE xListIsEmpty;
\r
2127 /* ucTasksDeleted is used to prevent vTaskSuspendAll() being called
\r
2128 too often in the idle task. */
\r
2129 if( uxTasksDeleted > ( unsigned portBASE_TYPE ) 0 )
\r
2131 vTaskSuspendAll();
\r
2132 xListIsEmpty = listLIST_IS_EMPTY( &xTasksWaitingTermination );
\r
2135 if( xListIsEmpty == pdFALSE )
\r
2139 taskENTER_CRITICAL();
\r
2141 pxTCB = ( tskTCB * ) listGET_OWNER_OF_HEAD_ENTRY( ( ( xList * ) &xTasksWaitingTermination ) );
\r
2142 vListRemove( &( pxTCB->xGenericListItem ) );
\r
2143 --uxCurrentNumberOfTasks;
\r
2146 taskEXIT_CRITICAL();
\r
2148 prvDeleteTCB( pxTCB );
\r
2154 /*-----------------------------------------------------------*/
\r
2156 static void prvAddCurrentTaskToDelayedList( portTickType xTimeToWake )
\r
2158 /* The list item will be inserted in wake time order. */
\r
2159 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xGenericListItem ), xTimeToWake );
\r
2161 if( xTimeToWake < xTickCount )
\r
2163 /* Wake time has overflowed. Place this item in the overflow list. */
\r
2164 vListInsert( ( xList * ) pxOverflowDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
\r
2168 /* The wake time has not overflowed, so we can use the current block list. */
\r
2169 vListInsert( ( xList * ) pxDelayedTaskList, ( xListItem * ) &( pxCurrentTCB->xGenericListItem ) );
\r
2171 /* If the task entering the blocked state was placed at the head of the
\r
2172 list of blocked tasks then xNextTaskUnblockTime needs to be updated
\r
2174 if( xTimeToWake < xNextTaskUnblockTime )
\r
2176 xNextTaskUnblockTime = xTimeToWake;
\r
2180 /*-----------------------------------------------------------*/
\r
2182 static tskTCB *prvAllocateTCBAndStack( unsigned short usStackDepth, portSTACK_TYPE *puxStackBuffer )
\r
2186 /* Allocate space for the TCB. Where the memory comes from depends on
\r
2187 the implementation of the port malloc function. */
\r
2188 pxNewTCB = ( tskTCB * ) pvPortMalloc( sizeof( tskTCB ) );
\r
2190 if( pxNewTCB != NULL )
\r
2192 /* Allocate space for the stack used by the task being created.
\r
2193 The base of the stack memory stored in the TCB so the task can
\r
2194 be deleted later if required. */
\r
2195 pxNewTCB->pxStack = ( portSTACK_TYPE * ) pvPortMallocAligned( ( ( ( size_t )usStackDepth ) * sizeof( portSTACK_TYPE ) ), puxStackBuffer );
\r
2197 if( pxNewTCB->pxStack == NULL )
\r
2199 /* Could not allocate the stack. Delete the allocated TCB. */
\r
2200 vPortFree( pxNewTCB );
\r
2205 /* Just to help debugging. */
\r
2206 memset( pxNewTCB->pxStack, tskSTACK_FILL_BYTE, usStackDepth * sizeof( portSTACK_TYPE ) );
\r
2212 /*-----------------------------------------------------------*/
\r
2214 #if ( configUSE_TRACE_FACILITY == 1 )
\r
2216 static void prvListTaskWithinSingleList( const signed char *pcWriteBuffer, xList *pxList, signed char cStatus )
\r
2218 volatile tskTCB *pxNextTCB, *pxFirstTCB;
\r
2219 unsigned short usStackRemaining;
\r
2221 /* Write the details of all the TCB's in pxList into the buffer. */
\r
2222 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
\r
2225 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
\r
2226 #if ( portSTACK_GROWTH > 0 )
\r
2228 usStackRemaining = usTaskCheckFreeStackSpace( ( unsigned char * ) pxNextTCB->pxEndOfStack );
\r
2232 usStackRemaining = usTaskCheckFreeStackSpace( ( unsigned char * ) pxNextTCB->pxStack );
\r
2236 sprintf( pcStatusString, ( char * ) "%s\t\t%c\t%u\t%u\t%u\r\n", pxNextTCB->pcTaskName, cStatus, ( unsigned int ) pxNextTCB->uxPriority, usStackRemaining, ( unsigned int ) pxNextTCB->uxTCBNumber );
\r
2237 strcat( ( char * ) pcWriteBuffer, ( char * ) pcStatusString );
\r
2239 } while( pxNextTCB != pxFirstTCB );
\r
2243 /*-----------------------------------------------------------*/
\r
2245 #if ( configGENERATE_RUN_TIME_STATS == 1 )
\r
2247 static void prvGenerateRunTimeStatsForTasksInList( const signed char *pcWriteBuffer, xList *pxList, unsigned long ulTotalRunTime )
\r
2249 volatile tskTCB *pxNextTCB, *pxFirstTCB;
\r
2250 unsigned long ulStatsAsPercentage;
\r
2252 /* Write the run time stats of all the TCB's in pxList into the buffer. */
\r
2253 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
\r
2256 /* Get next TCB in from the list. */
\r
2257 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
\r
2259 /* Divide by zero check. */
\r
2260 if( ulTotalRunTime > 0UL )
\r
2262 /* Has the task run at all? */
\r
2263 if( pxNextTCB->ulRunTimeCounter == 0 )
\r
2265 /* The task has used no CPU time at all. */
\r
2266 sprintf( pcStatsString, ( char * ) "%s\t\t0\t\t0%%\r\n", pxNextTCB->pcTaskName );
\r
2270 /* What percentage of the total run time has the task used?
\r
2271 This will always be rounded down to the nearest integer.
\r
2272 ulTotalRunTime has already been divided by 100. */
\r
2273 ulStatsAsPercentage = pxNextTCB->ulRunTimeCounter / ulTotalRunTime;
\r
2275 if( ulStatsAsPercentage > 0UL )
\r
2277 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
\r
2279 sprintf( pcStatsString, ( char * ) "%s\t\t%lu\t\t%lu%%\r\n", pxNextTCB->pcTaskName, pxNextTCB->ulRunTimeCounter, ulStatsAsPercentage );
\r
2283 /* sizeof( int ) == sizeof( long ) so a smaller
\r
2284 printf() library can be used. */
\r
2285 sprintf( pcStatsString, ( char * ) "%s\t\t%u\t\t%u%%\r\n", pxNextTCB->pcTaskName, ( unsigned int ) pxNextTCB->ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage );
\r
2291 /* If the percentage is zero here then the task has
\r
2292 consumed less than 1% of the total run time. */
\r
2293 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
\r
2295 sprintf( pcStatsString, ( char * ) "%s\t\t%lu\t\t<1%%\r\n", pxNextTCB->pcTaskName, pxNextTCB->ulRunTimeCounter );
\r
2299 /* sizeof( int ) == sizeof( long ) so a smaller
\r
2300 printf() library can be used. */
\r
2301 sprintf( pcStatsString, ( char * ) "%s\t\t%u\t\t<1%%\r\n", pxNextTCB->pcTaskName, ( unsigned int ) pxNextTCB->ulRunTimeCounter );
\r
2307 strcat( ( char * ) pcWriteBuffer, ( char * ) pcStatsString );
\r
2310 } while( pxNextTCB != pxFirstTCB );
\r
2314 /*-----------------------------------------------------------*/
\r
2316 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )
\r
2318 static unsigned short usTaskCheckFreeStackSpace( const unsigned char * pucStackByte )
\r
2320 register unsigned short usCount = 0;
\r
2322 while( *pucStackByte == tskSTACK_FILL_BYTE )
\r
2324 pucStackByte -= portSTACK_GROWTH;
\r
2328 usCount /= sizeof( portSTACK_TYPE );
\r
2334 /*-----------------------------------------------------------*/
\r
2336 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
\r
2338 unsigned portBASE_TYPE uxTaskGetStackHighWaterMark( xTaskHandle xTask )
\r
2341 unsigned char *pcEndOfStack;
\r
2342 unsigned portBASE_TYPE uxReturn;
\r
2344 pxTCB = prvGetTCBFromHandle( xTask );
\r
2346 #if portSTACK_GROWTH < 0
\r
2348 pcEndOfStack = ( unsigned char * ) pxTCB->pxStack;
\r
2352 pcEndOfStack = ( unsigned char * ) pxTCB->pxEndOfStack;
\r
2356 uxReturn = ( unsigned portBASE_TYPE ) usTaskCheckFreeStackSpace( pcEndOfStack );
\r
2362 /*-----------------------------------------------------------*/
\r
2364 #if ( ( INCLUDE_vTaskDelete == 1 ) || ( INCLUDE_vTaskCleanUpResources == 1 ) )
\r
2366 static void prvDeleteTCB( tskTCB *pxTCB )
\r
2368 /* Free up the memory allocated by the scheduler for the task. It is up to
\r
2369 the task to free any memory allocated at the application level. */
\r
2370 vPortFreeAligned( pxTCB->pxStack );
\r
2371 vPortFree( pxTCB );
\r
2377 /*-----------------------------------------------------------*/
\r
2379 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
\r
2381 xTaskHandle xTaskGetCurrentTaskHandle( void )
\r
2383 xTaskHandle xReturn;
\r
2385 /* A critical section is not required as this is not called from
\r
2386 an interrupt and the current TCB will always be the same for any
\r
2387 individual execution thread. */
\r
2388 xReturn = pxCurrentTCB;
\r
2395 /*-----------------------------------------------------------*/
\r
2397 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
\r
2399 portBASE_TYPE xTaskGetSchedulerState( void )
\r
2401 portBASE_TYPE xReturn;
\r
2403 if( xSchedulerRunning == pdFALSE )
\r
2405 xReturn = taskSCHEDULER_NOT_STARTED;
\r
2409 if( uxSchedulerSuspended == ( unsigned portBASE_TYPE ) pdFALSE )
\r
2411 xReturn = taskSCHEDULER_RUNNING;
\r
2415 xReturn = taskSCHEDULER_SUSPENDED;
\r
2423 /*-----------------------------------------------------------*/
\r
2425 #if ( configUSE_MUTEXES == 1 )
\r
2427 void vTaskPriorityInherit( xTaskHandle * const pxMutexHolder )
\r
2429 tskTCB * const pxTCB = ( tskTCB * ) pxMutexHolder;
\r
2431 configASSERT( pxMutexHolder );
\r
2433 if( pxTCB->uxPriority < pxCurrentTCB->uxPriority )
\r
2435 /* Adjust the mutex holder state to account for its new priority. */
\r
2436 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) pxCurrentTCB->uxPriority );
\r
2438 /* If the task being modified is in the ready state it will need to
\r
2439 be moved in to a new list. */
\r
2440 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxTCB->uxPriority ] ), &( pxTCB->xGenericListItem ) ) )
\r
2442 vListRemove( &( pxTCB->xGenericListItem ) );
\r
2444 /* Inherit the priority before being moved into the new list. */
\r
2445 pxTCB->uxPriority = pxCurrentTCB->uxPriority;
\r
2446 prvAddTaskToReadyQueue( pxTCB );
\r
2450 /* Just inherit the priority. */
\r
2451 pxTCB->uxPriority = pxCurrentTCB->uxPriority;
\r
2457 /*-----------------------------------------------------------*/
\r
2459 #if ( configUSE_MUTEXES == 1 )
\r
2461 void vTaskPriorityDisinherit( xTaskHandle * const pxMutexHolder )
\r
2463 tskTCB * const pxTCB = ( tskTCB * ) pxMutexHolder;
\r
2465 if( pxMutexHolder != NULL )
\r
2467 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
\r
2469 /* We must be the running task to be able to give the mutex back.
\r
2470 Remove ourselves from the ready list we currently appear in. */
\r
2471 vListRemove( &( pxTCB->xGenericListItem ) );
\r
2473 /* Disinherit the priority before adding ourselves into the new
\r
2475 pxTCB->uxPriority = pxTCB->uxBasePriority;
\r
2476 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), configMAX_PRIORITIES - ( portTickType ) pxTCB->uxPriority );
\r
2477 prvAddTaskToReadyQueue( pxTCB );
\r
2483 /*-----------------------------------------------------------*/
\r
2485 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
\r
2487 void vTaskEnterCritical( void )
\r
2489 portDISABLE_INTERRUPTS();
\r
2491 if( xSchedulerRunning != pdFALSE )
\r
2493 ( pxCurrentTCB->uxCriticalNesting )++;
\r
2498 /*-----------------------------------------------------------*/
\r
2500 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
\r
2502 void vTaskExitCritical( void )
\r
2504 if( xSchedulerRunning != pdFALSE )
\r
2506 if( pxCurrentTCB->uxCriticalNesting > 0 )
\r
2508 ( pxCurrentTCB->uxCriticalNesting )--;
\r
2510 if( pxCurrentTCB->uxCriticalNesting == 0 )
\r
2512 portENABLE_INTERRUPTS();
\r
2519 /*-----------------------------------------------------------*/
\r