PIR grove sensor that sends the sensed data to Thingspeak.com through the wifi module ESP 8266

Dependencies:   mbed

Committer:
skrawool
Date:
Mon Dec 05 16:39:27 2016 +0000
Revision:
0:3954a906acc2
PIR grove sensor that sends the sensed data to thingspeak through the wifi module ESP 8266

Who changed what in which revision?

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