Skip to content

Commit

Permalink
drivers: counter: legacy API support
Browse files Browse the repository at this point in the history
Added optional legacy API support.

Signed-off-by: Krzysztof Chruscinski <[email protected]>
  • Loading branch information
nordic-krch committed Nov 29, 2018
1 parent c78e4a1 commit e6eafe4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
5 changes: 5 additions & 0 deletions drivers/counter/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ menuconfig COUNTER

if COUNTER

config COUNTER_DEPRECATED_API_SUPPORT
bool "Enable legacy counter API"
help
If legacy API is used then counter_set_wrap() function cannot be used.

config COUNTER_LOG_LEVEL
int "Counter log level"
default 0
Expand Down
18 changes: 15 additions & 3 deletions drivers/counter/counter_nrfx_rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ LOG_MODULE_REGISTER();
#define COUNTER_WRAP_INT NRFX_RTC_INT_COMPARE0

struct counter_nrfx_data {
counter_wrap_callback_t wrap_cb;
union {
counter_wrap_callback_t wrap_cb;
counter_callback_t legacy_cb;
};
void *wrap_user_data;
u32_t wrap;
};
Expand Down Expand Up @@ -134,7 +137,12 @@ static int counter_nrfx_set_wrap(struct device *dev, u32_t ticks,
nrfx_rtc_cc_disable(rtc, WRAP_CH);
nrfx_rtc_counter_clear(rtc);

dev_data->wrap_cb = callback;
if (IS_ENABLED(CONFIG_COUNTER_DEPRECATED_API_SUPPORT)) {
dev_data->legacy_cb = (counter_callback_t)callback;
} else {
dev_data->wrap_cb = callback;
}

dev_data->wrap_user_data = user_data;
dev_data->wrap = ticks;
nrfx_rtc_cc_set(rtc, WRAP_CH, ticks, callback ? true : false);
Expand Down Expand Up @@ -177,7 +185,11 @@ static void event_handler(nrfx_rtc_int_type_t int_type, void *p_context)
}

if (data->wrap_cb) {
data->wrap_cb(dev, data->wrap_user_data);
if (IS_ENABLED(CONFIG_COUNTER_DEPRECATED_API_SUPPORT)) {
data->legacy_cb(dev, data->wrap_user_data);
} else {
data->wrap_cb(data->wrap_user_data);
}
}
} else if (int_type > COUNTER_WRAP_INT) {
alarm_event_handler(dev, CC_TO_ID(int_type));
Expand Down
17 changes: 12 additions & 5 deletions include/counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ struct counter_alarm_cfg {
*/
typedef void (*counter_wrap_callback_t)(struct device *dev, void *user_data);

/* Deprecated counter callback. */
typedef void (*counter_callback_t)(void *user_data);

/** @brief Structure with generic counter features.
*
* @param max_wrap Maximal (default) wrap value on which counter is reset
Expand Down Expand Up @@ -374,18 +377,22 @@ static inline u32_t _impl_counter_get_max_relative_alarm(struct device *dev)
return api->get_max_relative_alarm(dev);
}

/* Deprecated counter callback. */
typedef void (*counter_callback_t)(struct device *dev,
void *user_data);

/**
* @brief Deprecated function.
*/
__deprecated static inline int counter_set_alarm(struct device *dev,
counter_callback_t callback,
u32_t count, void *user_data)
{
return counter_set_wrap(dev, count, callback, user_data);
const struct counter_driver_api *api = dev->driver_api;

if (IS_ENABLED(CONFIG_COUNTER_DEPRECATED_API_SUPPORT)) {
return counter_set_wrap(dev, count,
(counter_wrap_callback_t)callback,
user_data);
}

return -ENOTSUP;
}

/**
Expand Down

0 comments on commit e6eafe4

Please sign in to comment.