FreeRTOS学习(4)

任务管理
任务函数结构
必须是空返回值,并且接受一个任何类型的指针参数。函数主体由不结束的循环构成
任务函数 不允许有返回值!!!
任务的创建与启动
BaseType_t xTaskCreate( 
TaskFunction_t pvTaskCode,  
//*The pvTaskCode parameter is simply a 
//*pointer to the function that implements the task       
const char * const pcName,
//not used by FreeRTOS in any way
uint16_t usStackDepth,
//the depth of Stack
void *pvParameters,
//Parameters
UBaseType_t uxPriority,
/*Priorities can be 
*assigned from 0, which is the lowest priority, to 
*(configMAX_PRIORITIES – 1), which is the highest priority.
*/
TaskHandle_t *pxCreatedTask );
//if it is used,writers should set the handle name
/*Returned Value
pdPASS:Task has been created successfully
pdFAIL:Task failed to be created because there is 
insufficient heap memory available for FreeRTOS to allocate
enough RAM to hold the task data structures and stack
*
*/

有个有意思的事情是,实际上idle任务的栈深也是可以设置的

操作非官方例程所给出的代码,有可能会出现字符串无法完整打印出的问题。这是正常现象,这是因为printf使用fputc重定向打印到串口,时间上并不能够保证用一个tick中断就能完成单次任务。具体解决方案将会在后面讲到现场恢复的时候详细解释

任务优先级问题
优先级设定

使用xTaskCreate(...)函数可以在其中设定优先级,也可以在线程跑起来以后使用vTaskPrioritySet(...)对任务优先级进行修改

FreeRTOS任务交错运行的机制
time slice、tick interrupt、tick period

tick interrupt是生成time slice的基础,两个tick interrupt中间是一个time slice,其时间上的大小与tick period相等

任务交错的时候,是当前任务运行的时候,tick interrupt发生,内核在回调函数中选择下个应当运行的任务,中断结束并运行该任务

pdMS_TO_TICKS()
以下是官方给出的例程,用于将200ms转换为ticks的数量,方便在各个函数之间交换数据。实际上,官方并不建议直接用ticks来表示时间,而建议使用该函数
在使用ticks表示时间时,有句话意味深长:
ensuring times specified within the application do not change if the tick frequency is changed.
时间是一个绝对的量值而不是相对的
/* pdMS_TO_TICKS() takes a time in milliseconds as its only parameter, and evaluates
to the equivalent time in tick periods. This example shows xTimeInTicks being set to 
the number of tick periods that are equivalent to 200 milliseconds. */
TickType_t xTimeInTicks = pdMS_TO_TICKS( 200 );
任务的状态

运行态

正常运行

阻塞态

任务在等待时间或在等待数据

挂起态

不受scheduler管理,需要用户自行恢复

预备态

不阻塞不挂起就是预备态

两个产生阻塞态的api比较

void vTaskDelay( TickType_t xTicksToDelay );
//看到TickType_t类型的数据,最好使用pdMS_TO_TICKS()来根据输入时间
获得需要的tick
void vTaskDelayUntil( TickType_t * pxPreviousWakeTime,
 TickType_t xTimeIncrement );
//这是一种线程中记数的功能,能够在一个准确的tick count唤醒任务
Idle任务与其钩子函数(回调函数)

Idle任务的职责

This is because the Idle task is responsible for cleaning up
kernel resources after a task has been deleted

Idle Hook functions不接受参数也不返回值,被Idle唤起的名称标识是确定的

void vApplicationIdleHook( void )
任务优先级修改

修改和读取优先级对应着两个api,查阅相关内容即可,要注意传入函数是句柄的指针,需要在xTaskCreate()中声明一下句柄的来源

任务删除

基本和优先级的操作不相上下

任务调度算法(重点 )

1.Prioritized Pre-emptive Scheduling with Time Slicing

used by most small RTOS applications

工作机理主要是有一套固定的优先级,支持抢占,处理器分配资源依据时间切片原则,除了Idle和主动阻塞挂起的任务,同优先级的任务都应当运行一个完整的时间片;特殊的,Idle无法运行一个完整的时间片是因为它会主动让出处理器资源(yield)

2.Prioritized Pre-emptive Scheduling (without Time Slicing)

仍然是抢占式的做法

和1.中方法不同在于,不启用time slice的做法会导致正在运行的任务在不被高优先级任务抢占/阻塞/挂起的前提下,处于同优先级的其余任务是无法执行的.只有当该任务进入非运行状态让出处理器资源后才能够让下一个任务进入运行状态.以此往复

3.Co-operative Scheduling

非抢占式的做法
It is normally easier to avoid problems caused by simultaneous access when the co-operative scheduler is used than when the pre-emptive scheduler is used

不抢占了,意味着时间切片在任务运行过程中起到的作用相对较低.一个任务足够长时间的占用处理器资源,需要该任务主动让出处理器资源才能够实现其它任务的运行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值