テスト用
Fork of nRF51822 by
nordic/nrf-sdk/app_common/app_util.h@5:b11766b636aa, 2014-05-23 (annotated)
- Committer:
- Rohit Grover
- Date:
- Fri May 23 11:14:45 2014 +0100
- Revision:
- 5:b11766b636aa
- Parent:
- 0:eff01767de02
- Child:
- 37:c29c330d942c
remove un-necessary definition of macro PACKED(TYPE)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 0:eff01767de02 | 1 | /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved. |
bogdanm | 0:eff01767de02 | 2 | * |
bogdanm | 0:eff01767de02 | 3 | * The information contained herein is property of Nordic Semiconductor ASA. |
bogdanm | 0:eff01767de02 | 4 | * Terms and conditions of usage are described in detail in NORDIC |
bogdanm | 0:eff01767de02 | 5 | * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. |
bogdanm | 0:eff01767de02 | 6 | * |
bogdanm | 0:eff01767de02 | 7 | * Licensees are granted free, non-transferable use of the information. NO |
bogdanm | 0:eff01767de02 | 8 | * WARRANTY of ANY KIND is provided. This heading must NOT be removed from |
bogdanm | 0:eff01767de02 | 9 | * the file. |
bogdanm | 0:eff01767de02 | 10 | * |
bogdanm | 0:eff01767de02 | 11 | */ |
bogdanm | 0:eff01767de02 | 12 | |
bogdanm | 0:eff01767de02 | 13 | /** @file |
bogdanm | 0:eff01767de02 | 14 | * |
bogdanm | 0:eff01767de02 | 15 | * @defgroup app_util Utility Functions and Definitions |
bogdanm | 0:eff01767de02 | 16 | * @{ |
bogdanm | 0:eff01767de02 | 17 | * @ingroup app_common |
bogdanm | 0:eff01767de02 | 18 | * |
bogdanm | 0:eff01767de02 | 19 | * @brief Various types and definitions available to all applications. |
bogdanm | 0:eff01767de02 | 20 | */ |
bogdanm | 0:eff01767de02 | 21 | |
bogdanm | 0:eff01767de02 | 22 | #ifndef APP_UTIL_H__ |
bogdanm | 0:eff01767de02 | 23 | #define APP_UTIL_H__ |
bogdanm | 0:eff01767de02 | 24 | |
bogdanm | 0:eff01767de02 | 25 | #include <stdint.h> |
bogdanm | 0:eff01767de02 | 26 | #include "nordic_global.h" |
bogdanm | 0:eff01767de02 | 27 | #include "compiler_abstraction.h" |
bogdanm | 0:eff01767de02 | 28 | #include "nrf51.h" |
bogdanm | 0:eff01767de02 | 29 | #include "app_error.h" |
bogdanm | 0:eff01767de02 | 30 | |
bogdanm | 0:eff01767de02 | 31 | /**@brief The interrupt priorities available to the application while the softdevice is active. */ |
bogdanm | 0:eff01767de02 | 32 | typedef enum |
bogdanm | 0:eff01767de02 | 33 | { |
bogdanm | 0:eff01767de02 | 34 | APP_IRQ_PRIORITY_HIGH = 1, |
bogdanm | 0:eff01767de02 | 35 | APP_IRQ_PRIORITY_LOW = 3 |
bogdanm | 0:eff01767de02 | 36 | } app_irq_priority_t; |
bogdanm | 0:eff01767de02 | 37 | |
bogdanm | 0:eff01767de02 | 38 | enum |
bogdanm | 0:eff01767de02 | 39 | { |
bogdanm | 0:eff01767de02 | 40 | UNIT_0_625_MS = 625, /**< Number of microseconds in 0.625 milliseconds. */ |
bogdanm | 0:eff01767de02 | 41 | UNIT_1_25_MS = 1250, /**< Number of microseconds in 1.25 milliseconds. */ |
bogdanm | 0:eff01767de02 | 42 | UNIT_10_MS = 10000 /**< Number of microseconds in 10 milliseconds. */ |
bogdanm | 0:eff01767de02 | 43 | }; |
bogdanm | 0:eff01767de02 | 44 | |
bogdanm | 0:eff01767de02 | 45 | #define NRF_APP_PRIORITY_THREAD 4 /**< "Interrupt level" when running in Thread Mode. */ |
bogdanm | 0:eff01767de02 | 46 | |
bogdanm | 0:eff01767de02 | 47 | /**@cond NO_DOXYGEN */ |
bogdanm | 0:eff01767de02 | 48 | #define EXTERNAL_INT_VECTOR_OFFSET 16 |
bogdanm | 0:eff01767de02 | 49 | /**@endcond */ |
bogdanm | 0:eff01767de02 | 50 | |
bogdanm | 0:eff01767de02 | 51 | /**@brief Macro for doing static (i.e. compile time) assertion. |
bogdanm | 0:eff01767de02 | 52 | * |
bogdanm | 0:eff01767de02 | 53 | * @note If the assertion fails when compiling using Keil, the compiler will report error message |
bogdanm | 0:eff01767de02 | 54 | * "error: #94: the size of an array must be greater than zero" (while gcc will list the |
bogdanm | 0:eff01767de02 | 55 | * symbol static_assert_failed, making the error message more readable). |
bogdanm | 0:eff01767de02 | 56 | * If the supplied expression can not be evaluated at compile time, Keil will report |
bogdanm | 0:eff01767de02 | 57 | * "error: #28: expression must have a constant value". |
bogdanm | 0:eff01767de02 | 58 | * |
bogdanm | 0:eff01767de02 | 59 | * @note The macro is intentionally implemented not using do while(0), allowing it to be used |
bogdanm | 0:eff01767de02 | 60 | * outside function blocks (e.g. close to global type- and variable declarations). |
bogdanm | 0:eff01767de02 | 61 | * If used in a code block, it must be used before any executable code in this block. |
bogdanm | 0:eff01767de02 | 62 | * |
bogdanm | 0:eff01767de02 | 63 | * @param[in] EXPR Constant expression to be verified. |
bogdanm | 0:eff01767de02 | 64 | */ |
bogdanm | 0:eff01767de02 | 65 | |
bogdanm | 0:eff01767de02 | 66 | #define STATIC_ASSERT(EXPR) typedef char static_assert_failed[(EXPR) ? 1 : -1] |
bogdanm | 0:eff01767de02 | 67 | |
bogdanm | 0:eff01767de02 | 68 | /**@brief type for holding an encoded (i.e. little endian) 16 bit unsigned integer. */ |
bogdanm | 0:eff01767de02 | 69 | typedef uint8_t uint16_le_t[2]; |
bogdanm | 0:eff01767de02 | 70 | |
bogdanm | 0:eff01767de02 | 71 | /**@brief type for holding an encoded (i.e. little endian) 32 bit unsigned integer. */ |
bogdanm | 0:eff01767de02 | 72 | typedef uint8_t uint32_le_t[4]; |
bogdanm | 0:eff01767de02 | 73 | |
bogdanm | 0:eff01767de02 | 74 | /**@brief Byte array type. */ |
bogdanm | 0:eff01767de02 | 75 | typedef struct |
bogdanm | 0:eff01767de02 | 76 | { |
bogdanm | 0:eff01767de02 | 77 | uint16_t size; /**< Number of array entries. */ |
bogdanm | 0:eff01767de02 | 78 | uint8_t * p_data; /**< Pointer to array entries. */ |
bogdanm | 0:eff01767de02 | 79 | } uint8_array_t; |
bogdanm | 0:eff01767de02 | 80 | |
bogdanm | 0:eff01767de02 | 81 | /**@brief Macro for entering a critical region. |
bogdanm | 0:eff01767de02 | 82 | * |
bogdanm | 0:eff01767de02 | 83 | * @note Due to implementation details, there must exist one and only one call to |
bogdanm | 0:eff01767de02 | 84 | * CRITICAL_REGION_EXIT() for each call to CRITICAL_REGION_ENTER(), and they must be located |
bogdanm | 0:eff01767de02 | 85 | * in the same scope. |
bogdanm | 0:eff01767de02 | 86 | */ |
bogdanm | 0:eff01767de02 | 87 | #define CRITICAL_REGION_ENTER() \ |
bogdanm | 0:eff01767de02 | 88 | { \ |
bogdanm | 0:eff01767de02 | 89 | uint8_t IS_NESTED_CRITICAL_REGION = 0; \ |
bogdanm | 0:eff01767de02 | 90 | uint32_t CURRENT_INT_PRI = current_int_priority_get(); \ |
bogdanm | 0:eff01767de02 | 91 | if (CURRENT_INT_PRI != APP_IRQ_PRIORITY_HIGH) \ |
bogdanm | 0:eff01767de02 | 92 | { \ |
bogdanm | 0:eff01767de02 | 93 | uint32_t ERR_CODE = sd_nvic_critical_region_enter(&IS_NESTED_CRITICAL_REGION); \ |
bogdanm | 0:eff01767de02 | 94 | if (ERR_CODE == NRF_ERROR_SOFTDEVICE_NOT_ENABLED) \ |
bogdanm | 0:eff01767de02 | 95 | { \ |
bogdanm | 0:eff01767de02 | 96 | __disable_irq(); \ |
bogdanm | 0:eff01767de02 | 97 | } \ |
bogdanm | 0:eff01767de02 | 98 | else \ |
bogdanm | 0:eff01767de02 | 99 | { \ |
bogdanm | 0:eff01767de02 | 100 | APP_ERROR_CHECK(ERR_CODE); \ |
bogdanm | 0:eff01767de02 | 101 | } \ |
bogdanm | 0:eff01767de02 | 102 | } |
bogdanm | 0:eff01767de02 | 103 | |
bogdanm | 0:eff01767de02 | 104 | /**@brief Macro for leaving a critical region. |
bogdanm | 0:eff01767de02 | 105 | * |
bogdanm | 0:eff01767de02 | 106 | * @note Due to implementation details, there must exist one and only one call to |
bogdanm | 0:eff01767de02 | 107 | * CRITICAL_REGION_EXIT() for each call to CRITICAL_REGION_ENTER(), and they must be located |
bogdanm | 0:eff01767de02 | 108 | * in the same scope. |
bogdanm | 0:eff01767de02 | 109 | */ |
bogdanm | 0:eff01767de02 | 110 | #define CRITICAL_REGION_EXIT() \ |
bogdanm | 0:eff01767de02 | 111 | if (CURRENT_INT_PRI != APP_IRQ_PRIORITY_HIGH) \ |
bogdanm | 0:eff01767de02 | 112 | { \ |
bogdanm | 0:eff01767de02 | 113 | uint32_t ERR_CODE; \ |
bogdanm | 0:eff01767de02 | 114 | __enable_irq(); \ |
bogdanm | 0:eff01767de02 | 115 | ERR_CODE = sd_nvic_critical_region_exit(IS_NESTED_CRITICAL_REGION); \ |
bogdanm | 0:eff01767de02 | 116 | if (ERR_CODE != NRF_ERROR_SOFTDEVICE_NOT_ENABLED) \ |
bogdanm | 0:eff01767de02 | 117 | { \ |
bogdanm | 0:eff01767de02 | 118 | APP_ERROR_CHECK(ERR_CODE); \ |
bogdanm | 0:eff01767de02 | 119 | } \ |
bogdanm | 0:eff01767de02 | 120 | } \ |
bogdanm | 0:eff01767de02 | 121 | } |
bogdanm | 0:eff01767de02 | 122 | |
bogdanm | 0:eff01767de02 | 123 | /**@brief Perform rounded integer division (as opposed to truncating the result). |
bogdanm | 0:eff01767de02 | 124 | * |
bogdanm | 0:eff01767de02 | 125 | * @param[in] A Numerator. |
bogdanm | 0:eff01767de02 | 126 | * @param[in] B Denominator. |
bogdanm | 0:eff01767de02 | 127 | * |
bogdanm | 0:eff01767de02 | 128 | * @return Rounded (integer) result of dividing A by B. |
bogdanm | 0:eff01767de02 | 129 | */ |
bogdanm | 0:eff01767de02 | 130 | #define ROUNDED_DIV(A, B) (((A) + ((B) / 2)) / (B)) |
bogdanm | 0:eff01767de02 | 131 | |
bogdanm | 0:eff01767de02 | 132 | /**@brief Check if the integer provided is a power of two. |
bogdanm | 0:eff01767de02 | 133 | * |
bogdanm | 0:eff01767de02 | 134 | * @param[in] A Number to be tested. |
bogdanm | 0:eff01767de02 | 135 | * |
bogdanm | 0:eff01767de02 | 136 | * @return true if value is power of two. |
bogdanm | 0:eff01767de02 | 137 | * @return false if value not power of two. |
bogdanm | 0:eff01767de02 | 138 | */ |
bogdanm | 0:eff01767de02 | 139 | #define IS_POWER_OF_TWO(A) ( ((A) != 0) && ((((A) - 1) & (A)) == 0) ) |
bogdanm | 0:eff01767de02 | 140 | |
bogdanm | 0:eff01767de02 | 141 | /**@brief To convert ticks to millisecond |
bogdanm | 0:eff01767de02 | 142 | * @param[in] time Number of millseconds that needs to be converted. |
bogdanm | 0:eff01767de02 | 143 | * @param[in] resolution Units to be converted. |
bogdanm | 0:eff01767de02 | 144 | */ |
bogdanm | 0:eff01767de02 | 145 | #define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION)) |
bogdanm | 0:eff01767de02 | 146 | |
bogdanm | 0:eff01767de02 | 147 | |
bogdanm | 0:eff01767de02 | 148 | /**@brief Perform integer division, making sure the result is rounded up. |
bogdanm | 0:eff01767de02 | 149 | * |
bogdanm | 0:eff01767de02 | 150 | * @details One typical use for this is to compute the number of objects with size B is needed to |
bogdanm | 0:eff01767de02 | 151 | * hold A number of bytes. |
bogdanm | 0:eff01767de02 | 152 | * |
bogdanm | 0:eff01767de02 | 153 | * @param[in] A Numerator. |
bogdanm | 0:eff01767de02 | 154 | * @param[in] B Denominator. |
bogdanm | 0:eff01767de02 | 155 | * |
bogdanm | 0:eff01767de02 | 156 | * @return Integer result of dividing A by B, rounded up. |
bogdanm | 0:eff01767de02 | 157 | */ |
bogdanm | 0:eff01767de02 | 158 | #define CEIL_DIV(A, B) \ |
bogdanm | 0:eff01767de02 | 159 | /*lint -save -e573 */ \ |
bogdanm | 0:eff01767de02 | 160 | ((((A) - 1) / (B)) + 1) \ |
bogdanm | 0:eff01767de02 | 161 | /*lint -restore */ |
bogdanm | 0:eff01767de02 | 162 | |
bogdanm | 0:eff01767de02 | 163 | /**@brief Function for encoding a uint16 value. |
bogdanm | 0:eff01767de02 | 164 | * |
bogdanm | 0:eff01767de02 | 165 | * @param[in] value Value to be encoded. |
bogdanm | 0:eff01767de02 | 166 | * @param[out] p_encoded_data Buffer where the encoded data is to be written. |
bogdanm | 0:eff01767de02 | 167 | * |
bogdanm | 0:eff01767de02 | 168 | * @return Number of bytes written. |
bogdanm | 0:eff01767de02 | 169 | */ |
bogdanm | 0:eff01767de02 | 170 | static __INLINE uint8_t uint16_encode(uint16_t value, uint8_t * p_encoded_data) |
bogdanm | 0:eff01767de02 | 171 | { |
bogdanm | 0:eff01767de02 | 172 | p_encoded_data[0] = (uint8_t) ((value & 0x00FF) >> 0); |
bogdanm | 0:eff01767de02 | 173 | p_encoded_data[1] = (uint8_t) ((value & 0xFF00) >> 8); |
bogdanm | 0:eff01767de02 | 174 | return sizeof(uint16_t); |
bogdanm | 0:eff01767de02 | 175 | } |
bogdanm | 0:eff01767de02 | 176 | |
bogdanm | 0:eff01767de02 | 177 | /**@brief Function for encoding a uint32 value. |
bogdanm | 0:eff01767de02 | 178 | * |
bogdanm | 0:eff01767de02 | 179 | * @param[in] value Value to be encoded. |
bogdanm | 0:eff01767de02 | 180 | * @param[out] p_encoded_data Buffer where the encoded data is to be written. |
bogdanm | 0:eff01767de02 | 181 | * |
bogdanm | 0:eff01767de02 | 182 | * @return Number of bytes written. |
bogdanm | 0:eff01767de02 | 183 | */ |
bogdanm | 0:eff01767de02 | 184 | static __INLINE uint8_t uint32_encode(uint32_t value, uint8_t * p_encoded_data) |
bogdanm | 0:eff01767de02 | 185 | { |
bogdanm | 0:eff01767de02 | 186 | p_encoded_data[0] = (uint8_t) ((value & 0x000000FF) >> 0); |
bogdanm | 0:eff01767de02 | 187 | p_encoded_data[1] = (uint8_t) ((value & 0x0000FF00) >> 8); |
bogdanm | 0:eff01767de02 | 188 | p_encoded_data[2] = (uint8_t) ((value & 0x00FF0000) >> 16); |
bogdanm | 0:eff01767de02 | 189 | p_encoded_data[3] = (uint8_t) ((value & 0xFF000000) >> 24); |
bogdanm | 0:eff01767de02 | 190 | return sizeof(uint32_t); |
bogdanm | 0:eff01767de02 | 191 | } |
bogdanm | 0:eff01767de02 | 192 | |
bogdanm | 0:eff01767de02 | 193 | /**@brief Function for decoding a uint16 value. |
bogdanm | 0:eff01767de02 | 194 | * |
bogdanm | 0:eff01767de02 | 195 | * @param[in] p_encoded_data Buffer where the encoded data is stored. |
bogdanm | 0:eff01767de02 | 196 | * |
bogdanm | 0:eff01767de02 | 197 | * @return Decoded value. |
bogdanm | 0:eff01767de02 | 198 | */ |
bogdanm | 0:eff01767de02 | 199 | static __INLINE uint16_t uint16_decode(const uint8_t * p_encoded_data) |
bogdanm | 0:eff01767de02 | 200 | { |
bogdanm | 0:eff01767de02 | 201 | return ( (((uint16_t)((uint8_t *)p_encoded_data)[0])) | |
bogdanm | 0:eff01767de02 | 202 | (((uint16_t)((uint8_t *)p_encoded_data)[1]) << 8 )); |
bogdanm | 0:eff01767de02 | 203 | } |
bogdanm | 0:eff01767de02 | 204 | |
bogdanm | 0:eff01767de02 | 205 | /**@brief Function for decoding a uint32 value. |
bogdanm | 0:eff01767de02 | 206 | * |
bogdanm | 0:eff01767de02 | 207 | * @param[in] p_encoded_data Buffer where the encoded data is stored. |
bogdanm | 0:eff01767de02 | 208 | * |
bogdanm | 0:eff01767de02 | 209 | * @return Decoded value. |
bogdanm | 0:eff01767de02 | 210 | */ |
bogdanm | 0:eff01767de02 | 211 | static __INLINE uint32_t uint32_decode(const uint8_t * p_encoded_data) |
bogdanm | 0:eff01767de02 | 212 | { |
bogdanm | 0:eff01767de02 | 213 | return ( (((uint32_t)((uint8_t *)p_encoded_data)[0]) << 0) | |
bogdanm | 0:eff01767de02 | 214 | (((uint32_t)((uint8_t *)p_encoded_data)[1]) << 8) | |
bogdanm | 0:eff01767de02 | 215 | (((uint32_t)((uint8_t *)p_encoded_data)[2]) << 16) | |
bogdanm | 0:eff01767de02 | 216 | (((uint32_t)((uint8_t *)p_encoded_data)[3]) << 24 )); |
bogdanm | 0:eff01767de02 | 217 | } |
bogdanm | 0:eff01767de02 | 218 | |
bogdanm | 0:eff01767de02 | 219 | |
bogdanm | 0:eff01767de02 | 220 | /**@brief Function for finding the current interrupt level. |
bogdanm | 0:eff01767de02 | 221 | * |
bogdanm | 0:eff01767de02 | 222 | * @return Current interrupt level. |
bogdanm | 0:eff01767de02 | 223 | * @retval APP_IRQ_PRIORITY_HIGH We are running in Application High interrupt level. |
bogdanm | 0:eff01767de02 | 224 | * @retval APP_IRQ_PRIORITY_LOW We are running in Application Low interrupt level. |
bogdanm | 0:eff01767de02 | 225 | * @retval APP_IRQ_PRIORITY_THREAD We are running in Thread Mode. |
bogdanm | 0:eff01767de02 | 226 | */ |
bogdanm | 0:eff01767de02 | 227 | static __INLINE uint8_t current_int_priority_get(void) |
bogdanm | 0:eff01767de02 | 228 | { |
bogdanm | 0:eff01767de02 | 229 | uint32_t isr_vector_num = (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk); |
bogdanm | 0:eff01767de02 | 230 | if (isr_vector_num > 0) |
bogdanm | 0:eff01767de02 | 231 | { |
bogdanm | 0:eff01767de02 | 232 | int32_t irq_type = ((int32_t)isr_vector_num - EXTERNAL_INT_VECTOR_OFFSET); |
bogdanm | 0:eff01767de02 | 233 | return (NVIC_GetPriority((IRQn_Type)irq_type) & 0xFF); |
bogdanm | 0:eff01767de02 | 234 | } |
bogdanm | 0:eff01767de02 | 235 | else |
bogdanm | 0:eff01767de02 | 236 | { |
bogdanm | 0:eff01767de02 | 237 | return NRF_APP_PRIORITY_THREAD; |
bogdanm | 0:eff01767de02 | 238 | } |
bogdanm | 0:eff01767de02 | 239 | } |
bogdanm | 0:eff01767de02 | 240 | |
bogdanm | 0:eff01767de02 | 241 | /** @brief Function for converting the input voltage (in milli volts) into percentage of 3.0 Volts. |
bogdanm | 0:eff01767de02 | 242 | * |
bogdanm | 0:eff01767de02 | 243 | * @details The calculation is based on a linearized version of the battery's discharge |
bogdanm | 0:eff01767de02 | 244 | * curve. 3.0V returns 100% battery level. The limit for power failure is 2.1V and |
bogdanm | 0:eff01767de02 | 245 | * is considered to be the lower boundary. |
bogdanm | 0:eff01767de02 | 246 | * |
bogdanm | 0:eff01767de02 | 247 | * The discharge curve for CR2032 is non-linear. In this model it is split into |
bogdanm | 0:eff01767de02 | 248 | * 4 linear sections: |
bogdanm | 0:eff01767de02 | 249 | * - Section 1: 3.0V - 2.9V = 100% - 42% (58% drop on 100 mV) |
bogdanm | 0:eff01767de02 | 250 | * - Section 2: 2.9V - 2.74V = 42% - 18% (24% drop on 160 mV) |
bogdanm | 0:eff01767de02 | 251 | * - Section 3: 2.74V - 2.44V = 18% - 6% (12% drop on 300 mV) |
bogdanm | 0:eff01767de02 | 252 | * - Section 4: 2.44V - 2.1V = 6% - 0% (6% drop on 340 mV) |
bogdanm | 0:eff01767de02 | 253 | * |
bogdanm | 0:eff01767de02 | 254 | * These numbers are by no means accurate. Temperature and |
bogdanm | 0:eff01767de02 | 255 | * load in the actual application is not accounted for! |
bogdanm | 0:eff01767de02 | 256 | * |
bogdanm | 0:eff01767de02 | 257 | * @param[in] mvolts The voltage in mV |
bogdanm | 0:eff01767de02 | 258 | * |
bogdanm | 0:eff01767de02 | 259 | * @return Battery level in percent. |
bogdanm | 0:eff01767de02 | 260 | */ |
bogdanm | 0:eff01767de02 | 261 | static __INLINE uint8_t battery_level_in_percent(const uint16_t mvolts) |
bogdanm | 0:eff01767de02 | 262 | { |
bogdanm | 0:eff01767de02 | 263 | uint8_t battery_level; |
bogdanm | 0:eff01767de02 | 264 | |
bogdanm | 0:eff01767de02 | 265 | if (mvolts >= 3000) |
bogdanm | 0:eff01767de02 | 266 | { |
bogdanm | 0:eff01767de02 | 267 | battery_level = 100; |
bogdanm | 0:eff01767de02 | 268 | } |
bogdanm | 0:eff01767de02 | 269 | else if (mvolts > 2900) |
bogdanm | 0:eff01767de02 | 270 | { |
bogdanm | 0:eff01767de02 | 271 | battery_level = 100 - ((3000 - mvolts) * 58) / 100; |
bogdanm | 0:eff01767de02 | 272 | } |
bogdanm | 0:eff01767de02 | 273 | else if (mvolts > 2740) |
bogdanm | 0:eff01767de02 | 274 | { |
bogdanm | 0:eff01767de02 | 275 | battery_level = 42 - ((2900 - mvolts) * 24) / 160; |
bogdanm | 0:eff01767de02 | 276 | } |
bogdanm | 0:eff01767de02 | 277 | else if (mvolts > 2440) |
bogdanm | 0:eff01767de02 | 278 | { |
bogdanm | 0:eff01767de02 | 279 | battery_level = 18 - ((2740 - mvolts) * 12) / 300; |
bogdanm | 0:eff01767de02 | 280 | } |
bogdanm | 0:eff01767de02 | 281 | else if (mvolts > 2100) |
bogdanm | 0:eff01767de02 | 282 | { |
bogdanm | 0:eff01767de02 | 283 | battery_level = 6 - ((2440 - mvolts) * 6) / 340; |
bogdanm | 0:eff01767de02 | 284 | } |
bogdanm | 0:eff01767de02 | 285 | else |
bogdanm | 0:eff01767de02 | 286 | { |
bogdanm | 0:eff01767de02 | 287 | battery_level = 0; |
bogdanm | 0:eff01767de02 | 288 | } |
bogdanm | 0:eff01767de02 | 289 | |
bogdanm | 0:eff01767de02 | 290 | return battery_level; |
bogdanm | 0:eff01767de02 | 291 | } |
bogdanm | 0:eff01767de02 | 292 | |
bogdanm | 0:eff01767de02 | 293 | /**@brief Function for checking if a pointer value is aligned to a 4 byte boundary. |
bogdanm | 0:eff01767de02 | 294 | * |
bogdanm | 0:eff01767de02 | 295 | * @param[in] p Pointer value to be checked. |
bogdanm | 0:eff01767de02 | 296 | * |
bogdanm | 0:eff01767de02 | 297 | * @return TRUE if pointer is aligned to a 4 byte boundary, FALSE otherwise. |
bogdanm | 0:eff01767de02 | 298 | */ |
bogdanm | 0:eff01767de02 | 299 | static __INLINE bool is_word_aligned(void * p) |
bogdanm | 0:eff01767de02 | 300 | { |
bogdanm | 0:eff01767de02 | 301 | return (((uint32_t)p & 0x00000003) == 0); |
bogdanm | 0:eff01767de02 | 302 | } |
bogdanm | 0:eff01767de02 | 303 | |
bogdanm | 0:eff01767de02 | 304 | #endif // APP_UTIL_H__ |
bogdanm | 0:eff01767de02 | 305 | |
bogdanm | 0:eff01767de02 | 306 | /** @} */ |