Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
Counter Interface

Counter Interface. More...

Modules

 Alarm configuration flags
 Used in alarm configuration structure (counter_alarm_cfg).
 
 Counter device capabilities
 
 Counter guard period flags
 Used by counter_set_guard_period and counter_get_guard_period.
 
 Flags used by @ref counter_top_cfg.
 

Data Structures

struct  counter_alarm_cfg
 Alarm callback structure. More...
 
struct  counter_top_cfg
 Top value configuration structure. More...
 
struct  counter_config_info
 Structure with generic counter features. More...
 
struct  counter_driver_api
 

Typedefs

typedef void(* counter_alarm_callback_t) (const struct device *dev, uint8_t chan_id, uint32_t ticks, void *user_data)
 Alarm callback. More...
 
typedef void(* counter_top_callback_t) (const struct device *dev, void *user_data)
 Callback called when counter turns around. More...
 
typedef int(* counter_api_start) (const struct device *dev)
 
typedef int(* counter_api_stop) (const struct device *dev)
 
typedef int(* counter_api_get_value) (const struct device *dev, uint32_t *ticks)
 
typedef int(* counter_api_set_alarm) (const struct device *dev, uint8_t chan_id, const struct counter_alarm_cfg *alarm_cfg)
 
typedef int(* counter_api_cancel_alarm) (const struct device *dev, uint8_t chan_id)
 
typedef int(* counter_api_set_top_value) (const struct device *dev, const struct counter_top_cfg *cfg)
 
typedef uint32_t(* counter_api_get_pending_int) (const struct device *dev)
 
typedef uint32_t(* counter_api_get_top_value) (const struct device *dev)
 
typedef uint32_t(* counter_api_get_guard_period) (const struct device *dev, uint32_t flags)
 
typedef int(* counter_api_set_guard_period) (const struct device *dev, uint32_t ticks, uint32_t flags)
 

Functions

bool counter_is_counting_up (const struct device *dev)
 Function to check if counter is counting up. More...
 
uint8_t counter_get_num_of_channels (const struct device *dev)
 Function to get number of alarm channels. More...
 
uint32_t counter_get_frequency (const struct device *dev)
 Function to get counter frequency. More...
 
uint32_t counter_us_to_ticks (const struct device *dev, uint64_t us)
 Function to convert microseconds to ticks. More...
 
uint64_t counter_ticks_to_us (const struct device *dev, uint32_t ticks)
 Function to convert ticks to microseconds. More...
 
uint32_t counter_get_max_top_value (const struct device *dev)
 Function to retrieve maximum top value that can be set. More...
 
int counter_start (const struct device *dev)
 Start counter device in free running mode. More...
 
int counter_stop (const struct device *dev)
 Stop counter device. More...
 
int counter_get_value (const struct device *dev, uint32_t *ticks)
 Get current counter value. More...
 
int counter_set_channel_alarm (const struct device *dev, uint8_t chan_id, const struct counter_alarm_cfg *alarm_cfg)
 Set a single shot alarm on a channel. More...
 
int counter_cancel_channel_alarm (const struct device *dev, uint8_t chan_id)
 Cancel an alarm on a channel. More...
 
int counter_set_top_value (const struct device *dev, const struct counter_top_cfg *cfg)
 Set counter top value. More...
 
int counter_get_pending_int (const struct device *dev)
 Function to get pending interrupts. More...
 
uint32_t counter_get_top_value (const struct device *dev)
 Function to retrieve current top value. More...
 
int counter_set_guard_period (const struct device *dev, uint32_t ticks, uint32_t flags)
 Set guard period in counter ticks. More...
 
uint32_t counter_get_guard_period (const struct device *dev, uint32_t flags)
 Return guard period. More...
 

Detailed Description

Counter Interface.

Typedef Documentation

◆ counter_alarm_callback_t

typedef void(* counter_alarm_callback_t) (const struct device *dev, uint8_t chan_id, uint32_t ticks, void *user_data)

#include <include/drivers/counter.h>

Alarm callback.

Parameters
devPointer to the device structure for the driver instance.
chan_idChannel ID.
ticksCounter value that triggered the alarm.
user_dataUser data.

◆ counter_api_cancel_alarm

typedef int(* counter_api_cancel_alarm) (const struct device *dev, uint8_t chan_id)

◆ counter_api_get_guard_period

typedef uint32_t(* counter_api_get_guard_period) (const struct device *dev, uint32_t flags)

◆ counter_api_get_pending_int

typedef uint32_t(* counter_api_get_pending_int) (const struct device *dev)

◆ counter_api_get_top_value

typedef uint32_t(* counter_api_get_top_value) (const struct device *dev)

◆ counter_api_get_value

typedef int(* counter_api_get_value) (const struct device *dev, uint32_t *ticks)

◆ counter_api_set_alarm

typedef int(* counter_api_set_alarm) (const struct device *dev, uint8_t chan_id, const struct counter_alarm_cfg *alarm_cfg)

◆ counter_api_set_guard_period

typedef int(* counter_api_set_guard_period) (const struct device *dev, uint32_t ticks, uint32_t flags)

◆ counter_api_set_top_value

typedef int(* counter_api_set_top_value) (const struct device *dev, const struct counter_top_cfg *cfg)

◆ counter_api_start

typedef int(* counter_api_start) (const struct device *dev)

◆ counter_api_stop

typedef int(* counter_api_stop) (const struct device *dev)

◆ counter_top_callback_t

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

#include <include/drivers/counter.h>

Callback called when counter turns around.

Parameters
devPointer to the device structure for the driver instance.
user_dataUser data provided in counter_set_top_value.

Function Documentation

◆ counter_cancel_channel_alarm()

int counter_cancel_channel_alarm ( const struct device dev,
uint8_t  chan_id 
)

#include <include/drivers/counter.h>

Cancel an alarm on a channel.

Note
API is not thread safe.
Parameters
devPointer to the device structure for the driver instance.
chan_idChannel ID.
Return values
0If successful.
-ENOTSUPif request is not supported or the counter was not started yet.

◆ counter_get_frequency()

uint32_t counter_get_frequency ( const struct device dev)

#include <include/drivers/counter.h>

Function to get counter frequency.

Parameters
[in]devPointer to the device structure for the driver instance.
Returns
Frequency of the counter in Hz, or zero if the counter does not have a fixed frequency.

◆ counter_get_guard_period()

uint32_t counter_get_guard_period ( const struct device dev,
uint32_t  flags 
)

#include <include/drivers/counter.h>

Return guard period.

See counter_set_guard_period.

Parameters
devPointer to the device structure for the driver instance.
flagsSee Counter guard period flags.
Returns
Guard period given in counter ticks or 0 if function or flags are not supported.

◆ counter_get_max_top_value()

uint32_t counter_get_max_top_value ( const struct device dev)

#include <include/drivers/counter.h>

Function to retrieve maximum top value that can be set.

Parameters
[in]devPointer to the device structure for the driver instance.
Returns
Max top value.

◆ counter_get_num_of_channels()

uint8_t counter_get_num_of_channels ( const struct device dev)

#include <include/drivers/counter.h>

Function to get number of alarm channels.

Parameters
[in]devPointer to the device structure for the driver instance.
Returns
Number of alarm channels.

◆ counter_get_pending_int()

int counter_get_pending_int ( const struct device dev)

#include <include/drivers/counter.h>

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
devPointer to the device structure for the driver instance.
Return values
1if any counter interrupt is pending.
0if no counter interrupt is pending.

◆ counter_get_top_value()

uint32_t counter_get_top_value ( const struct device dev)

#include <include/drivers/counter.h>

Function to retrieve current top value.

Parameters
[in]devPointer to the device structure for the driver instance.
Returns
Top value.

◆ counter_get_value()

int counter_get_value ( const struct device dev,
uint32_t ticks 
)

#include <include/drivers/counter.h>

Get current counter value.

Parameters
devPointer to the device structure for the driver instance.
ticksPointer to where to store the current counter value
Return values
0If successful.
Negativeerror code on failure getting the counter value

◆ counter_is_counting_up()

bool counter_is_counting_up ( const struct device dev)

#include <include/drivers/counter.h>

Function to check if counter is counting up.

Parameters
[in]devPointer to the device structure for the driver instance.
Return values
trueif counter is counting up.
falseif counter is counting down.

◆ counter_set_channel_alarm()

int counter_set_channel_alarm ( const struct device dev,
uint8_t  chan_id,
const struct counter_alarm_cfg alarm_cfg 
)

#include <include/drivers/counter.h>

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
devPointer to the device structure for the driver instance.
chan_idChannel ID.
alarm_cfgAlarm configuration.
Return values
0If successful.
-ENOTSUPif request is not supported (device does not support interrupts or requested channel).
-EINVALif alarm settings are invalid.
-ETIMEif absolute alarm was set too late.

◆ counter_set_guard_period()

int counter_set_guard_period ( const struct device dev,
uint32_t  ticks,
uint32_t  flags 
)

#include <include/drivers/counter.h>

Set guard period in counter ticks.

Setting non-zero guard period enables detection of setting absolute alarm too late. It limits how far in the future absolute alarm can be set.

Detection of too late setting is vital since if it is not detected alarm is delayed by full period of the counter (up to 32 bits). Because of the wrapping, it is impossible to distinguish alarm which is short in the past from alarm which is targeted to expire after full counter period. In order to detect too late setting, longest possible alarm is limited. Absolute value cannot exceed: (now + top_value - guard_period) % top_value.

Guard period depends on application and counter frequency. If it is expected that absolute alarms setting might be delayed then guard period should exceed maximal potential delay. If use case allows, guard period can be set very high (e.g. half of the counter top value).

After initialization guard period is set to 0 and late detection is disabled.

Parameters
devPointer to the device structure for the driver instance.
ticksGuard period in counter ticks.
flagsSee Counter guard period flags.
Return values
0if successful.
-ENOTSUPif function or flags are not supported.
-EINVALif ticks value is invalid.

◆ counter_set_top_value()

int counter_set_top_value ( const struct device dev,
const struct counter_top_cfg cfg 
)

#include <include/drivers/counter.h>

Set counter top value.

Function sets top value and optionally resets the counter to 0 or top value depending on counter direction. On turnaround, counter can be reset and optional callback is periodically called. Top value can only be changed when there is no active channel alarm.

COUNTER_TOP_CFG_DONT_RESET prevents counter reset. When counter is running while top value is updated, it is possible that counter progresses outside the new top value. In that case, error is returned and optionally driver can reset the counter (see COUNTER_TOP_CFG_RESET_WHEN_LATE).

Parameters
devPointer to the device structure for the driver instance.
cfgConfiguration. Cannot be NULL.
Return values
0If successful.
-ENOTSUPif request is not supported (e.g. top value cannot be changed or counter cannot/must be reset during top value update).
-EBUSYif any alarm is active.
-ETIMEif COUNTER_TOP_CFG_DONT_RESET was set and new top value is smaller than current counter value (counter counting up).

◆ counter_start()

int counter_start ( const struct device dev)

#include <include/drivers/counter.h>

Start counter device in free running mode.

Parameters
devPointer to the device structure for the driver instance.
Return values
0If successful.
Negativeerrno code if failure.

◆ counter_stop()

int counter_stop ( const struct device dev)

#include <include/drivers/counter.h>

Stop counter device.

Parameters
devPointer to the device structure for the driver instance.
Return values
0If successful.
-ENOTSUPif the device doesn't support stopping the counter.

◆ counter_ticks_to_us()

uint64_t counter_ticks_to_us ( const struct device dev,
uint32_t  ticks 
)

#include <include/drivers/counter.h>

Function to convert ticks to microseconds.

Parameters
[in]devPointer to the device structure for the driver instance.
[in]ticksTicks.
Returns
Converted microseconds.

◆ counter_us_to_ticks()

uint32_t counter_us_to_ticks ( const struct device dev,
uint64_t  us 
)

#include <include/drivers/counter.h>

Function to convert microseconds to ticks.

Parameters
[in]devPointer to the device structure for the driver instance.
[in]usMicroseconds.
Returns
Converted ticks. Ticks will be saturated if exceed 32 bits.