core_task.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. enum
  24. {
  25. TASK_POINT_LIST_HEAD,
  26. TASK_POINT_EVENT_SEM,
  27. TASK_POINT_EVENT_TIMER,
  28. TASK_POINT_DELAY_SEM,
  29. TASK_POINT_DELAY_TIMER,
  30. TASK_POINT_DEBUG,
  31. };
  32. typedef struct
  33. {
  34. llist_head Node;
  35. uint32_t ID;
  36. uint32_t Param1;
  37. uint32_t Param2;
  38. uint32_t Param3;
  39. }Core_EventStruct;
  40. extern void * vTaskGetPoint(TaskHandle_t xHandle, uint8_t Sn);
  41. extern void vTaskSetPoint(TaskHandle_t xHandle, uint8_t Sn, void * p);
  42. extern void *vTaskGetCurrent(void);
  43. extern char * vTaskInfo(void *xHandle, uint32_t *StackTopAddress, uint32_t *StackStartAddress, uint32_t *Len);
  44. extern void vTaskModifyPoint(TaskHandle_t xHandle, uint8_t Sn, int Add);
  45. static int32_t prvTaskTimerCallback(void *pData, void *pParam)
  46. {
  47. Task_SendEvent(pParam, CORE_EVENT_TIMEOUT, 0, 0, 0);
  48. return 0;
  49. }
  50. static int32_t prvTaskDelayTimerCallback(void *pData, void *pParam)
  51. {
  52. SemaphoreHandle_t sem = (SemaphoreHandle_t)vTaskGetPoint(pParam, TASK_POINT_DELAY_SEM);
  53. OS_MutexRelease(sem);
  54. return 0;
  55. }
  56. HANDLE Task_Create(TaskFun_t EntryFunction, void *Param, uint32_t StackSize, uint32_t Priority, const char *Name)
  57. {
  58. TaskHandle_t Handle;
  59. llist_head *Head;
  60. Timer_t *Timer, *DelayTimer;
  61. SemaphoreHandle_t Sem, DelaySem;
  62. if (pdPASS != xTaskCreate(EntryFunction, Name, StackSize>>2, Param, Priority, &Handle))
  63. {
  64. return NULL;
  65. }
  66. if (!Handle) return NULL;
  67. Head = OS_Zalloc(sizeof(llist_head));
  68. Sem = OS_MutexCreate();
  69. DelaySem = OS_MutexCreate();
  70. Timer = Timer_Create(prvTaskTimerCallback, Handle, NULL);
  71. DelayTimer = Timer_Create(prvTaskDelayTimerCallback, Handle, NULL);
  72. INIT_LLIST_HEAD(Head);
  73. vTaskSetPoint(Handle, TASK_POINT_LIST_HEAD, Head);
  74. vTaskSetPoint(Handle, TASK_POINT_EVENT_SEM, Sem);
  75. vTaskSetPoint(Handle, TASK_POINT_EVENT_TIMER, Timer);
  76. vTaskSetPoint(Handle, TASK_POINT_DELAY_SEM, DelaySem);
  77. vTaskSetPoint(Handle, TASK_POINT_DELAY_TIMER, DelayTimer);
  78. vTaskSetPoint(Handle, TASK_POINT_DEBUG, 0);
  79. // DBG("%s, %x", Name, Handle);
  80. return Handle;
  81. }
  82. void Task_SendEvent(HANDLE TaskHandle, uint32_t ID, uint32_t P1, uint32_t P2, uint32_t P3)
  83. {
  84. if (!TaskHandle) return;
  85. uint32_t Critical;
  86. llist_head *Head = (llist_head *)vTaskGetPoint(TaskHandle, TASK_POINT_LIST_HEAD);
  87. SemaphoreHandle_t sem = (SemaphoreHandle_t)vTaskGetPoint(TaskHandle, TASK_POINT_EVENT_SEM);
  88. Core_EventStruct *Event = OS_Zalloc(sizeof(Core_EventStruct));
  89. Event->ID = ID;
  90. Event->Param1 = P1;
  91. Event->Param2 = P2;
  92. Event->Param3 = P3;
  93. Critical = OS_EnterCritical();
  94. vTaskModifyPoint(TaskHandle, TASK_POINT_DEBUG, 1);
  95. llist_add_tail(&Event->Node, Head);
  96. if (vTaskGetPoint(TaskHandle, TASK_POINT_DEBUG) >= 1024)
  97. {
  98. DBG_Trace("%s wait too much msg!", vTaskInfo(TaskHandle, &ID, &P1, &P2));
  99. Core_PrintServiceStack();
  100. __disable_irq();
  101. while(1) {;}
  102. }
  103. OS_ExitCritical(Critical);
  104. OS_MutexRelease(sem);
  105. }
  106. int32_t Task_GetEvent(HANDLE TaskHandle, uint32_t TargetEventID, Task_EventStruct *OutEvent, CBFuncEx_t Callback, uint64_t Tick)
  107. {
  108. uint64_t ToTick = 0;
  109. Core_EventStruct *Event;
  110. uint32_t Critical;
  111. int32_t Result = ERROR_NONE;
  112. if (OS_CheckInIrq())
  113. {
  114. return -ERROR_PERMISSION_DENIED;
  115. }
  116. if (!TaskHandle) return -ERROR_PARAM_INVALID;
  117. llist_head *Head = (llist_head *)vTaskGetPoint(TaskHandle, TASK_POINT_LIST_HEAD);
  118. SemaphoreHandle_t sem = (SemaphoreHandle_t)vTaskGetPoint(TaskHandle, TASK_POINT_EVENT_SEM);
  119. Timer_t *Timer = (Timer_t *)vTaskGetPoint(TaskHandle, TASK_POINT_EVENT_TIMER);
  120. if (Tick)
  121. {
  122. ToTick = GetSysTick() + Tick;
  123. }
  124. GET_NEW_EVENT:
  125. Critical = OS_EnterCritical();
  126. if (!llist_empty(Head))
  127. {
  128. Event = (Core_EventStruct *)Head->next;
  129. llist_del(&Event->Node);
  130. vTaskModifyPoint(TaskHandle, TASK_POINT_DEBUG, -1);
  131. }
  132. else
  133. {
  134. Event = NULL;
  135. }
  136. OS_ExitCritical(Critical);
  137. if (Event)
  138. {
  139. OutEvent->ID = Event->ID;
  140. OutEvent->Param1 = Event->Param1;
  141. OutEvent->Param2 = Event->Param2;
  142. OutEvent->Param3 = Event->Param3;
  143. OS_Free(Event);
  144. }
  145. else
  146. {
  147. if (!ToTick)
  148. {
  149. goto WAIT_NEW_EVENT;
  150. }
  151. else
  152. {
  153. if (ToTick > (5 * SYS_TIMER_1US + GetSysTick()))
  154. {
  155. goto WAIT_NEW_EVENT;
  156. }
  157. else
  158. {
  159. Result = -ERROR_TIMEOUT;
  160. goto GET_EVENT_DONE;
  161. }
  162. }
  163. }
  164. switch(OutEvent->ID)
  165. {
  166. case CORE_EVENT_TIMEOUT:
  167. goto GET_NEW_EVENT;
  168. }
  169. if ((TargetEventID == CORE_EVENT_ID_ANY) || (OutEvent->ID == TargetEventID))
  170. {
  171. goto GET_EVENT_DONE;
  172. }
  173. if (Callback)
  174. {
  175. Callback(OutEvent, NULL);
  176. }
  177. goto GET_NEW_EVENT;
  178. WAIT_NEW_EVENT:
  179. if (ToTick)
  180. {
  181. if (ToTick > (5 * SYS_TIMER_1US + GetSysTick()))
  182. {
  183. Timer_Start(Timer, ToTick - GetSysTick(), 0);
  184. }
  185. else
  186. {
  187. Result = -ERROR_TIMEOUT;
  188. goto GET_EVENT_DONE;
  189. }
  190. }
  191. OS_MutexLock(sem);
  192. goto GET_NEW_EVENT;
  193. GET_EVENT_DONE:
  194. Timer_Stop(Timer);
  195. return Result;
  196. }
  197. int32_t Task_GetEventByMS(HANDLE TaskHandle, uint32_t TargetEventID, Task_EventStruct *OutEvent, CBFuncEx_t Callback, uint32_t ms)
  198. {
  199. uint64_t ToTick = ms;
  200. ToTick *= SYS_TIMER_1MS;
  201. if (ms != 0xffffffff)
  202. {
  203. return Task_GetEvent(TaskHandle, TargetEventID, OutEvent, Callback, ToTick);
  204. }
  205. else
  206. {
  207. return Task_GetEvent(TaskHandle, TargetEventID, OutEvent, Callback, 0);
  208. }
  209. }
  210. HANDLE Task_GetCurrent(void)
  211. {
  212. return vTaskGetCurrent();
  213. }
  214. void Task_DelayTick(uint64_t Tick)
  215. {
  216. if (OS_CheckInIrq() || Tick < (SYS_TIMER_1US*5))
  217. {
  218. SysTickDelay((uint32_t)Tick);
  219. return;
  220. }
  221. HANDLE TaskHandle = vTaskGetCurrent();
  222. SemaphoreHandle_t sem = (SemaphoreHandle_t)vTaskGetPoint(TaskHandle, TASK_POINT_DELAY_SEM);
  223. Timer_t *Timer = (Timer_t *)vTaskGetPoint(TaskHandle, TASK_POINT_DELAY_TIMER);
  224. Timer_Start(Timer, Tick, 0);
  225. OS_MutexLock(sem);
  226. }
  227. void Task_DelayUS(uint32_t US)
  228. {
  229. uint64_t ToTick = US;
  230. ToTick *= SYS_TIMER_1US;
  231. Task_DelayTick(ToTick);
  232. }
  233. void Task_DelayMS(uint32_t MS)
  234. {
  235. uint64_t ToTick = MS;
  236. ToTick *= SYS_TIMER_1MS;
  237. Task_DelayTick(ToTick);
  238. }
  239. #endif