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