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_List.c 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*----------------------------------------------------------------------------
  2. * RL-ARM - RTX
  3. *----------------------------------------------------------------------------
  4. * Name: RT_LIST.C
  5. * Purpose: Functions for the management of different lists
  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_List.h"
  38. #include "rt_Task.h"
  39. #include "rt_Time.h"
  40. #ifdef __CORTEX_A9
  41. #include "rt_HAL_CA.h"
  42. #else
  43. #include "rt_HAL_CM.h"
  44. #endif
  45. /*----------------------------------------------------------------------------
  46. * Global Variables
  47. *---------------------------------------------------------------------------*/
  48. /* List head of chained ready tasks */
  49. struct OS_XCB os_rdy;
  50. /* List head of chained delay tasks */
  51. struct OS_XCB os_dly;
  52. /*----------------------------------------------------------------------------
  53. * Functions
  54. *---------------------------------------------------------------------------*/
  55. /*--------------------------- rt_put_prio -----------------------------------*/
  56. void rt_put_prio (P_XCB p_CB, P_TCB p_task) {
  57. /* Put task identified with "p_task" into list ordered by priority. */
  58. /* "p_CB" points to head of list; list has always an element at end with */
  59. /* a priority less than "p_task->prio". */
  60. P_TCB p_CB2;
  61. U32 prio;
  62. BOOL sem_mbx = __FALSE;
  63. if (p_CB->cb_type == SCB || p_CB->cb_type == MCB || p_CB->cb_type == MUCB) {
  64. sem_mbx = __TRUE;
  65. }
  66. prio = p_task->prio;
  67. p_CB2 = p_CB->p_lnk;
  68. /* Search for an entry in the list */
  69. while (p_CB2 != NULL && prio <= p_CB2->prio) {
  70. p_CB = (P_XCB)p_CB2;
  71. p_CB2 = p_CB2->p_lnk;
  72. }
  73. /* Entry found, insert the task into the list */
  74. p_task->p_lnk = p_CB2;
  75. p_CB->p_lnk = p_task;
  76. if (sem_mbx) {
  77. if (p_CB2 != NULL) {
  78. p_CB2->p_rlnk = p_task;
  79. }
  80. p_task->p_rlnk = (P_TCB)p_CB;
  81. }
  82. else {
  83. p_task->p_rlnk = NULL;
  84. }
  85. }
  86. /*--------------------------- rt_get_first ----------------------------------*/
  87. P_TCB rt_get_first (P_XCB p_CB) {
  88. /* Get task at head of list: it is the task with highest priority. */
  89. /* "p_CB" points to head of list. */
  90. P_TCB p_first;
  91. p_first = p_CB->p_lnk;
  92. p_CB->p_lnk = p_first->p_lnk;
  93. if (p_CB->cb_type == SCB || p_CB->cb_type == MCB || p_CB->cb_type == MUCB) {
  94. if (p_first->p_lnk != NULL) {
  95. p_first->p_lnk->p_rlnk = (P_TCB)p_CB;
  96. p_first->p_lnk = NULL;
  97. }
  98. p_first->p_rlnk = NULL;
  99. }
  100. else {
  101. p_first->p_lnk = NULL;
  102. }
  103. return (p_first);
  104. }
  105. /*--------------------------- rt_put_rdy_first ------------------------------*/
  106. void rt_put_rdy_first (P_TCB p_task) {
  107. /* Put task identified with "p_task" at the head of the ready list. The */
  108. /* task must have at least a priority equal to highest priority in list. */
  109. p_task->p_lnk = os_rdy.p_lnk;
  110. p_task->p_rlnk = NULL;
  111. os_rdy.p_lnk = p_task;
  112. }
  113. /*--------------------------- rt_get_same_rdy_prio --------------------------*/
  114. P_TCB rt_get_same_rdy_prio (void) {
  115. /* Remove a task of same priority from ready list if any exists. Other- */
  116. /* wise return NULL. */
  117. P_TCB p_first;
  118. p_first = os_rdy.p_lnk;
  119. if (p_first->prio == os_tsk.run->prio) {
  120. os_rdy.p_lnk = os_rdy.p_lnk->p_lnk;
  121. return (p_first);
  122. }
  123. return (NULL);
  124. }
  125. /*--------------------------- rt_resort_prio --------------------------------*/
  126. void rt_resort_prio (P_TCB p_task) {
  127. /* Re-sort ordered lists after the priority of 'p_task' has changed. */
  128. P_TCB p_CB;
  129. if (p_task->p_rlnk == NULL) {
  130. if (p_task->state == READY) {
  131. /* Task is chained into READY list. */
  132. p_CB = (P_TCB)&os_rdy;
  133. goto res;
  134. }
  135. }
  136. else {
  137. p_CB = p_task->p_rlnk;
  138. while (p_CB->cb_type == TCB) {
  139. /* Find a header of this task chain list. */
  140. p_CB = p_CB->p_rlnk;
  141. }
  142. res:rt_rmv_list (p_task);
  143. rt_put_prio ((P_XCB)p_CB, p_task);
  144. }
  145. }
  146. /*--------------------------- rt_put_dly ------------------------------------*/
  147. void rt_put_dly (P_TCB p_task, U16 delay) {
  148. /* Put a task identified with "p_task" into chained delay wait list using */
  149. /* a delay value of "delay". */
  150. P_TCB p;
  151. U32 delta,idelay = delay;
  152. p = (P_TCB)&os_dly;
  153. if (p->p_dlnk == NULL) {
  154. /* Delay list empty */
  155. delta = 0;
  156. goto last;
  157. }
  158. delta = os_dly.delta_time;
  159. while (delta < idelay) {
  160. if (p->p_dlnk == NULL) {
  161. /* End of list found */
  162. last: p_task->p_dlnk = NULL;
  163. p->p_dlnk = p_task;
  164. p_task->p_blnk = p;
  165. p->delta_time = (U16)(idelay - delta);
  166. p_task->delta_time = 0;
  167. return;
  168. }
  169. p = p->p_dlnk;
  170. delta += p->delta_time;
  171. }
  172. /* Right place found */
  173. p_task->p_dlnk = p->p_dlnk;
  174. p->p_dlnk = p_task;
  175. p_task->p_blnk = p;
  176. if (p_task->p_dlnk != NULL) {
  177. p_task->p_dlnk->p_blnk = p_task;
  178. }
  179. p_task->delta_time = (U16)(delta - idelay);
  180. p->delta_time -= p_task->delta_time;
  181. }
  182. /*--------------------------- rt_dec_dly ------------------------------------*/
  183. void rt_dec_dly (void) {
  184. /* Decrement delta time of list head: remove tasks having a value of zero.*/
  185. P_TCB p_rdy;
  186. if (os_dly.p_dlnk == NULL) {
  187. return;
  188. }
  189. os_dly.delta_time--;
  190. while ((os_dly.delta_time == 0) && (os_dly.p_dlnk != NULL)) {
  191. p_rdy = os_dly.p_dlnk;
  192. if (p_rdy->p_rlnk != NULL) {
  193. /* Task is really enqueued, remove task from semaphore/mailbox */
  194. /* timeout waiting list. */
  195. p_rdy->p_rlnk->p_lnk = p_rdy->p_lnk;
  196. if (p_rdy->p_lnk != NULL) {
  197. p_rdy->p_lnk->p_rlnk = p_rdy->p_rlnk;
  198. p_rdy->p_lnk = NULL;
  199. }
  200. p_rdy->p_rlnk = NULL;
  201. }
  202. rt_put_prio (&os_rdy, p_rdy);
  203. os_dly.delta_time = p_rdy->delta_time;
  204. if (p_rdy->state == WAIT_ITV) {
  205. /* Calculate the next time for interval wait. */
  206. p_rdy->delta_time = p_rdy->interval_time + (U16)os_time;
  207. }
  208. p_rdy->state = READY;
  209. os_dly.p_dlnk = p_rdy->p_dlnk;
  210. if (p_rdy->p_dlnk != NULL) {
  211. p_rdy->p_dlnk->p_blnk = (P_TCB)&os_dly;
  212. p_rdy->p_dlnk = NULL;
  213. }
  214. p_rdy->p_blnk = NULL;
  215. }
  216. }
  217. /*--------------------------- rt_rmv_list -----------------------------------*/
  218. void rt_rmv_list (P_TCB p_task) {
  219. /* Remove task identified with "p_task" from ready, semaphore or mailbox */
  220. /* waiting list if enqueued. */
  221. P_TCB p_b;
  222. if (p_task->p_rlnk != NULL) {
  223. /* A task is enqueued in semaphore / mailbox waiting list. */
  224. p_task->p_rlnk->p_lnk = p_task->p_lnk;
  225. if (p_task->p_lnk != NULL) {
  226. p_task->p_lnk->p_rlnk = p_task->p_rlnk;
  227. }
  228. return;
  229. }
  230. p_b = (P_TCB)&os_rdy;
  231. while (p_b != NULL) {
  232. /* Search the ready list for task "p_task" */
  233. if (p_b->p_lnk == p_task) {
  234. p_b->p_lnk = p_task->p_lnk;
  235. return;
  236. }
  237. p_b = p_b->p_lnk;
  238. }
  239. }
  240. /*--------------------------- rt_rmv_dly ------------------------------------*/
  241. void rt_rmv_dly (P_TCB p_task) {
  242. /* Remove task identified with "p_task" from delay list if enqueued. */
  243. P_TCB p_b;
  244. p_b = p_task->p_blnk;
  245. if (p_b != NULL) {
  246. /* Task is really enqueued */
  247. p_b->p_dlnk = p_task->p_dlnk;
  248. if (p_task->p_dlnk != NULL) {
  249. /* 'p_task' is in the middle of list */
  250. p_b->delta_time += p_task->delta_time;
  251. p_task->p_dlnk->p_blnk = p_b;
  252. p_task->p_dlnk = NULL;
  253. }
  254. else {
  255. /* 'p_task' is at the end of list */
  256. p_b->delta_time = 0;
  257. }
  258. p_task->p_blnk = NULL;
  259. }
  260. }
  261. /*--------------------------- rt_psq_enq ------------------------------------*/
  262. void rt_psq_enq (OS_ID entry, U32 arg) {
  263. /* Insert post service request "entry" into ps-queue. */
  264. U32 idx;
  265. idx = rt_inc_qi (os_psq->size, &os_psq->count, &os_psq->first);
  266. if (idx < os_psq->size) {
  267. os_psq->q[idx].id = entry;
  268. os_psq->q[idx].arg = arg;
  269. }
  270. else {
  271. os_error (OS_ERR_FIFO_OVF);
  272. }
  273. }
  274. /*----------------------------------------------------------------------------
  275. * end of file
  276. *---------------------------------------------------------------------------*/