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

@ -29,45 +29,37 @@
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 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 {
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
@ -77,32 +69,29 @@ struct counter_alarm_cfg {
*/
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 {
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_wrap;
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_disable_alarm)(struct device *dev, u8_t chan_id);
typedef int (*counter_api_set_wrap)(struct device *dev, u32_t ticks,
counter_wrap_callback_t callback,
void *user_data);
@ -133,7 +122,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;
@ -273,6 +262,7 @@ static inline u32_t _impl_counter_read(struct device *dev)
* 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,39 +270,38 @@ 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);
}
/**
@ -402,9 +391,12 @@ 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)