app_util.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /* Copyright (c) 2012 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. /** @file
  13. *
  14. * @defgroup app_util Utility Functions and Definitions
  15. * @{
  16. * @ingroup app_common
  17. *
  18. * @brief Various types and definitions available to all applications.
  19. */
  20. #ifndef APP_UTIL_H__
  21. #define APP_UTIL_H__
  22. #include <stdint.h>
  23. #include <stdbool.h>
  24. #include "compiler_abstraction.h"
  25. #include "nrf.h"
  26. //lint -save -e27 -e10 -e19
  27. #if defined ( __CC_ARM )
  28. extern char STACK$$Base;
  29. extern char STACK$$Length;
  30. #define STACK_BASE &STACK$$Base
  31. #define STACK_TOP ((void*)((uint32_t)STACK_BASE + (uint32_t)&STACK$$Length))
  32. #elif defined ( __ICCARM__ )
  33. extern char CSTACK$$Base;
  34. extern char CSTACK$$Length;
  35. #define STACK_BASE &CSTACK$$Base
  36. #define STACK_TOP ((void*)((uint32_t)STACK_BASE + (uint32_t)&CSTACK$$Length))
  37. #elif defined ( __GNUC__ )
  38. extern uint32_t __StackTop;
  39. extern uint32_t __StackLimit;
  40. #define STACK_BASE &__StackLimit
  41. #define STACK_TOP &__StackTop
  42. #endif
  43. //lint -restore
  44. enum
  45. {
  46. UNIT_0_625_MS = 625, /**< Number of microseconds in 0.625 milliseconds. */
  47. UNIT_1_25_MS = 1250, /**< Number of microseconds in 1.25 milliseconds. */
  48. UNIT_10_MS = 10000 /**< Number of microseconds in 10 milliseconds. */
  49. };
  50. /**@brief Implementation specific macro for delayed macro expansion used in string concatenation
  51. *
  52. * @param[in] lhs Left hand side in concatenation
  53. * @param[in] rhs Right hand side in concatenation
  54. */
  55. #define STRING_CONCATENATE_IMPL(lhs, rhs) lhs ## rhs
  56. /**@brief Macro used to concatenate string using delayed macro expansion
  57. *
  58. * @note This macro will delay concatenation until the expressions have been resolved
  59. *
  60. * @param[in] lhs Left hand side in concatenation
  61. * @param[in] rhs Right hand side in concatenation
  62. */
  63. #define STRING_CONCATENATE(lhs, rhs) STRING_CONCATENATE_IMPL(lhs, rhs)
  64. // Disable lint-warnings/errors for STATIC_ASSERT
  65. //lint --emacro(10,STATIC_ASSERT)
  66. //lint --emacro(18,STATIC_ASSERT)
  67. //lint --emacro(19,STATIC_ASSERT)
  68. //lint --emacro(30,STATIC_ASSERT)
  69. //lint --emacro(37,STATIC_ASSERT)
  70. //lint --emacro(42,STATIC_ASSERT)
  71. //lint --emacro(26,STATIC_ASSERT)
  72. //lint --emacro(102,STATIC_ASSERT)
  73. //lint --emacro(533,STATIC_ASSERT)
  74. //lint --emacro(534,STATIC_ASSERT)
  75. //lint --emacro(132,STATIC_ASSERT)
  76. //lint --emacro(414,STATIC_ASSERT)
  77. //lint --emacro(578,STATIC_ASSERT)
  78. //lint --emacro(628,STATIC_ASSERT)
  79. //lint --emacro(648,STATIC_ASSERT)
  80. //lint --emacro(830,STATIC_ASSERT)
  81. /**@brief Macro for doing static (i.e. compile time) assertion.
  82. *
  83. * @note If the EXPR isn't resolvable, then the error message won't be shown.
  84. *
  85. * @note The output of STATIC_ASSERT will be different across different compilers.
  86. *
  87. * @param[in] EXPR Constant expression to be verified.
  88. */
  89. #if defined ( __COUNTER__ )
  90. #define STATIC_ASSERT(EXPR) \
  91. ;enum { STRING_CONCATENATE(static_assert_, __COUNTER__) = 1/(!!(EXPR)) }
  92. #else
  93. #define STATIC_ASSERT(EXPR) \
  94. ;enum { STRING_CONCATENATE(assert_line_, __LINE__) = 1/(!!(EXPR)) }
  95. #endif
  96. /**@brief Implementation details for NUM_VAR_ARGS */
  97. #define NUM_VA_ARGS_IMPL( \
  98. _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
  99. _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
  100. _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \
  101. _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \
  102. _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \
  103. _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, \
  104. _61, _62, N, ...) N
  105. /**@brief Macro to get the number of arguments in a call variadic macro call
  106. *
  107. * param[in] ... List of arguments
  108. *
  109. * @retval Number of variadic arguments in the argument list
  110. */
  111. #define NUM_VA_ARGS(...) NUM_VA_ARGS_IMPL(__VA_ARGS__, 63, 62, 61, \
  112. 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, \
  113. 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, \
  114. 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, \
  115. 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, \
  116. 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \
  117. 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
  118. /**@brief type for holding an encoded (i.e. little endian) 16 bit unsigned integer. */
  119. typedef uint8_t uint16_le_t[2];
  120. /**@brief Type for holding an encoded (i.e. little endian) 32 bit unsigned integer. */
  121. typedef uint8_t uint32_le_t[4];
  122. /**@brief Byte array type. */
  123. typedef struct
  124. {
  125. uint16_t size; /**< Number of array entries. */
  126. uint8_t * p_data; /**< Pointer to array entries. */
  127. } uint8_array_t;
  128. /**@brief Macro for performing rounded integer division (as opposed to truncating the result).
  129. *
  130. * @param[in] A Numerator.
  131. * @param[in] B Denominator.
  132. *
  133. * @return Rounded (integer) result of dividing A by B.
  134. */
  135. #define ROUNDED_DIV(A, B) (((A) + ((B) / 2)) / (B))
  136. /**@brief Macro for checking if an integer is a power of two.
  137. *
  138. * @param[in] A Number to be tested.
  139. *
  140. * @return true if value is power of two.
  141. * @return false if value not power of two.
  142. */
  143. #define IS_POWER_OF_TWO(A) ( ((A) != 0) && ((((A) - 1) & (A)) == 0) )
  144. /**@brief Macro for converting milliseconds to ticks.
  145. *
  146. * @param[in] TIME Number of milliseconds to convert.
  147. * @param[in] RESOLUTION Unit to be converted to in [us/ticks].
  148. */
  149. #define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION))
  150. /**@brief Macro for performing integer division, making sure the result is rounded up.
  151. *
  152. * @details One typical use for this is to compute the number of objects with size B is needed to
  153. * hold A number of bytes.
  154. *
  155. * @param[in] A Numerator.
  156. * @param[in] B Denominator.
  157. *
  158. * @return Integer result of dividing A by B, rounded up.
  159. */
  160. #define CEIL_DIV(A, B) \
  161. (((A) + (B) - 1) / (B))
  162. /**@brief Macro for creating a buffer aligned to 4 bytes.
  163. *
  164. * @param[in] NAME Name of the buffor.
  165. * @param[in] MIN_SIZE Size of this buffor (it will be rounded up to multiples of 4 bytes).
  166. */
  167. #define WORD_ALIGNED_MEM_BUFF(NAME, MIN_SIZE) static uint32_t NAME[CEIL_DIV(MIN_SIZE, sizeof(uint32_t))]
  168. /**@brief Macro for calculating the number of words that are needed to hold a number of bytes.
  169. *
  170. * @details Adds 3 and divides by 4.
  171. *
  172. * @param[in] n_bytes The number of bytes.
  173. *
  174. * @return The number of words that @p n_bytes take up (rounded up).
  175. */
  176. #define BYTES_TO_WORDS(n_bytes) (((n_bytes) + 3) >> 2)
  177. /**@brief The number of bytes in a word.
  178. */
  179. #define BYTES_PER_WORD (4)
  180. /**@brief Macro for increasing a number to the nearest (larger) multiple of another number.
  181. *
  182. * @param[in] alignment The number to align to.
  183. * @param[in] number The number to align (increase).
  184. *
  185. * @return The aligned (increased) @p number.
  186. */
  187. #define ALIGN_NUM(alignment, number) ((number - 1) + alignment - ((number - 1) % alignment))
  188. /**@brief Function for changing the value unit.
  189. *
  190. * @param[in] value Value to be rescaled.
  191. * @param[in] old_unit_reversal Reversal of the incoming unit.
  192. * @param[in] new_unit_reversal Reversal of the desired unit.
  193. *
  194. * @return Number of bytes written.
  195. */
  196. static __INLINE uint64_t value_rescale(uint32_t value, uint32_t old_unit_reversal, uint16_t new_unit_reversal)
  197. {
  198. return (uint64_t)ROUNDED_DIV((uint64_t)value * new_unit_reversal, old_unit_reversal);
  199. }
  200. /**@brief Function for encoding a uint16 value.
  201. *
  202. * @param[in] value Value to be encoded.
  203. * @param[out] p_encoded_data Buffer where the encoded data is to be written.
  204. *
  205. * @return Number of bytes written.
  206. */
  207. static __INLINE uint8_t uint16_encode(uint16_t value, uint8_t * p_encoded_data)
  208. {
  209. p_encoded_data[0] = (uint8_t) ((value & 0x00FF) >> 0);
  210. p_encoded_data[1] = (uint8_t) ((value & 0xFF00) >> 8);
  211. return sizeof(uint16_t);
  212. }
  213. /**@brief Function for encoding a three-byte value.
  214. *
  215. * @param[in] value Value to be encoded.
  216. * @param[out] p_encoded_data Buffer where the encoded data is to be written.
  217. *
  218. * @return Number of bytes written.
  219. */
  220. static __INLINE uint8_t uint24_encode(uint32_t value, uint8_t * p_encoded_data)
  221. {
  222. p_encoded_data[0] = (uint8_t) ((value & 0x000000FF) >> 0);
  223. p_encoded_data[1] = (uint8_t) ((value & 0x0000FF00) >> 8);
  224. p_encoded_data[2] = (uint8_t) ((value & 0x00FF0000) >> 16);
  225. return 3;
  226. }
  227. /**@brief Function for encoding a uint32 value.
  228. *
  229. * @param[in] value Value to be encoded.
  230. * @param[out] p_encoded_data Buffer where the encoded data is to be written.
  231. *
  232. * @return Number of bytes written.
  233. */
  234. static __INLINE uint8_t uint32_encode(uint32_t value, uint8_t * p_encoded_data)
  235. {
  236. p_encoded_data[0] = (uint8_t) ((value & 0x000000FF) >> 0);
  237. p_encoded_data[1] = (uint8_t) ((value & 0x0000FF00) >> 8);
  238. p_encoded_data[2] = (uint8_t) ((value & 0x00FF0000) >> 16);
  239. p_encoded_data[3] = (uint8_t) ((value & 0xFF000000) >> 24);
  240. return sizeof(uint32_t);
  241. }
  242. /**@brief Function for encoding a uint48 value.
  243. *
  244. * @param[in] value Value to be encoded.
  245. * @param[out] p_encoded_data Buffer where the encoded data is to be written.
  246. *
  247. * @return Number of bytes written.
  248. */
  249. static __INLINE uint8_t uint48_encode(uint64_t value, uint8_t * p_encoded_data)
  250. {
  251. p_encoded_data[0] = (uint8_t) ((value & 0x0000000000FF) >> 0);
  252. p_encoded_data[1] = (uint8_t) ((value & 0x00000000FF00) >> 8);
  253. p_encoded_data[2] = (uint8_t) ((value & 0x000000FF0000) >> 16);
  254. p_encoded_data[3] = (uint8_t) ((value & 0x0000FF000000) >> 24);
  255. p_encoded_data[4] = (uint8_t) ((value & 0x00FF00000000) >> 32);
  256. p_encoded_data[5] = (uint8_t) ((value & 0xFF0000000000) >> 40);
  257. return 6;
  258. }
  259. /**@brief Function for decoding a uint16 value.
  260. *
  261. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  262. *
  263. * @return Decoded value.
  264. */
  265. static __INLINE uint16_t uint16_decode(const uint8_t * p_encoded_data)
  266. {
  267. return ( (((uint16_t)((uint8_t *)p_encoded_data)[0])) |
  268. (((uint16_t)((uint8_t *)p_encoded_data)[1]) << 8 ));
  269. }
  270. /**@brief Function for decoding a uint16 value in big-endian format.
  271. *
  272. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  273. *
  274. * @return Decoded value.
  275. */
  276. static __INLINE uint16_t uint16_big_decode(const uint8_t * p_encoded_data)
  277. {
  278. return ( (((uint16_t)((uint8_t *)p_encoded_data)[0]) << 8 ) |
  279. (((uint16_t)((uint8_t *)p_encoded_data)[1])) );
  280. }
  281. /**@brief Function for decoding a three-byte value.
  282. *
  283. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  284. *
  285. * @return Decoded value (uint32_t).
  286. */
  287. static __INLINE uint32_t uint24_decode(const uint8_t * p_encoded_data)
  288. {
  289. return ( (((uint32_t)((uint8_t *)p_encoded_data)[0]) << 0) |
  290. (((uint32_t)((uint8_t *)p_encoded_data)[1]) << 8) |
  291. (((uint32_t)((uint8_t *)p_encoded_data)[2]) << 16));
  292. }
  293. /**@brief Function for decoding a uint32 value.
  294. *
  295. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  296. *
  297. * @return Decoded value.
  298. */
  299. static __INLINE uint32_t uint32_decode(const uint8_t * p_encoded_data)
  300. {
  301. return ( (((uint32_t)((uint8_t *)p_encoded_data)[0]) << 0) |
  302. (((uint32_t)((uint8_t *)p_encoded_data)[1]) << 8) |
  303. (((uint32_t)((uint8_t *)p_encoded_data)[2]) << 16) |
  304. (((uint32_t)((uint8_t *)p_encoded_data)[3]) << 24 ));
  305. }
  306. /**@brief Function for decoding a uint32 value in big-endian format.
  307. *
  308. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  309. *
  310. * @return Decoded value.
  311. */
  312. static __INLINE uint32_t uint32_big_decode(const uint8_t * p_encoded_data)
  313. {
  314. return ( (((uint32_t)((uint8_t *)p_encoded_data)[0]) << 24) |
  315. (((uint32_t)((uint8_t *)p_encoded_data)[1]) << 16) |
  316. (((uint32_t)((uint8_t *)p_encoded_data)[2]) << 8) |
  317. (((uint32_t)((uint8_t *)p_encoded_data)[3]) << 0) );
  318. }
  319. /**@brief Function for encoding a uint32 value in big-endian format.
  320. *
  321. * @param[in] value Value to be encoded.
  322. * @param[out] p_encoded_data Buffer where the encoded data will be written.
  323. *
  324. * @return Number of bytes written.
  325. */
  326. static __INLINE uint8_t uint32_big_encode(uint32_t value, uint8_t * p_encoded_data)
  327. {
  328. #ifdef NRF51
  329. p_encoded_data[0] = (uint8_t) ((value & 0xFF000000) >> 24);
  330. p_encoded_data[1] = (uint8_t) ((value & 0x00FF0000) >> 16);
  331. p_encoded_data[2] = (uint8_t) ((value & 0x0000FF00) >> 8);
  332. p_encoded_data[3] = (uint8_t) ((value & 0x000000FF) >> 0);
  333. #elif NRF52
  334. *(uint32_t *)p_encoded_data = __REV(value);
  335. #endif
  336. return sizeof(uint32_t);
  337. }
  338. /**@brief Function for decoding a uint48 value.
  339. *
  340. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  341. *
  342. * @return Decoded value. (uint64_t)
  343. */
  344. static __INLINE uint64_t uint48_decode(const uint8_t * p_encoded_data)
  345. {
  346. return ( (((uint64_t)((uint8_t *)p_encoded_data)[0]) << 0) |
  347. (((uint64_t)((uint8_t *)p_encoded_data)[1]) << 8) |
  348. (((uint64_t)((uint8_t *)p_encoded_data)[2]) << 16) |
  349. (((uint64_t)((uint8_t *)p_encoded_data)[3]) << 24) |
  350. (((uint64_t)((uint8_t *)p_encoded_data)[4]) << 32) |
  351. (((uint64_t)((uint8_t *)p_encoded_data)[5]) << 40 ));
  352. }
  353. /** @brief Function for converting the input voltage (in milli volts) into percentage of 3.0 Volts.
  354. *
  355. * @details The calculation is based on a linearized version of the battery's discharge
  356. * curve. 3.0V returns 100% battery level. The limit for power failure is 2.1V and
  357. * is considered to be the lower boundary.
  358. *
  359. * The discharge curve for CR2032 is non-linear. In this model it is split into
  360. * 4 linear sections:
  361. * - Section 1: 3.0V - 2.9V = 100% - 42% (58% drop on 100 mV)
  362. * - Section 2: 2.9V - 2.74V = 42% - 18% (24% drop on 160 mV)
  363. * - Section 3: 2.74V - 2.44V = 18% - 6% (12% drop on 300 mV)
  364. * - Section 4: 2.44V - 2.1V = 6% - 0% (6% drop on 340 mV)
  365. *
  366. * These numbers are by no means accurate. Temperature and
  367. * load in the actual application is not accounted for!
  368. *
  369. * @param[in] mvolts The voltage in mV
  370. *
  371. * @return Battery level in percent.
  372. */
  373. static __INLINE uint8_t battery_level_in_percent(const uint16_t mvolts)
  374. {
  375. uint8_t battery_level;
  376. if (mvolts >= 3000)
  377. {
  378. battery_level = 100;
  379. }
  380. else if (mvolts > 2900)
  381. {
  382. battery_level = 100 - ((3000 - mvolts) * 58) / 100;
  383. }
  384. else if (mvolts > 2740)
  385. {
  386. battery_level = 42 - ((2900 - mvolts) * 24) / 160;
  387. }
  388. else if (mvolts > 2440)
  389. {
  390. battery_level = 18 - ((2740 - mvolts) * 12) / 300;
  391. }
  392. else if (mvolts > 2100)
  393. {
  394. battery_level = 6 - ((2440 - mvolts) * 6) / 340;
  395. }
  396. else
  397. {
  398. battery_level = 0;
  399. }
  400. return battery_level;
  401. }
  402. /**@brief Function for checking if a pointer value is aligned to a 4 byte boundary.
  403. *
  404. * @param[in] p Pointer value to be checked.
  405. *
  406. * @return TRUE if pointer is aligned to a 4 byte boundary, FALSE otherwise.
  407. */
  408. static __INLINE bool is_word_aligned(void const* p)
  409. {
  410. return (((uintptr_t)p & 0x03) == 0);
  411. }
  412. /**
  413. * @brief Function for checking if provided address is located in stack space.
  414. *
  415. * @param[in] ptr Pointer to be checked.
  416. *
  417. * @return true if address is in stack space, false otherwise.
  418. */
  419. static __INLINE bool is_address_from_stack(void * ptr)
  420. {
  421. if (((uint32_t)ptr >= (uint32_t)STACK_BASE) &&
  422. ((uint32_t)ptr < (uint32_t)STACK_TOP) )
  423. {
  424. return true;
  425. }
  426. else
  427. {
  428. return false;
  429. }
  430. }
  431. #endif // APP_UTIL_H__
  432. /** @} */