1 /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
3 * The information contained herein is property of Nordic Semiconductor ASA.
4 * Terms and conditions of usage are described in detail in NORDIC
5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
7 * Licensees are granted free, non-transferable use of the information. NO
8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
15 * @defgroup app_util Utility Functions and Definitions
19 * @brief Various types and definitions available to all applications.
27 #include "compiler_abstraction.h"
31 UNIT_0_625_MS = 625, /**< Number of microseconds in 0.625 milliseconds. */
32 UNIT_1_25_MS = 1250, /**< Number of microseconds in 1.25 milliseconds. */
33 UNIT_10_MS = 10000 /**< Number of microseconds in 10 milliseconds. */
36 /**@brief Macro for doing static (i.e. compile time) assertion.
38 * @note If the assertion fails when compiling using Keil, the compiler will report error message
39 * "error: #94: the size of an array must be greater than zero" (while gcc will list the
40 * symbol static_assert_failed, making the error message more readable).
41 * If the supplied expression can not be evaluated at compile time, Keil will report
42 * "error: #28: expression must have a constant value".
44 * @note The macro is intentionally implemented not using do while(0), allowing it to be used
45 * outside function blocks (e.g. close to global type- and variable declarations).
46 * If used in a code block, it must be used before any executable code in this block.
48 * @param[in] EXPR Constant expression to be verified.
52 #define STATIC_ASSERT(EXPR) typedef char __attribute__((unused)) static_assert_failed[(EXPR) ? 1 : -1]
53 #elif defined(__ICCARM__)
54 #define STATIC_ASSERT(EXPR) extern char static_assert_failed[(EXPR) ? 1 : -1]
56 #define STATIC_ASSERT(EXPR) typedef char static_assert_failed[(EXPR) ? 1 : -1]
60 /**@brief type for holding an encoded (i.e. little endian) 16 bit unsigned integer. */
61 typedef uint8_t uint16_le_t[2];
63 /**@brief type for holding an encoded (i.e. little endian) 32 bit unsigned integer. */
64 typedef uint8_t uint32_le_t[4];
66 /**@brief Byte array type. */
69 uint16_t size; /**< Number of array entries. */
70 uint8_t * p_data; /**< Pointer to array entries. */
73 /**@brief Perform rounded integer division (as opposed to truncating the result).
75 * @param[in] A Numerator.
76 * @param[in] B Denominator.
78 * @return Rounded (integer) result of dividing A by B.
80 #define ROUNDED_DIV(A, B) (((A) + ((B) / 2)) / (B))
82 /**@brief Check if the integer provided is a power of two.
84 * @param[in] A Number to be tested.
86 * @return true if value is power of two.
87 * @return false if value not power of two.
89 #define IS_POWER_OF_TWO(A) ( ((A) != 0) && ((((A) - 1) & (A)) == 0) )
91 /**@brief To convert milliseconds to ticks.
92 * @param[in] TIME Number of milliseconds to convert.
93 * @param[in] RESOLUTION Unit to be converted to in [us/ticks].
95 #define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION))
98 /**@brief Perform integer division, making sure the result is rounded up.
100 * @details One typical use for this is to compute the number of objects with size B is needed to
101 * hold A number of bytes.
103 * @param[in] A Numerator.
104 * @param[in] B Denominator.
106 * @return Integer result of dividing A by B, rounded up.
108 #define CEIL_DIV(A, B) \
109 /*lint -save -e573 */ \
110 ((((A) - 1) / (B)) + 1) \
113 /**@brief Function for encoding a uint16 value.
115 * @param[in] value Value to be encoded.
116 * @param[out] p_encoded_data Buffer where the encoded data is to be written.
118 * @return Number of bytes written.
120 static __INLINE uint8_t uint16_encode(uint16_t value, uint8_t * p_encoded_data)
122 p_encoded_data[0] = (uint8_t) ((value & 0x00FF) >> 0);
123 p_encoded_data[1] = (uint8_t) ((value & 0xFF00) >> 8);
124 return sizeof(uint16_t);
127 /**@brief Function for encoding a uint32 value.
129 * @param[in] value Value to be encoded.
130 * @param[out] p_encoded_data Buffer where the encoded data is to be written.
132 * @return Number of bytes written.
134 static __INLINE uint8_t uint32_encode(uint32_t value, uint8_t * p_encoded_data)
136 p_encoded_data[0] = (uint8_t) ((value & 0x000000FF) >> 0);
137 p_encoded_data[1] = (uint8_t) ((value & 0x0000FF00) >> 8);
138 p_encoded_data[2] = (uint8_t) ((value & 0x00FF0000) >> 16);
139 p_encoded_data[3] = (uint8_t) ((value & 0xFF000000) >> 24);
140 return sizeof(uint32_t);
143 /**@brief Function for decoding a uint16 value.
145 * @param[in] p_encoded_data Buffer where the encoded data is stored.
147 * @return Decoded value.
149 static __INLINE uint16_t uint16_decode(const uint8_t * p_encoded_data)
151 return ( (((uint16_t)((uint8_t *)p_encoded_data)[0])) |
152 (((uint16_t)((uint8_t *)p_encoded_data)[1]) << 8 ));
155 /**@brief Function for decoding a uint32 value.
157 * @param[in] p_encoded_data Buffer where the encoded data is stored.
159 * @return Decoded value.
161 static __INLINE uint32_t uint32_decode(const uint8_t * p_encoded_data)
163 return ( (((uint32_t)((uint8_t *)p_encoded_data)[0]) << 0) |
164 (((uint32_t)((uint8_t *)p_encoded_data)[1]) << 8) |
165 (((uint32_t)((uint8_t *)p_encoded_data)[2]) << 16) |
166 (((uint32_t)((uint8_t *)p_encoded_data)[3]) << 24 ));
169 /** @brief Function for converting the input voltage (in milli volts) into percentage of 3.0 Volts.
171 * @details The calculation is based on a linearized version of the battery's discharge
172 * curve. 3.0V returns 100% battery level. The limit for power failure is 2.1V and
173 * is considered to be the lower boundary.
175 * The discharge curve for CR2032 is non-linear. In this model it is split into
177 * - Section 1: 3.0V - 2.9V = 100% - 42% (58% drop on 100 mV)
178 * - Section 2: 2.9V - 2.74V = 42% - 18% (24% drop on 160 mV)
179 * - Section 3: 2.74V - 2.44V = 18% - 6% (12% drop on 300 mV)
180 * - Section 4: 2.44V - 2.1V = 6% - 0% (6% drop on 340 mV)
182 * These numbers are by no means accurate. Temperature and
183 * load in the actual application is not accounted for!
185 * @param[in] mvolts The voltage in mV
187 * @return Battery level in percent.
189 static __INLINE uint8_t battery_level_in_percent(const uint16_t mvolts)
191 uint8_t battery_level;
197 else if (mvolts > 2900)
199 battery_level = 100 - ((3000 - mvolts) * 58) / 100;
201 else if (mvolts > 2740)
203 battery_level = 42 - ((2900 - mvolts) * 24) / 160;
205 else if (mvolts > 2440)
207 battery_level = 18 - ((2740 - mvolts) * 12) / 300;
209 else if (mvolts > 2100)
211 battery_level = 6 - ((2440 - mvolts) * 6) / 340;
218 return battery_level;
221 /**@brief Function for checking if a pointer value is aligned to a 4 byte boundary.
223 * @param[in] p Pointer value to be checked.
225 * @return TRUE if pointer is aligned to a 4 byte boundary, FALSE otherwise.
227 static __INLINE bool is_word_aligned(void * p)
229 return (((uintptr_t)p & 0x03) == 0);
232 #endif // APP_UTIL_H__