一个任务的线程:
假设一个任务的执行代码有50步,通常编程只会一次执行完毕,但是我们现在需要想想,因为我们会嫌这个任务总占用着ALU的时间而影响其他任务的执行效果,所以就可以对任务进行划分,把它分为5份,每份10步,这样我们每次执行其中的一个程序片–每次正在运行的程序片我们称为线程。(CSDN博客:https://blog.csdn.net/qq_37272520/article/details/88916568)
代码实现
首先定义一个跟任务相关的结构体,Delay正是时间片执行的时长,Period是任务的执行周期
// ------ Public data type declarations ----------------------------
// User-defined type to store required data for each task
typedef struct
{
// Pointer to the task
// (must be a 'uint32_t (void)' function)
uint32_t (*pTask)(void);
// void (*pTask) (void);
// Delay (ticks) until the task will (next) be run
uint32_t Delay;
// Interval (ticks) between subsequent runs.
uint32_t Period;
} sTask_t;
添加(创建)任务
// Add_Task
void SCH_Add_Task(uint32_t (*pTask)(),
const uint32_t DELAY,
const uint32_t PERIOD)
{
uint32_t Task_id = 0;
// Check pre-conditions (START)
// First find a gap in the array (if there is one)
while ((SCH_tasks_g[Task_id].pTask != SCH_NULL_PTR) && (Task_id < SCH_MAX_TASKS))
{
Task_id++;
}
// Have we reached the end of the list?
if ((Task_id < SCH_MAX_TASKS) || (PERIOD > 0))
{
// If we're here, there is a space in the task array
// and the task to be added is periodic
SCH_tasks_g[Task_id].pTask = pTask;
SCH_tasks_g[Task_id].Delay = DELAY + 1;
SCH_tasks_g[Task_id].Period = PERIOD;
}
}
删除任务
void SCH_delete_Task(uint32_t (*pTask)())
{
uint32_t id_counter;
for (id_counter = 0; id_counter < SCH_MAX_TASKS;)
{
if (SCH_tasks_g[id_counter].pTask != pTask)
id_counter++;
else
{
__disable_irq();
SCH_tasks_g[id_counter].pTask = SCH_NULL_PTR;
__enable_irq();
id_counter = SCH_MAX_TASKS + 1;
}
}
}
更改任务
//任务运行过程中切换为其他任务运行。
//则当前任务返回后不再运行。
//为了安全应该关中断操作。
// 可以在task中增加一个参数,task运行到一定次数切换到其他的task;
//或者 事件触发 退出当前task,执行新的task
void SCH_change_Task(uint32_t (*pTask)(),
const uint32_t DELAY,
const uint32_t PERIOD)
{
__disable_irq();
if ((Current_Task_id < SCH_MAX_TASKS) || (PERIOD > 0))
{
SCH_tasks_g[Current_Task_id].pTask = pTask;
SCH_tasks_g[Current_Task_id].Delay = DELAY + 1;
SCH_tasks_g[Current_Task_id].Period = PERIOD;
}
__enable_irq();
}
执行调度器
/*----------------------------------------------------------------------------*-
SCH_Dispatch_Tasks()
-*----------------------------------------------------------------------------*/
void SCH_Dispatch_Tasks(void)
{
uint32_t Status;
uint32_t Task_id;
// Go through the task array
for (Task_id = 0; Task_id < SCH_MAX_TASKS; Task_id++)
{
// Check if there is a task at this location
if (SCH_tasks_g[Task_id].pTask != SCH_NULL_PTR)
{
if (SCH_tasks_g[Task_id].Delay == 0)
{
// printf("\n task=%d \n",Task_id);
Current_Task_id = Task_id;
Status = (*SCH_tasks_g[Task_id].pTask)(); // Run the task
// All tasks are periodic: schedule task to run again
SCH_tasks_g[Task_id].Delay = SCH_tasks_g[Task_id].Period;
}
}
}
// Update inverted copy of Tick_count_g
// Tick_count_ig = ~Tick_count_g;
// The scheduler enters idle mode at this point
// __WFI();
}
定时器查询时间片
void TIMX_IRQHandler_user(void)
{
uint32_t Task_id;
++Tick_count_g;
for (Task_id = 0; Task_id < SCH_MAX_TASKS; Task_id++)
{
if (SCH_tasks_g[Task_id].Delay > 0)
SCH_tasks_g[Task_id].Delay--;
}
}
可以看到,代码量是非常小的,当然了,功能也很单一,有得必有失嘛
实验测试
封装好了必要的函数之后,接下来学习如何使用,很简单,首先创建几个任务,小飞哥创建了2个任务,两个任务分别是task01,“时间片”是50ms(自己根据需要订),任务周期是500ms,task02,“时间片”是10ms(自己根据需要订),任务周期是1000ms
SCH_Add_Task(Task_01,50,500);
SCH_Add_Task(Task_02,10,1000);
uint32_t Task_01(void){
//HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
//HAL_Delay(500);
printf("task01 test\r\n");
}
uint32_t Task_02(void){
printf("task02 test\r\n");
//HAL_Delay(500);
}
在systick(或者其他定时器)中调用,关于Systick的使用详解见:Systick
void HAL_SYSTICK_Callback(void)
{
TIMX_IRQHandler_user(); //100ms调用一次
// systick_flag = 1; //中断置标志,逻辑函数中断外执行
}
最后只需要在while中调用调度器就OK了(类似于LVGL的设计思路),根据我们的设计,两个任务,一个是500ms打印“task01 test”,另一个1000ms打印“task02 test”
OK,完美,end~