Nordic nrf51 sdk sources. Mirrored from https://github.com/ARMmbed/nrf51-sdk.

Dependents:   nRF51822 nRF51822

Committer:
vcoubard
Date:
Thu Apr 07 17:37:40 2016 +0100
Revision:
19:47192cb9def7
Parent:
10:233fefd8162b
Child:
20:a90c48eb1d30
Synchronized with git rev 9251259f
Author: Liyou Zhou
Copy over coresponding files from nordic-sdk 9.0.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vcoubard 19:47192cb9def7 1 /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
vcoubard 19:47192cb9def7 2 *
vcoubard 19:47192cb9def7 3 * The information contained herein is property of Nordic Semiconductor ASA.
vcoubard 19:47192cb9def7 4 * Terms and conditions of usage are described in detail in NORDIC
vcoubard 19:47192cb9def7 5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
vcoubard 19:47192cb9def7 6 *
vcoubard 19:47192cb9def7 7 * Licensees are granted free, non-transferable use of the information. NO
vcoubard 19:47192cb9def7 8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
vcoubard 19:47192cb9def7 9 * the file.
vcoubard 19:47192cb9def7 10 *
Vincent Coubard 0:f2542974c862 11 */
Vincent Coubard 0:f2542974c862 12
Vincent Coubard 0:f2542974c862 13 /** @file
Vincent Coubard 0:f2542974c862 14 *
Vincent Coubard 0:f2542974c862 15 * @defgroup app_util Utility Functions and Definitions
Vincent Coubard 0:f2542974c862 16 * @{
Vincent Coubard 0:f2542974c862 17 * @ingroup app_common
Vincent Coubard 0:f2542974c862 18 *
Vincent Coubard 0:f2542974c862 19 * @brief Various types and definitions available to all applications.
Vincent Coubard 0:f2542974c862 20 */
Vincent Coubard 0:f2542974c862 21
Vincent Coubard 0:f2542974c862 22 #ifndef APP_UTIL_H__
Vincent Coubard 0:f2542974c862 23 #define APP_UTIL_H__
Vincent Coubard 0:f2542974c862 24
Vincent Coubard 0:f2542974c862 25 #include <stdint.h>
Vincent Coubard 0:f2542974c862 26 #include <stdbool.h>
Vincent Coubard 0:f2542974c862 27 #include "compiler_abstraction.h"
Vincent Coubard 0:f2542974c862 28
Vincent Coubard 0:f2542974c862 29 enum
Vincent Coubard 0:f2542974c862 30 {
Vincent Coubard 0:f2542974c862 31 UNIT_0_625_MS = 625, /**< Number of microseconds in 0.625 milliseconds. */
Vincent Coubard 0:f2542974c862 32 UNIT_1_25_MS = 1250, /**< Number of microseconds in 1.25 milliseconds. */
Vincent Coubard 0:f2542974c862 33 UNIT_10_MS = 10000 /**< Number of microseconds in 10 milliseconds. */
Vincent Coubard 0:f2542974c862 34 };
Vincent Coubard 0:f2542974c862 35
vcoubard 19:47192cb9def7 36
vcoubard 19:47192cb9def7 37 /**@brief Implementation specific macro for delayed macro expansion used in string concatenation
vcoubard 19:47192cb9def7 38 *
vcoubard 19:47192cb9def7 39 * @param[in] lhs Left hand side in concatenation
vcoubard 19:47192cb9def7 40 * @param[in] rhs Right hand side in concatenation
vcoubard 19:47192cb9def7 41 */
vcoubard 19:47192cb9def7 42 #define STRING_CONCATENATE_IMPL(lhs, rhs) lhs ## rhs
vcoubard 19:47192cb9def7 43
vcoubard 19:47192cb9def7 44
vcoubard 19:47192cb9def7 45 /**@brief Macro used to concatenate string using delayed macro expansion
vcoubard 19:47192cb9def7 46 *
vcoubard 19:47192cb9def7 47 * @note This macro will delay concatenation until the expressions have been resolved
vcoubard 19:47192cb9def7 48 *
vcoubard 19:47192cb9def7 49 * @param[in] lhs Left hand side in concatenation
vcoubard 19:47192cb9def7 50 * @param[in] rhs Right hand side in concatenation
vcoubard 19:47192cb9def7 51 */
vcoubard 19:47192cb9def7 52 #define STRING_CONCATENATE(lhs, rhs) STRING_CONCATENATE_IMPL(lhs, rhs)
vcoubard 19:47192cb9def7 53
vcoubard 19:47192cb9def7 54
vcoubard 19:47192cb9def7 55 // Disable lint-warnings/errors for STATIC_ASSERT
vcoubard 19:47192cb9def7 56 //lint --emacro(10,STATIC_ASSERT)
vcoubard 19:47192cb9def7 57 //lint --emacro(18,STATIC_ASSERT)
vcoubard 19:47192cb9def7 58 //lint --emacro(19,STATIC_ASSERT)
vcoubard 19:47192cb9def7 59 //lint --emacro(30,STATIC_ASSERT)
vcoubard 19:47192cb9def7 60 //lint --emacro(37,STATIC_ASSERT)
vcoubard 19:47192cb9def7 61 //lint --emacro(42,STATIC_ASSERT)
vcoubard 19:47192cb9def7 62 //lint --emacro(26,STATIC_ASSERT)
vcoubard 19:47192cb9def7 63 //lint --emacro(102,STATIC_ASSERT)
vcoubard 19:47192cb9def7 64 //lint --emacro(533,STATIC_ASSERT)
vcoubard 19:47192cb9def7 65 //lint --emacro(534,STATIC_ASSERT)
vcoubard 19:47192cb9def7 66 //lint --emacro(132,STATIC_ASSERT)
vcoubard 19:47192cb9def7 67 //lint --emacro(414,STATIC_ASSERT)
vcoubard 19:47192cb9def7 68 //lint --emacro(578,STATIC_ASSERT)
vcoubard 19:47192cb9def7 69 //lint --emacro(628,STATIC_ASSERT)
vcoubard 19:47192cb9def7 70 //lint --emacro(648,STATIC_ASSERT)
vcoubard 19:47192cb9def7 71 //lint --emacro(830,STATIC_ASSERT)
vcoubard 19:47192cb9def7 72
vcoubard 19:47192cb9def7 73
Vincent Coubard 0:f2542974c862 74 /**@brief Macro for doing static (i.e. compile time) assertion.
vcoubard 19:47192cb9def7 75 *
vcoubard 19:47192cb9def7 76 * @note If the EXPR isn't resolvable, then the error message won't be shown.
vcoubard 19:47192cb9def7 77 *
vcoubard 19:47192cb9def7 78 * @note The output of STATIC_ASSERT_MSG will be different across different compilers.
vcoubard 19:47192cb9def7 79 *
vcoubard 19:47192cb9def7 80 * @param[in] EXPR Constant expression to be verified.
vcoubard 19:47192cb9def7 81 */
vcoubard 19:47192cb9def7 82 #if defined ( __COUNTER__ )
Vincent Coubard 0:f2542974c862 83
vcoubard 19:47192cb9def7 84 #define STATIC_ASSERT(EXPR) \
vcoubard 19:47192cb9def7 85 ;enum { STRING_CONCATENATE(static_assert_, __COUNTER__) = 1/(!!(EXPR)) }
vcoubard 19:47192cb9def7 86
Vincent Coubard 0:f2542974c862 87 #else
vcoubard 19:47192cb9def7 88
vcoubard 19:47192cb9def7 89 #define STATIC_ASSERT(EXPR) \
vcoubard 19:47192cb9def7 90 ;enum { STRING_CONCATENATE(assert_line_, __LINE__) = 1/(!!(EXPR)) }
vcoubard 19:47192cb9def7 91
Vincent Coubard 0:f2542974c862 92 #endif
Vincent Coubard 0:f2542974c862 93
Vincent Coubard 0:f2542974c862 94
vcoubard 19:47192cb9def7 95
Vincent Coubard 0:f2542974c862 96 /**@brief type for holding an encoded (i.e. little endian) 16 bit unsigned integer. */
Vincent Coubard 0:f2542974c862 97 typedef uint8_t uint16_le_t[2];
Vincent Coubard 0:f2542974c862 98
Vincent Coubard 0:f2542974c862 99 /**@brief type for holding an encoded (i.e. little endian) 32 bit unsigned integer. */
Vincent Coubard 0:f2542974c862 100 typedef uint8_t uint32_le_t[4];
Vincent Coubard 0:f2542974c862 101
Vincent Coubard 0:f2542974c862 102 /**@brief Byte array type. */
Vincent Coubard 0:f2542974c862 103 typedef struct
Vincent Coubard 0:f2542974c862 104 {
Vincent Coubard 0:f2542974c862 105 uint16_t size; /**< Number of array entries. */
Vincent Coubard 0:f2542974c862 106 uint8_t * p_data; /**< Pointer to array entries. */
Vincent Coubard 0:f2542974c862 107 } uint8_array_t;
vcoubard 1:ebc0e0ef0a11 108
Vincent Coubard 0:f2542974c862 109 /**@brief Perform rounded integer division (as opposed to truncating the result).
Vincent Coubard 0:f2542974c862 110 *
Vincent Coubard 0:f2542974c862 111 * @param[in] A Numerator.
Vincent Coubard 0:f2542974c862 112 * @param[in] B Denominator.
Vincent Coubard 0:f2542974c862 113 *
Vincent Coubard 0:f2542974c862 114 * @return Rounded (integer) result of dividing A by B.
Vincent Coubard 0:f2542974c862 115 */
Vincent Coubard 0:f2542974c862 116 #define ROUNDED_DIV(A, B) (((A) + ((B) / 2)) / (B))
Vincent Coubard 0:f2542974c862 117
Vincent Coubard 0:f2542974c862 118 /**@brief Check if the integer provided is a power of two.
Vincent Coubard 0:f2542974c862 119 *
Vincent Coubard 0:f2542974c862 120 * @param[in] A Number to be tested.
Vincent Coubard 0:f2542974c862 121 *
Vincent Coubard 0:f2542974c862 122 * @return true if value is power of two.
Vincent Coubard 0:f2542974c862 123 * @return false if value not power of two.
Vincent Coubard 0:f2542974c862 124 */
Vincent Coubard 0:f2542974c862 125 #define IS_POWER_OF_TWO(A) ( ((A) != 0) && ((((A) - 1) & (A)) == 0) )
Vincent Coubard 0:f2542974c862 126
Vincent Coubard 0:f2542974c862 127 /**@brief To convert milliseconds to ticks.
Vincent Coubard 0:f2542974c862 128 * @param[in] TIME Number of milliseconds to convert.
Vincent Coubard 0:f2542974c862 129 * @param[in] RESOLUTION Unit to be converted to in [us/ticks].
Vincent Coubard 0:f2542974c862 130 */
Vincent Coubard 0:f2542974c862 131 #define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION))
Vincent Coubard 0:f2542974c862 132
Vincent Coubard 0:f2542974c862 133
Vincent Coubard 0:f2542974c862 134 /**@brief Perform integer division, making sure the result is rounded up.
Vincent Coubard 0:f2542974c862 135 *
Vincent Coubard 0:f2542974c862 136 * @details One typical use for this is to compute the number of objects with size B is needed to
Vincent Coubard 0:f2542974c862 137 * hold A number of bytes.
Vincent Coubard 0:f2542974c862 138 *
Vincent Coubard 0:f2542974c862 139 * @param[in] A Numerator.
Vincent Coubard 0:f2542974c862 140 * @param[in] B Denominator.
Vincent Coubard 0:f2542974c862 141 *
Vincent Coubard 0:f2542974c862 142 * @return Integer result of dividing A by B, rounded up.
Vincent Coubard 0:f2542974c862 143 */
Vincent Coubard 0:f2542974c862 144 #define CEIL_DIV(A, B) \
Vincent Coubard 0:f2542974c862 145 /*lint -save -e573 */ \
Vincent Coubard 0:f2542974c862 146 ((((A) - 1) / (B)) + 1) \
Vincent Coubard 0:f2542974c862 147 /*lint -restore */
Vincent Coubard 0:f2542974c862 148
Vincent Coubard 0:f2542974c862 149 /**@brief Function for encoding a uint16 value.
Vincent Coubard 0:f2542974c862 150 *
Vincent Coubard 0:f2542974c862 151 * @param[in] value Value to be encoded.
Vincent Coubard 0:f2542974c862 152 * @param[out] p_encoded_data Buffer where the encoded data is to be written.
Vincent Coubard 0:f2542974c862 153 *
Vincent Coubard 0:f2542974c862 154 * @return Number of bytes written.
Vincent Coubard 0:f2542974c862 155 */
Vincent Coubard 0:f2542974c862 156 static __INLINE uint8_t uint16_encode(uint16_t value, uint8_t * p_encoded_data)
Vincent Coubard 0:f2542974c862 157 {
Vincent Coubard 0:f2542974c862 158 p_encoded_data[0] = (uint8_t) ((value & 0x00FF) >> 0);
Vincent Coubard 0:f2542974c862 159 p_encoded_data[1] = (uint8_t) ((value & 0xFF00) >> 8);
Vincent Coubard 0:f2542974c862 160 return sizeof(uint16_t);
Vincent Coubard 0:f2542974c862 161 }
vcoubard 1:ebc0e0ef0a11 162
Vincent Coubard 0:f2542974c862 163 /**@brief Function for encoding a uint32 value.
Vincent Coubard 0:f2542974c862 164 *
Vincent Coubard 0:f2542974c862 165 * @param[in] value Value to be encoded.
Vincent Coubard 0:f2542974c862 166 * @param[out] p_encoded_data Buffer where the encoded data is to be written.
Vincent Coubard 0:f2542974c862 167 *
Vincent Coubard 0:f2542974c862 168 * @return Number of bytes written.
Vincent Coubard 0:f2542974c862 169 */
Vincent Coubard 0:f2542974c862 170 static __INLINE uint8_t uint32_encode(uint32_t value, uint8_t * p_encoded_data)
Vincent Coubard 0:f2542974c862 171 {
Vincent Coubard 0:f2542974c862 172 p_encoded_data[0] = (uint8_t) ((value & 0x000000FF) >> 0);
Vincent Coubard 0:f2542974c862 173 p_encoded_data[1] = (uint8_t) ((value & 0x0000FF00) >> 8);
Vincent Coubard 0:f2542974c862 174 p_encoded_data[2] = (uint8_t) ((value & 0x00FF0000) >> 16);
Vincent Coubard 0:f2542974c862 175 p_encoded_data[3] = (uint8_t) ((value & 0xFF000000) >> 24);
Vincent Coubard 0:f2542974c862 176 return sizeof(uint32_t);
Vincent Coubard 0:f2542974c862 177 }
Vincent Coubard 0:f2542974c862 178
Vincent Coubard 0:f2542974c862 179 /**@brief Function for decoding a uint16 value.
Vincent Coubard 0:f2542974c862 180 *
Vincent Coubard 0:f2542974c862 181 * @param[in] p_encoded_data Buffer where the encoded data is stored.
Vincent Coubard 0:f2542974c862 182 *
Vincent Coubard 0:f2542974c862 183 * @return Decoded value.
Vincent Coubard 0:f2542974c862 184 */
Vincent Coubard 0:f2542974c862 185 static __INLINE uint16_t uint16_decode(const uint8_t * p_encoded_data)
Vincent Coubard 0:f2542974c862 186 {
vcoubard 1:ebc0e0ef0a11 187 return ( (((uint16_t)((uint8_t *)p_encoded_data)[0])) |
Vincent Coubard 0:f2542974c862 188 (((uint16_t)((uint8_t *)p_encoded_data)[1]) << 8 ));
Vincent Coubard 0:f2542974c862 189 }
Vincent Coubard 0:f2542974c862 190
Vincent Coubard 0:f2542974c862 191 /**@brief Function for decoding a uint32 value.
Vincent Coubard 0:f2542974c862 192 *
Vincent Coubard 0:f2542974c862 193 * @param[in] p_encoded_data Buffer where the encoded data is stored.
Vincent Coubard 0:f2542974c862 194 *
Vincent Coubard 0:f2542974c862 195 * @return Decoded value.
Vincent Coubard 0:f2542974c862 196 */
Vincent Coubard 0:f2542974c862 197 static __INLINE uint32_t uint32_decode(const uint8_t * p_encoded_data)
Vincent Coubard 0:f2542974c862 198 {
Vincent Coubard 0:f2542974c862 199 return ( (((uint32_t)((uint8_t *)p_encoded_data)[0]) << 0) |
Vincent Coubard 0:f2542974c862 200 (((uint32_t)((uint8_t *)p_encoded_data)[1]) << 8) |
Vincent Coubard 0:f2542974c862 201 (((uint32_t)((uint8_t *)p_encoded_data)[2]) << 16) |
Vincent Coubard 0:f2542974c862 202 (((uint32_t)((uint8_t *)p_encoded_data)[3]) << 24 ));
Vincent Coubard 0:f2542974c862 203 }
vcoubard 1:ebc0e0ef0a11 204
Vincent Coubard 0:f2542974c862 205 /** @brief Function for converting the input voltage (in milli volts) into percentage of 3.0 Volts.
Vincent Coubard 0:f2542974c862 206 *
Vincent Coubard 0:f2542974c862 207 * @details The calculation is based on a linearized version of the battery's discharge
Vincent Coubard 0:f2542974c862 208 * curve. 3.0V returns 100% battery level. The limit for power failure is 2.1V and
Vincent Coubard 0:f2542974c862 209 * is considered to be the lower boundary.
Vincent Coubard 0:f2542974c862 210 *
Vincent Coubard 0:f2542974c862 211 * The discharge curve for CR2032 is non-linear. In this model it is split into
Vincent Coubard 0:f2542974c862 212 * 4 linear sections:
Vincent Coubard 0:f2542974c862 213 * - Section 1: 3.0V - 2.9V = 100% - 42% (58% drop on 100 mV)
Vincent Coubard 0:f2542974c862 214 * - Section 2: 2.9V - 2.74V = 42% - 18% (24% drop on 160 mV)
Vincent Coubard 0:f2542974c862 215 * - Section 3: 2.74V - 2.44V = 18% - 6% (12% drop on 300 mV)
Vincent Coubard 0:f2542974c862 216 * - Section 4: 2.44V - 2.1V = 6% - 0% (6% drop on 340 mV)
Vincent Coubard 0:f2542974c862 217 *
Vincent Coubard 0:f2542974c862 218 * These numbers are by no means accurate. Temperature and
Vincent Coubard 0:f2542974c862 219 * load in the actual application is not accounted for!
Vincent Coubard 0:f2542974c862 220 *
Vincent Coubard 0:f2542974c862 221 * @param[in] mvolts The voltage in mV
Vincent Coubard 0:f2542974c862 222 *
Vincent Coubard 0:f2542974c862 223 * @return Battery level in percent.
Vincent Coubard 0:f2542974c862 224 */
Vincent Coubard 0:f2542974c862 225 static __INLINE uint8_t battery_level_in_percent(const uint16_t mvolts)
Vincent Coubard 0:f2542974c862 226 {
Vincent Coubard 0:f2542974c862 227 uint8_t battery_level;
Vincent Coubard 0:f2542974c862 228
Vincent Coubard 0:f2542974c862 229 if (mvolts >= 3000)
Vincent Coubard 0:f2542974c862 230 {
Vincent Coubard 0:f2542974c862 231 battery_level = 100;
Vincent Coubard 0:f2542974c862 232 }
Vincent Coubard 0:f2542974c862 233 else if (mvolts > 2900)
Vincent Coubard 0:f2542974c862 234 {
Vincent Coubard 0:f2542974c862 235 battery_level = 100 - ((3000 - mvolts) * 58) / 100;
Vincent Coubard 0:f2542974c862 236 }
Vincent Coubard 0:f2542974c862 237 else if (mvolts > 2740)
Vincent Coubard 0:f2542974c862 238 {
Vincent Coubard 0:f2542974c862 239 battery_level = 42 - ((2900 - mvolts) * 24) / 160;
Vincent Coubard 0:f2542974c862 240 }
Vincent Coubard 0:f2542974c862 241 else if (mvolts > 2440)
Vincent Coubard 0:f2542974c862 242 {
Vincent Coubard 0:f2542974c862 243 battery_level = 18 - ((2740 - mvolts) * 12) / 300;
Vincent Coubard 0:f2542974c862 244 }
Vincent Coubard 0:f2542974c862 245 else if (mvolts > 2100)
Vincent Coubard 0:f2542974c862 246 {
Vincent Coubard 0:f2542974c862 247 battery_level = 6 - ((2440 - mvolts) * 6) / 340;
Vincent Coubard 0:f2542974c862 248 }
Vincent Coubard 0:f2542974c862 249 else
Vincent Coubard 0:f2542974c862 250 {
Vincent Coubard 0:f2542974c862 251 battery_level = 0;
Vincent Coubard 0:f2542974c862 252 }
Vincent Coubard 0:f2542974c862 253
Vincent Coubard 0:f2542974c862 254 return battery_level;
Vincent Coubard 0:f2542974c862 255 }
Vincent Coubard 0:f2542974c862 256
Vincent Coubard 0:f2542974c862 257 /**@brief Function for checking if a pointer value is aligned to a 4 byte boundary.
Vincent Coubard 0:f2542974c862 258 *
Vincent Coubard 0:f2542974c862 259 * @param[in] p Pointer value to be checked.
Vincent Coubard 0:f2542974c862 260 *
Vincent Coubard 0:f2542974c862 261 * @return TRUE if pointer is aligned to a 4 byte boundary, FALSE otherwise.
Vincent Coubard 0:f2542974c862 262 */
Vincent Coubard 0:f2542974c862 263 static __INLINE bool is_word_aligned(void * p)
Vincent Coubard 0:f2542974c862 264 {
Vincent Coubard 0:f2542974c862 265 return (((uintptr_t)p & 0x03) == 0);
Vincent Coubard 0:f2542974c862 266 }
Vincent Coubard 0:f2542974c862 267
Vincent Coubard 0:f2542974c862 268 #endif // APP_UTIL_H__
Vincent Coubard 0:f2542974c862 269
vcoubard 1:ebc0e0ef0a11 270 /** @} */