Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

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