Pierre Provent / USBHost

Dependents:   TEST_USB_Nucleo_F429ZI Essais_USB_Nucleo_F429ZI SID_V3_Nucleo_F429ZI SID_V4_Nucleo_F429ZI_copy

Committer:
pierreprovent
Date:
Fri Sep 25 10:17:49 2020 +0000
Revision:
0:77ca32e8e04e
Programme acquisition en enregistrement sur clef USB carte Nucleo F429ZI cours ELE118 Cnam

Who changed what in which revision?

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