FreeRTOS (一) 初探

  • 资源
F:\3SDK\STM32_F4_explorer\探索者F4 资料盘(A盘)\4,程序源码\3,扩展例程\5,FreeRTOS例程\FreeRTOS实验2-1 FreeRTOS移植实验
https://blog.csdn.net/zhzht19861011/category_9265276.html

https://blog.csdn.net/zhzht19861011/category_9265965.html
任务该怎么写
1.
while(1){
	...
}
2.
do{
	...
	vTaskDelete(myself_Task_Handler);
}while(0)

  • 怎么查看有几个任务及任务详情
#define configUSE_TRACE_FACILITY				      1
vTaskList(打印每个任务的详细信息,) // uxTaskGetNumberOfTasks(打印有几个任务)
// 0 的优先级最低,configMAX_PRIORITIES-1 的优先级最高
Name			State(XBRSD) Priority(less is low) Stack_water_level Num_create_time  // 自己添的,打印信息没有这一行
------------------------------------------// 自己添的,打印信息没有这一行
float_task     	R	4	15	6 // Ready
led1_task      	R	3	29	5
led0_task      	R	2	29	4
IDLE           	R	0	107	2 // 在vTaskStartScheduler中创建,一般是在用户创建第1 2 3 ... n个任务后,调用vTaskStartScheduler,因此 IDLE是 第n+1 个任务 
Tmr Svc        	S	31	235	3 // 在vTaskStartScheduler中创建, ... 第n+2个任务
 // Suspended
 // Deleted
 // BLocked
 // Executing // 调用 vTaskList 函数的 状态,但是....,float_task调用的 vTaskList ,为什么是 Ready???
// 优先级可以相等,在freertos中,叫做共享优先级
float_task      R       2       13      6
led0_task       R       2       29      4
IDLE            R       0       107     2
led1_task       B       2       29      5
Tmr Svc         S       31      235     3
  • 调度时机,什么时候开始调度
1. 时间片用完时,只在tick中断时发生 // 每个任务的时间片是固定的,大小等于 systick发生的间隔.即 #define portTICK_PERIOD_MS			( ( TickType_t ) 1000 / configTICK_RATE_HZ ) ,单位为ms
一般 configTICK_RATE_HZ  在 [1-1000之间]

2. 主动放弃cpu时,vTaskDelay

vTaskDelay(500); 是 延时 500ms, // 具体延时为(499ms,500ms)
vTaskDelay(0); 是 A进程 yeild cpu,如果此时有相同优先级任务B处于ready状态,则B运行;若无,则A继续运行
#define configCPU_CLOCK_HZ						(SystemCoreClock)       //CPU频率 // 168000000 // 与移植到的soc cpu频率相等.
#define configTICK_RATE_HZ						(1000)                  //时钟节拍频率,这里设置为1000,周期就是1ms // 1s 1000次,一次1ms

3.
It is important to note that the end of a time slice is not the only place that the scheduler can select a new task to run; 
as will be demonstrated throughout this book, the scheduler will also select a new task to run immediately after the currently executing task enters the Blocked state, or when an interrupt moves a higher priority task into the Ready state.

什么时候配置 systick_rate
xPortStartScheduler
	vPortSetupTimerInterrupt
		portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
		portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT );
  • 调度策略,哪个进程开始运行
#define configUSE_PREEMPTION					1                       //1:使用抢占式内核,0:使用协程
#define configUSE_TIME_SLICING					1						 //1:使能时间片调度

协程(Co-routines)主要用于资源发非常受限的嵌入式系统(RAM非常少),通常不会用于32位微处理器。
在当前嵌入式硬件环境下,不建议使用协程,FreeRTOS的开发者早已经停止开发协程。


优先级高(3比2高)的任务先运行
1.
	有两个任务A B为3,不管AB是否主动放弃cpu,则在 AB 间轮换.
2.
	有一个任务为3,有一个任务为2,3主动放弃cpu,才会轮到2.3不主动放弃cpu,2永远没机会.

对比2个线程,优先级大小为A(3)B(2),和A(6)B(2) 是一个效果.


Tasks that are not actually running, but are not in either the Blocked state or the Suspended state, are in the Ready state. 

Tasks that are in the Ready state are available to be selected by the scheduler as the task to enter the Running state. 

The scheduler will always choose the highest priority Ready state task to enter the Running state.



The scheduling algorithm is the software routine that decides which Ready state task to transition into the Running state.


the algorithm can be changed using the configUSE_PREEMPTION and configUSE_TIME_SLICING configuration constants. Both constants are defined in FreeRTOSConfig.h.

A third configuration constant, configUSE_TICKLESS_IDLE, also affects the scheduling algorithm, as its use can result in the tick interrupt being turned off completely for extended periods. 
configUSE_TICKLESS_IDLE is an advanced option provided specifically for use in applications that must minimize their power consumption. 



In all possible configurations the FreeRTOS scheduler will ensure tasks that share a priority are selected to enter the Running state in turn.
This ‘take it in turn’ policy is often referred to as ‘Round Robin Scheduling’. 

A Round Robin scheduling algorithm does not guarantee time is shared equally between tasks of equal priority, only that Ready state tasks of equal priority will enter the Running state in turn.



任务状态

任务状态:
	1.运行态
		1.1 什么时候进入
			systick中断
			某个任务进入block时
			在中断中将高优先级任务移动到ready状态
		1.2 哪一个ready任务进入
			根据 configUSE_PREEMPTION  configUSE_TIME_SLICING  configUSE_TICKLESS_IDLE 的配置变化
			但总之就是 选最高优先级的

			在所有可能的配置中,FreeRTOS调度器将确保选择共享优先级的任务以依次进入运行状态。
			这种“轮流”策略通常被称为“循环调度”。
			循环调度算法并不能保证等优先级任务之间的时间平均分配,只有等优先级的就绪状态任务才会依次进入运行状态。
	2.非运行态
		2.1 ready
			2.1.1 如何进入
				从block状态进入
			2.1.2 任务A如何退出ready
				在其他任务中vTaskSuspend 任务A
				进入running状态(运行态)
		2.2 suspend
			2.2.1 如何进入
				在其他任务中vTaskSuspend 任务A
			2.2.2 如何退出
				vTaskResume,进入ready
		2.3 block
			2.3.1 如何进入
				2.3.1.1 Methods can all be used to create synchronization events
						1.queues,
						2.binary semaphores, 
						3.counting semaphores, 
						4.mutexes, 
						5.recursive mutexes,
						5.event groups
						6.direct to task notifications 
				2.3.1.2 Temporal (time-related) events
					vTaskDelay
			2.3.2 如何退出
				2.3.1.1 Methods can all be used to create synchronization events
						1.queues,
						2.binary semaphores, 
						3.counting semaphores, 
						4.mutexes, 
						5.recursive mutexes,
						5.event groups
						6.direct to task notifications 
				2.3.1.2 Temporal (time-related) events
					systick中断	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值