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_MEMBOX.C
samdanbury 6:37b6d0d56190 5 * Purpose: Interface functions for fixed memory block management system
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_MemBox.h"
samdanbury 6:37b6d0d56190 39 #include "rt_HAL_CM.h"
samdanbury 6:37b6d0d56190 40
samdanbury 6:37b6d0d56190 41 /*----------------------------------------------------------------------------
samdanbury 6:37b6d0d56190 42 * Global Functions
samdanbury 6:37b6d0d56190 43 *---------------------------------------------------------------------------*/
samdanbury 6:37b6d0d56190 44
samdanbury 6:37b6d0d56190 45
samdanbury 6:37b6d0d56190 46 /*--------------------------- _init_box -------------------------------------*/
samdanbury 6:37b6d0d56190 47
samdanbury 6:37b6d0d56190 48 int _init_box (void *box_mem, U32 box_size, U32 blk_size) {
samdanbury 6:37b6d0d56190 49 /* Initialize memory block system, returns 0 if OK, 1 if fails. */
samdanbury 6:37b6d0d56190 50 void *end;
samdanbury 6:37b6d0d56190 51 void *blk;
samdanbury 6:37b6d0d56190 52 void *next;
samdanbury 6:37b6d0d56190 53 U32 sizeof_bm;
samdanbury 6:37b6d0d56190 54
samdanbury 6:37b6d0d56190 55 /* Create memory structure. */
samdanbury 6:37b6d0d56190 56 if (blk_size & BOX_ALIGN_8) {
samdanbury 6:37b6d0d56190 57 /* Memory blocks 8-byte aligned. */
samdanbury 6:37b6d0d56190 58 blk_size = ((blk_size & ~BOX_ALIGN_8) + 7) & ~7;
samdanbury 6:37b6d0d56190 59 sizeof_bm = (sizeof (struct OS_BM) + 7) & ~7;
samdanbury 6:37b6d0d56190 60 }
samdanbury 6:37b6d0d56190 61 else {
samdanbury 6:37b6d0d56190 62 /* Memory blocks 4-byte aligned. */
samdanbury 6:37b6d0d56190 63 blk_size = (blk_size + 3) & ~3;
samdanbury 6:37b6d0d56190 64 sizeof_bm = sizeof (struct OS_BM);
samdanbury 6:37b6d0d56190 65 }
samdanbury 6:37b6d0d56190 66 if (blk_size == 0) {
samdanbury 6:37b6d0d56190 67 return (1);
samdanbury 6:37b6d0d56190 68 }
samdanbury 6:37b6d0d56190 69 if ((blk_size + sizeof_bm) > box_size) {
samdanbury 6:37b6d0d56190 70 return (1);
samdanbury 6:37b6d0d56190 71 }
samdanbury 6:37b6d0d56190 72 /* Create a Memory structure. */
samdanbury 6:37b6d0d56190 73 blk = ((U8 *) box_mem) + sizeof_bm;
samdanbury 6:37b6d0d56190 74 ((P_BM) box_mem)->free = blk;
samdanbury 6:37b6d0d56190 75 end = ((U8 *) box_mem) + box_size;
samdanbury 6:37b6d0d56190 76 ((P_BM) box_mem)->end = end;
samdanbury 6:37b6d0d56190 77 ((P_BM) box_mem)->blk_size = blk_size;
samdanbury 6:37b6d0d56190 78
samdanbury 6:37b6d0d56190 79 /* Link all free blocks using offsets. */
samdanbury 6:37b6d0d56190 80 end = ((U8 *) end) - blk_size;
samdanbury 6:37b6d0d56190 81 while (1) {
samdanbury 6:37b6d0d56190 82 next = ((U8 *) blk) + blk_size;
samdanbury 6:37b6d0d56190 83 if (next > end) break;
samdanbury 6:37b6d0d56190 84 *((void **)blk) = next;
samdanbury 6:37b6d0d56190 85 blk = next;
samdanbury 6:37b6d0d56190 86 }
samdanbury 6:37b6d0d56190 87 /* end marker */
samdanbury 6:37b6d0d56190 88 *((void **)blk) = 0;
samdanbury 6:37b6d0d56190 89 return (0);
samdanbury 6:37b6d0d56190 90 }
samdanbury 6:37b6d0d56190 91
samdanbury 6:37b6d0d56190 92 /*--------------------------- rt_alloc_box ----------------------------------*/
samdanbury 6:37b6d0d56190 93
samdanbury 6:37b6d0d56190 94 void *rt_alloc_box (void *box_mem) {
samdanbury 6:37b6d0d56190 95 /* Allocate a memory block and return start address. */
samdanbury 6:37b6d0d56190 96 void **free;
samdanbury 6:37b6d0d56190 97 #ifndef __USE_EXCLUSIVE_ACCESS
samdanbury 6:37b6d0d56190 98 int irq_dis;
samdanbury 6:37b6d0d56190 99
samdanbury 6:37b6d0d56190 100 irq_dis = __disable_irq ();
samdanbury 6:37b6d0d56190 101 free = ((P_BM) box_mem)->free;
samdanbury 6:37b6d0d56190 102 if (free) {
samdanbury 6:37b6d0d56190 103 ((P_BM) box_mem)->free = *free;
samdanbury 6:37b6d0d56190 104 }
samdanbury 6:37b6d0d56190 105 if (!irq_dis) __enable_irq ();
samdanbury 6:37b6d0d56190 106 #else
samdanbury 6:37b6d0d56190 107 do {
samdanbury 6:37b6d0d56190 108 if ((free = (void **)__ldrex(&((P_BM) box_mem)->free)) == 0) {
samdanbury 6:37b6d0d56190 109 __clrex();
samdanbury 6:37b6d0d56190 110 break;
samdanbury 6:37b6d0d56190 111 }
samdanbury 6:37b6d0d56190 112 } while (__strex((U32)*free, &((P_BM) box_mem)->free));
samdanbury 6:37b6d0d56190 113 #endif
samdanbury 6:37b6d0d56190 114 return (free);
samdanbury 6:37b6d0d56190 115 }
samdanbury 6:37b6d0d56190 116
samdanbury 6:37b6d0d56190 117
samdanbury 6:37b6d0d56190 118 /*--------------------------- _calloc_box -----------------------------------*/
samdanbury 6:37b6d0d56190 119
samdanbury 6:37b6d0d56190 120 void *_calloc_box (void *box_mem) {
samdanbury 6:37b6d0d56190 121 /* Allocate a 0-initialized memory block and return start address. */
samdanbury 6:37b6d0d56190 122 void *free;
samdanbury 6:37b6d0d56190 123 U32 *p;
samdanbury 6:37b6d0d56190 124 U32 i;
samdanbury 6:37b6d0d56190 125
samdanbury 6:37b6d0d56190 126 free = _alloc_box (box_mem);
samdanbury 6:37b6d0d56190 127 if (free) {
samdanbury 6:37b6d0d56190 128 p = free;
samdanbury 6:37b6d0d56190 129 for (i = ((P_BM) box_mem)->blk_size; i; i -= 4) {
samdanbury 6:37b6d0d56190 130 *p = 0;
samdanbury 6:37b6d0d56190 131 p++;
samdanbury 6:37b6d0d56190 132 }
samdanbury 6:37b6d0d56190 133 }
samdanbury 6:37b6d0d56190 134 return (free);
samdanbury 6:37b6d0d56190 135 }
samdanbury 6:37b6d0d56190 136
samdanbury 6:37b6d0d56190 137
samdanbury 6:37b6d0d56190 138 /*--------------------------- rt_free_box -----------------------------------*/
samdanbury 6:37b6d0d56190 139
samdanbury 6:37b6d0d56190 140 int rt_free_box (void *box_mem, void *box) {
samdanbury 6:37b6d0d56190 141 /* Free a memory block, returns 0 if OK, 1 if box does not belong to box_mem */
samdanbury 6:37b6d0d56190 142 #ifndef __USE_EXCLUSIVE_ACCESS
samdanbury 6:37b6d0d56190 143 int irq_dis;
samdanbury 6:37b6d0d56190 144 #endif
samdanbury 6:37b6d0d56190 145
samdanbury 6:37b6d0d56190 146 if (box < box_mem || box >= ((P_BM) box_mem)->end) {
samdanbury 6:37b6d0d56190 147 return (1);
samdanbury 6:37b6d0d56190 148 }
samdanbury 6:37b6d0d56190 149
samdanbury 6:37b6d0d56190 150 #ifndef __USE_EXCLUSIVE_ACCESS
samdanbury 6:37b6d0d56190 151 irq_dis = __disable_irq ();
samdanbury 6:37b6d0d56190 152 *((void **)box) = ((P_BM) box_mem)->free;
samdanbury 6:37b6d0d56190 153 ((P_BM) box_mem)->free = box;
samdanbury 6:37b6d0d56190 154 if (!irq_dis) __enable_irq ();
samdanbury 6:37b6d0d56190 155 #else
samdanbury 6:37b6d0d56190 156 do {
samdanbury 6:37b6d0d56190 157 *((void **)box) = (void *)__ldrex(&((P_BM) box_mem)->free);
samdanbury 6:37b6d0d56190 158 } while (__strex ((U32)box, &((P_BM) box_mem)->free));
samdanbury 6:37b6d0d56190 159 #endif
samdanbury 6:37b6d0d56190 160 return (0);
samdanbury 6:37b6d0d56190 161 }
samdanbury 6:37b6d0d56190 162
samdanbury 6:37b6d0d56190 163 /*----------------------------------------------------------------------------
samdanbury 6:37b6d0d56190 164 * end of file
samdanbury 6:37b6d0d56190 165 *---------------------------------------------------------------------------*/
samdanbury 6:37b6d0d56190 166