drivers: counter: Add optional flags to alarm configuration structure

Flags in alarm configuration structure will allow further extention
without breaking API. Initially, existing absolute flag was added
as the only flag.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruscinski 2019-05-20 09:46:19 +02:00 committed by Maureen Helm
parent c8f7c329a2
commit a37fce6171
10 changed files with 27 additions and 18 deletions

View file

@ -172,7 +172,7 @@ static int counter_gecko_set_alarm(struct device *dev, u8_t chan_id,
return -EBUSY;
}
if (alarm_cfg->absolute) {
if ((alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE) != 0) {
ccv = alarm_cfg->ticks;
} else {
if (top_value == 0) {

View file

@ -42,7 +42,6 @@ struct rtc_stm32_data {
counter_alarm_callback_t callback;
u32_t ticks;
void *user_data;
bool absolute;
};
@ -129,9 +128,8 @@ static int rtc_stm32_set_alarm(struct device *dev, u8_t chan_id,
data->callback = alarm_cfg->callback;
data->user_data = alarm_cfg->user_data;
data->absolute = alarm_cfg->absolute;
if (!alarm_cfg->absolute) {
if ((alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE) == 0) {
ticks += now;
}

View file

@ -63,7 +63,7 @@ static int mcux_gpt_set_alarm(struct device *dev, u8_t chan_id,
return -EINVAL;
}
if (!alarm_cfg->absolute) {
if ((alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE) == 0) {
ticks += current;
}

View file

@ -101,7 +101,7 @@ static int mcux_rtc_set_alarm(struct device *dev, u8_t chan_id,
return -EBUSY;
}
if (!alarm_cfg->absolute) {
if ((alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE) == 0) {
ticks += current;
}

View file

@ -101,7 +101,7 @@ static int counter_nrfx_set_alarm(struct device *dev, u8_t chan_id,
return -EBUSY;
}
if (alarm_cfg->absolute) {
if ((alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE) != 0) {
cc_val = alarm_cfg->ticks;
} else {
/* As RTC is 24 bit there is no risk of overflow. */

View file

@ -99,7 +99,7 @@ static inline u32_t counter_nrfx_get_cc_value(struct device *dev,
u32_t cc_val;
u32_t ticks = alarm_cfg->ticks;
if (alarm_cfg->absolute) {
if ((alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE) != 0) {
return ticks;
}

View file

@ -194,7 +194,7 @@ static int counter_sam0_tc32_set_alarm(struct device *dev, u8_t chan_id,
data->ch.callback = alarm_cfg->callback;
data->ch.user_data = alarm_cfg->user_data;
if (alarm_cfg->absolute) {
if ((alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE) != 0) {
tc->CC[1].reg = alarm_cfg->ticks;
wait_synchronization(tc);
tc->INTFLAG.reg = TC_INTFLAG_MC1;

View file

@ -61,6 +61,19 @@ extern "C" {
/**@} */
/**@defgroup COUNTER_ALARM_FLAGS Counter device capabilities
* @{ */
/**
* @brief Counter alarm absolute value flag.
*
* Ticks relation to counter value. If set ticks are treated as absolute value,
* else it is relative to the counter reading performed during the call.
*/
#define COUNTER_ALARM_CFG_ABSOLUTE BIT(0)
/**@} */
/** @brief Alarm callback
*
* @param dev Pointer to the device structure for the driver instance.
@ -84,15 +97,13 @@ typedef void (*counter_alarm_callback_t)(struct device *dev,
* Alternatively, counter implementation may count asynchronous
* events.
* @param user_data User data returned in callback.
* @param absolute Ticks relation to counter value. If true ticks are treated as
* absolute value, else it is relative to the counter reading
* performed during the call.
* @param flags Alarm flags.
*/
struct counter_alarm_cfg {
counter_alarm_callback_t callback;
u32_t ticks;
void *user_data;
bool absolute;
u32_t flags;
};
/** @brief Callback called when counter turns around.

View file

@ -47,7 +47,7 @@ void main(void)
counter_start(counter_dev);
alarm_cfg.absolute = false;
alarm_cfg.flags = 0;
alarm_cfg.ticks = counter_us_to_ticks(counter_dev, DELAY);
alarm_cfg.callback = test_counter_interrupt_fn;
alarm_cfg.user_data = &alarm_cfg;

View file

@ -200,7 +200,7 @@ void test_single_shot_alarm_instance(const char *dev_name, bool set_top)
ticks = counter_us_to_ticks(dev, COUNTER_PERIOD_US);
top_cfg.ticks = ticks;
alarm_cfg.absolute = false;
alarm_cfg.flags = 0;
alarm_cfg.ticks = ticks;
alarm_cfg.callback = alarm_handler;
alarm_cfg.user_data = &alarm_cfg;
@ -312,12 +312,12 @@ void test_multiple_alarms_instance(const char *dev_name)
ticks = counter_us_to_ticks(dev, COUNTER_PERIOD_US);
top_cfg.ticks = ticks;
alarm_cfg.absolute = true;
alarm_cfg.flags = COUNTER_ALARM_CFG_ABSOLUTE;
alarm_cfg.ticks = counter_us_to_ticks(dev, 2000);
alarm_cfg.callback = alarm_handler2;
alarm_cfg.user_data = &alarm_cfg;
alarm_cfg2.absolute = false;
alarm_cfg2.flags = 0;
alarm_cfg2.ticks = counter_us_to_ticks(dev, 2000);
alarm_cfg2.callback = alarm_handler2;
alarm_cfg2.user_data = &alarm_cfg2;
@ -382,7 +382,7 @@ void test_all_channels_instance(const char *dev_name)
dev = device_get_binding(dev_name);
ticks = counter_us_to_ticks(dev, COUNTER_PERIOD_US);
alarm_cfgs.absolute = false;
alarm_cfgs.flags = 0;
alarm_cfgs.ticks = ticks;
alarm_cfgs.callback = alarm_handler2;
alarm_cfgs.user_data = NULL;