The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
Kojto
Date:
Wed Jul 19 16:46:19 2017 +0100
Revision:
147:a97add6d7e64
Parent:
145:64910690c574
Child:
152:235179ab3f27
Release 147 of the mbed library.

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);
AnnaBridge 145:64910690c574 75 /** @}*/
<> 128:9bcdf88f62b0 76
<> 128:9bcdf88f62b0 77 /**
<> 128:9bcdf88f62b0 78 * Class for created a pointer with data bound to it
<> 128:9bcdf88f62b0 79 *
AnnaBridge 145:64910690c574 80 * @note Synchronization level: Not protected
AnnaBridge 145:64910690c574 81 * @ingroup platform
<> 128:9bcdf88f62b0 82 */
<> 128:9bcdf88f62b0 83 template<class T>
<> 128:9bcdf88f62b0 84 class CThunk
<> 128:9bcdf88f62b0 85 {
<> 128:9bcdf88f62b0 86 public:
<> 128:9bcdf88f62b0 87 typedef void (T::*CCallbackSimple)(void);
<> 128:9bcdf88f62b0 88 typedef void (T::*CCallback)(void* context);
<> 128:9bcdf88f62b0 89
<> 128:9bcdf88f62b0 90 inline CThunk(T *instance)
<> 128:9bcdf88f62b0 91 {
<> 128:9bcdf88f62b0 92 init(instance, NULL, NULL);
<> 128:9bcdf88f62b0 93 }
<> 128:9bcdf88f62b0 94
<> 128:9bcdf88f62b0 95 inline CThunk(T *instance, CCallback callback)
<> 128:9bcdf88f62b0 96 {
<> 128:9bcdf88f62b0 97 init(instance, callback, NULL);
<> 128:9bcdf88f62b0 98 }
<> 128:9bcdf88f62b0 99
<> 128:9bcdf88f62b0 100 ~CThunk() {
<> 128:9bcdf88f62b0 101
<> 128:9bcdf88f62b0 102 }
<> 128:9bcdf88f62b0 103
<> 128:9bcdf88f62b0 104 inline CThunk(T *instance, CCallbackSimple callback)
<> 128:9bcdf88f62b0 105 {
<> 128:9bcdf88f62b0 106 init(instance, (CCallback)callback, NULL);
<> 128:9bcdf88f62b0 107 }
<> 128:9bcdf88f62b0 108
<> 128:9bcdf88f62b0 109 inline CThunk(T &instance, CCallback callback)
<> 128:9bcdf88f62b0 110 {
<> 128:9bcdf88f62b0 111 init(instance, callback, NULL);
<> 128:9bcdf88f62b0 112 }
<> 128:9bcdf88f62b0 113
<> 128:9bcdf88f62b0 114 inline CThunk(T &instance, CCallbackSimple callback)
<> 128:9bcdf88f62b0 115 {
<> 128:9bcdf88f62b0 116 init(instance, (CCallback)callback, NULL);
<> 128:9bcdf88f62b0 117 }
<> 128:9bcdf88f62b0 118
<> 128:9bcdf88f62b0 119 inline CThunk(T &instance, CCallback callback, void* context)
<> 128:9bcdf88f62b0 120 {
<> 128:9bcdf88f62b0 121 init(instance, callback, context);
<> 128:9bcdf88f62b0 122 }
<> 128:9bcdf88f62b0 123
<> 128:9bcdf88f62b0 124 inline void callback(CCallback callback)
<> 128:9bcdf88f62b0 125 {
<> 128:9bcdf88f62b0 126 m_callback = callback;
<> 128:9bcdf88f62b0 127 }
<> 128:9bcdf88f62b0 128
<> 128:9bcdf88f62b0 129 inline void callback(CCallbackSimple callback)
<> 128:9bcdf88f62b0 130 {
<> 128:9bcdf88f62b0 131 m_callback = (CCallback)callback;
<> 128:9bcdf88f62b0 132 }
<> 128:9bcdf88f62b0 133
<> 128:9bcdf88f62b0 134 inline void context(void* context)
<> 128:9bcdf88f62b0 135 {
<> 128:9bcdf88f62b0 136 m_thunk.context = (uint32_t)context;
<> 128:9bcdf88f62b0 137 }
<> 128:9bcdf88f62b0 138
<> 128:9bcdf88f62b0 139 inline void context(uint32_t context)
<> 128:9bcdf88f62b0 140 {
<> 128:9bcdf88f62b0 141 m_thunk.context = context;
<> 128:9bcdf88f62b0 142 }
<> 128:9bcdf88f62b0 143
<> 128:9bcdf88f62b0 144 inline uint32_t entry(void)
<> 128:9bcdf88f62b0 145 {
<> 128:9bcdf88f62b0 146 return (((uint32_t)&m_thunk)|CTHUNK_ADDRESS);
<> 128:9bcdf88f62b0 147 }
<> 128:9bcdf88f62b0 148
<> 128:9bcdf88f62b0 149 /* get thunk entry point for connecting rhunk to an IRQ table */
<> 128:9bcdf88f62b0 150 inline operator CThunkEntry(void)
<> 128:9bcdf88f62b0 151 {
<> 128:9bcdf88f62b0 152 return (CThunkEntry)entry();
<> 128:9bcdf88f62b0 153 }
<> 128:9bcdf88f62b0 154
<> 128:9bcdf88f62b0 155 /* get thunk entry point for connecting rhunk to an IRQ table */
<> 128:9bcdf88f62b0 156 inline operator uint32_t(void)
<> 128:9bcdf88f62b0 157 {
<> 128:9bcdf88f62b0 158 return entry();
<> 128:9bcdf88f62b0 159 }
<> 128:9bcdf88f62b0 160
<> 128:9bcdf88f62b0 161 /* simple test function */
<> 128:9bcdf88f62b0 162 inline void call(void)
<> 128:9bcdf88f62b0 163 {
<> 128:9bcdf88f62b0 164 (((CThunkEntry)(entry()))());
<> 128:9bcdf88f62b0 165 }
<> 128:9bcdf88f62b0 166
<> 128:9bcdf88f62b0 167 private:
<> 128:9bcdf88f62b0 168 T* m_instance;
<> 128:9bcdf88f62b0 169 volatile CCallback m_callback;
<> 128:9bcdf88f62b0 170
<> 128:9bcdf88f62b0 171 // TODO: this needs proper fix, to refactor toolchain header file and all its use
<> 128:9bcdf88f62b0 172 // PACKED there is not defined properly for IAR
<> 128:9bcdf88f62b0 173 #if defined (__ICCARM__)
<> 128:9bcdf88f62b0 174 typedef __packed struct
<> 128:9bcdf88f62b0 175 {
<> 128:9bcdf88f62b0 176 CTHUNK_VARIABLES;
<> 128:9bcdf88f62b0 177 volatile uint32_t instance;
<> 128:9bcdf88f62b0 178 volatile uint32_t context;
<> 128:9bcdf88f62b0 179 volatile uint32_t callback;
<> 128:9bcdf88f62b0 180 volatile uint32_t trampoline;
<> 128:9bcdf88f62b0 181 } CThunkTrampoline;
<> 128:9bcdf88f62b0 182 #else
<> 128:9bcdf88f62b0 183 typedef struct
<> 128:9bcdf88f62b0 184 {
<> 128:9bcdf88f62b0 185 CTHUNK_VARIABLES;
<> 128:9bcdf88f62b0 186 volatile uint32_t instance;
<> 128:9bcdf88f62b0 187 volatile uint32_t context;
<> 128:9bcdf88f62b0 188 volatile uint32_t callback;
<> 128:9bcdf88f62b0 189 volatile uint32_t trampoline;
<> 128:9bcdf88f62b0 190 } __attribute__((__packed__)) CThunkTrampoline;
<> 128:9bcdf88f62b0 191 #endif
<> 128:9bcdf88f62b0 192
<> 128:9bcdf88f62b0 193 static void trampoline(T* instance, void* context, CCallback* callback)
<> 128:9bcdf88f62b0 194 {
<> 128:9bcdf88f62b0 195 if(instance && *callback) {
<> 128:9bcdf88f62b0 196 (static_cast<T*>(instance)->**callback)(context);
<> 128:9bcdf88f62b0 197 }
<> 128:9bcdf88f62b0 198 }
<> 128:9bcdf88f62b0 199
<> 128:9bcdf88f62b0 200 volatile CThunkTrampoline m_thunk;
<> 128:9bcdf88f62b0 201
<> 128:9bcdf88f62b0 202 inline void init(T *instance, CCallback callback, void* context)
<> 128:9bcdf88f62b0 203 {
<> 128:9bcdf88f62b0 204 /* remember callback - need to add this level of redirection
<> 128:9bcdf88f62b0 205 as pointer size for member functions differs between platforms */
<> 128:9bcdf88f62b0 206 m_callback = callback;
<> 128:9bcdf88f62b0 207
<> 128:9bcdf88f62b0 208 /* populate thunking trampoline */
<> 128:9bcdf88f62b0 209 CTHUNK_ASSIGMENT;
<> 128:9bcdf88f62b0 210 m_thunk.context = (uint32_t)context;
<> 128:9bcdf88f62b0 211 m_thunk.instance = (uint32_t)instance;
<> 128:9bcdf88f62b0 212 m_thunk.callback = (uint32_t)&m_callback;
<> 128:9bcdf88f62b0 213 m_thunk.trampoline = (uint32_t)&trampoline;
<> 128:9bcdf88f62b0 214
<> 128:9bcdf88f62b0 215 #if defined(__CORTEX_A9)
<> 128:9bcdf88f62b0 216 /* Data cache clean */
<> 128:9bcdf88f62b0 217 /* Cache control */
<> 128:9bcdf88f62b0 218 {
<> 128:9bcdf88f62b0 219 uint32_t start_addr = (uint32_t)&m_thunk & 0xFFFFFFE0;
<> 128:9bcdf88f62b0 220 uint32_t end_addr = (uint32_t)&m_thunk + sizeof(m_thunk);
<> 128:9bcdf88f62b0 221 uint32_t addr;
<> 128:9bcdf88f62b0 222
<> 128:9bcdf88f62b0 223 /* Data cache clean and invalid */
<> 128:9bcdf88f62b0 224 for (addr = start_addr; addr < end_addr; addr += 0x20) {
<> 128:9bcdf88f62b0 225 __v7_clean_inv_dcache_mva((void *)addr);
<> 128:9bcdf88f62b0 226 }
<> 128:9bcdf88f62b0 227 /* Instruction cache invalid */
<> 128:9bcdf88f62b0 228 __v7_inv_icache_all();
<> 128:9bcdf88f62b0 229 __ca9u_inv_tlb_all();
<> 128:9bcdf88f62b0 230 __v7_inv_btac();
<> 128:9bcdf88f62b0 231 }
<> 128:9bcdf88f62b0 232 #endif
<> 128:9bcdf88f62b0 233 #if defined(__CORTEX_M7)
<> 128:9bcdf88f62b0 234 /* Data cache clean and invalid */
<> 128:9bcdf88f62b0 235 SCB_CleanInvalidateDCache();
<> 128:9bcdf88f62b0 236
<> 128:9bcdf88f62b0 237 /* Instruction cache invalid */
<> 128:9bcdf88f62b0 238 SCB_InvalidateICache();
<> 128:9bcdf88f62b0 239 #endif
<> 128:9bcdf88f62b0 240 __ISB();
<> 128:9bcdf88f62b0 241 __DSB();
<> 128:9bcdf88f62b0 242 }
<> 128:9bcdf88f62b0 243 };
<> 128:9bcdf88f62b0 244
<> 128:9bcdf88f62b0 245 #endif/*__CTHUNK_H__*/
<> 128:9bcdf88f62b0 246