Dataloger

Committer:
jhon309
Date:
Thu Aug 20 00:37:14 2015 +0000
Revision:
0:666850d06c9f
ok

Who changed what in which revision?

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