core_task.c 8.0 KB

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