DHT11

Committer:
jhon309
Date:
Thu Aug 13 00:21:57 2015 +0000
Revision:
0:c52df770855b
DHT11

Who changed what in which revision?

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