mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed Sep 30 17:00:09 2015 +0100
Revision:
636:a11c0372f0ba
Parent:
525:c320967f86b9
Synchronized with git revision d29c98dae61be0946ddf3a3c641c7726056f9452

Full URL: https://github.com/mbedmicro/mbed/commit/d29c98dae61be0946ddf3a3c641c7726056f9452/

Added support for SAMW25

Who changed what in which revision?

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