dhgdh

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by joey shelton

Committer:
cyberjoey
Date:
Sat Oct 22 01:31:58 2016 +0000
Revision:
9:6bb35cef007d
Parent:
1:55a6170b404f
WORKING

Who changed what in which revision?

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