Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers: counter: Add user_data to alarm callback #11572

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions drivers/counter/counter_nrfx_rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ struct counter_nrfx_data {
u32_t wrap;
};

struct counter_nrfx_ch_data {
counter_alarm_callback_t callback;
void *user_data;
};

struct counter_nrfx_config {
struct counter_config_info info;
const struct counter_alarm_cfg **alarm_cfgs;
struct counter_nrfx_ch_data *ch_data;
nrfx_rtc_t rtc;

LOG_INSTANCE_PTR_DECLARE(log);
Expand Down Expand Up @@ -67,7 +72,7 @@ static u32_t counter_nrfx_read(struct device *dev)
return nrfx_rtc_counter_get(&get_nrfx_config(dev)->rtc);
}

static int counter_nrfx_set_alarm(struct device *dev,
static int counter_nrfx_set_alarm(struct device *dev, u8_t chan_id,
const struct counter_alarm_cfg *alarm_cfg)
{
const struct counter_nrfx_config *nrfx_config = get_nrfx_config(dev);
Expand All @@ -78,17 +83,18 @@ static int counter_nrfx_set_alarm(struct device *dev,
return -EINVAL;
}

if (nrfx_config->alarm_cfgs[alarm_cfg->channel_id]) {
if (nrfx_config->ch_data[chan_id].callback) {
return -EBUSY;
}

cc_val = alarm_cfg->ticks + (alarm_cfg->absolute ?
0 : nrfx_rtc_counter_get(rtc));
cc_val = (cc_val > get_dev_data(dev)->wrap) ?
(cc_val - get_dev_data(dev)->wrap) : cc_val;
nrfx_config->alarm_cfgs[alarm_cfg->channel_id] = alarm_cfg;
nrfx_config->ch_data[chan_id].callback = alarm_cfg->callback;
nrfx_config->ch_data[chan_id].user_data = alarm_cfg->user_data;

nrfx_rtc_cc_set(rtc, ID_TO_CC(alarm_cfg->channel_id), cc_val, true);
nrfx_rtc_cc_set(rtc, ID_TO_CC(chan_id), cc_val, true);

return 0;
}
Expand All @@ -98,13 +104,12 @@ static void _disable(struct device *dev, u8_t id)
const struct counter_nrfx_config *config = get_nrfx_config(dev);

nrfx_rtc_cc_disable(&config->rtc, ID_TO_CC(id));
config->alarm_cfgs[id] = NULL;
config->ch_data[id].callback = NULL;
}

static int counter_nrfx_disable_alarm(struct device *dev,
const struct counter_alarm_cfg *alarm_cfg)
static int counter_nrfx_disable_alarm(struct device *dev, u8_t chan_id)
{
_disable(dev, alarm_cfg->channel_id);
_disable(dev, chan_id);

return 0;
}
Expand All @@ -121,7 +126,7 @@ static int counter_nrfx_set_wrap(struct device *dev, u32_t ticks,
/* Overflow can be changed only when all alarms are
* disables.
*/
if (nrfx_config->alarm_cfgs[i]) {
if (nrfx_config->ch_data[i].callback) {
return -EBUSY;
}
}
Expand All @@ -145,13 +150,17 @@ static u32_t counter_nrfx_get_pending_int(struct device *dev)
static void alarm_event_handler(struct device *dev, u32_t id)
{
const struct counter_nrfx_config *config = get_nrfx_config(dev);
const struct counter_alarm_cfg *alarm_cfg = config->alarm_cfgs[id];
counter_alarm_callback_t clbk = config->ch_data[id].callback;
u32_t cc_val;

if (!clbk) {
return;
}

cc_val = nrf_rtc_cc_get(config->rtc.p_reg, ID_TO_CC(id));

_disable(dev, id);
alarm_cfg->handler(dev, alarm_cfg, cc_val);
clbk(dev, id, cc_val, config->ch_data[id].user_data);
}

static void event_handler(nrfx_rtc_int_type_t int_type, void *p_context)
Expand Down Expand Up @@ -244,8 +253,8 @@ static const struct counter_driver_api counter_nrfx_driver_api = {
return init_rtc(dev, &config, rtc_##idx##_handler); \
} \
static struct counter_nrfx_data counter_##idx##_data; \
static const struct counter_alarm_cfg \
*counter##idx##_alarm_cfgs[CC_TO_ID(RTC##idx##_CC_NUM)]; \
static struct counter_nrfx_ch_data \
counter##idx##_ch_data[CC_TO_ID(RTC##idx##_CC_NUM)]; \
LOG_INSTANCE_REGISTER(LOG_MODULE_NAME, idx, CONFIG_COUNTER_LOG_LEVEL); \
static const struct counter_nrfx_config nrfx_counter_##idx##_config = {\
.info = { \
Expand All @@ -255,7 +264,7 @@ static const struct counter_driver_api counter_nrfx_driver_api = {
.count_up = true, \
.channels = CC_TO_ID(RTC##idx##_CC_NUM) \
}, \
.alarm_cfgs = counter##idx##_alarm_cfgs, \
.ch_data = counter##idx##_ch_data, \
.rtc = NRFX_RTC_INSTANCE(idx), \
LOG_INSTANCE_PTR_INIT(log, LOG_MODULE_NAME, idx) \
}; \
Expand Down
44 changes: 24 additions & 20 deletions drivers/counter/counter_nrfx_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ struct counter_nrfx_data {
void *wrap_user_data;
};

struct counter_nrfx_ch_data {
counter_alarm_callback_t callback;
void *user_data;
};

struct counter_nrfx_config {
struct counter_config_info info;
const struct counter_alarm_cfg **alarm_cfgs;
struct counter_nrfx_ch_data *ch_data;
nrfx_timer_t timer;

LOG_INSTANCE_PTR_DECLARE(log);
Expand Down Expand Up @@ -70,7 +75,7 @@ static u32_t counter_nrfx_read(struct device *dev)
COUNTER_READ_CC);
}

static int counter_nrfx_set_alarm(struct device *dev,
static int counter_nrfx_set_alarm(struct device *dev, u8_t chan_id,
const struct counter_alarm_cfg *alarm_cfg)
{
const struct counter_nrfx_config *nrfx_config = get_nrfx_config(dev);
Expand All @@ -81,16 +86,16 @@ static int counter_nrfx_set_alarm(struct device *dev,
return -EINVAL;
}

if (nrfx_config->alarm_cfgs[alarm_cfg->channel_id]) {
if (nrfx_config->ch_data[chan_id].callback) {
return -EBUSY;
}

cc_val = alarm_cfg->ticks + (alarm_cfg->absolute ?
0 : nrfx_timer_capture(timer, COUNTER_READ_CC));
nrfx_config->alarm_cfgs[alarm_cfg->channel_id] = alarm_cfg;
nrfx_config->ch_data[chan_id].callback = alarm_cfg->callback;
nrfx_config->ch_data[chan_id].user_data = alarm_cfg->user_data;

nrfx_timer_compare(timer, ID_TO_CC(alarm_cfg->channel_id),
cc_val, true);
nrfx_timer_compare(timer, ID_TO_CC(chan_id), cc_val, true);

return 0;
}
Expand All @@ -100,17 +105,17 @@ static void _disable(struct device *dev, u8_t id)
const struct counter_nrfx_config *config = get_nrfx_config(dev);

nrfx_timer_compare_int_disable(&config->timer, ID_TO_CC(id));
config->alarm_cfgs[id] = NULL;
config->ch_data[id].callback = NULL;
}

static int counter_nrfx_disable_alarm(struct device *dev,
const struct counter_alarm_cfg *alarm_cfg)
static int counter_nrfx_disable_alarm(struct device *dev,u8_t chan_id)
{
_disable(dev, alarm_cfg->channel_id);
_disable(dev, chan_id);

return 0;
}


static int counter_nrfx_set_wrap(struct device *dev, u32_t ticks,
counter_wrap_callback_t callback,
void *user_data)
Expand All @@ -123,7 +128,7 @@ static int counter_nrfx_set_wrap(struct device *dev, u32_t ticks,
/* Overflow can be changed only when all alarms are
* disables.
*/
if (nrfx_config->alarm_cfgs[i]) {
if (nrfx_config->ch_data[i].callback) {
return -EBUSY;
}
}
Expand All @@ -147,18 +152,17 @@ static u32_t counter_nrfx_get_pending_int(struct device *dev)

static void alarm_event_handler(struct device *dev, u32_t id)
{
const nrfx_timer_t *timer = &get_nrfx_config(dev)->timer;
const struct counter_alarm_cfg *alarm_cfg =
get_nrfx_config(dev)->alarm_cfgs[id];
const struct counter_nrfx_config *config = get_nrfx_config(dev);
counter_alarm_callback_t clbk = config->ch_data[id].callback;
u32_t cc_val;

if (!alarm_cfg->handler) {
if (!clbk) {
return;
}

cc_val = nrfx_timer_capture_get(timer, ID_TO_CC(id));
cc_val = nrfx_timer_capture_get(&config->timer, ID_TO_CC(id));
_disable(dev, id);
alarm_cfg->handler(dev, alarm_cfg, cc_val);
clbk(dev, id, cc_val, config->ch_data[id].user_data);
}

static void event_handler(nrf_timer_event_t event_type, void *p_context)
Expand Down Expand Up @@ -234,8 +238,8 @@ static const struct counter_driver_api counter_nrfx_driver_api = {
return init_timer(dev, &config); \
} \
static struct counter_nrfx_data counter_##idx##_data; \
static const struct counter_alarm_cfg \
*counter##idx##_alarm_cfgs[CC_TO_ID(TIMER##idx##_CC_NUM)]; \
static struct counter_nrfx_ch_data \
counter##idx##_ch_data[CC_TO_ID(TIMER##idx##_CC_NUM)]; \
LOG_INSTANCE_REGISTER(LOG_MODULE_NAME, idx, CONFIG_COUNTER_LOG_LEVEL); \
static const struct counter_nrfx_config nrfx_counter_##idx##_config = {\
.info = { \
Expand All @@ -246,7 +250,7 @@ static const struct counter_driver_api counter_nrfx_driver_api = {
.count_up = true, \
.channels = CC_TO_ID(TIMER##idx##_CC_NUM), \
}, \
.alarm_cfgs = counter##idx##_alarm_cfgs, \
.ch_data = counter##idx##_ch_data, \
.timer = NRFX_TIMER_INSTANCE(idx), \
LOG_INSTANCE_PTR_INIT(log, LOG_MODULE_NAME, idx) \
}; \
Expand Down
Loading