konashi/SBBLEのテスト

Dependencies:   BLE_API mbed

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Committer:
robo8080
Date:
Sun Aug 17 00:53:23 2014 +0000
Revision:
8:a62b8f7d5dcf
Parent:
5:61109bce11fe
DeviceName??

Who changed what in which revision?

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