テスト用です。

Dependencies:   mbed

Committer:
jksoft
Date:
Tue Oct 11 11:09:42 2016 +0000
Revision:
0:8468a4403fea
SB??ver;

Who changed what in which revision?

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