Greg Steiert / pegasus_dev

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Nov 11 20:59:50 2016 +0000
Revision:
0:5c4d7b2438d3
Initial commit

Who changed what in which revision?

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