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