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