Jonathan Austin / nRF51822

Dependents:   microbit_rubber_ducky microbit_mouse_BLE microbit_mouse_BLE_daybreak_version microbit_presenter

Fork of nRF51822 by Nordic Semiconductor

Committer:
rgrover1
Date:
Mon Jul 06 10:21:01 2015 +0100
Revision:
374:d57070489710
Synchronized with git rev 3d87035e
Author: Rohit Grover
fix #16: hide duplicate symbol definitions in ble_types.h

Who changed what in which revision?

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