core_task.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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_SendEvent(HANDLE TaskHandle, uint32_t ID, uint32_t P1, uint32_t P2, uint32_t P3)
  106. {
  107. if (!TaskHandle) return;
  108. uint32_t Critical;
  109. llist_head *Head = (llist_head *)vTaskGetPoint(TaskHandle, TASK_POINT_LIST_HEAD);
  110. #ifdef __USE_SEMAPHORE__
  111. SemaphoreHandle_t sem = (SemaphoreHandle_t)vTaskGetPoint(TaskHandle, TASK_POINT_EVENT_SEM);
  112. #endif
  113. Core_EventStruct *Event = OS_Zalloc(sizeof(Core_EventStruct));
  114. Event->ID = ID;
  115. Event->Param1 = P1;
  116. Event->Param2 = P2;
  117. Event->Param3 = P3;
  118. Critical = OS_EnterCritical();
  119. vTaskModifyPoint(TaskHandle, TASK_POINT_DEBUG, 1);
  120. llist_add_tail(&Event->Node, Head);
  121. if (vTaskGetPoint(TaskHandle, TASK_POINT_DEBUG) >= 1024)
  122. {
  123. DBG_Trace("%s wait too much msg!", vTaskInfo(TaskHandle, &ID, &P1, &P2));
  124. Core_PrintServiceStack();
  125. __disable_irq();
  126. while(1) {;}
  127. }
  128. OS_ExitCritical(Critical);
  129. #ifdef __USE_SEMAPHORE__
  130. OS_MutexRelease(sem);
  131. #endif
  132. #ifdef __USE_FREERTOS_NOTIFY__
  133. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  134. if (OS_CheckInIrq())
  135. {
  136. vTaskGenericNotifyGiveFromISR(TaskHandle, TASK_NOTIFY_EVENT, &xHigherPriorityTaskWoken);
  137. if (xHigherPriorityTaskWoken)
  138. {
  139. portYIELD_WITHIN_API();
  140. }
  141. }
  142. else
  143. {
  144. xTaskGenericNotify(TaskHandle, TASK_NOTIFY_EVENT, ( 0 ), eIncrement, NULL );
  145. }
  146. #endif
  147. }
  148. int32_t Task_GetEvent(HANDLE TaskHandle, uint32_t TargetEventID, Task_EventStruct *OutEvent, CBFuncEx_t Callback, uint64_t Tick)
  149. {
  150. uint64_t ToTick = 0;
  151. Core_EventStruct *Event;
  152. uint32_t Critical;
  153. int32_t Result = ERROR_NONE;
  154. if (OS_CheckInIrq())
  155. {
  156. return -ERROR_PERMISSION_DENIED;
  157. }
  158. if (!TaskHandle) return -ERROR_PARAM_INVALID;
  159. llist_head *Head = (llist_head *)vTaskGetPoint(TaskHandle, TASK_POINT_LIST_HEAD);
  160. #ifdef __USE_SEMAPHORE__
  161. SemaphoreHandle_t sem = (SemaphoreHandle_t)vTaskGetPoint(TaskHandle, TASK_POINT_EVENT_SEM);
  162. #endif
  163. Timer_t *Timer = (Timer_t *)vTaskGetPoint(TaskHandle, TASK_POINT_EVENT_TIMER);
  164. if (Tick)
  165. {
  166. ToTick = GetSysTick() + Tick;
  167. }
  168. GET_NEW_EVENT:
  169. Critical = OS_EnterCritical();
  170. if (!llist_empty(Head))
  171. {
  172. Event = (Core_EventStruct *)Head->next;
  173. llist_del(&Event->Node);
  174. vTaskModifyPoint(TaskHandle, TASK_POINT_DEBUG, -1);
  175. }
  176. else
  177. {
  178. Event = NULL;
  179. }
  180. OS_ExitCritical(Critical);
  181. if (Event)
  182. {
  183. OutEvent->ID = Event->ID;
  184. OutEvent->Param1 = Event->Param1;
  185. OutEvent->Param2 = Event->Param2;
  186. OutEvent->Param3 = Event->Param3;
  187. OS_Free(Event);
  188. }
  189. else
  190. {
  191. if (!ToTick)
  192. {
  193. goto WAIT_NEW_EVENT;
  194. }
  195. else
  196. {
  197. if (ToTick > (5 * SYS_TIMER_1US + GetSysTick()))
  198. {
  199. goto WAIT_NEW_EVENT;
  200. }
  201. else
  202. {
  203. Result = -ERROR_TIMEOUT;
  204. goto GET_EVENT_DONE;
  205. }
  206. }
  207. }
  208. switch(OutEvent->ID)
  209. {
  210. case CORE_EVENT_TIMEOUT:
  211. goto GET_NEW_EVENT;
  212. }
  213. if ((TargetEventID == CORE_EVENT_ID_ANY) || (OutEvent->ID == TargetEventID))
  214. {
  215. goto GET_EVENT_DONE;
  216. }
  217. if (Callback)
  218. {
  219. Callback(OutEvent, NULL);
  220. }
  221. goto GET_NEW_EVENT;
  222. WAIT_NEW_EVENT:
  223. if (ToTick)
  224. {
  225. if (ToTick > (5 * SYS_TIMER_1US + GetSysTick()))
  226. {
  227. Timer_Start(Timer, ToTick - GetSysTick(), 0);
  228. }
  229. else
  230. {
  231. Result = -ERROR_TIMEOUT;
  232. goto GET_EVENT_DONE;
  233. }
  234. }
  235. #ifdef __USE_SEMAPHORE__
  236. OS_MutexLock(sem);
  237. #endif
  238. #ifdef __USE_FREERTOS_NOTIFY__
  239. xTaskGenericNotifyWait(TASK_NOTIFY_EVENT, 0, 0, NULL, portMAX_DELAY);
  240. #endif
  241. goto GET_NEW_EVENT;
  242. GET_EVENT_DONE:
  243. Timer_Stop(Timer);
  244. return Result;
  245. }
  246. int32_t Task_GetEventByMS(HANDLE TaskHandle, uint32_t TargetEventID, Task_EventStruct *OutEvent, CBFuncEx_t Callback, uint32_t ms)
  247. {
  248. uint64_t ToTick = ms;
  249. ToTick *= SYS_TIMER_1MS;
  250. if (ms != 0xffffffff)
  251. {
  252. return Task_GetEvent(TaskHandle, TargetEventID, OutEvent, Callback, ToTick);
  253. }
  254. else
  255. {
  256. return Task_GetEvent(TaskHandle, TargetEventID, OutEvent, Callback, 0);
  257. }
  258. }
  259. HANDLE Task_GetCurrent(void)
  260. {
  261. return vTaskGetCurrent();
  262. }
  263. void Task_DelayTick(uint64_t Tick)
  264. {
  265. if (OS_CheckInIrq() || Tick < (SYS_TIMER_1US*5))
  266. {
  267. SysTickDelay((uint32_t)Tick);
  268. return;
  269. }
  270. HANDLE TaskHandle = vTaskGetCurrent();
  271. #ifdef __USE_SEMAPHORE__
  272. SemaphoreHandle_t sem = (SemaphoreHandle_t)vTaskGetPoint(TaskHandle, TASK_POINT_DELAY_SEM);
  273. #endif
  274. Timer_t *Timer = (Timer_t *)vTaskGetPoint(TaskHandle, TASK_POINT_DELAY_TIMER);
  275. Timer_Start(Timer, Tick, 0);
  276. #ifdef __USE_SEMAPHORE__
  277. OS_MutexLock(sem);
  278. #endif
  279. #ifdef __USE_FREERTOS_NOTIFY__
  280. xTaskGenericNotifyWait(TASK_NOTIFY_DELAY, 0, 0, NULL, portMAX_DELAY);
  281. #endif
  282. }
  283. void Task_DelayUS(uint32_t US)
  284. {
  285. uint64_t ToTick = US;
  286. ToTick *= SYS_TIMER_1US;
  287. Task_DelayTick(ToTick);
  288. }
  289. void Task_DelayMS(uint32_t MS)
  290. {
  291. uint64_t ToTick = MS;
  292. ToTick *= SYS_TIMER_1MS;
  293. Task_DelayTick(ToTick);
  294. }
  295. #endif