Final version.

Dependencies:   F746_GUI F746_SAI_IO UIT_FFT_Real

Fork of F746_Spectrogram by 不韋 呂

Committer:
MikamiUitOpen
Date:
Sun Oct 02 10:51:10 2016 +0000
Revision:
4:c1beacfc42c7
5

Who changed what in which revision?

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