library for BLE_GAP_backpack

Dependencies:   nrf51-sdk

Fork of nRF51822 by Nordic Semiconductor

Committer:
rgrover1
Date:
Mon Jul 06 10:21:00 2015 +0100
Revision:
372:758e9a3a346a
Synchronized with git rev 488e2462
Author: James Crosby
restructure to yotta module, with a few tweaks to get things building

Who changed what in which revision?

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