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