ads1115 only

Fork of mbed by mbed official

Committer:
kaoshen
Date:
Tue Jan 17 23:27:32 2017 +0000
Revision:
135:fce8a9387ed1
Parent:
128:9bcdf88f62b0
333 ADS1115 ADC1

Who changed what in which revision?

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