mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Thu Nov 23 11:57:25 2017 +0000
Revision:
178:79309dc6340a
Parent:
174:b96e65c34a4d
Child:
180:96ed750bd169
mbed-dev library. Release version 156

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 149:156823d33999 1
<> 149:156823d33999 2 /** \addtogroup platform */
<> 149:156823d33999 3 /** @{*/
AnnaBridge 178:79309dc6340a 4 /**
AnnaBridge 178:79309dc6340a 5 * \defgroup platform_CThunk CThunk class
AnnaBridge 178:79309dc6340a 6 * @{
AnnaBridge 178:79309dc6340a 7 */
<> 149:156823d33999 8 /* General C++ Object Thunking class
<> 149:156823d33999 9 *
<> 149:156823d33999 10 * - allows direct callbacks to non-static C++ class functions
<> 149:156823d33999 11 * - keeps track for the corresponding class instance
<> 149:156823d33999 12 * - supports an optional context parameter for the called function
<> 149:156823d33999 13 * - ideally suited for class object receiving interrupts (NVIC_SetVector)
<> 149:156823d33999 14 *
<> 149:156823d33999 15 * Copyright (c) 2014-2015 ARM Limited
<> 149:156823d33999 16 *
<> 149:156823d33999 17 * Licensed under the Apache License, Version 2.0 (the "License");
<> 149:156823d33999 18 * you may not use this file except in compliance with the License.
<> 149:156823d33999 19 * You may obtain a copy of the License at
<> 149:156823d33999 20 *
<> 149:156823d33999 21 * http://www.apache.org/licenses/LICENSE-2.0
<> 149:156823d33999 22 *
<> 149:156823d33999 23 * Unless required by applicable law or agreed to in writing, software
<> 149:156823d33999 24 * distributed under the License is distributed on an "AS IS" BASIS,
<> 149:156823d33999 25 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<> 149:156823d33999 26 * See the License for the specific language governing permissions and
<> 149:156823d33999 27 * limitations under the License.
<> 149:156823d33999 28 */
<> 149:156823d33999 29
<> 149:156823d33999 30 /* General C++ Object Thunking class
<> 149:156823d33999 31 *
<> 149:156823d33999 32 * - allows direct callbacks to non-static C++ class functions
<> 149:156823d33999 33 * - keeps track for the corresponding class instance
<> 149:156823d33999 34 * - supports an optional context parameter for the called function
<> 149:156823d33999 35 * - ideally suited for class object receiving interrupts (NVIC_SetVector)
<> 149:156823d33999 36 */
<> 149:156823d33999 37
<> 149:156823d33999 38 #ifndef __CTHUNK_H__
<> 149:156823d33999 39 #define __CTHUNK_H__
<> 149:156823d33999 40
<> 149:156823d33999 41 #define CTHUNK_ADDRESS 1
<> 149:156823d33999 42 #define CTHUNK_VARIABLES volatile uint32_t code[2]
<> 149:156823d33999 43
AnnaBridge 174:b96e65c34a4d 44 #if (defined(__CORTEX_M3) || defined(__CORTEX_M4) || defined(__CORTEX_M7) || defined(__CORTEX_A9) \
AnnaBridge 174:b96e65c34a4d 45 || defined(__CORTEX_M23) || defined(__CORTEX_M33))
<> 149:156823d33999 46 /**
<> 149:156823d33999 47 * CTHUNK disassembly for Cortex-M3/M4/M7/A9 (thumb2):
<> 149:156823d33999 48 * * adr r0, #4
<> 149:156823d33999 49 * * ldm r0, {r0, r1, r2, pc}
<> 149:156823d33999 50 *
<> 149:156823d33999 51 * This instruction loads the arguments for the static thunking function to r0-r2, and
<> 149:156823d33999 52 * branches to that function by loading its address into PC.
<> 149:156823d33999 53 *
<> 149:156823d33999 54 * This is safe for both regular calling and interrupt calling, since it only touches scratch registers
<> 149:156823d33999 55 * which should be saved by the caller, and are automatically saved as part of the IRQ context switch.
<> 149:156823d33999 56 */
<> 149:156823d33999 57 #define CTHUNK_ASSIGMENT do { \
<> 149:156823d33999 58 m_thunk.code[0] = 0xE890A001; \
<> 149:156823d33999 59 m_thunk.code[1] = 0x00008007; \
<> 149:156823d33999 60 } while (0)
<> 149:156823d33999 61
<> 149:156823d33999 62 #elif (defined(__CORTEX_M0PLUS) || defined(__CORTEX_M0))
<> 149:156823d33999 63 /*
<> 149:156823d33999 64 * CTHUNK disassembly for Cortex M0/M0+ (thumb):
<> 149:156823d33999 65 * * adr r0, #4
<> 149:156823d33999 66 * * ldm r0, {r0, r1, r2, r3}
<> 149:156823d33999 67 * * bx r3
<> 149:156823d33999 68 */
<> 149:156823d33999 69 #define CTHUNK_ASSIGMENT do { \
<> 149:156823d33999 70 m_thunk.code[0] = 0xC80FA001; \
<> 149:156823d33999 71 m_thunk.code[1] = 0x00004718; \
<> 149:156823d33999 72 } while (0)
<> 149:156823d33999 73
<> 149:156823d33999 74 #else
<> 149:156823d33999 75 #error "Target is not currently suported."
<> 149:156823d33999 76 #endif
<> 149:156823d33999 77
<> 149:156823d33999 78 /* IRQ/Exception compatible thunk entry function */
<> 149:156823d33999 79 typedef void (*CThunkEntry)(void);
<> 149:156823d33999 80
<> 149:156823d33999 81 /**
<> 149:156823d33999 82 * Class for created a pointer with data bound to it
<> 149:156823d33999 83 *
AnnaBridge 167:e84263d55307 84 * @note Synchronization level: Not protected
<> 149:156823d33999 85 */
<> 149:156823d33999 86 template<class T>
<> 149:156823d33999 87 class CThunk
<> 149:156823d33999 88 {
<> 149:156823d33999 89 public:
<> 149:156823d33999 90 typedef void (T::*CCallbackSimple)(void);
<> 149:156823d33999 91 typedef void (T::*CCallback)(void* context);
<> 149:156823d33999 92
<> 149:156823d33999 93 inline CThunk(T *instance)
<> 149:156823d33999 94 {
<> 149:156823d33999 95 init(instance, NULL, NULL);
<> 149:156823d33999 96 }
<> 149:156823d33999 97
<> 149:156823d33999 98 inline CThunk(T *instance, CCallback callback)
<> 149:156823d33999 99 {
<> 149:156823d33999 100 init(instance, callback, NULL);
<> 149:156823d33999 101 }
<> 149:156823d33999 102
<> 149:156823d33999 103 ~CThunk() {
<> 149:156823d33999 104
<> 149:156823d33999 105 }
<> 149:156823d33999 106
<> 149:156823d33999 107 inline CThunk(T *instance, CCallbackSimple callback)
<> 149:156823d33999 108 {
<> 149:156823d33999 109 init(instance, (CCallback)callback, NULL);
<> 149:156823d33999 110 }
<> 149:156823d33999 111
<> 149:156823d33999 112 inline CThunk(T &instance, CCallback callback)
<> 149:156823d33999 113 {
<> 149:156823d33999 114 init(instance, callback, NULL);
<> 149:156823d33999 115 }
<> 149:156823d33999 116
<> 149:156823d33999 117 inline CThunk(T &instance, CCallbackSimple callback)
<> 149:156823d33999 118 {
<> 149:156823d33999 119 init(instance, (CCallback)callback, NULL);
<> 149:156823d33999 120 }
<> 149:156823d33999 121
<> 149:156823d33999 122 inline CThunk(T &instance, CCallback callback, void* context)
<> 149:156823d33999 123 {
<> 149:156823d33999 124 init(instance, callback, context);
<> 149:156823d33999 125 }
<> 149:156823d33999 126
<> 149:156823d33999 127 inline void callback(CCallback callback)
<> 149:156823d33999 128 {
<> 149:156823d33999 129 m_callback = callback;
<> 149:156823d33999 130 }
<> 149:156823d33999 131
<> 149:156823d33999 132 inline void callback(CCallbackSimple callback)
<> 149:156823d33999 133 {
<> 149:156823d33999 134 m_callback = (CCallback)callback;
<> 149:156823d33999 135 }
<> 149:156823d33999 136
<> 149:156823d33999 137 inline void context(void* context)
<> 149:156823d33999 138 {
<> 149:156823d33999 139 m_thunk.context = (uint32_t)context;
<> 149:156823d33999 140 }
<> 149:156823d33999 141
<> 149:156823d33999 142 inline void context(uint32_t context)
<> 149:156823d33999 143 {
<> 149:156823d33999 144 m_thunk.context = context;
<> 149:156823d33999 145 }
<> 149:156823d33999 146
<> 149:156823d33999 147 inline uint32_t entry(void)
<> 149:156823d33999 148 {
<> 149:156823d33999 149 return (((uint32_t)&m_thunk)|CTHUNK_ADDRESS);
<> 149:156823d33999 150 }
<> 149:156823d33999 151
<> 149:156823d33999 152 /* get thunk entry point for connecting rhunk to an IRQ table */
<> 149:156823d33999 153 inline operator CThunkEntry(void)
<> 149:156823d33999 154 {
<> 149:156823d33999 155 return (CThunkEntry)entry();
<> 149:156823d33999 156 }
<> 149:156823d33999 157
<> 149:156823d33999 158 /* get thunk entry point for connecting rhunk to an IRQ table */
<> 149:156823d33999 159 inline operator uint32_t(void)
<> 149:156823d33999 160 {
<> 149:156823d33999 161 return entry();
<> 149:156823d33999 162 }
<> 149:156823d33999 163
<> 149:156823d33999 164 /* simple test function */
<> 149:156823d33999 165 inline void call(void)
<> 149:156823d33999 166 {
<> 149:156823d33999 167 (((CThunkEntry)(entry()))());
<> 149:156823d33999 168 }
<> 149:156823d33999 169
<> 149:156823d33999 170 private:
<> 149:156823d33999 171 T* m_instance;
<> 149:156823d33999 172 volatile CCallback m_callback;
<> 149:156823d33999 173
<> 149:156823d33999 174 // TODO: this needs proper fix, to refactor toolchain header file and all its use
<> 149:156823d33999 175 // PACKED there is not defined properly for IAR
<> 149:156823d33999 176 #if defined (__ICCARM__)
<> 149:156823d33999 177 typedef __packed struct
<> 149:156823d33999 178 {
<> 149:156823d33999 179 CTHUNK_VARIABLES;
<> 149:156823d33999 180 volatile uint32_t instance;
<> 149:156823d33999 181 volatile uint32_t context;
<> 149:156823d33999 182 volatile uint32_t callback;
<> 149:156823d33999 183 volatile uint32_t trampoline;
<> 149:156823d33999 184 } CThunkTrampoline;
<> 149:156823d33999 185 #else
<> 149:156823d33999 186 typedef struct
<> 149:156823d33999 187 {
<> 149:156823d33999 188 CTHUNK_VARIABLES;
<> 149:156823d33999 189 volatile uint32_t instance;
<> 149:156823d33999 190 volatile uint32_t context;
<> 149:156823d33999 191 volatile uint32_t callback;
<> 149:156823d33999 192 volatile uint32_t trampoline;
<> 149:156823d33999 193 } __attribute__((__packed__)) CThunkTrampoline;
<> 149:156823d33999 194 #endif
<> 149:156823d33999 195
<> 149:156823d33999 196 static void trampoline(T* instance, void* context, CCallback* callback)
<> 149:156823d33999 197 {
<> 149:156823d33999 198 if(instance && *callback) {
<> 149:156823d33999 199 (static_cast<T*>(instance)->**callback)(context);
<> 149:156823d33999 200 }
<> 149:156823d33999 201 }
<> 149:156823d33999 202
<> 149:156823d33999 203 volatile CThunkTrampoline m_thunk;
<> 149:156823d33999 204
<> 149:156823d33999 205 inline void init(T *instance, CCallback callback, void* context)
<> 149:156823d33999 206 {
<> 149:156823d33999 207 /* remember callback - need to add this level of redirection
<> 149:156823d33999 208 as pointer size for member functions differs between platforms */
<> 149:156823d33999 209 m_callback = callback;
<> 149:156823d33999 210
<> 149:156823d33999 211 /* populate thunking trampoline */
<> 149:156823d33999 212 CTHUNK_ASSIGMENT;
<> 149:156823d33999 213 m_thunk.context = (uint32_t)context;
<> 149:156823d33999 214 m_thunk.instance = (uint32_t)instance;
<> 149:156823d33999 215 m_thunk.callback = (uint32_t)&m_callback;
<> 149:156823d33999 216 m_thunk.trampoline = (uint32_t)&trampoline;
<> 149:156823d33999 217
<> 149:156823d33999 218 #if defined(__CORTEX_A9)
<> 149:156823d33999 219 /* Data cache clean */
<> 149:156823d33999 220 /* Cache control */
<> 149:156823d33999 221 {
<> 149:156823d33999 222 uint32_t start_addr = (uint32_t)&m_thunk & 0xFFFFFFE0;
<> 149:156823d33999 223 uint32_t end_addr = (uint32_t)&m_thunk + sizeof(m_thunk);
<> 149:156823d33999 224 uint32_t addr;
<> 149:156823d33999 225
<> 149:156823d33999 226 /* Data cache clean and invalid */
<> 149:156823d33999 227 for (addr = start_addr; addr < end_addr; addr += 0x20) {
<> 149:156823d33999 228 __v7_clean_inv_dcache_mva((void *)addr);
<> 149:156823d33999 229 }
<> 149:156823d33999 230 /* Instruction cache invalid */
<> 149:156823d33999 231 __v7_inv_icache_all();
<> 149:156823d33999 232 __ca9u_inv_tlb_all();
<> 149:156823d33999 233 __v7_inv_btac();
<> 149:156823d33999 234 }
<> 149:156823d33999 235 #endif
<> 149:156823d33999 236 #if defined(__CORTEX_M7)
<> 149:156823d33999 237 /* Data cache clean and invalid */
<> 149:156823d33999 238 SCB_CleanInvalidateDCache();
<> 149:156823d33999 239
<> 149:156823d33999 240 /* Instruction cache invalid */
<> 149:156823d33999 241 SCB_InvalidateICache();
<> 149:156823d33999 242 #endif
<> 149:156823d33999 243 __ISB();
<> 149:156823d33999 244 __DSB();
<> 149:156823d33999 245 }
<> 149:156823d33999 246 };
<> 149:156823d33999 247
AnnaBridge 178:79309dc6340a 248 /**@}*/
AnnaBridge 178:79309dc6340a 249
AnnaBridge 178:79309dc6340a 250 /**@}*/
AnnaBridge 178:79309dc6340a 251
<> 149:156823d33999 252 #endif/*__CTHUNK_H__*/
<> 149:156823d33999 253