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