mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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