mbed library sources

Dependents:   Nucleo_blink_led

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Apr 07 06:45:07 2015 +0100
Revision:
501:36015dec7d16
Synchronized with git revision 40d3a79298f37284b863f90e33e261986340964e

Full URL: https://github.com/mbedmicro/mbed/commit/40d3a79298f37284b863f90e33e261986340964e/

fixes #984: updating to v7.1 of the Nordic SDK.

Who changed what in which revision?

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