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