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 /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
yihui 1:a607cd9655d7 2 *
yihui 1:a607cd9655d7 3 * The information contained herein is property of Nordic Semiconductor ASA.
yihui 1:a607cd9655d7 4 * Terms and conditions of usage are described in detail in NORDIC
yihui 1:a607cd9655d7 5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
yihui 1:a607cd9655d7 6 *
yihui 1:a607cd9655d7 7 * Licensees are granted free, non-transferable use of the information. NO
yihui 1:a607cd9655d7 8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
yihui 1:a607cd9655d7 9 * the file.
yihui 1:a607cd9655d7 10 *
yihui 1:a607cd9655d7 11 */
yihui 1:a607cd9655d7 12
yihui 1:a607cd9655d7 13 /** @file
yihui 1:a607cd9655d7 14 *
yihui 1:a607cd9655d7 15 * @defgroup app_util Utility Functions and Definitions
yihui 1:a607cd9655d7 16 * @{
yihui 1:a607cd9655d7 17 * @ingroup app_common
yihui 1:a607cd9655d7 18 *
yihui 1:a607cd9655d7 19 * @brief Various types and definitions available to all applications.
yihui 1:a607cd9655d7 20 */
yihui 1:a607cd9655d7 21
yihui 1:a607cd9655d7 22 #ifndef APP_UTIL_H__
yihui 1:a607cd9655d7 23 #define APP_UTIL_H__
yihui 1:a607cd9655d7 24
yihui 1:a607cd9655d7 25 #include <stdint.h>
yihui 1:a607cd9655d7 26 #include <stdbool.h>
yihui 1:a607cd9655d7 27 #include "compiler_abstraction.h"
yihui 1:a607cd9655d7 28
yihui 1:a607cd9655d7 29 enum
yihui 1:a607cd9655d7 30 {
yihui 1:a607cd9655d7 31 UNIT_0_625_MS = 625, /**< Number of microseconds in 0.625 milliseconds. */
yihui 1:a607cd9655d7 32 UNIT_1_25_MS = 1250, /**< Number of microseconds in 1.25 milliseconds. */
yihui 1:a607cd9655d7 33 UNIT_10_MS = 10000 /**< Number of microseconds in 10 milliseconds. */
yihui 1:a607cd9655d7 34 };
yihui 1:a607cd9655d7 35
yihui 1:a607cd9655d7 36 /**@brief Macro for doing static (i.e. compile time) assertion.
yihui 1:a607cd9655d7 37 *
yihui 1:a607cd9655d7 38 * @note If the assertion fails when compiling using Keil, the compiler will report error message
yihui 1:a607cd9655d7 39 * "error: #94: the size of an array must be greater than zero" (while gcc will list the
yihui 1:a607cd9655d7 40 * symbol static_assert_failed, making the error message more readable).
yihui 1:a607cd9655d7 41 * If the supplied expression can not be evaluated at compile time, Keil will report
yihui 1:a607cd9655d7 42 * "error: #28: expression must have a constant value".
yihui 1:a607cd9655d7 43 *
yihui 1:a607cd9655d7 44 * @note The macro is intentionally implemented not using do while(0), allowing it to be used
yihui 1:a607cd9655d7 45 * outside function blocks (e.g. close to global type- and variable declarations).
yihui 1:a607cd9655d7 46 * If used in a code block, it must be used before any executable code in this block.
yihui 1:a607cd9655d7 47 *
yihui 1:a607cd9655d7 48 * @param[in] EXPR Constant expression to be verified.
yihui 1:a607cd9655d7 49 */
yihui 1:a607cd9655d7 50
yihui 1:a607cd9655d7 51 #if defined(__GNUC__)
yihui 1:a607cd9655d7 52 #define STATIC_ASSERT(EXPR) typedef char __attribute__((unused)) static_assert_failed[(EXPR) ? 1 : -1]
yihui 1:a607cd9655d7 53 #else
yihui 1:a607cd9655d7 54 #define STATIC_ASSERT(EXPR) typedef char static_assert_failed[(EXPR) ? 1 : -1]
yihui 1:a607cd9655d7 55 #endif
yihui 1:a607cd9655d7 56
yihui 1:a607cd9655d7 57
yihui 1:a607cd9655d7 58 /**@brief type for holding an encoded (i.e. little endian) 16 bit unsigned integer. */
yihui 1:a607cd9655d7 59 typedef uint8_t uint16_le_t[2];
yihui 1:a607cd9655d7 60
yihui 1:a607cd9655d7 61 /**@brief type for holding an encoded (i.e. little endian) 32 bit unsigned integer. */
yihui 1:a607cd9655d7 62 typedef uint8_t uint32_le_t[4];
yihui 1:a607cd9655d7 63
yihui 1:a607cd9655d7 64 /**@brief Byte array type. */
yihui 1:a607cd9655d7 65 typedef struct
yihui 1:a607cd9655d7 66 {
yihui 1:a607cd9655d7 67 uint16_t size; /**< Number of array entries. */
yihui 1:a607cd9655d7 68 uint8_t * p_data; /**< Pointer to array entries. */
yihui 1:a607cd9655d7 69 } uint8_array_t;
yihui 1:a607cd9655d7 70
yihui 1:a607cd9655d7 71 /**@brief Perform rounded integer division (as opposed to truncating the result).
yihui 1:a607cd9655d7 72 *
yihui 1:a607cd9655d7 73 * @param[in] A Numerator.
yihui 1:a607cd9655d7 74 * @param[in] B Denominator.
yihui 1:a607cd9655d7 75 *
yihui 1:a607cd9655d7 76 * @return Rounded (integer) result of dividing A by B.
yihui 1:a607cd9655d7 77 */
yihui 1:a607cd9655d7 78 #define ROUNDED_DIV(A, B) (((A) + ((B) / 2)) / (B))
yihui 1:a607cd9655d7 79
yihui 1:a607cd9655d7 80 /**@brief Check if the integer provided is a power of two.
yihui 1:a607cd9655d7 81 *
yihui 1:a607cd9655d7 82 * @param[in] A Number to be tested.
yihui 1:a607cd9655d7 83 *
yihui 1:a607cd9655d7 84 * @return true if value is power of two.
yihui 1:a607cd9655d7 85 * @return false if value not power of two.
yihui 1:a607cd9655d7 86 */
yihui 1:a607cd9655d7 87 #define IS_POWER_OF_TWO(A) ( ((A) != 0) && ((((A) - 1) & (A)) == 0) )
yihui 1:a607cd9655d7 88
yihui 1:a607cd9655d7 89 /**@brief To convert ticks to millisecond
yihui 1:a607cd9655d7 90 * @param[in] time Number of millseconds that needs to be converted.
yihui 1:a607cd9655d7 91 * @param[in] resolution Units to be converted.
yihui 1:a607cd9655d7 92 */
yihui 1:a607cd9655d7 93 #define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION))
yihui 1:a607cd9655d7 94
yihui 1:a607cd9655d7 95
yihui 1:a607cd9655d7 96 /**@brief Perform integer division, making sure the result is rounded up.
yihui 1:a607cd9655d7 97 *
yihui 1:a607cd9655d7 98 * @details One typical use for this is to compute the number of objects with size B is needed to
yihui 1:a607cd9655d7 99 * hold A number of bytes.
yihui 1:a607cd9655d7 100 *
yihui 1:a607cd9655d7 101 * @param[in] A Numerator.
yihui 1:a607cd9655d7 102 * @param[in] B Denominator.
yihui 1:a607cd9655d7 103 *
yihui 1:a607cd9655d7 104 * @return Integer result of dividing A by B, rounded up.
yihui 1:a607cd9655d7 105 */
yihui 1:a607cd9655d7 106 #define CEIL_DIV(A, B) \
yihui 1:a607cd9655d7 107 /*lint -save -e573 */ \
yihui 1:a607cd9655d7 108 ((((A) - 1) / (B)) + 1) \
yihui 1:a607cd9655d7 109 /*lint -restore */
yihui 1:a607cd9655d7 110
yihui 1:a607cd9655d7 111 /**@brief Function for encoding a uint16 value.
yihui 1:a607cd9655d7 112 *
yihui 1:a607cd9655d7 113 * @param[in] value Value to be encoded.
yihui 1:a607cd9655d7 114 * @param[out] p_encoded_data Buffer where the encoded data is to be written.
yihui 1:a607cd9655d7 115 *
yihui 1:a607cd9655d7 116 * @return Number of bytes written.
yihui 1:a607cd9655d7 117 */
yihui 1:a607cd9655d7 118 static __INLINE uint8_t uint16_encode(uint16_t value, uint8_t * p_encoded_data)
yihui 1:a607cd9655d7 119 {
yihui 1:a607cd9655d7 120 p_encoded_data[0] = (uint8_t) ((value & 0x00FF) >> 0);
yihui 1:a607cd9655d7 121 p_encoded_data[1] = (uint8_t) ((value & 0xFF00) >> 8);
yihui 1:a607cd9655d7 122 return sizeof(uint16_t);
yihui 1:a607cd9655d7 123 }
yihui 1:a607cd9655d7 124
yihui 1:a607cd9655d7 125 /**@brief Function for encoding a uint32 value.
yihui 1:a607cd9655d7 126 *
yihui 1:a607cd9655d7 127 * @param[in] value Value to be encoded.
yihui 1:a607cd9655d7 128 * @param[out] p_encoded_data Buffer where the encoded data is to be written.
yihui 1:a607cd9655d7 129 *
yihui 1:a607cd9655d7 130 * @return Number of bytes written.
yihui 1:a607cd9655d7 131 */
yihui 1:a607cd9655d7 132 static __INLINE uint8_t uint32_encode(uint32_t value, uint8_t * p_encoded_data)
yihui 1:a607cd9655d7 133 {
yihui 1:a607cd9655d7 134 p_encoded_data[0] = (uint8_t) ((value & 0x000000FF) >> 0);
yihui 1:a607cd9655d7 135 p_encoded_data[1] = (uint8_t) ((value & 0x0000FF00) >> 8);
yihui 1:a607cd9655d7 136 p_encoded_data[2] = (uint8_t) ((value & 0x00FF0000) >> 16);
yihui 1:a607cd9655d7 137 p_encoded_data[3] = (uint8_t) ((value & 0xFF000000) >> 24);
yihui 1:a607cd9655d7 138 return sizeof(uint32_t);
yihui 1:a607cd9655d7 139 }
yihui 1:a607cd9655d7 140
yihui 1:a607cd9655d7 141 /**@brief Function for decoding a uint16 value.
yihui 1:a607cd9655d7 142 *
yihui 1:a607cd9655d7 143 * @param[in] p_encoded_data Buffer where the encoded data is stored.
yihui 1:a607cd9655d7 144 *
yihui 1:a607cd9655d7 145 * @return Decoded value.
yihui 1:a607cd9655d7 146 */
yihui 1:a607cd9655d7 147 static __INLINE uint16_t uint16_decode(const uint8_t * p_encoded_data)
yihui 1:a607cd9655d7 148 {
yihui 1:a607cd9655d7 149 return ( (((uint16_t)((uint8_t *)p_encoded_data)[0])) |
yihui 1:a607cd9655d7 150 (((uint16_t)((uint8_t *)p_encoded_data)[1]) << 8 ));
yihui 1:a607cd9655d7 151 }
yihui 1:a607cd9655d7 152
yihui 1:a607cd9655d7 153 /**@brief Function for decoding a uint32 value.
yihui 1:a607cd9655d7 154 *
yihui 1:a607cd9655d7 155 * @param[in] p_encoded_data Buffer where the encoded data is stored.
yihui 1:a607cd9655d7 156 *
yihui 1:a607cd9655d7 157 * @return Decoded value.
yihui 1:a607cd9655d7 158 */
yihui 1:a607cd9655d7 159 static __INLINE uint32_t uint32_decode(const uint8_t * p_encoded_data)
yihui 1:a607cd9655d7 160 {
yihui 1:a607cd9655d7 161 return ( (((uint32_t)((uint8_t *)p_encoded_data)[0]) << 0) |
yihui 1:a607cd9655d7 162 (((uint32_t)((uint8_t *)p_encoded_data)[1]) << 8) |
yihui 1:a607cd9655d7 163 (((uint32_t)((uint8_t *)p_encoded_data)[2]) << 16) |
yihui 1:a607cd9655d7 164 (((uint32_t)((uint8_t *)p_encoded_data)[3]) << 24 ));
yihui 1:a607cd9655d7 165 }
yihui 1:a607cd9655d7 166
yihui 1:a607cd9655d7 167 /** @brief Function for converting the input voltage (in milli volts) into percentage of 3.0 Volts.
yihui 1:a607cd9655d7 168 *
yihui 1:a607cd9655d7 169 * @details The calculation is based on a linearized version of the battery's discharge
yihui 1:a607cd9655d7 170 * curve. 3.0V returns 100% battery level. The limit for power failure is 2.1V and
yihui 1:a607cd9655d7 171 * is considered to be the lower boundary.
yihui 1:a607cd9655d7 172 *
yihui 1:a607cd9655d7 173 * The discharge curve for CR2032 is non-linear. In this model it is split into
yihui 1:a607cd9655d7 174 * 4 linear sections:
yihui 1:a607cd9655d7 175 * - Section 1: 3.0V - 2.9V = 100% - 42% (58% drop on 100 mV)
yihui 1:a607cd9655d7 176 * - Section 2: 2.9V - 2.74V = 42% - 18% (24% drop on 160 mV)
yihui 1:a607cd9655d7 177 * - Section 3: 2.74V - 2.44V = 18% - 6% (12% drop on 300 mV)
yihui 1:a607cd9655d7 178 * - Section 4: 2.44V - 2.1V = 6% - 0% (6% drop on 340 mV)
yihui 1:a607cd9655d7 179 *
yihui 1:a607cd9655d7 180 * These numbers are by no means accurate. Temperature and
yihui 1:a607cd9655d7 181 * load in the actual application is not accounted for!
yihui 1:a607cd9655d7 182 *
yihui 1:a607cd9655d7 183 * @param[in] mvolts The voltage in mV
yihui 1:a607cd9655d7 184 *
yihui 1:a607cd9655d7 185 * @return Battery level in percent.
yihui 1:a607cd9655d7 186 */
yihui 1:a607cd9655d7 187 static __INLINE uint8_t battery_level_in_percent(const uint16_t mvolts)
yihui 1:a607cd9655d7 188 {
yihui 1:a607cd9655d7 189 uint8_t battery_level;
yihui 1:a607cd9655d7 190
yihui 1:a607cd9655d7 191 if (mvolts >= 3000)
yihui 1:a607cd9655d7 192 {
yihui 1:a607cd9655d7 193 battery_level = 100;
yihui 1:a607cd9655d7 194 }
yihui 1:a607cd9655d7 195 else if (mvolts > 2900)
yihui 1:a607cd9655d7 196 {
yihui 1:a607cd9655d7 197 battery_level = 100 - ((3000 - mvolts) * 58) / 100;
yihui 1:a607cd9655d7 198 }
yihui 1:a607cd9655d7 199 else if (mvolts > 2740)
yihui 1:a607cd9655d7 200 {
yihui 1:a607cd9655d7 201 battery_level = 42 - ((2900 - mvolts) * 24) / 160;
yihui 1:a607cd9655d7 202 }
yihui 1:a607cd9655d7 203 else if (mvolts > 2440)
yihui 1:a607cd9655d7 204 {
yihui 1:a607cd9655d7 205 battery_level = 18 - ((2740 - mvolts) * 12) / 300;
yihui 1:a607cd9655d7 206 }
yihui 1:a607cd9655d7 207 else if (mvolts > 2100)
yihui 1:a607cd9655d7 208 {
yihui 1:a607cd9655d7 209 battery_level = 6 - ((2440 - mvolts) * 6) / 340;
yihui 1:a607cd9655d7 210 }
yihui 1:a607cd9655d7 211 else
yihui 1:a607cd9655d7 212 {
yihui 1:a607cd9655d7 213 battery_level = 0;
yihui 1:a607cd9655d7 214 }
yihui 1:a607cd9655d7 215
yihui 1:a607cd9655d7 216 return battery_level;
yihui 1:a607cd9655d7 217 }
yihui 1:a607cd9655d7 218
yihui 1:a607cd9655d7 219 /**@brief Function for checking if a pointer value is aligned to a 4 byte boundary.
yihui 1:a607cd9655d7 220 *
yihui 1:a607cd9655d7 221 * @param[in] p Pointer value to be checked.
yihui 1:a607cd9655d7 222 *
yihui 1:a607cd9655d7 223 * @return TRUE if pointer is aligned to a 4 byte boundary, FALSE otherwise.
yihui 1:a607cd9655d7 224 */
yihui 1:a607cd9655d7 225 static __INLINE bool is_word_aligned(void * p)
yihui 1:a607cd9655d7 226 {
yihui 1:a607cd9655d7 227 return (((uintptr_t)p & 0x03) == 0);
yihui 1:a607cd9655d7 228 }
yihui 1:a607cd9655d7 229
yihui 1:a607cd9655d7 230 #endif // APP_UTIL_H__
yihui 1:a607cd9655d7 231
yihui 1:a607cd9655d7 232 /** @} */