forked

Dependents:   WireFSHandControl

Fork of mbed-rtos by mbed official

Committer:
Kojto
Date:
Tue Jul 04 13:32:20 2017 +0100
Revision:
125:5713cbbdb706
Parent:
80:2dab120a94c2
replace mbed_rtx by mbed_rtx4

Not causing a conflict with mbed_rtx that is for newer rtos

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 80:2dab120a94c2 1 /*----------------------------------------------------------------------------
mbed_official 80:2dab120a94c2 2 * RL-ARM - RTX
mbed_official 80:2dab120a94c2 3 *----------------------------------------------------------------------------
mbed_official 80:2dab120a94c2 4 * Name: RT_MAILBOX.C
mbed_official 80:2dab120a94c2 5 * Purpose: Implements waits and wake-ups for mailbox messages
mbed_official 80:2dab120a94c2 6 * Rev.: V4.60
mbed_official 80:2dab120a94c2 7 *----------------------------------------------------------------------------
mbed_official 80:2dab120a94c2 8 *
mbed_official 80:2dab120a94c2 9 * Copyright (c) 1999-2009 KEIL, 2009-2015 ARM Germany GmbH
mbed_official 80:2dab120a94c2 10 * All rights reserved.
mbed_official 80:2dab120a94c2 11 * Redistribution and use in source and binary forms, with or without
mbed_official 80:2dab120a94c2 12 * modification, are permitted provided that the following conditions are met:
mbed_official 80:2dab120a94c2 13 * - Redistributions of source code must retain the above copyright
mbed_official 80:2dab120a94c2 14 * notice, this list of conditions and the following disclaimer.
mbed_official 80:2dab120a94c2 15 * - Redistributions in binary form must reproduce the above copyright
mbed_official 80:2dab120a94c2 16 * notice, this list of conditions and the following disclaimer in the
mbed_official 80:2dab120a94c2 17 * documentation and/or other materials provided with the distribution.
mbed_official 80:2dab120a94c2 18 * - Neither the name of ARM nor the names of its contributors may be used
mbed_official 80:2dab120a94c2 19 * to endorse or promote products derived from this software without
mbed_official 80:2dab120a94c2 20 * specific prior written permission.
mbed_official 80:2dab120a94c2 21 *
mbed_official 80:2dab120a94c2 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 80:2dab120a94c2 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 80:2dab120a94c2 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
mbed_official 80:2dab120a94c2 25 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
mbed_official 80:2dab120a94c2 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
mbed_official 80:2dab120a94c2 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
mbed_official 80:2dab120a94c2 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
mbed_official 80:2dab120a94c2 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
mbed_official 80:2dab120a94c2 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
mbed_official 80:2dab120a94c2 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
mbed_official 80:2dab120a94c2 32 * POSSIBILITY OF SUCH DAMAGE.
mbed_official 80:2dab120a94c2 33 *---------------------------------------------------------------------------*/
mbed_official 80:2dab120a94c2 34
mbed_official 80:2dab120a94c2 35 #include "rt_TypeDef.h"
mbed_official 80:2dab120a94c2 36 #include "RTX_Conf.h"
mbed_official 80:2dab120a94c2 37 #include "rt_System.h"
mbed_official 80:2dab120a94c2 38 #include "rt_List.h"
mbed_official 80:2dab120a94c2 39 #include "rt_Mailbox.h"
mbed_official 80:2dab120a94c2 40 #include "rt_MemBox.h"
mbed_official 80:2dab120a94c2 41 #include "rt_Task.h"
mbed_official 80:2dab120a94c2 42 #include "rt_HAL_CM.h"
mbed_official 80:2dab120a94c2 43
mbed_official 80:2dab120a94c2 44
mbed_official 80:2dab120a94c2 45 /*----------------------------------------------------------------------------
mbed_official 80:2dab120a94c2 46 * Functions
mbed_official 80:2dab120a94c2 47 *---------------------------------------------------------------------------*/
mbed_official 80:2dab120a94c2 48
mbed_official 80:2dab120a94c2 49
mbed_official 80:2dab120a94c2 50 /*--------------------------- rt_mbx_init -----------------------------------*/
mbed_official 80:2dab120a94c2 51
mbed_official 80:2dab120a94c2 52 void rt_mbx_init (OS_ID mailbox, U16 mbx_size) {
mbed_official 80:2dab120a94c2 53 /* Initialize a mailbox */
mbed_official 80:2dab120a94c2 54 P_MCB p_MCB = mailbox;
mbed_official 80:2dab120a94c2 55
mbed_official 80:2dab120a94c2 56 p_MCB->cb_type = MCB;
mbed_official 80:2dab120a94c2 57 p_MCB->state = 0;
mbed_official 80:2dab120a94c2 58 p_MCB->isr_st = 0;
mbed_official 80:2dab120a94c2 59 p_MCB->p_lnk = NULL;
mbed_official 80:2dab120a94c2 60 p_MCB->first = 0;
mbed_official 80:2dab120a94c2 61 p_MCB->last = 0;
mbed_official 80:2dab120a94c2 62 p_MCB->count = 0;
mbed_official 80:2dab120a94c2 63 p_MCB->size = (mbx_size + sizeof(void *) - sizeof(struct OS_MCB)) /
mbed_official 80:2dab120a94c2 64 (U32)sizeof (void *);
mbed_official 80:2dab120a94c2 65 }
mbed_official 80:2dab120a94c2 66
mbed_official 80:2dab120a94c2 67
mbed_official 80:2dab120a94c2 68 /*--------------------------- rt_mbx_send -----------------------------------*/
mbed_official 80:2dab120a94c2 69
mbed_official 80:2dab120a94c2 70 OS_RESULT rt_mbx_send (OS_ID mailbox, void *p_msg, U16 timeout) {
mbed_official 80:2dab120a94c2 71 /* Send message to a mailbox */
mbed_official 80:2dab120a94c2 72 P_MCB p_MCB = mailbox;
mbed_official 80:2dab120a94c2 73 P_TCB p_TCB;
mbed_official 80:2dab120a94c2 74
mbed_official 80:2dab120a94c2 75 if ((p_MCB->p_lnk != NULL) && (p_MCB->state == 1)) {
mbed_official 80:2dab120a94c2 76 /* A task is waiting for message */
mbed_official 80:2dab120a94c2 77 p_TCB = rt_get_first ((P_XCB)p_MCB);
mbed_official 80:2dab120a94c2 78 #ifdef __CMSIS_RTOS
mbed_official 80:2dab120a94c2 79 rt_ret_val2(p_TCB, 0x10/*osEventMessage*/, (U32)p_msg);
mbed_official 80:2dab120a94c2 80 #else
mbed_official 80:2dab120a94c2 81 *p_TCB->msg = p_msg;
mbed_official 80:2dab120a94c2 82 rt_ret_val (p_TCB, OS_R_MBX);
mbed_official 80:2dab120a94c2 83 #endif
mbed_official 80:2dab120a94c2 84 rt_rmv_dly (p_TCB);
mbed_official 80:2dab120a94c2 85 rt_dispatch (p_TCB);
mbed_official 80:2dab120a94c2 86 }
mbed_official 80:2dab120a94c2 87 else {
mbed_official 80:2dab120a94c2 88 /* Store message in mailbox queue */
mbed_official 80:2dab120a94c2 89 if (p_MCB->count == p_MCB->size) {
mbed_official 80:2dab120a94c2 90 /* No free message entry, wait for one. If message queue is full, */
mbed_official 80:2dab120a94c2 91 /* then no task is waiting for message. The 'p_MCB->p_lnk' list */
mbed_official 80:2dab120a94c2 92 /* pointer can now be reused for send message waits task list. */
mbed_official 80:2dab120a94c2 93 if (timeout == 0) {
mbed_official 80:2dab120a94c2 94 return (OS_R_TMO);
mbed_official 80:2dab120a94c2 95 }
mbed_official 80:2dab120a94c2 96 if (p_MCB->p_lnk != NULL) {
mbed_official 80:2dab120a94c2 97 rt_put_prio ((P_XCB)p_MCB, os_tsk.run);
mbed_official 80:2dab120a94c2 98 }
mbed_official 80:2dab120a94c2 99 else {
mbed_official 80:2dab120a94c2 100 p_MCB->p_lnk = os_tsk.run;
mbed_official 80:2dab120a94c2 101 os_tsk.run->p_lnk = NULL;
mbed_official 80:2dab120a94c2 102 os_tsk.run->p_rlnk = (P_TCB)p_MCB;
mbed_official 80:2dab120a94c2 103 /* Task is waiting to send a message */
mbed_official 80:2dab120a94c2 104 p_MCB->state = 2;
mbed_official 80:2dab120a94c2 105 }
mbed_official 80:2dab120a94c2 106 os_tsk.run->msg = p_msg;
mbed_official 80:2dab120a94c2 107 rt_block (timeout, WAIT_MBX);
mbed_official 80:2dab120a94c2 108 return (OS_R_TMO);
mbed_official 80:2dab120a94c2 109 }
mbed_official 80:2dab120a94c2 110 /* Yes, there is a free entry in a mailbox. */
mbed_official 80:2dab120a94c2 111 p_MCB->msg[p_MCB->first] = p_msg;
mbed_official 80:2dab120a94c2 112 rt_inc (&p_MCB->count);
mbed_official 80:2dab120a94c2 113 if (++p_MCB->first == p_MCB->size) {
mbed_official 80:2dab120a94c2 114 p_MCB->first = 0;
mbed_official 80:2dab120a94c2 115 }
mbed_official 80:2dab120a94c2 116 }
mbed_official 80:2dab120a94c2 117 return (OS_R_OK);
mbed_official 80:2dab120a94c2 118 }
mbed_official 80:2dab120a94c2 119
mbed_official 80:2dab120a94c2 120
mbed_official 80:2dab120a94c2 121 /*--------------------------- rt_mbx_wait -----------------------------------*/
mbed_official 80:2dab120a94c2 122
mbed_official 80:2dab120a94c2 123 OS_RESULT rt_mbx_wait (OS_ID mailbox, void **message, U16 timeout) {
mbed_official 80:2dab120a94c2 124 /* Receive a message; possibly wait for it */
mbed_official 80:2dab120a94c2 125 P_MCB p_MCB = mailbox;
mbed_official 80:2dab120a94c2 126 P_TCB p_TCB;
mbed_official 80:2dab120a94c2 127
mbed_official 80:2dab120a94c2 128 /* If a message is available in the fifo buffer */
mbed_official 80:2dab120a94c2 129 /* remove it from the fifo buffer and return. */
mbed_official 80:2dab120a94c2 130 if (p_MCB->count) {
mbed_official 80:2dab120a94c2 131 *message = p_MCB->msg[p_MCB->last];
mbed_official 80:2dab120a94c2 132 if (++p_MCB->last == p_MCB->size) {
mbed_official 80:2dab120a94c2 133 p_MCB->last = 0;
mbed_official 80:2dab120a94c2 134 }
mbed_official 80:2dab120a94c2 135 if ((p_MCB->p_lnk != NULL) && (p_MCB->state == 2)) {
mbed_official 80:2dab120a94c2 136 /* A task is waiting to send message */
mbed_official 80:2dab120a94c2 137 p_TCB = rt_get_first ((P_XCB)p_MCB);
mbed_official 80:2dab120a94c2 138 #ifdef __CMSIS_RTOS
mbed_official 80:2dab120a94c2 139 rt_ret_val(p_TCB, 0/*osOK*/);
mbed_official 80:2dab120a94c2 140 #else
mbed_official 80:2dab120a94c2 141 rt_ret_val(p_TCB, OS_R_OK);
mbed_official 80:2dab120a94c2 142 #endif
mbed_official 80:2dab120a94c2 143 p_MCB->msg[p_MCB->first] = p_TCB->msg;
mbed_official 80:2dab120a94c2 144 if (++p_MCB->first == p_MCB->size) {
mbed_official 80:2dab120a94c2 145 p_MCB->first = 0;
mbed_official 80:2dab120a94c2 146 }
mbed_official 80:2dab120a94c2 147 rt_rmv_dly (p_TCB);
mbed_official 80:2dab120a94c2 148 rt_dispatch (p_TCB);
mbed_official 80:2dab120a94c2 149 }
mbed_official 80:2dab120a94c2 150 else {
mbed_official 80:2dab120a94c2 151 rt_dec (&p_MCB->count);
mbed_official 80:2dab120a94c2 152 }
mbed_official 80:2dab120a94c2 153 return (OS_R_OK);
mbed_official 80:2dab120a94c2 154 }
mbed_official 80:2dab120a94c2 155 /* No message available: wait for one */
mbed_official 80:2dab120a94c2 156 if (timeout == 0) {
mbed_official 80:2dab120a94c2 157 return (OS_R_TMO);
mbed_official 80:2dab120a94c2 158 }
mbed_official 80:2dab120a94c2 159 if (p_MCB->p_lnk != NULL) {
mbed_official 80:2dab120a94c2 160 rt_put_prio ((P_XCB)p_MCB, os_tsk.run);
mbed_official 80:2dab120a94c2 161 }
mbed_official 80:2dab120a94c2 162 else {
mbed_official 80:2dab120a94c2 163 p_MCB->p_lnk = os_tsk.run;
mbed_official 80:2dab120a94c2 164 os_tsk.run->p_lnk = NULL;
mbed_official 80:2dab120a94c2 165 os_tsk.run->p_rlnk = (P_TCB)p_MCB;
mbed_official 80:2dab120a94c2 166 /* Task is waiting to receive a message */
mbed_official 80:2dab120a94c2 167 p_MCB->state = 1;
mbed_official 80:2dab120a94c2 168 }
mbed_official 80:2dab120a94c2 169 rt_block(timeout, WAIT_MBX);
mbed_official 80:2dab120a94c2 170 #ifndef __CMSIS_RTOS
mbed_official 80:2dab120a94c2 171 os_tsk.run->msg = message;
mbed_official 80:2dab120a94c2 172 #endif
mbed_official 80:2dab120a94c2 173 return (OS_R_TMO);
mbed_official 80:2dab120a94c2 174 }
mbed_official 80:2dab120a94c2 175
mbed_official 80:2dab120a94c2 176
mbed_official 80:2dab120a94c2 177 /*--------------------------- rt_mbx_check ----------------------------------*/
mbed_official 80:2dab120a94c2 178
mbed_official 80:2dab120a94c2 179 OS_RESULT rt_mbx_check (OS_ID mailbox) {
mbed_official 80:2dab120a94c2 180 /* Check for free space in a mailbox. Returns the number of messages */
mbed_official 80:2dab120a94c2 181 /* that can be stored to a mailbox. It returns 0 when mailbox is full. */
mbed_official 80:2dab120a94c2 182 P_MCB p_MCB = mailbox;
mbed_official 80:2dab120a94c2 183
mbed_official 80:2dab120a94c2 184 return (p_MCB->size - p_MCB->count);
mbed_official 80:2dab120a94c2 185 }
mbed_official 80:2dab120a94c2 186
mbed_official 80:2dab120a94c2 187
mbed_official 80:2dab120a94c2 188 /*--------------------------- isr_mbx_send ----------------------------------*/
mbed_official 80:2dab120a94c2 189
mbed_official 80:2dab120a94c2 190 void isr_mbx_send (OS_ID mailbox, void *p_msg) {
mbed_official 80:2dab120a94c2 191 /* Same function as "os_mbx_send", but to be called by ISRs. */
mbed_official 80:2dab120a94c2 192 P_MCB p_MCB = mailbox;
mbed_official 80:2dab120a94c2 193
mbed_official 80:2dab120a94c2 194 rt_psq_enq (p_MCB, (U32)p_msg);
mbed_official 80:2dab120a94c2 195 rt_psh_req ();
mbed_official 80:2dab120a94c2 196 }
mbed_official 80:2dab120a94c2 197
mbed_official 80:2dab120a94c2 198
mbed_official 80:2dab120a94c2 199 /*--------------------------- isr_mbx_receive -------------------------------*/
mbed_official 80:2dab120a94c2 200
mbed_official 80:2dab120a94c2 201 OS_RESULT isr_mbx_receive (OS_ID mailbox, void **message) {
mbed_official 80:2dab120a94c2 202 /* Receive a message in the interrupt function. The interrupt function */
mbed_official 80:2dab120a94c2 203 /* should not wait for a message since this would block the rtx os. */
mbed_official 80:2dab120a94c2 204 P_MCB p_MCB = mailbox;
mbed_official 80:2dab120a94c2 205
mbed_official 80:2dab120a94c2 206 if (p_MCB->count) {
mbed_official 80:2dab120a94c2 207 /* A message is available in the fifo buffer. */
mbed_official 80:2dab120a94c2 208 *message = p_MCB->msg[p_MCB->last];
mbed_official 80:2dab120a94c2 209 if (p_MCB->state == 2) {
mbed_official 80:2dab120a94c2 210 /* A task is locked waiting to send message */
mbed_official 80:2dab120a94c2 211 rt_psq_enq (p_MCB, 0);
mbed_official 80:2dab120a94c2 212 rt_psh_req ();
mbed_official 80:2dab120a94c2 213 }
mbed_official 80:2dab120a94c2 214 rt_dec (&p_MCB->count);
mbed_official 80:2dab120a94c2 215 if (++p_MCB->last == p_MCB->size) {
mbed_official 80:2dab120a94c2 216 p_MCB->last = 0;
mbed_official 80:2dab120a94c2 217 }
mbed_official 80:2dab120a94c2 218 return (OS_R_MBX);
mbed_official 80:2dab120a94c2 219 }
mbed_official 80:2dab120a94c2 220 return (OS_R_OK);
mbed_official 80:2dab120a94c2 221 }
mbed_official 80:2dab120a94c2 222
mbed_official 80:2dab120a94c2 223
mbed_official 80:2dab120a94c2 224 /*--------------------------- rt_mbx_psh ------------------------------------*/
mbed_official 80:2dab120a94c2 225
mbed_official 80:2dab120a94c2 226 void rt_mbx_psh (P_MCB p_CB, void *p_msg) {
mbed_official 80:2dab120a94c2 227 /* Store the message to the mailbox queue or pass it to task directly. */
mbed_official 80:2dab120a94c2 228 P_TCB p_TCB;
mbed_official 80:2dab120a94c2 229 void *mem;
mbed_official 80:2dab120a94c2 230
mbed_official 80:2dab120a94c2 231 if (p_CB->p_lnk != NULL) switch (p_CB->state) {
mbed_official 80:2dab120a94c2 232 #ifdef __CMSIS_RTOS
mbed_official 80:2dab120a94c2 233 case 3:
mbed_official 80:2dab120a94c2 234 /* Task is waiting to allocate memory, remove it from the waiting list */
mbed_official 80:2dab120a94c2 235 mem = rt_alloc_box(p_msg);
mbed_official 80:2dab120a94c2 236 if (mem == NULL) break;
mbed_official 80:2dab120a94c2 237 p_TCB = rt_get_first ((P_XCB)p_CB);
mbed_official 80:2dab120a94c2 238 rt_ret_val(p_TCB, (U32)mem);
mbed_official 80:2dab120a94c2 239 p_TCB->state = READY;
mbed_official 80:2dab120a94c2 240 rt_rmv_dly (p_TCB);
mbed_official 80:2dab120a94c2 241 rt_put_prio (&os_rdy, p_TCB);
mbed_official 80:2dab120a94c2 242 break;
mbed_official 80:2dab120a94c2 243 #endif
mbed_official 80:2dab120a94c2 244 case 2:
mbed_official 80:2dab120a94c2 245 /* Task is waiting to send a message, remove it from the waiting list */
mbed_official 80:2dab120a94c2 246 p_TCB = rt_get_first ((P_XCB)p_CB);
mbed_official 80:2dab120a94c2 247 #ifdef __CMSIS_RTOS
mbed_official 80:2dab120a94c2 248 rt_ret_val(p_TCB, 0/*osOK*/);
mbed_official 80:2dab120a94c2 249 #else
mbed_official 80:2dab120a94c2 250 rt_ret_val(p_TCB, OS_R_OK);
mbed_official 80:2dab120a94c2 251 #endif
mbed_official 80:2dab120a94c2 252 p_CB->msg[p_CB->first] = p_TCB->msg;
mbed_official 80:2dab120a94c2 253 rt_inc (&p_CB->count);
mbed_official 80:2dab120a94c2 254 if (++p_CB->first == p_CB->size) {
mbed_official 80:2dab120a94c2 255 p_CB->first = 0;
mbed_official 80:2dab120a94c2 256 }
mbed_official 80:2dab120a94c2 257 p_TCB->state = READY;
mbed_official 80:2dab120a94c2 258 rt_rmv_dly (p_TCB);
mbed_official 80:2dab120a94c2 259 rt_put_prio (&os_rdy, p_TCB);
mbed_official 80:2dab120a94c2 260 break;
mbed_official 80:2dab120a94c2 261 case 1:
mbed_official 80:2dab120a94c2 262 /* Task is waiting for a message, pass the message to the task directly */
mbed_official 80:2dab120a94c2 263 p_TCB = rt_get_first ((P_XCB)p_CB);
mbed_official 80:2dab120a94c2 264 #ifdef __CMSIS_RTOS
mbed_official 80:2dab120a94c2 265 rt_ret_val2(p_TCB, 0x10/*osEventMessage*/, (U32)p_msg);
mbed_official 80:2dab120a94c2 266 #else
mbed_official 80:2dab120a94c2 267 *p_TCB->msg = p_msg;
mbed_official 80:2dab120a94c2 268 rt_ret_val (p_TCB, OS_R_MBX);
mbed_official 80:2dab120a94c2 269 #endif
mbed_official 80:2dab120a94c2 270 p_TCB->state = READY;
mbed_official 80:2dab120a94c2 271 rt_rmv_dly (p_TCB);
mbed_official 80:2dab120a94c2 272 rt_put_prio (&os_rdy, p_TCB);
mbed_official 80:2dab120a94c2 273 break;
mbed_official 80:2dab120a94c2 274 } else {
mbed_official 80:2dab120a94c2 275 /* No task is waiting for a message, store it to the mailbox queue */
mbed_official 80:2dab120a94c2 276 if (p_CB->count < p_CB->size) {
mbed_official 80:2dab120a94c2 277 p_CB->msg[p_CB->first] = p_msg;
mbed_official 80:2dab120a94c2 278 rt_inc (&p_CB->count);
mbed_official 80:2dab120a94c2 279 if (++p_CB->first == p_CB->size) {
mbed_official 80:2dab120a94c2 280 p_CB->first = 0;
mbed_official 80:2dab120a94c2 281 }
mbed_official 80:2dab120a94c2 282 }
mbed_official 80:2dab120a94c2 283 else {
mbed_official 80:2dab120a94c2 284 os_error (OS_ERR_MBX_OVF);
mbed_official 80:2dab120a94c2 285 }
mbed_official 80:2dab120a94c2 286 }
mbed_official 80:2dab120a94c2 287 }
mbed_official 80:2dab120a94c2 288
mbed_official 80:2dab120a94c2 289 /*----------------------------------------------------------------------------
mbed_official 80:2dab120a94c2 290 * end of file
mbed_official 80:2dab120a94c2 291 *---------------------------------------------------------------------------*/
mbed_official 80:2dab120a94c2 292