mbed client lightswitch demo

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by Austin Blackstone

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 11:cada08fc8a70 1 /*----------------------------------------------------------------------------
mbedAustin 11:cada08fc8a70 2 * RL-ARM - RTX
mbedAustin 11:cada08fc8a70 3 *----------------------------------------------------------------------------
mbedAustin 11:cada08fc8a70 4 * Name: RT_MEMBOX.C
mbedAustin 11:cada08fc8a70 5 * Purpose: Interface functions for fixed memory block management system
mbedAustin 11:cada08fc8a70 6 * Rev.: V4.60
mbedAustin 11:cada08fc8a70 7 *----------------------------------------------------------------------------
mbedAustin 11:cada08fc8a70 8 *
mbedAustin 11:cada08fc8a70 9 * Copyright (c) 1999-2009 KEIL, 2009-2015 ARM Germany GmbH
mbedAustin 11:cada08fc8a70 10 * All rights reserved.
mbedAustin 11:cada08fc8a70 11 * Redistribution and use in source and binary forms, with or without
mbedAustin 11:cada08fc8a70 12 * modification, are permitted provided that the following conditions are met:
mbedAustin 11:cada08fc8a70 13 * - Redistributions of source code must retain the above copyright
mbedAustin 11:cada08fc8a70 14 * notice, this list of conditions and the following disclaimer.
mbedAustin 11:cada08fc8a70 15 * - Redistributions in binary form must reproduce the above copyright
mbedAustin 11:cada08fc8a70 16 * notice, this list of conditions and the following disclaimer in the
mbedAustin 11:cada08fc8a70 17 * documentation and/or other materials provided with the distribution.
mbedAustin 11:cada08fc8a70 18 * - Neither the name of ARM nor the names of its contributors may be used
mbedAustin 11:cada08fc8a70 19 * to endorse or promote products derived from this software without
mbedAustin 11:cada08fc8a70 20 * specific prior written permission.
mbedAustin 11:cada08fc8a70 21 *
mbedAustin 11:cada08fc8a70 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbedAustin 11:cada08fc8a70 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbedAustin 11:cada08fc8a70 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
mbedAustin 11:cada08fc8a70 25 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
mbedAustin 11:cada08fc8a70 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
mbedAustin 11:cada08fc8a70 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
mbedAustin 11:cada08fc8a70 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
mbedAustin 11:cada08fc8a70 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
mbedAustin 11:cada08fc8a70 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
mbedAustin 11:cada08fc8a70 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
mbedAustin 11:cada08fc8a70 32 * POSSIBILITY OF SUCH DAMAGE.
mbedAustin 11:cada08fc8a70 33 *---------------------------------------------------------------------------*/
mbedAustin 11:cada08fc8a70 34
mbedAustin 11:cada08fc8a70 35 #include "rt_TypeDef.h"
mbedAustin 11:cada08fc8a70 36 #include "RTX_Conf.h"
mbedAustin 11:cada08fc8a70 37 #include "rt_System.h"
mbedAustin 11:cada08fc8a70 38 #include "rt_MemBox.h"
mbedAustin 11:cada08fc8a70 39 #include "rt_HAL_CM.h"
mbedAustin 11:cada08fc8a70 40
mbedAustin 11:cada08fc8a70 41 /*----------------------------------------------------------------------------
mbedAustin 11:cada08fc8a70 42 * Global Functions
mbedAustin 11:cada08fc8a70 43 *---------------------------------------------------------------------------*/
mbedAustin 11:cada08fc8a70 44
mbedAustin 11:cada08fc8a70 45
mbedAustin 11:cada08fc8a70 46 /*--------------------------- _init_box -------------------------------------*/
mbedAustin 11:cada08fc8a70 47
mbedAustin 11:cada08fc8a70 48 int _init_box (void *box_mem, U32 box_size, U32 blk_size) {
mbedAustin 11:cada08fc8a70 49 /* Initialize memory block system, returns 0 if OK, 1 if fails. */
mbedAustin 11:cada08fc8a70 50 void *end;
mbedAustin 11:cada08fc8a70 51 void *blk;
mbedAustin 11:cada08fc8a70 52 void *next;
mbedAustin 11:cada08fc8a70 53 U32 sizeof_bm;
mbedAustin 11:cada08fc8a70 54
mbedAustin 11:cada08fc8a70 55 /* Create memory structure. */
mbedAustin 11:cada08fc8a70 56 if (blk_size & BOX_ALIGN_8) {
mbedAustin 11:cada08fc8a70 57 /* Memory blocks 8-byte aligned. */
mbedAustin 11:cada08fc8a70 58 blk_size = ((blk_size & ~BOX_ALIGN_8) + 7) & ~7;
mbedAustin 11:cada08fc8a70 59 sizeof_bm = (sizeof (struct OS_BM) + 7) & ~7;
mbedAustin 11:cada08fc8a70 60 }
mbedAustin 11:cada08fc8a70 61 else {
mbedAustin 11:cada08fc8a70 62 /* Memory blocks 4-byte aligned. */
mbedAustin 11:cada08fc8a70 63 blk_size = (blk_size + 3) & ~3;
mbedAustin 11:cada08fc8a70 64 sizeof_bm = sizeof (struct OS_BM);
mbedAustin 11:cada08fc8a70 65 }
mbedAustin 11:cada08fc8a70 66 if (blk_size == 0) {
mbedAustin 11:cada08fc8a70 67 return (1);
mbedAustin 11:cada08fc8a70 68 }
mbedAustin 11:cada08fc8a70 69 if ((blk_size + sizeof_bm) > box_size) {
mbedAustin 11:cada08fc8a70 70 return (1);
mbedAustin 11:cada08fc8a70 71 }
mbedAustin 11:cada08fc8a70 72 /* Create a Memory structure. */
mbedAustin 11:cada08fc8a70 73 blk = ((U8 *) box_mem) + sizeof_bm;
mbedAustin 11:cada08fc8a70 74 ((P_BM) box_mem)->free = blk;
mbedAustin 11:cada08fc8a70 75 end = ((U8 *) box_mem) + box_size;
mbedAustin 11:cada08fc8a70 76 ((P_BM) box_mem)->end = end;
mbedAustin 11:cada08fc8a70 77 ((P_BM) box_mem)->blk_size = blk_size;
mbedAustin 11:cada08fc8a70 78
mbedAustin 11:cada08fc8a70 79 /* Link all free blocks using offsets. */
mbedAustin 11:cada08fc8a70 80 end = ((U8 *) end) - blk_size;
mbedAustin 11:cada08fc8a70 81 while (1) {
mbedAustin 11:cada08fc8a70 82 next = ((U8 *) blk) + blk_size;
mbedAustin 11:cada08fc8a70 83 if (next > end) break;
mbedAustin 11:cada08fc8a70 84 *((void **)blk) = next;
mbedAustin 11:cada08fc8a70 85 blk = next;
mbedAustin 11:cada08fc8a70 86 }
mbedAustin 11:cada08fc8a70 87 /* end marker */
mbedAustin 11:cada08fc8a70 88 *((void **)blk) = 0;
mbedAustin 11:cada08fc8a70 89 return (0);
mbedAustin 11:cada08fc8a70 90 }
mbedAustin 11:cada08fc8a70 91
mbedAustin 11:cada08fc8a70 92 /*--------------------------- rt_alloc_box ----------------------------------*/
mbedAustin 11:cada08fc8a70 93
mbedAustin 11:cada08fc8a70 94 void *rt_alloc_box (void *box_mem) {
mbedAustin 11:cada08fc8a70 95 /* Allocate a memory block and return start address. */
mbedAustin 11:cada08fc8a70 96 void **free;
mbedAustin 11:cada08fc8a70 97 #ifndef __USE_EXCLUSIVE_ACCESS
mbedAustin 11:cada08fc8a70 98 int irq_dis;
mbedAustin 11:cada08fc8a70 99
mbedAustin 11:cada08fc8a70 100 irq_dis = __disable_irq ();
mbedAustin 11:cada08fc8a70 101 free = ((P_BM) box_mem)->free;
mbedAustin 11:cada08fc8a70 102 if (free) {
mbedAustin 11:cada08fc8a70 103 ((P_BM) box_mem)->free = *free;
mbedAustin 11:cada08fc8a70 104 }
mbedAustin 11:cada08fc8a70 105 if (!irq_dis) __enable_irq ();
mbedAustin 11:cada08fc8a70 106 #else
mbedAustin 11:cada08fc8a70 107 do {
mbedAustin 11:cada08fc8a70 108 if ((free = (void **)__ldrex(&((P_BM) box_mem)->free)) == 0) {
mbedAustin 11:cada08fc8a70 109 __clrex();
mbedAustin 11:cada08fc8a70 110 break;
mbedAustin 11:cada08fc8a70 111 }
mbedAustin 11:cada08fc8a70 112 } while (__strex((U32)*free, &((P_BM) box_mem)->free));
mbedAustin 11:cada08fc8a70 113 #endif
mbedAustin 11:cada08fc8a70 114 return (free);
mbedAustin 11:cada08fc8a70 115 }
mbedAustin 11:cada08fc8a70 116
mbedAustin 11:cada08fc8a70 117
mbedAustin 11:cada08fc8a70 118 /*--------------------------- _calloc_box -----------------------------------*/
mbedAustin 11:cada08fc8a70 119
mbedAustin 11:cada08fc8a70 120 void *_calloc_box (void *box_mem) {
mbedAustin 11:cada08fc8a70 121 /* Allocate a 0-initialized memory block and return start address. */
mbedAustin 11:cada08fc8a70 122 void *free;
mbedAustin 11:cada08fc8a70 123 U32 *p;
mbedAustin 11:cada08fc8a70 124 U32 i;
mbedAustin 11:cada08fc8a70 125
mbedAustin 11:cada08fc8a70 126 free = _alloc_box (box_mem);
mbedAustin 11:cada08fc8a70 127 if (free) {
mbedAustin 11:cada08fc8a70 128 p = free;
mbedAustin 11:cada08fc8a70 129 for (i = ((P_BM) box_mem)->blk_size; i; i -= 4) {
mbedAustin 11:cada08fc8a70 130 *p = 0;
mbedAustin 11:cada08fc8a70 131 p++;
mbedAustin 11:cada08fc8a70 132 }
mbedAustin 11:cada08fc8a70 133 }
mbedAustin 11:cada08fc8a70 134 return (free);
mbedAustin 11:cada08fc8a70 135 }
mbedAustin 11:cada08fc8a70 136
mbedAustin 11:cada08fc8a70 137
mbedAustin 11:cada08fc8a70 138 /*--------------------------- rt_free_box -----------------------------------*/
mbedAustin 11:cada08fc8a70 139
mbedAustin 11:cada08fc8a70 140 int rt_free_box (void *box_mem, void *box) {
mbedAustin 11:cada08fc8a70 141 /* Free a memory block, returns 0 if OK, 1 if box does not belong to box_mem */
mbedAustin 11:cada08fc8a70 142 #ifndef __USE_EXCLUSIVE_ACCESS
mbedAustin 11:cada08fc8a70 143 int irq_dis;
mbedAustin 11:cada08fc8a70 144 #endif
mbedAustin 11:cada08fc8a70 145
mbedAustin 11:cada08fc8a70 146 if (box < box_mem || box >= ((P_BM) box_mem)->end) {
mbedAustin 11:cada08fc8a70 147 return (1);
mbedAustin 11:cada08fc8a70 148 }
mbedAustin 11:cada08fc8a70 149
mbedAustin 11:cada08fc8a70 150 #ifndef __USE_EXCLUSIVE_ACCESS
mbedAustin 11:cada08fc8a70 151 irq_dis = __disable_irq ();
mbedAustin 11:cada08fc8a70 152 *((void **)box) = ((P_BM) box_mem)->free;
mbedAustin 11:cada08fc8a70 153 ((P_BM) box_mem)->free = box;
mbedAustin 11:cada08fc8a70 154 if (!irq_dis) __enable_irq ();
mbedAustin 11:cada08fc8a70 155 #else
mbedAustin 11:cada08fc8a70 156 do {
mbedAustin 11:cada08fc8a70 157 *((void **)box) = (void *)__ldrex(&((P_BM) box_mem)->free);
mbedAustin 11:cada08fc8a70 158 } while (__strex ((U32)box, &((P_BM) box_mem)->free));
mbedAustin 11:cada08fc8a70 159 #endif
mbedAustin 11:cada08fc8a70 160 return (0);
mbedAustin 11:cada08fc8a70 161 }
mbedAustin 11:cada08fc8a70 162
mbedAustin 11:cada08fc8a70 163 /*----------------------------------------------------------------------------
mbedAustin 11:cada08fc8a70 164 * end of file
mbedAustin 11:cada08fc8a70 165 *---------------------------------------------------------------------------*/
mbedAustin 11:cada08fc8a70 166