Nordic nrf51 sdk sources. Mirrored from https://github.com/ARMmbed/nrf51-sdk.
source/nordic_sdk/components/libraries/util/app_util.h@24:2aea0c1c57ee, 2016-04-07 (annotated)
- Committer:
- vcoubard
- Date:
- Thu Apr 07 17:37:49 2016 +0100
- Revision:
- 24:2aea0c1c57ee
- Parent:
- 22:67a8d2c0bbbf
- Child:
- 27:0fe148f1bca3
Synchronized with git rev 709d3cdb
Author: Liyou Zhou
Change version number in README
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vcoubard | 20:a90c48eb1d30 | 1 | /* |
vcoubard | 20:a90c48eb1d30 | 2 | * Copyright (c) Nordic Semiconductor ASA |
vcoubard | 20:a90c48eb1d30 | 3 | * All rights reserved. |
vcoubard | 20:a90c48eb1d30 | 4 | * |
vcoubard | 20:a90c48eb1d30 | 5 | * Redistribution and use in source and binary forms, with or without modification, |
vcoubard | 20:a90c48eb1d30 | 6 | * are permitted provided that the following conditions are met: |
vcoubard | 20:a90c48eb1d30 | 7 | * |
vcoubard | 20:a90c48eb1d30 | 8 | * 1. Redistributions of source code must retain the above copyright notice, this |
vcoubard | 20:a90c48eb1d30 | 9 | * list of conditions and the following disclaimer. |
vcoubard | 20:a90c48eb1d30 | 10 | * |
vcoubard | 20:a90c48eb1d30 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, this |
vcoubard | 20:a90c48eb1d30 | 12 | * list of conditions and the following disclaimer in the documentation and/or |
vcoubard | 20:a90c48eb1d30 | 13 | * other materials provided with the distribution. |
vcoubard | 20:a90c48eb1d30 | 14 | * |
vcoubard | 20:a90c48eb1d30 | 15 | * 3. Neither the name of Nordic Semiconductor ASA nor the names of other |
vcoubard | 20:a90c48eb1d30 | 16 | * contributors to this software may be used to endorse or promote products |
vcoubard | 20:a90c48eb1d30 | 17 | * derived from this software without specific prior written permission. |
vcoubard | 20:a90c48eb1d30 | 18 | * |
vcoubard | 20:a90c48eb1d30 | 19 | * |
vcoubard | 20:a90c48eb1d30 | 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
vcoubard | 20:a90c48eb1d30 | 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
vcoubard | 20:a90c48eb1d30 | 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
vcoubard | 20:a90c48eb1d30 | 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
vcoubard | 20:a90c48eb1d30 | 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
vcoubard | 20:a90c48eb1d30 | 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
vcoubard | 20:a90c48eb1d30 | 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
vcoubard | 20:a90c48eb1d30 | 27 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
vcoubard | 20:a90c48eb1d30 | 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
vcoubard | 20:a90c48eb1d30 | 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
vcoubard | 20:a90c48eb1d30 | 30 | * |
Vincent Coubard |
0:f2542974c862 | 31 | */ |
Vincent Coubard |
0:f2542974c862 | 32 | |
Vincent Coubard |
0:f2542974c862 | 33 | /** @file |
Vincent Coubard |
0:f2542974c862 | 34 | * |
Vincent Coubard |
0:f2542974c862 | 35 | * @defgroup app_util Utility Functions and Definitions |
Vincent Coubard |
0:f2542974c862 | 36 | * @{ |
Vincent Coubard |
0:f2542974c862 | 37 | * @ingroup app_common |
Vincent Coubard |
0:f2542974c862 | 38 | * |
Vincent Coubard |
0:f2542974c862 | 39 | * @brief Various types and definitions available to all applications. |
Vincent Coubard |
0:f2542974c862 | 40 | */ |
Vincent Coubard |
0:f2542974c862 | 41 | |
Vincent Coubard |
0:f2542974c862 | 42 | #ifndef APP_UTIL_H__ |
Vincent Coubard |
0:f2542974c862 | 43 | #define APP_UTIL_H__ |
Vincent Coubard |
0:f2542974c862 | 44 | |
Vincent Coubard |
0:f2542974c862 | 45 | #include <stdint.h> |
Vincent Coubard |
0:f2542974c862 | 46 | #include <stdbool.h> |
Vincent Coubard |
0:f2542974c862 | 47 | #include "compiler_abstraction.h" |
Vincent Coubard |
0:f2542974c862 | 48 | |
Vincent Coubard |
0:f2542974c862 | 49 | enum |
Vincent Coubard |
0:f2542974c862 | 50 | { |
Vincent Coubard |
0:f2542974c862 | 51 | UNIT_0_625_MS = 625, /**< Number of microseconds in 0.625 milliseconds. */ |
Vincent Coubard |
0:f2542974c862 | 52 | UNIT_1_25_MS = 1250, /**< Number of microseconds in 1.25 milliseconds. */ |
Vincent Coubard |
0:f2542974c862 | 53 | UNIT_10_MS = 10000 /**< Number of microseconds in 10 milliseconds. */ |
Vincent Coubard |
0:f2542974c862 | 54 | }; |
Vincent Coubard |
0:f2542974c862 | 55 | |
vcoubard | 24:2aea0c1c57ee | 56 | |
vcoubard | 24:2aea0c1c57ee | 57 | /**@brief Implementation specific macro for delayed macro expansion used in string concatenation |
vcoubard | 24:2aea0c1c57ee | 58 | * |
vcoubard | 24:2aea0c1c57ee | 59 | * @param[in] lhs Left hand side in concatenation |
vcoubard | 24:2aea0c1c57ee | 60 | * @param[in] rhs Right hand side in concatenation |
vcoubard | 24:2aea0c1c57ee | 61 | */ |
vcoubard | 24:2aea0c1c57ee | 62 | #define STRING_CONCATENATE_IMPL(lhs, rhs) lhs ## rhs |
vcoubard | 24:2aea0c1c57ee | 63 | |
vcoubard | 24:2aea0c1c57ee | 64 | |
vcoubard | 24:2aea0c1c57ee | 65 | /**@brief Macro used to concatenate string using delayed macro expansion |
vcoubard | 24:2aea0c1c57ee | 66 | * |
vcoubard | 24:2aea0c1c57ee | 67 | * @note This macro will delay concatenation until the expressions have been resolved |
vcoubard | 24:2aea0c1c57ee | 68 | * |
vcoubard | 24:2aea0c1c57ee | 69 | * @param[in] lhs Left hand side in concatenation |
vcoubard | 24:2aea0c1c57ee | 70 | * @param[in] rhs Right hand side in concatenation |
vcoubard | 24:2aea0c1c57ee | 71 | */ |
vcoubard | 24:2aea0c1c57ee | 72 | #define STRING_CONCATENATE(lhs, rhs) STRING_CONCATENATE_IMPL(lhs, rhs) |
vcoubard | 24:2aea0c1c57ee | 73 | |
vcoubard | 24:2aea0c1c57ee | 74 | |
vcoubard | 24:2aea0c1c57ee | 75 | // Disable lint-warnings/errors for STATIC_ASSERT |
vcoubard | 24:2aea0c1c57ee | 76 | //lint --emacro(10,STATIC_ASSERT) |
vcoubard | 24:2aea0c1c57ee | 77 | //lint --emacro(18,STATIC_ASSERT) |
vcoubard | 24:2aea0c1c57ee | 78 | //lint --emacro(19,STATIC_ASSERT) |
vcoubard | 24:2aea0c1c57ee | 79 | //lint --emacro(30,STATIC_ASSERT) |
vcoubard | 24:2aea0c1c57ee | 80 | //lint --emacro(37,STATIC_ASSERT) |
vcoubard | 24:2aea0c1c57ee | 81 | //lint --emacro(42,STATIC_ASSERT) |
vcoubard | 24:2aea0c1c57ee | 82 | //lint --emacro(26,STATIC_ASSERT) |
vcoubard | 24:2aea0c1c57ee | 83 | //lint --emacro(102,STATIC_ASSERT) |
vcoubard | 24:2aea0c1c57ee | 84 | //lint --emacro(533,STATIC_ASSERT) |
vcoubard | 24:2aea0c1c57ee | 85 | //lint --emacro(534,STATIC_ASSERT) |
vcoubard | 24:2aea0c1c57ee | 86 | //lint --emacro(132,STATIC_ASSERT) |
vcoubard | 24:2aea0c1c57ee | 87 | //lint --emacro(414,STATIC_ASSERT) |
vcoubard | 24:2aea0c1c57ee | 88 | //lint --emacro(578,STATIC_ASSERT) |
vcoubard | 24:2aea0c1c57ee | 89 | //lint --emacro(628,STATIC_ASSERT) |
vcoubard | 24:2aea0c1c57ee | 90 | //lint --emacro(648,STATIC_ASSERT) |
vcoubard | 24:2aea0c1c57ee | 91 | //lint --emacro(830,STATIC_ASSERT) |
vcoubard | 24:2aea0c1c57ee | 92 | |
vcoubard | 24:2aea0c1c57ee | 93 | |
vcoubard | 22:67a8d2c0bbbf | 94 | /**@brief Macro for doing static (i.e. compile time) assertion. |
vcoubard | 24:2aea0c1c57ee | 95 | * |
vcoubard | 24:2aea0c1c57ee | 96 | * @note If the EXPR isn't resolvable, then the error message won't be shown. |
vcoubard | 24:2aea0c1c57ee | 97 | * |
vcoubard | 24:2aea0c1c57ee | 98 | * @note The output of STATIC_ASSERT_MSG will be different across different compilers. |
vcoubard | 24:2aea0c1c57ee | 99 | * |
vcoubard | 24:2aea0c1c57ee | 100 | * @param[in] EXPR Constant expression to be verified. |
vcoubard | 24:2aea0c1c57ee | 101 | */ |
vcoubard | 24:2aea0c1c57ee | 102 | #if defined ( __COUNTER__ ) |
vcoubard | 19:47192cb9def7 | 103 | |
vcoubard | 24:2aea0c1c57ee | 104 | #define STATIC_ASSERT(EXPR) \ |
vcoubard | 24:2aea0c1c57ee | 105 | ;enum { STRING_CONCATENATE(static_assert_, __COUNTER__) = 1/(!!(EXPR)) } |
vcoubard | 24:2aea0c1c57ee | 106 | |
Vincent Coubard |
0:f2542974c862 | 107 | #else |
vcoubard | 24:2aea0c1c57ee | 108 | |
vcoubard | 24:2aea0c1c57ee | 109 | #define STATIC_ASSERT(EXPR) \ |
vcoubard | 24:2aea0c1c57ee | 110 | ;enum { STRING_CONCATENATE(assert_line_, __LINE__) = 1/(!!(EXPR)) } |
vcoubard | 24:2aea0c1c57ee | 111 | |
Vincent Coubard |
0:f2542974c862 | 112 | #endif |
Vincent Coubard |
0:f2542974c862 | 113 | |
Vincent Coubard |
0:f2542974c862 | 114 | |
vcoubard | 24:2aea0c1c57ee | 115 | |
Vincent Coubard |
0:f2542974c862 | 116 | /**@brief type for holding an encoded (i.e. little endian) 16 bit unsigned integer. */ |
Vincent Coubard |
0:f2542974c862 | 117 | typedef uint8_t uint16_le_t[2]; |
Vincent Coubard |
0:f2542974c862 | 118 | |
Vincent Coubard |
0:f2542974c862 | 119 | /**@brief type for holding an encoded (i.e. little endian) 32 bit unsigned integer. */ |
Vincent Coubard |
0:f2542974c862 | 120 | typedef uint8_t uint32_le_t[4]; |
Vincent Coubard |
0:f2542974c862 | 121 | |
Vincent Coubard |
0:f2542974c862 | 122 | /**@brief Byte array type. */ |
Vincent Coubard |
0:f2542974c862 | 123 | typedef struct |
Vincent Coubard |
0:f2542974c862 | 124 | { |
Vincent Coubard |
0:f2542974c862 | 125 | uint16_t size; /**< Number of array entries. */ |
Vincent Coubard |
0:f2542974c862 | 126 | uint8_t * p_data; /**< Pointer to array entries. */ |
Vincent Coubard |
0:f2542974c862 | 127 | } uint8_array_t; |
vcoubard | 1:ebc0e0ef0a11 | 128 | |
Vincent Coubard |
0:f2542974c862 | 129 | /**@brief Perform rounded integer division (as opposed to truncating the result). |
Vincent Coubard |
0:f2542974c862 | 130 | * |
Vincent Coubard |
0:f2542974c862 | 131 | * @param[in] A Numerator. |
Vincent Coubard |
0:f2542974c862 | 132 | * @param[in] B Denominator. |
Vincent Coubard |
0:f2542974c862 | 133 | * |
Vincent Coubard |
0:f2542974c862 | 134 | * @return Rounded (integer) result of dividing A by B. |
Vincent Coubard |
0:f2542974c862 | 135 | */ |
Vincent Coubard |
0:f2542974c862 | 136 | #define ROUNDED_DIV(A, B) (((A) + ((B) / 2)) / (B)) |
Vincent Coubard |
0:f2542974c862 | 137 | |
Vincent Coubard |
0:f2542974c862 | 138 | /**@brief Check if the integer provided is a power of two. |
Vincent Coubard |
0:f2542974c862 | 139 | * |
Vincent Coubard |
0:f2542974c862 | 140 | * @param[in] A Number to be tested. |
Vincent Coubard |
0:f2542974c862 | 141 | * |
Vincent Coubard |
0:f2542974c862 | 142 | * @return true if value is power of two. |
Vincent Coubard |
0:f2542974c862 | 143 | * @return false if value not power of two. |
Vincent Coubard |
0:f2542974c862 | 144 | */ |
Vincent Coubard |
0:f2542974c862 | 145 | #define IS_POWER_OF_TWO(A) ( ((A) != 0) && ((((A) - 1) & (A)) == 0) ) |
Vincent Coubard |
0:f2542974c862 | 146 | |
Vincent Coubard |
0:f2542974c862 | 147 | /**@brief To convert milliseconds to ticks. |
Vincent Coubard |
0:f2542974c862 | 148 | * @param[in] TIME Number of milliseconds to convert. |
Vincent Coubard |
0:f2542974c862 | 149 | * @param[in] RESOLUTION Unit to be converted to in [us/ticks]. |
Vincent Coubard |
0:f2542974c862 | 150 | */ |
Vincent Coubard |
0:f2542974c862 | 151 | #define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION)) |
Vincent Coubard |
0:f2542974c862 | 152 | |
Vincent Coubard |
0:f2542974c862 | 153 | |
Vincent Coubard |
0:f2542974c862 | 154 | /**@brief Perform integer division, making sure the result is rounded up. |
Vincent Coubard |
0:f2542974c862 | 155 | * |
Vincent Coubard |
0:f2542974c862 | 156 | * @details One typical use for this is to compute the number of objects with size B is needed to |
Vincent Coubard |
0:f2542974c862 | 157 | * hold A number of bytes. |
Vincent Coubard |
0:f2542974c862 | 158 | * |
Vincent Coubard |
0:f2542974c862 | 159 | * @param[in] A Numerator. |
Vincent Coubard |
0:f2542974c862 | 160 | * @param[in] B Denominator. |
Vincent Coubard |
0:f2542974c862 | 161 | * |
Vincent Coubard |
0:f2542974c862 | 162 | * @return Integer result of dividing A by B, rounded up. |
Vincent Coubard |
0:f2542974c862 | 163 | */ |
Vincent Coubard |
0:f2542974c862 | 164 | #define CEIL_DIV(A, B) \ |
Vincent Coubard |
0:f2542974c862 | 165 | /*lint -save -e573 */ \ |
Vincent Coubard |
0:f2542974c862 | 166 | ((((A) - 1) / (B)) + 1) \ |
Vincent Coubard |
0:f2542974c862 | 167 | /*lint -restore */ |
Vincent Coubard |
0:f2542974c862 | 168 | |
Vincent Coubard |
0:f2542974c862 | 169 | /**@brief Function for encoding a uint16 value. |
Vincent Coubard |
0:f2542974c862 | 170 | * |
Vincent Coubard |
0:f2542974c862 | 171 | * @param[in] value Value to be encoded. |
Vincent Coubard |
0:f2542974c862 | 172 | * @param[out] p_encoded_data Buffer where the encoded data is to be written. |
Vincent Coubard |
0:f2542974c862 | 173 | * |
Vincent Coubard |
0:f2542974c862 | 174 | * @return Number of bytes written. |
Vincent Coubard |
0:f2542974c862 | 175 | */ |
Vincent Coubard |
0:f2542974c862 | 176 | static __INLINE uint8_t uint16_encode(uint16_t value, uint8_t * p_encoded_data) |
Vincent Coubard |
0:f2542974c862 | 177 | { |
Vincent Coubard |
0:f2542974c862 | 178 | p_encoded_data[0] = (uint8_t) ((value & 0x00FF) >> 0); |
Vincent Coubard |
0:f2542974c862 | 179 | p_encoded_data[1] = (uint8_t) ((value & 0xFF00) >> 8); |
Vincent Coubard |
0:f2542974c862 | 180 | return sizeof(uint16_t); |
Vincent Coubard |
0:f2542974c862 | 181 | } |
vcoubard | 1:ebc0e0ef0a11 | 182 | |
Vincent Coubard |
0:f2542974c862 | 183 | /**@brief Function for encoding a uint32 value. |
Vincent Coubard |
0:f2542974c862 | 184 | * |
Vincent Coubard |
0:f2542974c862 | 185 | * @param[in] value Value to be encoded. |
Vincent Coubard |
0:f2542974c862 | 186 | * @param[out] p_encoded_data Buffer where the encoded data is to be written. |
Vincent Coubard |
0:f2542974c862 | 187 | * |
Vincent Coubard |
0:f2542974c862 | 188 | * @return Number of bytes written. |
Vincent Coubard |
0:f2542974c862 | 189 | */ |
Vincent Coubard |
0:f2542974c862 | 190 | static __INLINE uint8_t uint32_encode(uint32_t value, uint8_t * p_encoded_data) |
Vincent Coubard |
0:f2542974c862 | 191 | { |
Vincent Coubard |
0:f2542974c862 | 192 | p_encoded_data[0] = (uint8_t) ((value & 0x000000FF) >> 0); |
Vincent Coubard |
0:f2542974c862 | 193 | p_encoded_data[1] = (uint8_t) ((value & 0x0000FF00) >> 8); |
Vincent Coubard |
0:f2542974c862 | 194 | p_encoded_data[2] = (uint8_t) ((value & 0x00FF0000) >> 16); |
Vincent Coubard |
0:f2542974c862 | 195 | p_encoded_data[3] = (uint8_t) ((value & 0xFF000000) >> 24); |
Vincent Coubard |
0:f2542974c862 | 196 | return sizeof(uint32_t); |
Vincent Coubard |
0:f2542974c862 | 197 | } |
Vincent Coubard |
0:f2542974c862 | 198 | |
Vincent Coubard |
0:f2542974c862 | 199 | /**@brief Function for decoding a uint16 value. |
Vincent Coubard |
0:f2542974c862 | 200 | * |
Vincent Coubard |
0:f2542974c862 | 201 | * @param[in] p_encoded_data Buffer where the encoded data is stored. |
Vincent Coubard |
0:f2542974c862 | 202 | * |
Vincent Coubard |
0:f2542974c862 | 203 | * @return Decoded value. |
Vincent Coubard |
0:f2542974c862 | 204 | */ |
Vincent Coubard |
0:f2542974c862 | 205 | static __INLINE uint16_t uint16_decode(const uint8_t * p_encoded_data) |
Vincent Coubard |
0:f2542974c862 | 206 | { |
vcoubard | 1:ebc0e0ef0a11 | 207 | return ( (((uint16_t)((uint8_t *)p_encoded_data)[0])) | |
Vincent Coubard |
0:f2542974c862 | 208 | (((uint16_t)((uint8_t *)p_encoded_data)[1]) << 8 )); |
Vincent Coubard |
0:f2542974c862 | 209 | } |
Vincent Coubard |
0:f2542974c862 | 210 | |
Vincent Coubard |
0:f2542974c862 | 211 | /**@brief Function for decoding a uint32 value. |
Vincent Coubard |
0:f2542974c862 | 212 | * |
Vincent Coubard |
0:f2542974c862 | 213 | * @param[in] p_encoded_data Buffer where the encoded data is stored. |
Vincent Coubard |
0:f2542974c862 | 214 | * |
Vincent Coubard |
0:f2542974c862 | 215 | * @return Decoded value. |
Vincent Coubard |
0:f2542974c862 | 216 | */ |
Vincent Coubard |
0:f2542974c862 | 217 | static __INLINE uint32_t uint32_decode(const uint8_t * p_encoded_data) |
Vincent Coubard |
0:f2542974c862 | 218 | { |
Vincent Coubard |
0:f2542974c862 | 219 | return ( (((uint32_t)((uint8_t *)p_encoded_data)[0]) << 0) | |
Vincent Coubard |
0:f2542974c862 | 220 | (((uint32_t)((uint8_t *)p_encoded_data)[1]) << 8) | |
Vincent Coubard |
0:f2542974c862 | 221 | (((uint32_t)((uint8_t *)p_encoded_data)[2]) << 16) | |
Vincent Coubard |
0:f2542974c862 | 222 | (((uint32_t)((uint8_t *)p_encoded_data)[3]) << 24 )); |
Vincent Coubard |
0:f2542974c862 | 223 | } |
vcoubard | 1:ebc0e0ef0a11 | 224 | |
Vincent Coubard |
0:f2542974c862 | 225 | /** @brief Function for converting the input voltage (in milli volts) into percentage of 3.0 Volts. |
Vincent Coubard |
0:f2542974c862 | 226 | * |
Vincent Coubard |
0:f2542974c862 | 227 | * @details The calculation is based on a linearized version of the battery's discharge |
Vincent Coubard |
0:f2542974c862 | 228 | * curve. 3.0V returns 100% battery level. The limit for power failure is 2.1V and |
Vincent Coubard |
0:f2542974c862 | 229 | * is considered to be the lower boundary. |
Vincent Coubard |
0:f2542974c862 | 230 | * |
Vincent Coubard |
0:f2542974c862 | 231 | * The discharge curve for CR2032 is non-linear. In this model it is split into |
Vincent Coubard |
0:f2542974c862 | 232 | * 4 linear sections: |
Vincent Coubard |
0:f2542974c862 | 233 | * - Section 1: 3.0V - 2.9V = 100% - 42% (58% drop on 100 mV) |
Vincent Coubard |
0:f2542974c862 | 234 | * - Section 2: 2.9V - 2.74V = 42% - 18% (24% drop on 160 mV) |
Vincent Coubard |
0:f2542974c862 | 235 | * - Section 3: 2.74V - 2.44V = 18% - 6% (12% drop on 300 mV) |
Vincent Coubard |
0:f2542974c862 | 236 | * - Section 4: 2.44V - 2.1V = 6% - 0% (6% drop on 340 mV) |
Vincent Coubard |
0:f2542974c862 | 237 | * |
Vincent Coubard |
0:f2542974c862 | 238 | * These numbers are by no means accurate. Temperature and |
Vincent Coubard |
0:f2542974c862 | 239 | * load in the actual application is not accounted for! |
Vincent Coubard |
0:f2542974c862 | 240 | * |
Vincent Coubard |
0:f2542974c862 | 241 | * @param[in] mvolts The voltage in mV |
Vincent Coubard |
0:f2542974c862 | 242 | * |
Vincent Coubard |
0:f2542974c862 | 243 | * @return Battery level in percent. |
Vincent Coubard |
0:f2542974c862 | 244 | */ |
Vincent Coubard |
0:f2542974c862 | 245 | static __INLINE uint8_t battery_level_in_percent(const uint16_t mvolts) |
Vincent Coubard |
0:f2542974c862 | 246 | { |
Vincent Coubard |
0:f2542974c862 | 247 | uint8_t battery_level; |
Vincent Coubard |
0:f2542974c862 | 248 | |
Vincent Coubard |
0:f2542974c862 | 249 | if (mvolts >= 3000) |
Vincent Coubard |
0:f2542974c862 | 250 | { |
Vincent Coubard |
0:f2542974c862 | 251 | battery_level = 100; |
Vincent Coubard |
0:f2542974c862 | 252 | } |
Vincent Coubard |
0:f2542974c862 | 253 | else if (mvolts > 2900) |
Vincent Coubard |
0:f2542974c862 | 254 | { |
Vincent Coubard |
0:f2542974c862 | 255 | battery_level = 100 - ((3000 - mvolts) * 58) / 100; |
Vincent Coubard |
0:f2542974c862 | 256 | } |
Vincent Coubard |
0:f2542974c862 | 257 | else if (mvolts > 2740) |
Vincent Coubard |
0:f2542974c862 | 258 | { |
Vincent Coubard |
0:f2542974c862 | 259 | battery_level = 42 - ((2900 - mvolts) * 24) / 160; |
Vincent Coubard |
0:f2542974c862 | 260 | } |
Vincent Coubard |
0:f2542974c862 | 261 | else if (mvolts > 2440) |
Vincent Coubard |
0:f2542974c862 | 262 | { |
Vincent Coubard |
0:f2542974c862 | 263 | battery_level = 18 - ((2740 - mvolts) * 12) / 300; |
Vincent Coubard |
0:f2542974c862 | 264 | } |
Vincent Coubard |
0:f2542974c862 | 265 | else if (mvolts > 2100) |
Vincent Coubard |
0:f2542974c862 | 266 | { |
Vincent Coubard |
0:f2542974c862 | 267 | battery_level = 6 - ((2440 - mvolts) * 6) / 340; |
Vincent Coubard |
0:f2542974c862 | 268 | } |
Vincent Coubard |
0:f2542974c862 | 269 | else |
Vincent Coubard |
0:f2542974c862 | 270 | { |
Vincent Coubard |
0:f2542974c862 | 271 | battery_level = 0; |
Vincent Coubard |
0:f2542974c862 | 272 | } |
Vincent Coubard |
0:f2542974c862 | 273 | |
Vincent Coubard |
0:f2542974c862 | 274 | return battery_level; |
Vincent Coubard |
0:f2542974c862 | 275 | } |
Vincent Coubard |
0:f2542974c862 | 276 | |
Vincent Coubard |
0:f2542974c862 | 277 | /**@brief Function for checking if a pointer value is aligned to a 4 byte boundary. |
Vincent Coubard |
0:f2542974c862 | 278 | * |
Vincent Coubard |
0:f2542974c862 | 279 | * @param[in] p Pointer value to be checked. |
Vincent Coubard |
0:f2542974c862 | 280 | * |
Vincent Coubard |
0:f2542974c862 | 281 | * @return TRUE if pointer is aligned to a 4 byte boundary, FALSE otherwise. |
Vincent Coubard |
0:f2542974c862 | 282 | */ |
Vincent Coubard |
0:f2542974c862 | 283 | static __INLINE bool is_word_aligned(void * p) |
Vincent Coubard |
0:f2542974c862 | 284 | { |
Vincent Coubard |
0:f2542974c862 | 285 | return (((uintptr_t)p & 0x03) == 0); |
Vincent Coubard |
0:f2542974c862 | 286 | } |
Vincent Coubard |
0:f2542974c862 | 287 | |
Vincent Coubard |
0:f2542974c862 | 288 | #endif // APP_UTIL_H__ |
Vincent Coubard |
0:f2542974c862 | 289 | |
vcoubard | 1:ebc0e0ef0a11 | 290 | /** @} */ |