I2C_EEPROM

Committer:
jhon309
Date:
Thu Aug 13 00:23:16 2015 +0000
Revision:
0:ac8863619623
I2C

Who changed what in which revision?

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