rt_Task.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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-2015 ARM Germany GmbH
  10. * All rights reserved.
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. * - Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * - Neither the name of ARM nor the names of its contributors may be used
  19. * to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *---------------------------------------------------------------------------*/
  34. #include "rt_TypeDef.h"
  35. #include "RTX_Config.h"
  36. #include "rt_System.h"
  37. #include "rt_Task.h"
  38. #include "rt_List.h"
  39. #include "rt_MemBox.h"
  40. #include "rt_Robin.h"
  41. #include "rt_HAL_CM.h"
  42. /*----------------------------------------------------------------------------
  43. * Global Variables
  44. *---------------------------------------------------------------------------*/
  45. /* Running and next task info. */
  46. struct OS_TSK os_tsk;
  47. /* Task Control Blocks of idle demon */
  48. struct OS_TCB os_idle_TCB;
  49. /*----------------------------------------------------------------------------
  50. * Local Functions
  51. *---------------------------------------------------------------------------*/
  52. static OS_TID rt_get_TID (void) {
  53. U32 tid;
  54. for (tid = 1U; tid <= os_maxtaskrun; tid++) {
  55. if (os_active_TCB[tid-1U] == NULL) {
  56. return ((OS_TID)tid);
  57. }
  58. }
  59. return (0U);
  60. }
  61. /*--------------------------- rt_init_context -------------------------------*/
  62. static void rt_init_context (P_TCB p_TCB, U8 priority, FUNCP task_body) {
  63. /* Initialize general part of the Task Control Block. */
  64. p_TCB->cb_type = TCB;
  65. p_TCB->state = READY;
  66. p_TCB->prio = priority;
  67. p_TCB->prio_base = priority;
  68. p_TCB->p_lnk = NULL;
  69. p_TCB->p_rlnk = NULL;
  70. p_TCB->p_dlnk = NULL;
  71. p_TCB->p_blnk = NULL;
  72. p_TCB->p_mlnk = NULL;
  73. p_TCB->delta_time = 0U;
  74. p_TCB->interval_time = 0U;
  75. p_TCB->events = 0U;
  76. p_TCB->waits = 0U;
  77. p_TCB->stack_frame = 0U;
  78. if (p_TCB->priv_stack == 0U) {
  79. /* Allocate the memory space for the stack. */
  80. p_TCB->stack = rt_alloc_box (mp_stk);
  81. }
  82. rt_init_stack (p_TCB, task_body);
  83. }
  84. /*--------------------------- rt_switch_req ---------------------------------*/
  85. void rt_switch_req (P_TCB p_new) {
  86. /* Switch to next task (identified by "p_new"). */
  87. os_tsk.new = p_new;
  88. p_new->state = RUNNING;
  89. DBG_TASK_SWITCH(p_new->task_id);
  90. }
  91. /*--------------------------- rt_dispatch -----------------------------------*/
  92. void rt_dispatch (P_TCB next_TCB) {
  93. /* Dispatch next task if any identified or dispatch highest ready task */
  94. /* "next_TCB" identifies a task to run or has value NULL (=no next task) */
  95. if (next_TCB == NULL) {
  96. /* Running task was blocked: continue with highest ready task */
  97. next_TCB = rt_get_first (&os_rdy);
  98. rt_switch_req (next_TCB);
  99. }
  100. else {
  101. /* Check which task continues */
  102. if (next_TCB->prio > os_tsk.run->prio) {
  103. /* preempt running task */
  104. rt_put_rdy_first (os_tsk.run);
  105. os_tsk.run->state = READY;
  106. rt_switch_req (next_TCB);
  107. }
  108. else {
  109. /* put next task into ready list, no task switch takes place */
  110. next_TCB->state = READY;
  111. rt_put_prio (&os_rdy, next_TCB);
  112. }
  113. }
  114. }
  115. /*--------------------------- rt_block --------------------------------------*/
  116. void rt_block (U16 timeout, U8 block_state) {
  117. /* Block running task and choose next ready task. */
  118. /* "timeout" sets a time-out value or is 0xffff (=no time-out). */
  119. /* "block_state" defines the appropriate task state */
  120. P_TCB next_TCB;
  121. if (timeout) {
  122. if (timeout < 0xFFFFU) {
  123. rt_put_dly (os_tsk.run, timeout);
  124. }
  125. os_tsk.run->state = block_state;
  126. next_TCB = rt_get_first (&os_rdy);
  127. rt_switch_req (next_TCB);
  128. }
  129. }
  130. /*--------------------------- rt_tsk_pass -----------------------------------*/
  131. void rt_tsk_pass (void) {
  132. /* Allow tasks of same priority level to run cooperatively.*/
  133. P_TCB p_new;
  134. p_new = rt_get_same_rdy_prio();
  135. if (p_new != NULL) {
  136. rt_put_prio ((P_XCB)&os_rdy, os_tsk.run);
  137. os_tsk.run->state = READY;
  138. rt_switch_req (p_new);
  139. }
  140. }
  141. /*--------------------------- rt_tsk_self -----------------------------------*/
  142. OS_TID rt_tsk_self (void) {
  143. /* Return own task identifier value. */
  144. if (os_tsk.run == NULL) {
  145. return (0U);
  146. }
  147. return ((OS_TID)os_tsk.run->task_id);
  148. }
  149. /*--------------------------- rt_tsk_prio -----------------------------------*/
  150. OS_RESULT rt_tsk_prio (OS_TID task_id, U8 new_prio) {
  151. /* Change execution priority of a task to "new_prio". */
  152. P_TCB p_task;
  153. if (task_id == 0U) {
  154. /* Change execution priority of calling task. */
  155. os_tsk.run->prio = new_prio;
  156. os_tsk.run->prio_base = new_prio;
  157. run:if (rt_rdy_prio() > new_prio) {
  158. rt_put_prio (&os_rdy, os_tsk.run);
  159. os_tsk.run->state = READY;
  160. rt_dispatch (NULL);
  161. }
  162. return (OS_R_OK);
  163. }
  164. /* Find the task in the "os_active_TCB" array. */
  165. if ((task_id > os_maxtaskrun) || (os_active_TCB[task_id-1U] == NULL)) {
  166. /* Task with "task_id" not found or not started. */
  167. return (OS_R_NOK);
  168. }
  169. p_task = os_active_TCB[task_id-1U];
  170. p_task->prio = new_prio;
  171. p_task->prio_base = new_prio;
  172. if (p_task == os_tsk.run) {
  173. goto run;
  174. }
  175. rt_resort_prio (p_task);
  176. if (p_task->state == READY) {
  177. /* Task enqueued in a ready list. */
  178. p_task = rt_get_first (&os_rdy);
  179. rt_dispatch (p_task);
  180. }
  181. return (OS_R_OK);
  182. }
  183. /*--------------------------- rt_tsk_create ---------------------------------*/
  184. OS_TID rt_tsk_create (FUNCP task, U32 prio_stksz, void *stk, void *argv) {
  185. /* Start a new task declared with "task". */
  186. P_TCB task_context;
  187. U32 i;
  188. /* Priority 0 is reserved for idle task! */
  189. if ((prio_stksz & 0xFFU) == 0U) {
  190. prio_stksz += 1U;
  191. }
  192. task_context = rt_alloc_box (mp_tcb);
  193. if (task_context == NULL) {
  194. return (0U);
  195. }
  196. /* If "size != 0" use a private user provided stack. */
  197. task_context->stack = stk;
  198. task_context->priv_stack = (U16)(prio_stksz >> 8);
  199. /* Pass parameter 'argv' to 'rt_init_context' */
  200. task_context->msg = argv;
  201. /* For 'size == 0' system allocates the user stack from the memory pool. */
  202. rt_init_context (task_context, (U8)(prio_stksz & 0xFFU), task);
  203. /* Find a free entry in 'os_active_TCB' table. */
  204. i = rt_get_TID ();
  205. if (i == 0U) {
  206. return (0U);
  207. }
  208. os_active_TCB[i-1U] = task_context;
  209. task_context->task_id = (U8)i;
  210. DBG_TASK_NOTIFY(task_context, __TRUE);
  211. rt_dispatch (task_context);
  212. return ((OS_TID)i);
  213. }
  214. /*--------------------------- rt_tsk_delete ---------------------------------*/
  215. OS_RESULT rt_tsk_delete (OS_TID task_id) {
  216. /* Terminate the task identified with "task_id". */
  217. P_TCB task_context;
  218. P_TCB p_TCB;
  219. P_MUCB p_MCB, p_MCB0;
  220. if ((task_id == 0U) || (task_id == os_tsk.run->task_id)) {
  221. /* Terminate itself. */
  222. os_tsk.run->state = INACTIVE;
  223. os_tsk.run->tsk_stack = rt_get_PSP ();
  224. rt_stk_check ();
  225. p_MCB = os_tsk.run->p_mlnk;
  226. while (p_MCB) {
  227. /* Release mutexes owned by this task */
  228. if (p_MCB->p_lnk) {
  229. /* A task is waiting for mutex. */
  230. p_TCB = rt_get_first ((P_XCB)p_MCB);
  231. #ifdef __CMSIS_RTOS
  232. rt_ret_val (p_TCB, 0U/*osOK*/);
  233. #else
  234. rt_ret_val (p_TCB, OS_R_MUT);
  235. #endif
  236. rt_rmv_dly (p_TCB);
  237. p_TCB->state = READY;
  238. rt_put_prio (&os_rdy, p_TCB);
  239. /* A waiting task becomes the owner of this mutex. */
  240. p_MCB0 = p_MCB->p_mlnk;
  241. p_MCB->level = 1U;
  242. p_MCB->owner = p_TCB;
  243. p_MCB->p_mlnk = p_TCB->p_mlnk;
  244. p_TCB->p_mlnk = p_MCB;
  245. p_MCB = p_MCB0;
  246. }
  247. else {
  248. p_MCB0 = p_MCB->p_mlnk;
  249. p_MCB->level = 0U;
  250. p_MCB->owner = NULL;
  251. p_MCB->p_mlnk = NULL;
  252. p_MCB = p_MCB0;
  253. }
  254. }
  255. os_active_TCB[os_tsk.run->task_id-1U] = NULL;
  256. rt_free_box (mp_stk, os_tsk.run->stack);
  257. os_tsk.run->stack = NULL;
  258. DBG_TASK_NOTIFY(os_tsk.run, __FALSE);
  259. rt_free_box (mp_tcb, os_tsk.run);
  260. os_tsk.run = NULL;
  261. rt_dispatch (NULL);
  262. /* The program should never come to this point. */
  263. }
  264. else {
  265. /* Find the task in the "os_active_TCB" array. */
  266. if ((task_id > os_maxtaskrun) || (os_active_TCB[task_id-1U] == NULL)) {
  267. /* Task with "task_id" not found or not started. */
  268. return (OS_R_NOK);
  269. }
  270. task_context = os_active_TCB[task_id-1U];
  271. rt_rmv_list (task_context);
  272. rt_rmv_dly (task_context);
  273. p_MCB = task_context->p_mlnk;
  274. while (p_MCB) {
  275. /* Release mutexes owned by this task */
  276. if (p_MCB->p_lnk) {
  277. /* A task is waiting for mutex. */
  278. p_TCB = rt_get_first ((P_XCB)p_MCB);
  279. #ifdef __CMSIS_RTOS
  280. rt_ret_val (p_TCB, 0U/*osOK*/);
  281. #else
  282. rt_ret_val (p_TCB, OS_R_MUT);
  283. #endif
  284. rt_rmv_dly (p_TCB);
  285. p_TCB->state = READY;
  286. rt_put_prio (&os_rdy, p_TCB);
  287. /* A waiting task becomes the owner of this mutex. */
  288. p_MCB0 = p_MCB->p_mlnk;
  289. p_MCB->level = 1U;
  290. p_MCB->owner = p_TCB;
  291. p_MCB->p_mlnk = p_TCB->p_mlnk;
  292. p_TCB->p_mlnk = p_MCB;
  293. p_MCB = p_MCB0;
  294. }
  295. else {
  296. p_MCB0 = p_MCB->p_mlnk;
  297. p_MCB->level = 0U;
  298. p_MCB->owner = NULL;
  299. p_MCB->p_mlnk = NULL;
  300. p_MCB = p_MCB0;
  301. }
  302. }
  303. os_active_TCB[task_id-1U] = NULL;
  304. rt_free_box (mp_stk, task_context->stack);
  305. task_context->stack = NULL;
  306. DBG_TASK_NOTIFY(task_context, __FALSE);
  307. rt_free_box (mp_tcb, task_context);
  308. if (rt_rdy_prio() > os_tsk.run->prio) {
  309. /* Ready task has higher priority than running task. */
  310. os_tsk.run->state = READY;
  311. rt_put_prio (&os_rdy, os_tsk.run);
  312. rt_dispatch (NULL);
  313. }
  314. }
  315. return (OS_R_OK);
  316. }
  317. /*--------------------------- rt_sys_init -----------------------------------*/
  318. #ifdef __CMSIS_RTOS
  319. void rt_sys_init (void) {
  320. #else
  321. void rt_sys_init (FUNCP first_task, U32 prio_stksz, void *stk) {
  322. #endif
  323. /* Initialize system and start up task declared with "first_task". */
  324. U32 i;
  325. DBG_INIT();
  326. /* Initialize dynamic memory and task TCB pointers to NULL. */
  327. for (i = 0U; i < os_maxtaskrun; i++) {
  328. os_active_TCB[i] = NULL;
  329. }
  330. rt_init_box (mp_tcb, (U32)mp_tcb_size, sizeof(struct OS_TCB));
  331. rt_init_box (mp_stk, mp_stk_size, BOX_ALIGN_8 | (U16)(os_stackinfo));
  332. rt_init_box ((U32 *)m_tmr, (U32)mp_tmr_size, sizeof(struct OS_TMR));
  333. /* Set up TCB of idle demon */
  334. os_idle_TCB.task_id = 255U;
  335. os_idle_TCB.priv_stack = 0U;
  336. rt_init_context (&os_idle_TCB, 0U, os_idle_demon);
  337. /* Set up ready list: initially empty */
  338. os_rdy.cb_type = HCB;
  339. os_rdy.p_lnk = NULL;
  340. /* Set up delay list: initially empty */
  341. os_dly.cb_type = HCB;
  342. os_dly.p_dlnk = NULL;
  343. os_dly.p_blnk = NULL;
  344. os_dly.delta_time = 0U;
  345. /* Fix SP and system variables to assume idle task is running */
  346. /* Transform main program into idle task by assuming idle TCB */
  347. #ifndef __CMSIS_RTOS
  348. rt_set_PSP (os_idle_TCB.tsk_stack+32U);
  349. #endif
  350. os_tsk.run = &os_idle_TCB;
  351. os_tsk.run->state = RUNNING;
  352. /* Initialize ps queue */
  353. os_psq->first = 0U;
  354. os_psq->last = 0U;
  355. os_psq->size = os_fifo_size;
  356. rt_init_robin ();
  357. #ifndef __CMSIS_RTOS
  358. /* Initialize SVC and PendSV */
  359. rt_svc_init ();
  360. /* Initialize and start system clock timer */
  361. os_tick_irqn = os_tick_init ();
  362. if (os_tick_irqn >= 0) {
  363. OS_X_INIT((U32)os_tick_irqn);
  364. }
  365. /* Start up first user task before entering the endless loop */
  366. rt_tsk_create (first_task, prio_stksz, stk, NULL);
  367. #endif
  368. }
  369. /*--------------------------- rt_sys_start ----------------------------------*/
  370. #ifdef __CMSIS_RTOS
  371. void rt_sys_start (void) {
  372. /* Start system */
  373. /* Initialize SVC and PendSV */
  374. rt_svc_init ();
  375. /* Initialize and start system clock timer */
  376. os_tick_irqn = os_tick_init ();
  377. if (os_tick_irqn >= 0) {
  378. OS_X_INIT((U32)os_tick_irqn);
  379. }
  380. }
  381. #endif
  382. /*----------------------------------------------------------------------------
  383. * end of file
  384. *---------------------------------------------------------------------------*/