Maxim mbed development library

Dependents:   sensomed

Committer:
switches
Date:
Tue Nov 08 18:27:11 2016 +0000
Revision:
0:0e018d759a2a
Initial commit

Who changed what in which revision?

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