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