fix nrf51822 i2c & spi conflict

Dependencies:   BLE_API eMPL_MPU6050 nRF51822

Fork of Seeed_Tiny_BLE_Flash by Darren Huang

Committer:
yihui
Date:
Tue Nov 17 07:48:56 2015 +0000
Revision:
5:b8c02645e6af
fix i2c & spi conflict

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 5:b8c02645e6af 1 /* mbed Microcontroller Library
yihui 5:b8c02645e6af 2 * Copyright (c) 2006-2015 ARM Limited
yihui 5:b8c02645e6af 3 *
yihui 5:b8c02645e6af 4 * Licensed under the Apache License, Version 2.0 (the "License");
yihui 5:b8c02645e6af 5 * you may not use this file except in compliance with the License.
yihui 5:b8c02645e6af 6 * You may obtain a copy of the License at
yihui 5:b8c02645e6af 7 *
yihui 5:b8c02645e6af 8 * http://www.apache.org/licenses/LICENSE-2.0
yihui 5:b8c02645e6af 9 *
yihui 5:b8c02645e6af 10 * Unless required by applicable law or agreed to in writing, software
yihui 5:b8c02645e6af 11 * distributed under the License is distributed on an "AS IS" BASIS,
yihui 5:b8c02645e6af 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
yihui 5:b8c02645e6af 13 * See the License for the specific language governing permissions and
yihui 5:b8c02645e6af 14 * limitations under the License.
yihui 5:b8c02645e6af 15 */
yihui 5:b8c02645e6af 16 #ifndef MBED_FUNCTIONPOINTER_H
yihui 5:b8c02645e6af 17 #define MBED_FUNCTIONPOINTER_H
yihui 5:b8c02645e6af 18
yihui 5:b8c02645e6af 19 #include <string.h>
yihui 5:b8c02645e6af 20 #include <stdint.h>
yihui 5:b8c02645e6af 21
yihui 5:b8c02645e6af 22 namespace mbed {
yihui 5:b8c02645e6af 23
yihui 5:b8c02645e6af 24 /* If we had variaditic templates, this wouldn't be a problem, but until C++11 is enabled, we are stuck with multiple classes... */
yihui 5:b8c02645e6af 25
yihui 5:b8c02645e6af 26 /** A class for storing and calling a pointer to a static or member function
yihui 5:b8c02645e6af 27 */
yihui 5:b8c02645e6af 28 template <typename R, typename A1>
yihui 5:b8c02645e6af 29 class FunctionPointerArg1{
yihui 5:b8c02645e6af 30 public:
yihui 5:b8c02645e6af 31 /** Create a FunctionPointer, attaching a static function
yihui 5:b8c02645e6af 32 *
yihui 5:b8c02645e6af 33 * @param function The static function to attach (default is none)
yihui 5:b8c02645e6af 34 */
yihui 5:b8c02645e6af 35 FunctionPointerArg1(R (*function)(A1) = 0) {
yihui 5:b8c02645e6af 36 attach(function);
yihui 5:b8c02645e6af 37 }
yihui 5:b8c02645e6af 38
yihui 5:b8c02645e6af 39 /** Create a FunctionPointer, attaching a member function
yihui 5:b8c02645e6af 40 *
yihui 5:b8c02645e6af 41 * @param object The object pointer to invoke the member function on (i.e. the this pointer)
yihui 5:b8c02645e6af 42 * @param function The address of the member function to attach
yihui 5:b8c02645e6af 43 */
yihui 5:b8c02645e6af 44 template<typename T>
yihui 5:b8c02645e6af 45 FunctionPointerArg1(T *object, R (T::*member)(A1)) {
yihui 5:b8c02645e6af 46 attach(object, member);
yihui 5:b8c02645e6af 47 }
yihui 5:b8c02645e6af 48
yihui 5:b8c02645e6af 49 /** Attach a static function
yihui 5:b8c02645e6af 50 *
yihui 5:b8c02645e6af 51 * @param function The static function to attach (default is none)
yihui 5:b8c02645e6af 52 */
yihui 5:b8c02645e6af 53 void attach(R (*function)(A1)) {
yihui 5:b8c02645e6af 54 _p.function = function;
yihui 5:b8c02645e6af 55 _membercaller = 0;
yihui 5:b8c02645e6af 56 }
yihui 5:b8c02645e6af 57
yihui 5:b8c02645e6af 58 /** Attach a member function
yihui 5:b8c02645e6af 59 *
yihui 5:b8c02645e6af 60 * @param object The object pointer to invoke the member function on (i.e. the this pointer)
yihui 5:b8c02645e6af 61 * @param function The address of the member function to attach
yihui 5:b8c02645e6af 62 */
yihui 5:b8c02645e6af 63 template<typename T>
yihui 5:b8c02645e6af 64 void attach(T *object, R (T::*member)(A1)) {
yihui 5:b8c02645e6af 65 _p.object = static_cast<void*>(object);
yihui 5:b8c02645e6af 66 *reinterpret_cast<R (T::**)(A1)>(_member) = member;
yihui 5:b8c02645e6af 67 _membercaller = &FunctionPointerArg1::membercaller<T>;
yihui 5:b8c02645e6af 68 }
yihui 5:b8c02645e6af 69
yihui 5:b8c02645e6af 70 /** Call the attached static or member function
yihui 5:b8c02645e6af 71 */
yihui 5:b8c02645e6af 72 R call(A1 a) {
yihui 5:b8c02645e6af 73 if (_membercaller == 0 && _p.function) {
yihui 5:b8c02645e6af 74 return _p.function(a);
yihui 5:b8c02645e6af 75 } else if (_membercaller && _p.object) {
yihui 5:b8c02645e6af 76 return _membercaller(_p.object, _member, a);
yihui 5:b8c02645e6af 77 }
yihui 5:b8c02645e6af 78 return (R)0;
yihui 5:b8c02645e6af 79 }
yihui 5:b8c02645e6af 80
yihui 5:b8c02645e6af 81 /** Get registered static function
yihui 5:b8c02645e6af 82 */
yihui 5:b8c02645e6af 83 R(*get_function(A1))() {
yihui 5:b8c02645e6af 84 return _membercaller ? (R(*)(A1))0 : (R(*)(A1))_p.function;
yihui 5:b8c02645e6af 85 }
yihui 5:b8c02645e6af 86
yihui 5:b8c02645e6af 87 #ifdef MBED_OPERATORS
yihui 5:b8c02645e6af 88 R operator ()(A1 a) {
yihui 5:b8c02645e6af 89 return call(a);
yihui 5:b8c02645e6af 90 }
yihui 5:b8c02645e6af 91 operator bool(void) const {
yihui 5:b8c02645e6af 92 return (_membercaller != NULL ? _p.object : (void*)_p.function) != NULL;
yihui 5:b8c02645e6af 93 }
yihui 5:b8c02645e6af 94 #endif
yihui 5:b8c02645e6af 95 private:
yihui 5:b8c02645e6af 96 template<typename T>
yihui 5:b8c02645e6af 97 static R membercaller(void *object, uintptr_t *member, A1 a) {
yihui 5:b8c02645e6af 98 T* o = static_cast<T*>(object);
yihui 5:b8c02645e6af 99 R (T::**m)(A1) = reinterpret_cast<R (T::**)(A1)>(member);
yihui 5:b8c02645e6af 100 return (o->**m)(a);
yihui 5:b8c02645e6af 101 }
yihui 5:b8c02645e6af 102
yihui 5:b8c02645e6af 103 union {
yihui 5:b8c02645e6af 104 R (*function)(A1); // static function pointer
yihui 5:b8c02645e6af 105 void *object; // object this pointer
yihui 5:b8c02645e6af 106 } _p;
yihui 5:b8c02645e6af 107 uintptr_t _member[4]; // aligned raw member function pointer storage - converted back by registered _membercaller
yihui 5:b8c02645e6af 108 R (*_membercaller)(void*, uintptr_t*, A1); // registered membercaller function to convert back and call _m.member on _object
yihui 5:b8c02645e6af 109 };
yihui 5:b8c02645e6af 110
yihui 5:b8c02645e6af 111 /** A class for storing and calling a pointer to a static or member function (R ()(void))
yihui 5:b8c02645e6af 112 */
yihui 5:b8c02645e6af 113 template <typename R>
yihui 5:b8c02645e6af 114 class FunctionPointerArg1<R, void>{
yihui 5:b8c02645e6af 115 public:
yihui 5:b8c02645e6af 116 /** Create a FunctionPointer, attaching a static function
yihui 5:b8c02645e6af 117 *
yihui 5:b8c02645e6af 118 * @param function The static function to attach (default is none)
yihui 5:b8c02645e6af 119 */
yihui 5:b8c02645e6af 120 FunctionPointerArg1(R (*function)(void) = 0) {
yihui 5:b8c02645e6af 121 attach(function);
yihui 5:b8c02645e6af 122 }
yihui 5:b8c02645e6af 123
yihui 5:b8c02645e6af 124 /** Create a FunctionPointer, attaching a member function
yihui 5:b8c02645e6af 125 *
yihui 5:b8c02645e6af 126 * @param object The object pointer to invoke the member function on (i.e. the this pointer)
yihui 5:b8c02645e6af 127 * @param function The address of the void member function to attach
yihui 5:b8c02645e6af 128 */
yihui 5:b8c02645e6af 129 template<typename T>
yihui 5:b8c02645e6af 130 FunctionPointerArg1(T *object, R (T::*member)(void)) {
yihui 5:b8c02645e6af 131 attach(object, member);
yihui 5:b8c02645e6af 132 }
yihui 5:b8c02645e6af 133
yihui 5:b8c02645e6af 134 /** Attach a static function
yihui 5:b8c02645e6af 135 *
yihui 5:b8c02645e6af 136 * @param function The void static function to attach (default is none)
yihui 5:b8c02645e6af 137 */
yihui 5:b8c02645e6af 138 void attach(R (*function)(void)) {
yihui 5:b8c02645e6af 139 _p.function = function;
yihui 5:b8c02645e6af 140 _membercaller = 0;
yihui 5:b8c02645e6af 141 }
yihui 5:b8c02645e6af 142
yihui 5:b8c02645e6af 143 /** Attach a member function
yihui 5:b8c02645e6af 144 *
yihui 5:b8c02645e6af 145 * @param object The object pointer to invoke the member function on (i.e. the this pointer)
yihui 5:b8c02645e6af 146 * @param function The address of the void member function to attach
yihui 5:b8c02645e6af 147 */
yihui 5:b8c02645e6af 148 template<typename T>
yihui 5:b8c02645e6af 149 void attach(T *object, R (T::*member)(void)) {
yihui 5:b8c02645e6af 150 _p.object = static_cast<void*>(object);
yihui 5:b8c02645e6af 151 *reinterpret_cast<R (T::**)(void)>(_member) = member;
yihui 5:b8c02645e6af 152 _membercaller = &FunctionPointerArg1::membercaller<T>;
yihui 5:b8c02645e6af 153 }
yihui 5:b8c02645e6af 154
yihui 5:b8c02645e6af 155 /** Call the attached static or member function
yihui 5:b8c02645e6af 156 */
yihui 5:b8c02645e6af 157 R call(){
yihui 5:b8c02645e6af 158 if (_membercaller == 0 && _p.function) {
yihui 5:b8c02645e6af 159 return _p.function();
yihui 5:b8c02645e6af 160 } else if (_membercaller && _p.object) {
yihui 5:b8c02645e6af 161 return _membercaller(_p.object, _member);
yihui 5:b8c02645e6af 162 }
yihui 5:b8c02645e6af 163 return (R)0;
yihui 5:b8c02645e6af 164 }
yihui 5:b8c02645e6af 165
yihui 5:b8c02645e6af 166 /** Get registered static function
yihui 5:b8c02645e6af 167 */
yihui 5:b8c02645e6af 168 R(*get_function())() {
yihui 5:b8c02645e6af 169 return _membercaller ? (R(*)())0 : (R(*)())_p.function;
yihui 5:b8c02645e6af 170 }
yihui 5:b8c02645e6af 171
yihui 5:b8c02645e6af 172 #ifdef MBED_OPERATORS
yihui 5:b8c02645e6af 173 R operator ()(void) {
yihui 5:b8c02645e6af 174 return call();
yihui 5:b8c02645e6af 175 }
yihui 5:b8c02645e6af 176 operator bool(void) const {
yihui 5:b8c02645e6af 177 return (_membercaller != NULL ? _p.object : (void*)_p.function) != NULL;
yihui 5:b8c02645e6af 178 }
yihui 5:b8c02645e6af 179 #endif
yihui 5:b8c02645e6af 180
yihui 5:b8c02645e6af 181 private:
yihui 5:b8c02645e6af 182 template<typename T>
yihui 5:b8c02645e6af 183 static R membercaller(void *object, uintptr_t *member) {
yihui 5:b8c02645e6af 184 T* o = static_cast<T*>(object);
yihui 5:b8c02645e6af 185 R (T::**m)(void) = reinterpret_cast<R (T::**)(void)>(member);
yihui 5:b8c02645e6af 186 return (o->**m)();
yihui 5:b8c02645e6af 187 }
yihui 5:b8c02645e6af 188
yihui 5:b8c02645e6af 189 union {
yihui 5:b8c02645e6af 190 R (*function)(void); // static function pointer
yihui 5:b8c02645e6af 191 void *object; // object this pointer
yihui 5:b8c02645e6af 192 } _p;
yihui 5:b8c02645e6af 193 uintptr_t _member[4]; // aligned raw member function pointer storage - converted back by registered _membercaller
yihui 5:b8c02645e6af 194 R (*_membercaller)(void*, uintptr_t*); // registered membercaller function to convert back and call _m.member on _object
yihui 5:b8c02645e6af 195 };
yihui 5:b8c02645e6af 196
yihui 5:b8c02645e6af 197 typedef FunctionPointerArg1<void, void> FunctionPointer;
yihui 5:b8c02645e6af 198 typedef FunctionPointerArg1<void, int> event_callback_t;
yihui 5:b8c02645e6af 199
yihui 5:b8c02645e6af 200 } // namespace mbed
yihui 5:b8c02645e6af 201
yihui 5:b8c02645e6af 202 #endif