app_util.h 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  1. /**
  2. * Copyright (c) 2012 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file
  41. *
  42. * @defgroup app_util Utility Functions and Definitions
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief Various types and definitions available to all applications.
  47. */
  48. #ifndef APP_UTIL_H__
  49. #define APP_UTIL_H__
  50. #include <stdint.h>
  51. #include <stdbool.h>
  52. #include <stddef.h>
  53. #include "compiler_abstraction.h"
  54. #include "nordic_common.h"
  55. #include "nrf.h"
  56. #ifdef __cplusplus
  57. extern "C" {
  58. #endif
  59. /**
  60. * @cond (NODOX)
  61. */
  62. /*lint -save -e27 -e10 -e19 */
  63. #if defined (__LINT__)
  64. #define STACK_BASE 0x1F000 // Arbitrary value.
  65. #define STACK_TOP 0x20000 // Arbitrary value.
  66. #elif defined ( __CC_ARM )
  67. extern char STACK$$Base;
  68. extern char STACK$$Length;
  69. #define STACK_BASE &STACK$$Base
  70. #define STACK_TOP ((void*)((uint32_t)STACK_BASE + (uint32_t)&STACK$$Length))
  71. #elif defined ( __ICCARM__ )
  72. extern char CSTACK$$Base;
  73. extern char CSTACK$$Length;
  74. #define STACK_BASE &CSTACK$$Base
  75. #define STACK_TOP ((void*)((uint32_t)STACK_BASE + (uint32_t)&CSTACK$$Length))
  76. #elif defined ( __GNUC__ )
  77. extern uint32_t __StackTop;
  78. extern uint32_t __StackLimit;
  79. #define STACK_BASE &__StackLimit
  80. #define STACK_TOP &__StackTop
  81. #endif
  82. /* These macros are valid only when absolute placement is used for the application
  83. * image. The macros are not compile time symbols. They cannot be used as a
  84. * constant expression, for example, inside a static assert or linker script
  85. * at-placement. */
  86. #if defined (__LINT__)
  87. #define CODE_START (0) // Arbitrary value.
  88. #define CODE_END (0x1000) // Arbitrary value.
  89. #define CODE_SIZE (0x1000) // Arbitrary value.
  90. #elif defined ( __CC_ARM )
  91. extern char Load$$LR$$LR_IROM1$$Base;
  92. extern char Load$$LR$$LR_IROM1$$Length;
  93. extern char Load$$LR$$LR_IROM1$$Limit;
  94. #define CODE_START ((uint32_t)&Load$$LR$$LR_IROM1$$Base)
  95. #define CODE_END ((uint32_t)&Load$$LR$$LR_IROM1$$Limit)
  96. #define CODE_SIZE ((uint32_t)&Load$$LR$$LR_IROM1$$Length)
  97. #elif defined ( __ICCARM__ )
  98. extern void * __vector_table;
  99. extern char RO_END$$Base;
  100. #define CODE_START ((uint32_t)&__vector_table)
  101. #define CODE_END ((uint32_t)&RO_END$$Base)
  102. #define CODE_SIZE (CODE_END - CODE_START)
  103. #elif defined(__SES_ARM)
  104. extern uint32_t * _vectors;
  105. extern uint32_t __FLASH_segment_used_end__;
  106. #define CODE_START ((uint32_t)&_vectors)
  107. #define CODE_END ((uint32_t)&__FLASH_segment_used_end__)
  108. #define CODE_SIZE (CODE_END - CODE_START)
  109. #elif defined ( __GNUC__ )
  110. extern uint32_t __isr_vector;
  111. extern uint32_t __etext;
  112. #define CODE_START ((uint32_t)&__isr_vector)
  113. #define CODE_END ((uint32_t)&__etext)
  114. #define CODE_SIZE (CODE_END - CODE_START)
  115. #endif
  116. /** @}
  117. * @endcond
  118. */
  119. /* lint -restore */
  120. #if defined(MBR_PRESENT) || defined(SOFTDEVICE_PRESENT)
  121. #include "nrf_mbr.h"
  122. #define BOOTLOADER_ADDRESS ((*(uint32_t *)MBR_BOOTLOADER_ADDR) == 0xFFFFFFFF ? *MBR_UICR_BOOTLOADER_ADDR : *(uint32_t *)MBR_BOOTLOADER_ADDR) /**< The currently configured start address of the bootloader. If 0xFFFFFFFF, no bootloader start address is configured. */
  123. #define MBR_PARAMS_PAGE_ADDRESS ((*(uint32_t *)MBR_PARAM_PAGE_ADDR) == 0xFFFFFFFF ? *MBR_UICR_PARAM_PAGE_ADDR : *(uint32_t *)MBR_PARAM_PAGE_ADDR) /**< The currently configured address of the MBR params page. If 0xFFFFFFFF, no MBR params page address is configured. */
  124. #else
  125. #define BOOTLOADER_ADDRESS (NRF_UICR->NRFFW[0]) /**< Check UICR, just in case. */
  126. #define MBR_PARAMS_PAGE_ADDRESS (NRF_UICR->NRFFW[1]) /**< Check UICR, just in case. */
  127. #endif
  128. enum
  129. {
  130. UNIT_0_625_MS = 625, /**< Number of microseconds in 0.625 milliseconds. */
  131. UNIT_1_25_MS = 1250, /**< Number of microseconds in 1.25 milliseconds. */
  132. UNIT_10_MS = 10000 /**< Number of microseconds in 10 milliseconds. */
  133. };
  134. /**
  135. * @brief Counts number of bits required for the given value
  136. *
  137. * The macro technically searches for the highest bit set.
  138. * For value 0 it returns 0.
  139. *
  140. * @param val Value to be processed
  141. *
  142. * @return Number of bits required for the given value
  143. */
  144. //lint -emacro(572,VBITS)
  145. #define VBITS(val) VBITS_32(val)
  146. /**
  147. * @def VBITS_1
  148. * @brief Internal macro used by @ref VBITS */
  149. /**
  150. * @def VBITS_2
  151. * @brief Internal macro used by @ref VBITS */
  152. /**
  153. * @def VBITS_4
  154. * @brief Internal macro used by @ref VBITS */
  155. /**
  156. * @def VBITS_8
  157. * @brief Internal macro used by @ref VBITS */
  158. /**
  159. * @def VBITS_16
  160. * @brief Internal macro used by @ref VBITS */
  161. /**
  162. * @def VBITS_32
  163. * @brief Internal macro used by @ref VBITS */
  164. #define VBITS_1( v) ((((v) & (0x0001U << 0)) != 0) ? 1U : 0U)
  165. #define VBITS_2( v) ((((v) & (0x0001U << 1)) != 0) ? VBITS_1 ((v) >> 1) + 1 : VBITS_1 (v))
  166. #define VBITS_4( v) ((((v) & (0x0003U << 2)) != 0) ? VBITS_2 ((v) >> 2) + 2 : VBITS_2 (v))
  167. #define VBITS_8( v) ((((v) & (0x000fU << 4)) != 0) ? VBITS_4 ((v) >> 4) + 4 : VBITS_4 (v))
  168. #define VBITS_16(v) ((((v) & (0x00ffU << 8)) != 0) ? VBITS_8 ((v) >> 8) + 8 : VBITS_8 (v))
  169. #define VBITS_32(v) ((((v) & (0xffffU << 16)) != 0) ? VBITS_16((v) >> 16) + 16 : VBITS_16(v))
  170. /*Segger embedded studio originally has offsetof macro which cannot be used in macros (like STATIC_ASSERT).
  171. This redefinition is to allow using that. */
  172. #if defined(__SES_ARM) && defined(__GNUC__)
  173. #undef offsetof
  174. #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
  175. #endif
  176. /**@brief Implementation specific macro for delayed macro expansion used in string concatenation
  177. *
  178. * @param[in] lhs Left hand side in concatenation
  179. * @param[in] rhs Right hand side in concatenation
  180. */
  181. #define STRING_CONCATENATE_IMPL(lhs, rhs) lhs ## rhs
  182. /**@brief Macro used to concatenate string using delayed macro expansion
  183. *
  184. * @note This macro will delay concatenation until the expressions have been resolved
  185. *
  186. * @param[in] lhs Left hand side in concatenation
  187. * @param[in] rhs Right hand side in concatenation
  188. */
  189. #define STRING_CONCATENATE(lhs, rhs) STRING_CONCATENATE_IMPL(lhs, rhs)
  190. #ifndef __LINT__
  191. #ifdef __GNUC__
  192. #ifdef __cplusplus
  193. #define STATIC_ASSERT_SIMPLE(EXPR) extern char (*_do_assert(void)) [sizeof(char[1 - 2*!(EXPR)])]
  194. #define STATIC_ASSERT_MSG(EXPR, MSG) extern char (*_do_assert(void)) [sizeof(char[1 - 2*!(EXPR)])]
  195. #else
  196. #define STATIC_ASSERT_SIMPLE(EXPR) _Static_assert(EXPR, "unspecified message")
  197. #define STATIC_ASSERT_MSG(EXPR, MSG) _Static_assert(EXPR, MSG)
  198. #endif
  199. #endif
  200. #ifdef __CC_ARM
  201. #define STATIC_ASSERT_SIMPLE(EXPR) extern char (*_do_assert(void)) [sizeof(char[1 - 2*!(EXPR)])]
  202. #define STATIC_ASSERT_MSG(EXPR, MSG) extern char (*_do_assert(void)) [sizeof(char[1 - 2*!(EXPR)])]
  203. #endif
  204. #ifdef __ICCARM__
  205. #define STATIC_ASSERT_SIMPLE(EXPR) static_assert(EXPR, "unspecified message")
  206. #define STATIC_ASSERT_MSG(EXPR, MSG) static_assert(EXPR, MSG)
  207. #endif
  208. #else // __LINT__
  209. #define STATIC_ASSERT_SIMPLE(EXPR) extern char (*_ignore(void))
  210. #define STATIC_ASSERT_MSG(EXPR, MSG) extern char (*_ignore(void))
  211. #endif
  212. #define _SELECT_ASSERT_FUNC(x, EXPR, MSG, ASSERT_MACRO, ...) ASSERT_MACRO
  213. /**
  214. * @brief Static (i.e. compile time) assert macro.
  215. *
  216. * @note The output of STATIC_ASSERT can be different across compilers.
  217. *
  218. * Usage:
  219. * STATIC_ASSERT(expression);
  220. * STATIC_ASSERT(expression, message);
  221. *
  222. * @hideinitializer
  223. */
  224. //lint -save -esym(???, STATIC_ASSERT)
  225. #define STATIC_ASSERT(...) \
  226. _SELECT_ASSERT_FUNC(x, ##__VA_ARGS__, \
  227. STATIC_ASSERT_MSG(__VA_ARGS__), \
  228. STATIC_ASSERT_SIMPLE(__VA_ARGS__))
  229. //lint -restore
  230. /**@brief Implementation details for NUM_VAR_ARGS */
  231. #define NUM_VA_ARGS_IMPL( \
  232. _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
  233. _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
  234. _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \
  235. _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \
  236. _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \
  237. _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, \
  238. _61, _62, N, ...) N
  239. /**@brief Macro to get the number of arguments in a call variadic macro call
  240. *
  241. * param[in] ... List of arguments
  242. *
  243. * @retval Number of variadic arguments in the argument list
  244. */
  245. #define NUM_VA_ARGS(...) NUM_VA_ARGS_IMPL(__VA_ARGS__, 63, 62, 61, \
  246. 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, \
  247. 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, \
  248. 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, \
  249. 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, \
  250. 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \
  251. 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
  252. /**@brief Implementation details for NUM_VAR_ARGS */
  253. #define NUM_VA_ARGS_LESS_1_IMPL( \
  254. _ignored, \
  255. _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
  256. _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
  257. _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \
  258. _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \
  259. _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \
  260. _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, \
  261. _61, _62, N, ...) N
  262. /**@brief Macro to get the number of arguments in a call variadic macro call.
  263. * First argument is not counted.
  264. *
  265. * param[in] ... List of arguments
  266. *
  267. * @retval Number of variadic arguments in the argument list
  268. */
  269. #define NUM_VA_ARGS_LESS_1(...) NUM_VA_ARGS_LESS_1_IMPL(__VA_ARGS__, 63, 62, 61, \
  270. 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, \
  271. 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, \
  272. 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, \
  273. 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, \
  274. 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \
  275. 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, ~)
  276. /**@brief type for holding an encoded (i.e. little endian) 16 bit unsigned integer. */
  277. typedef uint8_t uint16_le_t[2];
  278. /**@brief Type for holding an encoded (i.e. little endian) 32 bit unsigned integer. */
  279. typedef uint8_t uint32_le_t[4];
  280. /**@brief Byte array type. */
  281. typedef struct
  282. {
  283. uint16_t size; /**< Number of array entries. */
  284. uint8_t * p_data; /**< Pointer to array entries. */
  285. } uint8_array_t;
  286. /**@brief Macro for performing rounded integer division (as opposed to truncating the result).
  287. *
  288. * @param[in] A Numerator.
  289. * @param[in] B Denominator.
  290. *
  291. * @return Rounded (integer) result of dividing A by B.
  292. */
  293. #define ROUNDED_DIV(A, B) (((A) + ((B) / 2)) / (B))
  294. /**@brief Macro for checking if an integer is a power of two.
  295. *
  296. * @param[in] A Number to be tested.
  297. *
  298. * @return true if value is power of two.
  299. * @return false if value not power of two.
  300. */
  301. #define IS_POWER_OF_TWO(A) ( ((A) != 0) && ((((A) - 1) & (A)) == 0) )
  302. /**@brief Macro for converting milliseconds to ticks.
  303. *
  304. * @param[in] TIME Number of milliseconds to convert.
  305. * @param[in] RESOLUTION Unit to be converted to in [us/ticks].
  306. */
  307. #define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION))
  308. /**@brief Macro for performing integer division, making sure the result is rounded up.
  309. *
  310. * @details One typical use for this is to compute the number of objects with size B is needed to
  311. * hold A number of bytes.
  312. *
  313. * @param[in] A Numerator.
  314. * @param[in] B Denominator.
  315. *
  316. * @return Integer result of dividing A by B, rounded up.
  317. */
  318. #define CEIL_DIV(A, B) \
  319. (((A) + (B) - 1) / (B))
  320. /**@brief Macro for creating a buffer aligned to 4 bytes.
  321. *
  322. * @param[in] NAME Name of the buffor.
  323. * @param[in] MIN_SIZE Size of this buffor (it will be rounded up to multiples of 4 bytes).
  324. */
  325. #define WORD_ALIGNED_MEM_BUFF(NAME, MIN_SIZE) static uint32_t NAME[CEIL_DIV(MIN_SIZE, sizeof(uint32_t))]
  326. /**@brief Macro for calculating the number of words that are needed to hold a number of bytes.
  327. *
  328. * @details Adds 3 and divides by 4.
  329. *
  330. * @param[in] n_bytes The number of bytes.
  331. *
  332. * @return The number of words that @p n_bytes take up (rounded up).
  333. */
  334. #define BYTES_TO_WORDS(n_bytes) (((n_bytes) + 3) >> 2)
  335. /**@brief The number of bytes in a word.
  336. */
  337. #define BYTES_PER_WORD (4)
  338. /**@brief Macro for increasing a number to the nearest (larger) multiple of another number.
  339. *
  340. * @param[in] alignment The number to align to.
  341. * @param[in] number The number to align (increase).
  342. *
  343. * @return The aligned (increased) @p number.
  344. */
  345. #define ALIGN_NUM(alignment, number) (((number) - 1) + (alignment) - (((number) - 1) % (alignment)))
  346. /**@brief Macro for getting first of 2 parameters.
  347. *
  348. * @param[in] a1 First parameter.
  349. * @param[in] a2 Second parameter.
  350. */
  351. #define GET_ARG_1(a1, a2) a1
  352. /**@brief Macro for getting second of 2 parameters.
  353. *
  354. * @param[in] a1 First parameter.
  355. * @param[in] a2 Second parameter.
  356. */
  357. #define GET_ARG_2(a1, a2) a2
  358. /**@brief Container of macro (borrowed from Linux kernel).
  359. *
  360. * This macro returns parent structure address basing on child member address.
  361. *
  362. * @param ptr Address of child type.
  363. * @param type Type of parent structure.
  364. * @param member Name of child field in parent structure.
  365. *
  366. * @return Parent structure address.
  367. * */
  368. #define CONTAINER_OF(ptr, type, member) \
  369. (type *)((char *)ptr - offsetof(type, member))
  370. /**
  371. * @brief Define Bit-field mask
  372. *
  373. * Macro that defined the mask with selected number of bits set, starting from
  374. * provided bit number.
  375. *
  376. * @param[in] bcnt Number of bits in the bit-field
  377. * @param[in] boff Lowest bit number
  378. */
  379. #define BF_MASK(bcnt, boff) ( ((1U << (bcnt)) - 1U) << (boff) )
  380. /**
  381. * @brief Get bit-field
  382. *
  383. * Macro that extracts selected bit-field from provided value
  384. *
  385. * @param[in] val Value from which selected bit-field would be extracted
  386. * @param[in] bcnt Number of bits in the bit-field
  387. * @param[in] boff Lowest bit number
  388. *
  389. * @return Value of the selected bits
  390. */
  391. #define BF_GET(val, bcnt, boff) ( ( (val) & BF_MASK((bcnt), (boff)) ) >> (boff) )
  392. /**
  393. * @brief Create bit-field value
  394. *
  395. * Value is masked and shifted to match given bit-field
  396. *
  397. * @param[in] val Value to set on bit-field
  398. * @param[in] bcnt Number of bits for bit-field
  399. * @param[in] boff Offset of bit-field
  400. *
  401. * @return Value positioned of given bit-field.
  402. */
  403. #define BF_VAL(val, bcnt, boff) ( (((uint32_t)(val)) << (boff)) & BF_MASK(bcnt, boff) )
  404. /**
  405. * @name Configuration of complex bit-field
  406. *
  407. * @sa BF_CX
  408. * @{
  409. */
  410. /** @brief Position of bit count in complex bit-field value */
  411. #define BF_CX_BCNT_POS 0U
  412. /** @brief Mask of bit count in complex bit-field value */
  413. #define BF_CX_BCNT_MASK (0xffU << BF_CX_BCNT_POS)
  414. /** @brief Position of bit position in complex bit-field value */
  415. #define BF_CX_BOFF_POS 8U
  416. /** @brief Mask of bit position in complex bit-field value */
  417. #define BF_CX_BOFF_MASK (0xffU << BF_CX_BOFF_POS)
  418. /** @} */
  419. /**
  420. * @brief Define complex bit-field
  421. *
  422. * Complex bit-field would contain its position and size in one number.
  423. * @sa BF_CX_MASK
  424. * @sa BF_CX_POS
  425. * @sa BF_CX_GET
  426. *
  427. * @param[in] bcnt Number of bits in the bit-field
  428. * @param[in] boff Lowest bit number
  429. *
  430. * @return The single number that describes the bit-field completely.
  431. */
  432. #define BF_CX(bcnt, boff) ( ((((uint32_t)(bcnt)) << BF_CX_BCNT_POS) & BF_CX_BCNT_MASK) | ((((uint32_t)(boff)) << BF_CX_BOFF_POS) & BF_CX_BOFF_MASK) )
  433. /**
  434. * @brief Get number of bits in bit-field
  435. *
  436. * @sa BF_CX
  437. *
  438. * @param bf_cx Complex bit-field
  439. *
  440. * @return Number of bits in given bit-field
  441. */
  442. #define BF_CX_BCNT(bf_cx) ( ((bf_cx) & BF_CX_BCNT_MASK) >> BF_CX_BCNT_POS )
  443. /**
  444. * @brief Get lowest bit number in the field
  445. *
  446. * @sa BF_CX
  447. *
  448. * @param[in] bf_cx Complex bit-field
  449. *
  450. * @return Lowest bit number in given bit-field
  451. */
  452. #define BF_CX_BOFF(bf_cx) ( ((bf_cx) & BF_CX_BOFF_MASK) >> BF_CX_BOFF_POS )
  453. /**
  454. * @brief Get bit mask of the selected field
  455. *
  456. * @sa BF_CX
  457. *
  458. * @param[in] bf_cx Complex bit-field
  459. *
  460. * @return Mask of given bit-field
  461. */
  462. #define BF_CX_MASK(bf_cx) BF_MASK(BF_CX_BCNT(bf_cx), BF_CX_BOFF(bf_cx))
  463. /**
  464. * @brief Get bit-field
  465. *
  466. * Macro that extracts selected bit-field from provided value.
  467. * Bit-field is given as a complex value.
  468. *
  469. * @sa BF_CX
  470. * @sa BF_GET
  471. *
  472. * @param[in] val Value from which selected bit-field would be extracted
  473. * @param[in] bf_cx Complex bit-field
  474. *
  475. * @return Value of the selected bits.
  476. */
  477. #define BF_CX_GET(val, bf_cx) BF_GET(val, BF_CX_BCNT(bf_cx), BF_CX_BOFF(bf_cx))
  478. /**
  479. * @brief Create bit-field value
  480. *
  481. * Value is masked and shifted to match given bit-field.
  482. *
  483. * @param[in] val Value to set on bit-field
  484. * @param[in] bf_cx Complex bit-field
  485. *
  486. * @return Value positioned of given bit-field.
  487. */
  488. #define BF_CX_VAL(val, bf_cx) BF_VAL(val, BF_CX_BCNT(bf_cx), BF_CX_BOFF(bf_cx))
  489. /**
  490. * @brief Extracting data from the brackets
  491. *
  492. * This macro get rid of brackets around the argument.
  493. * It can be used to pass multiple arguments in logical one argument to a macro.
  494. * Call it with arguments inside brackets:
  495. * @code
  496. * #define ARGUMENTS (a, b, c)
  497. * BRACKET_EXTRACT(ARGUMENTS)
  498. * @endcode
  499. * It would produce:
  500. * @code
  501. * a, b, c
  502. * @endcode
  503. *
  504. * @param a Argument with anything inside brackets
  505. * @return Anything that appears inside the brackets of the argument
  506. *
  507. * @note
  508. * The argument of the macro have to be inside brackets.
  509. * In other case the compilation would fail.
  510. */
  511. #define BRACKET_EXTRACT(a) BRACKET_EXTRACT_(a)
  512. #define BRACKET_EXTRACT_(a) BRACKET_EXTRACT__ a
  513. #define BRACKET_EXTRACT__(...) __VA_ARGS__
  514. /**
  515. * @brief Check if number of parameters is more than 1
  516. *
  517. * @param ... Arguments to count
  518. *
  519. * @return 0 If argument count is <= 1
  520. * @return 1 If argument count is > 1
  521. *
  522. * @sa NUM_VA_ARGS
  523. * @sa NUM_IS_MORE_THAN_1
  524. */
  525. #define NUM_VA_ARGS_IS_MORE_THAN_1(...) NUM_IS_MORE_THAN_1(NUM_VA_ARGS(__VA_ARGS__))
  526. /**
  527. * @brief Check if given numeric value is bigger than 1
  528. *
  529. * This macro accepts numeric value, that may be the result of argument expansion.
  530. * This numeric value is then converted to 0 if it is lover than 1 or to 1 if
  531. * its value is higher than 1.
  532. * The generated result can be used to glue it into other macro mnemonic name.
  533. *
  534. * @param N Numeric value to check
  535. *
  536. * @return 0 If argument is <= 1
  537. * @return 1 If argument is > 1
  538. *
  539. * @note Any existing definition of a form NUM_IS_MORE_THAN_1_PROBE_[N] can
  540. * broke the result of this macro
  541. */
  542. #define NUM_IS_MORE_THAN_1(N) NUM_IS_MORE_THAN_1_(N)
  543. #define NUM_IS_MORE_THAN_1_(N) NUM_IS_MORE_THAN_1_PROBE_(NUM_IS_MORE_THAN_1_PROBE_ ## N, 1)
  544. #define NUM_IS_MORE_THAN_1_PROBE_(...) GET_VA_ARG_1(GET_ARGS_AFTER_1(__VA_ARGS__))
  545. #define NUM_IS_MORE_THAN_1_PROBE_0 ~, 0
  546. #define NUM_IS_MORE_THAN_1_PROBE_1 ~, 0
  547. /**
  548. * @brief Get the first argument
  549. *
  550. * @param ... Arguments to select
  551. *
  552. * @return First argument or empty if no arguments are provided
  553. */
  554. #define GET_VA_ARG_1(...) GET_VA_ARG_1_(__VA_ARGS__, ) // Make sure that also for 1 argument it works
  555. #define GET_VA_ARG_1_(a1, ...) a1
  556. /**
  557. * @brief Get all the arguments but the first one
  558. *
  559. * @param ... Arguments to select
  560. *
  561. * @return All arguments after the first one or empty if less than 2 arguments are provided
  562. */
  563. #define GET_ARGS_AFTER_1(...) GET_ARGS_AFTER_1_(__VA_ARGS__, ) // Make sure that also for 1 argument it works
  564. #define GET_ARGS_AFTER_1_(a1, ...) __VA_ARGS__
  565. /**
  566. * @brief Size of a field in declared structure
  567. *
  568. * Macro that returns the size of the structure field.
  569. * @param struct_type Variable type to get the field size from
  570. * @param field Field name to analyze. It can be even field inside field (field.somethingelse.and_another).
  571. *
  572. * @return Size of the field
  573. */
  574. #define FIELD_SIZE(struct_type, field) sizeof(((struct struct_type*)NULL)->field)
  575. /**
  576. * @brief Number of elements in field array in declared structure
  577. *
  578. * Macro that returns number of elementy in structure field.
  579. * @param struct_type Variable type to get the field size from
  580. * @param field Field name to analyze.
  581. *
  582. * @return Number of elements in field array
  583. *
  584. * @sa FIELD_SIZE
  585. */
  586. #define FIELD_ARRAY_SIZE(struct_type, field) (FIELD_SIZE(struct_type, field) / FIELD_SIZE(struct_type, field[0]))
  587. /**
  588. * @brief Mapping macro
  589. *
  590. * Macro that process all arguments using given macro
  591. *
  592. * @param ... Macro name to be used for argument processing followed by arguments to process.
  593. * Macro should have following form: MACRO(argument)
  594. *
  595. * @return All arguments processed by given macro
  596. */
  597. #define MACRO_MAP(...) MACRO_MAP_(__VA_ARGS__)
  598. #define MACRO_MAP_(...) MACRO_MAP_N(NUM_VA_ARGS_LESS_1(__VA_ARGS__), __VA_ARGS__) // To make sure it works also for 2 arguments in total
  599. /**
  600. * @brief Mapping macro, recursive version
  601. *
  602. * Can be used in @ref MACRO_MAP macro
  603. */
  604. #define MACRO_MAP_REC(...) MACRO_MAP_REC_(__VA_ARGS__)
  605. #define MACRO_MAP_REC_(...) MACRO_MAP_REC_N(NUM_VA_ARGS_LESS_1(__VA_ARGS__), __VA_ARGS__) // To make sure it works also for 2 arguments in total
  606. /**
  607. * @brief Mapping N arguments macro
  608. *
  609. * Macro similar to @ref MACRO_MAP but maps exact number of arguments.
  610. * If there is more arguments given, the rest would be ignored.
  611. *
  612. * @param N Number of arguments to map
  613. * @param ... Macro name to be used for argument processing followed by arguments to process.
  614. * Macro should have following form: MACRO(argument)
  615. *
  616. * @return Selected number of arguments processed by given macro
  617. */
  618. #define MACRO_MAP_N(N, ...) MACRO_MAP_N_(N, __VA_ARGS__)
  619. #define MACRO_MAP_N_(N, ...) CONCAT_2(MACRO_MAP_, N)(__VA_ARGS__, )
  620. /**
  621. * @brief Mapping N arguments macro, recursive version
  622. *
  623. * Can be used in @ref MACRO_MAP_N macro
  624. */
  625. #define MACRO_MAP_REC_N(N, ...) MACRO_MAP_REC_N_(N, __VA_ARGS__)
  626. #define MACRO_MAP_REC_N_(N, ...) CONCAT_2(MACRO_MAP_REC_, N)(__VA_ARGS__, )
  627. #define MACRO_MAP_0( ...)
  628. #define MACRO_MAP_1( macro, a, ...) macro(a)
  629. #define MACRO_MAP_2( macro, a, ...) macro(a) MACRO_MAP_1 (macro, __VA_ARGS__, )
  630. #define MACRO_MAP_3( macro, a, ...) macro(a) MACRO_MAP_2 (macro, __VA_ARGS__, )
  631. #define MACRO_MAP_4( macro, a, ...) macro(a) MACRO_MAP_3 (macro, __VA_ARGS__, )
  632. #define MACRO_MAP_5( macro, a, ...) macro(a) MACRO_MAP_4 (macro, __VA_ARGS__, )
  633. #define MACRO_MAP_6( macro, a, ...) macro(a) MACRO_MAP_5 (macro, __VA_ARGS__, )
  634. #define MACRO_MAP_7( macro, a, ...) macro(a) MACRO_MAP_6 (macro, __VA_ARGS__, )
  635. #define MACRO_MAP_8( macro, a, ...) macro(a) MACRO_MAP_7 (macro, __VA_ARGS__, )
  636. #define MACRO_MAP_9( macro, a, ...) macro(a) MACRO_MAP_8 (macro, __VA_ARGS__, )
  637. #define MACRO_MAP_10(macro, a, ...) macro(a) MACRO_MAP_9 (macro, __VA_ARGS__, )
  638. #define MACRO_MAP_11(macro, a, ...) macro(a) MACRO_MAP_10(macro, __VA_ARGS__, )
  639. #define MACRO_MAP_12(macro, a, ...) macro(a) MACRO_MAP_11(macro, __VA_ARGS__, )
  640. #define MACRO_MAP_13(macro, a, ...) macro(a) MACRO_MAP_12(macro, __VA_ARGS__, )
  641. #define MACRO_MAP_14(macro, a, ...) macro(a) MACRO_MAP_13(macro, __VA_ARGS__, )
  642. #define MACRO_MAP_15(macro, a, ...) macro(a) MACRO_MAP_14(macro, __VA_ARGS__, )
  643. #define MACRO_MAP_16(macro, a, ...) macro(a) MACRO_MAP_15(macro, __VA_ARGS__, )
  644. #define MACRO_MAP_17(macro, a, ...) macro(a) MACRO_MAP_16(macro, __VA_ARGS__, )
  645. #define MACRO_MAP_18(macro, a, ...) macro(a) MACRO_MAP_17(macro, __VA_ARGS__, )
  646. #define MACRO_MAP_19(macro, a, ...) macro(a) MACRO_MAP_18(macro, __VA_ARGS__, )
  647. #define MACRO_MAP_20(macro, a, ...) macro(a) MACRO_MAP_19(macro, __VA_ARGS__, )
  648. #define MACRO_MAP_21(macro, a, ...) macro(a) MACRO_MAP_20(macro, __VA_ARGS__, )
  649. #define MACRO_MAP_22(macro, a, ...) macro(a) MACRO_MAP_21(macro, __VA_ARGS__, )
  650. #define MACRO_MAP_23(macro, a, ...) macro(a) MACRO_MAP_22(macro, __VA_ARGS__, )
  651. #define MACRO_MAP_24(macro, a, ...) macro(a) MACRO_MAP_23(macro, __VA_ARGS__, )
  652. #define MACRO_MAP_25(macro, a, ...) macro(a) MACRO_MAP_24(macro, __VA_ARGS__, )
  653. #define MACRO_MAP_26(macro, a, ...) macro(a) MACRO_MAP_25(macro, __VA_ARGS__, )
  654. #define MACRO_MAP_27(macro, a, ...) macro(a) MACRO_MAP_26(macro, __VA_ARGS__, )
  655. #define MACRO_MAP_28(macro, a, ...) macro(a) MACRO_MAP_27(macro, __VA_ARGS__, )
  656. #define MACRO_MAP_29(macro, a, ...) macro(a) MACRO_MAP_28(macro, __VA_ARGS__, )
  657. #define MACRO_MAP_30(macro, a, ...) macro(a) MACRO_MAP_29(macro, __VA_ARGS__, )
  658. #define MACRO_MAP_31(macro, a, ...) macro(a) MACRO_MAP_30(macro, __VA_ARGS__, )
  659. #define MACRO_MAP_32(macro, a, ...) macro(a) MACRO_MAP_31(macro, __VA_ARGS__, )
  660. #define MACRO_MAP_REC_0( ...)
  661. #define MACRO_MAP_REC_1( macro, a, ...) macro(a)
  662. #define MACRO_MAP_REC_2( macro, a, ...) macro(a) MACRO_MAP_REC_1 (macro, __VA_ARGS__, )
  663. #define MACRO_MAP_REC_3( macro, a, ...) macro(a) MACRO_MAP_REC_2 (macro, __VA_ARGS__, )
  664. #define MACRO_MAP_REC_4( macro, a, ...) macro(a) MACRO_MAP_REC_3 (macro, __VA_ARGS__, )
  665. #define MACRO_MAP_REC_5( macro, a, ...) macro(a) MACRO_MAP_REC_4 (macro, __VA_ARGS__, )
  666. #define MACRO_MAP_REC_6( macro, a, ...) macro(a) MACRO_MAP_REC_5 (macro, __VA_ARGS__, )
  667. #define MACRO_MAP_REC_7( macro, a, ...) macro(a) MACRO_MAP_REC_6 (macro, __VA_ARGS__, )
  668. #define MACRO_MAP_REC_8( macro, a, ...) macro(a) MACRO_MAP_REC_7 (macro, __VA_ARGS__, )
  669. #define MACRO_MAP_REC_9( macro, a, ...) macro(a) MACRO_MAP_REC_8 (macro, __VA_ARGS__, )
  670. #define MACRO_MAP_REC_10(macro, a, ...) macro(a) MACRO_MAP_REC_9 (macro, __VA_ARGS__, )
  671. #define MACRO_MAP_REC_11(macro, a, ...) macro(a) MACRO_MAP_REC_10(macro, __VA_ARGS__, )
  672. #define MACRO_MAP_REC_12(macro, a, ...) macro(a) MACRO_MAP_REC_11(macro, __VA_ARGS__, )
  673. #define MACRO_MAP_REC_13(macro, a, ...) macro(a) MACRO_MAP_REC_12(macro, __VA_ARGS__, )
  674. #define MACRO_MAP_REC_14(macro, a, ...) macro(a) MACRO_MAP_REC_13(macro, __VA_ARGS__, )
  675. #define MACRO_MAP_REC_15(macro, a, ...) macro(a) MACRO_MAP_REC_14(macro, __VA_ARGS__, )
  676. #define MACRO_MAP_REC_16(macro, a, ...) macro(a) MACRO_MAP_REC_15(macro, __VA_ARGS__, )
  677. #define MACRO_MAP_REC_17(macro, a, ...) macro(a) MACRO_MAP_REC_16(macro, __VA_ARGS__, )
  678. #define MACRO_MAP_REC_18(macro, a, ...) macro(a) MACRO_MAP_REC_17(macro, __VA_ARGS__, )
  679. #define MACRO_MAP_REC_19(macro, a, ...) macro(a) MACRO_MAP_REC_18(macro, __VA_ARGS__, )
  680. #define MACRO_MAP_REC_20(macro, a, ...) macro(a) MACRO_MAP_REC_19(macro, __VA_ARGS__, )
  681. #define MACRO_MAP_REC_21(macro, a, ...) macro(a) MACRO_MAP_REC_20(macro, __VA_ARGS__, )
  682. #define MACRO_MAP_REC_22(macro, a, ...) macro(a) MACRO_MAP_REC_21(macro, __VA_ARGS__, )
  683. #define MACRO_MAP_REC_23(macro, a, ...) macro(a) MACRO_MAP_REC_22(macro, __VA_ARGS__, )
  684. #define MACRO_MAP_REC_24(macro, a, ...) macro(a) MACRO_MAP_REC_23(macro, __VA_ARGS__, )
  685. #define MACRO_MAP_REC_25(macro, a, ...) macro(a) MACRO_MAP_REC_24(macro, __VA_ARGS__, )
  686. #define MACRO_MAP_REC_26(macro, a, ...) macro(a) MACRO_MAP_REC_25(macro, __VA_ARGS__, )
  687. #define MACRO_MAP_REC_27(macro, a, ...) macro(a) MACRO_MAP_REC_26(macro, __VA_ARGS__, )
  688. #define MACRO_MAP_REC_28(macro, a, ...) macro(a) MACRO_MAP_REC_27(macro, __VA_ARGS__, )
  689. #define MACRO_MAP_REC_29(macro, a, ...) macro(a) MACRO_MAP_REC_28(macro, __VA_ARGS__, )
  690. #define MACRO_MAP_REC_30(macro, a, ...) macro(a) MACRO_MAP_REC_29(macro, __VA_ARGS__, )
  691. #define MACRO_MAP_REC_31(macro, a, ...) macro(a) MACRO_MAP_REC_30(macro, __VA_ARGS__, )
  692. #define MACRO_MAP_REC_32(macro, a, ...) macro(a) MACRO_MAP_REC_31(macro, __VA_ARGS__, )
  693. /**
  694. * @brief Mapping macro with current index
  695. *
  696. * Basically macro similar to @ref MACRO_MAP, but the processing function would get an argument
  697. * and current argument index (beginning from 0).
  698. *
  699. * @param ... Macro name to be used for argument processing followed by arguments to process.
  700. * Macro should have following form: MACRO(argument, index)
  701. * @return All arguments processed by given macro
  702. */
  703. #define MACRO_MAP_FOR(...) MACRO_MAP_FOR_(__VA_ARGS__)
  704. #define MACRO_MAP_FOR_N_LIST 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, \
  705. 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32
  706. #define MACRO_MAP_FOR_(...) MACRO_MAP_FOR_N(NUM_VA_ARGS_LESS_1(__VA_ARGS__), __VA_ARGS__)
  707. /**
  708. * @brief Mapping N arguments macro with current index
  709. *
  710. * Macro is similar to @ref MACRO_MAP_FOR but maps exact number of arguments.
  711. * If there is more arguments given, the rest would be ignored.
  712. *
  713. * @param N Number of arguments to map
  714. * @param ... Macro name to be used for argument processing followed by arguments to process.
  715. * Macro should have following form: MACRO(argument, index)
  716. *
  717. * @return Selected number of arguments processed by given macro
  718. */
  719. #define MACRO_MAP_FOR_N(N, ...) MACRO_MAP_FOR_N_(N, __VA_ARGS__)
  720. #define MACRO_MAP_FOR_N_(N, ...) CONCAT_2(MACRO_MAP_FOR_, N)((MACRO_MAP_FOR_N_LIST), __VA_ARGS__, )
  721. #define MACRO_MAP_FOR_0( n_list, ...)
  722. #define MACRO_MAP_FOR_1( n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)))
  723. #define MACRO_MAP_FOR_2( n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_1 ((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  724. #define MACRO_MAP_FOR_3( n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_2 ((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  725. #define MACRO_MAP_FOR_4( n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_3 ((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  726. #define MACRO_MAP_FOR_5( n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_4 ((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  727. #define MACRO_MAP_FOR_6( n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_5 ((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  728. #define MACRO_MAP_FOR_7( n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_6 ((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  729. #define MACRO_MAP_FOR_8( n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_7 ((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  730. #define MACRO_MAP_FOR_9( n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_8 ((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  731. #define MACRO_MAP_FOR_10(n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_9 ((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  732. #define MACRO_MAP_FOR_11(n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_10((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  733. #define MACRO_MAP_FOR_12(n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_11((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  734. #define MACRO_MAP_FOR_13(n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_12((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  735. #define MACRO_MAP_FOR_14(n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_13((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  736. #define MACRO_MAP_FOR_15(n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_14((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  737. #define MACRO_MAP_FOR_16(n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_15((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  738. #define MACRO_MAP_FOR_17(n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_16((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  739. #define MACRO_MAP_FOR_18(n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_17((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  740. #define MACRO_MAP_FOR_19(n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_18((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  741. #define MACRO_MAP_FOR_20(n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_19((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  742. #define MACRO_MAP_FOR_21(n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_20((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  743. #define MACRO_MAP_FOR_22(n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_21((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  744. #define MACRO_MAP_FOR_23(n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_22((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  745. #define MACRO_MAP_FOR_24(n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_23((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  746. #define MACRO_MAP_FOR_25(n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_24((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  747. #define MACRO_MAP_FOR_26(n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_25((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  748. #define MACRO_MAP_FOR_27(n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_26((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  749. #define MACRO_MAP_FOR_28(n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_27((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  750. #define MACRO_MAP_FOR_29(n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_28((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  751. #define MACRO_MAP_FOR_30(n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_29((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  752. #define MACRO_MAP_FOR_31(n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_30((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  753. #define MACRO_MAP_FOR_32(n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list))) MACRO_MAP_FOR_31((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__, )
  754. /**
  755. * @brief Mapping macro with current index and parameter
  756. *
  757. * Version of @ref MACRO_MAP_FOR that passes also the same parameter to all macros.
  758. *
  759. * @param param Parameter that would be passed to each macro call during mapping.
  760. * @param ... Macro name to be used for argument processing followed by arguments to process.
  761. * Macro should have following form: MACRO(argument, index, param)
  762. *
  763. * @return All arguments processed by given macro
  764. */
  765. #define MACRO_MAP_FOR_PARAM(param, ...) MACRO_MAP_FOR_PARAM_(param, __VA_ARGS__)
  766. #define MACRO_MAP_FOR_PARAM_(param, ...) MACRO_MAP_FOR_PARAM_N(NUM_VA_ARGS_LESS_1(__VA_ARGS__), param, __VA_ARGS__)
  767. /**
  768. * @brief Mapping N arguments macro with with current index and parameter
  769. *
  770. * @param N Number of arguments to map
  771. * @param param Parameter that would be passed to each macro call during mapping.
  772. * @param ... Macro name to be used for argument processing followed by arguments to process.
  773. * Macro should have following form: MACRO(argument, index, param)
  774. *
  775. * @return All arguments processed by given macro
  776. */
  777. #define MACRO_MAP_FOR_PARAM_N(N, param, ...) MACRO_MAP_FOR_PARAM_N_(N, param, __VA_ARGS__)
  778. #define MACRO_MAP_FOR_PARAM_N_(N, param, ...) CONCAT_2(MACRO_MAP_FOR_PARAM_, N)((MACRO_MAP_FOR_N_LIST), param, __VA_ARGS__, )
  779. #define MACRO_MAP_FOR_PARAM_0( n_list, param, ...)
  780. #define MACRO_MAP_FOR_PARAM_1( n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param)
  781. #define MACRO_MAP_FOR_PARAM_2( n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_1 ((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  782. #define MACRO_MAP_FOR_PARAM_3( n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_2 ((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  783. #define MACRO_MAP_FOR_PARAM_4( n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_3 ((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  784. #define MACRO_MAP_FOR_PARAM_5( n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_4 ((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  785. #define MACRO_MAP_FOR_PARAM_6( n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_5 ((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  786. #define MACRO_MAP_FOR_PARAM_7( n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_6 ((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  787. #define MACRO_MAP_FOR_PARAM_8( n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_7 ((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  788. #define MACRO_MAP_FOR_PARAM_9( n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_8 ((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  789. #define MACRO_MAP_FOR_PARAM_10(n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_9 ((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  790. #define MACRO_MAP_FOR_PARAM_11(n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_10((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  791. #define MACRO_MAP_FOR_PARAM_12(n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_11((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  792. #define MACRO_MAP_FOR_PARAM_13(n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_12((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  793. #define MACRO_MAP_FOR_PARAM_14(n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_13((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  794. #define MACRO_MAP_FOR_PARAM_15(n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_14((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  795. #define MACRO_MAP_FOR_PARAM_16(n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_15((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  796. #define MACRO_MAP_FOR_PARAM_17(n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_16((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  797. #define MACRO_MAP_FOR_PARAM_18(n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_17((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  798. #define MACRO_MAP_FOR_PARAM_19(n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_18((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  799. #define MACRO_MAP_FOR_PARAM_20(n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_19((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  800. #define MACRO_MAP_FOR_PARAM_21(n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_20((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  801. #define MACRO_MAP_FOR_PARAM_22(n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_21((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  802. #define MACRO_MAP_FOR_PARAM_23(n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_22((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  803. #define MACRO_MAP_FOR_PARAM_24(n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_23((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  804. #define MACRO_MAP_FOR_PARAM_25(n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_24((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  805. #define MACRO_MAP_FOR_PARAM_26(n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_25((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  806. #define MACRO_MAP_FOR_PARAM_27(n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_26((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  807. #define MACRO_MAP_FOR_PARAM_28(n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_27((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  808. #define MACRO_MAP_FOR_PARAM_29(n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_28((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  809. #define MACRO_MAP_FOR_PARAM_30(n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_29((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  810. #define MACRO_MAP_FOR_PARAM_31(n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_30((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  811. #define MACRO_MAP_FOR_PARAM_32(n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param) MACRO_MAP_FOR_PARAM_31((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), param, macro, __VA_ARGS__, )
  812. /**
  813. * @brief Repeating macro.
  814. *
  815. * @param count Count of repeats.
  816. * @param macro Macro must have the following form: MACRO(arguments).
  817. * @param ... Arguments passed to the macro.
  818. *
  819. * @return All arguments processed by the given macro.
  820. */
  821. #define MACRO_REPEAT(count, macro, ...) MACRO_REPEAT_(count, macro, __VA_ARGS__)
  822. #define MACRO_REPEAT_(count, macro, ...) CONCAT_2(MACRO_REPEAT_, count)(macro, __VA_ARGS__)
  823. #define MACRO_REPEAT_0(macro, ...)
  824. #define MACRO_REPEAT_1(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_0(macro, __VA_ARGS__)
  825. #define MACRO_REPEAT_2(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_1(macro, __VA_ARGS__)
  826. #define MACRO_REPEAT_3(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_2(macro, __VA_ARGS__)
  827. #define MACRO_REPEAT_4(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_3(macro, __VA_ARGS__)
  828. #define MACRO_REPEAT_5(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_4(macro, __VA_ARGS__)
  829. #define MACRO_REPEAT_6(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_5(macro, __VA_ARGS__)
  830. #define MACRO_REPEAT_7(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_6(macro, __VA_ARGS__)
  831. #define MACRO_REPEAT_8(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_7(macro, __VA_ARGS__)
  832. #define MACRO_REPEAT_9(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_8(macro, __VA_ARGS__)
  833. #define MACRO_REPEAT_10(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_9(macro, __VA_ARGS__)
  834. #define MACRO_REPEAT_11(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_10(macro, __VA_ARGS__)
  835. #define MACRO_REPEAT_12(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_11(macro, __VA_ARGS__)
  836. #define MACRO_REPEAT_13(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_12(macro, __VA_ARGS__)
  837. #define MACRO_REPEAT_14(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_13(macro, __VA_ARGS__)
  838. #define MACRO_REPEAT_15(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_14(macro, __VA_ARGS__)
  839. #define MACRO_REPEAT_16(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_15(macro, __VA_ARGS__)
  840. #define MACRO_REPEAT_17(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_16(macro, __VA_ARGS__)
  841. #define MACRO_REPEAT_18(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_17(macro, __VA_ARGS__)
  842. #define MACRO_REPEAT_19(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_18(macro, __VA_ARGS__)
  843. #define MACRO_REPEAT_20(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_19(macro, __VA_ARGS__)
  844. #define MACRO_REPEAT_21(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_20(macro, __VA_ARGS__)
  845. #define MACRO_REPEAT_22(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_21(macro, __VA_ARGS__)
  846. #define MACRO_REPEAT_23(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_22(macro, __VA_ARGS__)
  847. #define MACRO_REPEAT_24(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_23(macro, __VA_ARGS__)
  848. #define MACRO_REPEAT_25(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_24(macro, __VA_ARGS__)
  849. #define MACRO_REPEAT_26(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_25(macro, __VA_ARGS__)
  850. #define MACRO_REPEAT_27(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_26(macro, __VA_ARGS__)
  851. #define MACRO_REPEAT_28(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_27(macro, __VA_ARGS__)
  852. #define MACRO_REPEAT_29(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_28(macro, __VA_ARGS__)
  853. #define MACRO_REPEAT_30(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_29(macro, __VA_ARGS__)
  854. #define MACRO_REPEAT_31(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_30(macro, __VA_ARGS__)
  855. #define MACRO_REPEAT_32(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_31(macro, __VA_ARGS__)
  856. /**
  857. * @brief Repeating macro with current index.
  858. *
  859. * Macro similar to @ref MACRO_REPEAT but the processing function gets the arguments
  860. * and the current argument index (beginning from 0).
  861. * @param count Count of repeats.
  862. * @param macro Macro must have the following form: MACRO(index, arguments).
  863. * @param ... Arguments passed to the macro.
  864. *
  865. * @return All arguments processed by the given macro.
  866. */
  867. #define MACRO_REPEAT_FOR(count, macro, ...) MACRO_REPEAT_FOR_(count, macro, __VA_ARGS__)
  868. #define MACRO_REPEAT_FOR_(count, macro, ...) CONCAT_2(MACRO_REPEAT_FOR_, count)((MACRO_MAP_FOR_N_LIST), macro, __VA_ARGS__)
  869. #define MACRO_REPEAT_FOR_0(n_list, macro, ...)
  870. #define MACRO_REPEAT_FOR_1(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_0((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  871. #define MACRO_REPEAT_FOR_2(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_1((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  872. #define MACRO_REPEAT_FOR_3(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_2((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  873. #define MACRO_REPEAT_FOR_4(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_3((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  874. #define MACRO_REPEAT_FOR_5(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_4((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  875. #define MACRO_REPEAT_FOR_6(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_5((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  876. #define MACRO_REPEAT_FOR_7(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_6((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  877. #define MACRO_REPEAT_FOR_8(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_7((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  878. #define MACRO_REPEAT_FOR_9(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_8((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  879. #define MACRO_REPEAT_FOR_10(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_9((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  880. #define MACRO_REPEAT_FOR_11(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_10((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  881. #define MACRO_REPEAT_FOR_12(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_11((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  882. #define MACRO_REPEAT_FOR_13(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_12((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  883. #define MACRO_REPEAT_FOR_14(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_13((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  884. #define MACRO_REPEAT_FOR_15(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_14((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  885. #define MACRO_REPEAT_FOR_16(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_15((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  886. #define MACRO_REPEAT_FOR_17(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_16((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  887. #define MACRO_REPEAT_FOR_18(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_17((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  888. #define MACRO_REPEAT_FOR_19(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_18((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  889. #define MACRO_REPEAT_FOR_20(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_19((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  890. #define MACRO_REPEAT_FOR_21(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_20((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  891. #define MACRO_REPEAT_FOR_22(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_21((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  892. #define MACRO_REPEAT_FOR_23(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_22((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  893. #define MACRO_REPEAT_FOR_24(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_23((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  894. #define MACRO_REPEAT_FOR_25(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_24((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  895. #define MACRO_REPEAT_FOR_26(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_25((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  896. #define MACRO_REPEAT_FOR_27(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_26((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  897. #define MACRO_REPEAT_FOR_28(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_27((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  898. #define MACRO_REPEAT_FOR_29(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_28((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  899. #define MACRO_REPEAT_FOR_30(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_29((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  900. #define MACRO_REPEAT_FOR_31(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_30((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  901. #define MACRO_REPEAT_FOR_32(n_list, macro, ...) macro(GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), __VA_ARGS__) MACRO_REPEAT_FOR_31((GET_ARGS_AFTER_1(BRACKET_EXTRACT(n_list))), macro, __VA_ARGS__)
  902. /**@brief Adding curly brace to the macro parameter.
  903. *
  904. * Useful in array of structures initialization.
  905. *
  906. * @param p Parameter to put into the curly brace. */
  907. #define PARAM_CBRACE(p) { p },
  908. /**@brief Function for changing the value unit.
  909. *
  910. * @param[in] value Value to be rescaled.
  911. * @param[in] old_unit_reversal Reversal of the incoming unit.
  912. * @param[in] new_unit_reversal Reversal of the desired unit.
  913. *
  914. * @return Number of bytes written.
  915. */
  916. static __INLINE uint64_t value_rescale(uint32_t value, uint32_t old_unit_reversal, uint16_t new_unit_reversal)
  917. {
  918. return (uint64_t)ROUNDED_DIV((uint64_t)value * new_unit_reversal, old_unit_reversal);
  919. }
  920. /**@brief Function for encoding a uint16 value.
  921. *
  922. * @param[in] value Value to be encoded.
  923. * @param[out] p_encoded_data Buffer where the encoded data is to be written.
  924. *
  925. * @return Number of bytes written.
  926. */
  927. static __INLINE uint8_t uint16_encode(uint16_t value, uint8_t * p_encoded_data)
  928. {
  929. p_encoded_data[0] = (uint8_t) ((value & 0x00FF) >> 0);
  930. p_encoded_data[1] = (uint8_t) ((value & 0xFF00) >> 8);
  931. return sizeof(uint16_t);
  932. }
  933. /**@brief Function for encoding a three-byte value.
  934. *
  935. * @param[in] value Value to be encoded.
  936. * @param[out] p_encoded_data Buffer where the encoded data is to be written.
  937. *
  938. * @return Number of bytes written.
  939. */
  940. static __INLINE uint8_t uint24_encode(uint32_t value, uint8_t * p_encoded_data)
  941. {
  942. p_encoded_data[0] = (uint8_t) ((value & 0x000000FF) >> 0);
  943. p_encoded_data[1] = (uint8_t) ((value & 0x0000FF00) >> 8);
  944. p_encoded_data[2] = (uint8_t) ((value & 0x00FF0000) >> 16);
  945. return 3;
  946. }
  947. /**@brief Function for encoding a uint32 value.
  948. *
  949. * @param[in] value Value to be encoded.
  950. * @param[out] p_encoded_data Buffer where the encoded data is to be written.
  951. *
  952. * @return Number of bytes written.
  953. */
  954. static __INLINE uint8_t uint32_encode(uint32_t value, uint8_t * p_encoded_data)
  955. {
  956. p_encoded_data[0] = (uint8_t) ((value & 0x000000FF) >> 0);
  957. p_encoded_data[1] = (uint8_t) ((value & 0x0000FF00) >> 8);
  958. p_encoded_data[2] = (uint8_t) ((value & 0x00FF0000) >> 16);
  959. p_encoded_data[3] = (uint8_t) ((value & 0xFF000000) >> 24);
  960. return sizeof(uint32_t);
  961. }
  962. /**@brief Function for encoding a uint40 value.
  963. *
  964. * @param[in] value Value to be encoded.
  965. * @param[out] p_encoded_data Buffer where the encoded data is to be written.
  966. *
  967. * @return Number of bytes written.
  968. */
  969. static __INLINE uint8_t uint40_encode(uint64_t value, uint8_t * p_encoded_data)
  970. {
  971. p_encoded_data[0] = (uint8_t) ((value & 0x00000000FF) >> 0);
  972. p_encoded_data[1] = (uint8_t) ((value & 0x000000FF00) >> 8);
  973. p_encoded_data[2] = (uint8_t) ((value & 0x0000FF0000) >> 16);
  974. p_encoded_data[3] = (uint8_t) ((value & 0x00FF000000) >> 24);
  975. p_encoded_data[4] = (uint8_t) ((value & 0xFF00000000) >> 32);
  976. return 5;
  977. }
  978. /**@brief Function for encoding a uint48 value.
  979. *
  980. * @param[in] value Value to be encoded.
  981. * @param[out] p_encoded_data Buffer where the encoded data is to be written.
  982. *
  983. * @return Number of bytes written.
  984. */
  985. static __INLINE uint8_t uint48_encode(uint64_t value, uint8_t * p_encoded_data)
  986. {
  987. p_encoded_data[0] = (uint8_t) ((value & 0x0000000000FF) >> 0);
  988. p_encoded_data[1] = (uint8_t) ((value & 0x00000000FF00) >> 8);
  989. p_encoded_data[2] = (uint8_t) ((value & 0x000000FF0000) >> 16);
  990. p_encoded_data[3] = (uint8_t) ((value & 0x0000FF000000) >> 24);
  991. p_encoded_data[4] = (uint8_t) ((value & 0x00FF00000000) >> 32);
  992. p_encoded_data[5] = (uint8_t) ((value & 0xFF0000000000) >> 40);
  993. return 6;
  994. }
  995. /**@brief Function for decoding a uint16 value.
  996. *
  997. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  998. *
  999. * @return Decoded value.
  1000. */
  1001. static __INLINE uint16_t uint16_decode(const uint8_t * p_encoded_data)
  1002. {
  1003. return ( (((uint16_t)((uint8_t *)p_encoded_data)[0])) |
  1004. (((uint16_t)((uint8_t *)p_encoded_data)[1]) << 8 ));
  1005. }
  1006. /**@brief Function for decoding a uint16 value in big-endian format.
  1007. *
  1008. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  1009. *
  1010. * @return Decoded value.
  1011. */
  1012. static __INLINE uint16_t uint16_big_decode(const uint8_t * p_encoded_data)
  1013. {
  1014. return ( (((uint16_t)((uint8_t *)p_encoded_data)[0]) << 8 ) |
  1015. (((uint16_t)((uint8_t *)p_encoded_data)[1])) );
  1016. }
  1017. /**@brief Function for decoding a three-byte value.
  1018. *
  1019. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  1020. *
  1021. * @return Decoded value (uint32_t).
  1022. */
  1023. static __INLINE uint32_t uint24_decode(const uint8_t * p_encoded_data)
  1024. {
  1025. return ( (((uint32_t)((uint8_t *)p_encoded_data)[0]) << 0) |
  1026. (((uint32_t)((uint8_t *)p_encoded_data)[1]) << 8) |
  1027. (((uint32_t)((uint8_t *)p_encoded_data)[2]) << 16));
  1028. }
  1029. /**@brief Function for decoding a uint32 value.
  1030. *
  1031. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  1032. *
  1033. * @return Decoded value.
  1034. */
  1035. static __INLINE uint32_t uint32_decode(const uint8_t * p_encoded_data)
  1036. {
  1037. return ( (((uint32_t)((uint8_t *)p_encoded_data)[0]) << 0) |
  1038. (((uint32_t)((uint8_t *)p_encoded_data)[1]) << 8) |
  1039. (((uint32_t)((uint8_t *)p_encoded_data)[2]) << 16) |
  1040. (((uint32_t)((uint8_t *)p_encoded_data)[3]) << 24 ));
  1041. }
  1042. /**@brief Function for decoding a uint32 value in big-endian format.
  1043. *
  1044. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  1045. *
  1046. * @return Decoded value.
  1047. */
  1048. static __INLINE uint32_t uint32_big_decode(const uint8_t * p_encoded_data)
  1049. {
  1050. return ( (((uint32_t)((uint8_t *)p_encoded_data)[0]) << 24) |
  1051. (((uint32_t)((uint8_t *)p_encoded_data)[1]) << 16) |
  1052. (((uint32_t)((uint8_t *)p_encoded_data)[2]) << 8) |
  1053. (((uint32_t)((uint8_t *)p_encoded_data)[3]) << 0) );
  1054. }
  1055. /**
  1056. * @brief Function for encoding an uint16 value in big-endian format.
  1057. *
  1058. * @param[in] value Value to be encoded.
  1059. * @param[out] p_encoded_data Buffer where the encoded data will be written.
  1060. *
  1061. * @return Number of bytes written.
  1062. */
  1063. static __INLINE uint8_t uint16_big_encode(uint16_t value, uint8_t * p_encoded_data)
  1064. {
  1065. p_encoded_data[0] = (uint8_t) (value >> 8);
  1066. p_encoded_data[1] = (uint8_t) (value & 0xFF);
  1067. return sizeof(uint16_t);
  1068. }
  1069. /*lint -esym(526, __rev) */
  1070. /*lint -esym(628, __rev) */
  1071. /**@brief Function for encoding a uint32 value in big-endian format.
  1072. *
  1073. * @param[in] value Value to be encoded.
  1074. * @param[out] p_encoded_data Buffer where the encoded data will be written.
  1075. * The address pointed to must be word alligned.
  1076. *
  1077. * @return Number of bytes written.
  1078. */
  1079. static __INLINE uint8_t uint32_big_encode(uint32_t value, uint8_t * p_encoded_data)
  1080. {
  1081. *(uint32_t *)p_encoded_data = __REV(value);
  1082. return sizeof(uint32_t);
  1083. }
  1084. /**@brief Function for decoding a uint40 value.
  1085. *
  1086. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  1087. *
  1088. * @return Decoded value. (uint64_t)
  1089. */
  1090. static __INLINE uint64_t uint40_decode(const uint8_t * p_encoded_data)
  1091. {
  1092. return ( (((uint64_t)((uint8_t *)p_encoded_data)[0]) << 0) |
  1093. (((uint64_t)((uint8_t *)p_encoded_data)[1]) << 8) |
  1094. (((uint64_t)((uint8_t *)p_encoded_data)[2]) << 16) |
  1095. (((uint64_t)((uint8_t *)p_encoded_data)[3]) << 24) |
  1096. (((uint64_t)((uint8_t *)p_encoded_data)[4]) << 32 ));
  1097. }
  1098. /**@brief Function for decoding a uint48 value.
  1099. *
  1100. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  1101. *
  1102. * @return Decoded value. (uint64_t)
  1103. */
  1104. static __INLINE uint64_t uint48_decode(const uint8_t * p_encoded_data)
  1105. {
  1106. return ( (((uint64_t)((uint8_t *)p_encoded_data)[0]) << 0) |
  1107. (((uint64_t)((uint8_t *)p_encoded_data)[1]) << 8) |
  1108. (((uint64_t)((uint8_t *)p_encoded_data)[2]) << 16) |
  1109. (((uint64_t)((uint8_t *)p_encoded_data)[3]) << 24) |
  1110. (((uint64_t)((uint8_t *)p_encoded_data)[4]) << 32) |
  1111. (((uint64_t)((uint8_t *)p_encoded_data)[5]) << 40 ));
  1112. }
  1113. /** @brief Function for converting the input voltage (in milli volts) into percentage of 3.0 Volts.
  1114. *
  1115. * @details The calculation is based on a linearized version of the battery's discharge
  1116. * curve. 3.0V returns 100% battery level. The limit for power failure is 2.1V and
  1117. * is considered to be the lower boundary.
  1118. *
  1119. * The discharge curve for CR2032 is non-linear. In this model it is split into
  1120. * 4 linear sections:
  1121. * - Section 1: 3.0V - 2.9V = 100% - 42% (58% drop on 100 mV)
  1122. * - Section 2: 2.9V - 2.74V = 42% - 18% (24% drop on 160 mV)
  1123. * - Section 3: 2.74V - 2.44V = 18% - 6% (12% drop on 300 mV)
  1124. * - Section 4: 2.44V - 2.1V = 6% - 0% (6% drop on 340 mV)
  1125. *
  1126. * These numbers are by no means accurate. Temperature and
  1127. * load in the actual application is not accounted for!
  1128. *
  1129. * @param[in] mvolts The voltage in mV
  1130. *
  1131. * @return Battery level in percent.
  1132. */
  1133. static __INLINE uint8_t battery_level_in_percent(const uint16_t mvolts)
  1134. {
  1135. uint8_t battery_level;
  1136. if (mvolts >= 3000)
  1137. {
  1138. battery_level = 100;
  1139. }
  1140. else if (mvolts > 2900)
  1141. {
  1142. battery_level = 100 - ((3000 - mvolts) * 58) / 100;
  1143. }
  1144. else if (mvolts > 2740)
  1145. {
  1146. battery_level = 42 - ((2900 - mvolts) * 24) / 160;
  1147. }
  1148. else if (mvolts > 2440)
  1149. {
  1150. battery_level = 18 - ((2740 - mvolts) * 12) / 300;
  1151. }
  1152. else if (mvolts > 2100)
  1153. {
  1154. battery_level = 6 - ((2440 - mvolts) * 6) / 340;
  1155. }
  1156. else
  1157. {
  1158. battery_level = 0;
  1159. }
  1160. return battery_level;
  1161. }
  1162. /**@brief Function for checking if a pointer value is aligned to a 4 byte boundary.
  1163. *
  1164. * @param[in] p Pointer value to be checked.
  1165. *
  1166. * @return TRUE if pointer is aligned to a 4 byte boundary, FALSE otherwise.
  1167. */
  1168. static __INLINE bool is_word_aligned(void const* p)
  1169. {
  1170. return (((uintptr_t)p & 0x03) == 0);
  1171. }
  1172. /*lint -e{568, 685} */
  1173. /**
  1174. * @brief Function for checking if provided address is located in stack space.
  1175. *
  1176. * @param[in] ptr Pointer to be checked.
  1177. *
  1178. * @return true if address is in stack space, false otherwise.
  1179. */
  1180. static __INLINE bool is_address_from_stack(void * ptr)
  1181. {
  1182. if (((uint32_t)ptr >= (uint32_t)STACK_BASE) &&
  1183. ((uint32_t)ptr < (uint32_t)STACK_TOP) )
  1184. {
  1185. return true;
  1186. }
  1187. else
  1188. {
  1189. return false;
  1190. }
  1191. }
  1192. #ifdef __cplusplus
  1193. }
  1194. #endif
  1195. #endif // APP_UTIL_H__
  1196. /** @} */