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:
AnnaBridge
Date:
Mon Oct 02 15:20:36 2017 +0100
Revision:
152:235179ab3f27
Parent:
145:64910690c574
Release 152 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
AnnaBridge 152:235179ab3f27 40 #if (defined(__CORTEX_M3) || defined(__CORTEX_M4) || defined(__CORTEX_M7) || defined(__CORTEX_A9) \
AnnaBridge 152:235179ab3f27 41 || defined(__CORTEX_M23) || defined(__CORTEX_M33))
<> 128:9bcdf88f62b0 42 /**
<> 128:9bcdf88f62b0 43 * CTHUNK disassembly for Cortex-M3/M4/M7/A9 (thumb2):
<> 128:9bcdf88f62b0 44 * * adr r0, #4
<> 128:9bcdf88f62b0 45 * * ldm r0, {r0, r1, r2, pc}
<> 128:9bcdf88f62b0 46 *
<> 128:9bcdf88f62b0 47 * This instruction loads the arguments for the static thunking function to r0-r2, and
<> 128:9bcdf88f62b0 48 * branches to that function by loading its address into PC.
<> 128:9bcdf88f62b0 49 *
<> 128:9bcdf88f62b0 50 * This is safe for both regular calling and interrupt calling, since it only touches scratch registers
<> 128:9bcdf88f62b0 51 * which should be saved by the caller, and are automatically saved as part of the IRQ context switch.
<> 128:9bcdf88f62b0 52 */
<> 128:9bcdf88f62b0 53 #define CTHUNK_ASSIGMENT do { \
<> 128:9bcdf88f62b0 54 m_thunk.code[0] = 0xE890A001; \
<> 128:9bcdf88f62b0 55 m_thunk.code[1] = 0x00008007; \
<> 128:9bcdf88f62b0 56 } while (0)
<> 128:9bcdf88f62b0 57
<> 128:9bcdf88f62b0 58 #elif (defined(__CORTEX_M0PLUS) || defined(__CORTEX_M0))
<> 128:9bcdf88f62b0 59 /*
<> 128:9bcdf88f62b0 60 * CTHUNK disassembly for Cortex M0/M0+ (thumb):
<> 128:9bcdf88f62b0 61 * * adr r0, #4
<> 128:9bcdf88f62b0 62 * * ldm r0, {r0, r1, r2, r3}
<> 128:9bcdf88f62b0 63 * * bx r3
<> 128:9bcdf88f62b0 64 */
<> 128:9bcdf88f62b0 65 #define CTHUNK_ASSIGMENT do { \
<> 128:9bcdf88f62b0 66 m_thunk.code[0] = 0xC80FA001; \
<> 128:9bcdf88f62b0 67 m_thunk.code[1] = 0x00004718; \
<> 128:9bcdf88f62b0 68 } while (0)
<> 128:9bcdf88f62b0 69
<> 128:9bcdf88f62b0 70 #else
<> 128:9bcdf88f62b0 71 #error "Target is not currently suported."
<> 128:9bcdf88f62b0 72 #endif
<> 128:9bcdf88f62b0 73
<> 128:9bcdf88f62b0 74 /* IRQ/Exception compatible thunk entry function */
<> 128:9bcdf88f62b0 75 typedef void (*CThunkEntry)(void);
AnnaBridge 145:64910690c574 76 /** @}*/
<> 128:9bcdf88f62b0 77
<> 128:9bcdf88f62b0 78 /**
<> 128:9bcdf88f62b0 79 * Class for created a pointer with data bound to it
<> 128:9bcdf88f62b0 80 *
AnnaBridge 145:64910690c574 81 * @note Synchronization level: Not protected
AnnaBridge 145:64910690c574 82 * @ingroup platform
<> 128:9bcdf88f62b0 83 */
<> 128:9bcdf88f62b0 84 template<class T>
<> 128:9bcdf88f62b0 85 class CThunk
<> 128:9bcdf88f62b0 86 {
<> 128:9bcdf88f62b0 87 public:
<> 128:9bcdf88f62b0 88 typedef void (T::*CCallbackSimple)(void);
<> 128:9bcdf88f62b0 89 typedef void (T::*CCallback)(void* context);
<> 128:9bcdf88f62b0 90
<> 128:9bcdf88f62b0 91 inline CThunk(T *instance)
<> 128:9bcdf88f62b0 92 {
<> 128:9bcdf88f62b0 93 init(instance, NULL, NULL);
<> 128:9bcdf88f62b0 94 }
<> 128:9bcdf88f62b0 95
<> 128:9bcdf88f62b0 96 inline CThunk(T *instance, CCallback callback)
<> 128:9bcdf88f62b0 97 {
<> 128:9bcdf88f62b0 98 init(instance, callback, NULL);
<> 128:9bcdf88f62b0 99 }
<> 128:9bcdf88f62b0 100
<> 128:9bcdf88f62b0 101 ~CThunk() {
<> 128:9bcdf88f62b0 102
<> 128:9bcdf88f62b0 103 }
<> 128:9bcdf88f62b0 104
<> 128:9bcdf88f62b0 105 inline CThunk(T *instance, CCallbackSimple callback)
<> 128:9bcdf88f62b0 106 {
<> 128:9bcdf88f62b0 107 init(instance, (CCallback)callback, NULL);
<> 128:9bcdf88f62b0 108 }
<> 128:9bcdf88f62b0 109
<> 128:9bcdf88f62b0 110 inline CThunk(T &instance, CCallback callback)
<> 128:9bcdf88f62b0 111 {
<> 128:9bcdf88f62b0 112 init(instance, callback, NULL);
<> 128:9bcdf88f62b0 113 }
<> 128:9bcdf88f62b0 114
<> 128:9bcdf88f62b0 115 inline CThunk(T &instance, CCallbackSimple callback)
<> 128:9bcdf88f62b0 116 {
<> 128:9bcdf88f62b0 117 init(instance, (CCallback)callback, NULL);
<> 128:9bcdf88f62b0 118 }
<> 128:9bcdf88f62b0 119
<> 128:9bcdf88f62b0 120 inline CThunk(T &instance, CCallback callback, void* context)
<> 128:9bcdf88f62b0 121 {
<> 128:9bcdf88f62b0 122 init(instance, callback, context);
<> 128:9bcdf88f62b0 123 }
<> 128:9bcdf88f62b0 124
<> 128:9bcdf88f62b0 125 inline void callback(CCallback callback)
<> 128:9bcdf88f62b0 126 {
<> 128:9bcdf88f62b0 127 m_callback = callback;
<> 128:9bcdf88f62b0 128 }
<> 128:9bcdf88f62b0 129
<> 128:9bcdf88f62b0 130 inline void callback(CCallbackSimple callback)
<> 128:9bcdf88f62b0 131 {
<> 128:9bcdf88f62b0 132 m_callback = (CCallback)callback;
<> 128:9bcdf88f62b0 133 }
<> 128:9bcdf88f62b0 134
<> 128:9bcdf88f62b0 135 inline void context(void* context)
<> 128:9bcdf88f62b0 136 {
<> 128:9bcdf88f62b0 137 m_thunk.context = (uint32_t)context;
<> 128:9bcdf88f62b0 138 }
<> 128:9bcdf88f62b0 139
<> 128:9bcdf88f62b0 140 inline void context(uint32_t context)
<> 128:9bcdf88f62b0 141 {
<> 128:9bcdf88f62b0 142 m_thunk.context = context;
<> 128:9bcdf88f62b0 143 }
<> 128:9bcdf88f62b0 144
<> 128:9bcdf88f62b0 145 inline uint32_t entry(void)
<> 128:9bcdf88f62b0 146 {
<> 128:9bcdf88f62b0 147 return (((uint32_t)&m_thunk)|CTHUNK_ADDRESS);
<> 128:9bcdf88f62b0 148 }
<> 128:9bcdf88f62b0 149
<> 128:9bcdf88f62b0 150 /* get thunk entry point for connecting rhunk to an IRQ table */
<> 128:9bcdf88f62b0 151 inline operator CThunkEntry(void)
<> 128:9bcdf88f62b0 152 {
<> 128:9bcdf88f62b0 153 return (CThunkEntry)entry();
<> 128:9bcdf88f62b0 154 }
<> 128:9bcdf88f62b0 155
<> 128:9bcdf88f62b0 156 /* get thunk entry point for connecting rhunk to an IRQ table */
<> 128:9bcdf88f62b0 157 inline operator uint32_t(void)
<> 128:9bcdf88f62b0 158 {
<> 128:9bcdf88f62b0 159 return entry();
<> 128:9bcdf88f62b0 160 }
<> 128:9bcdf88f62b0 161
<> 128:9bcdf88f62b0 162 /* simple test function */
<> 128:9bcdf88f62b0 163 inline void call(void)
<> 128:9bcdf88f62b0 164 {
<> 128:9bcdf88f62b0 165 (((CThunkEntry)(entry()))());
<> 128:9bcdf88f62b0 166 }
<> 128:9bcdf88f62b0 167
<> 128:9bcdf88f62b0 168 private:
<> 128:9bcdf88f62b0 169 T* m_instance;
<> 128:9bcdf88f62b0 170 volatile CCallback m_callback;
<> 128:9bcdf88f62b0 171
<> 128:9bcdf88f62b0 172 // TODO: this needs proper fix, to refactor toolchain header file and all its use
<> 128:9bcdf88f62b0 173 // PACKED there is not defined properly for IAR
<> 128:9bcdf88f62b0 174 #if defined (__ICCARM__)
<> 128:9bcdf88f62b0 175 typedef __packed struct
<> 128:9bcdf88f62b0 176 {
<> 128:9bcdf88f62b0 177 CTHUNK_VARIABLES;
<> 128:9bcdf88f62b0 178 volatile uint32_t instance;
<> 128:9bcdf88f62b0 179 volatile uint32_t context;
<> 128:9bcdf88f62b0 180 volatile uint32_t callback;
<> 128:9bcdf88f62b0 181 volatile uint32_t trampoline;
<> 128:9bcdf88f62b0 182 } CThunkTrampoline;
<> 128:9bcdf88f62b0 183 #else
<> 128:9bcdf88f62b0 184 typedef struct
<> 128:9bcdf88f62b0 185 {
<> 128:9bcdf88f62b0 186 CTHUNK_VARIABLES;
<> 128:9bcdf88f62b0 187 volatile uint32_t instance;
<> 128:9bcdf88f62b0 188 volatile uint32_t context;
<> 128:9bcdf88f62b0 189 volatile uint32_t callback;
<> 128:9bcdf88f62b0 190 volatile uint32_t trampoline;
<> 128:9bcdf88f62b0 191 } __attribute__((__packed__)) CThunkTrampoline;
<> 128:9bcdf88f62b0 192 #endif
<> 128:9bcdf88f62b0 193
<> 128:9bcdf88f62b0 194 static void trampoline(T* instance, void* context, CCallback* callback)
<> 128:9bcdf88f62b0 195 {
<> 128:9bcdf88f62b0 196 if(instance && *callback) {
<> 128:9bcdf88f62b0 197 (static_cast<T*>(instance)->**callback)(context);
<> 128:9bcdf88f62b0 198 }
<> 128:9bcdf88f62b0 199 }
<> 128:9bcdf88f62b0 200
<> 128:9bcdf88f62b0 201 volatile CThunkTrampoline m_thunk;
<> 128:9bcdf88f62b0 202
<> 128:9bcdf88f62b0 203 inline void init(T *instance, CCallback callback, void* context)
<> 128:9bcdf88f62b0 204 {
<> 128:9bcdf88f62b0 205 /* remember callback - need to add this level of redirection
<> 128:9bcdf88f62b0 206 as pointer size for member functions differs between platforms */
<> 128:9bcdf88f62b0 207 m_callback = callback;
<> 128:9bcdf88f62b0 208
<> 128:9bcdf88f62b0 209 /* populate thunking trampoline */
<> 128:9bcdf88f62b0 210 CTHUNK_ASSIGMENT;
<> 128:9bcdf88f62b0 211 m_thunk.context = (uint32_t)context;
<> 128:9bcdf88f62b0 212 m_thunk.instance = (uint32_t)instance;
<> 128:9bcdf88f62b0 213 m_thunk.callback = (uint32_t)&m_callback;
<> 128:9bcdf88f62b0 214 m_thunk.trampoline = (uint32_t)&trampoline;
<> 128:9bcdf88f62b0 215
<> 128:9bcdf88f62b0 216 #if defined(__CORTEX_A9)
<> 128:9bcdf88f62b0 217 /* Data cache clean */
<> 128:9bcdf88f62b0 218 /* Cache control */
<> 128:9bcdf88f62b0 219 {
<> 128:9bcdf88f62b0 220 uint32_t start_addr = (uint32_t)&m_thunk & 0xFFFFFFE0;
<> 128:9bcdf88f62b0 221 uint32_t end_addr = (uint32_t)&m_thunk + sizeof(m_thunk);
<> 128:9bcdf88f62b0 222 uint32_t addr;
<> 128:9bcdf88f62b0 223
<> 128:9bcdf88f62b0 224 /* Data cache clean and invalid */
<> 128:9bcdf88f62b0 225 for (addr = start_addr; addr < end_addr; addr += 0x20) {
<> 128:9bcdf88f62b0 226 __v7_clean_inv_dcache_mva((void *)addr);
<> 128:9bcdf88f62b0 227 }
<> 128:9bcdf88f62b0 228 /* Instruction cache invalid */
<> 128:9bcdf88f62b0 229 __v7_inv_icache_all();
<> 128:9bcdf88f62b0 230 __ca9u_inv_tlb_all();
<> 128:9bcdf88f62b0 231 __v7_inv_btac();
<> 128:9bcdf88f62b0 232 }
<> 128:9bcdf88f62b0 233 #endif
<> 128:9bcdf88f62b0 234 #if defined(__CORTEX_M7)
<> 128:9bcdf88f62b0 235 /* Data cache clean and invalid */
<> 128:9bcdf88f62b0 236 SCB_CleanInvalidateDCache();
<> 128:9bcdf88f62b0 237
<> 128:9bcdf88f62b0 238 /* Instruction cache invalid */
<> 128:9bcdf88f62b0 239 SCB_InvalidateICache();
<> 128:9bcdf88f62b0 240 #endif
<> 128:9bcdf88f62b0 241 __ISB();
<> 128:9bcdf88f62b0 242 __DSB();
<> 128:9bcdf88f62b0 243 }
<> 128:9bcdf88f62b0 244 };
<> 128:9bcdf88f62b0 245
<> 128:9bcdf88f62b0 246 #endif/*__CTHUNK_H__*/
<> 128:9bcdf88f62b0 247