Abraham Marsen / Mbed 2 deprecated Jazz_Hands_Nordic

Dependencies:   mbed

Committer:
Grimmkey
Date:
Thu Apr 30 20:46:27 2015 +0000
Revision:
0:b8221deeaa87
Georgia Institute of Technology ECE 4180 Spring 2015 Jazz Hands project, Nordic nRF51822 half

Who changed what in which revision?

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