initial

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:32:24 2016 +0000
Revision:
0:638edba3adf6
initial

Who changed what in which revision?

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