skeleton for lab1

Dependencies:   AvailableMemory mbed-rtos mbed

Fork of helloaabbc by 32314 mbed

Committer:
y7jin
Date:
Thu Apr 03 22:56:32 2014 +0000
Revision:
0:1c8f2727e9f5
hello

Who changed what in which revision?

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