Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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