lib
Fork of mbed-dev by
platform/CThunk.h@149:156823d33999, 2016-10-28 (annotated)
- Committer:
- <>
- Date:
- Fri Oct 28 11:17:30 2016 +0100
- Revision:
- 149:156823d33999
This updates the lib to the mbed lib v128
NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.
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 | |
<> | 149:156823d33999 | 40 | #if (defined(__CORTEX_M3) || defined(__CORTEX_M4) || defined(__CORTEX_M7) || defined(__CORTEX_A9)) |
<> | 149:156823d33999 | 41 | /** |
<> | 149:156823d33999 | 42 | * CTHUNK disassembly for Cortex-M3/M4/M7/A9 (thumb2): |
<> | 149:156823d33999 | 43 | * * adr r0, #4 |
<> | 149:156823d33999 | 44 | * * ldm r0, {r0, r1, r2, pc} |
<> | 149:156823d33999 | 45 | * |
<> | 149:156823d33999 | 46 | * This instruction loads the arguments for the static thunking function to r0-r2, and |
<> | 149:156823d33999 | 47 | * branches to that function by loading its address into PC. |
<> | 149:156823d33999 | 48 | * |
<> | 149:156823d33999 | 49 | * This is safe for both regular calling and interrupt calling, since it only touches scratch registers |
<> | 149:156823d33999 | 50 | * which should be saved by the caller, and are automatically saved as part of the IRQ context switch. |
<> | 149:156823d33999 | 51 | */ |
<> | 149:156823d33999 | 52 | #define CTHUNK_ASSIGMENT do { \ |
<> | 149:156823d33999 | 53 | m_thunk.code[0] = 0xE890A001; \ |
<> | 149:156823d33999 | 54 | m_thunk.code[1] = 0x00008007; \ |
<> | 149:156823d33999 | 55 | } while (0) |
<> | 149:156823d33999 | 56 | |
<> | 149:156823d33999 | 57 | #elif (defined(__CORTEX_M0PLUS) || defined(__CORTEX_M0)) |
<> | 149:156823d33999 | 58 | /* |
<> | 149:156823d33999 | 59 | * CTHUNK disassembly for Cortex M0/M0+ (thumb): |
<> | 149:156823d33999 | 60 | * * adr r0, #4 |
<> | 149:156823d33999 | 61 | * * ldm r0, {r0, r1, r2, r3} |
<> | 149:156823d33999 | 62 | * * bx r3 |
<> | 149:156823d33999 | 63 | */ |
<> | 149:156823d33999 | 64 | #define CTHUNK_ASSIGMENT do { \ |
<> | 149:156823d33999 | 65 | m_thunk.code[0] = 0xC80FA001; \ |
<> | 149:156823d33999 | 66 | m_thunk.code[1] = 0x00004718; \ |
<> | 149:156823d33999 | 67 | } while (0) |
<> | 149:156823d33999 | 68 | |
<> | 149:156823d33999 | 69 | #else |
<> | 149:156823d33999 | 70 | #error "Target is not currently suported." |
<> | 149:156823d33999 | 71 | #endif |
<> | 149:156823d33999 | 72 | |
<> | 149:156823d33999 | 73 | /* IRQ/Exception compatible thunk entry function */ |
<> | 149:156823d33999 | 74 | typedef void (*CThunkEntry)(void); |
<> | 149:156823d33999 | 75 | |
<> | 149:156823d33999 | 76 | /** |
<> | 149:156823d33999 | 77 | * Class for created a pointer with data bound to it |
<> | 149:156823d33999 | 78 | * |
<> | 149:156823d33999 | 79 | * @Note Synchronization level: Not protected |
<> | 149:156823d33999 | 80 | */ |
<> | 149:156823d33999 | 81 | template<class T> |
<> | 149:156823d33999 | 82 | class CThunk |
<> | 149:156823d33999 | 83 | { |
<> | 149:156823d33999 | 84 | public: |
<> | 149:156823d33999 | 85 | typedef void (T::*CCallbackSimple)(void); |
<> | 149:156823d33999 | 86 | typedef void (T::*CCallback)(void* context); |
<> | 149:156823d33999 | 87 | |
<> | 149:156823d33999 | 88 | inline CThunk(T *instance) |
<> | 149:156823d33999 | 89 | { |
<> | 149:156823d33999 | 90 | init(instance, NULL, NULL); |
<> | 149:156823d33999 | 91 | } |
<> | 149:156823d33999 | 92 | |
<> | 149:156823d33999 | 93 | inline CThunk(T *instance, CCallback callback) |
<> | 149:156823d33999 | 94 | { |
<> | 149:156823d33999 | 95 | init(instance, callback, NULL); |
<> | 149:156823d33999 | 96 | } |
<> | 149:156823d33999 | 97 | |
<> | 149:156823d33999 | 98 | ~CThunk() { |
<> | 149:156823d33999 | 99 | |
<> | 149:156823d33999 | 100 | } |
<> | 149:156823d33999 | 101 | |
<> | 149:156823d33999 | 102 | inline CThunk(T *instance, CCallbackSimple callback) |
<> | 149:156823d33999 | 103 | { |
<> | 149:156823d33999 | 104 | init(instance, (CCallback)callback, NULL); |
<> | 149:156823d33999 | 105 | } |
<> | 149:156823d33999 | 106 | |
<> | 149:156823d33999 | 107 | inline CThunk(T &instance, CCallback callback) |
<> | 149:156823d33999 | 108 | { |
<> | 149:156823d33999 | 109 | init(instance, callback, NULL); |
<> | 149:156823d33999 | 110 | } |
<> | 149:156823d33999 | 111 | |
<> | 149:156823d33999 | 112 | inline CThunk(T &instance, CCallbackSimple callback) |
<> | 149:156823d33999 | 113 | { |
<> | 149:156823d33999 | 114 | init(instance, (CCallback)callback, NULL); |
<> | 149:156823d33999 | 115 | } |
<> | 149:156823d33999 | 116 | |
<> | 149:156823d33999 | 117 | inline CThunk(T &instance, CCallback callback, void* context) |
<> | 149:156823d33999 | 118 | { |
<> | 149:156823d33999 | 119 | init(instance, callback, context); |
<> | 149:156823d33999 | 120 | } |
<> | 149:156823d33999 | 121 | |
<> | 149:156823d33999 | 122 | inline void callback(CCallback callback) |
<> | 149:156823d33999 | 123 | { |
<> | 149:156823d33999 | 124 | m_callback = callback; |
<> | 149:156823d33999 | 125 | } |
<> | 149:156823d33999 | 126 | |
<> | 149:156823d33999 | 127 | inline void callback(CCallbackSimple callback) |
<> | 149:156823d33999 | 128 | { |
<> | 149:156823d33999 | 129 | m_callback = (CCallback)callback; |
<> | 149:156823d33999 | 130 | } |
<> | 149:156823d33999 | 131 | |
<> | 149:156823d33999 | 132 | inline void context(void* context) |
<> | 149:156823d33999 | 133 | { |
<> | 149:156823d33999 | 134 | m_thunk.context = (uint32_t)context; |
<> | 149:156823d33999 | 135 | } |
<> | 149:156823d33999 | 136 | |
<> | 149:156823d33999 | 137 | inline void context(uint32_t context) |
<> | 149:156823d33999 | 138 | { |
<> | 149:156823d33999 | 139 | m_thunk.context = context; |
<> | 149:156823d33999 | 140 | } |
<> | 149:156823d33999 | 141 | |
<> | 149:156823d33999 | 142 | inline uint32_t entry(void) |
<> | 149:156823d33999 | 143 | { |
<> | 149:156823d33999 | 144 | return (((uint32_t)&m_thunk)|CTHUNK_ADDRESS); |
<> | 149:156823d33999 | 145 | } |
<> | 149:156823d33999 | 146 | |
<> | 149:156823d33999 | 147 | /* get thunk entry point for connecting rhunk to an IRQ table */ |
<> | 149:156823d33999 | 148 | inline operator CThunkEntry(void) |
<> | 149:156823d33999 | 149 | { |
<> | 149:156823d33999 | 150 | return (CThunkEntry)entry(); |
<> | 149:156823d33999 | 151 | } |
<> | 149:156823d33999 | 152 | |
<> | 149:156823d33999 | 153 | /* get thunk entry point for connecting rhunk to an IRQ table */ |
<> | 149:156823d33999 | 154 | inline operator uint32_t(void) |
<> | 149:156823d33999 | 155 | { |
<> | 149:156823d33999 | 156 | return entry(); |
<> | 149:156823d33999 | 157 | } |
<> | 149:156823d33999 | 158 | |
<> | 149:156823d33999 | 159 | /* simple test function */ |
<> | 149:156823d33999 | 160 | inline void call(void) |
<> | 149:156823d33999 | 161 | { |
<> | 149:156823d33999 | 162 | (((CThunkEntry)(entry()))()); |
<> | 149:156823d33999 | 163 | } |
<> | 149:156823d33999 | 164 | |
<> | 149:156823d33999 | 165 | private: |
<> | 149:156823d33999 | 166 | T* m_instance; |
<> | 149:156823d33999 | 167 | volatile CCallback m_callback; |
<> | 149:156823d33999 | 168 | |
<> | 149:156823d33999 | 169 | // TODO: this needs proper fix, to refactor toolchain header file and all its use |
<> | 149:156823d33999 | 170 | // PACKED there is not defined properly for IAR |
<> | 149:156823d33999 | 171 | #if defined (__ICCARM__) |
<> | 149:156823d33999 | 172 | typedef __packed struct |
<> | 149:156823d33999 | 173 | { |
<> | 149:156823d33999 | 174 | CTHUNK_VARIABLES; |
<> | 149:156823d33999 | 175 | volatile uint32_t instance; |
<> | 149:156823d33999 | 176 | volatile uint32_t context; |
<> | 149:156823d33999 | 177 | volatile uint32_t callback; |
<> | 149:156823d33999 | 178 | volatile uint32_t trampoline; |
<> | 149:156823d33999 | 179 | } CThunkTrampoline; |
<> | 149:156823d33999 | 180 | #else |
<> | 149:156823d33999 | 181 | typedef struct |
<> | 149:156823d33999 | 182 | { |
<> | 149:156823d33999 | 183 | CTHUNK_VARIABLES; |
<> | 149:156823d33999 | 184 | volatile uint32_t instance; |
<> | 149:156823d33999 | 185 | volatile uint32_t context; |
<> | 149:156823d33999 | 186 | volatile uint32_t callback; |
<> | 149:156823d33999 | 187 | volatile uint32_t trampoline; |
<> | 149:156823d33999 | 188 | } __attribute__((__packed__)) CThunkTrampoline; |
<> | 149:156823d33999 | 189 | #endif |
<> | 149:156823d33999 | 190 | |
<> | 149:156823d33999 | 191 | static void trampoline(T* instance, void* context, CCallback* callback) |
<> | 149:156823d33999 | 192 | { |
<> | 149:156823d33999 | 193 | if(instance && *callback) { |
<> | 149:156823d33999 | 194 | (static_cast<T*>(instance)->**callback)(context); |
<> | 149:156823d33999 | 195 | } |
<> | 149:156823d33999 | 196 | } |
<> | 149:156823d33999 | 197 | |
<> | 149:156823d33999 | 198 | volatile CThunkTrampoline m_thunk; |
<> | 149:156823d33999 | 199 | |
<> | 149:156823d33999 | 200 | inline void init(T *instance, CCallback callback, void* context) |
<> | 149:156823d33999 | 201 | { |
<> | 149:156823d33999 | 202 | /* remember callback - need to add this level of redirection |
<> | 149:156823d33999 | 203 | as pointer size for member functions differs between platforms */ |
<> | 149:156823d33999 | 204 | m_callback = callback; |
<> | 149:156823d33999 | 205 | |
<> | 149:156823d33999 | 206 | /* populate thunking trampoline */ |
<> | 149:156823d33999 | 207 | CTHUNK_ASSIGMENT; |
<> | 149:156823d33999 | 208 | m_thunk.context = (uint32_t)context; |
<> | 149:156823d33999 | 209 | m_thunk.instance = (uint32_t)instance; |
<> | 149:156823d33999 | 210 | m_thunk.callback = (uint32_t)&m_callback; |
<> | 149:156823d33999 | 211 | m_thunk.trampoline = (uint32_t)&trampoline; |
<> | 149:156823d33999 | 212 | |
<> | 149:156823d33999 | 213 | #if defined(__CORTEX_A9) |
<> | 149:156823d33999 | 214 | /* Data cache clean */ |
<> | 149:156823d33999 | 215 | /* Cache control */ |
<> | 149:156823d33999 | 216 | { |
<> | 149:156823d33999 | 217 | uint32_t start_addr = (uint32_t)&m_thunk & 0xFFFFFFE0; |
<> | 149:156823d33999 | 218 | uint32_t end_addr = (uint32_t)&m_thunk + sizeof(m_thunk); |
<> | 149:156823d33999 | 219 | uint32_t addr; |
<> | 149:156823d33999 | 220 | |
<> | 149:156823d33999 | 221 | /* Data cache clean and invalid */ |
<> | 149:156823d33999 | 222 | for (addr = start_addr; addr < end_addr; addr += 0x20) { |
<> | 149:156823d33999 | 223 | __v7_clean_inv_dcache_mva((void *)addr); |
<> | 149:156823d33999 | 224 | } |
<> | 149:156823d33999 | 225 | /* Instruction cache invalid */ |
<> | 149:156823d33999 | 226 | __v7_inv_icache_all(); |
<> | 149:156823d33999 | 227 | __ca9u_inv_tlb_all(); |
<> | 149:156823d33999 | 228 | __v7_inv_btac(); |
<> | 149:156823d33999 | 229 | } |
<> | 149:156823d33999 | 230 | #endif |
<> | 149:156823d33999 | 231 | #if defined(__CORTEX_M7) |
<> | 149:156823d33999 | 232 | /* Data cache clean and invalid */ |
<> | 149:156823d33999 | 233 | SCB_CleanInvalidateDCache(); |
<> | 149:156823d33999 | 234 | |
<> | 149:156823d33999 | 235 | /* Instruction cache invalid */ |
<> | 149:156823d33999 | 236 | SCB_InvalidateICache(); |
<> | 149:156823d33999 | 237 | #endif |
<> | 149:156823d33999 | 238 | __ISB(); |
<> | 149:156823d33999 | 239 | __DSB(); |
<> | 149:156823d33999 | 240 | } |
<> | 149:156823d33999 | 241 | }; |
<> | 149:156823d33999 | 242 | |
<> | 149:156823d33999 | 243 | #endif/*__CTHUNK_H__*/ |
<> | 149:156823d33999 | 244 | |
<> | 149:156823d33999 | 245 | /** @}*/ |