代码可能有点乱,因为还不会排版,粘贴上来就全乱了,大概弄了下
英文注释为原注释,中文注释为自己的理解,不一定对
#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
BaseType_t xTaskCreate( TaskFunction_t pxTaskCode, /* main_task 函数指针 */
const char * const pcName, /*函数名*/ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
const configSTACK_DEPTH_TYPE usStackDepth, /*堆栈大小*/
void * const pvParameters, /*函数参数*/
UBaseType_t uxPriority, /*任务优先级*/
TaskHandle_t * const pxCreatedTask ) /*void * 类型,任务句柄,保存任务的结构体*/
{
TCB_t *pxNewTCB; /*新建一个任务控制块,如果任务的stack块创建成功,则添加到这个TCB结构下*/
BaseType_t xReturn;
/* If the stack grows down then allocate the stack then the TCB so the stack
does not grow into the TCB. Likewise if the stack grows up then allocate
the TCB then the stack. */
#if( portSTACK_GROWTH > 0 )
{
/* Allocate space for the TCB. Where the memory comes from depends on the implementation of the port malloc function and whether or not static allocation is being used. */
pxNewTCB = ( TCB_t * )
pvPortMalloc( sizeof( TCB_t ) );
if( pxNewTCB != NULL ) { /* Allocate space for the stack used by the task being created. The base of the stack memory stored in the TCB so the task can be deleted later if required. */
pxNewTCB->pxStack = ( StackType_t * ) pvPortMalloc( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
if( pxNewTCB->pxStack == NULL )
{
/* Could not allocate the stack. Delete the allocated TCB. */
vPortFree( pxNewTCB );
pxNewTCB = NULL;
}
}
}
接上一部分,vscode代码粘上来换行全都没有了,而且还不好改
#else /* portSTACK_GROWTH */
{
StackType_t *pxStack;
/*新建一个新的栈内存,用来记录新的块, 再新建一个TCB,用TCB结构来加入(包装)这个块*/
/* Allocate space for the stack used by the task being created. */
pxStack = ( StackType_t * ) pvPortMalloc( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) );
/*lint !e961 MISRA exception as the casts are only redundant for some ports. */
if( pxStack != NULL )
{
/* Allocate space for the TCB. */ /* 给TCB 申请内存 ,然后让其中的stack成员指向刚申请好的stack内存 */
pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
/*lint !e961 MISRA exception as the casts are only redundant for some paths. */
if( pxNewTCB != NULL )
{
/* Store the stack location in the TCB. */
pxNewTCB->pxStack = pxStack;
}
else
{
/* The stack cannot be used as the TCB was not created. Free it again. */ /* 申请TCB失败,释放stack内存 */
vPortFree( pxStack );
}
}
else
/*stack申请失败 TCB为NULL */
{
pxNewTCB = NULL;
}
}
#endif /* portSTACK_GROWTH */
/* 当TCB及stack 内存申请成功时,用传入的任务函数来初始化TCB */ if( pxNewTCB != NULL ) { #if( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 Macro has been consolidated for readability reasons. */ { /* Tasks can be created statically or dynamically, so note this task was created dynamically in case it is later deleted. */ /* 动态申请stack内存 ,需要标记TCB中的静态申请flag为 0 防止之后被删除。可能需要free,静态不需要free直接删除delete */ pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
/*
#define tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
#define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 0 )
#define tskSTATICALLY_ALLOCATED_STACK_ONLY ( ( uint8_t ) 1 )
#define tskSTATICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 2 ) */
}
#endif
/* configSUPPORT_STATIC_ALLOCATION */
/*
将申请的内存,绑定TCB块,添加新的taskCode到任务链表, */
prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
prvAddNewTaskToReadyList( pxNewTCB );
xReturn = pdPASS;
}
else
{
xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY; }
return xReturn;
}
/* 创建任务完成 */