LH_TaskManager.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #include "LH_TaskManager.h"
  2. /*
  3. 文件说明:任务管理器源文件
  4. 作者: 刘浩
  5. 原创代码:使用请保留此段
  6. 更新日期:2020年3月27日
  7. 注意:本程序在项目实践测试过,能够正常稳定运行
  8. 目前支持MCU型号:STC Atmel stm32 stm8 msp430 cc2530 新唐 NXP pic
  9. */
  10. LH_TaskList ON_Stack,Running_Stack,Suspend_Stack;
  11. TaskList_Items LH_UserTsak[TaskNumber];
  12. int (* Running_TaskFun)(void);
  13. void TaskManager_Init(void)//三个任务堆栈进行初始化
  14. {
  15. char idx;
  16. ON_Stack=0,Running_Stack=0;Suspend_Stack = 0;
  17. for(idx=0;idx<TaskNumber;idx++)
  18. {
  19. LH_UserTsak[idx].Task_ID = 0;
  20. LH_UserTsak[idx].LIst_Next = 0;
  21. LH_UserTsak[idx]._Fun=0;
  22. }
  23. }
  24. char Start_Tasks(char id,int time) // 启动挂起任务
  25. {
  26. LH_TaskList List_Item,List_Item1;
  27. List_Item1 = List_Item = Suspend_Stack;
  28. while(List_Item != 0)
  29. {
  30. if(List_Item->Task_ID == id)
  31. {
  32. DISABLE_INTERRUPT__ // 关闭总中断
  33. if(List_Item == Suspend_Stack )
  34. Suspend_Stack = List_Item->LIst_Next;
  35. else
  36. List_Item1->LIst_Next = List_Item->LIst_Next;
  37. List_Item->Task_Time = time ;
  38. if(time == 0)
  39. Add_WaitList(List_Item) ;
  40. else
  41. Add_TimingList(List_Item) ;
  42. ENABLED_INTERRUPT__ // 打开总中断
  43. return 1; // 返回成功
  44. }
  45. List_Item1 = List_Item;
  46. List_Item = List_Item->LIst_Next;
  47. }
  48. return 0; // 失败,未找到相应任务
  49. }
  50. char Stop_Tasks(char id) // 将任务挂起
  51. {
  52. LH_TaskList List_Item,List_Item1;
  53. DISABLE_INTERRUPT__
  54. if(ON_Stack != 0)
  55. {
  56. List_Item1 = List_Item = ON_Stack;
  57. while(List_Item != 0)
  58. {
  59. if(List_Item->Task_ID == id)
  60. { DISABLE_INTERRUPT__ // 关闭总中断
  61. if(List_Item == ON_Stack)
  62. ON_Stack = List_Item->LIst_Next ;
  63. else
  64. List_Item1->LIst_Next = List_Item->LIst_Next ;
  65. Add_SuspendList(List_Item) ;
  66. ENABLED_INTERRUPT__ // 打开总中断
  67. return 1; // 任务挂起成功,返回1
  68. }
  69. List_Item1 = List_Item;
  70. List_Item = List_Item->LIst_Next;
  71. }
  72. }
  73. if(Running_Stack != 0)
  74. {
  75. List_Item1 = List_Item = Running_Stack;
  76. while(List_Item != 0)
  77. {
  78. if(List_Item->Task_ID == id)
  79. { DISABLE_INTERRUPT__ // 关闭总中断
  80. if(List_Item == Running_Stack)
  81. Running_Stack = List_Item->LIst_Next ;
  82. else
  83. List_Item1->LIst_Next = List_Item->LIst_Next ;
  84. Add_SuspendList(List_Item) ;
  85. ENABLED_INTERRUPT__ // 打开总中断
  86. return 2; // 任务挂起成功,返回1
  87. }
  88. List_Item1 = List_Item;
  89. List_Item = List_Item->LIst_Next;
  90. }
  91. }
  92. ENABLED_INTERRUPT__ // 打开总中断
  93. return 0;
  94. }
  95. char Delete_Tasks(char id)
  96. {
  97. // 删除任务
  98. LH_TaskList List_Item,List_Item1;
  99. Stop_Tasks(id);
  100. DISABLE_INTERRUPT__
  101. List_Item1 = List_Item = Suspend_Stack;
  102. while(List_Item != 0)
  103. {
  104. if(List_Item->Task_ID == id)
  105. {
  106. if(List_Item == Suspend_Stack )
  107. Suspend_Stack = List_Item->LIst_Next;
  108. else
  109. List_Item1->LIst_Next = List_Item->LIst_Next;
  110. List_Item->Task_ID = 0 ; // 删除Task_ID
  111. ENABLED_INTERRUPT__
  112. return 1; // 返回成功
  113. }
  114. List_Item1 = List_Item;
  115. List_Item = List_Item->LIst_Next;
  116. }
  117. ENABLED_INTERRUPT__
  118. return 0; // 找不到任务
  119. }
  120. // 创建定时器
  121. char UserTimerTask_Create(int (*_Fun)(void),int time)
  122. {
  123. char i;
  124. for(i=0;i<TaskNumber;i++) // 寻找当前函数是否已经创建定时器
  125. {
  126. if(LH_UserTsak[i]._Fun == _Fun)
  127. {
  128. LH_UserTsak[i].Task_Time=time;//重复创建赋值新的定时时间
  129. return 0; // 已经创建,直接退出
  130. }
  131. }
  132. for(i=0;i<TaskNumber;i++)
  133. {
  134. if(LH_UserTsak[i].Task_ID == 0)
  135. {
  136. LH_UserTsak[i].Task_ID = -1;
  137. LH_UserTsak[i]._Fun = _Fun;
  138. LH_UserTsak[i].Task_Time = time;
  139. LH_UserTsak[i].LIst_Next = 0;
  140. Add_TimingList(&LH_UserTsak[i]);
  141. return 1;
  142. }
  143. }
  144. return 0;
  145. }
  146. /*
  147. 函数功能:创建任务
  148. 输出参数:id为任务号 _Fun为任务函数 time 为是否需要计时
  149. */
  150. char UserTask_Create(char id,int (*_Fun)(void),int time)
  151. {
  152. char idx;
  153. for(idx=0;idx<TaskNumber;idx++) // 查看Task_ID是否为唯一
  154. {
  155. if(LH_UserTsak[idx].Task_ID == id)
  156. return 0; // 返回0失败
  157. }
  158. for(idx=0;idx<TaskNumber;idx++) // 寻找空任务栈
  159. {
  160. if(LH_UserTsak[idx].Task_ID == 0)
  161. {
  162. LH_UserTsak[idx].Task_ID = id;
  163. LH_UserTsak[idx]._Fun = _Fun;
  164. LH_UserTsak[idx].Task_Time = time;
  165. LH_UserTsak[idx].LIst_Next = 0;
  166. if(time > 0)
  167. {
  168. Add_TimingList(&LH_UserTsak[idx]);
  169. }
  170. else
  171. {
  172. Add_SuspendList(&LH_UserTsak[idx]);
  173. }
  174. return 1;
  175. }
  176. }
  177. return 0;
  178. }
  179. void Add_SuspendList(LH_TaskList LHpStack) // 添加任务到挂起列表
  180. {
  181. LH_TaskList List_Item;
  182. LHpStack->LIst_Next = 0 ;
  183. do
  184. {
  185. if(Suspend_Stack == 0)
  186. {
  187. Suspend_Stack = LHpStack;
  188. break;
  189. }
  190. List_Item = Suspend_Stack; // 查找表尾
  191. while(List_Item->LIst_Next !=0)
  192. {
  193. List_Item = List_Item->LIst_Next;
  194. }
  195. List_Item->LIst_Next = LHpStack;
  196. }while(0);
  197. }
  198. void Add_TimingList(LH_TaskList LHpStack) // 添加任务到定时区
  199. {
  200. LH_TaskList List_Item;
  201. LHpStack->LIst_Next = 0;
  202. do
  203. {
  204. if(ON_Stack == 0)
  205. {
  206. ON_Stack = LHpStack;
  207. break;
  208. }
  209. List_Item = ON_Stack;
  210. while(List_Item->LIst_Next !=0)
  211. {
  212. List_Item = List_Item->LIst_Next;
  213. }
  214. List_Item->LIst_Next = LHpStack;
  215. }while(0);
  216. }
  217. void TaskManager_Scheduling(void)
  218. {
  219. LH_TaskList list,list1;
  220. list1 = list = ON_Stack; // 两个指针指向前后两个任务,便于删除就绪的任务
  221. while(list != 0)
  222. {
  223. list->Task_Time --;
  224. if(list->Task_Time == 0)
  225. {
  226. if(list == ON_Stack)
  227. ON_Stack = list->LIst_Next;
  228. else
  229. list1->LIst_Next = list->LIst_Next;
  230. Add_WaitList(list);
  231. }
  232. list1 = list;
  233. list = list->LIst_Next;
  234. }
  235. }
  236. void Add_WaitList(LH_TaskList LHpStack)
  237. {
  238. LH_TaskList list;
  239. LHpStack->LIst_Next = 0; // 末端任务标志
  240. list = Running_Stack;
  241. if(list == 0) // 等待执行列表为空
  242. {
  243. Running_Stack = LHpStack;
  244. }
  245. else
  246. {
  247. while(list->LIst_Next != 0)
  248. {
  249. list = list->LIst_Next;
  250. }
  251. list->LIst_Next = LHpStack ;
  252. }
  253. }
  254. void TaskManager_Run(void)
  255. {
  256. int time;
  257. static int id = 0;
  258. LH_TaskList List_Item;
  259. while(Running_Stack != 0)
  260. {
  261. id = Running_Stack->Task_ID;
  262. Running_TaskFun = Running_Stack->_Fun;
  263. time = (*Running_TaskFun)() ;
  264. if(id == Running_Stack->Task_ID)
  265. {
  266. if(Running_Stack->Task_ID > 0)
  267. {
  268. List_Item = Running_Stack->LIst_Next;
  269. if(time >0)
  270. {
  271. Running_Stack->Task_Time = time;
  272. Add_TimingList(Running_Stack) ;
  273. }
  274. else
  275. {
  276. Add_SuspendList(Running_Stack);
  277. }
  278. Running_Stack = List_Item ;
  279. }
  280. else
  281. {
  282. Running_Stack->Task_ID = 0;
  283. Running_Stack->_Fun = 0;
  284. Running_Stack = Running_Stack->LIst_Next;
  285. }
  286. }
  287. }
  288. }