123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- #include "LH_TaskManager.h"
- /*
- 文件说明:任务管理器源文件
- 作者: 刘浩
- 原创代码:使用请保留此段
- 更新日期:2020年3月27日
- 注意:本程序在项目实践测试过,能够正常稳定运行
- 目前支持MCU型号:STC Atmel stm32 stm8 msp430 cc2530 新唐 NXP pic
- */
- LH_TaskList ON_Stack,Running_Stack,Suspend_Stack;
- TaskList_Items LH_UserTsak[TaskNumber];
- int (* Running_TaskFun)(void);
- void TaskManager_Init(void)//三个任务堆栈进行初始化
- {
- char idx;
- ON_Stack=0,Running_Stack=0;Suspend_Stack = 0;
- for(idx=0;idx<TaskNumber;idx++)
- {
- LH_UserTsak[idx].Task_ID = 0;
- LH_UserTsak[idx].LIst_Next = 0;
- LH_UserTsak[idx]._Fun=0;
- }
- }
- char Start_Tasks(char id,int time) // 启动挂起任务
- {
- LH_TaskList List_Item,List_Item1;
- List_Item1 = List_Item = Suspend_Stack;
- while(List_Item != 0)
- {
- if(List_Item->Task_ID == id)
- {
- DISABLE_INTERRUPT__ // 关闭总中断
- if(List_Item == Suspend_Stack )
- Suspend_Stack = List_Item->LIst_Next;
- else
- List_Item1->LIst_Next = List_Item->LIst_Next;
-
- List_Item->Task_Time = time ;
- if(time == 0)
- Add_WaitList(List_Item) ;
- else
- Add_TimingList(List_Item) ;
-
- ENABLED_INTERRUPT__ // 打开总中断
- return 1; // 返回成功
-
- }
- List_Item1 = List_Item;
- List_Item = List_Item->LIst_Next;
- }
- return 0; // 失败,未找到相应任务
- }
- char Stop_Tasks(char id) // 将任务挂起
- {
- LH_TaskList List_Item,List_Item1;
- DISABLE_INTERRUPT__
- if(ON_Stack != 0)
- {
- List_Item1 = List_Item = ON_Stack;
- while(List_Item != 0)
- {
- if(List_Item->Task_ID == id)
- { DISABLE_INTERRUPT__ // 关闭总中断
- if(List_Item == ON_Stack)
- ON_Stack = List_Item->LIst_Next ;
- else
- List_Item1->LIst_Next = List_Item->LIst_Next ;
-
- Add_SuspendList(List_Item) ;
- ENABLED_INTERRUPT__ // 打开总中断
- return 1; // 任务挂起成功,返回1
- }
- List_Item1 = List_Item;
- List_Item = List_Item->LIst_Next;
- }
- }
-
- if(Running_Stack != 0)
- {
- List_Item1 = List_Item = Running_Stack;
- while(List_Item != 0)
- {
- if(List_Item->Task_ID == id)
- { DISABLE_INTERRUPT__ // 关闭总中断
- if(List_Item == Running_Stack)
- Running_Stack = List_Item->LIst_Next ;
- else
- List_Item1->LIst_Next = List_Item->LIst_Next ;
-
- Add_SuspendList(List_Item) ;
- ENABLED_INTERRUPT__ // 打开总中断
- return 2; // 任务挂起成功,返回1
- }
- List_Item1 = List_Item;
- List_Item = List_Item->LIst_Next;
- }
- }
-
-
- ENABLED_INTERRUPT__ // 打开总中断
- return 0;
- }
- char Delete_Tasks(char id)
- {
- // 删除任务
- LH_TaskList List_Item,List_Item1;
- Stop_Tasks(id);
- DISABLE_INTERRUPT__
- List_Item1 = List_Item = Suspend_Stack;
- while(List_Item != 0)
- {
- if(List_Item->Task_ID == id)
- {
-
- if(List_Item == Suspend_Stack )
- Suspend_Stack = List_Item->LIst_Next;
- else
- List_Item1->LIst_Next = List_Item->LIst_Next;
-
- List_Item->Task_ID = 0 ; // 删除Task_ID
- ENABLED_INTERRUPT__
- return 1; // 返回成功
-
- }
- List_Item1 = List_Item;
- List_Item = List_Item->LIst_Next;
- }
-
- ENABLED_INTERRUPT__
- return 0; // 找不到任务
- }
- // 创建定时器
- char UserTimerTask_Create(int (*_Fun)(void),int time)
- {
- char i;
- for(i=0;i<TaskNumber;i++) // 寻找当前函数是否已经创建定时器
- {
- if(LH_UserTsak[i]._Fun == _Fun)
- {
- LH_UserTsak[i].Task_Time=time;//重复创建赋值新的定时时间
- return 0; // 已经创建,直接退出
- }
- }
- for(i=0;i<TaskNumber;i++)
- {
- if(LH_UserTsak[i].Task_ID == 0)
- {
- LH_UserTsak[i].Task_ID = -1;
- LH_UserTsak[i]._Fun = _Fun;
- LH_UserTsak[i].Task_Time = time;
- LH_UserTsak[i].LIst_Next = 0;
- Add_TimingList(&LH_UserTsak[i]);
- return 1;
- }
- }
- return 0;
- }
- /*
- 函数功能:创建任务
- 输出参数:id为任务号 _Fun为任务函数 time 为是否需要计时
- */
- char UserTask_Create(char id,int (*_Fun)(void),int time)
- {
- char idx;
- for(idx=0;idx<TaskNumber;idx++) // 查看Task_ID是否为唯一
- {
- if(LH_UserTsak[idx].Task_ID == id)
- return 0; // 返回0失败
- }
- for(idx=0;idx<TaskNumber;idx++) // 寻找空任务栈
- {
- if(LH_UserTsak[idx].Task_ID == 0)
- {
- LH_UserTsak[idx].Task_ID = id;
- LH_UserTsak[idx]._Fun = _Fun;
- LH_UserTsak[idx].Task_Time = time;
- LH_UserTsak[idx].LIst_Next = 0;
-
- if(time > 0)
- {
- Add_TimingList(&LH_UserTsak[idx]);
- }
- else
- {
- Add_SuspendList(&LH_UserTsak[idx]);
-
- }
-
- return 1;
- }
- }
-
- return 0;
- }
- void Add_SuspendList(LH_TaskList LHpStack) // 添加任务到挂起列表
- {
- LH_TaskList List_Item;
- LHpStack->LIst_Next = 0 ;
- do
- {
- if(Suspend_Stack == 0)
- {
- Suspend_Stack = LHpStack;
- break;
- }
-
- List_Item = Suspend_Stack; // 查找表尾
- while(List_Item->LIst_Next !=0)
- {
- List_Item = List_Item->LIst_Next;
- }
- List_Item->LIst_Next = LHpStack;
- }while(0);
- }
- void Add_TimingList(LH_TaskList LHpStack) // 添加任务到定时区
- {
- LH_TaskList List_Item;
- LHpStack->LIst_Next = 0;
- do
- {
- if(ON_Stack == 0)
- {
- ON_Stack = LHpStack;
- break;
- }
- List_Item = ON_Stack;
- while(List_Item->LIst_Next !=0)
- {
- List_Item = List_Item->LIst_Next;
- }
- List_Item->LIst_Next = LHpStack;
- }while(0);
- }
- void TaskManager_Scheduling(void)
- {
- LH_TaskList list,list1;
- list1 = list = ON_Stack; // 两个指针指向前后两个任务,便于删除就绪的任务
- while(list != 0)
- {
- list->Task_Time --;
- if(list->Task_Time == 0)
- {
- if(list == ON_Stack)
- ON_Stack = list->LIst_Next;
- else
- list1->LIst_Next = list->LIst_Next;
-
- Add_WaitList(list);
- }
- list1 = list;
- list = list->LIst_Next;
- }
- }
- void Add_WaitList(LH_TaskList LHpStack)
- {
- LH_TaskList list;
- LHpStack->LIst_Next = 0; // 末端任务标志
- list = Running_Stack;
- if(list == 0) // 等待执行列表为空
- {
- Running_Stack = LHpStack;
- }
- else
- {
- while(list->LIst_Next != 0)
- {
- list = list->LIst_Next;
- }
- list->LIst_Next = LHpStack ;
- }
- }
- void TaskManager_Run(void)
- {
- int time;
- static int id = 0;
- LH_TaskList List_Item;
- while(Running_Stack != 0)
- {
- id = Running_Stack->Task_ID;
- Running_TaskFun = Running_Stack->_Fun;
- time = (*Running_TaskFun)() ;
- if(id == Running_Stack->Task_ID)
- {
- if(Running_Stack->Task_ID > 0)
- {
- List_Item = Running_Stack->LIst_Next;
- if(time >0)
- {
- Running_Stack->Task_Time = time;
- Add_TimingList(Running_Stack) ;
- }
- else
- {
- Add_SuspendList(Running_Stack);
- }
- Running_Stack = List_Item ;
- }
- else
- {
- Running_Stack->Task_ID = 0;
- Running_Stack->_Fun = 0;
- Running_Stack = Running_Stack->LIst_Next;
- }
- }
- }
- }
|