嵌入式学习-FreeRTOS-Day3
一、思维导图
二、
1.FreeRTOS任务的调度算法及实现
默认是抢占式调度+时间片轮询
1.抢占式调度:任务优先级高的可以打断任务优先级低的执行(适用于不同优先级)
2.时间片轮转:每一个任务拥有相同的时间片(1ms),保证任务执行的公平性,当一个任务的时间片耗尽时任务就必须退出(适用于相同优先级)
3.协作式调度:任务自行决定何时释放处理器控制权。通常是通过调用延迟函数或主动让出控制权的函数来实现。协作式调度不会被中断打断,而是等待任务自愿地让出 CPU 控制权。
验证抢占式调度、时间片轮询
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {.name = "defaultTask",.stack_size = 128 * 4,.priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for myTask02 */
osThreadId_t myTask02Handle;
uint32_t myTask02Buffer[ 64 ];
osStaticThreadDef_t myTask02ControlBlock;
const osThreadAttr_t myTask02_attributes = {.name = "myTask02",.cb_mem = &myTask02ControlBlock,.cb_size = sizeof(myTask02ControlBlock),.stack_mem = &myTask02Buffer[0],.stack_size = sizeof(myTask02Buffer),.priority = (osPriority_t) osPriorityNormal1,
};
/* Definitions for myTask03 */
osThreadId_t myTask03ControlBlock;
const osThreadAttr_t myTask03_attributes = {.name = "myTask03",.stack_size = 128 * 4,.priority = (osPriority_t) osPriorityNormal,
};/* Private function prototypes -----------------------------------------------*/
void StartDefaultTask(void *argument);
void StartTask02(void *argument);
void StartTask03(void *argument);/* Create the thread(s) *//* creation of defaultTask */defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);/* creation of myTask02 */myTask02Handle = osThreadNew(StartTask02, NULL, &myTask02_attributes);/* creation of myTask03 */myTask03Handle = osThreadNew(StartTask03, NULL, &myTask03_attributes); /* USER CODE BEGIN RTOS_THREADS *//* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{/* USER CODE BEGIN StartDefaultTask *//* Infinite loop *///int num = 0;int i = 0;for(;;){printf("defaulttask start\r\n");for (i = 0; i<10000000; i++){;}printf("defaulttask end\r\n");}/* USER CODE END StartDefaultTask */
}/* USER CODE END Header_StartTask02 */
void StartTask02(void *argument)
{/* USER CODE BEGIN StartTask02 *//* Infinite loop *///int i=1;for(;;){printf("task02 running\r\n");}/* USER CODE END StartTask02 */
}/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
void StartTask03(void *argument)
{for(;;){printf("task03 running\r\n");}
}
/* USER CODE END Application */
运行结果
2.任务的状态以及是怎么样进行转换的
创建态、就绪态、执行态、阻塞态、终止态