The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
AnnaBridge
Date:
Wed Feb 20 20:53:29 2019 +0000
Revision:
172:65be27845400
Parent:
171:3a7713b1edbc
mbed library release version 165

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AnnaBridge 143:86740a56073b 1 /*
AnnaBridge 143:86740a56073b 2 * Copyright (c) 2012 Nordic Semiconductor ASA
AnnaBridge 143:86740a56073b 3 * All rights reserved.
AnnaBridge 143:86740a56073b 4 *
AnnaBridge 143:86740a56073b 5 * Redistribution and use in source and binary forms, with or without modification,
AnnaBridge 143:86740a56073b 6 * are permitted provided that the following conditions are met:
AnnaBridge 143:86740a56073b 7 *
AnnaBridge 143:86740a56073b 8 * 1. Redistributions of source code must retain the above copyright notice, this list
AnnaBridge 143:86740a56073b 9 * of conditions and the following disclaimer.
AnnaBridge 143:86740a56073b 10 *
AnnaBridge 143:86740a56073b 11 * 2. Redistributions in binary form, except as embedded into a Nordic Semiconductor ASA
AnnaBridge 143:86740a56073b 12 * integrated circuit in a product or a software update for such product, must reproduce
AnnaBridge 143:86740a56073b 13 * the above copyright notice, this list of conditions and the following disclaimer in
AnnaBridge 143:86740a56073b 14 * the documentation and/or other materials provided with the distribution.
AnnaBridge 143:86740a56073b 15 *
AnnaBridge 143:86740a56073b 16 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its contributors may be
AnnaBridge 143:86740a56073b 17 * used to endorse or promote products derived from this software without specific prior
AnnaBridge 143:86740a56073b 18 * written permission.
AnnaBridge 143:86740a56073b 19 *
AnnaBridge 143:86740a56073b 20 * 4. This software, with or without modification, must only be used with a
AnnaBridge 143:86740a56073b 21 * Nordic Semiconductor ASA integrated circuit.
AnnaBridge 143:86740a56073b 22 *
AnnaBridge 143:86740a56073b 23 * 5. Any software provided in binary or object form under this license must not be reverse
AnnaBridge 143:86740a56073b 24 * engineered, decompiled, modified and/or disassembled.
AnnaBridge 143:86740a56073b 25 *
AnnaBridge 143:86740a56073b 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
AnnaBridge 143:86740a56073b 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
AnnaBridge 143:86740a56073b 28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
AnnaBridge 143:86740a56073b 29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
AnnaBridge 143:86740a56073b 30 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
AnnaBridge 143:86740a56073b 31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
AnnaBridge 143:86740a56073b 32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
AnnaBridge 143:86740a56073b 33 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
AnnaBridge 143:86740a56073b 34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
AnnaBridge 143:86740a56073b 35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
AnnaBridge 143:86740a56073b 36 *
AnnaBridge 143:86740a56073b 37 */
AnnaBridge 143:86740a56073b 38
AnnaBridge 143:86740a56073b 39
AnnaBridge 143:86740a56073b 40 /** @file
AnnaBridge 143:86740a56073b 41 *
AnnaBridge 143:86740a56073b 42 * @defgroup app_util Utility Functions and Definitions
AnnaBridge 143:86740a56073b 43 * @{
AnnaBridge 143:86740a56073b 44 * @ingroup app_common
AnnaBridge 143:86740a56073b 45 *
AnnaBridge 143:86740a56073b 46 * @brief Various types and definitions available to all applications.
AnnaBridge 143:86740a56073b 47 */
AnnaBridge 143:86740a56073b 48
AnnaBridge 143:86740a56073b 49 #ifndef APP_UTIL_H__
AnnaBridge 143:86740a56073b 50 #define APP_UTIL_H__
AnnaBridge 143:86740a56073b 51
AnnaBridge 143:86740a56073b 52 #include <stdint.h>
AnnaBridge 143:86740a56073b 53 #include <stdbool.h>
AnnaBridge 143:86740a56073b 54 #include "compiler_abstraction.h"
AnnaBridge 143:86740a56073b 55 #include "nrf.h"
AnnaBridge 143:86740a56073b 56
AnnaBridge 143:86740a56073b 57 //lint -save -e27 -e10 -e19
AnnaBridge 143:86740a56073b 58 #if defined ( __CC_ARM )
AnnaBridge 143:86740a56073b 59 extern char STACK$$Base;
AnnaBridge 143:86740a56073b 60 extern char STACK$$Length;
AnnaBridge 143:86740a56073b 61 #define STACK_BASE &STACK$$Base
AnnaBridge 143:86740a56073b 62 #define STACK_TOP ((void*)((uint32_t)STACK_BASE + (uint32_t)&STACK$$Length))
AnnaBridge 143:86740a56073b 63 #elif defined ( __ICCARM__ )
AnnaBridge 143:86740a56073b 64 extern char CSTACK$$Base;
AnnaBridge 143:86740a56073b 65 extern char CSTACK$$Length;
AnnaBridge 143:86740a56073b 66 #define STACK_BASE &CSTACK$$Base
AnnaBridge 143:86740a56073b 67 #define STACK_TOP ((void*)((uint32_t)STACK_BASE + (uint32_t)&CSTACK$$Length))
AnnaBridge 143:86740a56073b 68 #elif defined ( __GNUC__ )
AnnaBridge 143:86740a56073b 69 extern uint32_t __StackTop;
AnnaBridge 143:86740a56073b 70 extern uint32_t __StackLimit;
AnnaBridge 143:86740a56073b 71 #define STACK_BASE &__StackLimit
AnnaBridge 143:86740a56073b 72 #define STACK_TOP &__StackTop
AnnaBridge 143:86740a56073b 73 #endif
AnnaBridge 143:86740a56073b 74 //lint -restore
AnnaBridge 143:86740a56073b 75
AnnaBridge 143:86740a56073b 76 enum
AnnaBridge 143:86740a56073b 77 {
AnnaBridge 143:86740a56073b 78 UNIT_0_625_MS = 625, /**< Number of microseconds in 0.625 milliseconds. */
AnnaBridge 143:86740a56073b 79 UNIT_1_25_MS = 1250, /**< Number of microseconds in 1.25 milliseconds. */
AnnaBridge 143:86740a56073b 80 UNIT_10_MS = 10000 /**< Number of microseconds in 10 milliseconds. */
AnnaBridge 143:86740a56073b 81 };
AnnaBridge 143:86740a56073b 82
AnnaBridge 143:86740a56073b 83
AnnaBridge 143:86740a56073b 84 /**@brief Implementation specific macro for delayed macro expansion used in string concatenation
AnnaBridge 143:86740a56073b 85 *
AnnaBridge 143:86740a56073b 86 * @param[in] lhs Left hand side in concatenation
AnnaBridge 143:86740a56073b 87 * @param[in] rhs Right hand side in concatenation
AnnaBridge 143:86740a56073b 88 */
AnnaBridge 143:86740a56073b 89 #define STRING_CONCATENATE_IMPL(lhs, rhs) lhs ## rhs
AnnaBridge 143:86740a56073b 90
AnnaBridge 143:86740a56073b 91
AnnaBridge 143:86740a56073b 92 /**@brief Macro used to concatenate string using delayed macro expansion
AnnaBridge 143:86740a56073b 93 *
AnnaBridge 143:86740a56073b 94 * @note This macro will delay concatenation until the expressions have been resolved
AnnaBridge 143:86740a56073b 95 *
AnnaBridge 143:86740a56073b 96 * @param[in] lhs Left hand side in concatenation
AnnaBridge 143:86740a56073b 97 * @param[in] rhs Right hand side in concatenation
AnnaBridge 143:86740a56073b 98 */
AnnaBridge 143:86740a56073b 99 #define STRING_CONCATENATE(lhs, rhs) STRING_CONCATENATE_IMPL(lhs, rhs)
AnnaBridge 143:86740a56073b 100
AnnaBridge 143:86740a56073b 101
AnnaBridge 143:86740a56073b 102 // Disable lint-warnings/errors for STATIC_ASSERT
AnnaBridge 143:86740a56073b 103 //lint --emacro(10,STATIC_ASSERT)
AnnaBridge 143:86740a56073b 104 //lint --emacro(18,STATIC_ASSERT)
AnnaBridge 143:86740a56073b 105 //lint --emacro(19,STATIC_ASSERT)
AnnaBridge 143:86740a56073b 106 //lint --emacro(30,STATIC_ASSERT)
AnnaBridge 143:86740a56073b 107 //lint --emacro(37,STATIC_ASSERT)
AnnaBridge 143:86740a56073b 108 //lint --emacro(42,STATIC_ASSERT)
AnnaBridge 143:86740a56073b 109 //lint --emacro(26,STATIC_ASSERT)
AnnaBridge 143:86740a56073b 110 //lint --emacro(102,STATIC_ASSERT)
AnnaBridge 143:86740a56073b 111 //lint --emacro(533,STATIC_ASSERT)
AnnaBridge 143:86740a56073b 112 //lint --emacro(534,STATIC_ASSERT)
AnnaBridge 143:86740a56073b 113 //lint --emacro(132,STATIC_ASSERT)
AnnaBridge 143:86740a56073b 114 //lint --emacro(414,STATIC_ASSERT)
AnnaBridge 143:86740a56073b 115 //lint --emacro(578,STATIC_ASSERT)
AnnaBridge 143:86740a56073b 116 //lint --emacro(628,STATIC_ASSERT)
AnnaBridge 143:86740a56073b 117 //lint --emacro(648,STATIC_ASSERT)
AnnaBridge 143:86740a56073b 118 //lint --emacro(830,STATIC_ASSERT)
AnnaBridge 143:86740a56073b 119
AnnaBridge 143:86740a56073b 120
AnnaBridge 143:86740a56073b 121 /**@brief Macro for doing static (i.e. compile time) assertion.
AnnaBridge 143:86740a56073b 122 *
AnnaBridge 143:86740a56073b 123 * @note If the EXPR isn't resolvable, then the error message won't be shown.
AnnaBridge 143:86740a56073b 124 *
AnnaBridge 143:86740a56073b 125 * @note The output of STATIC_ASSERT will be different across different compilers.
AnnaBridge 143:86740a56073b 126 *
AnnaBridge 143:86740a56073b 127 * @param[in] EXPR Constant expression to be verified.
AnnaBridge 143:86740a56073b 128 */
AnnaBridge 143:86740a56073b 129 #if defined ( __COUNTER__ )
AnnaBridge 143:86740a56073b 130
AnnaBridge 143:86740a56073b 131 #define STATIC_ASSERT(EXPR) \
AnnaBridge 143:86740a56073b 132 ;enum { STRING_CONCATENATE(static_assert_, __COUNTER__) = 1/(!!(EXPR)) }
AnnaBridge 143:86740a56073b 133
AnnaBridge 143:86740a56073b 134 #else
AnnaBridge 143:86740a56073b 135
AnnaBridge 143:86740a56073b 136 #define STATIC_ASSERT(EXPR) \
AnnaBridge 143:86740a56073b 137 ;enum { STRING_CONCATENATE(assert_line_, __LINE__) = 1/(!!(EXPR)) }
AnnaBridge 143:86740a56073b 138
AnnaBridge 143:86740a56073b 139 #endif
AnnaBridge 143:86740a56073b 140
AnnaBridge 143:86740a56073b 141
AnnaBridge 143:86740a56073b 142 /**@brief Implementation details for NUM_VAR_ARGS */
AnnaBridge 143:86740a56073b 143 #define NUM_VA_ARGS_IMPL( \
AnnaBridge 143:86740a56073b 144 _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
AnnaBridge 143:86740a56073b 145 _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
AnnaBridge 143:86740a56073b 146 _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \
AnnaBridge 143:86740a56073b 147 _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \
AnnaBridge 143:86740a56073b 148 _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \
AnnaBridge 143:86740a56073b 149 _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, \
AnnaBridge 143:86740a56073b 150 _61, _62, N, ...) N
AnnaBridge 143:86740a56073b 151
AnnaBridge 143:86740a56073b 152
AnnaBridge 143:86740a56073b 153 /**@brief Macro to get the number of arguments in a call variadic macro call
AnnaBridge 143:86740a56073b 154 *
AnnaBridge 143:86740a56073b 155 * param[in] ... List of arguments
AnnaBridge 143:86740a56073b 156 *
AnnaBridge 143:86740a56073b 157 * @retval Number of variadic arguments in the argument list
AnnaBridge 143:86740a56073b 158 */
AnnaBridge 143:86740a56073b 159 #define NUM_VA_ARGS(...) NUM_VA_ARGS_IMPL(__VA_ARGS__, 63, 62, 61, \
AnnaBridge 143:86740a56073b 160 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, \
AnnaBridge 143:86740a56073b 161 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, \
AnnaBridge 143:86740a56073b 162 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, \
AnnaBridge 143:86740a56073b 163 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, \
AnnaBridge 143:86740a56073b 164 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \
AnnaBridge 143:86740a56073b 165 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
AnnaBridge 143:86740a56073b 166
AnnaBridge 143:86740a56073b 167
AnnaBridge 143:86740a56073b 168 /**@brief type for holding an encoded (i.e. little endian) 16 bit unsigned integer. */
AnnaBridge 143:86740a56073b 169 typedef uint8_t uint16_le_t[2];
AnnaBridge 143:86740a56073b 170
AnnaBridge 143:86740a56073b 171 /**@brief Type for holding an encoded (i.e. little endian) 32 bit unsigned integer. */
AnnaBridge 143:86740a56073b 172 typedef uint8_t uint32_le_t[4];
AnnaBridge 143:86740a56073b 173
AnnaBridge 143:86740a56073b 174 /**@brief Byte array type. */
AnnaBridge 143:86740a56073b 175 typedef struct
AnnaBridge 143:86740a56073b 176 {
AnnaBridge 143:86740a56073b 177 uint16_t size; /**< Number of array entries. */
AnnaBridge 143:86740a56073b 178 uint8_t * p_data; /**< Pointer to array entries. */
AnnaBridge 143:86740a56073b 179 } uint8_array_t;
AnnaBridge 143:86740a56073b 180
AnnaBridge 143:86740a56073b 181
AnnaBridge 143:86740a56073b 182 /**@brief Macro for performing rounded integer division (as opposed to truncating the result).
AnnaBridge 143:86740a56073b 183 *
AnnaBridge 143:86740a56073b 184 * @param[in] A Numerator.
AnnaBridge 143:86740a56073b 185 * @param[in] B Denominator.
AnnaBridge 143:86740a56073b 186 *
AnnaBridge 143:86740a56073b 187 * @return Rounded (integer) result of dividing A by B.
AnnaBridge 143:86740a56073b 188 */
AnnaBridge 143:86740a56073b 189 #define ROUNDED_DIV(A, B) (((A) + ((B) / 2)) / (B))
AnnaBridge 143:86740a56073b 190
AnnaBridge 143:86740a56073b 191
AnnaBridge 143:86740a56073b 192 /**@brief Macro for checking if an integer is a power of two.
AnnaBridge 143:86740a56073b 193 *
AnnaBridge 143:86740a56073b 194 * @param[in] A Number to be tested.
AnnaBridge 143:86740a56073b 195 *
AnnaBridge 143:86740a56073b 196 * @return true if value is power of two.
AnnaBridge 143:86740a56073b 197 * @return false if value not power of two.
AnnaBridge 143:86740a56073b 198 */
AnnaBridge 143:86740a56073b 199 #define IS_POWER_OF_TWO(A) ( ((A) != 0) && ((((A) - 1) & (A)) == 0) )
AnnaBridge 143:86740a56073b 200
AnnaBridge 143:86740a56073b 201
AnnaBridge 143:86740a56073b 202 /**@brief Macro for converting milliseconds to ticks.
AnnaBridge 143:86740a56073b 203 *
AnnaBridge 143:86740a56073b 204 * @param[in] TIME Number of milliseconds to convert.
AnnaBridge 143:86740a56073b 205 * @param[in] RESOLUTION Unit to be converted to in [us/ticks].
AnnaBridge 143:86740a56073b 206 */
AnnaBridge 143:86740a56073b 207 #define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION))
AnnaBridge 143:86740a56073b 208
AnnaBridge 143:86740a56073b 209
AnnaBridge 143:86740a56073b 210 /**@brief Macro for performing integer division, making sure the result is rounded up.
AnnaBridge 143:86740a56073b 211 *
AnnaBridge 143:86740a56073b 212 * @details One typical use for this is to compute the number of objects with size B is needed to
AnnaBridge 143:86740a56073b 213 * hold A number of bytes.
AnnaBridge 143:86740a56073b 214 *
AnnaBridge 143:86740a56073b 215 * @param[in] A Numerator.
AnnaBridge 143:86740a56073b 216 * @param[in] B Denominator.
AnnaBridge 143:86740a56073b 217 *
AnnaBridge 143:86740a56073b 218 * @return Integer result of dividing A by B, rounded up.
AnnaBridge 143:86740a56073b 219 */
AnnaBridge 143:86740a56073b 220 #define CEIL_DIV(A, B) \
AnnaBridge 143:86740a56073b 221 (((A) + (B) - 1) / (B))
AnnaBridge 143:86740a56073b 222
AnnaBridge 143:86740a56073b 223
AnnaBridge 143:86740a56073b 224 /**@brief Macro for creating a buffer aligned to 4 bytes.
AnnaBridge 143:86740a56073b 225 *
AnnaBridge 143:86740a56073b 226 * @param[in] NAME Name of the buffor.
AnnaBridge 143:86740a56073b 227 * @param[in] MIN_SIZE Size of this buffor (it will be rounded up to multiples of 4 bytes).
AnnaBridge 143:86740a56073b 228 */
AnnaBridge 143:86740a56073b 229 #define WORD_ALIGNED_MEM_BUFF(NAME, MIN_SIZE) static uint32_t NAME[CEIL_DIV(MIN_SIZE, sizeof(uint32_t))]
AnnaBridge 143:86740a56073b 230
AnnaBridge 143:86740a56073b 231
AnnaBridge 143:86740a56073b 232 /**@brief Macro for calculating the number of words that are needed to hold a number of bytes.
AnnaBridge 143:86740a56073b 233 *
AnnaBridge 143:86740a56073b 234 * @details Adds 3 and divides by 4.
AnnaBridge 143:86740a56073b 235 *
AnnaBridge 143:86740a56073b 236 * @param[in] n_bytes The number of bytes.
AnnaBridge 143:86740a56073b 237 *
AnnaBridge 143:86740a56073b 238 * @return The number of words that @p n_bytes take up (rounded up).
AnnaBridge 143:86740a56073b 239 */
AnnaBridge 143:86740a56073b 240 #define BYTES_TO_WORDS(n_bytes) (((n_bytes) + 3) >> 2)
AnnaBridge 143:86740a56073b 241
AnnaBridge 143:86740a56073b 242
AnnaBridge 143:86740a56073b 243 /**@brief The number of bytes in a word.
AnnaBridge 143:86740a56073b 244 */
AnnaBridge 143:86740a56073b 245 #define BYTES_PER_WORD (4)
AnnaBridge 143:86740a56073b 246
AnnaBridge 143:86740a56073b 247
AnnaBridge 143:86740a56073b 248 /**@brief Macro for increasing a number to the nearest (larger) multiple of another number.
AnnaBridge 143:86740a56073b 249 *
AnnaBridge 143:86740a56073b 250 * @param[in] alignment The number to align to.
AnnaBridge 143:86740a56073b 251 * @param[in] number The number to align (increase).
AnnaBridge 143:86740a56073b 252 *
AnnaBridge 143:86740a56073b 253 * @return The aligned (increased) @p number.
AnnaBridge 143:86740a56073b 254 */
AnnaBridge 143:86740a56073b 255 #define ALIGN_NUM(alignment, number) ((number - 1) + alignment - ((number - 1) % alignment))
AnnaBridge 143:86740a56073b 256
AnnaBridge 143:86740a56073b 257
AnnaBridge 143:86740a56073b 258 /**@brief Function for changing the value unit.
AnnaBridge 143:86740a56073b 259 *
AnnaBridge 143:86740a56073b 260 * @param[in] value Value to be rescaled.
AnnaBridge 143:86740a56073b 261 * @param[in] old_unit_reversal Reversal of the incoming unit.
AnnaBridge 143:86740a56073b 262 * @param[in] new_unit_reversal Reversal of the desired unit.
AnnaBridge 143:86740a56073b 263 *
AnnaBridge 143:86740a56073b 264 * @return Number of bytes written.
AnnaBridge 143:86740a56073b 265 */
AnnaBridge 143:86740a56073b 266 static __INLINE uint64_t value_rescale(uint32_t value, uint32_t old_unit_reversal, uint16_t new_unit_reversal)
AnnaBridge 143:86740a56073b 267 {
AnnaBridge 143:86740a56073b 268 return (uint64_t)ROUNDED_DIV((uint64_t)value * new_unit_reversal, old_unit_reversal);
AnnaBridge 143:86740a56073b 269 }
AnnaBridge 143:86740a56073b 270
AnnaBridge 143:86740a56073b 271 /**@brief Function for encoding a uint16 value.
AnnaBridge 143:86740a56073b 272 *
AnnaBridge 143:86740a56073b 273 * @param[in] value Value to be encoded.
AnnaBridge 143:86740a56073b 274 * @param[out] p_encoded_data Buffer where the encoded data is to be written.
AnnaBridge 143:86740a56073b 275 *
AnnaBridge 143:86740a56073b 276 * @return Number of bytes written.
AnnaBridge 143:86740a56073b 277 */
AnnaBridge 143:86740a56073b 278 static __INLINE uint8_t uint16_encode(uint16_t value, uint8_t * p_encoded_data)
AnnaBridge 143:86740a56073b 279 {
AnnaBridge 143:86740a56073b 280 p_encoded_data[0] = (uint8_t) ((value & 0x00FF) >> 0);
AnnaBridge 143:86740a56073b 281 p_encoded_data[1] = (uint8_t) ((value & 0xFF00) >> 8);
AnnaBridge 143:86740a56073b 282 return sizeof(uint16_t);
AnnaBridge 143:86740a56073b 283 }
AnnaBridge 143:86740a56073b 284
AnnaBridge 143:86740a56073b 285 /**@brief Function for encoding a three-byte value.
AnnaBridge 143:86740a56073b 286 *
AnnaBridge 143:86740a56073b 287 * @param[in] value Value to be encoded.
AnnaBridge 143:86740a56073b 288 * @param[out] p_encoded_data Buffer where the encoded data is to be written.
AnnaBridge 143:86740a56073b 289 *
AnnaBridge 143:86740a56073b 290 * @return Number of bytes written.
AnnaBridge 143:86740a56073b 291 */
AnnaBridge 143:86740a56073b 292 static __INLINE uint8_t uint24_encode(uint32_t value, uint8_t * p_encoded_data)
AnnaBridge 143:86740a56073b 293 {
AnnaBridge 143:86740a56073b 294 p_encoded_data[0] = (uint8_t) ((value & 0x000000FF) >> 0);
AnnaBridge 143:86740a56073b 295 p_encoded_data[1] = (uint8_t) ((value & 0x0000FF00) >> 8);
AnnaBridge 143:86740a56073b 296 p_encoded_data[2] = (uint8_t) ((value & 0x00FF0000) >> 16);
AnnaBridge 143:86740a56073b 297 return 3;
AnnaBridge 143:86740a56073b 298 }
AnnaBridge 143:86740a56073b 299
AnnaBridge 143:86740a56073b 300 /**@brief Function for encoding a uint32 value.
AnnaBridge 143:86740a56073b 301 *
AnnaBridge 143:86740a56073b 302 * @param[in] value Value to be encoded.
AnnaBridge 143:86740a56073b 303 * @param[out] p_encoded_data Buffer where the encoded data is to be written.
AnnaBridge 143:86740a56073b 304 *
AnnaBridge 143:86740a56073b 305 * @return Number of bytes written.
AnnaBridge 143:86740a56073b 306 */
AnnaBridge 143:86740a56073b 307 static __INLINE uint8_t uint32_encode(uint32_t value, uint8_t * p_encoded_data)
AnnaBridge 143:86740a56073b 308 {
AnnaBridge 143:86740a56073b 309 p_encoded_data[0] = (uint8_t) ((value & 0x000000FF) >> 0);
AnnaBridge 143:86740a56073b 310 p_encoded_data[1] = (uint8_t) ((value & 0x0000FF00) >> 8);
AnnaBridge 143:86740a56073b 311 p_encoded_data[2] = (uint8_t) ((value & 0x00FF0000) >> 16);
AnnaBridge 143:86740a56073b 312 p_encoded_data[3] = (uint8_t) ((value & 0xFF000000) >> 24);
AnnaBridge 143:86740a56073b 313 return sizeof(uint32_t);
AnnaBridge 143:86740a56073b 314 }
AnnaBridge 143:86740a56073b 315
AnnaBridge 143:86740a56073b 316 /**@brief Function for encoding a uint48 value.
AnnaBridge 143:86740a56073b 317 *
AnnaBridge 143:86740a56073b 318 * @param[in] value Value to be encoded.
AnnaBridge 143:86740a56073b 319 * @param[out] p_encoded_data Buffer where the encoded data is to be written.
AnnaBridge 143:86740a56073b 320 *
AnnaBridge 143:86740a56073b 321 * @return Number of bytes written.
AnnaBridge 143:86740a56073b 322 */
AnnaBridge 143:86740a56073b 323 static __INLINE uint8_t uint48_encode(uint64_t value, uint8_t * p_encoded_data)
AnnaBridge 143:86740a56073b 324 {
AnnaBridge 143:86740a56073b 325 p_encoded_data[0] = (uint8_t) ((value & 0x0000000000FF) >> 0);
AnnaBridge 143:86740a56073b 326 p_encoded_data[1] = (uint8_t) ((value & 0x00000000FF00) >> 8);
AnnaBridge 143:86740a56073b 327 p_encoded_data[2] = (uint8_t) ((value & 0x000000FF0000) >> 16);
AnnaBridge 143:86740a56073b 328 p_encoded_data[3] = (uint8_t) ((value & 0x0000FF000000) >> 24);
AnnaBridge 143:86740a56073b 329 p_encoded_data[4] = (uint8_t) ((value & 0x00FF00000000) >> 32);
AnnaBridge 143:86740a56073b 330 p_encoded_data[5] = (uint8_t) ((value & 0xFF0000000000) >> 40);
AnnaBridge 143:86740a56073b 331 return 6;
AnnaBridge 143:86740a56073b 332 }
AnnaBridge 143:86740a56073b 333
AnnaBridge 143:86740a56073b 334 /**@brief Function for decoding a uint16 value.
AnnaBridge 143:86740a56073b 335 *
AnnaBridge 143:86740a56073b 336 * @param[in] p_encoded_data Buffer where the encoded data is stored.
AnnaBridge 143:86740a56073b 337 *
AnnaBridge 143:86740a56073b 338 * @return Decoded value.
AnnaBridge 143:86740a56073b 339 */
AnnaBridge 143:86740a56073b 340 static __INLINE uint16_t uint16_decode(const uint8_t * p_encoded_data)
AnnaBridge 143:86740a56073b 341 {
AnnaBridge 143:86740a56073b 342 return ( (((uint16_t)((uint8_t *)p_encoded_data)[0])) |
AnnaBridge 143:86740a56073b 343 (((uint16_t)((uint8_t *)p_encoded_data)[1]) << 8 ));
AnnaBridge 143:86740a56073b 344 }
AnnaBridge 143:86740a56073b 345
AnnaBridge 143:86740a56073b 346 /**@brief Function for decoding a uint16 value in big-endian format.
AnnaBridge 143:86740a56073b 347 *
AnnaBridge 143:86740a56073b 348 * @param[in] p_encoded_data Buffer where the encoded data is stored.
AnnaBridge 143:86740a56073b 349 *
AnnaBridge 143:86740a56073b 350 * @return Decoded value.
AnnaBridge 143:86740a56073b 351 */
AnnaBridge 143:86740a56073b 352 static __INLINE uint16_t uint16_big_decode(const uint8_t * p_encoded_data)
AnnaBridge 143:86740a56073b 353 {
AnnaBridge 143:86740a56073b 354 return ( (((uint16_t)((uint8_t *)p_encoded_data)[0]) << 8 ) |
AnnaBridge 143:86740a56073b 355 (((uint16_t)((uint8_t *)p_encoded_data)[1])) );
AnnaBridge 143:86740a56073b 356 }
AnnaBridge 143:86740a56073b 357
AnnaBridge 143:86740a56073b 358 /**@brief Function for decoding a three-byte value.
AnnaBridge 143:86740a56073b 359 *
AnnaBridge 143:86740a56073b 360 * @param[in] p_encoded_data Buffer where the encoded data is stored.
AnnaBridge 143:86740a56073b 361 *
AnnaBridge 143:86740a56073b 362 * @return Decoded value (uint32_t).
AnnaBridge 143:86740a56073b 363 */
AnnaBridge 143:86740a56073b 364 static __INLINE uint32_t uint24_decode(const uint8_t * p_encoded_data)
AnnaBridge 143:86740a56073b 365 {
AnnaBridge 143:86740a56073b 366 return ( (((uint32_t)((uint8_t *)p_encoded_data)[0]) << 0) |
AnnaBridge 143:86740a56073b 367 (((uint32_t)((uint8_t *)p_encoded_data)[1]) << 8) |
AnnaBridge 143:86740a56073b 368 (((uint32_t)((uint8_t *)p_encoded_data)[2]) << 16));
AnnaBridge 143:86740a56073b 369 }
AnnaBridge 143:86740a56073b 370
AnnaBridge 143:86740a56073b 371 /**@brief Function for decoding a uint32 value.
AnnaBridge 143:86740a56073b 372 *
AnnaBridge 143:86740a56073b 373 * @param[in] p_encoded_data Buffer where the encoded data is stored.
AnnaBridge 143:86740a56073b 374 *
AnnaBridge 143:86740a56073b 375 * @return Decoded value.
AnnaBridge 143:86740a56073b 376 */
AnnaBridge 143:86740a56073b 377 static __INLINE uint32_t uint32_decode(const uint8_t * p_encoded_data)
AnnaBridge 143:86740a56073b 378 {
AnnaBridge 143:86740a56073b 379 return ( (((uint32_t)((uint8_t *)p_encoded_data)[0]) << 0) |
AnnaBridge 143:86740a56073b 380 (((uint32_t)((uint8_t *)p_encoded_data)[1]) << 8) |
AnnaBridge 143:86740a56073b 381 (((uint32_t)((uint8_t *)p_encoded_data)[2]) << 16) |
AnnaBridge 143:86740a56073b 382 (((uint32_t)((uint8_t *)p_encoded_data)[3]) << 24 ));
AnnaBridge 143:86740a56073b 383 }
AnnaBridge 143:86740a56073b 384
AnnaBridge 143:86740a56073b 385 /**@brief Function for decoding a uint32 value in big-endian format.
AnnaBridge 143:86740a56073b 386 *
AnnaBridge 143:86740a56073b 387 * @param[in] p_encoded_data Buffer where the encoded data is stored.
AnnaBridge 143:86740a56073b 388 *
AnnaBridge 143:86740a56073b 389 * @return Decoded value.
AnnaBridge 143:86740a56073b 390 */
AnnaBridge 143:86740a56073b 391 static __INLINE uint32_t uint32_big_decode(const uint8_t * p_encoded_data)
AnnaBridge 143:86740a56073b 392 {
AnnaBridge 143:86740a56073b 393 return ( (((uint32_t)((uint8_t *)p_encoded_data)[0]) << 24) |
AnnaBridge 143:86740a56073b 394 (((uint32_t)((uint8_t *)p_encoded_data)[1]) << 16) |
AnnaBridge 143:86740a56073b 395 (((uint32_t)((uint8_t *)p_encoded_data)[2]) << 8) |
AnnaBridge 143:86740a56073b 396 (((uint32_t)((uint8_t *)p_encoded_data)[3]) << 0) );
AnnaBridge 143:86740a56073b 397 }
AnnaBridge 143:86740a56073b 398
AnnaBridge 143:86740a56073b 399 /**@brief Function for encoding a uint32 value in big-endian format.
AnnaBridge 143:86740a56073b 400 *
AnnaBridge 143:86740a56073b 401 * @param[in] value Value to be encoded.
AnnaBridge 143:86740a56073b 402 * @param[out] p_encoded_data Buffer where the encoded data will be written.
AnnaBridge 143:86740a56073b 403 *
AnnaBridge 143:86740a56073b 404 * @return Number of bytes written.
AnnaBridge 143:86740a56073b 405 */
AnnaBridge 143:86740a56073b 406 static __INLINE uint8_t uint32_big_encode(uint32_t value, uint8_t * p_encoded_data)
AnnaBridge 143:86740a56073b 407 {
AnnaBridge 143:86740a56073b 408 #ifdef NRF51
AnnaBridge 143:86740a56073b 409 p_encoded_data[0] = (uint8_t) ((value & 0xFF000000) >> 24);
AnnaBridge 143:86740a56073b 410 p_encoded_data[1] = (uint8_t) ((value & 0x00FF0000) >> 16);
AnnaBridge 143:86740a56073b 411 p_encoded_data[2] = (uint8_t) ((value & 0x0000FF00) >> 8);
AnnaBridge 143:86740a56073b 412 p_encoded_data[3] = (uint8_t) ((value & 0x000000FF) >> 0);
AnnaBridge 143:86740a56073b 413 #elif NRF52
AnnaBridge 143:86740a56073b 414 *(uint32_t *)p_encoded_data = __REV(value);
AnnaBridge 143:86740a56073b 415 #endif
AnnaBridge 143:86740a56073b 416 return sizeof(uint32_t);
AnnaBridge 143:86740a56073b 417 }
AnnaBridge 143:86740a56073b 418
AnnaBridge 143:86740a56073b 419 /**@brief Function for decoding a uint48 value.
AnnaBridge 143:86740a56073b 420 *
AnnaBridge 143:86740a56073b 421 * @param[in] p_encoded_data Buffer where the encoded data is stored.
AnnaBridge 143:86740a56073b 422 *
AnnaBridge 143:86740a56073b 423 * @return Decoded value. (uint64_t)
AnnaBridge 143:86740a56073b 424 */
AnnaBridge 143:86740a56073b 425 static __INLINE uint64_t uint48_decode(const uint8_t * p_encoded_data)
AnnaBridge 143:86740a56073b 426 {
AnnaBridge 143:86740a56073b 427 return ( (((uint64_t)((uint8_t *)p_encoded_data)[0]) << 0) |
AnnaBridge 143:86740a56073b 428 (((uint64_t)((uint8_t *)p_encoded_data)[1]) << 8) |
AnnaBridge 143:86740a56073b 429 (((uint64_t)((uint8_t *)p_encoded_data)[2]) << 16) |
AnnaBridge 143:86740a56073b 430 (((uint64_t)((uint8_t *)p_encoded_data)[3]) << 24) |
AnnaBridge 143:86740a56073b 431 (((uint64_t)((uint8_t *)p_encoded_data)[4]) << 32) |
AnnaBridge 143:86740a56073b 432 (((uint64_t)((uint8_t *)p_encoded_data)[5]) << 40 ));
AnnaBridge 143:86740a56073b 433 }
AnnaBridge 143:86740a56073b 434
AnnaBridge 143:86740a56073b 435 /** @brief Function for converting the input voltage (in milli volts) into percentage of 3.0 Volts.
AnnaBridge 143:86740a56073b 436 *
AnnaBridge 143:86740a56073b 437 * @details The calculation is based on a linearized version of the battery's discharge
AnnaBridge 143:86740a56073b 438 * curve. 3.0V returns 100% battery level. The limit for power failure is 2.1V and
AnnaBridge 143:86740a56073b 439 * is considered to be the lower boundary.
AnnaBridge 143:86740a56073b 440 *
AnnaBridge 143:86740a56073b 441 * The discharge curve for CR2032 is non-linear. In this model it is split into
AnnaBridge 143:86740a56073b 442 * 4 linear sections:
AnnaBridge 143:86740a56073b 443 * - Section 1: 3.0V - 2.9V = 100% - 42% (58% drop on 100 mV)
AnnaBridge 143:86740a56073b 444 * - Section 2: 2.9V - 2.74V = 42% - 18% (24% drop on 160 mV)
AnnaBridge 143:86740a56073b 445 * - Section 3: 2.74V - 2.44V = 18% - 6% (12% drop on 300 mV)
AnnaBridge 143:86740a56073b 446 * - Section 4: 2.44V - 2.1V = 6% - 0% (6% drop on 340 mV)
AnnaBridge 143:86740a56073b 447 *
AnnaBridge 143:86740a56073b 448 * These numbers are by no means accurate. Temperature and
AnnaBridge 143:86740a56073b 449 * load in the actual application is not accounted for!
AnnaBridge 143:86740a56073b 450 *
AnnaBridge 143:86740a56073b 451 * @param[in] mvolts The voltage in mV
AnnaBridge 143:86740a56073b 452 *
AnnaBridge 143:86740a56073b 453 * @return Battery level in percent.
AnnaBridge 143:86740a56073b 454 */
AnnaBridge 143:86740a56073b 455 static __INLINE uint8_t battery_level_in_percent(const uint16_t mvolts)
AnnaBridge 143:86740a56073b 456 {
AnnaBridge 143:86740a56073b 457 uint8_t battery_level;
AnnaBridge 143:86740a56073b 458
AnnaBridge 143:86740a56073b 459 if (mvolts >= 3000)
AnnaBridge 143:86740a56073b 460 {
AnnaBridge 143:86740a56073b 461 battery_level = 100;
AnnaBridge 143:86740a56073b 462 }
AnnaBridge 143:86740a56073b 463 else if (mvolts > 2900)
AnnaBridge 143:86740a56073b 464 {
AnnaBridge 143:86740a56073b 465 battery_level = 100 - ((3000 - mvolts) * 58) / 100;
AnnaBridge 143:86740a56073b 466 }
AnnaBridge 143:86740a56073b 467 else if (mvolts > 2740)
AnnaBridge 143:86740a56073b 468 {
AnnaBridge 143:86740a56073b 469 battery_level = 42 - ((2900 - mvolts) * 24) / 160;
AnnaBridge 143:86740a56073b 470 }
AnnaBridge 143:86740a56073b 471 else if (mvolts > 2440)
AnnaBridge 143:86740a56073b 472 {
AnnaBridge 143:86740a56073b 473 battery_level = 18 - ((2740 - mvolts) * 12) / 300;
AnnaBridge 143:86740a56073b 474 }
AnnaBridge 143:86740a56073b 475 else if (mvolts > 2100)
AnnaBridge 143:86740a56073b 476 {
AnnaBridge 143:86740a56073b 477 battery_level = 6 - ((2440 - mvolts) * 6) / 340;
AnnaBridge 143:86740a56073b 478 }
AnnaBridge 143:86740a56073b 479 else
AnnaBridge 143:86740a56073b 480 {
AnnaBridge 143:86740a56073b 481 battery_level = 0;
AnnaBridge 143:86740a56073b 482 }
AnnaBridge 143:86740a56073b 483
AnnaBridge 143:86740a56073b 484 return battery_level;
AnnaBridge 143:86740a56073b 485 }
AnnaBridge 143:86740a56073b 486
AnnaBridge 143:86740a56073b 487 /**@brief Function for checking if a pointer value is aligned to a 4 byte boundary.
AnnaBridge 143:86740a56073b 488 *
AnnaBridge 143:86740a56073b 489 * @param[in] p Pointer value to be checked.
AnnaBridge 143:86740a56073b 490 *
AnnaBridge 143:86740a56073b 491 * @return TRUE if pointer is aligned to a 4 byte boundary, FALSE otherwise.
AnnaBridge 143:86740a56073b 492 */
AnnaBridge 143:86740a56073b 493 static __INLINE bool is_word_aligned(void const* p)
AnnaBridge 143:86740a56073b 494 {
AnnaBridge 143:86740a56073b 495 return (((uintptr_t)p & 0x03) == 0);
AnnaBridge 143:86740a56073b 496 }
AnnaBridge 143:86740a56073b 497
AnnaBridge 143:86740a56073b 498 /**
AnnaBridge 143:86740a56073b 499 * @brief Function for checking if provided address is located in stack space.
AnnaBridge 143:86740a56073b 500 *
AnnaBridge 143:86740a56073b 501 * @param[in] ptr Pointer to be checked.
AnnaBridge 143:86740a56073b 502 *
AnnaBridge 143:86740a56073b 503 * @return true if address is in stack space, false otherwise.
AnnaBridge 143:86740a56073b 504 */
AnnaBridge 143:86740a56073b 505 static __INLINE bool is_address_from_stack(void * ptr)
AnnaBridge 143:86740a56073b 506 {
AnnaBridge 143:86740a56073b 507 if (((uint32_t)ptr >= (uint32_t)STACK_BASE) &&
AnnaBridge 143:86740a56073b 508 ((uint32_t)ptr < (uint32_t)STACK_TOP) )
AnnaBridge 143:86740a56073b 509 {
AnnaBridge 143:86740a56073b 510 return true;
AnnaBridge 143:86740a56073b 511 }
AnnaBridge 143:86740a56073b 512 else
AnnaBridge 143:86740a56073b 513 {
AnnaBridge 143:86740a56073b 514 return false;
AnnaBridge 143:86740a56073b 515 }
AnnaBridge 143:86740a56073b 516 }
AnnaBridge 143:86740a56073b 517
AnnaBridge 143:86740a56073b 518 #endif // APP_UTIL_H__
AnnaBridge 143:86740a56073b 519
AnnaBridge 143:86740a56073b 520 /** @} */