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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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_Conf.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. 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. #if defined (__CC_ARM) && !defined (__MICROLIB)
  62. /*--------------------------- __user_perthread_libspace ---------------------*/
  63. extern void *__libspace_start;
  64. void *__user_perthread_libspace (void) {
  65. /* Provide a separate libspace for each task. */
  66. if (os_tsk.run == NULL) {
  67. /* RTX not running yet. */
  68. return (&__libspace_start);
  69. }
  70. return (void *)(os_tsk.run->std_libspace);
  71. }
  72. #endif
  73. /*--------------------------- rt_init_context -------------------------------*/
  74. void rt_init_context (P_TCB p_TCB, U8 priority, FUNCP task_body) {
  75. /* Initialize general part of the Task Control Block. */
  76. p_TCB->cb_type = TCB;
  77. p_TCB->state = READY;
  78. p_TCB->prio = priority;
  79. p_TCB->p_lnk = NULL;
  80. p_TCB->p_rlnk = NULL;
  81. p_TCB->p_dlnk = NULL;
  82. p_TCB->p_blnk = NULL;
  83. p_TCB->delta_time = 0;
  84. p_TCB->interval_time = 0;
  85. p_TCB->events = 0;
  86. p_TCB->waits = 0;
  87. p_TCB->stack_frame = 0;
  88. rt_init_stack (p_TCB, task_body);
  89. }
  90. /*--------------------------- rt_switch_req ---------------------------------*/
  91. void rt_switch_req (P_TCB p_new) {
  92. /* Switch to next task (identified by "p_new"). */
  93. os_tsk.new_tsk = p_new;
  94. p_new->state = RUNNING;
  95. DBG_TASK_SWITCH(p_new->task_id);
  96. }
  97. /*--------------------------- rt_dispatch -----------------------------------*/
  98. void rt_dispatch (P_TCB next_TCB) {
  99. /* Dispatch next task if any identified or dispatch highest ready task */
  100. /* "next_TCB" identifies a task to run or has value NULL (=no next task) */
  101. if (next_TCB == NULL) {
  102. /* Running task was blocked: continue with highest ready task */
  103. next_TCB = rt_get_first (&os_rdy);
  104. rt_switch_req (next_TCB);
  105. }
  106. else {
  107. /* Check which task continues */
  108. if (next_TCB->prio > os_tsk.run->prio) {
  109. /* preempt running task */
  110. rt_put_rdy_first (os_tsk.run);
  111. os_tsk.run->state = READY;
  112. rt_switch_req (next_TCB);
  113. }
  114. else {
  115. /* put next task into ready list, no task switch takes place */
  116. next_TCB->state = READY;
  117. rt_put_prio (&os_rdy, next_TCB);
  118. }
  119. }
  120. }
  121. /*--------------------------- rt_block --------------------------------------*/
  122. void rt_block (U16 timeout, U8 block_state) {
  123. /* Block running task and choose next ready task. */
  124. /* "timeout" sets a time-out value or is 0xffff (=no time-out). */
  125. /* "block_state" defines the appropriate task state */
  126. P_TCB next_TCB;
  127. if (timeout) {
  128. if (timeout < 0xffff) {
  129. rt_put_dly (os_tsk.run, timeout);
  130. }
  131. os_tsk.run->state = block_state;
  132. next_TCB = rt_get_first (&os_rdy);
  133. rt_switch_req (next_TCB);
  134. }
  135. }
  136. /*--------------------------- rt_tsk_pass -----------------------------------*/
  137. void rt_tsk_pass (void) {
  138. /* Allow tasks of same priority level to run cooperatively.*/
  139. P_TCB p_new;
  140. p_new = rt_get_same_rdy_prio();
  141. if (p_new != NULL) {
  142. rt_put_prio ((P_XCB)&os_rdy, os_tsk.run);
  143. os_tsk.run->state = READY;
  144. rt_switch_req (p_new);
  145. }
  146. }
  147. /*--------------------------- rt_tsk_self -----------------------------------*/
  148. OS_TID rt_tsk_self (void) {
  149. /* Return own task identifier value. */
  150. if (os_tsk.run == NULL) {
  151. return (0);
  152. }
  153. return (os_tsk.run->task_id);
  154. }
  155. /*--------------------------- rt_tsk_prio -----------------------------------*/
  156. OS_RESULT rt_tsk_prio (OS_TID task_id, U8 new_prio) {
  157. /* Change execution priority of a task to "new_prio". */
  158. P_TCB p_task;
  159. if (task_id == 0) {
  160. /* Change execution priority of calling task. */
  161. os_tsk.run->prio = new_prio;
  162. run:if (rt_rdy_prio() > new_prio) {
  163. rt_put_prio (&os_rdy, os_tsk.run);
  164. os_tsk.run->state = READY;
  165. rt_dispatch (NULL);
  166. }
  167. return (OS_R_OK);
  168. }
  169. /* Find the task in the "os_active_TCB" array. */
  170. if (task_id > os_maxtaskrun || os_active_TCB[task_id-1] == NULL) {
  171. /* Task with "task_id" not found or not started. */
  172. return (OS_R_NOK);
  173. }
  174. p_task = os_active_TCB[task_id-1];
  175. p_task->prio = new_prio;
  176. if (p_task == os_tsk.run) {
  177. goto run;
  178. }
  179. rt_resort_prio (p_task);
  180. if (p_task->state == READY) {
  181. /* Task enqueued in a ready list. */
  182. p_task = rt_get_first (&os_rdy);
  183. rt_dispatch (p_task);
  184. }
  185. return (OS_R_OK);
  186. }
  187. /*--------------------------- rt_tsk_delete ---------------------------------*/
  188. OS_RESULT rt_tsk_delete (OS_TID task_id) {
  189. /* Terminate the task identified with "task_id". */
  190. P_TCB task_context;
  191. if (task_id == 0 || task_id == os_tsk.run->task_id) {
  192. /* Terminate itself. */
  193. os_tsk.run->state = INACTIVE;
  194. os_tsk.run->tsk_stack = rt_get_PSP ();
  195. rt_stk_check ();
  196. os_active_TCB[os_tsk.run->task_id-1] = NULL;
  197. os_tsk.run->stack = NULL;
  198. DBG_TASK_NOTIFY(os_tsk.run, __FALSE);
  199. os_tsk.run = NULL;
  200. rt_dispatch (NULL);
  201. /* The program should never come to this point. */
  202. }
  203. else {
  204. /* Find the task in the "os_active_TCB" array. */
  205. if (task_id > os_maxtaskrun || os_active_TCB[task_id-1] == NULL) {
  206. /* Task with "task_id" not found or not started. */
  207. return (OS_R_NOK);
  208. }
  209. task_context = os_active_TCB[task_id-1];
  210. rt_rmv_list (task_context);
  211. rt_rmv_dly (task_context);
  212. os_active_TCB[task_id-1] = NULL;
  213. task_context->stack = NULL;
  214. DBG_TASK_NOTIFY(task_context, __FALSE);
  215. }
  216. return (OS_R_OK);
  217. }
  218. /*--------------------------- rt_sys_init -----------------------------------*/
  219. #ifdef __CMSIS_RTOS
  220. void rt_sys_init (void) {
  221. #else
  222. void rt_sys_init (FUNCP first_task, U32 prio_stksz, void *stk) {
  223. #endif
  224. /* Initialize system and start up task declared with "first_task". */
  225. U32 i;
  226. DBG_INIT();
  227. /* Initialize dynamic memory and task TCB pointers to NULL. */
  228. for (i = 0; i < os_maxtaskrun; i++) {
  229. os_active_TCB[i] = NULL;
  230. }
  231. /* Set up TCB of idle demon */
  232. os_idle_TCB.task_id = 255;
  233. os_idle_TCB.priv_stack = idle_task_stack_size;
  234. os_idle_TCB.stack = idle_task_stack;
  235. rt_init_context (&os_idle_TCB, 0, os_idle_demon);
  236. /* Set up ready list: initially empty */
  237. os_rdy.cb_type = HCB;
  238. os_rdy.p_lnk = NULL;
  239. /* Set up delay list: initially empty */
  240. os_dly.cb_type = HCB;
  241. os_dly.p_dlnk = NULL;
  242. os_dly.p_blnk = NULL;
  243. os_dly.delta_time = 0;
  244. /* Fix SP and systemvariables to assume idle task is running */
  245. /* Transform main program into idle task by assuming idle TCB */
  246. #ifndef __CMSIS_RTOS
  247. rt_set_PSP (os_idle_TCB.tsk_stack+32);
  248. #endif
  249. os_tsk.run = &os_idle_TCB;
  250. os_tsk.run->state = RUNNING;
  251. /* Initialize ps queue */
  252. os_psq->first = 0;
  253. os_psq->last = 0;
  254. os_psq->size = os_fifo_size;
  255. rt_init_robin ();
  256. /* Intitialize SVC and PendSV */
  257. rt_svc_init ();
  258. #ifndef __CMSIS_RTOS
  259. /* Intitialize and start system clock timer */
  260. os_tick_irqn = os_tick_init ();
  261. if (os_tick_irqn >= 0) {
  262. OS_X_INIT(os_tick_irqn);
  263. }
  264. /* Start up first user task before entering the endless loop */
  265. rt_tsk_create (first_task, prio_stksz, stk, NULL);
  266. #endif
  267. }
  268. /*--------------------------- rt_sys_start ----------------------------------*/
  269. #ifdef __CMSIS_RTOS
  270. void rt_sys_start (void) {
  271. /* Start system */
  272. /* Intitialize and start system clock timer */
  273. os_tick_irqn = os_tick_init ();
  274. if (os_tick_irqn >= 0) {
  275. OS_X_INIT(os_tick_irqn);
  276. }
  277. }
  278. #endif
  279. /*----------------------------------------------------------------------------
  280. * end of file
  281. *---------------------------------------------------------------------------*/