动态任务

1.任务句柄

/* LED任务句柄 */
static TaskHandle_t LED_Task_Handle;		

2.任务创建函数

BaseType_t xTaskCreate(	TaskFunction_t pxTaskCode,							//任务函数
							const char * const pcName,						//任务名称
							const uint16_t usStackDepth,					//堆栈大小
							void * const pvParameters,						//形参
							UBaseType_t uxPriority,							//优先级
							TaskHandle_t * const pxCreatedTask )			//任务句柄
		{
			TCB_t *pxNewTCB;
			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;
				}
			}
		}
		#else /* portSTACK_GROWTH */
		{
		StackType_t *pxStack;

			/* 给任务分配堆栈 */
			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 )
			{
				/* 给任务控制块TCB申请内存 */
				pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) ); /*lint !e961 MISRA exception as the casts are only redundant for some paths. */

				if( pxNewTCB != NULL )
				{
					/* 指向分配的内存地址. */
					pxNewTCB->pxStack = pxStack;
				}
				else
				{
					/* 创建失败,释放内存. */
					vPortFree( pxStack );
				}
			}
			else
			{
				pxNewTCB = NULL;
			}
		}
		#endif /* portSTACK_GROWTH */

		if( pxNewTCB != NULL )
		{
			#if( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
			{
				/* Tasks can be created statically or dynamically, so note this
				task was created dynamically in case it is later deleted. */
				pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
			}
			#endif /* configSUPPORT_STATIC_ALLOCATION */

			prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );	//初始化任务控制块
			prvAddNewTaskToReadyList( pxNewTCB );	//加入就绪列表中
			xReturn = pdPASS;
		}
		else
		{
			xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
		}

		return xReturn;
	}

举例:

BaseType_t ret = pdPASS;
  /* 创建动态任务 */
	ret = xTaskCreate((TaskFunction_t	)LED_Task,								//任务函数
															(const char* 	)"LED_Task",	//任务名称
															(uint32_t 		)512,					//内存大小
															(void* 		  	)NULL,					//´形参
															(UBaseType_t 	)2, 				//优先级
															&LED_Task_Handle);				//句柄
	
	if(pdPASS == ret)/* 任务创建成功 */
		printf("LED_Task创建成功!\n");
	else
		printf("LED_Task创建失败!\n");

3.任务函数实现

TaskFunction_t pxTaskCode			//函数指针指向一个void func(void*)函数
/*
 * Defines the prototype to which task functions must conform.  Defined in this
 * file to ensure the type is known before portable.h is included.
 */
typedef void (*TaskFunction_t)( void * );		

//任务函数实现
static void LED_Task(void* parameter)
{	
    while (1)
    {
        HAL_GPIO_WritePin(LD1_GPIO_Port, LD1_Pin, (GPIO_PinState)SET);
        vTaskDelay(500);   
        printf("LED_Task Running,LED1_ON\r\n");
        
        HAL_GPIO_WritePin(LD1_GPIO_Port, LD1_Pin, (GPIO_PinState)RESET);   
        vTaskDelay(500);   	 		
        printf("LED_Task Running,LED1_OFF\r\n");
    }
}

4.任务的删除

 void vTaskDelete( TaskHandle_t xTaskToDelete ); //将任务从就绪列表及任务列表中删除,形参xTaskToDelete 为任务句柄
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值