drivers: counter: Add user_data to alarm callback

Modify alarm callback to return user_data and channel_id.
Set_alarm and disable_alarm updated accordingly. Renamed
counter_*_ch_alarm to counter_*_channel_alarm. Updated test
and nrf implementations.

Updated doxygen comments.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruscinski 2018-11-21 10:40:08 +01:00 committed by Anas Nashif
commit b745355315
4 changed files with 134 additions and 131 deletions

View file

@ -28,9 +28,14 @@ struct counter_nrfx_data {
u32_t wrap; u32_t wrap;
}; };
struct counter_nrfx_ch_data {
counter_alarm_callback_t callback;
void *user_data;
};
struct counter_nrfx_config { struct counter_nrfx_config {
struct counter_config_info info; struct counter_config_info info;
const struct counter_alarm_cfg **alarm_cfgs; struct counter_nrfx_ch_data *ch_data;
nrfx_rtc_t rtc; nrfx_rtc_t rtc;
LOG_INSTANCE_PTR_DECLARE(log); LOG_INSTANCE_PTR_DECLARE(log);
@ -67,7 +72,7 @@ static u32_t counter_nrfx_read(struct device *dev)
return nrfx_rtc_counter_get(&get_nrfx_config(dev)->rtc); 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_alarm_cfg *alarm_cfg)
{ {
const struct counter_nrfx_config *nrfx_config = get_nrfx_config(dev); const struct counter_nrfx_config *nrfx_config = get_nrfx_config(dev);
@ -78,7 +83,7 @@ static int counter_nrfx_set_alarm(struct device *dev,
return -EINVAL; return -EINVAL;
} }
if (nrfx_config->alarm_cfgs[alarm_cfg->channel_id]) { if (nrfx_config->ch_data[chan_id].callback) {
return -EBUSY; return -EBUSY;
} }
@ -86,9 +91,10 @@ static int counter_nrfx_set_alarm(struct device *dev,
0 : nrfx_rtc_counter_get(rtc)); 0 : nrfx_rtc_counter_get(rtc));
cc_val = (cc_val > get_dev_data(dev)->wrap) ? cc_val = (cc_val > get_dev_data(dev)->wrap) ?
(cc_val - get_dev_data(dev)->wrap) : cc_val; (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; return 0;
} }
@ -98,13 +104,12 @@ static void _disable(struct device *dev, u8_t id)
const struct counter_nrfx_config *config = get_nrfx_config(dev); const struct counter_nrfx_config *config = get_nrfx_config(dev);
nrfx_rtc_cc_disable(&config->rtc, ID_TO_CC(id)); 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, static int counter_nrfx_disable_alarm(struct device *dev, u8_t chan_id)
const struct counter_alarm_cfg *alarm_cfg)
{ {
_disable(dev, alarm_cfg->channel_id); _disable(dev, chan_id);
return 0; return 0;
} }
@ -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 /* Overflow can be changed only when all alarms are
* disables. * disables.
*/ */
if (nrfx_config->alarm_cfgs[i]) { if (nrfx_config->ch_data[i].callback) {
return -EBUSY; return -EBUSY;
} }
} }
@ -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) static void alarm_event_handler(struct device *dev, u32_t id)
{ {
const struct counter_nrfx_config *config = get_nrfx_config(dev); 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; u32_t cc_val;
if (!clbk) {
return;
}
cc_val = nrf_rtc_cc_get(config->rtc.p_reg, ID_TO_CC(id)); cc_val = nrf_rtc_cc_get(config->rtc.p_reg, ID_TO_CC(id));
_disable(dev, 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) static void event_handler(nrfx_rtc_int_type_t int_type, void *p_context)
@ -244,8 +253,8 @@ static const struct counter_driver_api counter_nrfx_driver_api = {
return init_rtc(dev, &config, rtc_##idx##_handler); \ return init_rtc(dev, &config, rtc_##idx##_handler); \
} \ } \
static struct counter_nrfx_data counter_##idx##_data; \ static struct counter_nrfx_data counter_##idx##_data; \
static const struct counter_alarm_cfg \ static struct counter_nrfx_ch_data \
*counter##idx##_alarm_cfgs[CC_TO_ID(RTC##idx##_CC_NUM)]; \ counter##idx##_ch_data[CC_TO_ID(RTC##idx##_CC_NUM)]; \
LOG_INSTANCE_REGISTER(LOG_MODULE_NAME, idx, CONFIG_COUNTER_LOG_LEVEL); \ LOG_INSTANCE_REGISTER(LOG_MODULE_NAME, idx, CONFIG_COUNTER_LOG_LEVEL); \
static const struct counter_nrfx_config nrfx_counter_##idx##_config = {\ static const struct counter_nrfx_config nrfx_counter_##idx##_config = {\
.info = { \ .info = { \
@ -255,7 +264,7 @@ static const struct counter_driver_api counter_nrfx_driver_api = {
.count_up = true, \ .count_up = true, \
.channels = CC_TO_ID(RTC##idx##_CC_NUM) \ .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), \ .rtc = NRFX_RTC_INSTANCE(idx), \
LOG_INSTANCE_PTR_INIT(log, LOG_MODULE_NAME, idx) \ LOG_INSTANCE_PTR_INIT(log, LOG_MODULE_NAME, idx) \
}; \ }; \

View file

@ -30,9 +30,14 @@ struct counter_nrfx_data {
void *wrap_user_data; void *wrap_user_data;
}; };
struct counter_nrfx_ch_data {
counter_alarm_callback_t callback;
void *user_data;
};
struct counter_nrfx_config { struct counter_nrfx_config {
struct counter_config_info info; struct counter_config_info info;
const struct counter_alarm_cfg **alarm_cfgs; struct counter_nrfx_ch_data *ch_data;
nrfx_timer_t timer; nrfx_timer_t timer;
LOG_INSTANCE_PTR_DECLARE(log); LOG_INSTANCE_PTR_DECLARE(log);
@ -70,7 +75,7 @@ static u32_t counter_nrfx_read(struct device *dev)
COUNTER_READ_CC); 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_alarm_cfg *alarm_cfg)
{ {
const struct counter_nrfx_config *nrfx_config = get_nrfx_config(dev); const struct counter_nrfx_config *nrfx_config = get_nrfx_config(dev);
@ -81,16 +86,16 @@ static int counter_nrfx_set_alarm(struct device *dev,
return -EINVAL; return -EINVAL;
} }
if (nrfx_config->alarm_cfgs[alarm_cfg->channel_id]) { if (nrfx_config->ch_data[chan_id].callback) {
return -EBUSY; return -EBUSY;
} }
cc_val = alarm_cfg->ticks + (alarm_cfg->absolute ? cc_val = alarm_cfg->ticks + (alarm_cfg->absolute ?
0 : nrfx_timer_capture(timer, COUNTER_READ_CC)); 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), nrfx_timer_compare(timer, ID_TO_CC(chan_id), cc_val, true);
cc_val, true);
return 0; return 0;
} }
@ -100,17 +105,17 @@ static void _disable(struct device *dev, u8_t id)
const struct counter_nrfx_config *config = get_nrfx_config(dev); const struct counter_nrfx_config *config = get_nrfx_config(dev);
nrfx_timer_compare_int_disable(&config->timer, ID_TO_CC(id)); 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, static int counter_nrfx_disable_alarm(struct device *dev,u8_t chan_id)
const struct counter_alarm_cfg *alarm_cfg)
{ {
_disable(dev, alarm_cfg->channel_id); _disable(dev, chan_id);
return 0; return 0;
} }
static int counter_nrfx_set_wrap(struct device *dev, u32_t ticks, static int counter_nrfx_set_wrap(struct device *dev, u32_t ticks,
counter_wrap_callback_t callback, counter_wrap_callback_t callback,
void *user_data) void *user_data)
@ -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 /* Overflow can be changed only when all alarms are
* disables. * disables.
*/ */
if (nrfx_config->alarm_cfgs[i]) { if (nrfx_config->ch_data[i].callback) {
return -EBUSY; return -EBUSY;
} }
} }
@ -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) static void alarm_event_handler(struct device *dev, u32_t id)
{ {
const nrfx_timer_t *timer = &get_nrfx_config(dev)->timer; const struct counter_nrfx_config *config = get_nrfx_config(dev);
const struct counter_alarm_cfg *alarm_cfg = counter_alarm_callback_t clbk = config->ch_data[id].callback;
get_nrfx_config(dev)->alarm_cfgs[id];
u32_t cc_val; u32_t cc_val;
if (!alarm_cfg->handler) { if (!clbk) {
return; 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); _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) static void event_handler(nrf_timer_event_t event_type, void *p_context)
@ -234,8 +238,8 @@ static const struct counter_driver_api counter_nrfx_driver_api = {
return init_timer(dev, &config); \ return init_timer(dev, &config); \
} \ } \
static struct counter_nrfx_data counter_##idx##_data; \ static struct counter_nrfx_data counter_##idx##_data; \
static const struct counter_alarm_cfg \ static struct counter_nrfx_ch_data \
*counter##idx##_alarm_cfgs[CC_TO_ID(TIMER##idx##_CC_NUM)]; \ counter##idx##_ch_data[CC_TO_ID(TIMER##idx##_CC_NUM)]; \
LOG_INSTANCE_REGISTER(LOG_MODULE_NAME, idx, CONFIG_COUNTER_LOG_LEVEL); \ LOG_INSTANCE_REGISTER(LOG_MODULE_NAME, idx, CONFIG_COUNTER_LOG_LEVEL); \
static const struct counter_nrfx_config nrfx_counter_##idx##_config = {\ static const struct counter_nrfx_config nrfx_counter_##idx##_config = {\
.info = { \ .info = { \
@ -246,7 +250,7 @@ static const struct counter_driver_api counter_nrfx_driver_api = {
.count_up = true, \ .count_up = true, \
.channels = CC_TO_ID(TIMER##idx##_CC_NUM), \ .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), \ .timer = NRFX_TIMER_INSTANCE(idx), \
LOG_INSTANCE_PTR_INIT(log, LOG_MODULE_NAME, idx) \ LOG_INSTANCE_PTR_INIT(log, LOG_MODULE_NAME, idx) \
}; \ }; \

View file

@ -29,45 +29,37 @@
extern "C" { extern "C" {
#endif #endif
struct counter_alarm_cfg;
/** @brief Alarm callback /** @brief Alarm callback
* *
* @param dev Pointer to the device structure for the driver instance. * @param dev Pointer to the device structure for the driver instance.
* @param cfg Original structure owning this handler. * @param chan_id Channel ID.
* @param ticks Counter value that triggered the callback. * @param ticks Counter value that triggered the callback.
* @param user_data User data.
*/ */
typedef void (*counter_alarm_callback_t)(struct device *dev, typedef void (*counter_alarm_callback_t)(struct device *dev,
const struct counter_alarm_cfg *cfg, u8_t chan_id, u32_t ticks,
u32_t ticks); void *user_data);
/** @brief Alarm callback structure. /** @brief Alarm callback structure.
* *
* Used to set an alarm. * @param callback Callback called on alarm (cannot be NULL).
* Beware such structure should not be allocated on stack. * @param ticks Ticks that triggers the alarm. In case of absolute flag is set,
* maximal value that can be set equals wrap value
* (counter_get_wrap). Otherwise counter_get_max_relative_alarm()
* returns maximal value that can be set. If counter is clock
* driven then ticks can be converted to microseconds (see @ref
* counter_ticks_to_us). 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.
*/ */
struct counter_alarm_cfg { struct counter_alarm_cfg {
counter_alarm_callback_t handler; /*!< handler called on alarm counter_alarm_callback_t callback;
* (cannot be NULL). u32_t ticks;
*/ void *user_data;
bool absolute;
u32_t ticks; /*!< In case of absolute flag is set, maximal value that
* can be set equals wrap value (counter_get_wrap).
* Otherwise counter_get_max_relative_alarm() returns
* maximal value that can be set. If counter is clock
* driven then ticks can be converted to microseconds
* (see @ref counter_ticks_to_us). Alternatively,
* counter implementation may count asynchronous events.
*/
u8_t channel_id; /*!< Channel ID. Number of channels is driver
* dependent.
*/
bool absolute; /*!< If true ticks are treated as absolute value, else
* it is relative to the counter reading performed
* during the call.
*/
}; };
/** @brief Wrap callback /** @brief Wrap callback
@ -77,32 +69,29 @@ struct counter_alarm_cfg {
*/ */
typedef void (*counter_wrap_callback_t)(struct device *dev, void *user_data); typedef void (*counter_wrap_callback_t)(struct device *dev, void *user_data);
/** @brief Structure with generic counter features. */ /** @brief Structure with generic counter features.
*
* @param max_wrap Maximal (default) wrap value on which counter is reset
* (cleared or reloaded).
* @param freq Frequency of the source clock if synchronous events are counted.
* @param count_up Flag indicating direction of the counter. If true counter is
* counting up else counting down.
* @param channels Number of channels that can be used for setting alarm,
* see @ref counter_set_alarm.
*/
struct counter_config_info { struct counter_config_info {
u32_t max_wrap; /*!< Maximal (default) wrap value on which counter is u32_t max_wrap;
* reset (cleared or reloaded). u32_t freq;
*/ bool count_up;
u8_t channels;
u32_t freq; /*!< Frequency of the source clock if synchronous events
* are counted.
*/
bool count_up; /*!< Flag indicating direction of the counter. If true
* counter is counting up else counting down.
*/
u8_t channels; /*!< Number of channels that can be used for setting
* alarm, see @ref counter_set_alarm.
*/
}; };
typedef int (*counter_api_start)(struct device *dev); typedef int (*counter_api_start)(struct device *dev);
typedef int (*counter_api_stop)(struct device *dev); typedef int (*counter_api_stop)(struct device *dev);
typedef u32_t (*counter_api_read)(struct device *dev); typedef u32_t (*counter_api_read)(struct device *dev);
typedef int (*counter_api_set_alarm)(struct device *dev, typedef int (*counter_api_set_alarm)(struct device *dev, u8_t chan_id,
const struct counter_alarm_cfg *alarm_cfg);
typedef int (*counter_api_disable_alarm)(struct device *dev,
const struct counter_alarm_cfg *alarm_cfg); const struct counter_alarm_cfg *alarm_cfg);
typedef int (*counter_api_disable_alarm)(struct device *dev, u8_t chan_id);
typedef int (*counter_api_set_wrap)(struct device *dev, u32_t ticks, typedef int (*counter_api_set_wrap)(struct device *dev, u32_t ticks,
counter_wrap_callback_t callback, counter_wrap_callback_t callback,
void *user_data); void *user_data);
@ -133,7 +122,7 @@ struct counter_driver_api {
* @retval true if counter is counting up. * @retval true if counter is counting up.
* @retval false if counter is counting down. * @retval false if counter is counting down.
*/ */
static inline bool counter_count_up(const struct device *dev) static inline bool counter_is_counting_up(const struct device *dev)
{ {
const struct counter_config_info *config = dev->config->config_info; const struct counter_config_info *config = dev->config->config_info;
@ -273,6 +262,7 @@ static inline u32_t _impl_counter_read(struct device *dev)
* counter_get_max_relative_alarm. * counter_get_max_relative_alarm.
* *
* @param dev Pointer to the device structure for the driver instance. * @param dev Pointer to the device structure for the driver instance.
* @param chan_id Channel ID.
* @param alarm_cfg Alarm configuration. * @param alarm_cfg Alarm configuration.
* *
* @retval 0 If successful. * @retval 0 If successful.
@ -280,39 +270,38 @@ static inline u32_t _impl_counter_read(struct device *dev)
* interrupts or requested channel). * interrupts or requested channel).
* @retval -EINVAL if alarm settings are invalid. * @retval -EINVAL if alarm settings are invalid.
*/ */
static inline int counter_set_ch_alarm(struct device *dev, static inline int counter_set_channel_alarm(struct device *dev, u8_t chan_id,
const struct counter_alarm_cfg *alarm_cfg) const struct counter_alarm_cfg *alarm_cfg)
{ {
const struct counter_driver_api *api = dev->driver_api; const struct counter_driver_api *api = dev->driver_api;
if (alarm_cfg->channel_id >= counter_get_num_of_channels(dev)) { if (chan_id >= counter_get_num_of_channels(dev)) {
return -ENOTSUP; return -ENOTSUP;
} }
return api->set_alarm(dev, alarm_cfg); return api->set_alarm(dev, chan_id, alarm_cfg);
} }
/** /**
* @brief Disable an alarm on a channel. * @brief Disable an alarm on a channel.
* *
* @param dev Pointer to the device structure for the driver instance. * @param dev Pointer to the device structure for the driver instance.
* @param alarm_cfg Alarm configuration. It must be the same address as the * @param chan_id Channel ID.
* one used for @ref counter_set_alarm.
* *
* @retval 0 If successful. * @retval 0 If successful.
* @retval -ENOTSUP if request is not supported or the counter was not started * @retval -ENOTSUP if request is not supported or the counter was not started
* yet. * yet.
*/ */
static inline int counter_disable_ch_alarm(struct device *dev, static inline int counter_disable_channel_alarm(struct device *dev,
const struct counter_alarm_cfg *alarm_cfg) u8_t chan_id)
{ {
const struct counter_driver_api *api = dev->driver_api; const struct counter_driver_api *api = dev->driver_api;
if (alarm_cfg->channel_id >= counter_get_num_of_channels(dev)) { if (chan_id >= counter_get_num_of_channels(dev)) {
return -ENOTSUP; return -ENOTSUP;
} }
return api->disable_alarm(dev, alarm_cfg); return api->disable_alarm(dev, chan_id);
} }
/** /**
@ -402,9 +391,12 @@ static inline u32_t _impl_counter_get_max_relative_alarm(struct device *dev)
return api->get_max_relative_alarm(dev); return api->get_max_relative_alarm(dev);
} }
/* Deprecated */ /* Deprecated counter callback. */
typedef void (*counter_callback_t)(struct device *dev, void *user_data); typedef void (*counter_callback_t)(struct device *dev, void *user_data);
/**
* @brief Deprecated function.
*/
__deprecated static inline int counter_set_alarm(struct device *dev, __deprecated static inline int counter_set_alarm(struct device *dev,
counter_callback_t callback, counter_callback_t callback,
u32_t count, void *user_data) u32_t count, void *user_data)

View file

@ -8,8 +8,8 @@
#include <ztest.h> #include <ztest.h>
#include <kernel.h> #include <kernel.h>
static volatile int wrap_cnt; static volatile u32_t wrap_cnt;
static volatile int alarm_cnt; static volatile u32_t alarm_cnt;
static void wrap_handler(struct device *dev, void *user_data); static void wrap_handler(struct device *dev, void *user_data);
@ -83,12 +83,11 @@ static void wrap_handler(struct device *dev, void *user_data)
wrap_cnt++; wrap_cnt++;
} }
#include <nrf_gpio.h>
void test_wrap_alarm_instance(const char *dev_name) void test_wrap_alarm_instance(const char *dev_name)
{ {
struct device *dev; struct device *dev;
int err; int err;
int cnt; u32_t cnt;
u32_t ticks; u32_t ticks;
wrap_cnt = 0; wrap_cnt = 0;
@ -120,11 +119,10 @@ void test_wrap_alarm(void)
test_all_instances(test_wrap_alarm_instance); test_all_instances(test_wrap_alarm_instance);
} }
static void alarm_handler(struct device *dev, static void alarm_handler(struct device *dev, u8_t chan_id, u32_t counter,
const struct counter_alarm_cfg *cfg, void *user_data)
u32_t counter)
{ {
zassert_true(&alarm_cfg == cfg, "Unexpected callback\n"); zassert_true(&alarm_cfg == user_data, "Unexpected callback\n");
alarm_cnt++; alarm_cnt++;
} }
@ -137,10 +135,10 @@ void test_single_shot_alarm_instance(const char *dev_name)
dev = device_get_binding(dev_name); dev = device_get_binding(dev_name);
ticks = counter_us_to_ticks(dev, WRAP_PERIOD_US); ticks = counter_us_to_ticks(dev, WRAP_PERIOD_US);
alarm_cfg.channel_id = 0;
alarm_cfg.absolute = true; alarm_cfg.absolute = true;
alarm_cfg.ticks = ticks + 1; alarm_cfg.ticks = ticks + 1;
alarm_cfg.handler = alarm_handler; alarm_cfg.callback = alarm_handler;
alarm_cfg.user_data = &alarm_cfg;
alarm_cnt = 0; alarm_cnt = 0;
@ -150,11 +148,11 @@ void test_single_shot_alarm_instance(const char *dev_name)
err = counter_set_wrap(dev, ticks, wrap_handler, exp_user_data); err = counter_set_wrap(dev, ticks, wrap_handler, exp_user_data);
zassert_equal(0, err, "Counter failed to set wrap\n"); zassert_equal(0, err, "Counter failed to set wrap\n");
err = counter_set_ch_alarm(dev, &alarm_cfg); err = counter_set_channel_alarm(dev, 0, &alarm_cfg);
zassert_equal(-EINVAL, err, "Counter should return error because ticks exceeded the limit set alarm\n"); zassert_equal(-EINVAL, err, "Counter should return error because ticks exceeded the limit set alarm\n");
alarm_cfg.ticks = ticks - 100; alarm_cfg.ticks = ticks - 100;
err = counter_set_ch_alarm(dev, &alarm_cfg);
err = counter_set_channel_alarm(dev, 0, &alarm_cfg);
zassert_equal(0, err, "Counter set alarm failed\n"); zassert_equal(0, err, "Counter set alarm failed\n");
k_busy_wait(1.2*counter_ticks_to_us(dev, ticks)); k_busy_wait(1.2*counter_ticks_to_us(dev, ticks));
@ -164,7 +162,7 @@ void test_single_shot_alarm_instance(const char *dev_name)
k_busy_wait(counter_ticks_to_us(dev, 2*ticks)); k_busy_wait(counter_ticks_to_us(dev, 2*ticks));
zassert_equal(1, alarm_cnt, "Expecting alarm callback\n"); zassert_equal(1, alarm_cnt, "Expecting alarm callback\n");
err = counter_disable_ch_alarm(dev, &alarm_cfg); err = counter_disable_channel_alarm(dev, 0);
zassert_equal(0, err, "Counter disabling alarm failed\n"); zassert_equal(0, err, "Counter disabling alarm failed\n");
err = counter_set_wrap(dev, counter_get_max_wrap(dev), NULL, NULL); err = counter_set_wrap(dev, counter_get_max_wrap(dev), NULL, NULL);
@ -179,13 +177,12 @@ void test_single_shot_alarm(void)
test_all_instances(test_single_shot_alarm_instance); test_all_instances(test_single_shot_alarm_instance);
} }
static const struct counter_alarm_cfg *clbks[2]; static void *clbk_data[2];
static void alarm_handler2(struct device *dev, static void alarm_handler2(struct device *dev, u8_t chan_id, u32_t counter,
const struct counter_alarm_cfg *cfg, void *user_data)
u32_t counter)
{ {
clbks[alarm_cnt] = cfg; clbk_data[alarm_cnt] = user_data;
alarm_cnt++; alarm_cnt++;
} }
@ -204,15 +201,15 @@ void test_multiple_alarms_instance(const char *dev_name)
dev = device_get_binding(dev_name); dev = device_get_binding(dev_name);
ticks = counter_us_to_ticks(dev, WRAP_PERIOD_US); ticks = counter_us_to_ticks(dev, WRAP_PERIOD_US);
alarm_cfg.channel_id = 0;
alarm_cfg.absolute = true; alarm_cfg.absolute = true;
alarm_cfg.ticks = counter_us_to_ticks(dev, 2000); alarm_cfg.ticks = counter_us_to_ticks(dev, 2000);
alarm_cfg.handler = alarm_handler2; alarm_cfg.callback = alarm_handler2;
alarm_cfg.user_data = &alarm_cfg;
alarm_cfg2.channel_id = 1;
alarm_cfg2.absolute = false; alarm_cfg2.absolute = false;
alarm_cfg2.ticks = counter_us_to_ticks(dev, 2000); alarm_cfg2.ticks = counter_us_to_ticks(dev, 2000);
alarm_cfg2.handler = alarm_handler2; alarm_cfg2.callback = alarm_handler2;
alarm_cfg2.user_data = &alarm_cfg2;
alarm_cnt = 0; alarm_cnt = 0;
@ -222,24 +219,26 @@ void test_multiple_alarms_instance(const char *dev_name)
err = counter_set_wrap(dev, ticks, wrap_handler, exp_user_data); err = counter_set_wrap(dev, ticks, wrap_handler, exp_user_data);
zassert_equal(0, err, "Counter failed to set wrap\n"); zassert_equal(0, err, "Counter failed to set wrap\n");
k_busy_wait(1.2*counter_ticks_to_us(dev, alarm_cfg.ticks)); k_busy_wait(1.4*counter_ticks_to_us(dev, alarm_cfg.ticks));
err = counter_set_ch_alarm(dev, &alarm_cfg); err = counter_set_channel_alarm(dev, 0, &alarm_cfg);
zassert_equal(0, err, "Counter set alarm failed\n"); zassert_equal(0, err, "Counter set alarm failed\n");
err = counter_set_ch_alarm(dev, &alarm_cfg2); err = counter_set_channel_alarm(dev, 1, &alarm_cfg2);
zassert_equal(0, err, "Counter set alarm failed\n"); zassert_equal(0, err, "Counter set alarm failed\n");
k_busy_wait(1.2*counter_ticks_to_us(dev, 2*ticks)); k_busy_wait(1.2*counter_ticks_to_us(dev, 2*ticks));
zassert_equal(2, alarm_cnt, "Counter set alarm failed\n"); zassert_equal(2, alarm_cnt, "Counter set alarm failed\n");
zassert_equal(&alarm_cfg2, clbks[0], "Expected different order or callbacks\n"); zassert_equal(&alarm_cfg2, clbk_data[0],
zassert_equal(&alarm_cfg, clbks[1], "Expected different order or callbacks\n"); "Expected different order or callbacks\n");
zassert_equal(&alarm_cfg, clbk_data[1],
"Expected different order or callbacks\n");
/* tear down */ /* tear down */
err = counter_disable_ch_alarm(dev, &alarm_cfg); err = counter_disable_channel_alarm(dev, 0);
zassert_equal(0, err, "Counter disabling alarm failed\n"); zassert_equal(0, err, "Counter disabling alarm failed\n");
err = counter_disable_ch_alarm(dev, &alarm_cfg2); err = counter_disable_channel_alarm(dev, 1);
zassert_equal(0, err, "Counter disabling alarm failed\n"); zassert_equal(0, err, "Counter disabling alarm failed\n");
} }
@ -262,17 +261,16 @@ void test_all_channels_instance(const char *str)
ticks = counter_us_to_ticks(dev, WRAP_PERIOD_US); ticks = counter_us_to_ticks(dev, WRAP_PERIOD_US);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
alarm_cfgs[i].channel_id = i;
alarm_cfgs[i].absolute = false; alarm_cfgs[i].absolute = false;
alarm_cfgs[i].ticks = ticks-100; alarm_cfgs[i].ticks = ticks-100;
alarm_cfgs[i].handler = alarm_handler2; alarm_cfgs[i].callback = alarm_handler2;
} }
err = counter_start(dev); err = counter_start(dev);
zassert_equal(0, err, "Counter failed to start"); zassert_equal(0, err, "Counter failed to start");
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
err = counter_set_ch_alarm(dev, &alarm_cfgs[i]); err = counter_set_channel_alarm(dev, i, &alarm_cfgs[i]);
if ((err == 0) && !limit_reached) { if ((err == 0) && !limit_reached) {
nchan++; nchan++;
} else if (err == -ENOTSUP) { } else if (err == -ENOTSUP) {
@ -283,12 +281,12 @@ void test_all_channels_instance(const char *str)
} }
for (int i = 0; i < nchan; i++) { for (int i = 0; i < nchan; i++) {
err = counter_disable_ch_alarm(dev, &alarm_cfgs[i]); err = counter_disable_channel_alarm(dev, i);
zassert_equal(0, err, "Unexpected error on disabling alarm"); zassert_equal(0, err, "Unexpected error on disabling alarm");
} }
for (int i = nchan; i < n; i++) { for (int i = nchan; i < n; i++) {
err = counter_disable_ch_alarm(dev, &alarm_cfgs[i]); err = counter_disable_channel_alarm(dev, i);
zassert_equal(-ENOTSUP, err, "Unexpected error on disabling alarm\n"); zassert_equal(-ENOTSUP, err, "Unexpected error on disabling alarm\n");
} }
} }