rt_Task.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*----------------------------------------------------------------------------
  2. * CMSIS-RTOS - RTX
  3. *----------------------------------------------------------------------------
  4. * Name: RT_TASK.C
  5. * Purpose: Task functions and system start up.
  6. * Rev.: V4.80
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (c) 1999-2009 KEIL, 2009-2017 ARM Germany GmbH. All rights reserved.
  10. *
  11. * SPDX-License-Identifier: Apache-2.0
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the License); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *---------------------------------------------------------------------------*/
  25. #include "rt_TypeDef.h"
  26. #include "RTX_Config.h"
  27. #include "rt_System.h"
  28. #include "rt_Task.h"
  29. #include "rt_List.h"
  30. #include "rt_MemBox.h"
  31. #include "rt_Robin.h"
  32. #include "rt_HAL_CM.h"
  33. /*----------------------------------------------------------------------------
  34. * Global Variables
  35. *---------------------------------------------------------------------------*/
  36. /* Running and next task info. */
  37. struct OS_TSK os_tsk;
  38. /* Task Control Blocks of idle demon */
  39. struct OS_TCB os_idle_TCB;
  40. /*----------------------------------------------------------------------------
  41. * Local Functions
  42. *---------------------------------------------------------------------------*/
  43. static OS_TID rt_get_TID (void) {
  44. U32 tid;
  45. for (tid = 1U; tid <= os_maxtaskrun; tid++) {
  46. if (os_active_TCB[tid-1U] == NULL) {
  47. return ((OS_TID)tid);
  48. }
  49. }
  50. return (0U);
  51. }
  52. /*--------------------------- rt_init_context -------------------------------*/
  53. static void rt_init_context (P_TCB p_TCB, U8 priority, FUNCP task_body) {
  54. /* Initialize general part of the Task Control Block. */
  55. p_TCB->cb_type = TCB;
  56. p_TCB->state = READY;
  57. p_TCB->prio = priority;
  58. p_TCB->prio_base = priority;
  59. p_TCB->p_lnk = NULL;
  60. p_TCB->p_rlnk = NULL;
  61. p_TCB->p_dlnk = NULL;
  62. p_TCB->p_blnk = NULL;
  63. p_TCB->p_mlnk = NULL;
  64. p_TCB->delta_time = 0U;
  65. p_TCB->interval_time = 0U;
  66. p_TCB->events = 0U;
  67. p_TCB->waits = 0U;
  68. p_TCB->stack_frame = 0U;
  69. if (p_TCB->priv_stack == 0U) {
  70. /* Allocate the memory space for the stack. */
  71. p_TCB->stack = rt_alloc_box (mp_stk);
  72. }
  73. rt_init_stack (p_TCB, task_body);
  74. }
  75. /*--------------------------- rt_switch_req ---------------------------------*/
  76. void rt_switch_req (P_TCB p_next) {
  77. /* Switch to next task (identified by "p_next"). */
  78. os_tsk.next = p_next;
  79. p_next->state = RUNNING;
  80. DBG_TASK_SWITCH(p_next->task_id);
  81. }
  82. /*--------------------------- rt_dispatch -----------------------------------*/
  83. void rt_dispatch (P_TCB next_TCB) {
  84. /* Dispatch next task if any identified or dispatch highest ready task */
  85. /* "next_TCB" identifies a task to run or has value NULL (=no next task) */
  86. if (next_TCB == NULL) {
  87. /* Running task was blocked: continue with highest ready task */
  88. next_TCB = rt_get_first (&os_rdy);
  89. rt_switch_req (next_TCB);
  90. }
  91. else {
  92. /* Check which task continues */
  93. if (next_TCB->prio > os_tsk.run->prio) {
  94. /* preempt running task */
  95. rt_put_rdy_first (os_tsk.run);
  96. os_tsk.run->state = READY;
  97. rt_switch_req (next_TCB);
  98. }
  99. else {
  100. /* put next task into ready list, no task switch takes place */
  101. next_TCB->state = READY;
  102. rt_put_prio (&os_rdy, next_TCB);
  103. }
  104. }
  105. }
  106. /*--------------------------- rt_block --------------------------------------*/
  107. void rt_block (U16 timeout, U8 block_state) {
  108. /* Block running task and choose next ready task. */
  109. /* "timeout" sets a time-out value or is 0xffff (=no time-out). */
  110. /* "block_state" defines the appropriate task state */
  111. P_TCB next_TCB;
  112. if (timeout) {
  113. if (timeout < 0xFFFFU) {
  114. rt_put_dly (os_tsk.run, timeout);
  115. }
  116. os_tsk.run->state = block_state;
  117. next_TCB = rt_get_first (&os_rdy);
  118. rt_switch_req (next_TCB);
  119. }
  120. }
  121. /*--------------------------- rt_tsk_pass -----------------------------------*/
  122. void rt_tsk_pass (void) {
  123. /* Allow tasks of same priority level to run cooperatively.*/
  124. P_TCB p_new;
  125. p_new = rt_get_same_rdy_prio();
  126. if (p_new != NULL) {
  127. rt_put_prio ((P_XCB)&os_rdy, os_tsk.run);
  128. os_tsk.run->state = READY;
  129. rt_switch_req (p_new);
  130. }
  131. }
  132. /*--------------------------- rt_tsk_self -----------------------------------*/
  133. OS_TID rt_tsk_self (void) {
  134. /* Return own task identifier value. */
  135. if (os_tsk.run == NULL) {
  136. return (0U);
  137. }
  138. return ((OS_TID)os_tsk.run->task_id);
  139. }
  140. /*--------------------------- rt_tsk_prio -----------------------------------*/
  141. OS_RESULT rt_tsk_prio (OS_TID task_id, U8 new_prio) {
  142. /* Change execution priority of a task to "new_prio". */
  143. P_TCB p_task;
  144. if (task_id == 0U) {
  145. /* Change execution priority of calling task. */
  146. os_tsk.run->prio = new_prio;
  147. os_tsk.run->prio_base = new_prio;
  148. run:if (rt_rdy_prio() > new_prio) {
  149. rt_put_prio (&os_rdy, os_tsk.run);
  150. os_tsk.run->state = READY;
  151. rt_dispatch (NULL);
  152. }
  153. return (OS_R_OK);
  154. }
  155. /* Find the task in the "os_active_TCB" array. */
  156. if ((task_id > os_maxtaskrun) || (os_active_TCB[task_id-1U] == NULL)) {
  157. /* Task with "task_id" not found or not started. */
  158. return (OS_R_NOK);
  159. }
  160. p_task = os_active_TCB[task_id-1U];
  161. p_task->prio = new_prio;
  162. p_task->prio_base = new_prio;
  163. if (p_task == os_tsk.run) {
  164. goto run;
  165. }
  166. rt_resort_prio (p_task);
  167. if (p_task->state == READY) {
  168. /* Task enqueued in a ready list. */
  169. p_task = rt_get_first (&os_rdy);
  170. rt_dispatch (p_task);
  171. }
  172. return (OS_R_OK);
  173. }
  174. /*--------------------------- rt_tsk_create ---------------------------------*/
  175. OS_TID rt_tsk_create (FUNCP task, U32 prio_stksz, void *stk, void *argv) {
  176. /* Start a new task declared with "task". */
  177. P_TCB task_context;
  178. U32 i;
  179. /* Priority 0 is reserved for idle task! */
  180. if ((prio_stksz & 0xFFU) == 0U) {
  181. prio_stksz += 1U;
  182. }
  183. task_context = rt_alloc_box (mp_tcb);
  184. if (task_context == NULL) {
  185. return (0U);
  186. }
  187. /* If "size != 0" use a private user provided stack. */
  188. task_context->stack = stk;
  189. task_context->priv_stack = (U16)(prio_stksz >> 8);
  190. /* Pass parameter 'argv' to 'rt_init_context' */
  191. task_context->msg = argv;
  192. /* For 'size == 0' system allocates the user stack from the memory pool. */
  193. rt_init_context (task_context, (U8)(prio_stksz & 0xFFU), task);
  194. /* Find a free entry in 'os_active_TCB' table. */
  195. i = rt_get_TID ();
  196. if (i == 0U) {
  197. return (0U);
  198. }
  199. os_active_TCB[i-1U] = task_context;
  200. task_context->task_id = (U8)i;
  201. DBG_TASK_NOTIFY(task_context, __TRUE);
  202. rt_dispatch (task_context);
  203. return ((OS_TID)i);
  204. }
  205. /*--------------------------- rt_tsk_delete ---------------------------------*/
  206. OS_RESULT rt_tsk_delete (OS_TID task_id) {
  207. /* Terminate the task identified with "task_id". */
  208. P_TCB task_context;
  209. P_TCB p_TCB;
  210. P_MUCB p_MCB, p_MCB0;
  211. if ((task_id == 0U) || (task_id == os_tsk.run->task_id)) {
  212. /* Terminate itself. */
  213. os_tsk.run->state = INACTIVE;
  214. os_tsk.run->tsk_stack = rt_get_PSP ();
  215. rt_stk_check ();
  216. p_MCB = os_tsk.run->p_mlnk;
  217. while (p_MCB) {
  218. /* Release mutexes owned by this task */
  219. if (p_MCB->p_lnk) {
  220. /* A task is waiting for mutex. */
  221. p_TCB = rt_get_first ((P_XCB)p_MCB);
  222. #ifdef __CMSIS_RTOS
  223. rt_ret_val (p_TCB, 0U/*osOK*/);
  224. #else
  225. rt_ret_val (p_TCB, OS_R_MUT);
  226. #endif
  227. rt_rmv_dly (p_TCB);
  228. p_TCB->state = READY;
  229. rt_put_prio (&os_rdy, p_TCB);
  230. /* A waiting task becomes the owner of this mutex. */
  231. p_MCB0 = p_MCB->p_mlnk;
  232. p_MCB->level = 1U;
  233. p_MCB->owner = p_TCB;
  234. p_MCB->p_mlnk = p_TCB->p_mlnk;
  235. p_TCB->p_mlnk = p_MCB;
  236. p_MCB = p_MCB0;
  237. }
  238. else {
  239. p_MCB0 = p_MCB->p_mlnk;
  240. p_MCB->level = 0U;
  241. p_MCB->owner = NULL;
  242. p_MCB->p_mlnk = NULL;
  243. p_MCB = p_MCB0;
  244. }
  245. }
  246. os_active_TCB[os_tsk.run->task_id-1U] = NULL;
  247. rt_free_box (mp_stk, os_tsk.run->stack);
  248. os_tsk.run->stack = NULL;
  249. DBG_TASK_NOTIFY(os_tsk.run, __FALSE);
  250. rt_free_box (mp_tcb, os_tsk.run);
  251. os_tsk.run = NULL;
  252. rt_dispatch (NULL);
  253. /* The program should never come to this point. */
  254. }
  255. else {
  256. /* Find the task in the "os_active_TCB" array. */
  257. if ((task_id > os_maxtaskrun) || (os_active_TCB[task_id-1U] == NULL)) {
  258. /* Task with "task_id" not found or not started. */
  259. return (OS_R_NOK);
  260. }
  261. task_context = os_active_TCB[task_id-1U];
  262. rt_rmv_list (task_context);
  263. rt_rmv_dly (task_context);
  264. p_MCB = task_context->p_mlnk;
  265. while (p_MCB) {
  266. /* Release mutexes owned by this task */
  267. if (p_MCB->p_lnk) {
  268. /* A task is waiting for mutex. */
  269. p_TCB = rt_get_first ((P_XCB)p_MCB);
  270. #ifdef __CMSIS_RTOS
  271. rt_ret_val (p_TCB, 0U/*osOK*/);
  272. #else
  273. rt_ret_val (p_TCB, OS_R_MUT);
  274. #endif
  275. rt_rmv_dly (p_TCB);
  276. p_TCB->state = READY;
  277. rt_put_prio (&os_rdy, p_TCB);
  278. /* A waiting task becomes the owner of this mutex. */
  279. p_MCB0 = p_MCB->p_mlnk;
  280. p_MCB->level = 1U;
  281. p_MCB->owner = p_TCB;
  282. p_MCB->p_mlnk = p_TCB->p_mlnk;
  283. p_TCB->p_mlnk = p_MCB;
  284. p_MCB = p_MCB0;
  285. }
  286. else {
  287. p_MCB0 = p_MCB->p_mlnk;
  288. p_MCB->level = 0U;
  289. p_MCB->owner = NULL;
  290. p_MCB->p_mlnk = NULL;
  291. p_MCB = p_MCB0;
  292. }
  293. }
  294. os_active_TCB[task_id-1U] = NULL;
  295. rt_free_box (mp_stk, task_context->stack);
  296. task_context->stack = NULL;
  297. DBG_TASK_NOTIFY(task_context, __FALSE);
  298. rt_free_box (mp_tcb, task_context);
  299. if (rt_rdy_prio() > os_tsk.run->prio) {
  300. /* Ready task has higher priority than running task. */
  301. os_tsk.run->state = READY;
  302. rt_put_prio (&os_rdy, os_tsk.run);
  303. rt_dispatch (NULL);
  304. }
  305. }
  306. return (OS_R_OK);
  307. }
  308. /*--------------------------- rt_sys_init -----------------------------------*/
  309. #ifdef __CMSIS_RTOS
  310. void rt_sys_init (void) {
  311. #else
  312. void rt_sys_init (FUNCP first_task, U32 prio_stksz, void *stk) {
  313. #endif
  314. /* Initialize system and start up task declared with "first_task". */
  315. U32 i;
  316. DBG_INIT();
  317. /* Initialize dynamic memory and task TCB pointers to NULL. */
  318. for (i = 0U; i < os_maxtaskrun; i++) {
  319. os_active_TCB[i] = NULL;
  320. }
  321. rt_init_box (mp_tcb, (U32)mp_tcb_size, sizeof(struct OS_TCB));
  322. rt_init_box (mp_stk, mp_stk_size, BOX_ALIGN_8 | (U16)(os_stackinfo));
  323. rt_init_box ((U32 *)m_tmr, (U32)mp_tmr_size, sizeof(struct OS_TMR));
  324. /* Set up TCB of idle demon */
  325. os_idle_TCB.task_id = 255U;
  326. os_idle_TCB.priv_stack = 0U;
  327. rt_init_context (&os_idle_TCB, 0U, os_idle_demon);
  328. /* Set up ready list: initially empty */
  329. os_rdy.cb_type = HCB;
  330. os_rdy.p_lnk = NULL;
  331. /* Set up delay list: initially empty */
  332. os_dly.cb_type = HCB;
  333. os_dly.p_dlnk = NULL;
  334. os_dly.p_blnk = NULL;
  335. os_dly.delta_time = 0U;
  336. /* Fix SP and system variables to assume idle task is running */
  337. /* Transform main program into idle task by assuming idle TCB */
  338. #ifndef __CMSIS_RTOS
  339. rt_set_PSP (os_idle_TCB.tsk_stack+32U);
  340. #endif
  341. os_tsk.run = &os_idle_TCB;
  342. os_tsk.run->state = RUNNING;
  343. /* Initialize ps queue */
  344. os_psq->first = 0U;
  345. os_psq->last = 0U;
  346. os_psq->size = os_fifo_size;
  347. rt_init_robin ();
  348. #ifndef __CMSIS_RTOS
  349. /* Initialize SVC and PendSV */
  350. rt_svc_init ();
  351. /* Initialize and start system clock timer */
  352. os_tick_irqn = os_tick_init ();
  353. if (os_tick_irqn >= 0) {
  354. OS_X_INIT((U32)os_tick_irqn);
  355. }
  356. /* Start up first user task before entering the endless loop */
  357. rt_tsk_create (first_task, prio_stksz, stk, NULL);
  358. #endif
  359. }
  360. /*--------------------------- rt_sys_start ----------------------------------*/
  361. #ifdef __CMSIS_RTOS
  362. void rt_sys_start (void) {
  363. /* Start system */
  364. /* Initialize SVC and PendSV */
  365. rt_svc_init ();
  366. /* Initialize and start system clock timer */
  367. os_tick_irqn = os_tick_init ();
  368. if (os_tick_irqn >= 0) {
  369. OS_X_INIT((U32)os_tick_irqn);
  370. }
  371. }
  372. #endif
  373. /*----------------------------------------------------------------------------
  374. * end of file
  375. *---------------------------------------------------------------------------*/