diff --git a/drivers/counter/counter_handlers.c b/drivers/counter/counter_handlers.c index cca16156347972..4d4c6cf91612ba 100644 --- a/drivers/counter/counter_handlers.c +++ b/drivers/counter/counter_handlers.c @@ -21,5 +21,5 @@ COUNTER_HANDLER(get_pending_int) COUNTER_HANDLER(read) COUNTER_HANDLER(stop) COUNTER_HANDLER(start) -COUNTER_HANDLER(get_wrap) +COUNTER_HANDLER(get_top_value) COUNTER_HANDLER(get_max_relative_alarm) diff --git a/drivers/counter/counter_nrfx_rtc.c b/drivers/counter/counter_nrfx_rtc.c index 9cc9c104ab8636..f7b11f1eac991e 100644 --- a/drivers/counter/counter_nrfx_rtc.c +++ b/drivers/counter/counter_nrfx_rtc.c @@ -14,23 +14,28 @@ LOG_MODULE_REGISTER(); #define RTC_CLOCK 32768 -#define COUNTER_MAX_WRAP RTC_COUNTER_COUNTER_Msk +#define COUNTER_MAX_TOP_VALUE RTC_COUNTER_COUNTER_Msk #define CC_TO_ID(cc) ((cc) - 1) #define ID_TO_CC(id) ((id) + 1) -#define WRAP_CH 0 -#define COUNTER_WRAP_INT NRFX_RTC_INT_COMPARE0 +#define TOP_CH 0 +#define COUNTER_TOP_INT NRFX_RTC_INT_COMPARE0 struct counter_nrfx_data { - counter_wrap_callback_t wrap_cb; - void *wrap_user_data; - u32_t wrap; + counter_top_callback_t top_cb; + void *top_user_data; + u32_t top; +}; + +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); @@ -67,28 +72,29 @@ 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); const nrfx_rtc_t *rtc = &nrfx_config->rtc; u32_t cc_val; - if (alarm_cfg->ticks > get_dev_data(dev)->wrap) { + if (alarm_cfg->ticks > get_dev_data(dev)->top) { 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; + cc_val = (cc_val > get_dev_data(dev)->top) ? + (cc_val - get_dev_data(dev)->top) : cc_val; + 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; } @@ -98,20 +104,19 @@ 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; } -static int counter_nrfx_set_wrap(struct device *dev, u32_t ticks, - counter_wrap_callback_t callback, - void *user_data) +static int counter_nrfx_set_top_value(struct device *dev, u32_t ticks, + counter_top_callback_t callback, + void *user_data) { const struct counter_nrfx_config *nrfx_config = get_nrfx_config(dev); const nrfx_rtc_t *rtc = &nrfx_config->rtc; @@ -121,18 +126,18 @@ 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; } } - nrfx_rtc_cc_disable(rtc, WRAP_CH); + nrfx_rtc_cc_disable(rtc, TOP_CH); nrfx_rtc_counter_clear(rtc); - 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); + dev_data->top_cb = callback; + dev_data->top_user_data = user_data; + dev_data->top = ticks; + nrfx_rtc_cc_set(rtc, TOP_CH, ticks, callback ? true : false); return 0; } @@ -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) @@ -159,18 +168,18 @@ static void event_handler(nrfx_rtc_int_type_t int_type, void *p_context) struct device *dev = p_context; struct counter_nrfx_data *data = get_dev_data(dev); - if (int_type == COUNTER_WRAP_INT) { - /* Manually reset counter when wrap is different than max wrap*/ - if (data->wrap != COUNTER_MAX_WRAP) { + if (int_type == COUNTER_TOP_INT) { + /* Manually reset counter if top value is different than max. */ + if (data->top != COUNTER_MAX_TOP_VALUE) { nrfx_rtc_counter_clear(&get_nrfx_config(dev)->rtc); nrfx_rtc_cc_set(&get_nrfx_config(dev)->rtc, - WRAP_CH, data->wrap, true); + TOP_CH, data->top, true); } - if (data->wrap_cb) { - data->wrap_cb(dev, data->wrap_user_data); + if (data->top_cb) { + data->top_cb(dev, data->top_user_data); } - } else if (int_type > COUNTER_WRAP_INT) { + } else if (int_type > COUNTER_TOP_INT) { alarm_event_handler(dev, CC_TO_ID(int_type)); } @@ -198,21 +207,21 @@ static int init_rtc(struct device *dev, return -EBUSY; } - get_dev_data(dev)->wrap = COUNTER_MAX_WRAP; + get_dev_data(dev)->top = COUNTER_MAX_TOP_VALUE; LOG_INST_DBG(nrfx_config->log, "Initialized"); return 0; } -static u32_t counter_nrfx_get_wrap(struct device *dev) +static u32_t counter_nrfx_get_top_value(struct device *dev) { - return get_dev_data(dev)->wrap; + return get_dev_data(dev)->top; } static u32_t counter_nrfx_get_max_relative_alarm(struct device *dev) { /* Maybe decreased. */ - return get_dev_data(dev)->wrap; + return get_dev_data(dev)->top; } static const struct counter_driver_api counter_nrfx_driver_api = { @@ -221,9 +230,9 @@ static const struct counter_driver_api counter_nrfx_driver_api = { .read = counter_nrfx_read, .set_alarm = counter_nrfx_set_alarm, .disable_alarm = counter_nrfx_disable_alarm, - .set_wrap = counter_nrfx_set_wrap, + .set_top_value = counter_nrfx_set_top_value, .get_pending_int = counter_nrfx_get_pending_int, - .get_wrap = counter_nrfx_get_wrap, + .get_top_value = counter_nrfx_get_top_value, .get_max_relative_alarm = counter_nrfx_get_max_relative_alarm, }; @@ -244,18 +253,18 @@ 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 = { \ - .max_wrap = COUNTER_MAX_WRAP, \ + .max_top_value = COUNTER_MAX_TOP_VALUE, \ .freq = RTC_CLOCK / \ (CONFIG_COUNTER_RTC##idx##_PRESCALER + 1), \ .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) \ }; \ diff --git a/drivers/counter/counter_nrfx_timer.c b/drivers/counter/counter_nrfx_timer.c index 869f5d162ef0c3..791de4b69d43e4 100644 --- a/drivers/counter/counter_nrfx_timer.c +++ b/drivers/counter/counter_nrfx_timer.c @@ -20,19 +20,24 @@ LOG_MODULE_REGISTER(); #define COUNTER_EVENT_TO_ID(evt) \ (evt - NRF_TIMER_EVENT_COMPARE2)/sizeof(u32_t) -#define WRAP_CH NRF_TIMER_CC_CHANNEL0 -#define COUNTER_WRAP_INT NRF_TIMER_EVENT_COMPARE0 +#define TOP_CH NRF_TIMER_CC_CHANNEL0 +#define COUNTER_TOP_INT NRF_TIMER_EVENT_COMPARE0 #define COUNTER_OVERFLOW_SHORT NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK #define COUNTER_READ_CC NRF_TIMER_CC_CHANNEL1 struct counter_nrfx_data { - counter_wrap_callback_t wrap_cb; - void *wrap_user_data; + counter_top_callback_t top_cb; + void *top_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); @@ -70,27 +75,27 @@ 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); const nrfx_timer_t *timer = &nrfx_config->timer; u32_t cc_val; - if (alarm_cfg->ticks > nrfx_timer_capture_get(timer, WRAP_CH)) { + if (alarm_cfg->ticks > nrfx_timer_capture_get(timer, TOP_CH)) { 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; } @@ -100,20 +105,20 @@ 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) + +static int counter_nrfx_set_top_value(struct device *dev, u32_t ticks, + counter_top_callback_t callback, + void *user_data) { const struct counter_nrfx_config *nrfx_config = get_nrfx_config(dev); const nrfx_timer_t *timer = &nrfx_config->timer; @@ -123,17 +128,17 @@ 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; } } - nrfx_timer_compare_int_disable(timer, WRAP_CH); + nrfx_timer_compare_int_disable(timer, TOP_CH); nrfx_timer_clear(timer); - data->wrap_cb = callback; - data->wrap_user_data = user_data; - nrfx_timer_extended_compare(timer, WRAP_CH, + data->top_cb = callback; + data->top_user_data = user_data; + nrfx_timer_extended_compare(timer, TOP_CH, ticks, COUNTER_OVERFLOW_SHORT, callback ? true : false); @@ -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) @@ -166,9 +170,9 @@ static void event_handler(nrf_timer_event_t event_type, void *p_context) struct device *dev = p_context; struct counter_nrfx_data *dev_data = get_dev_data(dev); - if (event_type == COUNTER_WRAP_INT) { - if (dev_data->wrap_cb) { - dev_data->wrap_cb(dev, dev_data->wrap_user_data); + if (event_type == COUNTER_TOP_INT) { + if (dev_data->top_cb) { + dev_data->top_cb(dev, dev_data->top_user_data); } } else if (event_type > NRF_TIMER_EVENT_COMPARE1) { alarm_event_handler(dev, COUNTER_EVENT_TO_ID(event_type)); @@ -188,21 +192,21 @@ static int init_timer(struct device *dev, const nrfx_timer_config_t *config) return -EBUSY; } - nrfx_timer_compare(timer, WRAP_CH, UINT32_MAX, false); + nrfx_timer_compare(timer, TOP_CH, UINT32_MAX, false); LOG_INST_DBG(nrfx_config->log, "Initialized"); return 0; } -static u32_t counter_nrfx_get_wrap(struct device *dev) +static u32_t counter_nrfx_get_top_value(struct device *dev) { - return nrfx_timer_capture_get(&get_nrfx_config(dev)->timer, WRAP_CH); + return nrfx_timer_capture_get(&get_nrfx_config(dev)->timer, TOP_CH); } static u32_t counter_nrfx_get_max_relative_alarm(struct device *dev) { - return nrfx_timer_capture_get(&get_nrfx_config(dev)->timer, WRAP_CH); + return nrfx_timer_capture_get(&get_nrfx_config(dev)->timer, TOP_CH); } static const struct counter_driver_api counter_nrfx_driver_api = { @@ -211,9 +215,9 @@ static const struct counter_driver_api counter_nrfx_driver_api = { .read = counter_nrfx_read, .set_alarm = counter_nrfx_set_alarm, .disable_alarm = counter_nrfx_disable_alarm, - .set_wrap = counter_nrfx_set_wrap, + .set_top_value = counter_nrfx_set_top_value, .get_pending_int = counter_nrfx_get_pending_int, - .get_wrap = counter_nrfx_get_wrap, + .get_top_value = counter_nrfx_get_top_value, .get_max_relative_alarm = counter_nrfx_get_max_relative_alarm, }; @@ -234,19 +238,19 @@ 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 = { \ - .max_wrap = (TIMER##idx##_MAX_SIZE == 32) ? \ + .max_top_value = (TIMER##idx##_MAX_SIZE == 32) ? \ 0xffffffff : 0x0000ffff, \ .freq = TIMER_CLOCK / \ (1 << CONFIG_COUNTER_TIMER##idx##_PRESCALER), \ .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) \ }; \ diff --git a/include/counter.h b/include/counter.h index 97365b3ec3daa0..be3a7d6eeb8e3e 100644 --- a/include/counter.h +++ b/include/counter.h @@ -29,85 +29,76 @@ extern "C" { #endif -struct counter_alarm_cfg; - /** @brief Alarm callback * - * @param dev Pointer to the device structure for the driver instance. - * @param cfg Original structure owning this handler. - * @param ticks Counter value that triggered the callback. + * @param dev Pointer to the device structure for the driver instance. + * @param chan_id Channel ID. + * @param ticks Counter value that triggered the callback. + * @param user_data User data. */ typedef void (*counter_alarm_callback_t)(struct device *dev, - const struct counter_alarm_cfg *cfg, - u32_t ticks); + u8_t chan_id, u32_t ticks, + void *user_data); /** @brief Alarm callback structure. * - * Used to set an alarm. - * Beware such structure should not be allocated on stack. + * @param callback Callback called on alarm (cannot be NULL). + * @param ticks Ticks that triggers the alarm. In case of absolute flag is set, + * maximal value that can be set equals top value + * (@ref counter_get_top_value). Otherwise + * @ref 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 { - counter_alarm_callback_t handler; /*!< handler called on alarm - * (cannot be NULL). - */ - - 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. - */ + counter_alarm_callback_t callback; + u32_t ticks; + void *user_data; + bool absolute; }; -/** @brief Wrap callback +/** @brief Callback called when counter turns around. * * @param dev Pointer to the device structure for the driver instance. - * @param user_data User data provided in counter_set_wrap_alarm. + * @param user_data User data provided in @ref counter_set_top_value. + */ +typedef void (*counter_top_callback_t)(struct device *dev, void *user_data); + +/** @brief Structure with generic counter features. + * + * @param max_top_value Maximal (default) top 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. */ -typedef void (*counter_wrap_callback_t)(struct device *dev, void *user_data); - -/** @brief Structure with generic counter features. */ struct counter_config_info { - u32_t max_wrap; /*!< Maximal (default) wrap value on which counter is - * reset (cleared or reloaded). - */ - - 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. - */ + u32_t max_top_value; + u32_t freq; + bool count_up; + u8_t channels; }; typedef int (*counter_api_start)(struct device *dev); typedef int (*counter_api_stop)(struct device *dev); typedef u32_t (*counter_api_read)(struct device *dev); -typedef int (*counter_api_set_alarm)(struct device *dev, - const struct counter_alarm_cfg *alarm_cfg); -typedef int (*counter_api_disable_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_set_wrap)(struct device *dev, u32_t ticks, - counter_wrap_callback_t callback, +typedef int (*counter_api_disable_alarm)(struct device *dev, u8_t chan_id); +typedef int (*counter_api_set_top_value)(struct device *dev, u32_t ticks, + counter_top_callback_t callback, void *user_data); typedef u32_t (*counter_api_get_pending_int)(struct device *dev); -typedef u32_t (*counter_api_get_wrap)(struct device *dev); +typedef u32_t (*counter_api_get_top_value)(struct device *dev); typedef u32_t (*counter_api_get_max_relative_alarm)(struct device *dev); typedef void *(*counter_api_get_user_data)(struct device *dev); @@ -117,9 +108,9 @@ struct counter_driver_api { counter_api_read read; counter_api_set_alarm set_alarm; counter_api_disable_alarm disable_alarm; - counter_api_set_wrap set_wrap; + counter_api_set_top_value set_top_value; counter_api_get_pending_int get_pending_int; - counter_api_get_wrap get_wrap; + counter_api_get_top_value get_top_value; counter_api_get_max_relative_alarm get_max_relative_alarm; counter_api_get_user_data get_user_data; }; @@ -133,7 +124,7 @@ struct counter_driver_api { * @retval true if counter is counting up. * @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; @@ -201,17 +192,17 @@ static inline u64_t counter_ticks_to_us(const struct device *dev, u32_t ticks) } /** - * @brief Function to retrieve maximum wrap value that can be set. + * @brief Function to retrieve maximum top value that can be set. * * @param[in] dev Pointer to the device structure for the driver instance. * - * @return Max wrap value. + * @return Max top value. */ -static inline u32_t counter_get_max_wrap(const struct device *dev) +static inline u32_t counter_get_max_top_value(const struct device *dev) { const struct counter_config_info *config = dev->config->config_info; - return config->max_wrap; + return config->max_top_value; } /** @@ -268,11 +259,12 @@ static inline u32_t _impl_counter_read(struct device *dev) * @brief Set an alarm on a channel. * * In case of absolute request, maximal value that can be set is equal to - * wrap value set by counter_set_wrap call or default, maximal one. in case of - * relative request, Maximal value can be retrieved using - * counter_get_max_relative_alarm. + * top value set by @ref counter_set_top_value call or default, maximal one. In + * case of relative request, maximal value can be retrieved using + * @ref counter_get_max_relative_alarm. * * @param dev Pointer to the device structure for the driver instance. + * @param chan_id Channel ID. * @param alarm_cfg Alarm configuration. * * @retval 0 If successful. @@ -280,71 +272,70 @@ static inline u32_t _impl_counter_read(struct device *dev) * interrupts or requested channel). * @retval -EINVAL if alarm settings are invalid. */ -static inline int counter_set_ch_alarm(struct device *dev, - const struct counter_alarm_cfg *alarm_cfg) +static inline int counter_set_channel_alarm(struct device *dev, u8_t chan_id, + const struct counter_alarm_cfg *alarm_cfg) { 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 api->set_alarm(dev, alarm_cfg); + return api->set_alarm(dev, chan_id, alarm_cfg); } /** * @brief Disable an alarm on a channel. * * @param dev Pointer to the device structure for the driver instance. - * @param alarm_cfg Alarm configuration. It must be the same address as the - * one used for @ref counter_set_alarm. + * @param chan_id Channel ID. * * @retval 0 If successful. * @retval -ENOTSUP if request is not supported or the counter was not started * yet. */ -static inline int counter_disable_ch_alarm(struct device *dev, - const struct counter_alarm_cfg *alarm_cfg) +static inline int counter_disable_channel_alarm(struct device *dev, + u8_t chan_id) { 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 api->disable_alarm(dev, alarm_cfg); + return api->disable_alarm(dev, chan_id); } /** - * @brief Set counter wrap. + * @brief Set counter top value. * - * Function sets wrap value and resets the counter to 0 or wrap value - * depending on counter direction. On wrap, counter is reset and optinal - * callback is periodically called. Wrap value can only be changed when there - * is no active alarm. + * Function sets top value and resets the counter to 0 or top value + * depending on counter direction. On turnaround, counter is reset and optional + * callback is periodically called. Top value can only be changed when there + * is no active channel alarm. * * @param dev Pointer to the device structure for the driver instance. - * @param ticks Wrap value. + * @param ticks Top value. * @param callback Callback function. Can be NULL. * @param user_data User data passed to callback function. Not valid if * callback is NULL. * * @retval 0 If successful. - * @retval -ENOTSUP if request is not supported (e.g. wrap value cannot be + * @retval -ENOTSUP if request is not supported (e.g. top value cannot be * changed). * @retval -EBUSY if any alarm is active. */ -static inline int counter_set_wrap(struct device *dev, u32_t ticks, - counter_wrap_callback_t callback, - void *user_data) +static inline int counter_set_top_value(struct device *dev, u32_t ticks, + counter_top_callback_t callback, + void *user_data) { const struct counter_driver_api *api = dev->driver_api; - if (ticks > counter_get_max_wrap(dev)) { + if (ticks > counter_get_max_top_value(dev)) { return -EINVAL; } - return api->set_wrap(dev, ticks, callback, user_data); + return api->set_top_value(dev, ticks, callback, user_data); } /** @@ -370,19 +361,19 @@ static inline int _impl_counter_get_pending_int(struct device *dev) } /** - * @brief Function to retrieve current wrap value. + * @brief Function to retrieve current top value. * * @param[in] dev Pointer to the device structure for the driver instance. * - * @return Wrap value. + * @return Top value. */ -__syscall u32_t counter_get_wrap(struct device *dev); +__syscall u32_t counter_get_top_value(struct device *dev); -static inline u32_t _impl_counter_get_wrap(struct device *dev) +static inline u32_t _impl_counter_get_top_value(struct device *dev) { const struct counter_driver_api *api = dev->driver_api; - return api->get_wrap(dev); + return api->get_top_value(dev); } /** @@ -402,18 +393,21 @@ static inline u32_t _impl_counter_get_max_relative_alarm(struct device *dev) return api->get_max_relative_alarm(dev); } -/* Deprecated */ +/* 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); + return counter_set_top_value(dev, count, callback, user_data); } /** - * @brief Get user data set for wrap alarm. + * @brief Get user data set for top alarm. * * @note Function intended to be used only by deprecated RTC driver API to * provide backward compatibility. diff --git a/tests/drivers/counter/counter_basic_api/src/test_counter.c b/tests/drivers/counter/counter_basic_api/src/test_counter.c index 3c409233174a11..8f510b27fceec2 100644 --- a/tests/drivers/counter/counter_basic_api/src/test_counter.c +++ b/tests/drivers/counter/counter_basic_api/src/test_counter.c @@ -8,14 +8,14 @@ #include #include -static volatile int wrap_cnt; -static volatile int alarm_cnt; +static volatile u32_t top_cnt; +static volatile u32_t alarm_cnt; -static void wrap_handler(struct device *dev, void *user_data); +static void top_handler(struct device *dev, void *user_data); void *exp_user_data = (void *)199; -#define WRAP_PERIOD_US 20000 +#define COUNTER_PERIOD_US 20000 struct counter_alarm_cfg alarm_cfg; struct counter_alarm_cfg alarm_cfg2; @@ -57,8 +57,9 @@ static void counter_tear_down_instance(const char *dev_name) dev = device_get_binding(dev_name); - err = counter_set_wrap(dev, counter_get_max_wrap(dev), NULL, NULL); - zassert_equal(0, err, "Counter wrap to default failed\n"); + err = counter_set_top_value(dev, counter_get_max_top_value(dev), + NULL, NULL); + zassert_equal(0, err, "Setting top value to default failed\n"); err = counter_stop(dev); zassert_equal(0, err, "Counter failed to stop\n"); @@ -77,24 +78,23 @@ void counter_tear_down(void) } -static void wrap_handler(struct device *dev, void *user_data) +static void top_handler(struct device *dev, void *user_data) { zassert_true(user_data == exp_user_data, "Unexpected callback\n"); - wrap_cnt++; + top_cnt++; } -#include -void test_wrap_alarm_instance(const char *dev_name) +void test_set_top_value_with_alarm_instance(const char *dev_name) { struct device *dev; int err; - int cnt; + u32_t cnt; u32_t ticks; - wrap_cnt = 0; + top_cnt = 0; dev = device_get_binding(dev_name); - ticks = counter_us_to_ticks(dev, WRAP_PERIOD_US); + ticks = counter_us_to_ticks(dev, COUNTER_PERIOD_US); err = counter_start(dev); zassert_equal(0, err, "Counter failed to start\n"); @@ -104,27 +104,26 @@ void test_wrap_alarm_instance(const char *dev_name) cnt = counter_read(dev); zassert_true(cnt > 0, "Counter should progress (dev:%s)\n", dev_name); - err = counter_set_wrap(dev, ticks, wrap_handler, exp_user_data); - zassert_equal(0, err, "Counter failed to set wrap (dev:%s)\n", + err = counter_set_top_value(dev, ticks, top_handler, exp_user_data); + zassert_equal(0, err, "Counter failed to set top value (dev:%s)\n", dev_name); - k_busy_wait(5.2*WRAP_PERIOD_US); + k_busy_wait(5.2*COUNTER_PERIOD_US); - zassert_true(wrap_cnt == 5, - "Unexpected number of wraps (%d) (dev: %s).\n", - wrap_cnt, dev_name); + zassert_true(top_cnt == 5, + "Unexpected number of turnarounds (%d) (dev: %s).\n", + top_cnt, dev_name); } -void test_wrap_alarm(void) +void test_set_top_value_with_alarm(void) { - test_all_instances(test_wrap_alarm_instance); + test_all_instances(test_set_top_value_with_alarm_instance); } -static void alarm_handler(struct device *dev, - const struct counter_alarm_cfg *cfg, - u32_t counter) +static void alarm_handler(struct device *dev, u8_t chan_id, u32_t counter, + void *user_data) { - zassert_true(&alarm_cfg == cfg, "Unexpected callback\n"); + zassert_true(&alarm_cfg == user_data, "Unexpected callback\n"); alarm_cnt++; } @@ -135,26 +134,27 @@ void test_single_shot_alarm_instance(const char *dev_name) u32_t ticks; dev = device_get_binding(dev_name); - ticks = counter_us_to_ticks(dev, WRAP_PERIOD_US); + ticks = counter_us_to_ticks(dev, COUNTER_PERIOD_US); - alarm_cfg.channel_id = 0; alarm_cfg.absolute = true; alarm_cfg.ticks = ticks + 1; - alarm_cfg.handler = alarm_handler; + alarm_cfg.callback = alarm_handler; + alarm_cfg.user_data = &alarm_cfg; alarm_cnt = 0; err = counter_start(dev); zassert_equal(0, err, "Counter failed to start\n"); - err = counter_set_wrap(dev, ticks, wrap_handler, exp_user_data); - zassert_equal(0, err, "Counter failed to set wrap\n"); - - err = counter_set_ch_alarm(dev, &alarm_cfg); - zassert_equal(-EINVAL, err, "Counter should return error because ticks exceeded the limit set alarm\n"); + err = counter_set_top_value(dev, ticks, top_handler, exp_user_data); + zassert_equal(0, err, "Counter failed to set top value\n"); + 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"); 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"); k_busy_wait(1.2*counter_ticks_to_us(dev, ticks)); @@ -164,11 +164,12 @@ void test_single_shot_alarm_instance(const char *dev_name) k_busy_wait(counter_ticks_to_us(dev, 2*ticks)); 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"); - err = counter_set_wrap(dev, counter_get_max_wrap(dev), NULL, NULL); - zassert_equal(0, err, "Counter wrap to default failed\n"); + err = counter_set_top_value(dev, counter_get_max_top_value(dev), + NULL, NULL); + zassert_equal(0, err, "Setting top value to default failed\n"); err = counter_stop(dev); zassert_equal(0, err, "Counter failed to stop\n"); @@ -179,13 +180,12 @@ void test_single_shot_alarm(void) 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, - const struct counter_alarm_cfg *cfg, - u32_t counter) +static void alarm_handler2(struct device *dev, u8_t chan_id, u32_t counter, + void *user_data) { - clbks[alarm_cnt] = cfg; + clbk_data[alarm_cnt] = user_data; alarm_cnt++; } @@ -193,7 +193,7 @@ static void alarm_handler2(struct device *dev, * Two alarms set. First alarm is absolute, second relative. Because * setting of both alarms is delayed it is expected that second alarm * will expire first (relative to the time called) while first alarm - * will expire after next wrap. + * will expire after next wrap around. */ void test_multiple_alarms_instance(const char *dev_name) { @@ -202,44 +202,46 @@ void test_multiple_alarms_instance(const char *dev_name) u32_t ticks; dev = device_get_binding(dev_name); - ticks = counter_us_to_ticks(dev, WRAP_PERIOD_US); + ticks = counter_us_to_ticks(dev, COUNTER_PERIOD_US); - alarm_cfg.channel_id = 0; alarm_cfg.absolute = true; 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.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; err = counter_start(dev); zassert_equal(0, err, "Counter failed to start\n"); - err = counter_set_wrap(dev, ticks, wrap_handler, exp_user_data); - zassert_equal(0, err, "Counter failed to set wrap\n"); + err = counter_set_top_value(dev, ticks, top_handler, exp_user_data); + zassert_equal(0, err, "Counter failed to set top value\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"); - 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"); k_busy_wait(1.2*counter_ticks_to_us(dev, 2*ticks)); 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_cfg, clbks[1], "Expected different order or callbacks\n"); + zassert_equal(&alarm_cfg2, clbk_data[0], + "Expected different order or callbacks\n"); + zassert_equal(&alarm_cfg, clbk_data[1], + "Expected different order or callbacks\n"); /* 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"); - err = counter_disable_ch_alarm(dev, &alarm_cfg2); + err = counter_disable_channel_alarm(dev, 1); zassert_equal(0, err, "Counter disabling alarm failed\n"); } @@ -259,37 +261,38 @@ void test_all_channels_instance(const char *str) u32_t ticks; dev = device_get_binding(str); - ticks = counter_us_to_ticks(dev, WRAP_PERIOD_US); + ticks = counter_us_to_ticks(dev, COUNTER_PERIOD_US); for (int i = 0; i < n; i++) { - alarm_cfgs[i].channel_id = i; alarm_cfgs[i].absolute = false; alarm_cfgs[i].ticks = ticks-100; - alarm_cfgs[i].handler = alarm_handler2; + alarm_cfgs[i].callback = alarm_handler2; } err = counter_start(dev); zassert_equal(0, err, "Counter failed to start"); 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) { nchan++; } else if (err == -ENOTSUP) { limit_reached = true; } else { - zassert_equal(0, 1, "Unexpected error on setting alarm"); + zassert_equal(0, 1, + "Unexpected error on setting alarm"); } } 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"); } for (int i = nchan; i < n; i++) { - err = counter_disable_ch_alarm(dev, &alarm_cfgs[i]); - zassert_equal(-ENOTSUP, err, "Unexpected error on disabling alarm\n"); + err = counter_disable_channel_alarm(dev, i); + zassert_equal(-ENOTSUP, err, + "Unexpected error on disabling alarm\n"); } } @@ -301,7 +304,7 @@ void test_all_channels(void) void test_main(void) { ztest_test_suite(test_counter, - ztest_unit_test_setup_teardown(test_wrap_alarm, + ztest_unit_test_setup_teardown(test_set_top_value_with_alarm, unit_test_noop, counter_tear_down), ztest_unit_test_setup_teardown(test_single_shot_alarm,