rt_Task.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*----------------------------------------------------------------------------
  2. * RL-ARM - RTX
  3. *----------------------------------------------------------------------------
  4. * Name: RT_TASK.C
  5. * Purpose: Task functions and system start up.
  6. * Rev.: V4.73
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (c) 1999-2009 KEIL, 2009-2013 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 = 1; tid <= os_maxtaskrun; tid++) {
  55. if (os_active_TCB[tid-1] == NULL) {
  56. return ((OS_TID)tid);
  57. }
  58. }
  59. return (0);
  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 = 0;
  74. p_TCB->interval_time = 0;
  75. p_TCB->events = 0;
  76. p_TCB->waits = 0;
  77. p_TCB->stack_frame = 0;
  78. if (p_TCB->priv_stack == 0) {
  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 < 0xffff) {
  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 (0);
  146. }
  147. return (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 == 0) {
  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-1] == 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-1];
  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 & 0xFF) == 0) {
  190. prio_stksz += 1;
  191. }
  192. task_context = rt_alloc_box (mp_tcb);
  193. if (task_context == NULL) {
  194. return (0);
  195. }
  196. /* If "size != 0" use a private user provided stack. */
  197. task_context->stack = stk;
  198. task_context->priv_stack = 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, prio_stksz & 0xFF, task);
  203. /* Find a free entry in 'os_active_TCB' table. */
  204. i = rt_get_TID ();
  205. os_active_TCB[i-1] = task_context;
  206. task_context->task_id = i;
  207. DBG_TASK_NOTIFY(task_context, __TRUE);
  208. rt_dispatch (task_context);
  209. return ((OS_TID)i);
  210. }
  211. /*--------------------------- rt_tsk_delete ---------------------------------*/
  212. OS_RESULT rt_tsk_delete (OS_TID task_id) {
  213. /* Terminate the task identified with "task_id". */
  214. P_TCB task_context;
  215. P_TCB p_TCB;
  216. P_MUCB p_MCB, p_MCB0;
  217. if (task_id == 0 || task_id == os_tsk.run->task_id) {
  218. /* Terminate itself. */
  219. os_tsk.run->state = INACTIVE;
  220. os_tsk.run->tsk_stack = rt_get_PSP ();
  221. rt_stk_check ();
  222. p_MCB = os_tsk.run->p_mlnk;
  223. while (p_MCB) {
  224. /* Release mutexes owned by this task */
  225. if (p_MCB->p_lnk) {
  226. /* A task is waiting for mutex. */
  227. p_TCB = rt_get_first ((P_XCB)p_MCB);
  228. #ifdef __CMSIS_RTOS
  229. rt_ret_val(p_TCB, 0/*osOK*/);
  230. #else
  231. rt_ret_val(p_TCB, OS_R_MUT);
  232. #endif
  233. rt_rmv_dly (p_TCB);
  234. p_TCB->state = READY;
  235. rt_put_prio (&os_rdy, p_TCB);
  236. /* A waiting task becomes the owner of this mutex. */
  237. p_MCB0 = p_MCB;
  238. p_MCB->level = 1;
  239. p_MCB->owner = p_TCB;
  240. p_MCB->p_mlnk = p_TCB->p_mlnk;
  241. p_TCB->p_mlnk = p_MCB;
  242. p_MCB = p_MCB0->p_mlnk;
  243. }
  244. else {
  245. p_MCB = p_MCB->p_mlnk;
  246. }
  247. }
  248. os_active_TCB[os_tsk.run->task_id-1] = NULL;
  249. rt_free_box (mp_stk, os_tsk.run->stack);
  250. os_tsk.run->stack = NULL;
  251. DBG_TASK_NOTIFY(os_tsk.run, __FALSE);
  252. rt_free_box (mp_tcb, os_tsk.run);
  253. os_tsk.run = NULL;
  254. rt_dispatch (NULL);
  255. /* The program should never come to this point. */
  256. }
  257. else {
  258. /* Find the task in the "os_active_TCB" array. */
  259. if (task_id > os_maxtaskrun || os_active_TCB[task_id-1] == NULL) {
  260. /* Task with "task_id" not found or not started. */
  261. return (OS_R_NOK);
  262. }
  263. task_context = os_active_TCB[task_id-1];
  264. rt_rmv_list (task_context);
  265. rt_rmv_dly (task_context);
  266. p_MCB = task_context->p_mlnk;
  267. while (p_MCB) {
  268. /* Release mutexes owned by this task */
  269. if (p_MCB->p_lnk) {
  270. /* A task is waiting for mutex. */
  271. p_TCB = rt_get_first ((P_XCB)p_MCB);
  272. #ifdef __CMSIS_RTOS
  273. rt_ret_val(p_TCB, 0/*osOK*/);
  274. #else
  275. rt_ret_val(p_TCB, OS_R_MUT);
  276. #endif
  277. rt_rmv_dly (p_TCB);
  278. p_TCB->state = READY;
  279. rt_put_prio (&os_rdy, p_TCB);
  280. /* A waiting task becomes the owner of this mutex. */
  281. p_MCB0 = p_MCB;
  282. p_MCB->level = 1;
  283. p_MCB->owner = p_TCB;
  284. p_MCB->p_mlnk = p_TCB->p_mlnk;
  285. p_TCB->p_mlnk = p_MCB;
  286. p_MCB = p_MCB0->p_mlnk;
  287. }
  288. else {
  289. p_MCB = p_MCB->p_mlnk;
  290. }
  291. }
  292. os_active_TCB[task_id-1] = NULL;
  293. rt_free_box (mp_stk, task_context->stack);
  294. task_context->stack = NULL;
  295. DBG_TASK_NOTIFY(task_context, __FALSE);
  296. rt_free_box (mp_tcb, task_context);
  297. if (rt_rdy_prio() > os_tsk.run->prio) {
  298. /* Ready task has higher priority than running task. */
  299. os_tsk.run->state = READY;
  300. rt_put_prio (&os_rdy, os_tsk.run);
  301. rt_dispatch (NULL);
  302. }
  303. }
  304. return (OS_R_OK);
  305. }
  306. /*--------------------------- rt_sys_init -----------------------------------*/
  307. #ifdef __CMSIS_RTOS
  308. void rt_sys_init (void) {
  309. #else
  310. void rt_sys_init (FUNCP first_task, U32 prio_stksz, void *stk) {
  311. #endif
  312. /* Initialize system and start up task declared with "first_task". */
  313. U32 i;
  314. DBG_INIT();
  315. /* Initialize dynamic memory and task TCB pointers to NULL. */
  316. for (i = 0; i < os_maxtaskrun; i++) {
  317. os_active_TCB[i] = NULL;
  318. }
  319. rt_init_box (&mp_tcb, mp_tcb_size, sizeof(struct OS_TCB));
  320. rt_init_box (&mp_stk, mp_stk_size, BOX_ALIGN_8 | (U16)(os_stackinfo));
  321. rt_init_box ((U32 *)m_tmr, mp_tmr_size, sizeof(struct OS_TMR));
  322. /* Set up TCB of idle demon */
  323. os_idle_TCB.task_id = 255;
  324. os_idle_TCB.priv_stack = 0;
  325. rt_init_context (&os_idle_TCB, 0, os_idle_demon);
  326. /* Set up ready list: initially empty */
  327. os_rdy.cb_type = HCB;
  328. os_rdy.p_lnk = NULL;
  329. /* Set up delay list: initially empty */
  330. os_dly.cb_type = HCB;
  331. os_dly.p_dlnk = NULL;
  332. os_dly.p_blnk = NULL;
  333. os_dly.delta_time = 0;
  334. /* Fix SP and system variables to assume idle task is running */
  335. /* Transform main program into idle task by assuming idle TCB */
  336. #ifndef __CMSIS_RTOS
  337. rt_set_PSP (os_idle_TCB.tsk_stack+32);
  338. #endif
  339. os_tsk.run = &os_idle_TCB;
  340. os_tsk.run->state = RUNNING;
  341. /* Initialize ps queue */
  342. os_psq->first = 0;
  343. os_psq->last = 0;
  344. os_psq->size = os_fifo_size;
  345. rt_init_robin ();
  346. /* Intitialize SVC and PendSV */
  347. rt_svc_init ();
  348. #ifndef __CMSIS_RTOS
  349. /* Intitialize and start system clock timer */
  350. os_tick_irqn = os_tick_init ();
  351. if (os_tick_irqn >= 0) {
  352. OS_X_INIT(os_tick_irqn);
  353. }
  354. /* Start up first user task before entering the endless loop */
  355. rt_tsk_create (first_task, prio_stksz, stk, NULL);
  356. #endif
  357. }
  358. /*--------------------------- rt_sys_start ----------------------------------*/
  359. #ifdef __CMSIS_RTOS
  360. void rt_sys_start (void) {
  361. /* Start system */
  362. /* Intitialize and start system clock timer */
  363. os_tick_irqn = os_tick_init ();
  364. if (os_tick_irqn >= 0) {
  365. OS_X_INIT(os_tick_irqn);
  366. }
  367. }
  368. #endif
  369. /*----------------------------------------------------------------------------
  370. * end of file
  371. *---------------------------------------------------------------------------*/