Official mbed Real Time Operating System based on the RTX implementation of the CMSIS-RTOS API open standard.

Dependents:   denki-yohou_b TestY201 Network-RTOS NTPClient_HelloWorld ... more

Deprecated

This is the mbed 2 rtos library. mbed OS 5 integrates the mbed library with mbed-rtos. With this, we have provided thread safety for all mbed APIs. If you'd like to learn about using mbed OS 5, please see the docs.

Committer:
c1728p9
Date:
Mon Nov 14 17:14:42 2016 -0600
Revision:
123:58563e6cba1e
Parent:
112:53ace74b190c
Configure RTOS to behave as it did before 5.0

Who changed what in which revision?

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