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