Contains example code to connect the mbed LPC1768 or FRDM-K64F devices to the IBM Internet of Things Cloud service via ethernet.

Dependencies:   C12832 MQTT LM75B MMA7660

Dependents:   MFT_IoT_demo_USB400 IBM_RFID

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_SEMAPHORE.C
samdanbury 6:37b6d0d56190 5 * Purpose: Implements binary and counting semaphores
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_Task.h"
samdanbury 6:37b6d0d56190 40 #include "rt_Semaphore.h"
samdanbury 6:37b6d0d56190 41 #include "rt_HAL_CM.h"
samdanbury 6:37b6d0d56190 42
samdanbury 6:37b6d0d56190 43
samdanbury 6:37b6d0d56190 44 /*----------------------------------------------------------------------------
samdanbury 6:37b6d0d56190 45 * Functions
samdanbury 6:37b6d0d56190 46 *---------------------------------------------------------------------------*/
samdanbury 6:37b6d0d56190 47
samdanbury 6:37b6d0d56190 48
samdanbury 6:37b6d0d56190 49 /*--------------------------- rt_sem_init -----------------------------------*/
samdanbury 6:37b6d0d56190 50
samdanbury 6:37b6d0d56190 51 void rt_sem_init (OS_ID semaphore, U16 token_count) {
samdanbury 6:37b6d0d56190 52 /* Initialize a semaphore */
samdanbury 6:37b6d0d56190 53 P_SCB p_SCB = semaphore;
samdanbury 6:37b6d0d56190 54
samdanbury 6:37b6d0d56190 55 p_SCB->cb_type = SCB;
samdanbury 6:37b6d0d56190 56 p_SCB->p_lnk = NULL;
samdanbury 6:37b6d0d56190 57 p_SCB->tokens = token_count;
samdanbury 6:37b6d0d56190 58 }
samdanbury 6:37b6d0d56190 59
samdanbury 6:37b6d0d56190 60
samdanbury 6:37b6d0d56190 61 /*--------------------------- rt_sem_delete ---------------------------------*/
samdanbury 6:37b6d0d56190 62
samdanbury 6:37b6d0d56190 63 #ifdef __CMSIS_RTOS
samdanbury 6:37b6d0d56190 64 OS_RESULT rt_sem_delete (OS_ID semaphore) {
samdanbury 6:37b6d0d56190 65 /* Delete semaphore */
samdanbury 6:37b6d0d56190 66 P_SCB p_SCB = semaphore;
samdanbury 6:37b6d0d56190 67 P_TCB p_TCB;
samdanbury 6:37b6d0d56190 68
samdanbury 6:37b6d0d56190 69 while (p_SCB->p_lnk != NULL) {
samdanbury 6:37b6d0d56190 70 /* A task is waiting for token */
samdanbury 6:37b6d0d56190 71 p_TCB = rt_get_first ((P_XCB)p_SCB);
samdanbury 6:37b6d0d56190 72 rt_ret_val(p_TCB, 0);
samdanbury 6:37b6d0d56190 73 rt_rmv_dly(p_TCB);
samdanbury 6:37b6d0d56190 74 p_TCB->state = READY;
samdanbury 6:37b6d0d56190 75 rt_put_prio (&os_rdy, p_TCB);
samdanbury 6:37b6d0d56190 76 }
samdanbury 6:37b6d0d56190 77
samdanbury 6:37b6d0d56190 78 if (os_rdy.p_lnk && (os_rdy.p_lnk->prio > os_tsk.run->prio)) {
samdanbury 6:37b6d0d56190 79 /* preempt running task */
samdanbury 6:37b6d0d56190 80 rt_put_prio (&os_rdy, os_tsk.run);
samdanbury 6:37b6d0d56190 81 os_tsk.run->state = READY;
samdanbury 6:37b6d0d56190 82 rt_dispatch (NULL);
samdanbury 6:37b6d0d56190 83 }
samdanbury 6:37b6d0d56190 84
samdanbury 6:37b6d0d56190 85 p_SCB->cb_type = 0;
samdanbury 6:37b6d0d56190 86
samdanbury 6:37b6d0d56190 87 return (OS_R_OK);
samdanbury 6:37b6d0d56190 88 }
samdanbury 6:37b6d0d56190 89 #endif
samdanbury 6:37b6d0d56190 90
samdanbury 6:37b6d0d56190 91
samdanbury 6:37b6d0d56190 92 /*--------------------------- rt_sem_send -----------------------------------*/
samdanbury 6:37b6d0d56190 93
samdanbury 6:37b6d0d56190 94 OS_RESULT rt_sem_send (OS_ID semaphore) {
samdanbury 6:37b6d0d56190 95 /* Return a token to semaphore */
samdanbury 6:37b6d0d56190 96 P_SCB p_SCB = semaphore;
samdanbury 6:37b6d0d56190 97 P_TCB p_TCB;
samdanbury 6:37b6d0d56190 98
samdanbury 6:37b6d0d56190 99 if (p_SCB->p_lnk != NULL) {
samdanbury 6:37b6d0d56190 100 /* A task is waiting for token */
samdanbury 6:37b6d0d56190 101 p_TCB = rt_get_first ((P_XCB)p_SCB);
samdanbury 6:37b6d0d56190 102 #ifdef __CMSIS_RTOS
samdanbury 6:37b6d0d56190 103 rt_ret_val(p_TCB, 1);
samdanbury 6:37b6d0d56190 104 #else
samdanbury 6:37b6d0d56190 105 rt_ret_val(p_TCB, OS_R_SEM);
samdanbury 6:37b6d0d56190 106 #endif
samdanbury 6:37b6d0d56190 107 rt_rmv_dly (p_TCB);
samdanbury 6:37b6d0d56190 108 rt_dispatch (p_TCB);
samdanbury 6:37b6d0d56190 109 }
samdanbury 6:37b6d0d56190 110 else {
samdanbury 6:37b6d0d56190 111 /* Store token. */
samdanbury 6:37b6d0d56190 112 p_SCB->tokens++;
samdanbury 6:37b6d0d56190 113 }
samdanbury 6:37b6d0d56190 114 return (OS_R_OK);
samdanbury 6:37b6d0d56190 115 }
samdanbury 6:37b6d0d56190 116
samdanbury 6:37b6d0d56190 117
samdanbury 6:37b6d0d56190 118 /*--------------------------- rt_sem_wait -----------------------------------*/
samdanbury 6:37b6d0d56190 119
samdanbury 6:37b6d0d56190 120 OS_RESULT rt_sem_wait (OS_ID semaphore, U16 timeout) {
samdanbury 6:37b6d0d56190 121 /* Obtain a token; possibly wait for it */
samdanbury 6:37b6d0d56190 122 P_SCB p_SCB = semaphore;
samdanbury 6:37b6d0d56190 123
samdanbury 6:37b6d0d56190 124 if (p_SCB->tokens) {
samdanbury 6:37b6d0d56190 125 p_SCB->tokens--;
samdanbury 6:37b6d0d56190 126 return (OS_R_OK);
samdanbury 6:37b6d0d56190 127 }
samdanbury 6:37b6d0d56190 128 /* No token available: wait for one */
samdanbury 6:37b6d0d56190 129 if (timeout == 0) {
samdanbury 6:37b6d0d56190 130 return (OS_R_TMO);
samdanbury 6:37b6d0d56190 131 }
samdanbury 6:37b6d0d56190 132 if (p_SCB->p_lnk != NULL) {
samdanbury 6:37b6d0d56190 133 rt_put_prio ((P_XCB)p_SCB, os_tsk.run);
samdanbury 6:37b6d0d56190 134 }
samdanbury 6:37b6d0d56190 135 else {
samdanbury 6:37b6d0d56190 136 p_SCB->p_lnk = os_tsk.run;
samdanbury 6:37b6d0d56190 137 os_tsk.run->p_lnk = NULL;
samdanbury 6:37b6d0d56190 138 os_tsk.run->p_rlnk = (P_TCB)p_SCB;
samdanbury 6:37b6d0d56190 139 }
samdanbury 6:37b6d0d56190 140 rt_block(timeout, WAIT_SEM);
samdanbury 6:37b6d0d56190 141 return (OS_R_TMO);
samdanbury 6:37b6d0d56190 142 }
samdanbury 6:37b6d0d56190 143
samdanbury 6:37b6d0d56190 144
samdanbury 6:37b6d0d56190 145 /*--------------------------- isr_sem_send ----------------------------------*/
samdanbury 6:37b6d0d56190 146
samdanbury 6:37b6d0d56190 147 void isr_sem_send (OS_ID semaphore) {
samdanbury 6:37b6d0d56190 148 /* Same function as "os_sem"send", but to be called by ISRs */
samdanbury 6:37b6d0d56190 149 P_SCB p_SCB = semaphore;
samdanbury 6:37b6d0d56190 150
samdanbury 6:37b6d0d56190 151 rt_psq_enq (p_SCB, 0);
samdanbury 6:37b6d0d56190 152 rt_psh_req ();
samdanbury 6:37b6d0d56190 153 }
samdanbury 6:37b6d0d56190 154
samdanbury 6:37b6d0d56190 155
samdanbury 6:37b6d0d56190 156 /*--------------------------- rt_sem_psh ------------------------------------*/
samdanbury 6:37b6d0d56190 157
samdanbury 6:37b6d0d56190 158 void rt_sem_psh (P_SCB p_CB) {
samdanbury 6:37b6d0d56190 159 /* Check if task has to be waken up */
samdanbury 6:37b6d0d56190 160 P_TCB p_TCB;
samdanbury 6:37b6d0d56190 161
samdanbury 6:37b6d0d56190 162 if (p_CB->p_lnk != NULL) {
samdanbury 6:37b6d0d56190 163 /* A task is waiting for token */
samdanbury 6:37b6d0d56190 164 p_TCB = rt_get_first ((P_XCB)p_CB);
samdanbury 6:37b6d0d56190 165 rt_rmv_dly (p_TCB);
samdanbury 6:37b6d0d56190 166 p_TCB->state = READY;
samdanbury 6:37b6d0d56190 167 #ifdef __CMSIS_RTOS
samdanbury 6:37b6d0d56190 168 rt_ret_val(p_TCB, 1);
samdanbury 6:37b6d0d56190 169 #else
samdanbury 6:37b6d0d56190 170 rt_ret_val(p_TCB, OS_R_SEM);
samdanbury 6:37b6d0d56190 171 #endif
samdanbury 6:37b6d0d56190 172 rt_put_prio (&os_rdy, p_TCB);
samdanbury 6:37b6d0d56190 173 }
samdanbury 6:37b6d0d56190 174 else {
samdanbury 6:37b6d0d56190 175 /* Store token */
samdanbury 6:37b6d0d56190 176 p_CB->tokens++;
samdanbury 6:37b6d0d56190 177 }
samdanbury 6:37b6d0d56190 178 }
samdanbury 6:37b6d0d56190 179
samdanbury 6:37b6d0d56190 180 /*----------------------------------------------------------------------------
samdanbury 6:37b6d0d56190 181 * end of file
samdanbury 6:37b6d0d56190 182 *---------------------------------------------------------------------------*/
samdanbury 6:37b6d0d56190 183