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