2016-02-23 09:10:56 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Intel Corporation
|
|
|
|
*
|
2017-01-19 09:01:01 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-02-23 09:10:56 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Public API for counter and timer drivers
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __COUNTER_H__
|
|
|
|
#define __COUNTER_H__
|
|
|
|
|
|
|
|
/**
|
2017-12-01 06:44:16 +08:00
|
|
|
* @brief Counter Interface
|
|
|
|
* @defgroup counter_interface Counter Interface
|
2016-04-16 02:18:46 +08:00
|
|
|
* @ingroup io_interfaces
|
2016-02-23 09:10:56 +08:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
Introduce new sized integer typedefs
This is a start to move away from the C99 {u}int{8,16,32,64}_t types to
Zephyr defined u{8,16,32,64}_t and s{8,16,32,64}_t. This allows Zephyr
to define the sized types in a consistent manor across all the
architectures we support and not conflict with what various compilers
and libc might do with regards to the C99 types.
We introduce <zephyr/types.h> as part of this and have it include
<stdint.h> for now until we transition all the code away from the C99
types.
We go with u{8,16,32,64}_t and s{8,16,32,64}_t as there are some
existing variables defined u8 & u16 as well as to be consistent with
Zephyr naming conventions.
Jira: ZEP-2051
Change-Id: I451fed0623b029d65866622e478225dfab2c0ca8
Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2017-04-19 23:32:08 +08:00
|
|
|
#include <zephyr/types.h>
|
2016-02-23 09:10:56 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <device.h>
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef void (*counter_callback_t)(struct device *dev, void *user_data);
|
|
|
|
|
|
|
|
typedef int (*counter_api_start)(struct device *dev);
|
|
|
|
typedef int (*counter_api_stop)(struct device *dev);
|
2017-04-21 23:55:34 +08:00
|
|
|
typedef u32_t (*counter_api_read)(struct device *dev);
|
2016-02-23 09:10:56 +08:00
|
|
|
typedef int (*counter_api_set_alarm)(struct device *dev,
|
|
|
|
counter_callback_t callback,
|
2017-04-21 23:55:34 +08:00
|
|
|
u32_t count, void *user_data);
|
|
|
|
typedef u32_t (*counter_api_get_pending_int)(struct device *dev);
|
2016-02-23 09:10:56 +08:00
|
|
|
|
|
|
|
struct counter_driver_api {
|
|
|
|
counter_api_start start;
|
|
|
|
counter_api_stop stop;
|
|
|
|
counter_api_read read;
|
|
|
|
counter_api_set_alarm set_alarm;
|
2016-11-03 21:43:36 +08:00
|
|
|
counter_api_get_pending_int get_pending_int;
|
2016-02-23 09:10:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Start counter device in free running mode.
|
|
|
|
*
|
|
|
|
* Start the counter device. If the device is a 'countup' counter, the
|
|
|
|
* counter initial value is set to zero. If it is a 'countdown' counter,
|
|
|
|
* the initial value is set to the maximum value supported by the device.
|
|
|
|
*
|
|
|
|
* @param dev Pointer to the device structure for the driver instance.
|
|
|
|
*
|
2016-03-22 04:02:03 +08:00
|
|
|
* @retval 0 If successful.
|
|
|
|
* @retval Negative errno code if failure.
|
2016-02-23 09:10:56 +08:00
|
|
|
*/
|
2017-10-26 02:59:13 +08:00
|
|
|
__syscall int counter_start(struct device *dev);
|
|
|
|
|
|
|
|
static inline int _impl_counter_start(struct device *dev)
|
2016-02-23 09:10:56 +08:00
|
|
|
{
|
2016-10-22 16:56:21 +08:00
|
|
|
const struct counter_driver_api *api = dev->driver_api;
|
2016-02-23 09:10:56 +08:00
|
|
|
|
|
|
|
return api->start(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Stop counter device.
|
|
|
|
* @param dev Pointer to the device structure for the driver instance.
|
|
|
|
*
|
2016-03-22 04:02:03 +08:00
|
|
|
* @retval 0 If successful.
|
|
|
|
* @retval -ENODEV if the device doesn't support stopping the
|
2016-02-23 09:10:56 +08:00
|
|
|
* counter.
|
|
|
|
*/
|
2017-10-26 02:59:13 +08:00
|
|
|
__syscall int counter_stop(struct device *dev);
|
|
|
|
|
|
|
|
static inline int _impl_counter_stop(struct device *dev)
|
2016-02-23 09:10:56 +08:00
|
|
|
{
|
2016-10-22 16:56:21 +08:00
|
|
|
const struct counter_driver_api *api = dev->driver_api;
|
2016-02-23 09:10:56 +08:00
|
|
|
|
|
|
|
return api->stop(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Read current counter value.
|
|
|
|
* @param dev Pointer to the device structure for the driver instance.
|
|
|
|
*
|
|
|
|
* @return 32-bit value
|
|
|
|
*/
|
2017-10-26 02:59:13 +08:00
|
|
|
__syscall u32_t counter_read(struct device *dev);
|
|
|
|
|
|
|
|
static inline u32_t _impl_counter_read(struct device *dev)
|
2016-02-23 09:10:56 +08:00
|
|
|
{
|
2016-10-22 16:56:21 +08:00
|
|
|
const struct counter_driver_api *api = dev->driver_api;
|
2016-02-23 09:10:56 +08:00
|
|
|
|
2016-11-30 00:35:15 +08:00
|
|
|
return api->read(dev);
|
2016-02-23 09:10:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set an alarm.
|
|
|
|
* @param dev Pointer to the device structure for the driver instance.
|
2017-09-15 23:07:44 +08:00
|
|
|
* @param callback Pointer to the callback function. If this is NULL,
|
2016-02-23 09:10:56 +08:00
|
|
|
* this function unsets the alarm.
|
2017-09-15 23:07:44 +08:00
|
|
|
* @param count Number of counter ticks. This is relative value, meaning
|
|
|
|
* an alarm will be triggered count ticks in the future.
|
2016-02-23 09:10:56 +08:00
|
|
|
* @param user_data Pointer to user data.
|
|
|
|
*
|
2016-03-22 04:02:03 +08:00
|
|
|
* @retval 0 If successful.
|
|
|
|
* @retval -ENOTSUP if the counter was not started yet.
|
|
|
|
* @retval -ENODEV if the device doesn't support interrupt (e.g. free
|
2016-02-23 09:10:56 +08:00
|
|
|
* running counters).
|
2016-03-22 04:02:03 +08:00
|
|
|
* @retval Negative errno code if failure.
|
2016-02-23 09:10:56 +08:00
|
|
|
*/
|
|
|
|
static inline int counter_set_alarm(struct device *dev,
|
|
|
|
counter_callback_t callback,
|
2017-04-21 23:55:34 +08:00
|
|
|
u32_t count, void *user_data)
|
2016-02-23 09:10:56 +08:00
|
|
|
{
|
2016-10-22 16:56:21 +08:00
|
|
|
const struct counter_driver_api *api = dev->driver_api;
|
2016-02-23 09:10:56 +08:00
|
|
|
|
|
|
|
return api->set_alarm(dev, callback, count, user_data);
|
|
|
|
}
|
|
|
|
|
2016-11-03 21:43:36 +08:00
|
|
|
/**
|
|
|
|
* @brief 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.
|
|
|
|
*
|
|
|
|
* @param dev Pointer to the device structure for the driver instance.
|
|
|
|
*
|
|
|
|
* @retval 1 if the counter interrupt is pending.
|
|
|
|
* @retval 0 if no counter interrupt is pending.
|
|
|
|
*/
|
2017-10-26 02:59:13 +08:00
|
|
|
__syscall int counter_get_pending_int(struct device *dev);
|
|
|
|
|
|
|
|
static inline int _impl_counter_get_pending_int(struct device *dev)
|
2016-11-03 21:43:36 +08:00
|
|
|
{
|
|
|
|
struct counter_driver_api *api;
|
|
|
|
|
|
|
|
api = (struct counter_driver_api *)dev->driver_api;
|
|
|
|
return api->get_pending_int(dev);
|
|
|
|
}
|
|
|
|
|
2016-02-23 09:10:56 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2017-10-26 02:59:13 +08:00
|
|
|
#include <syscalls/counter.h>
|
|
|
|
|
2016-02-23 09:10:56 +08:00
|
|
|
#endif /* __COUNTER_H__ */
|