FreeRTOS内核源码解读之-------任务创建

任务创建函数--------xTaskCreate(动态方法)

  • FreeRTOS中任务控制块详解
  • FreeRTOS任务创建和删除的动态和静态方法区别
  • FreeRTOS动态创建和删除任务
  • FreeRTOS静态创建

1、FreeRTOS中任务控制块详解

任务控制块是将每个任务的属性集中到一个结构体中,这个结构体叫做任务控制块:TCB_t。它就是一个任务的身份证,以后系统对任务的任何操作,都是通过这个任务控制块来实现的。具体代码:

typedef struct tskTaskControlBlock
{
   //任务栈顶指针,必须设置为任务控制块的第一个成员变量
   volatile StackType_t	*pxTopOfStack;	
   //	启用MPU的情况下,设置。MPU内存保护单元
   #if ( portUSING_MPU_WRAPPERS == 1 )
   	//设置任务访问的内存权限
   	xMPU_SETTINGS	xMPUSettings;		
   #endif
   //状态链表项,任务会处于不同的状态,该项会被插入到对应的链表,供链表引用	任务
   ListItem_t			xStateListItem;	 
   //事件链表项,
   ListItem_t			xEventListItem;		/*< Used to reference a task from an event list. */
   //任务优先级
   UBaseType_t			uxPriority;			
   //任务栈内存起始地址
   StackType_t			*pxStack;		
   //任务名字,字符串形式,一般用于调试	,configMAX_TASK_NAME_LEN表示任务名字最长值
   char				pcTaskName[ configMAX_TASK_NAME_LEN ];
   //栈生长方向,对于向上生长的栈,用于指明栈的上边界,用于判断是否溢出
   #if ( portSTACK_GROWTH > 0 )
   	StackType_t		*pxEndOfStack;		
   #endif
   //保存临界区嵌套深度
   #if ( portCRITICAL_NESTING_IN_TCB == 1 )
   	UBaseType_t		uxCriticalNesting;	
   #endif

   #if ( configUSE_TRACE_FACILITY == 1 )
   	//用于调试,表示本任务是第几个创建,每创建一个任务,系统有一个全局变量加1,并将该值赋给新任务
   	UBaseType_t		uxTCBNumber;		
   	//调试用,用户可以设定特定的数值
   	UBaseType_t		uxTaskNumber;		
   #endif

   #if ( configUSE_MUTEXES == 1 )
   	//
   	UBaseType_t		uxBasePriority;		
   	UBaseType_t		uxMutexesHeld;
   #endif

   #if ( configUSE_APPLICATION_TASK_TAG == 1 )
   	TaskHookFunction_t pxTaskTag;
   #endif

   #if( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
   	void *pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
   #endif

   #if( configGENERATE_RUN_TIME_STATS == 1 )
   	uint32_t		ulRunTimeCounter;	/*< Stores the amount of time the task has spent in the Running state. */
   #endif

   #if ( configUSE_NEWLIB_REENTRANT == 1 )
   	/* Allocate a Newlib reent structure that is specific to this task.
   	Note Newlib support has been included by popular demand, but is not
   	used by the FreeRTOS maintainers themselves.  FreeRTOS is not
   	responsible for resulting newlib operation.  User must be familiar with
   	newlib and must provide system-wide implementations of the necessary
   	stubs. Be warned that (at the time of writing) the current newlib design
   	implements a system-wide malloc() that must be provided with locks. */
   	struct	_reent xNewLib_reent;
   #endif

   #if( configUSE_TASK_NOTIFICATIONS == 1 )
   	volatile uint32_t ulNotifiedValue;
   	volatile uint8_t ucNotifyState;
   #endif

   /* See the comments above the definition of
   tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
   #if( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
   	uint8_t	ucStaticallyAllocated; 		/*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
   #endif

   #if( INCLUDE_xTaskAbortDelay == 1 )
   	uint8_t ucDelayAborted;
   #endif

} tskTCB;
typedef tskTCB TCB_t;

2、FreeRTOS任务创建和删除的动态和静态方法区别

在FreeRTOS实时操作系统中,提供了两种任务创建和删除函数的方法,一种是动态方式,对应的API函数是xTaskCreate和xTaskDelete;另一种是静态方式,对应的函数是xTaskCreateStatic和xTaskCreateRestricted函数。
两种方法的区别如下:
<1>在使用上,使用动态方式创建时,每个任务的任务堆栈需要从FreeRTOS的堆中自动申请,这时,FreeRTOS必须提供内存管理文件,在FreeRTOS提供了5中内存管理方式,后续会进行剖析;如果使用静态方式创建和删除任务,需要用户自己为每个任务指定任务堆栈。
<2>还有需要提醒的是,如果我们使用动态方式创建的任务,必须使用动态方式删除任务;使用静态方式创建任务,必须使用静态方式删除任务。
注: 相关代码
对于选择静态还是动态方式创建和删除任务,在FreeRTOS.h配置文件中可以指定,代码如下:

#ifndef configSUPPORT_STATIC_ALLOCATION
	/* Defaults to 0 for backward compatibility. */
	#define configSUPPORT_STATIC_ALLOCATION 0
#endif
#ifndef configSUPPORT_DYNAMIC_ALLOCATION
	/* Defaults to 1 for backward compatibility. */
	#define configSUPPORT_DYNAMIC_ALLOCATION 1
#endif

3、FreeRTOS动态创建和删除任务

  • a、函数原型
    BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
    const char * const pcName,
    const uint16_t usStackDepth,
    void * const pvParameters,
    UBaseType_t uxPriority,
    TaskHandle_t * const pxCreatedTask )
    {
    参数&#
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值