Port to C027 (using AppShield and Ethernet)

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application

Who changed what in which revision?

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