Opencv 3.1 project on GR-PEACH board

Fork of gr-peach-opencv-project by the do

Committer:
thedo
Date:
Thu Jun 29 11:01:39 2017 +0000
Revision:
167:1657b442184c
Opencv 3.1 project on GR-PEACH board, 4 apps

Who changed what in which revision?

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