To get started with Seeed Tiny BLE, include detecting motion, button and battery level.

Dependencies:   BLE_API eMPL_MPU6050 mbed nRF51822

Committer:
yihui
Date:
Wed Apr 22 07:47:17 2015 +0000
Revision:
1:fc2f9d636751
update libraries; ; delete nRF51822/nordic-sdk/components/gpiote/app_gpiote.c to solve GPIOTE_IRQHandler multiply defined issue. temperarily change nRF51822 library to folder

Who changed what in which revision?

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