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