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