Counter

Overview

API Reference

group counter_interface

Counter Interface.

Typedefs

typedef void (*counter_alarm_callback_t)(struct device *dev, u8_t chan_id, u32_t ticks, void *user_data)

Alarm callback.

Parameters
  • dev: Pointer to the device structure for the driver instance.
  • chan_id: Channel ID.
  • ticks: Counter value that triggered the callback.
  • user_data: User data.

typedef void (*counter_top_callback_t)(struct device *dev, void *user_data)

Callback called when counter turns around.

Parameters
  • dev: Pointer to the device structure for the driver instance.
  • user_data: User data provided in counter_set_top_value.

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, u8_t chan_id, const struct counter_alarm_cfg *alarm_cfg)
typedef int (*counter_api_cancel_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_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)
typedef void (*counter_callback_t)(struct device *dev, void *user_data)

Functions

static bool counter_is_counting_up(const struct device *dev)

Function to check if counter is counting up.

Parameters
  • dev: Pointer to the device structure for the driver instance.
Return Value
  • true: if counter is counting up.
  • false: if counter is counting down.

static u8_t counter_get_num_of_channels(const struct device *dev)

Function to get number of alarm channels.

Return
Number of alarm channels.
Parameters
  • dev: Pointer to the device structure for the driver instance.

static u32_t counter_get_frequency(const struct device *dev)

Function to get counter frequency.

Return
Frequency of the counter in Hz, or zero if the counter does not have a fixed frequency.
Parameters
  • dev: Pointer to the device structure for the driver instance.

static u32_t counter_us_to_ticks(const struct device *dev, u64_t us)

Function to convert microseconds to ticks.

Return
Converted ticks. Ticks will be saturated if exceed 32 bits.
Parameters
  • dev: Pointer to the device structure for the driver instance.
  • us: Microseconds.

static u64_t counter_ticks_to_us(const struct device *dev, u32_t ticks)

Function to convert ticks to microseconds.

Return
Converted microseconds.
Parameters
  • dev: Pointer to the device structure for the driver instance.
  • ticks: Ticks.

static u32_t counter_get_max_top_value(const struct device *dev)

Function to retrieve maximum top value that can be set.

Return
Max top value.
Parameters
  • dev: Pointer to the device structure for the driver instance.

int counter_start(struct device *dev)

Start counter device in free running mode.

Parameters
  • dev: Pointer to the device structure for the driver instance.
Return Value
  • 0: If successful.
  • Negative: errno code if failure.

static int z_impl_counter_start(struct device *dev)
int counter_stop(struct device *dev)

Stop counter device.

Parameters
  • dev: Pointer to the device structure for the driver instance.
Return Value
  • 0: If successful.
  • -ENOTSUP: if the device doesn’t support stopping the counter.

static int z_impl_counter_stop(struct device *dev)
u32_t counter_read(struct device *dev)

Read current counter value.

Return
32-bit value
Parameters
  • dev: Pointer to the device structure for the driver instance.

static u32_t z_impl_counter_read(struct device *dev)
static int counter_set_channel_alarm(struct device *dev, u8_t chan_id, const struct counter_alarm_cfg *alarm_cfg)

Set a single shot alarm on a channel.

After expiration alarm can be set again, disabling is not needed. When alarm expiration handler is called, channel is considered available and can be set again in that context.

Note
API is not thread safe.
Parameters
  • dev: Pointer to the device structure for the driver instance.
  • chan_id: Channel ID.
  • alarm_cfg: Alarm configuration.
Return Value
  • 0: If successful.
  • -ENOTSUP: if request is not supported (device does not support interrupts or requested channel).
  • -EINVAL: if alarm settings are invalid.

static int counter_cancel_channel_alarm(struct device *dev, u8_t chan_id)

Cancel an alarm on a channel.

Note
API is not thread safe.
Parameters
  • dev: Pointer to the device structure for the driver instance.
  • chan_id: Channel ID.
Return Value
  • 0: If successful.
  • -ENOTSUP: if request is not supported or the counter was not started yet.

static int counter_set_top_value(struct device *dev, u32_t ticks, counter_top_callback_t callback, void *user_data)

Set counter top value.

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.

Parameters
  • dev: Pointer to the device structure for the driver instance.
  • ticks: Top value.
  • callback: Callback function. Can be NULL.
  • user_data: User data passed to callback function. Not valid if callback is NULL.
Return Value
  • 0: If successful.
  • -ENOTSUP: if request is not supported (e.g. top value cannot be changed).
  • -EBUSY: if any alarm is active.

int counter_get_pending_int(struct device *dev)

Function to get pending interrupts.

The purpose of this function is to return the interrupt status register for the device. This is especially useful when waking up from low power states to check the wake up source.

Parameters
  • dev: Pointer to the device structure for the driver instance.
Return Value
  • 1: if any counter interrupt is pending.
  • 0: if no counter interrupt is pending.

static int z_impl_counter_get_pending_int(struct device *dev)
u32_t counter_get_top_value(struct device *dev)

Function to retrieve current top value.

Return
Top value.
Parameters
  • dev: Pointer to the device structure for the driver instance.

static u32_t z_impl_counter_get_top_value(struct device *dev)
u32_t counter_get_max_relative_alarm(struct device *dev)

Function to retrieve maximum relative value that can be set by counter_set_alarm.

Return
Max alarm value.
Parameters
  • dev: Pointer to the device structure for the driver instance.

static u32_t z_impl_counter_get_max_relative_alarm(struct device *dev)
static int counter_set_alarm(struct device *dev, counter_callback_t callback, u32_t count, void *user_data)

Deprecated function.

static void *counter_get_user_data(struct device *dev)

Get user data set for top alarm.

Note
Function intended to be used only by deprecated RTC driver API to provide backward compatibility.
Return
User data.
Parameters
  • dev: Pointer to the device structure for the driver instance.

struct counter_alarm_cfg
#include <counter.h>

Alarm callback structure.

Parameters
  • callback: Callback called on alarm (cannot be NULL).
  • ticks: Ticks that triggers the alarm. In case of absolute flag is set, maximal value that can be set equals top value (counter_get_top_value). 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 counter_ticks_to_us). Alternatively, counter implementation may count asynchronous events.
  • user_data: User data returned in callback.
  • 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_config_info
#include <counter.h>

Structure with generic counter features.

Parameters
  • max_top_value: Maximal (default) top value on which counter is reset (cleared or reloaded).
  • freq: Frequency of the source clock if synchronous events are counted.
  • flags: Flags. See Counter device capabilities.
  • channels: Number of channels that can be used for setting alarm, see counter_set_alarm.