NRF: receive, ntp, Data to Sd-card working

Dependencies:   F7_Ethernet mbed BSP_DISCO_F746NG Test_Mainboard SDFileSystem RF24

Committer:
lowlowry
Date:
Sun Jun 20 13:48:06 2021 +0000
Revision:
4:5ecb71f149bf
Parent:
0:d984976f1f1c
NRF: receive, ntp, Data to Sd-card working

Who changed what in which revision?

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