library for BLE_GAP_backpack

Dependencies:   nrf51-sdk

Fork of nRF51822 by Nordic Semiconductor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers common.h Source File

common.h

Go to the documentation of this file.
00001 /**************************************************************************/
00002 /*!
00003     @file     common.h
00004     @author   hathach (tinyusb.org)
00005 
00006     @section LICENSE
00007 
00008     Software License Agreement (BSD License)
00009 
00010     Copyright (c) 2013, K. Townsend (microBuilder.eu)
00011     All rights reserved.
00012 
00013     Redistribution and use in source and binary forms, with or without
00014     modification, are permitted provided that the following conditions are met:
00015     1. Redistributions of source code must retain the above copyright
00016     notice, this list of conditions and the following disclaimer.
00017     2. Redistributions in binary form must reproduce the above copyright
00018     notice, this list of conditions and the following disclaimer in the
00019     documentation and/or other materials provided with the distribution.
00020     3. Neither the name of the copyright holders nor the
00021     names of its contributors may be used to endorse or promote products
00022     derived from this software without specific prior written permission.
00023 
00024     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
00025     EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00026     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
00028     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029     INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND
00031     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032     INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
00033     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 */
00035 /**************************************************************************/
00036 
00037 /** \defgroup Group_Common Common Files
00038  * @{
00039  *
00040  *  \defgroup Group_CommonH common.h
00041  *
00042  *  @{
00043  */
00044 
00045 #ifndef _COMMON_H_
00046 #define _COMMON_H_
00047 
00048 #ifdef __cplusplus
00049  extern "C" {
00050 #endif
00051 
00052 //--------------------------------------------------------------------+
00053 // INCLUDES
00054 //--------------------------------------------------------------------+
00055 
00056 //------------- Standard Header -------------//
00057 #include <stdint.h>
00058 #include <stdbool.h>
00059 #include <stddef.h>
00060 #include <string.h>
00061 #include <stdio.h>
00062 
00063 //------------- General Header -------------//
00064 #include "projectconfig.h"
00065 #include "compiler.h"
00066 #include "assertion.h"
00067 #include "binary.h "
00068 #include "ble_error.h"
00069 
00070 //------------- MCU header -------------//
00071 //#include "nrf.h"
00072 
00073 //--------------------------------------------------------------------+
00074 // TYPEDEFS
00075 //--------------------------------------------------------------------+
00076 typedef unsigned char byte_t;
00077 typedef float  float32_t;
00078 typedef double float64_t;
00079 
00080 //--------------------------------------------------------------------+
00081 // MACROS
00082 //--------------------------------------------------------------------+
00083 #define STRING_(x)  #x                             // stringify without expand
00084 #define XSTRING_(x) STRING_(x)                     // expand then stringify
00085 #define STRING_CONCAT_(a, b) a##b                  // concat without expand
00086 #define XSTRING_CONCAT_(a, b) STRING_CONCAT_(a, b) // expand then concat
00087 
00088 #define U16_HIGH_U8(u16) ((uint8_t) (((u16) >> 8) & 0x00ff))
00089 #define U16_LOW_U8(u16)  ((uint8_t) ((u16)       & 0x00ff))
00090 #define U16_TO_U8S_BE(u16)  U16_HIGH_U8(u16), U16_LOW_U8(u16)
00091 #define U16_TO_U8S_LE(u16)  U16_LOW_U8(u16), U16_HIGH_U8(u16)
00092 
00093 #define U32_B1_U8(u32) ((uint8_t) (((u32) >> 24) & 0x000000ff)) // MSB
00094 #define U32_B2_U8(u32) ((uint8_t) (((u32) >> 16) & 0x000000ff))
00095 #define U32_B3_U8(u32) ((uint8_t) (((u32) >>  8) & 0x000000ff))
00096 #define U32_B4_U8(u32) ((uint8_t) ((u32)        & 0x000000ff)) // LSB
00097 
00098 #define U32_TO_U8S_BE(u32) U32_B1_U8(u32), U32_B2_U8(u32), U32_B3_U8(u32), U32_B4_U8(u32)
00099 #define U32_TO_U8S_LE(u32) U32_B4_U8(u32), U32_B3_U8(u32), U32_B2_U8(u32), U32_B1_U8(u32)
00100 
00101 //--------------------------------------------------------------------+
00102 // INLINE FUNCTION
00103 //--------------------------------------------------------------------+
00104 #define memclr_(buffer, size)  memset(buffer, 0, size)
00105 
00106 //------------- Conversion -------------//
00107 /// form an uint32_t from 4 x uint8_t
00108 static inline uint32_t u32_from_u8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4) ATTR_ALWAYS_INLINE ATTR_CONST;
00109 static inline uint32_t u32_from_u8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4)
00110 {
00111   return (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;
00112 }
00113 
00114 static inline uint8_t u16_high_u8(uint16_t u16) ATTR_CONST ATTR_ALWAYS_INLINE;
00115 static inline uint8_t u16_high_u8(uint16_t u16)
00116 {
00117   return (uint8_t) ((u16 >> 8) & 0x00ff);
00118 }
00119 
00120 static inline uint8_t u16_low_u8(uint16_t u16) ATTR_CONST ATTR_ALWAYS_INLINE;
00121 static inline uint8_t u16_low_u8(uint16_t u16)
00122 {
00123   return (uint8_t) (u16 & 0x00ff);
00124 }
00125 
00126 //------------- Min -------------//
00127 static inline uint8_t min8_of(uint8_t x, uint8_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
00128 static inline uint8_t min8_of(uint8_t x, uint8_t y)
00129 {
00130   return (x < y) ? x : y;
00131 }
00132 
00133 static inline uint16_t min16_of(uint16_t x, uint16_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
00134 static inline uint16_t min16_of(uint16_t x, uint16_t y)
00135 {
00136   return (x < y) ? x : y;
00137 }
00138 
00139 static inline uint32_t min32_of(uint32_t x, uint32_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
00140 static inline uint32_t min32_of(uint32_t x, uint32_t y)
00141 {
00142   return (x < y) ? x : y;
00143 }
00144 
00145 //------------- Max -------------//
00146 static inline uint32_t max32_of(uint32_t x, uint32_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
00147 static inline uint32_t max32_of(uint32_t x, uint32_t y)
00148 {
00149   return (x > y) ? x : y;
00150 }
00151 
00152 //------------- Align -------------//
00153 static inline uint32_t align32 (uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
00154 static inline uint32_t align32 (uint32_t value)
00155 {
00156     return (value & 0xFFFFFFE0UL);
00157 }
00158 
00159 static inline uint32_t align16 (uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
00160 static inline uint32_t align16 (uint32_t value)
00161 {
00162     return (value & 0xFFFFFFF0UL);
00163 }
00164 
00165 static inline uint32_t align_n (uint32_t alignment, uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
00166 static inline uint32_t align_n (uint32_t alignment, uint32_t value)
00167 {
00168     return value & (~(alignment-1));
00169 }
00170 
00171 static inline uint32_t align4k (uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
00172 static inline uint32_t align4k (uint32_t value)
00173 {
00174     return (value & 0xFFFFF000UL);
00175 }
00176 
00177 static inline uint32_t offset4k(uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
00178 static inline uint32_t offset4k(uint32_t value)
00179 {
00180     return (value & 0xFFFUL);
00181 }
00182 
00183 //------------- Mathematics -------------//
00184 /// inclusive range checking
00185 static inline bool is_in_range(uint32_t lower, uint32_t value, uint32_t upper) ATTR_ALWAYS_INLINE ATTR_CONST;
00186 static inline bool is_in_range(uint32_t lower, uint32_t value, uint32_t upper)
00187 {
00188   return (lower <= value) && (value <= upper);
00189 }
00190 
00191 /// exclusive range checking
00192 static inline bool is_in_range_exclusive(uint32_t lower, uint32_t value, uint32_t upper) ATTR_ALWAYS_INLINE ATTR_CONST;
00193 static inline bool is_in_range_exclusive(uint32_t lower, uint32_t value, uint32_t upper)
00194 {
00195   return (lower < value) && (value < upper);
00196 }
00197 
00198 static inline uint8_t log2_of(uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
00199 static inline uint8_t log2_of(uint32_t value)
00200 {
00201   uint8_t result = 0; // log2 of a value is its MSB's position
00202 
00203   while (value >>= 1)
00204   {
00205     result++;
00206   }
00207   return result;
00208 }
00209 
00210 // return the number of set bits in value
00211 static inline uint8_t cardinality_of(uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
00212 static inline uint8_t cardinality_of(uint32_t value)
00213 {
00214   // Brian Kernighan's method goes through as many iterations as there are set bits. So if we have a 32-bit word with only
00215   // the high bit set, then it will only go once through the loop
00216   // Published in 1988, the C Programming Language 2nd Ed. (by Brian W. Kernighan and Dennis M. Ritchie)
00217   // mentions this in exercise 2-9. On April 19, 2006 Don Knuth pointed out to me that this method
00218   // "was first published by Peter Wegner in CACM 3 (1960), 322. (Also discovered independently by Derrick Lehmer and
00219   // published in 1964 in a book edited by Beckenbach.)"
00220   uint8_t count;
00221   for (count = 0; value; count++)
00222   {
00223     value &= value - 1; // clear the least significant bit set
00224   }
00225 
00226   return count;
00227 }
00228 
00229 #ifdef __cplusplus
00230  }
00231 #endif
00232 
00233 #endif /* _COMMON_H_ */
00234 
00235 /**  @} */
00236 /**  @} */