BLE temperature profile using digital DS1820 or analog LM35 sensors
Embed:
(wiki syntax)
Show/hide line numbers
app_util.h
Go to the documentation of this file.
00001 /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved. 00002 * 00003 * The information contained herein is property of Nordic Semiconductor ASA. 00004 * Terms and conditions of usage are described in detail in NORDIC 00005 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. 00006 * 00007 * Licensees are granted free, non-transferable use of the information. NO 00008 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from 00009 * the file. 00010 * 00011 */ 00012 00013 /** @file 00014 * 00015 * @defgroup app_util Utility Functions and Definitions 00016 * @{ 00017 * @ingroup app_common 00018 * 00019 * @brief Various types and definitions available to all applications. 00020 */ 00021 00022 #ifndef APP_UTIL_H__ 00023 #define APP_UTIL_H__ 00024 00025 #include <stdint.h> 00026 #include "nordic_global.h" 00027 #include "compiler_abstraction.h" 00028 #include "nrf51.h" 00029 #include "app_error.h " 00030 00031 /**@brief The interrupt priorities available to the application while the softdevice is active. */ 00032 typedef enum 00033 { 00034 APP_IRQ_PRIORITY_HIGH = 1, 00035 APP_IRQ_PRIORITY_LOW = 3 00036 } app_irq_priority_t; 00037 00038 enum 00039 { 00040 UNIT_0_625_MS = 625, /**< Number of microseconds in 0.625 milliseconds. */ 00041 UNIT_1_25_MS = 1250, /**< Number of microseconds in 1.25 milliseconds. */ 00042 UNIT_10_MS = 10000 /**< Number of microseconds in 10 milliseconds. */ 00043 }; 00044 00045 #define NRF_APP_PRIORITY_THREAD 4 /**< "Interrupt level" when running in Thread Mode. */ 00046 00047 /**@cond NO_DOXYGEN */ 00048 #define EXTERNAL_INT_VECTOR_OFFSET 16 00049 /**@endcond */ 00050 00051 #define PACKED(TYPE) __packed TYPE 00052 00053 /**@brief Macro for doing static (i.e. compile time) assertion. 00054 * 00055 * @note If the assertion fails when compiling using Keil, the compiler will report error message 00056 * "error: #94: the size of an array must be greater than zero" (while gcc will list the 00057 * symbol static_assert_failed, making the error message more readable). 00058 * If the supplied expression can not be evaluated at compile time, Keil will report 00059 * "error: #28: expression must have a constant value". 00060 * 00061 * @note The macro is intentionally implemented not using do while(0), allowing it to be used 00062 * outside function blocks (e.g. close to global type- and variable declarations). 00063 * If used in a code block, it must be used before any executable code in this block. 00064 * 00065 * @param[in] EXPR Constant expression to be verified. 00066 */ 00067 00068 #define STATIC_ASSERT(EXPR) typedef char static_assert_failed[(EXPR) ? 1 : -1] 00069 00070 /**@brief type for holding an encoded (i.e. little endian) 16 bit unsigned integer. */ 00071 typedef uint8_t uint16_le_t[2]; 00072 00073 /**@brief type for holding an encoded (i.e. little endian) 32 bit unsigned integer. */ 00074 typedef uint8_t uint32_le_t[4]; 00075 00076 /**@brief Byte array type. */ 00077 typedef struct 00078 { 00079 uint16_t size; /**< Number of array entries. */ 00080 uint8_t * p_data; /**< Pointer to array entries. */ 00081 } uint8_array_t; 00082 00083 /**@brief Macro for entering a critical region. 00084 * 00085 * @note Due to implementation details, there must exist one and only one call to 00086 * CRITICAL_REGION_EXIT() for each call to CRITICAL_REGION_ENTER(), and they must be located 00087 * in the same scope. 00088 */ 00089 #define CRITICAL_REGION_ENTER() \ 00090 { \ 00091 uint8_t IS_NESTED_CRITICAL_REGION = 0; \ 00092 uint32_t CURRENT_INT_PRI = current_int_priority_get(); \ 00093 if (CURRENT_INT_PRI != APP_IRQ_PRIORITY_HIGH) \ 00094 { \ 00095 uint32_t ERR_CODE = sd_nvic_critical_region_enter(&IS_NESTED_CRITICAL_REGION); \ 00096 if (ERR_CODE == NRF_ERROR_SOFTDEVICE_NOT_ENABLED) \ 00097 { \ 00098 __disable_irq(); \ 00099 } \ 00100 else \ 00101 { \ 00102 APP_ERROR_CHECK(ERR_CODE); \ 00103 } \ 00104 } 00105 00106 /**@brief Macro for leaving a critical region. 00107 * 00108 * @note Due to implementation details, there must exist one and only one call to 00109 * CRITICAL_REGION_EXIT() for each call to CRITICAL_REGION_ENTER(), and they must be located 00110 * in the same scope. 00111 */ 00112 #define CRITICAL_REGION_EXIT() \ 00113 if (CURRENT_INT_PRI != APP_IRQ_PRIORITY_HIGH) \ 00114 { \ 00115 uint32_t ERR_CODE; \ 00116 __enable_irq(); \ 00117 ERR_CODE = sd_nvic_critical_region_exit(IS_NESTED_CRITICAL_REGION); \ 00118 if (ERR_CODE != NRF_ERROR_SOFTDEVICE_NOT_ENABLED) \ 00119 { \ 00120 APP_ERROR_CHECK(ERR_CODE); \ 00121 } \ 00122 } \ 00123 } 00124 00125 /**@brief Perform rounded integer division (as opposed to truncating the result). 00126 * 00127 * @param[in] A Numerator. 00128 * @param[in] B Denominator. 00129 * 00130 * @return Rounded (integer) result of dividing A by B. 00131 */ 00132 #define ROUNDED_DIV(A, B) (((A) + ((B) / 2)) / (B)) 00133 00134 /**@brief Check if the integer provided is a power of two. 00135 * 00136 * @param[in] A Number to be tested. 00137 * 00138 * @return true if value is power of two. 00139 * @return false if value not power of two. 00140 */ 00141 #define IS_POWER_OF_TWO(A) ( ((A) != 0) && ((((A) - 1) & (A)) == 0) ) 00142 00143 /**@brief To convert ticks to millisecond 00144 * @param[in] time Number of millseconds that needs to be converted. 00145 * @param[in] resolution Units to be converted. 00146 */ 00147 #define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION)) 00148 00149 00150 /**@brief Perform integer division, making sure the result is rounded up. 00151 * 00152 * @details One typical use for this is to compute the number of objects with size B is needed to 00153 * hold A number of bytes. 00154 * 00155 * @param[in] A Numerator. 00156 * @param[in] B Denominator. 00157 * 00158 * @return Integer result of dividing A by B, rounded up. 00159 */ 00160 #define CEIL_DIV(A, B) \ 00161 /*lint -save -e573 */ \ 00162 ((((A) - 1) / (B)) + 1) \ 00163 /*lint -restore */ 00164 00165 /**@brief Function for encoding a uint16 value. 00166 * 00167 * @param[in] value Value to be encoded. 00168 * @param[out] p_encoded_data Buffer where the encoded data is to be written. 00169 * 00170 * @return Number of bytes written. 00171 */ 00172 static __INLINE uint8_t uint16_encode(uint16_t value, uint8_t * p_encoded_data) 00173 { 00174 p_encoded_data[0] = (uint8_t) ((value & 0x00FF) >> 0); 00175 p_encoded_data[1] = (uint8_t) ((value & 0xFF00) >> 8); 00176 return sizeof(uint16_t); 00177 } 00178 00179 /**@brief Function for encoding a uint32 value. 00180 * 00181 * @param[in] value Value to be encoded. 00182 * @param[out] p_encoded_data Buffer where the encoded data is to be written. 00183 * 00184 * @return Number of bytes written. 00185 */ 00186 static __INLINE uint8_t uint32_encode(uint32_t value, uint8_t * p_encoded_data) 00187 { 00188 p_encoded_data[0] = (uint8_t) ((value & 0x000000FF) >> 0); 00189 p_encoded_data[1] = (uint8_t) ((value & 0x0000FF00) >> 8); 00190 p_encoded_data[2] = (uint8_t) ((value & 0x00FF0000) >> 16); 00191 p_encoded_data[3] = (uint8_t) ((value & 0xFF000000) >> 24); 00192 return sizeof(uint32_t); 00193 } 00194 00195 /**@brief Function for decoding a uint16 value. 00196 * 00197 * @param[in] p_encoded_data Buffer where the encoded data is stored. 00198 * 00199 * @return Decoded value. 00200 */ 00201 static __INLINE uint16_t uint16_decode(const uint8_t * p_encoded_data) 00202 { 00203 return ( (((uint16_t)((uint8_t *)p_encoded_data)[0])) | 00204 (((uint16_t)((uint8_t *)p_encoded_data)[1]) << 8 )); 00205 } 00206 00207 /**@brief Function for decoding a uint32 value. 00208 * 00209 * @param[in] p_encoded_data Buffer where the encoded data is stored. 00210 * 00211 * @return Decoded value. 00212 */ 00213 static __INLINE uint32_t uint32_decode(const uint8_t * p_encoded_data) 00214 { 00215 return ( (((uint32_t)((uint8_t *)p_encoded_data)[0]) << 0) | 00216 (((uint32_t)((uint8_t *)p_encoded_data)[1]) << 8) | 00217 (((uint32_t)((uint8_t *)p_encoded_data)[2]) << 16) | 00218 (((uint32_t)((uint8_t *)p_encoded_data)[3]) << 24 )); 00219 } 00220 00221 00222 /**@brief Function for finding the current interrupt level. 00223 * 00224 * @return Current interrupt level. 00225 * @retval APP_IRQ_PRIORITY_HIGH We are running in Application High interrupt level. 00226 * @retval APP_IRQ_PRIORITY_LOW We are running in Application Low interrupt level. 00227 * @retval APP_IRQ_PRIORITY_THREAD We are running in Thread Mode. 00228 */ 00229 static __INLINE uint8_t current_int_priority_get(void) 00230 { 00231 uint32_t isr_vector_num = (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk); 00232 if (isr_vector_num > 0) 00233 { 00234 int32_t irq_type = ((int32_t)isr_vector_num - EXTERNAL_INT_VECTOR_OFFSET); 00235 return (NVIC_GetPriority((IRQn_Type)irq_type) & 0xFF); 00236 } 00237 else 00238 { 00239 return NRF_APP_PRIORITY_THREAD; 00240 } 00241 } 00242 00243 /** @brief Function for converting the input voltage (in milli volts) into percentage of 3.0 Volts. 00244 * 00245 * @details The calculation is based on a linearized version of the battery's discharge 00246 * curve. 3.0V returns 100% battery level. The limit for power failure is 2.1V and 00247 * is considered to be the lower boundary. 00248 * 00249 * The discharge curve for CR2032 is non-linear. In this model it is split into 00250 * 4 linear sections: 00251 * - Section 1: 3.0V - 2.9V = 100% - 42% (58% drop on 100 mV) 00252 * - Section 2: 2.9V - 2.74V = 42% - 18% (24% drop on 160 mV) 00253 * - Section 3: 2.74V - 2.44V = 18% - 6% (12% drop on 300 mV) 00254 * - Section 4: 2.44V - 2.1V = 6% - 0% (6% drop on 340 mV) 00255 * 00256 * These numbers are by no means accurate. Temperature and 00257 * load in the actual application is not accounted for! 00258 * 00259 * @param[in] mvolts The voltage in mV 00260 * 00261 * @return Battery level in percent. 00262 */ 00263 static __INLINE uint8_t battery_level_in_percent(const uint16_t mvolts) 00264 { 00265 uint8_t battery_level; 00266 00267 if (mvolts >= 3000) 00268 { 00269 battery_level = 100; 00270 } 00271 else if (mvolts > 2900) 00272 { 00273 battery_level = 100 - ((3000 - mvolts) * 58) / 100; 00274 } 00275 else if (mvolts > 2740) 00276 { 00277 battery_level = 42 - ((2900 - mvolts) * 24) / 160; 00278 } 00279 else if (mvolts > 2440) 00280 { 00281 battery_level = 18 - ((2740 - mvolts) * 12) / 300; 00282 } 00283 else if (mvolts > 2100) 00284 { 00285 battery_level = 6 - ((2440 - mvolts) * 6) / 340; 00286 } 00287 else 00288 { 00289 battery_level = 0; 00290 } 00291 00292 return battery_level; 00293 } 00294 00295 /**@brief Function for checking if a pointer value is aligned to a 4 byte boundary. 00296 * 00297 * @param[in] p Pointer value to be checked. 00298 * 00299 * @return TRUE if pointer is aligned to a 4 byte boundary, FALSE otherwise. 00300 */ 00301 static __INLINE bool is_word_aligned(void * p) 00302 { 00303 return (((uint32_t)p & 0x00000003) == 0); 00304 } 00305 00306 #endif // APP_UTIL_H__ 00307 00308 /** @} */
Generated on Tue Jul 12 2022 15:07:11 by 1.7.2