Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

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