desp koval / mbed-rtos

Dependents:   Mecatro_Filtre

Committer:
mecatro_prod
Date:
Mon Mar 15 14:18:11 2021 +0000
Revision:
0:4a2c9c1f5b9e
Pas de changement

Who changed what in which revision?

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