core_task.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "user.h"
  22. #ifdef __BUILD_OS__
  23. #define __USE_FREERTOS_NOTIFY__
  24. #define __USE_CORE_TIMER__
  25. enum
  26. {
  27. TASK_NOTIFY_EVENT = tskDEFAULT_INDEX_TO_NOTIFY,
  28. TASK_NOTIFY_DELAY,
  29. };
  30. typedef struct
  31. {
  32. StaticTask_t TCB;
  33. uint8_t dummy[1]; //不加这个就会死机
  34. volatile llist_head EventHead;
  35. #ifdef __USE_CORE_TIMER__
  36. Timer_t *EventTimer;
  37. Timer_t *DelayTimer;
  38. #endif
  39. uint16_t EventCnt;
  40. uint8_t isRun;
  41. }Core_TaskStruct;
  42. typedef struct
  43. {
  44. llist_head Node;
  45. uint32_t ID;
  46. uint32_t Param1;
  47. uint32_t Param2;
  48. uint32_t Param3;
  49. }Core_EventStruct;
  50. extern void *vTaskGetCurrent(void);
  51. extern char * vTaskInfo(void *xHandle, uint32_t *StackTopAddress, uint32_t *StackStartAddress, uint32_t *Len);
  52. static int32_t prvTaskTimerCallback(void *pData, void *pParam)
  53. {
  54. Task_SendEvent(pParam, CORE_EVENT_TIMEOUT, 0, 0, 0);
  55. return 0;
  56. }
  57. #ifdef __USE_CORE_TIMER__
  58. static int32_t prvTaskDelayTimerCallback(void *pData, void *pParam)
  59. {
  60. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  61. vTaskGenericNotifyGiveFromISR(pParam, TASK_NOTIFY_DELAY, &xHigherPriorityTaskWoken);
  62. if (xHigherPriorityTaskWoken)
  63. {
  64. portYIELD_WITHIN_API();
  65. }
  66. return 0;
  67. }
  68. #endif
  69. HANDLE Task_Create(TaskFun_t EntryFunction, void *Param, uint32_t StackSize, uint32_t Priority, const char *Name)
  70. {
  71. StackSize = (StackSize + 3) >> 2;
  72. uint32_t *stack_mem = zalloc(StackSize * 4);
  73. Core_TaskStruct *Handle = zalloc(sizeof(Core_TaskStruct));
  74. if (!Handle || !stack_mem)
  75. {
  76. free(Handle);
  77. free(stack_mem);
  78. return NULL;
  79. }
  80. #ifdef __USE_CORE_TIMER__
  81. Handle->EventTimer = Timer_Create(prvTaskTimerCallback, Handle, NULL);
  82. Handle->DelayTimer = Timer_Create(prvTaskDelayTimerCallback, Handle, NULL);
  83. #endif
  84. if (!xTaskCreateStatic(EntryFunction, Name, StackSize, Param, Priority, stack_mem, &Handle->TCB))
  85. {
  86. Timer_Release(Handle->EventTimer);
  87. Timer_Release(Handle->DelayTimer);
  88. free(Handle);
  89. free(stack_mem);
  90. return NULL;
  91. }
  92. Handle->TCB.uxDummy20 = 0;
  93. INIT_LLIST_HEAD(&Handle->EventHead);
  94. Handle->isRun = 1;
  95. return Handle;
  96. }
  97. static int32_t DeleteTaskEvent(void *data, void *param)
  98. {
  99. return LIST_DEL;
  100. }
  101. void Task_Exit(HANDLE TaskHandle)
  102. {
  103. if (!TaskHandle)
  104. {
  105. TaskHandle = vTaskGetCurrent();
  106. }
  107. Core_TaskStruct *Handle = (Core_TaskStruct *)TaskHandle;
  108. uint32_t cr;
  109. cr = OS_EnterCritical();
  110. llist_traversal(&Handle->EventHead, DeleteTaskEvent, NULL);
  111. Handle->isRun = 0;
  112. #ifdef __USE_CORE_TIMER__
  113. Timer_Release(Handle->EventTimer);
  114. Timer_Release(Handle->DelayTimer);
  115. #endif
  116. OS_ExitCritical(cr);
  117. vTaskDelete(&Handle->TCB);
  118. }
  119. void Task_SendEvent(HANDLE TaskHandle, uint32_t ID, uint32_t P1, uint32_t P2, uint32_t P3)
  120. {
  121. if (!TaskHandle) return;
  122. uint32_t Critical;
  123. Core_TaskStruct *Handle = (void *)TaskHandle;
  124. volatile Core_EventStruct *Event = zalloc(sizeof(Core_EventStruct));
  125. volatile Core_EventStruct *Event2;
  126. Event->ID = ID;
  127. Event->Param1 = P1;
  128. Event->Param2 = P2;
  129. Event->Param3 = P3;
  130. Critical = OS_EnterCritical();
  131. Handle->EventCnt++;
  132. Event2 = Handle->EventHead.next;
  133. llist_add_tail(&Event->Node, &(Handle->EventHead));
  134. Event2 = Handle->EventHead.next;
  135. if (Handle->EventCnt >= 1024)
  136. {
  137. DBG_Trace("%s wait too much msg! %u", vTaskInfo(TaskHandle, &ID, &P1, &P2), Handle->EventCnt);
  138. Core_PrintServiceStack();
  139. __disable_irq();
  140. while(1) {;}
  141. }
  142. OS_ExitCritical(Critical);
  143. #ifdef __USE_FREERTOS_NOTIFY__
  144. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  145. if (OS_CheckInIrq())
  146. {
  147. vTaskGenericNotifyGiveFromISR(TaskHandle, TASK_NOTIFY_EVENT, &xHigherPriorityTaskWoken);
  148. if (xHigherPriorityTaskWoken)
  149. {
  150. portYIELD_WITHIN_API();
  151. }
  152. }
  153. else
  154. {
  155. xTaskGenericNotify(TaskHandle, TASK_NOTIFY_EVENT, ( 0 ), eIncrement, NULL );
  156. }
  157. #endif
  158. }
  159. int32_t Task_GetEvent(HANDLE TaskHandle, uint32_t TargetEventID, OS_EVENT *OutEvent, CBFuncEx_t Callback, uint64_t Tick)
  160. {
  161. uint64_t ToTick = 0;
  162. Core_EventStruct *Event;
  163. uint32_t Critical;
  164. int32_t Result = ERROR_NONE;
  165. if (OS_CheckInIrq())
  166. {
  167. return -ERROR_PERMISSION_DENIED;
  168. }
  169. if (!TaskHandle) return -ERROR_PARAM_INVALID;
  170. Core_TaskStruct *Handle = (void *)TaskHandle;
  171. if (vTaskGetCurrent() != TaskHandle)
  172. {
  173. return -1;
  174. }
  175. if (Tick)
  176. {
  177. ToTick = GetSysTick() + Tick;
  178. }
  179. GET_NEW_EVENT:
  180. Critical = OS_EnterCritical();
  181. if (!llist_empty(&Handle->EventHead))
  182. {
  183. Event = (Core_EventStruct *)Handle->EventHead.next;
  184. llist_del(&Event->Node);
  185. if (Handle->EventCnt > 0)
  186. {
  187. Handle->EventCnt--;
  188. }
  189. }
  190. else
  191. {
  192. Event = NULL;
  193. }
  194. OS_ExitCritical(Critical);
  195. if (Event)
  196. {
  197. OutEvent->ID = Event->ID;
  198. OutEvent->Param1 = Event->Param1;
  199. OutEvent->Param2 = Event->Param2;
  200. OutEvent->Param3 = Event->Param3;
  201. free(Event);
  202. }
  203. else
  204. {
  205. if (!ToTick)
  206. {
  207. goto WAIT_NEW_EVENT;
  208. }
  209. else
  210. {
  211. if (ToTick > (5 * SYS_TIMER_1US + GetSysTick()))
  212. {
  213. goto WAIT_NEW_EVENT;
  214. }
  215. else
  216. {
  217. Result = -ERROR_TIMEOUT;
  218. goto GET_EVENT_DONE;
  219. }
  220. }
  221. }
  222. switch(OutEvent->ID)
  223. {
  224. case CORE_EVENT_TIMEOUT:
  225. goto GET_NEW_EVENT;
  226. }
  227. if ((TargetEventID == CORE_EVENT_ID_ANY) || (OutEvent->ID == TargetEventID))
  228. {
  229. goto GET_EVENT_DONE;
  230. }
  231. if (Callback)
  232. {
  233. Callback(OutEvent, TaskHandle);
  234. }
  235. goto GET_NEW_EVENT;
  236. WAIT_NEW_EVENT:
  237. if (ToTick)
  238. {
  239. if (ToTick > (5 * SYS_TIMER_1US + GetSysTick()))
  240. {
  241. Timer_Start(Handle->EventTimer, ToTick - GetSysTick(), 0);
  242. }
  243. else
  244. {
  245. Result = -ERROR_TIMEOUT;
  246. goto GET_EVENT_DONE;
  247. }
  248. }
  249. #ifdef __USE_FREERTOS_NOTIFY__
  250. xTaskGenericNotifyWait(TASK_NOTIFY_EVENT, 0, 0, NULL, portMAX_DELAY);
  251. #endif
  252. goto GET_NEW_EVENT;
  253. GET_EVENT_DONE:
  254. Timer_Stop(Handle->EventTimer);
  255. return Result;
  256. }
  257. int32_t Task_GetEventByMS(HANDLE TaskHandle, uint32_t TargetEventID, OS_EVENT *OutEvent, CBFuncEx_t Callback, uint32_t ms)
  258. {
  259. uint64_t ToTick = ms;
  260. ToTick *= SYS_TIMER_1MS;
  261. if (ms != 0xffffffff)
  262. {
  263. return Task_GetEvent(TaskHandle, TargetEventID, OutEvent, Callback, ToTick);
  264. }
  265. else
  266. {
  267. return Task_GetEvent(TaskHandle, TargetEventID, OutEvent, Callback, 0);
  268. }
  269. }
  270. HANDLE Task_GetCurrent(void)
  271. {
  272. return vTaskGetCurrent();
  273. }
  274. void Task_DelayTick(uint64_t Tick)
  275. {
  276. if (OS_CheckInIrq() || Tick < (SYS_TIMER_1US*5))
  277. {
  278. SysTickDelay((uint32_t)Tick);
  279. return;
  280. }
  281. HANDLE TaskHandle = vTaskGetCurrent();
  282. Core_TaskStruct *Handle = (void *)TaskHandle;
  283. Timer_Start(Handle->DelayTimer, Tick, 0);
  284. #ifdef __USE_FREERTOS_NOTIFY__
  285. xTaskGenericNotifyWait(TASK_NOTIFY_DELAY, 0, 0, NULL, portMAX_DELAY);
  286. #endif
  287. }
  288. void Task_DelayUS(uint32_t US)
  289. {
  290. uint64_t ToTick = US;
  291. ToTick *= SYS_TIMER_1US;
  292. Task_DelayTick(ToTick);
  293. }
  294. void Task_DelayMS(uint32_t MS)
  295. {
  296. uint64_t ToTick = MS;
  297. ToTick *= SYS_TIMER_1MS;
  298. Task_DelayTick(ToTick);
  299. }
  300. void Task_Debug(HANDLE TaskHandle)
  301. {
  302. if (!TaskHandle)
  303. {
  304. TaskHandle = vTaskGetCurrent();
  305. }
  306. Core_TaskStruct *Handle = (void *)TaskHandle;
  307. DBG("%x,%x,%x", &Handle->EventHead, Handle->EventHead.next, Handle->EventHead.prev);
  308. }
  309. static StaticTask_t prvIdleTCB;
  310. static StaticTask_t prvTimerTaskTCB;
  311. StackType_t prvIdleTaskStack[256];
  312. StackType_t prvTimerTaskStack[256];
  313. void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
  314. StackType_t ** ppxIdleTaskStackBuffer,
  315. uint32_t * pulIdleTaskStackSize ) /*lint !e526 Symbol not defined as it is an application callback. */
  316. {
  317. *ppxIdleTaskTCBBuffer = &prvIdleTCB;
  318. *ppxIdleTaskStackBuffer = prvIdleTaskStack;
  319. *pulIdleTaskStackSize = 256;
  320. }
  321. void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
  322. StackType_t ** ppxTimerTaskStackBuffer,
  323. uint32_t * pulTimerTaskStackSize )
  324. {
  325. *ppxTimerTaskTCBBuffer = &prvTimerTaskTCB;
  326. *ppxTimerTaskStackBuffer = prvTimerTaskStack;
  327. *pulTimerTaskStackSize = 256;
  328. }
  329. #else
  330. void Task_SendEvent(HANDLE TaskHandle, uint32_t ID, uint32_t P1, uint32_t P2, uint32_t P3)
  331. {
  332. }
  333. #endif