テスト用です。

Dependencies:   mbed

Committer:
jksoft
Date:
Tue Oct 11 11:09:42 2016 +0000
Revision:
0:8468a4403fea
SB??ver;

Who changed what in which revision?

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