test

Dependents:   robotic_fish_7

Committer:
juansal12
Date:
Tue Jan 14 19:14:29 2020 +0000
Revision:
0:44931ff4a3ed
Sofi 7 code;

Who changed what in which revision?

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