fix nrf51822 i2c & spi conflict

Dependencies:   BLE_API eMPL_MPU6050 nRF51822

Fork of Seeed_Tiny_BLE_Flash by Darren Huang

Committer:
yihui
Date:
Tue Nov 17 07:48:56 2015 +0000
Revision:
5:b8c02645e6af
fix i2c & spi conflict

Who changed what in which revision?

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