IOTIO

Dependencies:   Nucleo_BLE_API_IOTIO Nucleo_BLE_BlueNRG Nucleo_BLE_DemoApp Nucleo_Sensor_Shield mbed

Dependents:   Nucleo_BLE_Demo_IOTIO

Fork of Nucleo_BLE_Demo by Cortex Challenge Team

Committer:
16038618
Date:
Sat Oct 29 15:11:28 2016 +0000
Revision:
1:4bdfa7d7e8bf
IOTIO

Who changed what in which revision?

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