Forked.

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Wed Jun 21 17:46:44 2017 +0100
Revision:
167:e84263d55307
Parent:
149:156823d33999
This updates the lib to the mbed lib v 145

Who changed what in which revision?

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