Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rt_Task.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*----------------------------------------------------------------------------
  2. * RL-ARM - RTX
  3. *----------------------------------------------------------------------------
  4. * Name: RT_TASK.C
  5. * Purpose: Task functions and system start up.
  6. * Rev.: V4.60
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (c) 1999-2009 KEIL, 2009-2012 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. #ifdef __CORTEX_A9
  42. #include "rt_HAL_CA.h"
  43. #else
  44. #include "rt_HAL_CM.h"
  45. #endif
  46. /*----------------------------------------------------------------------------
  47. * Global Variables
  48. *---------------------------------------------------------------------------*/
  49. /* Running and next task info. */
  50. struct OS_TSK os_tsk;
  51. /* Task Control Blocks of idle demon */
  52. struct OS_TCB os_idle_TCB;
  53. /*----------------------------------------------------------------------------
  54. * Local Functions
  55. *---------------------------------------------------------------------------*/
  56. static OS_TID rt_get_TID (void) {
  57. U32 tid;
  58. for (tid = 1; tid <= os_maxtaskrun; tid++) {
  59. if (os_active_TCB[tid-1] == NULL) {
  60. return ((OS_TID)tid);
  61. }
  62. }
  63. return (0);
  64. }
  65. /*--------------------------- rt_init_context -------------------------------*/
  66. static void rt_init_context (P_TCB p_TCB, U8 priority, FUNCP task_body) {
  67. /* Initialize general part of the Task Control Block. */
  68. p_TCB->cb_type = TCB;
  69. p_TCB->state = READY;
  70. p_TCB->prio = priority;
  71. p_TCB->p_lnk = NULL;
  72. p_TCB->p_rlnk = NULL;
  73. p_TCB->p_dlnk = NULL;
  74. p_TCB->p_blnk = NULL;
  75. p_TCB->delta_time = 0;
  76. p_TCB->interval_time = 0;
  77. p_TCB->events = 0;
  78. p_TCB->waits = 0;
  79. p_TCB->stack_frame = 0;
  80. if (p_TCB->priv_stack == 0) {
  81. /* Allocate the memory space for the stack. */
  82. p_TCB->stack = rt_alloc_box (mp_stk);
  83. }
  84. rt_init_stack (p_TCB, task_body);
  85. }
  86. /*--------------------------- rt_switch_req ---------------------------------*/
  87. void rt_switch_req (P_TCB p_new) {
  88. /* Switch to next task (identified by "p_new"). */
  89. os_tsk.new = p_new;
  90. p_new->state = RUNNING;
  91. DBG_TASK_SWITCH(p_new->task_id);
  92. }
  93. /*--------------------------- rt_dispatch -----------------------------------*/
  94. void rt_dispatch (P_TCB next_TCB) {
  95. /* Dispatch next task if any identified or dispatch highest ready task */
  96. /* "next_TCB" identifies a task to run or has value NULL (=no next task) */
  97. if (next_TCB == NULL) {
  98. /* Running task was blocked: continue with highest ready task */
  99. next_TCB = rt_get_first (&os_rdy);
  100. rt_switch_req (next_TCB);
  101. }
  102. else {
  103. /* Check which task continues */
  104. if (next_TCB->prio > os_tsk.run->prio) {
  105. /* preempt running task */
  106. rt_put_rdy_first (os_tsk.run);
  107. os_tsk.run->state = READY;
  108. rt_switch_req (next_TCB);
  109. }
  110. else {
  111. /* put next task into ready list, no task switch takes place */
  112. next_TCB->state = READY;
  113. rt_put_prio (&os_rdy, next_TCB);
  114. }
  115. }
  116. }
  117. /*--------------------------- rt_block --------------------------------------*/
  118. void rt_block (U16 timeout, U8 block_state) {
  119. /* Block running task and choose next ready task. */
  120. /* "timeout" sets a time-out value or is 0xffff (=no time-out). */
  121. /* "block_state" defines the appropriate task state */
  122. P_TCB next_TCB;
  123. if (timeout) {
  124. if (timeout < 0xffff) {
  125. rt_put_dly (os_tsk.run, timeout);
  126. }
  127. os_tsk.run->state = block_state;
  128. next_TCB = rt_get_first (&os_rdy);
  129. rt_switch_req (next_TCB);
  130. }
  131. }
  132. /*--------------------------- rt_tsk_pass -----------------------------------*/
  133. void rt_tsk_pass (void) {
  134. /* Allow tasks of same priority level to run cooperatively.*/
  135. P_TCB p_new;
  136. p_new = rt_get_same_rdy_prio();
  137. if (p_new != NULL) {
  138. rt_put_prio ((P_XCB)&os_rdy, os_tsk.run);
  139. os_tsk.run->state = READY;
  140. rt_switch_req (p_new);
  141. }
  142. }
  143. /*--------------------------- rt_tsk_self -----------------------------------*/
  144. OS_TID rt_tsk_self (void) {
  145. /* Return own task identifier value. */
  146. if (os_tsk.run == NULL) {
  147. return (0);
  148. }
  149. return (os_tsk.run->task_id);
  150. }
  151. /*--------------------------- rt_tsk_prio -----------------------------------*/
  152. OS_RESULT rt_tsk_prio (OS_TID task_id, U8 new_prio) {
  153. /* Change execution priority of a task to "new_prio". */
  154. P_TCB p_task;
  155. if (task_id == 0) {
  156. /* Change execution priority of calling task. */
  157. os_tsk.run->prio = new_prio;
  158. run:if (rt_rdy_prio() > new_prio) {
  159. rt_put_prio (&os_rdy, os_tsk.run);
  160. os_tsk.run->state = READY;
  161. rt_dispatch (NULL);
  162. }
  163. return (OS_R_OK);
  164. }
  165. /* Find the task in the "os_active_TCB" array. */
  166. if (task_id > os_maxtaskrun || os_active_TCB[task_id-1] == NULL) {
  167. /* Task with "task_id" not found or not started. */
  168. return (OS_R_NOK);
  169. }
  170. p_task = os_active_TCB[task_id-1];
  171. p_task->prio = 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. if (task_id == 0 || task_id == os_tsk.run->task_id) {
  216. /* Terminate itself. */
  217. os_tsk.run->state = INACTIVE;
  218. os_tsk.run->tsk_stack = rt_get_PSP ();
  219. rt_stk_check ();
  220. os_active_TCB[os_tsk.run->task_id-1] = NULL;
  221. rt_free_box (mp_stk, os_tsk.run->stack);
  222. os_tsk.run->stack = NULL;
  223. DBG_TASK_NOTIFY(os_tsk.run, __FALSE);
  224. rt_free_box (mp_tcb, os_tsk.run);
  225. os_tsk.run = NULL;
  226. rt_dispatch (NULL);
  227. /* The program should never come to this point. */
  228. }
  229. else {
  230. /* Find the task in the "os_active_TCB" array. */
  231. if (task_id > os_maxtaskrun || os_active_TCB[task_id-1] == NULL) {
  232. /* Task with "task_id" not found or not started. */
  233. return (OS_R_NOK);
  234. }
  235. task_context = os_active_TCB[task_id-1];
  236. rt_rmv_list (task_context);
  237. rt_rmv_dly (task_context);
  238. os_active_TCB[task_id-1] = NULL;
  239. rt_free_box (mp_stk, task_context->stack);
  240. task_context->stack = NULL;
  241. DBG_TASK_NOTIFY(task_context, __FALSE);
  242. rt_free_box (mp_tcb, task_context);
  243. }
  244. return (OS_R_OK);
  245. }
  246. /*--------------------------- rt_sys_init -----------------------------------*/
  247. #ifdef __CMSIS_RTOS
  248. void rt_sys_init (void) {
  249. #else
  250. void rt_sys_init (FUNCP first_task, U32 prio_stksz, void *stk) {
  251. #endif
  252. /* Initialize system and start up task declared with "first_task". */
  253. U32 i;
  254. DBG_INIT();
  255. /* Initialize dynamic memory and task TCB pointers to NULL. */
  256. for (i = 0; i < os_maxtaskrun; i++) {
  257. os_active_TCB[i] = NULL;
  258. }
  259. rt_init_box (&mp_tcb, mp_tcb_size, sizeof(struct OS_TCB));
  260. rt_init_box (&mp_stk, mp_stk_size, BOX_ALIGN_8 | (U16)(os_stackinfo));
  261. rt_init_box ((U32 *)m_tmr, mp_tmr_size, sizeof(struct OS_TMR));
  262. /* Set up TCB of idle demon */
  263. os_idle_TCB.task_id = 255;
  264. os_idle_TCB.priv_stack = 0;
  265. rt_init_context (&os_idle_TCB, 0, os_idle_demon);
  266. /* Set up ready list: initially empty */
  267. os_rdy.cb_type = HCB;
  268. os_rdy.p_lnk = NULL;
  269. /* Set up delay list: initially empty */
  270. os_dly.cb_type = HCB;
  271. os_dly.p_dlnk = NULL;
  272. os_dly.p_blnk = NULL;
  273. os_dly.delta_time = 0;
  274. /* Fix SP and system variables to assume idle task is running */
  275. /* Transform main program into idle task by assuming idle TCB */
  276. #ifndef __CMSIS_RTOS
  277. rt_set_PSP (os_idle_TCB.tsk_stack+32);
  278. #endif
  279. os_tsk.run = &os_idle_TCB;
  280. os_tsk.run->state = RUNNING;
  281. /* Initialize ps queue */
  282. os_psq->first = 0;
  283. os_psq->last = 0;
  284. os_psq->size = os_fifo_size;
  285. rt_init_robin ();
  286. /* Initialize SVC and PendSV */
  287. rt_svc_init ();
  288. #ifndef __CMSIS_RTOS
  289. /* Initialize and start system clock timer */
  290. os_tick_irqn = os_tick_init ();
  291. if (os_tick_irqn >= 0) {
  292. OS_X_INIT(os_tick_irqn);
  293. }
  294. /* Start up first user task before entering the endless loop */
  295. rt_tsk_create (first_task, prio_stksz, stk, NULL);
  296. #endif
  297. }
  298. /*--------------------------- rt_sys_start ----------------------------------*/
  299. #ifdef __CMSIS_RTOS
  300. void rt_sys_start (void) {
  301. /* Start system */
  302. /* Initialize and start system clock timer */
  303. os_tick_irqn = os_tick_init ();
  304. if (os_tick_irqn >= 0) {
  305. OS_X_INIT(os_tick_irqn);
  306. }
  307. }
  308. #endif
  309. /*----------------------------------------------------------------------------
  310. * end of file
  311. *---------------------------------------------------------------------------*/