mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
updated based on mbed-os5.15.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 1:9db0e321a9f4 1 /* mbed Microcontroller Library
kenjiArai 1:9db0e321a9f4 2 * Copyright (c) 2018-2019 ARM Limited
kenjiArai 1:9db0e321a9f4 3 * SPDX-License-Identifier: Apache-2.0
kenjiArai 1:9db0e321a9f4 4 *
kenjiArai 1:9db0e321a9f4 5 * Licensed under the Apache License, Version 2.0 (the "License");
kenjiArai 1:9db0e321a9f4 6 * you may not use this file except in compliance with the License.
kenjiArai 1:9db0e321a9f4 7 * You may obtain a copy of the License at
kenjiArai 1:9db0e321a9f4 8 *
kenjiArai 1:9db0e321a9f4 9 * http://www.apache.org/licenses/LICENSE-2.0
kenjiArai 1:9db0e321a9f4 10 *
kenjiArai 1:9db0e321a9f4 11 * Unless required by applicable law or agreed to in writing, software
kenjiArai 1:9db0e321a9f4 12 * distributed under the License is distributed on an "AS IS" BASIS,
kenjiArai 1:9db0e321a9f4 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kenjiArai 1:9db0e321a9f4 14 * See the License for the specific language governing permissions and
kenjiArai 1:9db0e321a9f4 15 * limitations under the License.
kenjiArai 1:9db0e321a9f4 16 */
kenjiArai 1:9db0e321a9f4 17
kenjiArai 1:9db0e321a9f4 18 #include "platform/platform.h"
kenjiArai 1:9db0e321a9f4 19 #include "platform/mbed_critical.h"
kenjiArai 1:9db0e321a9f4 20 #include "platform/mbed_assert.h"
kenjiArai 1:9db0e321a9f4 21 #include "platform/mbed_error.h"
kenjiArai 1:9db0e321a9f4 22
kenjiArai 1:9db0e321a9f4 23
kenjiArai 1:9db0e321a9f4 24 #include "platform/internal/CThunkBase.h"
kenjiArai 1:9db0e321a9f4 25
kenjiArai 1:9db0e321a9f4 26 MBED_STATIC_ASSERT(MBED_CONF_PLATFORM_CTHUNK_COUNT_MAX < 256, "MBED_CONF_PLATFORM_CTHUNK_COUNT_MAX must be less than 256");
kenjiArai 1:9db0e321a9f4 27 MBED_STATIC_ASSERT(MBED_CONF_PLATFORM_CTHUNK_COUNT_MAX > 0, "MBED_CONF_PLATFORM_CTHUNK_COUNT_MAX must be greater than 0");
kenjiArai 1:9db0e321a9f4 28
kenjiArai 1:9db0e321a9f4 29 #define ENABLE_N(N) ((MBED_CONF_PLATFORM_CTHUNK_COUNT_MAX & N) ? 1 : 0)
kenjiArai 1:9db0e321a9f4 30
kenjiArai 1:9db0e321a9f4 31 #define START_128 0
kenjiArai 1:9db0e321a9f4 32 #define START_64 (START_128 + ENABLE_N(128) * 128)
kenjiArai 1:9db0e321a9f4 33 #define START_32 (START_64 + ENABLE_N(64) * 64)
kenjiArai 1:9db0e321a9f4 34 #define START_16 (START_32 + ENABLE_N(32) * 32)
kenjiArai 1:9db0e321a9f4 35 #define START_8 (START_16 + ENABLE_N(16) * 16)
kenjiArai 1:9db0e321a9f4 36 #define START_4 (START_8 + ENABLE_N(8) * 8)
kenjiArai 1:9db0e321a9f4 37 #define START_2 (START_4 + ENABLE_N(4) * 4)
kenjiArai 1:9db0e321a9f4 38 #define START_1 (START_2 + ENABLE_N(2) * 2)
kenjiArai 1:9db0e321a9f4 39
kenjiArai 1:9db0e321a9f4 40 #define DECLARE_THUNK128(start) \
kenjiArai 1:9db0e321a9f4 41 DECLARE_THUNK64(start), \
kenjiArai 1:9db0e321a9f4 42 DECLARE_THUNK64(start + 64)
kenjiArai 1:9db0e321a9f4 43 #define DECLARE_THUNK64(start) \
kenjiArai 1:9db0e321a9f4 44 DECLARE_THUNK32(start), \
kenjiArai 1:9db0e321a9f4 45 DECLARE_THUNK32(start + 32)
kenjiArai 1:9db0e321a9f4 46 #define DECLARE_THUNK32(start) \
kenjiArai 1:9db0e321a9f4 47 DECLARE_THUNK16(start), \
kenjiArai 1:9db0e321a9f4 48 DECLARE_THUNK16(start + 16)
kenjiArai 1:9db0e321a9f4 49 #define DECLARE_THUNK16(start) \
kenjiArai 1:9db0e321a9f4 50 DECLARE_THUNK8(start), \
kenjiArai 1:9db0e321a9f4 51 DECLARE_THUNK8(start + 8)
kenjiArai 1:9db0e321a9f4 52 #define DECLARE_THUNK8(start) \
kenjiArai 1:9db0e321a9f4 53 DECLARE_THUNK4(start), \
kenjiArai 1:9db0e321a9f4 54 DECLARE_THUNK4(start + 4)
kenjiArai 1:9db0e321a9f4 55 #define DECLARE_THUNK4(start) \
kenjiArai 1:9db0e321a9f4 56 DECLARE_THUNK2(start), \
kenjiArai 1:9db0e321a9f4 57 DECLARE_THUNK2(start + 2)
kenjiArai 1:9db0e321a9f4 58 #define DECLARE_THUNK2(start) \
kenjiArai 1:9db0e321a9f4 59 DECLARE_THUNK1(start), \
kenjiArai 1:9db0e321a9f4 60 DECLARE_THUNK1(start + 1)
kenjiArai 1:9db0e321a9f4 61 #define DECLARE_THUNK1(index) &CThunkBase::thunk_entry<index>
kenjiArai 1:9db0e321a9f4 62
kenjiArai 1:9db0e321a9f4 63 const CThunkEntry CThunkBase::_thunk_table[MBED_CONF_PLATFORM_CTHUNK_COUNT_MAX] = {
kenjiArai 1:9db0e321a9f4 64 #if ENABLE_N(128)
kenjiArai 1:9db0e321a9f4 65 DECLARE_THUNK128(START_128),
kenjiArai 1:9db0e321a9f4 66 #endif
kenjiArai 1:9db0e321a9f4 67 #if ENABLE_N(64)
kenjiArai 1:9db0e321a9f4 68 DECLARE_THUNK64(START_64),
kenjiArai 1:9db0e321a9f4 69 #endif
kenjiArai 1:9db0e321a9f4 70 #if ENABLE_N(32)
kenjiArai 1:9db0e321a9f4 71 DECLARE_THUNK32(START_32),
kenjiArai 1:9db0e321a9f4 72 #endif
kenjiArai 1:9db0e321a9f4 73 #if ENABLE_N(16)
kenjiArai 1:9db0e321a9f4 74 DECLARE_THUNK16(START_16),
kenjiArai 1:9db0e321a9f4 75 #endif
kenjiArai 1:9db0e321a9f4 76 #if ENABLE_N(8)
kenjiArai 1:9db0e321a9f4 77 DECLARE_THUNK8(START_8),
kenjiArai 1:9db0e321a9f4 78 #endif
kenjiArai 1:9db0e321a9f4 79 #if ENABLE_N(4)
kenjiArai 1:9db0e321a9f4 80 DECLARE_THUNK4(START_4),
kenjiArai 1:9db0e321a9f4 81 #endif
kenjiArai 1:9db0e321a9f4 82 #if ENABLE_N(2)
kenjiArai 1:9db0e321a9f4 83 DECLARE_THUNK2(START_2),
kenjiArai 1:9db0e321a9f4 84 #endif
kenjiArai 1:9db0e321a9f4 85 #if ENABLE_N(1)
kenjiArai 1:9db0e321a9f4 86 DECLARE_THUNK1(START_1)
kenjiArai 1:9db0e321a9f4 87 #endif
kenjiArai 1:9db0e321a9f4 88 };
kenjiArai 1:9db0e321a9f4 89
kenjiArai 1:9db0e321a9f4 90 CThunkBase *CThunkBase::_thunk_storage[MBED_CONF_PLATFORM_CTHUNK_COUNT_MAX];
kenjiArai 1:9db0e321a9f4 91
kenjiArai 1:9db0e321a9f4 92 CThunkBase::CthunkFree CThunkBase::_cthunk_free_real = NULL;
kenjiArai 1:9db0e321a9f4 93
kenjiArai 1:9db0e321a9f4 94 CThunkEntry CThunkBase::cthunk_alloc(CThunkBase *cthunk)
kenjiArai 1:9db0e321a9f4 95 {
kenjiArai 1:9db0e321a9f4 96 // Atomically allocate one entry
kenjiArai 1:9db0e321a9f4 97 core_util_critical_section_enter();
kenjiArai 1:9db0e321a9f4 98 CThunkEntry entry = NULL;
kenjiArai 1:9db0e321a9f4 99 for (int i = 0; i < MBED_CONF_PLATFORM_CTHUNK_COUNT_MAX; i++) {
kenjiArai 1:9db0e321a9f4 100 if (_thunk_storage[i] == NULL) {
kenjiArai 1:9db0e321a9f4 101 _thunk_storage[i] = cthunk;
kenjiArai 1:9db0e321a9f4 102 entry = _thunk_table[i];
kenjiArai 1:9db0e321a9f4 103 break;
kenjiArai 1:9db0e321a9f4 104 }
kenjiArai 1:9db0e321a9f4 105 }
kenjiArai 1:9db0e321a9f4 106 core_util_critical_section_exit();
kenjiArai 1:9db0e321a9f4 107
kenjiArai 1:9db0e321a9f4 108 if (entry == NULL) {
kenjiArai 1:9db0e321a9f4 109 MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_OUT_OF_RESOURCES), "Ran out of CThunk entries. Increase MBED_CONF_PLATFORM_CTHUNK_COUNT_MAX to fix this error");
kenjiArai 1:9db0e321a9f4 110 }
kenjiArai 1:9db0e321a9f4 111
kenjiArai 1:9db0e321a9f4 112 // Set function pointer on first use. This allows _thunk_table
kenjiArai 1:9db0e321a9f4 113 // and _thunk_storage to get removed by the linker if
kenjiArai 1:9db0e321a9f4 114 // cthunk_alloc is never used.
kenjiArai 1:9db0e321a9f4 115 _cthunk_free_real = &cthunk_free_real;
kenjiArai 1:9db0e321a9f4 116
kenjiArai 1:9db0e321a9f4 117 return entry;
kenjiArai 1:9db0e321a9f4 118 }
kenjiArai 1:9db0e321a9f4 119
kenjiArai 1:9db0e321a9f4 120 void CThunkBase::cthunk_free(CThunkEntry item)
kenjiArai 1:9db0e321a9f4 121 {
kenjiArai 1:9db0e321a9f4 122 if (_cthunk_free_real) {
kenjiArai 1:9db0e321a9f4 123 _cthunk_free_real(item);
kenjiArai 1:9db0e321a9f4 124 }
kenjiArai 1:9db0e321a9f4 125 }
kenjiArai 1:9db0e321a9f4 126
kenjiArai 1:9db0e321a9f4 127 void CThunkBase::cthunk_free_real(CThunkEntry item)
kenjiArai 1:9db0e321a9f4 128 {
kenjiArai 1:9db0e321a9f4 129 bool found = false;
kenjiArai 1:9db0e321a9f4 130
kenjiArai 1:9db0e321a9f4 131 core_util_critical_section_enter();
kenjiArai 1:9db0e321a9f4 132 for (int i = 0; i < MBED_CONF_PLATFORM_CTHUNK_COUNT_MAX; i++) {
kenjiArai 1:9db0e321a9f4 133 if (_thunk_table[i] == item) {
kenjiArai 1:9db0e321a9f4 134 _thunk_storage[i] = NULL;
kenjiArai 1:9db0e321a9f4 135 found = true;
kenjiArai 1:9db0e321a9f4 136 break;
kenjiArai 1:9db0e321a9f4 137 }
kenjiArai 1:9db0e321a9f4 138 }
kenjiArai 1:9db0e321a9f4 139 core_util_critical_section_exit();
kenjiArai 1:9db0e321a9f4 140
kenjiArai 1:9db0e321a9f4 141 if (!found) {
kenjiArai 1:9db0e321a9f4 142 MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_INVALID_ARGUMENT), "Tried to free invalid CThunkEntry");
kenjiArai 1:9db0e321a9f4 143 }
kenjiArai 1:9db0e321a9f4 144
kenjiArai 1:9db0e321a9f4 145 }