nrfx 3.3
Macros
Preprocessor utility macros

Preprocessor utility macros. More...

Macros

#define NRFX_COND_CODE_1(_flag, _if_1_code, _else_code)    _NRFX_COND_CODE_1(_flag, _if_1_code, _else_code)
 Macro for inserting code depending on whether _flag exists and expands to 1 or not.
 
#define NRFX_COND_CODE_0(_flag, _if_0_code, _else_code)    _NRFX_COND_CODE_0(_flag, _if_0_code, _else_code)
 Macro for inserting code depending on whether _flag exists and expands to 0 or not.
 
#define NRFX_IS_ENABLED(config_macro)   _NRFX_IS_ENABLED1(config_macro)
 Macro for checking for macro definition in compiler-visible expressions.
 
#define NRFX_LISTIFY(LEN, F, sep, ...)    NRFX_CONCAT_2(_NRFX_LISTIFY_, LEN)(F, sep, __VA_ARGS__)
 Macro for generating a sequence of code with configurable separator.
 
#define NRFX_IS_EMPTY(arg)   _NRFX_IS_EMPTY(arg)
 Macro for checking if input argument is empty.
 
#define NRFX_NUM_VA_ARGS_LESS_1(...)
 Macro for calculating number of arguments in the variable arguments list minus one.
 
#define NRFX_CONCAT(...)    NRFX_CONCAT_2(_NRFX_CONCAT_, NRFX_NUM_VA_ARGS_LESS_1(__VA_ARGS__))(__VA_ARGS__)
 Macro for concatenating multiple arguments.
 
#define NRFX_ARG_HAS_PARENTHESIS(x)   _NRFX_GET_ARG3(_NRFX_EVAL(_NRFX_ARG_HAS_PARENTHESIS x, 1, 0))
 Macro for checking if argument starts with opening round bracket and contains matching closing bracket (parenthesis).
 
#define NRFX_FOR_EACH(F, sep, ...)    _NRFX_FOR_EACH(F, sep, NRFX_REVERSE_ARGS(__VA_ARGS__))
 Macro for calling a macro F on each provided argument with a given separator between each call.
 
#define NRFX_FOR_EACH_IDX(F, sep, ...)    _NRFX_FOR_EACH_IDX(F, sep, NRFX_REVERSE_ARGS(__VA_ARGS__))
 Call macro F on each provided argument, with the argument's index as an additional parameter.
 
#define NRFX_FOR_EACH_FIXED_ARG(F, sep, fixed_arg, ...)    _NRFX_FOR_EACH_FIXED_ARG(F, sep, fixed_arg, NRFX_REVERSE_ARGS(__VA_ARGS__))
 Macro for calling macro F on each provided argument, with an additional fixed argument as a parameter.
 
#define NRFX_FOR_EACH_IDX_FIXED_ARG(F, sep, fixed_arg, ...)    _NRFX_FOR_EACH_IDX_FIXED_ARG(F, sep, fixed_arg, NRFX_REVERSE_ARGS(__VA_ARGS__))
 Macro from calling macro F for each variable argument with an index and fixed argument.
 
#define NRFX_REVERSE_ARGS(...)    _NRFX_FOR_EACH_ENGINE(_NRFX_FOR_EACH_EXEC, (,), NRFX_EVAL, _, __VA_ARGS__)
 Macro for reversing arguments order.
 
#define NRFX_MAX_N(...)
 Macro for getting the highest value from input arguments.
 

Detailed Description

Preprocessor utility macros.

Macro Definition Documentation

◆ NRFX_ARG_HAS_PARENTHESIS

#define NRFX_ARG_HAS_PARENTHESIS (   x)    _NRFX_GET_ARG3(_NRFX_EVAL(_NRFX_ARG_HAS_PARENTHESIS x, 1, 0))

Macro for checking if argument starts with opening round bracket and contains matching closing bracket (parenthesis).

Parameters
[in]xInput argument.
Return values
1If input argument starts with opening bracket and contains closing bracket.
0If input argument does not match above mentioned condition.

◆ NRFX_CONCAT

#define NRFX_CONCAT (   ...)     NRFX_CONCAT_2(_NRFX_CONCAT_, NRFX_NUM_VA_ARGS_LESS_1(__VA_ARGS__))(__VA_ARGS__)

Macro for concatenating multiple arguments.

Support up to 8 arguments.

Parameters
[in]...Arguments to concatenate.

◆ NRFX_COND_CODE_0

#define NRFX_COND_CODE_0 (   _flag,
  _if_0_code,
  _else_code 
)     _NRFX_COND_CODE_0(_flag, _if_0_code, _else_code)

Macro for inserting code depending on whether _flag exists and expands to 0 or not.

This is like NRFX_COND_CODE_1(), except that it tests whether _flag expands to the integer literal 0. It expands to _if_0_code if so, and _else_code otherwise; both of these must be enclosed in parentheses.

Parameters
[in]_flagEvaluated flag
[in]_if_0_codeResult if _flag expands to 0; must be in parentheses
[in]_else_codeResult otherwise; must be in parentheses

◆ NRFX_COND_CODE_1

#define NRFX_COND_CODE_1 (   _flag,
  _if_1_code,
  _else_code 
)     _NRFX_COND_CODE_1(_flag, _if_1_code, _else_code)

Macro for inserting code depending on whether _flag exists and expands to 1 or not.

To prevent the preprocessor from treating commas as argument separators, the _if_1_code and _else_code expressions must be inside brackets/parentheses: (). These are stripped away during macro expansion.

Example:

NRFX_COND_CODE_1(CONFIG_FLAG, (uint32_t x;), (there_is_no_flag();))

If CONFIG_FLAG is defined to 1, this expands to:

uint32_t x;

It expands to there_is_no_flag(); otherwise.

This could be used as an alternative to:

#if defined(CONFIG_FLAG) && (CONFIG_FLAG == 1)
#define MAYBE_DECLARE(x) uint32_t x
#else
#define MAYBE_DECLARE(x) there_is_no_flag()
#endif

MAYBE_DECLARE(x);

However, the advantage of COND_CODE_1() is that code is resolved in place where it is used, while the #if method defines MAYBE_DECLARE on two lines and requires it to be invoked again on a separate line. This makes COND_CODE_1() more concise and also sometimes more useful when used within another macro's expansion.

Note
_flag can be the result of preprocessor expansion, however _if_1_code is only expanded if _flag expands to the integer literal 1. Integer expressions that evaluate to 1, e.g. after doing some arithmetic, will not work.
Parameters
[in]_flagEvaluated flag
[in]_if_1_codeResult if _flag expands to 1; must be in parentheses
[in]_else_codeResult otherwise; must be in parentheses

◆ NRFX_FOR_EACH

#define NRFX_FOR_EACH (   F,
  sep,
  ... 
)     _NRFX_FOR_EACH(F, sep, NRFX_REVERSE_ARGS(__VA_ARGS__))

Macro for calling a macro F on each provided argument with a given separator between each call.

Example:

#define F(x) int a##x
NRFX_FOR_EACH(F, (;), 4, 5, 6);

This expands to:

int a4;
int a5;
int a6;
Parameters
FMacro to invoke
sepSeparator (e.g. comma or semicolon). Must be in parentheses; this is required to enable providing a comma as a separator.
...Variable argument list. The macro F is invoked as F(element) for each element in the list.

◆ NRFX_FOR_EACH_FIXED_ARG

#define NRFX_FOR_EACH_FIXED_ARG (   F,
  sep,
  fixed_arg,
  ... 
)     _NRFX_FOR_EACH_FIXED_ARG(F, sep, fixed_arg, NRFX_REVERSE_ARGS(__VA_ARGS__))

Macro for calling macro F on each provided argument, with an additional fixed argument as a parameter.

This is like NRFX_FOR_EACH(), except F should be a macro which takes two arguments: F(variable_arg, fixed_arg).

Example:

static void func(int val, void *dev);
NRFX_FOR_EACH_FIXED_ARG(func, (;), dev, 4, 5, 6);

This expands to:

func(4, dev);
func(5, dev);
func(6, dev);
Parameters
FMacro to invoke
sepSeparator (e.g. comma or semicolon). Must be in parentheses; this is required to enable providing a comma as a separator.
fixed_argFixed argument passed to F as the second macro parameter.
...Variable argument list. The macro F is invoked as F(element, fixed_arg) for each element in the list.

◆ NRFX_FOR_EACH_IDX

#define NRFX_FOR_EACH_IDX (   F,
  sep,
  ... 
)     _NRFX_FOR_EACH_IDX(F, sep, NRFX_REVERSE_ARGS(__VA_ARGS__))

Call macro F on each provided argument, with the argument's index as an additional parameter.

This is like NRFX_FOR_EACH(), except F should be a macro which takes two arguments: F(index, variable_arg).

Example:

#define F(idx, x) int a##idx = x
NRFX_FOR_EACH_IDX(F, (;), 4, 5, 6);

This expands to:

int a0 = 4;
int a1 = 5;
int a2 = 6;
Parameters
FMacro to invoke
sepSeparator (e.g. comma or semicolon). Must be in parentheses; this is required to enable providing a comma as a separator.
...Variable argument list. The macro F is invoked as F(index, element) for each element in the list.

◆ NRFX_FOR_EACH_IDX_FIXED_ARG

#define NRFX_FOR_EACH_IDX_FIXED_ARG (   F,
  sep,
  fixed_arg,
  ... 
)     _NRFX_FOR_EACH_IDX_FIXED_ARG(F, sep, fixed_arg, NRFX_REVERSE_ARGS(__VA_ARGS__))

Macro from calling macro F for each variable argument with an index and fixed argument.

This is like the combination of NRFX_FOR_EACH_IDX() with NRFX_FOR_EACH_FIXED_ARG().

Example:

#define F(idx, x, fixed_arg) int fixed_arg##idx = x
NRFX_FOR_EACH_IDX_FIXED_ARG(F, (;), a, 4, 5, 6);

This expands to:

int a0 = 4;
int a1 = 5;
int a2 = 6;
Parameters
FMacro to invoke
sepSeparator (e.g. comma or semicolon). Must be in parentheses; This is required to enable providing a comma as a separator.
fixed_argFixed argument passed to F as the third macro parameter.
...Variable list of arguments. The macro F is invoked as F(index, element, fixed_arg) for each element in the list.

◆ NRFX_IS_EMPTY

#define NRFX_IS_EMPTY (   arg)    _NRFX_IS_EMPTY(arg)

Macro for checking if input argument is empty.

Empty means that nothing is provided or provided value is resolved to nothing (e.g. empty define).

Macro idea is taken from P99 which is under Apache 2.0 license and described by Jens Gustedt https://gustedt.wordpress.com/2010/06/08/detect-empty-macro-arguments/

Parameters
argArgument.
Return values
1if argument is empty.
0if argument is not empty.

◆ NRFX_IS_ENABLED

#define NRFX_IS_ENABLED (   config_macro)    _NRFX_IS_ENABLED1(config_macro)

Macro for checking for macro definition in compiler-visible expressions.

It has the effect of taking a macro value that may be defined to "1" or may not be defined at all and turning it into a literal expression that can be handled by the C compiler instead of just the preprocessor.

That is, it works similarly to #if defined(CONFIG_FOO) except that its expansion is a C expression. Thus, much #ifdef usage can be replaced with equivalents like:

if (IS_ENABLED(CONFIG_FOO)) {
        do_something_with_foo
}

This is cleaner since the compiler can generate errors and warnings for do_something_with_foo even when CONFIG_FOO is undefined.

Parameters
[in]config_macroMacro to check
Returns
1 if config_macro is defined to 1, 0 otherwise (including if config_macro is not defined)

◆ NRFX_LISTIFY

#define NRFX_LISTIFY (   LEN,
  F,
  sep,
  ... 
)     NRFX_CONCAT_2(_NRFX_LISTIFY_, LEN)(F, sep, __VA_ARGS__)

Macro for generating a sequence of code with configurable separator.

Example:

#define FOO(i, _) MY_PWM ## i
{ NRFX_LISTIFY(PWM_COUNT, FOO, (,)) }

The above two lines expand to:

{ MY_PWM0 , MY_PWM1 }

Parameters
[in]LENThe length of the sequence. Must be an integer literal less than 255.
[in]FA macro function that accepts at least two arguments: F(i, ...). F is called repeatedly in the expansion. Its first argument i is the index in the sequence, and the variable list of arguments passed to LISTIFY are passed through to F.
[in]sepSeparator (e.g. comma or semicolon). Must be in parentheses; this is required to enable providing a comma as separator.
Note
Calling NRFX_LISTIFY with undefined arguments has undefined behavior.

◆ NRFX_MAX_N

#define NRFX_MAX_N (   ...)
Value:
NRFX_EVAL(NRFX_FOR_EACH(_NRFX_MAX_P1, (), __VA_ARGS__) 0 \
NRFX_FOR_EACH(_NRFX_MAX_P2, (), __VA_ARGS__))
#define NRFX_FOR_EACH(F, sep,...)
Macro for calling a macro F on each provided argument with a given separator between each call.
Definition: nrfx_utils.h:203

Macro for getting the highest value from input arguments.

It is similar to NRFX_MAX but accepts a variable number of arguments.

Note
Input arguments must be numeric variables.
Parameters
...Variable argument list.
Returns
Highest value from the input list.

◆ NRFX_NUM_VA_ARGS_LESS_1

#define NRFX_NUM_VA_ARGS_LESS_1 (   ...)
Value:
_NRFX_NUM_VA_ARGS_LESS_1_IMPL(__VA_ARGS__, 63, 62, 61, \
60, 59, 58, 57, 56, 55, 54, 53, 52, 51, \
50, 49, 48, 47, 46, 45, 44, 43, 42, 41, \
40, 39, 38, 37, 36, 35, 34, 33, 32, 31, \
30, 29, 28, 27, 26, 25, 24, 23, 22, 21, \
20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, ~)

Macro for calculating number of arguments in the variable arguments list minus one.

Parameters
[in]...List of arguments
Returns
Number of variadic arguments in the argument list, minus one

◆ NRFX_REVERSE_ARGS

#define NRFX_REVERSE_ARGS (   ...)     _NRFX_FOR_EACH_ENGINE(_NRFX_FOR_EACH_EXEC, (,), NRFX_EVAL, _, __VA_ARGS__)

Macro for reversing arguments order.

Parameters
...Variable argument list.
Returns
Input arguments in reversed order.

Documentation feedback | Developer Zone | Subscribe | Updated