mbed library for NZ32-SC151

Committer:
modtronix-com
Date:
Fri Aug 19 15:46:42 2016 +1000
Revision:
17:639ed60ce759
Parent:
1:71204b8406f2
Added tag v1.1 for changeset 076cbe3e55be

Who changed what in which revision?

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