app_util.h 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. /**
  2. * Copyright (c) 2012 - 2019, 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. #define STATIC_ASSERT_SIMPLE(EXPR) _Static_assert(EXPR, "unspecified message")
  193. #define STATIC_ASSERT_MSG(EXPR, MSG) _Static_assert(EXPR, MSG)
  194. #endif
  195. #ifdef __CC_ARM
  196. #define STATIC_ASSERT_SIMPLE(EXPR) extern char (*_do_assert(void)) [sizeof(char[1 - 2*!(EXPR)])]
  197. #define STATIC_ASSERT_MSG(EXPR, MSG) extern char (*_do_assert(void)) [sizeof(char[1 - 2*!(EXPR)])]
  198. #endif
  199. #ifdef __ICCARM__
  200. #define STATIC_ASSERT_SIMPLE(EXPR) static_assert(EXPR, "unspecified message")
  201. #define STATIC_ASSERT_MSG(EXPR, MSG) static_assert(EXPR, MSG)
  202. #endif
  203. #else // __LINT__
  204. #define STATIC_ASSERT_SIMPLE(EXPR) extern char (*_ignore(void))
  205. #define STATIC_ASSERT_MSG(EXPR, MSG) extern char (*_ignore(void))
  206. #endif
  207. #define _SELECT_ASSERT_FUNC(x, EXPR, MSG, ASSERT_MACRO, ...) ASSERT_MACRO
  208. /**
  209. * @brief Static (i.e. compile time) assert macro.
  210. *
  211. * @note The output of STATIC_ASSERT can be different across compilers.
  212. *
  213. * Usage:
  214. * STATIC_ASSERT(expression);
  215. * STATIC_ASSERT(expression, message);
  216. *
  217. * @hideinitializer
  218. */
  219. //lint -save -esym(???, STATIC_ASSERT)
  220. #define STATIC_ASSERT(...) \
  221. _SELECT_ASSERT_FUNC(x, ##__VA_ARGS__, \
  222. STATIC_ASSERT_MSG(__VA_ARGS__), \
  223. STATIC_ASSERT_SIMPLE(__VA_ARGS__))
  224. //lint -restore
  225. /**@brief Implementation details for NUM_VAR_ARGS */
  226. #define NUM_VA_ARGS_IMPL( \
  227. _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
  228. _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
  229. _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \
  230. _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \
  231. _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \
  232. _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, \
  233. _61, _62, N, ...) N
  234. /**@brief Macro to get the number of arguments in a call variadic macro call
  235. *
  236. * param[in] ... List of arguments
  237. *
  238. * @retval Number of variadic arguments in the argument list
  239. */
  240. #define NUM_VA_ARGS(...) NUM_VA_ARGS_IMPL(__VA_ARGS__, 63, 62, 61, \
  241. 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, \
  242. 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, \
  243. 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, \
  244. 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, \
  245. 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \
  246. 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
  247. /**@brief Implementation details for NUM_VAR_ARGS */
  248. #define NUM_VA_ARGS_LESS_1_IMPL( \
  249. _ignored, \
  250. _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
  251. _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
  252. _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \
  253. _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \
  254. _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \
  255. _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, \
  256. _61, _62, N, ...) N
  257. /**@brief Macro to get the number of arguments in a call variadic macro call.
  258. * First argument is not counted.
  259. *
  260. * param[in] ... List of arguments
  261. *
  262. * @retval Number of variadic arguments in the argument list
  263. */
  264. #define NUM_VA_ARGS_LESS_1(...) NUM_VA_ARGS_LESS_1_IMPL(__VA_ARGS__, 63, 62, 61, \
  265. 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, \
  266. 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, \
  267. 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, \
  268. 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, \
  269. 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \
  270. 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, ~)
  271. /**@brief type for holding an encoded (i.e. little endian) 16 bit unsigned integer. */
  272. typedef uint8_t uint16_le_t[2];
  273. /**@brief Type for holding an encoded (i.e. little endian) 32 bit unsigned integer. */
  274. typedef uint8_t uint32_le_t[4];
  275. /**@brief Byte array type. */
  276. typedef struct
  277. {
  278. uint16_t size; /**< Number of array entries. */
  279. uint8_t * p_data; /**< Pointer to array entries. */
  280. } uint8_array_t;
  281. /**@brief Macro for performing rounded integer division (as opposed to truncating the result).
  282. *
  283. * @param[in] A Numerator.
  284. * @param[in] B Denominator.
  285. *
  286. * @return Rounded (integer) result of dividing A by B.
  287. */
  288. #define ROUNDED_DIV(A, B) (((A) + ((B) / 2)) / (B))
  289. /**@brief Macro for checking if an integer is a power of two.
  290. *
  291. * @param[in] A Number to be tested.
  292. *
  293. * @return true if value is power of two.
  294. * @return false if value not power of two.
  295. */
  296. #define IS_POWER_OF_TWO(A) ( ((A) != 0) && ((((A) - 1) & (A)) == 0) )
  297. /**@brief Macro for converting milliseconds to ticks.
  298. *
  299. * @param[in] TIME Number of milliseconds to convert.
  300. * @param[in] RESOLUTION Unit to be converted to in [us/ticks].
  301. */
  302. #define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION))
  303. /**@brief Macro for performing integer division, making sure the result is rounded up.
  304. *
  305. * @details One typical use for this is to compute the number of objects with size B is needed to
  306. * hold A number of bytes.
  307. *
  308. * @param[in] A Numerator.
  309. * @param[in] B Denominator.
  310. *
  311. * @return Integer result of dividing A by B, rounded up.
  312. */
  313. #define CEIL_DIV(A, B) \
  314. (((A) + (B) - 1) / (B))
  315. /**@brief Macro for creating a buffer aligned to 4 bytes.
  316. *
  317. * @param[in] NAME Name of the buffor.
  318. * @param[in] MIN_SIZE Size of this buffor (it will be rounded up to multiples of 4 bytes).
  319. */
  320. #define WORD_ALIGNED_MEM_BUFF(NAME, MIN_SIZE) static uint32_t NAME[CEIL_DIV(MIN_SIZE, sizeof(uint32_t))]
  321. /**@brief Macro for calculating the number of words that are needed to hold a number of bytes.
  322. *
  323. * @details Adds 3 and divides by 4.
  324. *
  325. * @param[in] n_bytes The number of bytes.
  326. *
  327. * @return The number of words that @p n_bytes take up (rounded up).
  328. */
  329. #define BYTES_TO_WORDS(n_bytes) (((n_bytes) + 3) >> 2)
  330. /**@brief The number of bytes in a word.
  331. */
  332. #define BYTES_PER_WORD (4)
  333. /**@brief Macro for increasing a number to the nearest (larger) multiple of another number.
  334. *
  335. * @param[in] alignment The number to align to.
  336. * @param[in] number The number to align (increase).
  337. *
  338. * @return The aligned (increased) @p number.
  339. */
  340. #define ALIGN_NUM(alignment, number) (((number) - 1) + (alignment) - (((number) - 1) % (alignment)))
  341. /**@brief Macro for getting first of 2 parameters.
  342. *
  343. * @param[in] a1 First parameter.
  344. * @param[in] a2 Second parameter.
  345. */
  346. #define GET_ARG_1(a1, a2) a1
  347. /**@brief Macro for getting second of 2 parameters.
  348. *
  349. * @param[in] a1 First parameter.
  350. * @param[in] a2 Second parameter.
  351. */
  352. #define GET_ARG_2(a1, a2) a2
  353. /**@brief Container of macro (borrowed from Linux kernel).
  354. *
  355. * This macro returns parent structure address basing on child member address.
  356. *
  357. * @param ptr Address of child type.
  358. * @param type Type of parent structure.
  359. * @param member Name of child field in parent structure.
  360. *
  361. * @return Parent structure address.
  362. * */
  363. #define CONTAINER_OF(ptr, type, member) \
  364. (type *)((char *)ptr - offsetof(type, member))
  365. /**
  366. * @brief Define Bit-field mask
  367. *
  368. * Macro that defined the mask with selected number of bits set, starting from
  369. * provided bit number.
  370. *
  371. * @param[in] bcnt Number of bits in the bit-field
  372. * @param[in] boff Lowest bit number
  373. */
  374. #define BF_MASK(bcnt, boff) ( ((1U << (bcnt)) - 1U) << (boff) )
  375. /**
  376. * @brief Get bit-field
  377. *
  378. * Macro that extracts selected bit-field from provided value
  379. *
  380. * @param[in] val Value from which selected bit-field would be extracted
  381. * @param[in] bcnt Number of bits in the bit-field
  382. * @param[in] boff Lowest bit number
  383. *
  384. * @return Value of the selected bits
  385. */
  386. #define BF_GET(val, bcnt, boff) ( ( (val) & BF_MASK((bcnt), (boff)) ) >> (boff) )
  387. /**
  388. * @brief Create bit-field value
  389. *
  390. * Value is masked and shifted to match given bit-field
  391. *
  392. * @param[in] val Value to set on bit-field
  393. * @param[in] bcnt Number of bits for bit-field
  394. * @param[in] boff Offset of bit-field
  395. *
  396. * @return Value positioned of given bit-field.
  397. */
  398. #define BF_VAL(val, bcnt, boff) ( (((uint32_t)(val)) << (boff)) & BF_MASK(bcnt, boff) )
  399. /**
  400. * @name Configuration of complex bit-field
  401. *
  402. * @sa BF_CX
  403. * @{
  404. */
  405. /** @brief Position of bit count in complex bit-field value */
  406. #define BF_CX_BCNT_POS 0U
  407. /** @brief Mask of bit count in complex bit-field value */
  408. #define BF_CX_BCNT_MASK (0xffU << BF_CX_BCNT_POS)
  409. /** @brief Position of bit position in complex bit-field value */
  410. #define BF_CX_BOFF_POS 8U
  411. /** @brief Mask of bit position in complex bit-field value */
  412. #define BF_CX_BOFF_MASK (0xffU << BF_CX_BOFF_POS)
  413. /** @} */
  414. /**
  415. * @brief Define complex bit-field
  416. *
  417. * Complex bit-field would contain its position and size in one number.
  418. * @sa BF_CX_MASK
  419. * @sa BF_CX_POS
  420. * @sa BF_CX_GET
  421. *
  422. * @param[in] bcnt Number of bits in the bit-field
  423. * @param[in] boff Lowest bit number
  424. *
  425. * @return The single number that describes the bit-field completely.
  426. */
  427. #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) )
  428. /**
  429. * @brief Get number of bits in bit-field
  430. *
  431. * @sa BF_CX
  432. *
  433. * @param bf_cx Complex bit-field
  434. *
  435. * @return Number of bits in given bit-field
  436. */
  437. #define BF_CX_BCNT(bf_cx) ( ((bf_cx) & BF_CX_BCNT_MASK) >> BF_CX_BCNT_POS )
  438. /**
  439. * @brief Get lowest bit number in the field
  440. *
  441. * @sa BF_CX
  442. *
  443. * @param[in] bf_cx Complex bit-field
  444. *
  445. * @return Lowest bit number in given bit-field
  446. */
  447. #define BF_CX_BOFF(bf_cx) ( ((bf_cx) & BF_CX_BOFF_MASK) >> BF_CX_BOFF_POS )
  448. /**
  449. * @brief Get bit mask of the selected field
  450. *
  451. * @sa BF_CX
  452. *
  453. * @param[in] bf_cx Complex bit-field
  454. *
  455. * @return Mask of given bit-field
  456. */
  457. #define BF_CX_MASK(bf_cx) BF_MASK(BF_CX_BCNT(bf_cx), BF_CX_BOFF(bf_cx))
  458. /**
  459. * @brief Get bit-field
  460. *
  461. * Macro that extracts selected bit-field from provided value.
  462. * Bit-field is given as a complex value.
  463. *
  464. * @sa BF_CX
  465. * @sa BF_GET
  466. *
  467. * @param[in] val Value from which selected bit-field would be extracted
  468. * @param[in] bf_cx Complex bit-field
  469. *
  470. * @return Value of the selected bits.
  471. */
  472. #define BF_CX_GET(val, bf_cx) BF_GET(val, BF_CX_BCNT(bf_cx), BF_CX_BOFF(bf_cx))
  473. /**
  474. * @brief Create bit-field value
  475. *
  476. * Value is masked and shifted to match given bit-field.
  477. *
  478. * @param[in] val Value to set on bit-field
  479. * @param[in] bf_cx Complex bit-field
  480. *
  481. * @return Value positioned of given bit-field.
  482. */
  483. #define BF_CX_VAL(val, bf_cx) BF_VAL(val, BF_CX_BCNT(bf_cx), BF_CX_BOFF(bf_cx))
  484. /**
  485. * @brief Extracting data from the brackets
  486. *
  487. * This macro get rid of brackets around the argument.
  488. * It can be used to pass multiple arguments in logical one argument to a macro.
  489. * Call it with arguments inside brackets:
  490. * @code
  491. * #define ARGUMENTS (a, b, c)
  492. * BRACKET_EXTRACT(ARGUMENTS)
  493. * @endcode
  494. * It would produce:
  495. * @code
  496. * a, b, c
  497. * @endcode
  498. *
  499. * @param a Argument with anything inside brackets
  500. * @return Anything that appears inside the brackets of the argument
  501. *
  502. * @note
  503. * The argument of the macro have to be inside brackets.
  504. * In other case the compilation would fail.
  505. */
  506. #define BRACKET_EXTRACT(a) BRACKET_EXTRACT_(a)
  507. #define BRACKET_EXTRACT_(a) BRACKET_EXTRACT__ a
  508. #define BRACKET_EXTRACT__(...) __VA_ARGS__
  509. /**
  510. * @brief Check if number of parameters is more than 1
  511. *
  512. * @param ... Arguments to count
  513. *
  514. * @return 0 If argument count is <= 1
  515. * @return 1 If argument count is > 1
  516. *
  517. * @sa NUM_VA_ARGS
  518. * @sa NUM_IS_MORE_THAN_1
  519. */
  520. #define NUM_VA_ARGS_IS_MORE_THAN_1(...) NUM_IS_MORE_THAN_1(NUM_VA_ARGS(__VA_ARGS__))
  521. /**
  522. * @brief Check if given numeric value is bigger than 1
  523. *
  524. * This macro accepts numeric value, that may be the result of argument expansion.
  525. * This numeric value is then converted to 0 if it is lover than 1 or to 1 if
  526. * its value is higher than 1.
  527. * The generated result can be used to glue it into other macro mnemonic name.
  528. *
  529. * @param N Numeric value to check
  530. *
  531. * @return 0 If argument is <= 1
  532. * @return 1 If argument is > 1
  533. *
  534. * @note Any existing definition of a form NUM_IS_MORE_THAN_1_PROBE_[N] can
  535. * broke the result of this macro
  536. */
  537. #define NUM_IS_MORE_THAN_1(N) NUM_IS_MORE_THAN_1_(N)
  538. #define NUM_IS_MORE_THAN_1_(N) NUM_IS_MORE_THAN_1_PROBE_(NUM_IS_MORE_THAN_1_PROBE_ ## N, 1)
  539. #define NUM_IS_MORE_THAN_1_PROBE_(...) GET_VA_ARG_1(GET_ARGS_AFTER_1(__VA_ARGS__))
  540. #define NUM_IS_MORE_THAN_1_PROBE_0 ~, 0
  541. #define NUM_IS_MORE_THAN_1_PROBE_1 ~, 0
  542. /**
  543. * @brief Get the first argument
  544. *
  545. * @param ... Arguments to select
  546. *
  547. * @return First argument or empty if no arguments are provided
  548. */
  549. #define GET_VA_ARG_1(...) GET_VA_ARG_1_(__VA_ARGS__, ) // Make sure that also for 1 argument it works
  550. #define GET_VA_ARG_1_(a1, ...) a1
  551. /**
  552. * @brief Get all the arguments but the first one
  553. *
  554. * @param ... Arguments to select
  555. *
  556. * @return All arguments after the first one or empty if less than 2 arguments are provided
  557. */
  558. #define GET_ARGS_AFTER_1(...) GET_ARGS_AFTER_1_(__VA_ARGS__, ) // Make sure that also for 1 argument it works
  559. #define GET_ARGS_AFTER_1_(a1, ...) __VA_ARGS__
  560. /**
  561. * @brief Size of a field in declared structure
  562. *
  563. * Macro that returns the size of the structure field.
  564. * @param struct_type Variable type to get the field size from
  565. * @param field Field name to analyze. It can be even field inside field (field.somethingelse.and_another).
  566. *
  567. * @return Size of the field
  568. */
  569. #define FIELD_SIZE(struct_type, field) sizeof(((struct struct_type*)NULL)->field)
  570. /**
  571. * @brief Number of elements in field array in declared structure
  572. *
  573. * Macro that returns number of elementy in structure field.
  574. * @param struct_type Variable type to get the field size from
  575. * @param field Field name to analyze.
  576. *
  577. * @return Number of elements in field array
  578. *
  579. * @sa FIELD_SIZE
  580. */
  581. #define FIELD_ARRAY_SIZE(struct_type, field) (FIELD_SIZE(struct_type, field) / FIELD_SIZE(struct_type, field[0]))
  582. /**
  583. * @brief Mapping macro
  584. *
  585. * Macro that process all arguments using given macro
  586. *
  587. * @param ... Macro name to be used for argument processing followed by arguments to process.
  588. * Macro should have following form: MACRO(argument)
  589. *
  590. * @return All arguments processed by given macro
  591. */
  592. #define MACRO_MAP(...) MACRO_MAP_(__VA_ARGS__)
  593. #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
  594. /**
  595. * @brief Mapping macro, recursive version
  596. *
  597. * Can be used in @ref MACRO_MAP macro
  598. */
  599. #define MACRO_MAP_REC(...) MACRO_MAP_REC_(__VA_ARGS__)
  600. #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
  601. /**
  602. * @brief Mapping N arguments macro
  603. *
  604. * Macro similar to @ref MACRO_MAP but maps exact number of arguments.
  605. * If there is more arguments given, the rest would be ignored.
  606. *
  607. * @param N Number of arguments to map
  608. * @param ... Macro name to be used for argument processing followed by arguments to process.
  609. * Macro should have following form: MACRO(argument)
  610. *
  611. * @return Selected number of arguments processed by given macro
  612. */
  613. #define MACRO_MAP_N(N, ...) MACRO_MAP_N_(N, __VA_ARGS__)
  614. #define MACRO_MAP_N_(N, ...) CONCAT_2(MACRO_MAP_, N)(__VA_ARGS__, )
  615. /**
  616. * @brief Mapping N arguments macro, recursive version
  617. *
  618. * Can be used in @ref MACRO_MAP_N macro
  619. */
  620. #define MACRO_MAP_REC_N(N, ...) MACRO_MAP_REC_N_(N, __VA_ARGS__)
  621. #define MACRO_MAP_REC_N_(N, ...) CONCAT_2(MACRO_MAP_REC_, N)(__VA_ARGS__, )
  622. #define MACRO_MAP_0( ...)
  623. #define MACRO_MAP_1( macro, a, ...) macro(a)
  624. #define MACRO_MAP_2( macro, a, ...) macro(a) MACRO_MAP_1 (macro, __VA_ARGS__, )
  625. #define MACRO_MAP_3( macro, a, ...) macro(a) MACRO_MAP_2 (macro, __VA_ARGS__, )
  626. #define MACRO_MAP_4( macro, a, ...) macro(a) MACRO_MAP_3 (macro, __VA_ARGS__, )
  627. #define MACRO_MAP_5( macro, a, ...) macro(a) MACRO_MAP_4 (macro, __VA_ARGS__, )
  628. #define MACRO_MAP_6( macro, a, ...) macro(a) MACRO_MAP_5 (macro, __VA_ARGS__, )
  629. #define MACRO_MAP_7( macro, a, ...) macro(a) MACRO_MAP_6 (macro, __VA_ARGS__, )
  630. #define MACRO_MAP_8( macro, a, ...) macro(a) MACRO_MAP_7 (macro, __VA_ARGS__, )
  631. #define MACRO_MAP_9( macro, a, ...) macro(a) MACRO_MAP_8 (macro, __VA_ARGS__, )
  632. #define MACRO_MAP_10(macro, a, ...) macro(a) MACRO_MAP_9 (macro, __VA_ARGS__, )
  633. #define MACRO_MAP_11(macro, a, ...) macro(a) MACRO_MAP_10(macro, __VA_ARGS__, )
  634. #define MACRO_MAP_12(macro, a, ...) macro(a) MACRO_MAP_11(macro, __VA_ARGS__, )
  635. #define MACRO_MAP_13(macro, a, ...) macro(a) MACRO_MAP_12(macro, __VA_ARGS__, )
  636. #define MACRO_MAP_14(macro, a, ...) macro(a) MACRO_MAP_13(macro, __VA_ARGS__, )
  637. #define MACRO_MAP_15(macro, a, ...) macro(a) MACRO_MAP_14(macro, __VA_ARGS__, )
  638. #define MACRO_MAP_16(macro, a, ...) macro(a) MACRO_MAP_15(macro, __VA_ARGS__, )
  639. #define MACRO_MAP_17(macro, a, ...) macro(a) MACRO_MAP_16(macro, __VA_ARGS__, )
  640. #define MACRO_MAP_18(macro, a, ...) macro(a) MACRO_MAP_17(macro, __VA_ARGS__, )
  641. #define MACRO_MAP_19(macro, a, ...) macro(a) MACRO_MAP_18(macro, __VA_ARGS__, )
  642. #define MACRO_MAP_20(macro, a, ...) macro(a) MACRO_MAP_19(macro, __VA_ARGS__, )
  643. #define MACRO_MAP_21(macro, a, ...) macro(a) MACRO_MAP_20(macro, __VA_ARGS__, )
  644. #define MACRO_MAP_22(macro, a, ...) macro(a) MACRO_MAP_21(macro, __VA_ARGS__, )
  645. #define MACRO_MAP_23(macro, a, ...) macro(a) MACRO_MAP_22(macro, __VA_ARGS__, )
  646. #define MACRO_MAP_24(macro, a, ...) macro(a) MACRO_MAP_23(macro, __VA_ARGS__, )
  647. #define MACRO_MAP_25(macro, a, ...) macro(a) MACRO_MAP_24(macro, __VA_ARGS__, )
  648. #define MACRO_MAP_26(macro, a, ...) macro(a) MACRO_MAP_25(macro, __VA_ARGS__, )
  649. #define MACRO_MAP_27(macro, a, ...) macro(a) MACRO_MAP_26(macro, __VA_ARGS__, )
  650. #define MACRO_MAP_28(macro, a, ...) macro(a) MACRO_MAP_27(macro, __VA_ARGS__, )
  651. #define MACRO_MAP_29(macro, a, ...) macro(a) MACRO_MAP_28(macro, __VA_ARGS__, )
  652. #define MACRO_MAP_30(macro, a, ...) macro(a) MACRO_MAP_29(macro, __VA_ARGS__, )
  653. #define MACRO_MAP_31(macro, a, ...) macro(a) MACRO_MAP_30(macro, __VA_ARGS__, )
  654. #define MACRO_MAP_32(macro, a, ...) macro(a) MACRO_MAP_31(macro, __VA_ARGS__, )
  655. #define MACRO_MAP_REC_0( ...)
  656. #define MACRO_MAP_REC_1( macro, a, ...) macro(a)
  657. #define MACRO_MAP_REC_2( macro, a, ...) macro(a) MACRO_MAP_REC_1 (macro, __VA_ARGS__, )
  658. #define MACRO_MAP_REC_3( macro, a, ...) macro(a) MACRO_MAP_REC_2 (macro, __VA_ARGS__, )
  659. #define MACRO_MAP_REC_4( macro, a, ...) macro(a) MACRO_MAP_REC_3 (macro, __VA_ARGS__, )
  660. #define MACRO_MAP_REC_5( macro, a, ...) macro(a) MACRO_MAP_REC_4 (macro, __VA_ARGS__, )
  661. #define MACRO_MAP_REC_6( macro, a, ...) macro(a) MACRO_MAP_REC_5 (macro, __VA_ARGS__, )
  662. #define MACRO_MAP_REC_7( macro, a, ...) macro(a) MACRO_MAP_REC_6 (macro, __VA_ARGS__, )
  663. #define MACRO_MAP_REC_8( macro, a, ...) macro(a) MACRO_MAP_REC_7 (macro, __VA_ARGS__, )
  664. #define MACRO_MAP_REC_9( macro, a, ...) macro(a) MACRO_MAP_REC_8 (macro, __VA_ARGS__, )
  665. #define MACRO_MAP_REC_10(macro, a, ...) macro(a) MACRO_MAP_REC_9 (macro, __VA_ARGS__, )
  666. #define MACRO_MAP_REC_11(macro, a, ...) macro(a) MACRO_MAP_REC_10(macro, __VA_ARGS__, )
  667. #define MACRO_MAP_REC_12(macro, a, ...) macro(a) MACRO_MAP_REC_11(macro, __VA_ARGS__, )
  668. #define MACRO_MAP_REC_13(macro, a, ...) macro(a) MACRO_MAP_REC_12(macro, __VA_ARGS__, )
  669. #define MACRO_MAP_REC_14(macro, a, ...) macro(a) MACRO_MAP_REC_13(macro, __VA_ARGS__, )
  670. #define MACRO_MAP_REC_15(macro, a, ...) macro(a) MACRO_MAP_REC_14(macro, __VA_ARGS__, )
  671. #define MACRO_MAP_REC_16(macro, a, ...) macro(a) MACRO_MAP_REC_15(macro, __VA_ARGS__, )
  672. #define MACRO_MAP_REC_17(macro, a, ...) macro(a) MACRO_MAP_REC_16(macro, __VA_ARGS__, )
  673. #define MACRO_MAP_REC_18(macro, a, ...) macro(a) MACRO_MAP_REC_17(macro, __VA_ARGS__, )
  674. #define MACRO_MAP_REC_19(macro, a, ...) macro(a) MACRO_MAP_REC_18(macro, __VA_ARGS__, )
  675. #define MACRO_MAP_REC_20(macro, a, ...) macro(a) MACRO_MAP_REC_19(macro, __VA_ARGS__, )
  676. #define MACRO_MAP_REC_21(macro, a, ...) macro(a) MACRO_MAP_REC_20(macro, __VA_ARGS__, )
  677. #define MACRO_MAP_REC_22(macro, a, ...) macro(a) MACRO_MAP_REC_21(macro, __VA_ARGS__, )
  678. #define MACRO_MAP_REC_23(macro, a, ...) macro(a) MACRO_MAP_REC_22(macro, __VA_ARGS__, )
  679. #define MACRO_MAP_REC_24(macro, a, ...) macro(a) MACRO_MAP_REC_23(macro, __VA_ARGS__, )
  680. #define MACRO_MAP_REC_25(macro, a, ...) macro(a) MACRO_MAP_REC_24(macro, __VA_ARGS__, )
  681. #define MACRO_MAP_REC_26(macro, a, ...) macro(a) MACRO_MAP_REC_25(macro, __VA_ARGS__, )
  682. #define MACRO_MAP_REC_27(macro, a, ...) macro(a) MACRO_MAP_REC_26(macro, __VA_ARGS__, )
  683. #define MACRO_MAP_REC_28(macro, a, ...) macro(a) MACRO_MAP_REC_27(macro, __VA_ARGS__, )
  684. #define MACRO_MAP_REC_29(macro, a, ...) macro(a) MACRO_MAP_REC_28(macro, __VA_ARGS__, )
  685. #define MACRO_MAP_REC_30(macro, a, ...) macro(a) MACRO_MAP_REC_29(macro, __VA_ARGS__, )
  686. #define MACRO_MAP_REC_31(macro, a, ...) macro(a) MACRO_MAP_REC_30(macro, __VA_ARGS__, )
  687. #define MACRO_MAP_REC_32(macro, a, ...) macro(a) MACRO_MAP_REC_31(macro, __VA_ARGS__, )
  688. /**
  689. * @brief Mapping macro with current index
  690. *
  691. * Basically macro similar to @ref MACRO_MAP, but the processing function would get an argument
  692. * and current argument index (beginning from 0).
  693. *
  694. * @param ... Macro name to be used for argument processing followed by arguments to process.
  695. * Macro should have following form: MACRO(argument, index)
  696. * @return All arguments processed by given macro
  697. */
  698. #define MACRO_MAP_FOR(...) MACRO_MAP_FOR_(__VA_ARGS__)
  699. #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, \
  700. 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32
  701. #define MACRO_MAP_FOR_(...) MACRO_MAP_FOR_N(NUM_VA_ARGS_LESS_1(__VA_ARGS__), __VA_ARGS__)
  702. /**
  703. * @brief Mapping N arguments macro with current index
  704. *
  705. * Macro is similar to @ref MACRO_MAP_FOR but maps exact number of arguments.
  706. * If there is more arguments given, the rest would be ignored.
  707. *
  708. * @param N Number of arguments to map
  709. * @param ... Macro name to be used for argument processing followed by arguments to process.
  710. * Macro should have following form: MACRO(argument, index)
  711. *
  712. * @return Selected number of arguments processed by given macro
  713. */
  714. #define MACRO_MAP_FOR_N(N, ...) MACRO_MAP_FOR_N_(N, __VA_ARGS__)
  715. #define MACRO_MAP_FOR_N_(N, ...) CONCAT_2(MACRO_MAP_FOR_, N)((MACRO_MAP_FOR_N_LIST), __VA_ARGS__, )
  716. #define MACRO_MAP_FOR_0( n_list, ...)
  717. #define MACRO_MAP_FOR_1( n_list, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)))
  718. #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__, )
  719. #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__, )
  720. #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__, )
  721. #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__, )
  722. #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__, )
  723. #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__, )
  724. #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__, )
  725. #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__, )
  726. #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__, )
  727. #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__, )
  728. #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__, )
  729. #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__, )
  730. #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__, )
  731. #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__, )
  732. #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__, )
  733. #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__, )
  734. #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__, )
  735. #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__, )
  736. #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__, )
  737. #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__, )
  738. #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__, )
  739. #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__, )
  740. #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__, )
  741. #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__, )
  742. #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__, )
  743. #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__, )
  744. #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__, )
  745. #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__, )
  746. #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__, )
  747. #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__, )
  748. #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__, )
  749. /**
  750. * @brief Mapping macro with current index and parameter
  751. *
  752. * Version of @ref MACRO_MAP_FOR that passes also the same parameter to all macros.
  753. *
  754. * @param param Parameter that would be passed to each macro call during mapping.
  755. * @param ... Macro name to be used for argument processing followed by arguments to process.
  756. * Macro should have following form: MACRO(argument, index, param)
  757. *
  758. * @return All arguments processed by given macro
  759. */
  760. #define MACRO_MAP_FOR_PARAM(param, ...) MACRO_MAP_FOR_PARAM_(param, __VA_ARGS__)
  761. #define MACRO_MAP_FOR_PARAM_(param, ...) MACRO_MAP_FOR_PARAM_N(NUM_VA_ARGS_LESS_1(__VA_ARGS__), param, __VA_ARGS__)
  762. /**
  763. * @brief Mapping N arguments macro with with current index and parameter
  764. *
  765. * @param N Number of arguments to map
  766. * @param param Parameter that would be passed to each macro call during mapping.
  767. * @param ... Macro name to be used for argument processing followed by arguments to process.
  768. * Macro should have following form: MACRO(argument, index, param)
  769. *
  770. * @return All arguments processed by given macro
  771. */
  772. #define MACRO_MAP_FOR_PARAM_N(N, param, ...) MACRO_MAP_FOR_PARAM_N_(N, param, __VA_ARGS__)
  773. #define MACRO_MAP_FOR_PARAM_N_(N, param, ...) CONCAT_2(MACRO_MAP_FOR_PARAM_, N)((MACRO_MAP_FOR_N_LIST), param, __VA_ARGS__, )
  774. #define MACRO_MAP_FOR_PARAM_0( n_list, param, ...)
  775. #define MACRO_MAP_FOR_PARAM_1( n_list, param, macro, a, ...) macro(a, GET_VA_ARG_1(BRACKET_EXTRACT(n_list)), param)
  776. #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__, )
  777. #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__, )
  778. #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__, )
  779. #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__, )
  780. #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__, )
  781. #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__, )
  782. #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__, )
  783. #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__, )
  784. #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__, )
  785. #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__, )
  786. #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__, )
  787. #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__, )
  788. #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__, )
  789. #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__, )
  790. #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__, )
  791. #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__, )
  792. #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__, )
  793. #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__, )
  794. #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__, )
  795. #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__, )
  796. #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__, )
  797. #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__, )
  798. #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__, )
  799. #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__, )
  800. #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__, )
  801. #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__, )
  802. #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__, )
  803. #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__, )
  804. #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__, )
  805. #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__, )
  806. #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__, )
  807. /**
  808. * @brief Repeating macro.
  809. *
  810. * @param count Count of repeats.
  811. * @param macro Macro must have the following form: MACRO(arguments).
  812. * @param ... Arguments passed to the macro.
  813. *
  814. * @return All arguments processed by the given macro.
  815. */
  816. #define MACRO_REPEAT(count, macro, ...) MACRO_REPEAT_(count, macro, __VA_ARGS__)
  817. #define MACRO_REPEAT_(count, macro, ...) CONCAT_2(MACRO_REPEAT_, count)(macro, __VA_ARGS__)
  818. #define MACRO_REPEAT_0(macro, ...)
  819. #define MACRO_REPEAT_1(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_0(macro, __VA_ARGS__)
  820. #define MACRO_REPEAT_2(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_1(macro, __VA_ARGS__)
  821. #define MACRO_REPEAT_3(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_2(macro, __VA_ARGS__)
  822. #define MACRO_REPEAT_4(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_3(macro, __VA_ARGS__)
  823. #define MACRO_REPEAT_5(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_4(macro, __VA_ARGS__)
  824. #define MACRO_REPEAT_6(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_5(macro, __VA_ARGS__)
  825. #define MACRO_REPEAT_7(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_6(macro, __VA_ARGS__)
  826. #define MACRO_REPEAT_8(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_7(macro, __VA_ARGS__)
  827. #define MACRO_REPEAT_9(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_8(macro, __VA_ARGS__)
  828. #define MACRO_REPEAT_10(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_9(macro, __VA_ARGS__)
  829. #define MACRO_REPEAT_11(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_10(macro, __VA_ARGS__)
  830. #define MACRO_REPEAT_12(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_11(macro, __VA_ARGS__)
  831. #define MACRO_REPEAT_13(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_12(macro, __VA_ARGS__)
  832. #define MACRO_REPEAT_14(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_13(macro, __VA_ARGS__)
  833. #define MACRO_REPEAT_15(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_14(macro, __VA_ARGS__)
  834. #define MACRO_REPEAT_16(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_15(macro, __VA_ARGS__)
  835. #define MACRO_REPEAT_17(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_16(macro, __VA_ARGS__)
  836. #define MACRO_REPEAT_18(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_17(macro, __VA_ARGS__)
  837. #define MACRO_REPEAT_19(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_18(macro, __VA_ARGS__)
  838. #define MACRO_REPEAT_20(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_19(macro, __VA_ARGS__)
  839. #define MACRO_REPEAT_21(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_20(macro, __VA_ARGS__)
  840. #define MACRO_REPEAT_22(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_21(macro, __VA_ARGS__)
  841. #define MACRO_REPEAT_23(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_22(macro, __VA_ARGS__)
  842. #define MACRO_REPEAT_24(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_23(macro, __VA_ARGS__)
  843. #define MACRO_REPEAT_25(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_24(macro, __VA_ARGS__)
  844. #define MACRO_REPEAT_26(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_25(macro, __VA_ARGS__)
  845. #define MACRO_REPEAT_27(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_26(macro, __VA_ARGS__)
  846. #define MACRO_REPEAT_28(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_27(macro, __VA_ARGS__)
  847. #define MACRO_REPEAT_29(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_28(macro, __VA_ARGS__)
  848. #define MACRO_REPEAT_30(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_29(macro, __VA_ARGS__)
  849. #define MACRO_REPEAT_31(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_30(macro, __VA_ARGS__)
  850. #define MACRO_REPEAT_32(macro, ...) macro(__VA_ARGS__) MACRO_REPEAT_31(macro, __VA_ARGS__)
  851. /**
  852. * @brief Repeating macro with current index.
  853. *
  854. * Macro similar to @ref MACRO_REPEAT but the processing function gets the arguments
  855. * and the current argument index (beginning from 0).
  856. * @param count Count of repeats.
  857. * @param macro Macro must have the following form: MACRO(index, arguments).
  858. * @param ... Arguments passed to the macro.
  859. *
  860. * @return All arguments processed by the given macro.
  861. */
  862. #define MACRO_REPEAT_FOR(count, macro, ...) MACRO_REPEAT_FOR_(count, macro, __VA_ARGS__)
  863. #define MACRO_REPEAT_FOR_(count, macro, ...) CONCAT_2(MACRO_REPEAT_FOR_, count)((MACRO_MAP_FOR_N_LIST), macro, __VA_ARGS__)
  864. #define MACRO_REPEAT_FOR_0(n_list, macro, ...)
  865. #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__)
  866. #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__)
  867. #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__)
  868. #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__)
  869. #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__)
  870. #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__)
  871. #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__)
  872. #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__)
  873. #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__)
  874. #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__)
  875. #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__)
  876. #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__)
  877. #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__)
  878. #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__)
  879. #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__)
  880. #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__)
  881. #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__)
  882. #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__)
  883. #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__)
  884. #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__)
  885. #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__)
  886. #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__)
  887. #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__)
  888. #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__)
  889. #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__)
  890. #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__)
  891. #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__)
  892. #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__)
  893. #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__)
  894. #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__)
  895. #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__)
  896. #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__)
  897. /**@brief Adding curly brace to the macro parameter.
  898. *
  899. * Useful in array of structures initialization.
  900. *
  901. * @param p Parameter to put into the curly brace. */
  902. #define PARAM_CBRACE(p) { p },
  903. /**@brief Function for changing the value unit.
  904. *
  905. * @param[in] value Value to be rescaled.
  906. * @param[in] old_unit_reversal Reversal of the incoming unit.
  907. * @param[in] new_unit_reversal Reversal of the desired unit.
  908. *
  909. * @return Number of bytes written.
  910. */
  911. static __INLINE uint64_t value_rescale(uint32_t value, uint32_t old_unit_reversal, uint16_t new_unit_reversal)
  912. {
  913. return (uint64_t)ROUNDED_DIV((uint64_t)value * new_unit_reversal, old_unit_reversal);
  914. }
  915. /**@brief Function for encoding a uint16 value.
  916. *
  917. * @param[in] value Value to be encoded.
  918. * @param[out] p_encoded_data Buffer where the encoded data is to be written.
  919. *
  920. * @return Number of bytes written.
  921. */
  922. static __INLINE uint8_t uint16_encode(uint16_t value, uint8_t * p_encoded_data)
  923. {
  924. p_encoded_data[0] = (uint8_t) ((value & 0x00FF) >> 0);
  925. p_encoded_data[1] = (uint8_t) ((value & 0xFF00) >> 8);
  926. return sizeof(uint16_t);
  927. }
  928. /**@brief Function for encoding a three-byte value.
  929. *
  930. * @param[in] value Value to be encoded.
  931. * @param[out] p_encoded_data Buffer where the encoded data is to be written.
  932. *
  933. * @return Number of bytes written.
  934. */
  935. static __INLINE uint8_t uint24_encode(uint32_t value, uint8_t * p_encoded_data)
  936. {
  937. p_encoded_data[0] = (uint8_t) ((value & 0x000000FF) >> 0);
  938. p_encoded_data[1] = (uint8_t) ((value & 0x0000FF00) >> 8);
  939. p_encoded_data[2] = (uint8_t) ((value & 0x00FF0000) >> 16);
  940. return 3;
  941. }
  942. /**@brief Function for encoding a uint32 value.
  943. *
  944. * @param[in] value Value to be encoded.
  945. * @param[out] p_encoded_data Buffer where the encoded data is to be written.
  946. *
  947. * @return Number of bytes written.
  948. */
  949. static __INLINE uint8_t uint32_encode(uint32_t value, uint8_t * p_encoded_data)
  950. {
  951. p_encoded_data[0] = (uint8_t) ((value & 0x000000FF) >> 0);
  952. p_encoded_data[1] = (uint8_t) ((value & 0x0000FF00) >> 8);
  953. p_encoded_data[2] = (uint8_t) ((value & 0x00FF0000) >> 16);
  954. p_encoded_data[3] = (uint8_t) ((value & 0xFF000000) >> 24);
  955. return sizeof(uint32_t);
  956. }
  957. /**@brief Function for encoding a uint40 value.
  958. *
  959. * @param[in] value Value to be encoded.
  960. * @param[out] p_encoded_data Buffer where the encoded data is to be written.
  961. *
  962. * @return Number of bytes written.
  963. */
  964. static __INLINE uint8_t uint40_encode(uint64_t value, uint8_t * p_encoded_data)
  965. {
  966. p_encoded_data[0] = (uint8_t) ((value & 0x00000000FF) >> 0);
  967. p_encoded_data[1] = (uint8_t) ((value & 0x000000FF00) >> 8);
  968. p_encoded_data[2] = (uint8_t) ((value & 0x0000FF0000) >> 16);
  969. p_encoded_data[3] = (uint8_t) ((value & 0x00FF000000) >> 24);
  970. p_encoded_data[4] = (uint8_t) ((value & 0xFF00000000) >> 32);
  971. return 5;
  972. }
  973. /**@brief Function for encoding a uint48 value.
  974. *
  975. * @param[in] value Value to be encoded.
  976. * @param[out] p_encoded_data Buffer where the encoded data is to be written.
  977. *
  978. * @return Number of bytes written.
  979. */
  980. static __INLINE uint8_t uint48_encode(uint64_t value, uint8_t * p_encoded_data)
  981. {
  982. p_encoded_data[0] = (uint8_t) ((value & 0x0000000000FF) >> 0);
  983. p_encoded_data[1] = (uint8_t) ((value & 0x00000000FF00) >> 8);
  984. p_encoded_data[2] = (uint8_t) ((value & 0x000000FF0000) >> 16);
  985. p_encoded_data[3] = (uint8_t) ((value & 0x0000FF000000) >> 24);
  986. p_encoded_data[4] = (uint8_t) ((value & 0x00FF00000000) >> 32);
  987. p_encoded_data[5] = (uint8_t) ((value & 0xFF0000000000) >> 40);
  988. return 6;
  989. }
  990. /**@brief Function for decoding a uint16 value.
  991. *
  992. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  993. *
  994. * @return Decoded value.
  995. */
  996. static __INLINE uint16_t uint16_decode(const uint8_t * p_encoded_data)
  997. {
  998. return ( (((uint16_t)((uint8_t *)p_encoded_data)[0])) |
  999. (((uint16_t)((uint8_t *)p_encoded_data)[1]) << 8 ));
  1000. }
  1001. /**@brief Function for decoding a uint16 value in big-endian format.
  1002. *
  1003. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  1004. *
  1005. * @return Decoded value.
  1006. */
  1007. static __INLINE uint16_t uint16_big_decode(const uint8_t * p_encoded_data)
  1008. {
  1009. return ( (((uint16_t)((uint8_t *)p_encoded_data)[0]) << 8 ) |
  1010. (((uint16_t)((uint8_t *)p_encoded_data)[1])) );
  1011. }
  1012. /**@brief Function for decoding a three-byte value.
  1013. *
  1014. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  1015. *
  1016. * @return Decoded value (uint32_t).
  1017. */
  1018. static __INLINE uint32_t uint24_decode(const uint8_t * p_encoded_data)
  1019. {
  1020. return ( (((uint32_t)((uint8_t *)p_encoded_data)[0]) << 0) |
  1021. (((uint32_t)((uint8_t *)p_encoded_data)[1]) << 8) |
  1022. (((uint32_t)((uint8_t *)p_encoded_data)[2]) << 16));
  1023. }
  1024. /**@brief Function for decoding a uint32 value.
  1025. *
  1026. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  1027. *
  1028. * @return Decoded value.
  1029. */
  1030. static __INLINE uint32_t uint32_decode(const uint8_t * p_encoded_data)
  1031. {
  1032. return ( (((uint32_t)((uint8_t *)p_encoded_data)[0]) << 0) |
  1033. (((uint32_t)((uint8_t *)p_encoded_data)[1]) << 8) |
  1034. (((uint32_t)((uint8_t *)p_encoded_data)[2]) << 16) |
  1035. (((uint32_t)((uint8_t *)p_encoded_data)[3]) << 24 ));
  1036. }
  1037. /**@brief Function for decoding a uint32 value in big-endian format.
  1038. *
  1039. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  1040. *
  1041. * @return Decoded value.
  1042. */
  1043. static __INLINE uint32_t uint32_big_decode(const uint8_t * p_encoded_data)
  1044. {
  1045. return ( (((uint32_t)((uint8_t *)p_encoded_data)[0]) << 24) |
  1046. (((uint32_t)((uint8_t *)p_encoded_data)[1]) << 16) |
  1047. (((uint32_t)((uint8_t *)p_encoded_data)[2]) << 8) |
  1048. (((uint32_t)((uint8_t *)p_encoded_data)[3]) << 0) );
  1049. }
  1050. /**
  1051. * @brief Function for encoding an uint16 value in big-endian format.
  1052. *
  1053. * @param[in] value Value to be encoded.
  1054. * @param[out] p_encoded_data Buffer where the encoded data will be written.
  1055. *
  1056. * @return Number of bytes written.
  1057. */
  1058. static __INLINE uint8_t uint16_big_encode(uint16_t value, uint8_t * p_encoded_data)
  1059. {
  1060. p_encoded_data[0] = (uint8_t) (value >> 8);
  1061. p_encoded_data[1] = (uint8_t) (value & 0xFF);
  1062. return sizeof(uint16_t);
  1063. }
  1064. /*lint -esym(526, __rev) */
  1065. /*lint -esym(628, __rev) */
  1066. /**@brief Function for encoding a uint32 value in big-endian format.
  1067. *
  1068. * @param[in] value Value to be encoded.
  1069. * @param[out] p_encoded_data Buffer where the encoded data will be written.
  1070. * The address pointed to must be word alligned.
  1071. *
  1072. * @return Number of bytes written.
  1073. */
  1074. static __INLINE uint8_t uint32_big_encode(uint32_t value, uint8_t * p_encoded_data)
  1075. {
  1076. *(uint32_t *)p_encoded_data = __REV(value);
  1077. return sizeof(uint32_t);
  1078. }
  1079. /**@brief Function for decoding a uint40 value.
  1080. *
  1081. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  1082. *
  1083. * @return Decoded value. (uint64_t)
  1084. */
  1085. static __INLINE uint64_t uint40_decode(const uint8_t * p_encoded_data)
  1086. {
  1087. return ( (((uint64_t)((uint8_t *)p_encoded_data)[0]) << 0) |
  1088. (((uint64_t)((uint8_t *)p_encoded_data)[1]) << 8) |
  1089. (((uint64_t)((uint8_t *)p_encoded_data)[2]) << 16) |
  1090. (((uint64_t)((uint8_t *)p_encoded_data)[3]) << 24) |
  1091. (((uint64_t)((uint8_t *)p_encoded_data)[4]) << 32 ));
  1092. }
  1093. /**@brief Function for decoding a uint48 value.
  1094. *
  1095. * @param[in] p_encoded_data Buffer where the encoded data is stored.
  1096. *
  1097. * @return Decoded value. (uint64_t)
  1098. */
  1099. static __INLINE uint64_t uint48_decode(const uint8_t * p_encoded_data)
  1100. {
  1101. return ( (((uint64_t)((uint8_t *)p_encoded_data)[0]) << 0) |
  1102. (((uint64_t)((uint8_t *)p_encoded_data)[1]) << 8) |
  1103. (((uint64_t)((uint8_t *)p_encoded_data)[2]) << 16) |
  1104. (((uint64_t)((uint8_t *)p_encoded_data)[3]) << 24) |
  1105. (((uint64_t)((uint8_t *)p_encoded_data)[4]) << 32) |
  1106. (((uint64_t)((uint8_t *)p_encoded_data)[5]) << 40 ));
  1107. }
  1108. /** @brief Function for converting the input voltage (in milli volts) into percentage of 3.0 Volts.
  1109. *
  1110. * @details The calculation is based on a linearized version of the battery's discharge
  1111. * curve. 3.0V returns 100% battery level. The limit for power failure is 2.1V and
  1112. * is considered to be the lower boundary.
  1113. *
  1114. * The discharge curve for CR2032 is non-linear. In this model it is split into
  1115. * 4 linear sections:
  1116. * - Section 1: 3.0V - 2.9V = 100% - 42% (58% drop on 100 mV)
  1117. * - Section 2: 2.9V - 2.74V = 42% - 18% (24% drop on 160 mV)
  1118. * - Section 3: 2.74V - 2.44V = 18% - 6% (12% drop on 300 mV)
  1119. * - Section 4: 2.44V - 2.1V = 6% - 0% (6% drop on 340 mV)
  1120. *
  1121. * These numbers are by no means accurate. Temperature and
  1122. * load in the actual application is not accounted for!
  1123. *
  1124. * @param[in] mvolts The voltage in mV
  1125. *
  1126. * @return Battery level in percent.
  1127. */
  1128. static __INLINE uint8_t battery_level_in_percent(const uint16_t mvolts)
  1129. {
  1130. uint8_t battery_level;
  1131. if (mvolts >= 3000)
  1132. {
  1133. battery_level = 100;
  1134. }
  1135. else if (mvolts > 2900)
  1136. {
  1137. battery_level = 100 - ((3000 - mvolts) * 58) / 100;
  1138. }
  1139. else if (mvolts > 2740)
  1140. {
  1141. battery_level = 42 - ((2900 - mvolts) * 24) / 160;
  1142. }
  1143. else if (mvolts > 2440)
  1144. {
  1145. battery_level = 18 - ((2740 - mvolts) * 12) / 300;
  1146. }
  1147. else if (mvolts > 2100)
  1148. {
  1149. battery_level = 6 - ((2440 - mvolts) * 6) / 340;
  1150. }
  1151. else
  1152. {
  1153. battery_level = 0;
  1154. }
  1155. return battery_level;
  1156. }
  1157. /**@brief Function for checking if a pointer value is aligned to a 4 byte boundary.
  1158. *
  1159. * @param[in] p Pointer value to be checked.
  1160. *
  1161. * @return TRUE if pointer is aligned to a 4 byte boundary, FALSE otherwise.
  1162. */
  1163. static __INLINE bool is_word_aligned(void const* p)
  1164. {
  1165. return (((uintptr_t)p & 0x03) == 0);
  1166. }
  1167. /*lint -e{568, 685} */
  1168. /**
  1169. * @brief Function for checking if provided address is located in stack space.
  1170. *
  1171. * @param[in] ptr Pointer to be checked.
  1172. *
  1173. * @return true if address is in stack space, false otherwise.
  1174. */
  1175. static __INLINE bool is_address_from_stack(void * ptr)
  1176. {
  1177. if (((uint32_t)ptr >= (uint32_t)STACK_BASE) &&
  1178. ((uint32_t)ptr < (uint32_t)STACK_TOP) )
  1179. {
  1180. return true;
  1181. }
  1182. else
  1183. {
  1184. return false;
  1185. }
  1186. }
  1187. #ifdef __cplusplus
  1188. }
  1189. #endif
  1190. #endif // APP_UTIL_H__
  1191. /** @} */