Control a robot over the internet using UDP and a Wifly module (WiFi).

Dependencies:   Motor TextLCD WiflyInterface mbed-rtos mbed

Committer:
apatel336
Date:
Thu Oct 17 13:27:56 2013 +0000
Revision:
0:c0dc3a76f3d4
Initial Release

Who changed what in which revision?

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