app_util.h 63 KB

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