mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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