FreeRTOS任务

1、FreeRTOS任务有4种状态
就绪态,运行态,阻塞态,挂起态,状态之间的切换如下图所示
在这里插入图片描述
2、任务优先级
优先级范围是0-configMAX_PRIORITIES -1,0最低级。空闲任务的优先级是0。

3、任务控制块
任务控制块包含了任务状态信息。
portSTACK_GROWTH < 0 表示栈从高地址向低地址生长,这是常规的方式,pxTopOfStack指向栈顶,pxStack指向栈起始地址,动态创建任务时,动态分配一块内存给栈,pxStack指向这块内存的起始地址。如下图所示。
栈向低地址生长
xStateListItem 状态列表项,记录任务的状态,pvContainer指向状态列表,就绪列表,运行列表,阻塞列表,挂起列表其中一个
xEventListItem 记录任务的事件
StackType_t 栈的类型是uint32_t


/*
 * Task control block.  A task control block (TCB) is allocated for each task,
 * and stores task state information, including a pointer to the task's context
 * (the task's run time environment, including register values)
 */
typedef struct tskTaskControlBlock
{
	volatile StackType_t	*pxTopOfStack;	/*< Points to the location of the last item placed on the tasks stack.  THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */

	#if ( portUSING_MPU_WRAPPERS == 1 )
		xMPU_SETTINGS	xMPUSettings;		/*< The MPU settings are defined as part of the port layer.  THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
	#endif

	ListItem_t			xStateListItem;	/*< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */
	ListItem_t			xEventListItem;		/*< Used to reference a task from an event list. */
	UBaseType_t			uxPriority;			/*< The priority of the task.  0 is the lowest priority. */
	StackType_t			*pxStack;			/*< Points to the start of the stack. */
	char				pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created.  Facilitates debugging only. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */

	#if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
		StackType_t		*pxEndOfStack;		/*< Points to the highest valid address for the stack. */
	#endif

	#if ( portCRITICAL_NESTING_IN_TCB == 1 )
		UBaseType_t		uxCriticalNesting;	/*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
	#endif

	#if ( configUSE_TRACE_FACILITY == 1 )
		UBaseType_t		uxTCBNumber;		/*< 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. */
		UBaseType_t		uxTaskNumber;		/*< Stores a number specifically for use by third party trace code. */
	#endif

	#if ( configUSE_MUTEXES == 1 )
		UBaseType_t		uxBasePriority;		/*< The priority last assigned to the task - used by the priority inheritance mechanism. */
		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 ) /*lint !e731 Macro has been consolidated for readability reasons. */
		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;

4、任务优先级
ISR优先级最高,其次是任务。
中断优先级
5、创建任务
以创建空闲任务为例,我们需要定义空闲任务的句柄defaultTaskHandle,它是一个全局变量,调用xTaskCreate之后,defaultTaskHandle指向了该任务的控制块,也就是说句柄实质是任务控制块。假设空闲任务的栈大小是128*4bytes,则
pxTopOfStack - pxStack + 1 = 128;
新创建的任务会通过调用prvAddNewTaskToReadyList加入就绪列表,就绪列表数组pxReadyTasksLists[ configMAX_PRIORITIES ]是一个全局变量,因为任务有多个优先级,可以理解为每个优先级都有一个就绪列表,实际上不是。( pxTCB )->xStateListItem->pvContainer = &( pxReadyTasksLists[ ( pxTCB )->uxPriority ]。

xTaskCreate

6、删除任务

vTaskDelete

7、挂起任务
挂起任务调用vListInsertEnd将任务添加到挂起列表xSuspendedTaskList

vTaskSuspend

8、任务恢复
任务恢复调用prvAddTaskToReadyList将任务添加到就绪列表

vTaskResume

9、启动任务调度器
SVC中断处理函数vPortSVCHandler获取当前任务控制块pxCurrentTCB来运行该任务。
任务调度流程
10、任务切换
SysTick_Handler → xPortSysTickHandler → 触发PSV中断portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT → xPortPendSVHandler → vTaskSwitchContext → taskSELECT_HIGHEST_PRIORITY_TASK
任务切换流程

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值