I'm working on understanding Nordic's embedded system timer library. I found that they define preprocessor directives in a way that I don't understand and haven't seen before:
/** @brief The configuration structure of the timer driver instance. */
typedef struct
{
nrf_timer_frequency_t frequency; ///< Frequency.
nrf_timer_mode_t mode; ///< Mode of operation.
nrf_timer_bit_width_t bit_width; ///< Bit width.
uint8_t interrupt_priority; ///< Interrupt priority.
void * p_context; ///< Context passed to interrupt handler.
} nrfx_timer_config_t;
/** @brief Timer driver instance default configuration. */
#define NRFX_TIMER_DEFAULT_CONFIG \
{ \
.frequency = (nrf_timer_frequency_t)NRFX_TIMER_DEFAULT_CONFIG_FREQUENCY,\
.mode = (nrf_timer_mode_t)NRFX_TIMER_DEFAULT_CONFIG_MODE, \
.bit_width = (nrf_timer_bit_width_t)NRFX_TIMER_DEFAULT_CONFIG_BIT_WIDTH,\
.interrupt_priority = NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY, \
.p_context = NULL \
}
What I don't understand is the notation in their preprocessor directive #define NRFX_TIMER_DEFAULT_CONFIG
. The dot operator is used to access struct members but there is nothing to the left of the dot so how does the compiler know what struct to access? I would have expected something like:
nrfx_timer_config_t.frequency = NRFX_TIMER_DEFAULT_CONFIG_FREQUENCY, \
but there is no reference to the nrfx_timer_config_t
struct. I'm guessing the (nrf_timer_frequency_t)
is denoting membership somehow, but I don't understand how it does this since .interrupt_priority
and .p_context
don't seem to have any references to the nrfx_timer_config_t
struct at all. How does the compiler know where these values come from and how to assign them?
Also I'm not sure if this context is needed but nrf_timer_frequency_t
, nrf_timer_mode_t
, and nrf_timer_bit_width_t
are all enumerations.
Any help with understanding what is going on here would be greatly appreciated, thanks!