mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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