test

Dependents:   robotic_fish_6

Committer:
juansal12
Date:
Fri Dec 03 23:00:34 2021 +0000
Revision:
0:c792b17d9f78
uploaded sofi code ;

Who changed what in which revision?

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