To get started with Seeed Tiny BLE, include detecting motion, button and battery level.

Dependencies:   BLE_API eMPL_MPU6050 mbed nRF51822

Committer:
yihui
Date:
Wed Apr 22 07:47:17 2015 +0000
Revision:
1:fc2f9d636751
update libraries; ; delete nRF51822/nordic-sdk/components/gpiote/app_gpiote.c to solve GPIOTE_IRQHandler multiply defined issue. temperarily change nRF51822 library to folder

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 1:fc2f9d636751 1 /**************************************************************************/
yihui 1:fc2f9d636751 2 /*!
yihui 1:fc2f9d636751 3 @file common.h
yihui 1:fc2f9d636751 4 @author hathach (tinyusb.org)
yihui 1:fc2f9d636751 5
yihui 1:fc2f9d636751 6 @section LICENSE
yihui 1:fc2f9d636751 7
yihui 1:fc2f9d636751 8 Software License Agreement (BSD License)
yihui 1:fc2f9d636751 9
yihui 1:fc2f9d636751 10 Copyright (c) 2013, K. Townsend (microBuilder.eu)
yihui 1:fc2f9d636751 11 All rights reserved.
yihui 1:fc2f9d636751 12
yihui 1:fc2f9d636751 13 Redistribution and use in source and binary forms, with or without
yihui 1:fc2f9d636751 14 modification, are permitted provided that the following conditions are met:
yihui 1:fc2f9d636751 15 1. Redistributions of source code must retain the above copyright
yihui 1:fc2f9d636751 16 notice, this list of conditions and the following disclaimer.
yihui 1:fc2f9d636751 17 2. Redistributions in binary form must reproduce the above copyright
yihui 1:fc2f9d636751 18 notice, this list of conditions and the following disclaimer in the
yihui 1:fc2f9d636751 19 documentation and/or other materials provided with the distribution.
yihui 1:fc2f9d636751 20 3. Neither the name of the copyright holders nor the
yihui 1:fc2f9d636751 21 names of its contributors may be used to endorse or promote products
yihui 1:fc2f9d636751 22 derived from this software without specific prior written permission.
yihui 1:fc2f9d636751 23
yihui 1:fc2f9d636751 24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
yihui 1:fc2f9d636751 25 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
yihui 1:fc2f9d636751 26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
yihui 1:fc2f9d636751 27 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
yihui 1:fc2f9d636751 28 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
yihui 1:fc2f9d636751 29 INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
yihui 1:fc2f9d636751 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND
yihui 1:fc2f9d636751 31 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
yihui 1:fc2f9d636751 32 INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
yihui 1:fc2f9d636751 33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
yihui 1:fc2f9d636751 34 */
yihui 1:fc2f9d636751 35 /**************************************************************************/
yihui 1:fc2f9d636751 36
yihui 1:fc2f9d636751 37 /** \defgroup Group_Common Common Files
yihui 1:fc2f9d636751 38 * @{
yihui 1:fc2f9d636751 39 *
yihui 1:fc2f9d636751 40 * \defgroup Group_CommonH common.h
yihui 1:fc2f9d636751 41 *
yihui 1:fc2f9d636751 42 * @{
yihui 1:fc2f9d636751 43 */
yihui 1:fc2f9d636751 44
yihui 1:fc2f9d636751 45 #ifndef _COMMON_H_
yihui 1:fc2f9d636751 46 #define _COMMON_H_
yihui 1:fc2f9d636751 47
yihui 1:fc2f9d636751 48 #ifdef __cplusplus
yihui 1:fc2f9d636751 49 extern "C" {
yihui 1:fc2f9d636751 50 #endif
yihui 1:fc2f9d636751 51
yihui 1:fc2f9d636751 52 //--------------------------------------------------------------------+
yihui 1:fc2f9d636751 53 // INCLUDES
yihui 1:fc2f9d636751 54 //--------------------------------------------------------------------+
yihui 1:fc2f9d636751 55
yihui 1:fc2f9d636751 56 //------------- Standard Header -------------//
yihui 1:fc2f9d636751 57 #include <stdint.h>
yihui 1:fc2f9d636751 58 #include <stdbool.h>
yihui 1:fc2f9d636751 59 #include <stddef.h>
yihui 1:fc2f9d636751 60 #include <string.h>
yihui 1:fc2f9d636751 61 #include <stdio.h>
yihui 1:fc2f9d636751 62
yihui 1:fc2f9d636751 63 //------------- General Header -------------//
yihui 1:fc2f9d636751 64 #include "projectconfig.h"
yihui 1:fc2f9d636751 65 #include "compiler.h"
yihui 1:fc2f9d636751 66 #include "assertion.h"
yihui 1:fc2f9d636751 67 #include "binary.h"
yihui 1:fc2f9d636751 68 #include "ble_error.h"
yihui 1:fc2f9d636751 69
yihui 1:fc2f9d636751 70 //------------- MCU header -------------//
yihui 1:fc2f9d636751 71 //#include "nrf.h"
yihui 1:fc2f9d636751 72
yihui 1:fc2f9d636751 73 //--------------------------------------------------------------------+
yihui 1:fc2f9d636751 74 // TYPEDEFS
yihui 1:fc2f9d636751 75 //--------------------------------------------------------------------+
yihui 1:fc2f9d636751 76 typedef unsigned char byte_t;
yihui 1:fc2f9d636751 77 typedef float float32_t;
yihui 1:fc2f9d636751 78 typedef double float64_t;
yihui 1:fc2f9d636751 79
yihui 1:fc2f9d636751 80 //--------------------------------------------------------------------+
yihui 1:fc2f9d636751 81 // MACROS
yihui 1:fc2f9d636751 82 //--------------------------------------------------------------------+
yihui 1:fc2f9d636751 83 #define STRING_(x) #x // stringify without expand
yihui 1:fc2f9d636751 84 #define XSTRING_(x) STRING_(x) // expand then stringify
yihui 1:fc2f9d636751 85 #define STRING_CONCAT_(a, b) a##b // concat without expand
yihui 1:fc2f9d636751 86 #define XSTRING_CONCAT_(a, b) STRING_CONCAT_(a, b) // expand then concat
yihui 1:fc2f9d636751 87
yihui 1:fc2f9d636751 88 #define U16_HIGH_U8(u16) ((uint8_t) (((u16) >> 8) & 0x00ff))
yihui 1:fc2f9d636751 89 #define U16_LOW_U8(u16) ((uint8_t) ((u16) & 0x00ff))
yihui 1:fc2f9d636751 90 #define U16_TO_U8S_BE(u16) U16_HIGH_U8(u16), U16_LOW_U8(u16)
yihui 1:fc2f9d636751 91 #define U16_TO_U8S_LE(u16) U16_LOW_U8(u16), U16_HIGH_U8(u16)
yihui 1:fc2f9d636751 92
yihui 1:fc2f9d636751 93 #define U32_B1_U8(u32) ((uint8_t) (((u32) >> 24) & 0x000000ff)) // MSB
yihui 1:fc2f9d636751 94 #define U32_B2_U8(u32) ((uint8_t) (((u32) >> 16) & 0x000000ff))
yihui 1:fc2f9d636751 95 #define U32_B3_U8(u32) ((uint8_t) (((u32) >> 8) & 0x000000ff))
yihui 1:fc2f9d636751 96 #define U32_B4_U8(u32) ((uint8_t) ((u32) & 0x000000ff)) // LSB
yihui 1:fc2f9d636751 97
yihui 1:fc2f9d636751 98 #define U32_TO_U8S_BE(u32) U32_B1_U8(u32), U32_B2_U8(u32), U32_B3_U8(u32), U32_B4_U8(u32)
yihui 1:fc2f9d636751 99 #define U32_TO_U8S_LE(u32) U32_B4_U8(u32), U32_B3_U8(u32), U32_B2_U8(u32), U32_B1_U8(u32)
yihui 1:fc2f9d636751 100
yihui 1:fc2f9d636751 101 //--------------------------------------------------------------------+
yihui 1:fc2f9d636751 102 // INLINE FUNCTION
yihui 1:fc2f9d636751 103 //--------------------------------------------------------------------+
yihui 1:fc2f9d636751 104 #define memclr_(buffer, size) memset(buffer, 0, size)
yihui 1:fc2f9d636751 105
yihui 1:fc2f9d636751 106 //------------- Conversion -------------//
yihui 1:fc2f9d636751 107 /// form an uint32_t from 4 x uint8_t
yihui 1:fc2f9d636751 108 static inline uint32_t u32_from_u8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4) ATTR_ALWAYS_INLINE ATTR_CONST;
yihui 1:fc2f9d636751 109 static inline uint32_t u32_from_u8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4)
yihui 1:fc2f9d636751 110 {
yihui 1:fc2f9d636751 111 return (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;
yihui 1:fc2f9d636751 112 }
yihui 1:fc2f9d636751 113
yihui 1:fc2f9d636751 114 static inline uint8_t u16_high_u8(uint16_t u16) ATTR_CONST ATTR_ALWAYS_INLINE;
yihui 1:fc2f9d636751 115 static inline uint8_t u16_high_u8(uint16_t u16)
yihui 1:fc2f9d636751 116 {
yihui 1:fc2f9d636751 117 return (uint8_t) ((u16 >> 8) & 0x00ff);
yihui 1:fc2f9d636751 118 }
yihui 1:fc2f9d636751 119
yihui 1:fc2f9d636751 120 static inline uint8_t u16_low_u8(uint16_t u16) ATTR_CONST ATTR_ALWAYS_INLINE;
yihui 1:fc2f9d636751 121 static inline uint8_t u16_low_u8(uint16_t u16)
yihui 1:fc2f9d636751 122 {
yihui 1:fc2f9d636751 123 return (uint8_t) (u16 & 0x00ff);
yihui 1:fc2f9d636751 124 }
yihui 1:fc2f9d636751 125
yihui 1:fc2f9d636751 126 //------------- Min -------------//
yihui 1:fc2f9d636751 127 static inline uint8_t min8_of(uint8_t x, uint8_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
yihui 1:fc2f9d636751 128 static inline uint8_t min8_of(uint8_t x, uint8_t y)
yihui 1:fc2f9d636751 129 {
yihui 1:fc2f9d636751 130 return (x < y) ? x : y;
yihui 1:fc2f9d636751 131 }
yihui 1:fc2f9d636751 132
yihui 1:fc2f9d636751 133 static inline uint16_t min16_of(uint16_t x, uint16_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
yihui 1:fc2f9d636751 134 static inline uint16_t min16_of(uint16_t x, uint16_t y)
yihui 1:fc2f9d636751 135 {
yihui 1:fc2f9d636751 136 return (x < y) ? x : y;
yihui 1:fc2f9d636751 137 }
yihui 1:fc2f9d636751 138
yihui 1:fc2f9d636751 139 static inline uint32_t min32_of(uint32_t x, uint32_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
yihui 1:fc2f9d636751 140 static inline uint32_t min32_of(uint32_t x, uint32_t y)
yihui 1:fc2f9d636751 141 {
yihui 1:fc2f9d636751 142 return (x < y) ? x : y;
yihui 1:fc2f9d636751 143 }
yihui 1:fc2f9d636751 144
yihui 1:fc2f9d636751 145 //------------- Max -------------//
yihui 1:fc2f9d636751 146 static inline uint32_t max32_of(uint32_t x, uint32_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
yihui 1:fc2f9d636751 147 static inline uint32_t max32_of(uint32_t x, uint32_t y)
yihui 1:fc2f9d636751 148 {
yihui 1:fc2f9d636751 149 return (x > y) ? x : y;
yihui 1:fc2f9d636751 150 }
yihui 1:fc2f9d636751 151
yihui 1:fc2f9d636751 152 //------------- Align -------------//
yihui 1:fc2f9d636751 153 static inline uint32_t align32 (uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
yihui 1:fc2f9d636751 154 static inline uint32_t align32 (uint32_t value)
yihui 1:fc2f9d636751 155 {
yihui 1:fc2f9d636751 156 return (value & 0xFFFFFFE0UL);
yihui 1:fc2f9d636751 157 }
yihui 1:fc2f9d636751 158
yihui 1:fc2f9d636751 159 static inline uint32_t align16 (uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
yihui 1:fc2f9d636751 160 static inline uint32_t align16 (uint32_t value)
yihui 1:fc2f9d636751 161 {
yihui 1:fc2f9d636751 162 return (value & 0xFFFFFFF0UL);
yihui 1:fc2f9d636751 163 }
yihui 1:fc2f9d636751 164
yihui 1:fc2f9d636751 165 static inline uint32_t align_n (uint32_t alignment, uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
yihui 1:fc2f9d636751 166 static inline uint32_t align_n (uint32_t alignment, uint32_t value)
yihui 1:fc2f9d636751 167 {
yihui 1:fc2f9d636751 168 return value & (~(alignment-1));
yihui 1:fc2f9d636751 169 }
yihui 1:fc2f9d636751 170
yihui 1:fc2f9d636751 171 static inline uint32_t align4k (uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
yihui 1:fc2f9d636751 172 static inline uint32_t align4k (uint32_t value)
yihui 1:fc2f9d636751 173 {
yihui 1:fc2f9d636751 174 return (value & 0xFFFFF000UL);
yihui 1:fc2f9d636751 175 }
yihui 1:fc2f9d636751 176
yihui 1:fc2f9d636751 177 static inline uint32_t offset4k(uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
yihui 1:fc2f9d636751 178 static inline uint32_t offset4k(uint32_t value)
yihui 1:fc2f9d636751 179 {
yihui 1:fc2f9d636751 180 return (value & 0xFFFUL);
yihui 1:fc2f9d636751 181 }
yihui 1:fc2f9d636751 182
yihui 1:fc2f9d636751 183 //------------- Mathematics -------------//
yihui 1:fc2f9d636751 184 /// inclusive range checking
yihui 1:fc2f9d636751 185 static inline bool is_in_range(uint32_t lower, uint32_t value, uint32_t upper) ATTR_ALWAYS_INLINE ATTR_CONST;
yihui 1:fc2f9d636751 186 static inline bool is_in_range(uint32_t lower, uint32_t value, uint32_t upper)
yihui 1:fc2f9d636751 187 {
yihui 1:fc2f9d636751 188 return (lower <= value) && (value <= upper);
yihui 1:fc2f9d636751 189 }
yihui 1:fc2f9d636751 190
yihui 1:fc2f9d636751 191 /// exclusive range checking
yihui 1:fc2f9d636751 192 static inline bool is_in_range_exclusive(uint32_t lower, uint32_t value, uint32_t upper) ATTR_ALWAYS_INLINE ATTR_CONST;
yihui 1:fc2f9d636751 193 static inline bool is_in_range_exclusive(uint32_t lower, uint32_t value, uint32_t upper)
yihui 1:fc2f9d636751 194 {
yihui 1:fc2f9d636751 195 return (lower < value) && (value < upper);
yihui 1:fc2f9d636751 196 }
yihui 1:fc2f9d636751 197
yihui 1:fc2f9d636751 198 static inline uint8_t log2_of(uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
yihui 1:fc2f9d636751 199 static inline uint8_t log2_of(uint32_t value)
yihui 1:fc2f9d636751 200 {
yihui 1:fc2f9d636751 201 uint8_t result = 0; // log2 of a value is its MSB's position
yihui 1:fc2f9d636751 202
yihui 1:fc2f9d636751 203 while (value >>= 1)
yihui 1:fc2f9d636751 204 {
yihui 1:fc2f9d636751 205 result++;
yihui 1:fc2f9d636751 206 }
yihui 1:fc2f9d636751 207 return result;
yihui 1:fc2f9d636751 208 }
yihui 1:fc2f9d636751 209
yihui 1:fc2f9d636751 210 // return the number of set bits in value
yihui 1:fc2f9d636751 211 static inline uint8_t cardinality_of(uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
yihui 1:fc2f9d636751 212 static inline uint8_t cardinality_of(uint32_t value)
yihui 1:fc2f9d636751 213 {
yihui 1:fc2f9d636751 214 // Brian Kernighan's method goes through as many iterations as there are set bits. So if we have a 32-bit word with only
yihui 1:fc2f9d636751 215 // the high bit set, then it will only go once through the loop
yihui 1:fc2f9d636751 216 // Published in 1988, the C Programming Language 2nd Ed. (by Brian W. Kernighan and Dennis M. Ritchie)
yihui 1:fc2f9d636751 217 // mentions this in exercise 2-9. On April 19, 2006 Don Knuth pointed out to me that this method
yihui 1:fc2f9d636751 218 // "was first published by Peter Wegner in CACM 3 (1960), 322. (Also discovered independently by Derrick Lehmer and
yihui 1:fc2f9d636751 219 // published in 1964 in a book edited by Beckenbach.)"
yihui 1:fc2f9d636751 220 uint8_t count;
yihui 1:fc2f9d636751 221 for (count = 0; value; count++)
yihui 1:fc2f9d636751 222 {
yihui 1:fc2f9d636751 223 value &= value - 1; // clear the least significant bit set
yihui 1:fc2f9d636751 224 }
yihui 1:fc2f9d636751 225
yihui 1:fc2f9d636751 226 return count;
yihui 1:fc2f9d636751 227 }
yihui 1:fc2f9d636751 228
yihui 1:fc2f9d636751 229 #ifdef __cplusplus
yihui 1:fc2f9d636751 230 }
yihui 1:fc2f9d636751 231 #endif
yihui 1:fc2f9d636751 232
yihui 1:fc2f9d636751 233 #endif /* _COMMON_H_ */
yihui 1:fc2f9d636751 234
yihui 1:fc2f9d636751 235 /** @} */
yihui 1:fc2f9d636751 236 /** @} */