CDY version that shares functionality with Counter

Dependencies:   SDFileSystem_HelloWorld mbed FATFileSystem

Committer:
Charles David Young
Date:
Mon Nov 05 09:52:17 2018 -0700
Revision:
3:c547dba5d39b
Parent:
0:aa13e1c335cd
debug

Who changed what in which revision?

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