Changed the device name.

Dependents:   BLE_Health_Thermometer_HeartRateMonitor

Fork of BLE_API_Native_IRC by Yoshihiro TSUBOI

Committer:
ktownsend
Date:
Mon Feb 10 13:43:38 2014 +0000
Revision:
5:2dca0528db6a
Parent:
0:4c3097c65247
Child:
10:eb5f77205fb5
Renamed error.h to ble_error.h

Who changed what in which revision?

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