mbed.h library with any bug fixes AV finds.

Dependents:   micromouse4_encoder_testing PID_Test Lab1_Test WorkingPID ... more

Committer:
aravindsv
Date:
Mon Nov 02 03:07:12 2015 +0000
Revision:
1:ebce2ad32f95
Parent:
0:ba7650f404af
Changed the RCC timeout value to 500 ms, so total code startup time before program starts running is ~1s. Hopefully no side-effects from lower startup timeouts

Who changed what in which revision?

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