Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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