test

Dependencies:   F7_Ethernet mbed BSP_DISCO_F746NG SDFileSystem RF24

Committer:
lowlowry
Date:
Tue Jun 08 16:21:16 2021 +0000
Revision:
3:1c6da30ca347
Parent:
0:d984976f1f1c
test final

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leo44 0:d984976f1f1c 1 /*----------------------------------------------------------------------------
leo44 0:d984976f1f1c 2 * RL-ARM - RTX
leo44 0:d984976f1f1c 3 *----------------------------------------------------------------------------
leo44 0:d984976f1f1c 4 * Name: RT_LIST.C
leo44 0:d984976f1f1c 5 * Purpose: Functions for the management of different lists
leo44 0:d984976f1f1c 6 * Rev.: V4.60
leo44 0:d984976f1f1c 7 *----------------------------------------------------------------------------
leo44 0:d984976f1f1c 8 *
leo44 0:d984976f1f1c 9 * Copyright (c) 1999-2009 KEIL, 2009-2015 ARM Germany GmbH
leo44 0:d984976f1f1c 10 * All rights reserved.
leo44 0:d984976f1f1c 11 * Redistribution and use in source and binary forms, with or without
leo44 0:d984976f1f1c 12 * modification, are permitted provided that the following conditions are met:
leo44 0:d984976f1f1c 13 * - Redistributions of source code must retain the above copyright
leo44 0:d984976f1f1c 14 * notice, this list of conditions and the following disclaimer.
leo44 0:d984976f1f1c 15 * - Redistributions in binary form must reproduce the above copyright
leo44 0:d984976f1f1c 16 * notice, this list of conditions and the following disclaimer in the
leo44 0:d984976f1f1c 17 * documentation and/or other materials provided with the distribution.
leo44 0:d984976f1f1c 18 * - Neither the name of ARM nor the names of its contributors may be used
leo44 0:d984976f1f1c 19 * to endorse or promote products derived from this software without
leo44 0:d984976f1f1c 20 * specific prior written permission.
leo44 0:d984976f1f1c 21 *
leo44 0:d984976f1f1c 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
leo44 0:d984976f1f1c 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
leo44 0:d984976f1f1c 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
leo44 0:d984976f1f1c 25 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
leo44 0:d984976f1f1c 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
leo44 0:d984976f1f1c 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
leo44 0:d984976f1f1c 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
leo44 0:d984976f1f1c 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
leo44 0:d984976f1f1c 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
leo44 0:d984976f1f1c 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
leo44 0:d984976f1f1c 32 * POSSIBILITY OF SUCH DAMAGE.
leo44 0:d984976f1f1c 33 *---------------------------------------------------------------------------*/
leo44 0:d984976f1f1c 34
leo44 0:d984976f1f1c 35 #include "rt_TypeDef.h"
leo44 0:d984976f1f1c 36 #include "RTX_Conf.h"
leo44 0:d984976f1f1c 37 #include "rt_System.h"
leo44 0:d984976f1f1c 38 #include "rt_List.h"
leo44 0:d984976f1f1c 39 #include "rt_Task.h"
leo44 0:d984976f1f1c 40 #include "rt_Time.h"
leo44 0:d984976f1f1c 41 #include "rt_HAL_CM.h"
leo44 0:d984976f1f1c 42
leo44 0:d984976f1f1c 43 /*----------------------------------------------------------------------------
leo44 0:d984976f1f1c 44 * Global Variables
leo44 0:d984976f1f1c 45 *---------------------------------------------------------------------------*/
leo44 0:d984976f1f1c 46
leo44 0:d984976f1f1c 47 /* List head of chained ready tasks */
leo44 0:d984976f1f1c 48 struct OS_XCB os_rdy;
leo44 0:d984976f1f1c 49 /* List head of chained delay tasks */
leo44 0:d984976f1f1c 50 struct OS_XCB os_dly;
leo44 0:d984976f1f1c 51
leo44 0:d984976f1f1c 52
leo44 0:d984976f1f1c 53 /*----------------------------------------------------------------------------
leo44 0:d984976f1f1c 54 * Functions
leo44 0:d984976f1f1c 55 *---------------------------------------------------------------------------*/
leo44 0:d984976f1f1c 56
leo44 0:d984976f1f1c 57
leo44 0:d984976f1f1c 58 /*--------------------------- rt_put_prio -----------------------------------*/
leo44 0:d984976f1f1c 59
leo44 0:d984976f1f1c 60 void rt_put_prio (P_XCB p_CB, P_TCB p_task) {
leo44 0:d984976f1f1c 61 /* Put task identified with "p_task" into list ordered by priority. */
leo44 0:d984976f1f1c 62 /* "p_CB" points to head of list; list has always an element at end with */
leo44 0:d984976f1f1c 63 /* a priority less than "p_task->prio". */
leo44 0:d984976f1f1c 64 P_TCB p_CB2;
leo44 0:d984976f1f1c 65 U32 prio;
leo44 0:d984976f1f1c 66 BOOL sem_mbx = __FALSE;
leo44 0:d984976f1f1c 67
leo44 0:d984976f1f1c 68 if (p_CB->cb_type == SCB || p_CB->cb_type == MCB || p_CB->cb_type == MUCB) {
leo44 0:d984976f1f1c 69 sem_mbx = __TRUE;
leo44 0:d984976f1f1c 70 }
leo44 0:d984976f1f1c 71 prio = p_task->prio;
leo44 0:d984976f1f1c 72 p_CB2 = p_CB->p_lnk;
leo44 0:d984976f1f1c 73 /* Search for an entry in the list */
leo44 0:d984976f1f1c 74 while (p_CB2 != NULL && prio <= p_CB2->prio) {
leo44 0:d984976f1f1c 75 p_CB = (P_XCB)p_CB2;
leo44 0:d984976f1f1c 76 p_CB2 = p_CB2->p_lnk;
leo44 0:d984976f1f1c 77 }
leo44 0:d984976f1f1c 78 /* Entry found, insert the task into the list */
leo44 0:d984976f1f1c 79 p_task->p_lnk = p_CB2;
leo44 0:d984976f1f1c 80 p_CB->p_lnk = p_task;
leo44 0:d984976f1f1c 81 if (sem_mbx) {
leo44 0:d984976f1f1c 82 if (p_CB2 != NULL) {
leo44 0:d984976f1f1c 83 p_CB2->p_rlnk = p_task;
leo44 0:d984976f1f1c 84 }
leo44 0:d984976f1f1c 85 p_task->p_rlnk = (P_TCB)p_CB;
leo44 0:d984976f1f1c 86 }
leo44 0:d984976f1f1c 87 else {
leo44 0:d984976f1f1c 88 p_task->p_rlnk = NULL;
leo44 0:d984976f1f1c 89 }
leo44 0:d984976f1f1c 90 }
leo44 0:d984976f1f1c 91
leo44 0:d984976f1f1c 92
leo44 0:d984976f1f1c 93 /*--------------------------- rt_get_first ----------------------------------*/
leo44 0:d984976f1f1c 94
leo44 0:d984976f1f1c 95 P_TCB rt_get_first (P_XCB p_CB) {
leo44 0:d984976f1f1c 96 /* Get task at head of list: it is the task with highest priority. */
leo44 0:d984976f1f1c 97 /* "p_CB" points to head of list. */
leo44 0:d984976f1f1c 98 P_TCB p_first;
leo44 0:d984976f1f1c 99
leo44 0:d984976f1f1c 100 p_first = p_CB->p_lnk;
leo44 0:d984976f1f1c 101 p_CB->p_lnk = p_first->p_lnk;
leo44 0:d984976f1f1c 102 if (p_CB->cb_type == SCB || p_CB->cb_type == MCB || p_CB->cb_type == MUCB) {
leo44 0:d984976f1f1c 103 if (p_first->p_lnk != NULL) {
leo44 0:d984976f1f1c 104 p_first->p_lnk->p_rlnk = (P_TCB)p_CB;
leo44 0:d984976f1f1c 105 p_first->p_lnk = NULL;
leo44 0:d984976f1f1c 106 }
leo44 0:d984976f1f1c 107 p_first->p_rlnk = NULL;
leo44 0:d984976f1f1c 108 }
leo44 0:d984976f1f1c 109 else {
leo44 0:d984976f1f1c 110 p_first->p_lnk = NULL;
leo44 0:d984976f1f1c 111 }
leo44 0:d984976f1f1c 112 return (p_first);
leo44 0:d984976f1f1c 113 }
leo44 0:d984976f1f1c 114
leo44 0:d984976f1f1c 115
leo44 0:d984976f1f1c 116 /*--------------------------- rt_put_rdy_first ------------------------------*/
leo44 0:d984976f1f1c 117
leo44 0:d984976f1f1c 118 void rt_put_rdy_first (P_TCB p_task) {
leo44 0:d984976f1f1c 119 /* Put task identified with "p_task" at the head of the ready list. The */
leo44 0:d984976f1f1c 120 /* task must have at least a priority equal to highest priority in list. */
leo44 0:d984976f1f1c 121 p_task->p_lnk = os_rdy.p_lnk;
leo44 0:d984976f1f1c 122 p_task->p_rlnk = NULL;
leo44 0:d984976f1f1c 123 os_rdy.p_lnk = p_task;
leo44 0:d984976f1f1c 124 }
leo44 0:d984976f1f1c 125
leo44 0:d984976f1f1c 126
leo44 0:d984976f1f1c 127 /*--------------------------- rt_get_same_rdy_prio --------------------------*/
leo44 0:d984976f1f1c 128
leo44 0:d984976f1f1c 129 P_TCB rt_get_same_rdy_prio (void) {
leo44 0:d984976f1f1c 130 /* Remove a task of same priority from ready list if any exists. Other- */
leo44 0:d984976f1f1c 131 /* wise return NULL. */
leo44 0:d984976f1f1c 132 P_TCB p_first;
leo44 0:d984976f1f1c 133
leo44 0:d984976f1f1c 134 p_first = os_rdy.p_lnk;
leo44 0:d984976f1f1c 135 if (p_first->prio == os_tsk.run->prio) {
leo44 0:d984976f1f1c 136 os_rdy.p_lnk = os_rdy.p_lnk->p_lnk;
leo44 0:d984976f1f1c 137 return (p_first);
leo44 0:d984976f1f1c 138 }
leo44 0:d984976f1f1c 139 return (NULL);
leo44 0:d984976f1f1c 140 }
leo44 0:d984976f1f1c 141
leo44 0:d984976f1f1c 142
leo44 0:d984976f1f1c 143 /*--------------------------- rt_resort_prio --------------------------------*/
leo44 0:d984976f1f1c 144
leo44 0:d984976f1f1c 145 void rt_resort_prio (P_TCB p_task) {
leo44 0:d984976f1f1c 146 /* Re-sort ordered lists after the priority of 'p_task' has changed. */
leo44 0:d984976f1f1c 147 P_TCB p_CB;
leo44 0:d984976f1f1c 148
leo44 0:d984976f1f1c 149 if (p_task->p_rlnk == NULL) {
leo44 0:d984976f1f1c 150 if (p_task->state == READY) {
leo44 0:d984976f1f1c 151 /* Task is chained into READY list. */
leo44 0:d984976f1f1c 152 p_CB = (P_TCB)&os_rdy;
leo44 0:d984976f1f1c 153 goto res;
leo44 0:d984976f1f1c 154 }
leo44 0:d984976f1f1c 155 }
leo44 0:d984976f1f1c 156 else {
leo44 0:d984976f1f1c 157 p_CB = p_task->p_rlnk;
leo44 0:d984976f1f1c 158 while (p_CB->cb_type == TCB) {
leo44 0:d984976f1f1c 159 /* Find a header of this task chain list. */
leo44 0:d984976f1f1c 160 p_CB = p_CB->p_rlnk;
leo44 0:d984976f1f1c 161 }
leo44 0:d984976f1f1c 162 res:rt_rmv_list (p_task);
leo44 0:d984976f1f1c 163 rt_put_prio ((P_XCB)p_CB, p_task);
leo44 0:d984976f1f1c 164 }
leo44 0:d984976f1f1c 165 }
leo44 0:d984976f1f1c 166
leo44 0:d984976f1f1c 167
leo44 0:d984976f1f1c 168 /*--------------------------- rt_put_dly ------------------------------------*/
leo44 0:d984976f1f1c 169
leo44 0:d984976f1f1c 170 void rt_put_dly (P_TCB p_task, U16 delay) {
leo44 0:d984976f1f1c 171 /* Put a task identified with "p_task" into chained delay wait list using */
leo44 0:d984976f1f1c 172 /* a delay value of "delay". */
leo44 0:d984976f1f1c 173 P_TCB p;
leo44 0:d984976f1f1c 174 U32 delta,idelay = delay;
leo44 0:d984976f1f1c 175
leo44 0:d984976f1f1c 176 p = (P_TCB)&os_dly;
leo44 0:d984976f1f1c 177 if (p->p_dlnk == NULL) {
leo44 0:d984976f1f1c 178 /* Delay list empty */
leo44 0:d984976f1f1c 179 delta = 0;
leo44 0:d984976f1f1c 180 goto last;
leo44 0:d984976f1f1c 181 }
leo44 0:d984976f1f1c 182 delta = os_dly.delta_time;
leo44 0:d984976f1f1c 183 while (delta < idelay) {
leo44 0:d984976f1f1c 184 if (p->p_dlnk == NULL) {
leo44 0:d984976f1f1c 185 /* End of list found */
leo44 0:d984976f1f1c 186 last: p_task->p_dlnk = NULL;
leo44 0:d984976f1f1c 187 p->p_dlnk = p_task;
leo44 0:d984976f1f1c 188 p_task->p_blnk = p;
leo44 0:d984976f1f1c 189 p->delta_time = (U16)(idelay - delta);
leo44 0:d984976f1f1c 190 p_task->delta_time = 0;
leo44 0:d984976f1f1c 191 return;
leo44 0:d984976f1f1c 192 }
leo44 0:d984976f1f1c 193 p = p->p_dlnk;
leo44 0:d984976f1f1c 194 delta += p->delta_time;
leo44 0:d984976f1f1c 195 }
leo44 0:d984976f1f1c 196 /* Right place found */
leo44 0:d984976f1f1c 197 p_task->p_dlnk = p->p_dlnk;
leo44 0:d984976f1f1c 198 p->p_dlnk = p_task;
leo44 0:d984976f1f1c 199 p_task->p_blnk = p;
leo44 0:d984976f1f1c 200 if (p_task->p_dlnk != NULL) {
leo44 0:d984976f1f1c 201 p_task->p_dlnk->p_blnk = p_task;
leo44 0:d984976f1f1c 202 }
leo44 0:d984976f1f1c 203 p_task->delta_time = (U16)(delta - idelay);
leo44 0:d984976f1f1c 204 p->delta_time -= p_task->delta_time;
leo44 0:d984976f1f1c 205 }
leo44 0:d984976f1f1c 206
leo44 0:d984976f1f1c 207
leo44 0:d984976f1f1c 208 /*--------------------------- rt_dec_dly ------------------------------------*/
leo44 0:d984976f1f1c 209
leo44 0:d984976f1f1c 210 void rt_dec_dly (void) {
leo44 0:d984976f1f1c 211 /* Decrement delta time of list head: remove tasks having a value of zero.*/
leo44 0:d984976f1f1c 212 P_TCB p_rdy;
leo44 0:d984976f1f1c 213
leo44 0:d984976f1f1c 214 if (os_dly.p_dlnk == NULL) {
leo44 0:d984976f1f1c 215 return;
leo44 0:d984976f1f1c 216 }
leo44 0:d984976f1f1c 217 os_dly.delta_time--;
leo44 0:d984976f1f1c 218 while ((os_dly.delta_time == 0) && (os_dly.p_dlnk != NULL)) {
leo44 0:d984976f1f1c 219 p_rdy = os_dly.p_dlnk;
leo44 0:d984976f1f1c 220 if (p_rdy->p_rlnk != NULL) {
leo44 0:d984976f1f1c 221 /* Task is really enqueued, remove task from semaphore/mailbox */
leo44 0:d984976f1f1c 222 /* timeout waiting list. */
leo44 0:d984976f1f1c 223 p_rdy->p_rlnk->p_lnk = p_rdy->p_lnk;
leo44 0:d984976f1f1c 224 if (p_rdy->p_lnk != NULL) {
leo44 0:d984976f1f1c 225 p_rdy->p_lnk->p_rlnk = p_rdy->p_rlnk;
leo44 0:d984976f1f1c 226 p_rdy->p_lnk = NULL;
leo44 0:d984976f1f1c 227 }
leo44 0:d984976f1f1c 228 p_rdy->p_rlnk = NULL;
leo44 0:d984976f1f1c 229 }
leo44 0:d984976f1f1c 230 rt_put_prio (&os_rdy, p_rdy);
leo44 0:d984976f1f1c 231 os_dly.delta_time = p_rdy->delta_time;
leo44 0:d984976f1f1c 232 if (p_rdy->state == WAIT_ITV) {
leo44 0:d984976f1f1c 233 /* Calculate the next time for interval wait. */
leo44 0:d984976f1f1c 234 p_rdy->delta_time = p_rdy->interval_time + (U16)os_time;
leo44 0:d984976f1f1c 235 }
leo44 0:d984976f1f1c 236 p_rdy->state = READY;
leo44 0:d984976f1f1c 237 os_dly.p_dlnk = p_rdy->p_dlnk;
leo44 0:d984976f1f1c 238 if (p_rdy->p_dlnk != NULL) {
leo44 0:d984976f1f1c 239 p_rdy->p_dlnk->p_blnk = (P_TCB)&os_dly;
leo44 0:d984976f1f1c 240 p_rdy->p_dlnk = NULL;
leo44 0:d984976f1f1c 241 }
leo44 0:d984976f1f1c 242 p_rdy->p_blnk = NULL;
leo44 0:d984976f1f1c 243 }
leo44 0:d984976f1f1c 244 }
leo44 0:d984976f1f1c 245
leo44 0:d984976f1f1c 246
leo44 0:d984976f1f1c 247 /*--------------------------- rt_rmv_list -----------------------------------*/
leo44 0:d984976f1f1c 248
leo44 0:d984976f1f1c 249 void rt_rmv_list (P_TCB p_task) {
leo44 0:d984976f1f1c 250 /* Remove task identified with "p_task" from ready, semaphore or mailbox */
leo44 0:d984976f1f1c 251 /* waiting list if enqueued. */
leo44 0:d984976f1f1c 252 P_TCB p_b;
leo44 0:d984976f1f1c 253
leo44 0:d984976f1f1c 254 if (p_task->p_rlnk != NULL) {
leo44 0:d984976f1f1c 255 /* A task is enqueued in semaphore / mailbox waiting list. */
leo44 0:d984976f1f1c 256 p_task->p_rlnk->p_lnk = p_task->p_lnk;
leo44 0:d984976f1f1c 257 if (p_task->p_lnk != NULL) {
leo44 0:d984976f1f1c 258 p_task->p_lnk->p_rlnk = p_task->p_rlnk;
leo44 0:d984976f1f1c 259 }
leo44 0:d984976f1f1c 260 return;
leo44 0:d984976f1f1c 261 }
leo44 0:d984976f1f1c 262
leo44 0:d984976f1f1c 263 p_b = (P_TCB)&os_rdy;
leo44 0:d984976f1f1c 264 while (p_b != NULL) {
leo44 0:d984976f1f1c 265 /* Search the ready list for task "p_task" */
leo44 0:d984976f1f1c 266 if (p_b->p_lnk == p_task) {
leo44 0:d984976f1f1c 267 p_b->p_lnk = p_task->p_lnk;
leo44 0:d984976f1f1c 268 return;
leo44 0:d984976f1f1c 269 }
leo44 0:d984976f1f1c 270 p_b = p_b->p_lnk;
leo44 0:d984976f1f1c 271 }
leo44 0:d984976f1f1c 272 }
leo44 0:d984976f1f1c 273
leo44 0:d984976f1f1c 274
leo44 0:d984976f1f1c 275 /*--------------------------- rt_rmv_dly ------------------------------------*/
leo44 0:d984976f1f1c 276
leo44 0:d984976f1f1c 277 void rt_rmv_dly (P_TCB p_task) {
leo44 0:d984976f1f1c 278 /* Remove task identified with "p_task" from delay list if enqueued. */
leo44 0:d984976f1f1c 279 P_TCB p_b;
leo44 0:d984976f1f1c 280
leo44 0:d984976f1f1c 281 p_b = p_task->p_blnk;
leo44 0:d984976f1f1c 282 if (p_b != NULL) {
leo44 0:d984976f1f1c 283 /* Task is really enqueued */
leo44 0:d984976f1f1c 284 p_b->p_dlnk = p_task->p_dlnk;
leo44 0:d984976f1f1c 285 if (p_task->p_dlnk != NULL) {
leo44 0:d984976f1f1c 286 /* 'p_task' is in the middle of list */
leo44 0:d984976f1f1c 287 p_b->delta_time += p_task->delta_time;
leo44 0:d984976f1f1c 288 p_task->p_dlnk->p_blnk = p_b;
leo44 0:d984976f1f1c 289 p_task->p_dlnk = NULL;
leo44 0:d984976f1f1c 290 }
leo44 0:d984976f1f1c 291 else {
leo44 0:d984976f1f1c 292 /* 'p_task' is at the end of list */
leo44 0:d984976f1f1c 293 p_b->delta_time = 0;
leo44 0:d984976f1f1c 294 }
leo44 0:d984976f1f1c 295 p_task->p_blnk = NULL;
leo44 0:d984976f1f1c 296 }
leo44 0:d984976f1f1c 297 }
leo44 0:d984976f1f1c 298
leo44 0:d984976f1f1c 299
leo44 0:d984976f1f1c 300 /*--------------------------- rt_psq_enq ------------------------------------*/
leo44 0:d984976f1f1c 301
leo44 0:d984976f1f1c 302 void rt_psq_enq (OS_ID entry, U32 arg) {
leo44 0:d984976f1f1c 303 /* Insert post service request "entry" into ps-queue. */
leo44 0:d984976f1f1c 304 U32 idx;
leo44 0:d984976f1f1c 305
leo44 0:d984976f1f1c 306 idx = rt_inc_qi (os_psq->size, &os_psq->count, &os_psq->first);
leo44 0:d984976f1f1c 307 if (idx < os_psq->size) {
leo44 0:d984976f1f1c 308 os_psq->q[idx].id = entry;
leo44 0:d984976f1f1c 309 os_psq->q[idx].arg = arg;
leo44 0:d984976f1f1c 310 }
leo44 0:d984976f1f1c 311 else {
leo44 0:d984976f1f1c 312 os_error (OS_ERR_FIFO_OVF);
leo44 0:d984976f1f1c 313 }
leo44 0:d984976f1f1c 314 }
leo44 0:d984976f1f1c 315
leo44 0:d984976f1f1c 316
leo44 0:d984976f1f1c 317 /*----------------------------------------------------------------------------
leo44 0:d984976f1f1c 318 * end of file
leo44 0:d984976f1f1c 319 *---------------------------------------------------------------------------*/
leo44 0:d984976f1f1c 320