BLE FOTA APP

Dependencies:   BLE_API mbed

It doesn't work with the default FOTA bootloader. It use NVIC_SystemReset() to enter a bootloader.

Committer:
yihui
Date:
Fri Oct 10 03:36:28 2014 +0000
Revision:
1:a607cd9655d7
use NVIC_SystemReset() to run bootloader

Who changed what in which revision?

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