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