Nordic stack and drivers for the mbed BLE API

Fork of nRF51822 by Nordic Semiconductor

Committer:
Vincent Coubard
Date:
Wed Sep 14 14:39:43 2016 +0100
Revision:
638:c90ae1400bf2
Sync with bdab10dc0f90748b6989c8b577771bb403ca6bd8 from ARMmbed/mbed-os.

Who changed what in which revision?

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