ソースの整理中ですが、利用はできます。

Dependencies:   EthernetInterface HttpServer TextLCD mbed-rpc mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Committer:
yueee_yt
Date:
Wed Mar 12 04:39:15 2014 +0000
Revision:
2:14b689a85306
Parent:
0:7766f6712673
bug fix

Who changed what in which revision?

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