.

Dependencies:   mbed-rtos

Dependents:   Glove

Fork of mbed-rtos by mbed official

Committer:
jboettcher
Date:
Wed Dec 07 05:05:18 2016 +0000
Revision:
124:62fd81674a5d
Parent:
80:2dab120a94c2
.

Who changed what in which revision?

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