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_SYSTEM.C
samdanbury 6:37b6d0d56190 5 * Purpose: System Task Manager
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_Task.h"
samdanbury 6:37b6d0d56190 38 #include "rt_System.h"
samdanbury 6:37b6d0d56190 39 #include "rt_Event.h"
samdanbury 6:37b6d0d56190 40 #include "rt_List.h"
samdanbury 6:37b6d0d56190 41 #include "rt_Mailbox.h"
samdanbury 6:37b6d0d56190 42 #include "rt_Semaphore.h"
samdanbury 6:37b6d0d56190 43 #include "rt_Time.h"
samdanbury 6:37b6d0d56190 44 #include "rt_Robin.h"
samdanbury 6:37b6d0d56190 45 #include "rt_HAL_CM.h"
samdanbury 6:37b6d0d56190 46
samdanbury 6:37b6d0d56190 47 /*----------------------------------------------------------------------------
samdanbury 6:37b6d0d56190 48 * Global Variables
samdanbury 6:37b6d0d56190 49 *---------------------------------------------------------------------------*/
samdanbury 6:37b6d0d56190 50
samdanbury 6:37b6d0d56190 51 int os_tick_irqn;
samdanbury 6:37b6d0d56190 52
samdanbury 6:37b6d0d56190 53 /*----------------------------------------------------------------------------
samdanbury 6:37b6d0d56190 54 * Local Variables
samdanbury 6:37b6d0d56190 55 *---------------------------------------------------------------------------*/
samdanbury 6:37b6d0d56190 56
samdanbury 6:37b6d0d56190 57 static volatile BIT os_lock;
samdanbury 6:37b6d0d56190 58 static volatile BIT os_psh_flag;
samdanbury 6:37b6d0d56190 59 static U8 pend_flags;
samdanbury 6:37b6d0d56190 60
samdanbury 6:37b6d0d56190 61 /*----------------------------------------------------------------------------
samdanbury 6:37b6d0d56190 62 * Global Functions
samdanbury 6:37b6d0d56190 63 *---------------------------------------------------------------------------*/
samdanbury 6:37b6d0d56190 64
samdanbury 6:37b6d0d56190 65 #if defined (__CC_ARM)
samdanbury 6:37b6d0d56190 66 __asm void $$RTX$$version (void) {
samdanbury 6:37b6d0d56190 67 /* Export a version number symbol for a version control. */
samdanbury 6:37b6d0d56190 68
samdanbury 6:37b6d0d56190 69 EXPORT __RL_RTX_VER
samdanbury 6:37b6d0d56190 70
samdanbury 6:37b6d0d56190 71 __RL_RTX_VER EQU 0x450
samdanbury 6:37b6d0d56190 72 }
samdanbury 6:37b6d0d56190 73 #endif
samdanbury 6:37b6d0d56190 74
samdanbury 6:37b6d0d56190 75
samdanbury 6:37b6d0d56190 76 /*--------------------------- rt_suspend ------------------------------------*/
samdanbury 6:37b6d0d56190 77 U32 rt_suspend (void) {
samdanbury 6:37b6d0d56190 78 /* Suspend OS scheduler */
samdanbury 6:37b6d0d56190 79 U32 delta = 0xFFFF;
samdanbury 6:37b6d0d56190 80
samdanbury 6:37b6d0d56190 81 rt_tsk_lock();
samdanbury 6:37b6d0d56190 82
samdanbury 6:37b6d0d56190 83 if (os_dly.p_dlnk) {
samdanbury 6:37b6d0d56190 84 delta = os_dly.delta_time;
samdanbury 6:37b6d0d56190 85 }
samdanbury 6:37b6d0d56190 86 #ifndef __CMSIS_RTOS
samdanbury 6:37b6d0d56190 87 if (os_tmr.next) {
samdanbury 6:37b6d0d56190 88 if (os_tmr.tcnt < delta) delta = os_tmr.tcnt;
samdanbury 6:37b6d0d56190 89 }
samdanbury 6:37b6d0d56190 90 #endif
samdanbury 6:37b6d0d56190 91
samdanbury 6:37b6d0d56190 92 return (delta);
samdanbury 6:37b6d0d56190 93 }
samdanbury 6:37b6d0d56190 94
samdanbury 6:37b6d0d56190 95
samdanbury 6:37b6d0d56190 96 /*--------------------------- rt_resume -------------------------------------*/
samdanbury 6:37b6d0d56190 97 void rt_resume (U32 sleep_time) {
samdanbury 6:37b6d0d56190 98 /* Resume OS scheduler after suspend */
samdanbury 6:37b6d0d56190 99 P_TCB next;
samdanbury 6:37b6d0d56190 100 U32 delta;
samdanbury 6:37b6d0d56190 101
samdanbury 6:37b6d0d56190 102 os_tsk.run->state = READY;
samdanbury 6:37b6d0d56190 103 rt_put_rdy_first (os_tsk.run);
samdanbury 6:37b6d0d56190 104
samdanbury 6:37b6d0d56190 105 os_robin.task = NULL;
samdanbury 6:37b6d0d56190 106
samdanbury 6:37b6d0d56190 107 /* Update delays. */
samdanbury 6:37b6d0d56190 108 if (os_dly.p_dlnk) {
samdanbury 6:37b6d0d56190 109 delta = sleep_time;
samdanbury 6:37b6d0d56190 110 if (delta >= os_dly.delta_time) {
samdanbury 6:37b6d0d56190 111 delta -= os_dly.delta_time;
samdanbury 6:37b6d0d56190 112 os_time += os_dly.delta_time;
samdanbury 6:37b6d0d56190 113 os_dly.delta_time = 1;
samdanbury 6:37b6d0d56190 114 while (os_dly.p_dlnk) {
samdanbury 6:37b6d0d56190 115 rt_dec_dly();
samdanbury 6:37b6d0d56190 116 if (delta == 0) break;
samdanbury 6:37b6d0d56190 117 delta--;
samdanbury 6:37b6d0d56190 118 os_time++;
samdanbury 6:37b6d0d56190 119 }
samdanbury 6:37b6d0d56190 120 } else {
samdanbury 6:37b6d0d56190 121 os_time += delta;
samdanbury 6:37b6d0d56190 122 os_dly.delta_time -= delta;
samdanbury 6:37b6d0d56190 123 }
samdanbury 6:37b6d0d56190 124 } else {
samdanbury 6:37b6d0d56190 125 os_time += sleep_time;
samdanbury 6:37b6d0d56190 126 }
samdanbury 6:37b6d0d56190 127
samdanbury 6:37b6d0d56190 128 #ifndef __CMSIS_RTOS
samdanbury 6:37b6d0d56190 129 /* Check the user timers. */
samdanbury 6:37b6d0d56190 130 if (os_tmr.next) {
samdanbury 6:37b6d0d56190 131 delta = sleep_time;
samdanbury 6:37b6d0d56190 132 if (delta >= os_tmr.tcnt) {
samdanbury 6:37b6d0d56190 133 delta -= os_tmr.tcnt;
samdanbury 6:37b6d0d56190 134 os_tmr.tcnt = 1;
samdanbury 6:37b6d0d56190 135 while (os_tmr.next) {
samdanbury 6:37b6d0d56190 136 rt_tmr_tick();
samdanbury 6:37b6d0d56190 137 if (delta == 0) break;
samdanbury 6:37b6d0d56190 138 delta--;
samdanbury 6:37b6d0d56190 139 }
samdanbury 6:37b6d0d56190 140 } else {
samdanbury 6:37b6d0d56190 141 os_tmr.tcnt -= delta;
samdanbury 6:37b6d0d56190 142 }
samdanbury 6:37b6d0d56190 143 }
samdanbury 6:37b6d0d56190 144 #endif
samdanbury 6:37b6d0d56190 145
samdanbury 6:37b6d0d56190 146 /* Switch back to highest ready task */
samdanbury 6:37b6d0d56190 147 next = rt_get_first (&os_rdy);
samdanbury 6:37b6d0d56190 148 rt_switch_req (next);
samdanbury 6:37b6d0d56190 149
samdanbury 6:37b6d0d56190 150 rt_tsk_unlock();
samdanbury 6:37b6d0d56190 151 }
samdanbury 6:37b6d0d56190 152
samdanbury 6:37b6d0d56190 153
samdanbury 6:37b6d0d56190 154 /*--------------------------- rt_tsk_lock -----------------------------------*/
samdanbury 6:37b6d0d56190 155
samdanbury 6:37b6d0d56190 156 void rt_tsk_lock (void) {
samdanbury 6:37b6d0d56190 157 /* Prevent task switching by locking out scheduler */
samdanbury 6:37b6d0d56190 158 if (os_tick_irqn < 0) {
samdanbury 6:37b6d0d56190 159 OS_LOCK();
samdanbury 6:37b6d0d56190 160 os_lock = __TRUE;
samdanbury 6:37b6d0d56190 161 OS_UNPEND (&pend_flags);
samdanbury 6:37b6d0d56190 162 } else {
samdanbury 6:37b6d0d56190 163 OS_X_LOCK(os_tick_irqn);
samdanbury 6:37b6d0d56190 164 os_lock = __TRUE;
samdanbury 6:37b6d0d56190 165 OS_X_UNPEND (&pend_flags);
samdanbury 6:37b6d0d56190 166 }
samdanbury 6:37b6d0d56190 167 }
samdanbury 6:37b6d0d56190 168
samdanbury 6:37b6d0d56190 169
samdanbury 6:37b6d0d56190 170 /*--------------------------- rt_tsk_unlock ---------------------------------*/
samdanbury 6:37b6d0d56190 171
samdanbury 6:37b6d0d56190 172 void rt_tsk_unlock (void) {
samdanbury 6:37b6d0d56190 173 /* Unlock scheduler and re-enable task switching */
samdanbury 6:37b6d0d56190 174 if (os_tick_irqn < 0) {
samdanbury 6:37b6d0d56190 175 OS_UNLOCK();
samdanbury 6:37b6d0d56190 176 os_lock = __FALSE;
samdanbury 6:37b6d0d56190 177 OS_PEND (pend_flags, os_psh_flag);
samdanbury 6:37b6d0d56190 178 os_psh_flag = __FALSE;
samdanbury 6:37b6d0d56190 179 } else {
samdanbury 6:37b6d0d56190 180 OS_X_UNLOCK(os_tick_irqn);
samdanbury 6:37b6d0d56190 181 os_lock = __FALSE;
samdanbury 6:37b6d0d56190 182 OS_X_PEND (pend_flags, os_psh_flag);
samdanbury 6:37b6d0d56190 183 os_psh_flag = __FALSE;
samdanbury 6:37b6d0d56190 184 }
samdanbury 6:37b6d0d56190 185 }
samdanbury 6:37b6d0d56190 186
samdanbury 6:37b6d0d56190 187
samdanbury 6:37b6d0d56190 188 /*--------------------------- rt_psh_req ------------------------------------*/
samdanbury 6:37b6d0d56190 189
samdanbury 6:37b6d0d56190 190 void rt_psh_req (void) {
samdanbury 6:37b6d0d56190 191 /* Initiate a post service handling request if required. */
samdanbury 6:37b6d0d56190 192 if (os_lock == __FALSE) {
samdanbury 6:37b6d0d56190 193 OS_PEND_IRQ ();
samdanbury 6:37b6d0d56190 194 }
samdanbury 6:37b6d0d56190 195 else {
samdanbury 6:37b6d0d56190 196 os_psh_flag = __TRUE;
samdanbury 6:37b6d0d56190 197 }
samdanbury 6:37b6d0d56190 198 }
samdanbury 6:37b6d0d56190 199
samdanbury 6:37b6d0d56190 200
samdanbury 6:37b6d0d56190 201 /*--------------------------- rt_pop_req ------------------------------------*/
samdanbury 6:37b6d0d56190 202
samdanbury 6:37b6d0d56190 203 void rt_pop_req (void) {
samdanbury 6:37b6d0d56190 204 /* Process an ISR post service requests. */
samdanbury 6:37b6d0d56190 205 struct OS_XCB *p_CB;
samdanbury 6:37b6d0d56190 206 P_TCB next;
samdanbury 6:37b6d0d56190 207 U32 idx;
samdanbury 6:37b6d0d56190 208
samdanbury 6:37b6d0d56190 209 os_tsk.run->state = READY;
samdanbury 6:37b6d0d56190 210 rt_put_rdy_first (os_tsk.run);
samdanbury 6:37b6d0d56190 211
samdanbury 6:37b6d0d56190 212 idx = os_psq->last;
samdanbury 6:37b6d0d56190 213 while (os_psq->count) {
samdanbury 6:37b6d0d56190 214 p_CB = os_psq->q[idx].id;
samdanbury 6:37b6d0d56190 215 if (p_CB->cb_type == TCB) {
samdanbury 6:37b6d0d56190 216 /* Is of TCB type */
samdanbury 6:37b6d0d56190 217 rt_evt_psh ((P_TCB)p_CB, (U16)os_psq->q[idx].arg);
samdanbury 6:37b6d0d56190 218 }
samdanbury 6:37b6d0d56190 219 else if (p_CB->cb_type == MCB) {
samdanbury 6:37b6d0d56190 220 /* Is of MCB type */
samdanbury 6:37b6d0d56190 221 rt_mbx_psh ((P_MCB)p_CB, (void *)os_psq->q[idx].arg);
samdanbury 6:37b6d0d56190 222 }
samdanbury 6:37b6d0d56190 223 else {
samdanbury 6:37b6d0d56190 224 /* Must be of SCB type */
samdanbury 6:37b6d0d56190 225 rt_sem_psh ((P_SCB)p_CB);
samdanbury 6:37b6d0d56190 226 }
samdanbury 6:37b6d0d56190 227 if (++idx == os_psq->size) idx = 0;
samdanbury 6:37b6d0d56190 228 rt_dec (&os_psq->count);
samdanbury 6:37b6d0d56190 229 }
samdanbury 6:37b6d0d56190 230 os_psq->last = idx;
samdanbury 6:37b6d0d56190 231
samdanbury 6:37b6d0d56190 232 next = rt_get_first (&os_rdy);
samdanbury 6:37b6d0d56190 233 rt_switch_req (next);
samdanbury 6:37b6d0d56190 234 }
samdanbury 6:37b6d0d56190 235
samdanbury 6:37b6d0d56190 236
samdanbury 6:37b6d0d56190 237 /*--------------------------- os_tick_init ----------------------------------*/
samdanbury 6:37b6d0d56190 238
samdanbury 6:37b6d0d56190 239 __weak int os_tick_init (void) {
samdanbury 6:37b6d0d56190 240 /* Initialize SysTick timer as system tick timer. */
samdanbury 6:37b6d0d56190 241 rt_systick_init ();
samdanbury 6:37b6d0d56190 242 return (-1); /* Return IRQ number of SysTick timer */
samdanbury 6:37b6d0d56190 243 }
samdanbury 6:37b6d0d56190 244
samdanbury 6:37b6d0d56190 245
samdanbury 6:37b6d0d56190 246 /*--------------------------- os_tick_irqack --------------------------------*/
samdanbury 6:37b6d0d56190 247
samdanbury 6:37b6d0d56190 248 __weak void os_tick_irqack (void) {
samdanbury 6:37b6d0d56190 249 /* Acknowledge timer interrupt. */
samdanbury 6:37b6d0d56190 250 }
samdanbury 6:37b6d0d56190 251
samdanbury 6:37b6d0d56190 252
samdanbury 6:37b6d0d56190 253 /*--------------------------- rt_systick ------------------------------------*/
samdanbury 6:37b6d0d56190 254
samdanbury 6:37b6d0d56190 255 extern void sysTimerTick(void);
samdanbury 6:37b6d0d56190 256
samdanbury 6:37b6d0d56190 257 void rt_systick (void) {
samdanbury 6:37b6d0d56190 258 /* Check for system clock update, suspend running task. */
samdanbury 6:37b6d0d56190 259 P_TCB next;
samdanbury 6:37b6d0d56190 260
samdanbury 6:37b6d0d56190 261 os_tsk.run->state = READY;
samdanbury 6:37b6d0d56190 262 rt_put_rdy_first (os_tsk.run);
samdanbury 6:37b6d0d56190 263
samdanbury 6:37b6d0d56190 264 /* Check Round Robin timeout. */
samdanbury 6:37b6d0d56190 265 rt_chk_robin ();
samdanbury 6:37b6d0d56190 266
samdanbury 6:37b6d0d56190 267 /* Update delays. */
samdanbury 6:37b6d0d56190 268 os_time++;
samdanbury 6:37b6d0d56190 269 rt_dec_dly ();
samdanbury 6:37b6d0d56190 270
samdanbury 6:37b6d0d56190 271 /* Check the user timers. */
samdanbury 6:37b6d0d56190 272 #ifdef __CMSIS_RTOS
samdanbury 6:37b6d0d56190 273 sysTimerTick();
samdanbury 6:37b6d0d56190 274 #else
samdanbury 6:37b6d0d56190 275 rt_tmr_tick ();
samdanbury 6:37b6d0d56190 276 #endif
samdanbury 6:37b6d0d56190 277
samdanbury 6:37b6d0d56190 278 /* Switch back to highest ready task */
samdanbury 6:37b6d0d56190 279 next = rt_get_first (&os_rdy);
samdanbury 6:37b6d0d56190 280 rt_switch_req (next);
samdanbury 6:37b6d0d56190 281 }
samdanbury 6:37b6d0d56190 282
samdanbury 6:37b6d0d56190 283 /*--------------------------- rt_stk_check ----------------------------------*/
samdanbury 6:37b6d0d56190 284 __weak void rt_stk_check (void) {
samdanbury 6:37b6d0d56190 285 /* Check for stack overflow. */
samdanbury 6:37b6d0d56190 286 if (os_tsk.run->task_id == 0x01) {
samdanbury 6:37b6d0d56190 287 // TODO: For the main thread the check should be done against the main heap pointer
samdanbury 6:37b6d0d56190 288 } else {
samdanbury 6:37b6d0d56190 289 if ((os_tsk.run->tsk_stack < (U32)os_tsk.run->stack) ||
samdanbury 6:37b6d0d56190 290 (os_tsk.run->stack[0] != MAGIC_WORD)) {
samdanbury 6:37b6d0d56190 291 os_error (OS_ERR_STK_OVF);
samdanbury 6:37b6d0d56190 292 }
samdanbury 6:37b6d0d56190 293 }
samdanbury 6:37b6d0d56190 294 }
samdanbury 6:37b6d0d56190 295
samdanbury 6:37b6d0d56190 296 /*----------------------------------------------------------------------------
samdanbury 6:37b6d0d56190 297 * end of file
samdanbury 6:37b6d0d56190 298 *---------------------------------------------------------------------------*/
samdanbury 6:37b6d0d56190 299