,,

Fork of Application by Daniel Sygut

Committer:
Zaitsev
Date:
Thu Feb 15 14:29:23 2018 +0000
Revision:
15:2a20c3d2616e
Parent:
10:41552d038a69
j

Who changed what in which revision?

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