Repostiory containing DAPLink source code with Reset Pin workaround for HANI_IOT board.

Upstream: https://github.com/ARMmbed/DAPLink

Committer:
Pawel Zarembski
Date:
Tue Apr 07 12:55:42 2020 +0200
Revision:
0:01f31e923fe2
hani: DAPLink with reset workaround

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pawel Zarembski 0:01f31e923fe2 1 /**
Pawel Zarembski 0:01f31e923fe2 2 * @file rt_MemBox.c
Pawel Zarembski 0:01f31e923fe2 3 * @brief
Pawel Zarembski 0:01f31e923fe2 4 *
Pawel Zarembski 0:01f31e923fe2 5 * DAPLink Interface Firmware
Pawel Zarembski 0:01f31e923fe2 6 * Copyright (c) 2009-2016, ARM Limited, All Rights Reserved
Pawel Zarembski 0:01f31e923fe2 7 * SPDX-License-Identifier: Apache-2.0
Pawel Zarembski 0:01f31e923fe2 8 *
Pawel Zarembski 0:01f31e923fe2 9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Pawel Zarembski 0:01f31e923fe2 10 * not use this file except in compliance with the License.
Pawel Zarembski 0:01f31e923fe2 11 * You may obtain a copy of the License at
Pawel Zarembski 0:01f31e923fe2 12 *
Pawel Zarembski 0:01f31e923fe2 13 * http://www.apache.org/licenses/LICENSE-2.0
Pawel Zarembski 0:01f31e923fe2 14 *
Pawel Zarembski 0:01f31e923fe2 15 * Unless required by applicable law or agreed to in writing, software
Pawel Zarembski 0:01f31e923fe2 16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
Pawel Zarembski 0:01f31e923fe2 17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Pawel Zarembski 0:01f31e923fe2 18 * See the License for the specific language governing permissions and
Pawel Zarembski 0:01f31e923fe2 19 * limitations under the License.
Pawel Zarembski 0:01f31e923fe2 20 */
Pawel Zarembski 0:01f31e923fe2 21
Pawel Zarembski 0:01f31e923fe2 22 #include "rt_TypeDef.h"
Pawel Zarembski 0:01f31e923fe2 23 #include "RTX_Config.h"
Pawel Zarembski 0:01f31e923fe2 24 #include "rt_System.h"
Pawel Zarembski 0:01f31e923fe2 25 #include "rt_MemBox.h"
Pawel Zarembski 0:01f31e923fe2 26 #include "rt_HAL_CM.h"
Pawel Zarembski 0:01f31e923fe2 27
Pawel Zarembski 0:01f31e923fe2 28 /*----------------------------------------------------------------------------
Pawel Zarembski 0:01f31e923fe2 29 * Global Functions
Pawel Zarembski 0:01f31e923fe2 30 *---------------------------------------------------------------------------*/
Pawel Zarembski 0:01f31e923fe2 31
Pawel Zarembski 0:01f31e923fe2 32
Pawel Zarembski 0:01f31e923fe2 33 /*--------------------------- _init_box -------------------------------------*/
Pawel Zarembski 0:01f31e923fe2 34
Pawel Zarembski 0:01f31e923fe2 35 int _init_box (void *box_mem, U32 box_size, U32 blk_size) {
Pawel Zarembski 0:01f31e923fe2 36 /* Initialize memory block system, returns 0 if OK, 1 if fails. */
Pawel Zarembski 0:01f31e923fe2 37 void *end;
Pawel Zarembski 0:01f31e923fe2 38 void *blk;
Pawel Zarembski 0:01f31e923fe2 39 void *next;
Pawel Zarembski 0:01f31e923fe2 40 U32 sizeof_bm;
Pawel Zarembski 0:01f31e923fe2 41
Pawel Zarembski 0:01f31e923fe2 42 /* Create memory structure. */
Pawel Zarembski 0:01f31e923fe2 43 if (blk_size & BOX_ALIGN_8) {
Pawel Zarembski 0:01f31e923fe2 44 /* Memory blocks 8-byte aligned. */
Pawel Zarembski 0:01f31e923fe2 45 blk_size = ((blk_size & ~BOX_ALIGN_8) + 7) & ~7;
Pawel Zarembski 0:01f31e923fe2 46 sizeof_bm = (sizeof (struct OS_BM) + 7) & ~7;
Pawel Zarembski 0:01f31e923fe2 47 }
Pawel Zarembski 0:01f31e923fe2 48 else {
Pawel Zarembski 0:01f31e923fe2 49 /* Memory blocks 4-byte aligned. */
Pawel Zarembski 0:01f31e923fe2 50 blk_size = (blk_size + 3) & ~3;
Pawel Zarembski 0:01f31e923fe2 51 sizeof_bm = sizeof (struct OS_BM);
Pawel Zarembski 0:01f31e923fe2 52 }
Pawel Zarembski 0:01f31e923fe2 53 if (blk_size == 0) {
Pawel Zarembski 0:01f31e923fe2 54 return (1);
Pawel Zarembski 0:01f31e923fe2 55 }
Pawel Zarembski 0:01f31e923fe2 56 if ((blk_size + sizeof_bm) > box_size) {
Pawel Zarembski 0:01f31e923fe2 57 return (1);
Pawel Zarembski 0:01f31e923fe2 58 }
Pawel Zarembski 0:01f31e923fe2 59 /* Create a Memory structure. */
Pawel Zarembski 0:01f31e923fe2 60 blk = ((U8 *) box_mem) + sizeof_bm;
Pawel Zarembski 0:01f31e923fe2 61 ((P_BM) box_mem)->free = blk;
Pawel Zarembski 0:01f31e923fe2 62 end = ((U8 *) box_mem) + box_size;
Pawel Zarembski 0:01f31e923fe2 63 ((P_BM) box_mem)->end = end;
Pawel Zarembski 0:01f31e923fe2 64 ((P_BM) box_mem)->blk_size = blk_size;
Pawel Zarembski 0:01f31e923fe2 65
Pawel Zarembski 0:01f31e923fe2 66 /* Link all free blocks using offsets. */
Pawel Zarembski 0:01f31e923fe2 67 end = ((U8 *) end) - blk_size;
Pawel Zarembski 0:01f31e923fe2 68 while (1) {
Pawel Zarembski 0:01f31e923fe2 69 next = ((U8 *) blk) + blk_size;
Pawel Zarembski 0:01f31e923fe2 70 if (next > end) break;
Pawel Zarembski 0:01f31e923fe2 71 *((void **)blk) = next;
Pawel Zarembski 0:01f31e923fe2 72 blk = next;
Pawel Zarembski 0:01f31e923fe2 73 }
Pawel Zarembski 0:01f31e923fe2 74 /* end marker */
Pawel Zarembski 0:01f31e923fe2 75 *((void **)blk) = 0;
Pawel Zarembski 0:01f31e923fe2 76 return (0);
Pawel Zarembski 0:01f31e923fe2 77 }
Pawel Zarembski 0:01f31e923fe2 78
Pawel Zarembski 0:01f31e923fe2 79 /*--------------------------- rt_alloc_box ----------------------------------*/
Pawel Zarembski 0:01f31e923fe2 80
Pawel Zarembski 0:01f31e923fe2 81 void *rt_alloc_box (void *box_mem) {
Pawel Zarembski 0:01f31e923fe2 82 /* Allocate a memory block and return start address. */
Pawel Zarembski 0:01f31e923fe2 83 void **free;
Pawel Zarembski 0:01f31e923fe2 84 #ifndef __USE_EXCLUSIVE_ACCESS
Pawel Zarembski 0:01f31e923fe2 85 int irq_dis;
Pawel Zarembski 0:01f31e923fe2 86
Pawel Zarembski 0:01f31e923fe2 87 irq_dis = __disable_irq ();
Pawel Zarembski 0:01f31e923fe2 88 free = ((P_BM) box_mem)->free;
Pawel Zarembski 0:01f31e923fe2 89 if (free) {
Pawel Zarembski 0:01f31e923fe2 90 ((P_BM) box_mem)->free = *free;
Pawel Zarembski 0:01f31e923fe2 91 }
Pawel Zarembski 0:01f31e923fe2 92 if (!irq_dis) __enable_irq ();
Pawel Zarembski 0:01f31e923fe2 93 #else
Pawel Zarembski 0:01f31e923fe2 94 do {
Pawel Zarembski 0:01f31e923fe2 95 if ((free = (void **)__ldrex(&((P_BM) box_mem)->free)) == 0) {
Pawel Zarembski 0:01f31e923fe2 96 __clrex();
Pawel Zarembski 0:01f31e923fe2 97 break;
Pawel Zarembski 0:01f31e923fe2 98 }
Pawel Zarembski 0:01f31e923fe2 99 } while (__strex((U32)*free, &((P_BM) box_mem)->free));
Pawel Zarembski 0:01f31e923fe2 100 #endif
Pawel Zarembski 0:01f31e923fe2 101 return (free);
Pawel Zarembski 0:01f31e923fe2 102 }
Pawel Zarembski 0:01f31e923fe2 103
Pawel Zarembski 0:01f31e923fe2 104
Pawel Zarembski 0:01f31e923fe2 105 /*--------------------------- _calloc_box -----------------------------------*/
Pawel Zarembski 0:01f31e923fe2 106
Pawel Zarembski 0:01f31e923fe2 107 void *_calloc_box (void *box_mem) {
Pawel Zarembski 0:01f31e923fe2 108 /* Allocate a 0-initialized memory block and return start address. */
Pawel Zarembski 0:01f31e923fe2 109 void *free;
Pawel Zarembski 0:01f31e923fe2 110 U32 *p;
Pawel Zarembski 0:01f31e923fe2 111 U32 i;
Pawel Zarembski 0:01f31e923fe2 112
Pawel Zarembski 0:01f31e923fe2 113 free = _alloc_box (box_mem);
Pawel Zarembski 0:01f31e923fe2 114 if (free) {
Pawel Zarembski 0:01f31e923fe2 115 p = free;
Pawel Zarembski 0:01f31e923fe2 116 for (i = ((P_BM) box_mem)->blk_size; i; i -= 4) {
Pawel Zarembski 0:01f31e923fe2 117 *p = 0;
Pawel Zarembski 0:01f31e923fe2 118 p++;
Pawel Zarembski 0:01f31e923fe2 119 }
Pawel Zarembski 0:01f31e923fe2 120 }
Pawel Zarembski 0:01f31e923fe2 121 return (free);
Pawel Zarembski 0:01f31e923fe2 122 }
Pawel Zarembski 0:01f31e923fe2 123
Pawel Zarembski 0:01f31e923fe2 124
Pawel Zarembski 0:01f31e923fe2 125 /*--------------------------- rt_free_box -----------------------------------*/
Pawel Zarembski 0:01f31e923fe2 126
Pawel Zarembski 0:01f31e923fe2 127 int rt_free_box (void *box_mem, void *box) {
Pawel Zarembski 0:01f31e923fe2 128 /* Free a memory block, returns 0 if OK, 1 if box does not belong to box_mem */
Pawel Zarembski 0:01f31e923fe2 129 #ifndef __USE_EXCLUSIVE_ACCESS
Pawel Zarembski 0:01f31e923fe2 130 int irq_dis;
Pawel Zarembski 0:01f31e923fe2 131 #endif
Pawel Zarembski 0:01f31e923fe2 132
Pawel Zarembski 0:01f31e923fe2 133 if (box < box_mem || box >= ((P_BM) box_mem)->end) {
Pawel Zarembski 0:01f31e923fe2 134 return (1);
Pawel Zarembski 0:01f31e923fe2 135 }
Pawel Zarembski 0:01f31e923fe2 136
Pawel Zarembski 0:01f31e923fe2 137 #ifndef __USE_EXCLUSIVE_ACCESS
Pawel Zarembski 0:01f31e923fe2 138 irq_dis = __disable_irq ();
Pawel Zarembski 0:01f31e923fe2 139 *((void **)box) = ((P_BM) box_mem)->free;
Pawel Zarembski 0:01f31e923fe2 140 ((P_BM) box_mem)->free = box;
Pawel Zarembski 0:01f31e923fe2 141 if (!irq_dis) __enable_irq ();
Pawel Zarembski 0:01f31e923fe2 142 #else
Pawel Zarembski 0:01f31e923fe2 143 do {
Pawel Zarembski 0:01f31e923fe2 144 *((void **)box) = (void *)__ldrex(&((P_BM) box_mem)->free);
Pawel Zarembski 0:01f31e923fe2 145 } while (__strex ((U32)box, &((P_BM) box_mem)->free));
Pawel Zarembski 0:01f31e923fe2 146 #endif
Pawel Zarembski 0:01f31e923fe2 147 return (0);
Pawel Zarembski 0:01f31e923fe2 148 }
Pawel Zarembski 0:01f31e923fe2 149
Pawel Zarembski 0:01f31e923fe2 150 /*----------------------------------------------------------------------------
Pawel Zarembski 0:01f31e923fe2 151 * end of file
Pawel Zarembski 0:01f31e923fe2 152 *---------------------------------------------------------------------------*/
Pawel Zarembski 0:01f31e923fe2 153