Changed the device name.

Dependents:   BLE_Health_Thermometer_HeartRateMonitor

Fork of BLE_API_Native_IRC by Yoshihiro TSUBOI

Committer:
ghz2000
Date:
Mon Jun 16 15:06:48 2014 +0000
Revision:
20:a58a51c92075
Parent:
17:d5600677d532
Add HeartRateMonitor

Who changed what in which revision?

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