meh

Fork of mbed by mbed official

Committer:
bogdanm
Date:
Fri Sep 12 16:41:52 2014 +0100
Revision:
89:552587b429a1
Release 89 of the mbed library

Main changes:

- low power optimizations for Nordic targets
- code structure changes for Freescale K64F targets
- bug fixes in various backends

Who changed what in which revision?

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