Lab Checkoff

Dependencies:   SDFileSystem TextLCD mbed-rtos mbed wave_player FATFileSystem

Committer:
doubster
Date:
Wed Nov 13 20:00:28 2013 +0000
Revision:
0:67dbd54e60d4
Lab Checkoff

Who changed what in which revision?

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