blob: eee070cc7d94f6cd8b6be56f71200ee9049b2a5c [file] [log] [blame]
Baohong Liuea5b4e42016-02-22 17:10:56 -08001/*
2 * Copyright (c) 2016 Intel Corporation
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Baohong Liuea5b4e42016-02-22 17:10:56 -08005 */
6
7/**
8 * @file
9 * @brief Public API for counter and timer drivers
10 */
11
12#ifndef __COUNTER_H__
13#define __COUNTER_H__
14
15/**
16 * @brief COUNTER Interface
17 * @defgroup counter_interface COUNTER Interface
Daniel Leungf9f10f32016-04-15 11:18:46 -070018 * @ingroup io_interfaces
Baohong Liuea5b4e42016-02-22 17:10:56 -080019 * @{
20 */
21
Kumar Gala78908162017-04-19 10:32:08 -050022#include <zephyr/types.h>
Baohong Liuea5b4e42016-02-22 17:10:56 -080023#include <stddef.h>
24#include <device.h>
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30typedef void (*counter_callback_t)(struct device *dev, void *user_data);
31
32typedef int (*counter_api_start)(struct device *dev);
33typedef int (*counter_api_stop)(struct device *dev);
Kumar Galacc334c72017-04-21 10:55:34 -050034typedef u32_t (*counter_api_read)(struct device *dev);
Baohong Liuea5b4e42016-02-22 17:10:56 -080035typedef int (*counter_api_set_alarm)(struct device *dev,
36 counter_callback_t callback,
Kumar Galacc334c72017-04-21 10:55:34 -050037 u32_t count, void *user_data);
38typedef u32_t (*counter_api_get_pending_int)(struct device *dev);
Baohong Liuea5b4e42016-02-22 17:10:56 -080039
40struct counter_driver_api {
41 counter_api_start start;
42 counter_api_stop stop;
43 counter_api_read read;
44 counter_api_set_alarm set_alarm;
Julien Delayen74799a32016-11-03 13:43:36 +000045 counter_api_get_pending_int get_pending_int;
Baohong Liuea5b4e42016-02-22 17:10:56 -080046};
47
48/**
49 * @brief Start counter device in free running mode.
50 *
51 * Start the counter device. If the device is a 'countup' counter, the
52 * counter initial value is set to zero. If it is a 'countdown' counter,
53 * the initial value is set to the maximum value supported by the device.
54 *
55 * @param dev Pointer to the device structure for the driver instance.
56 *
Andre Guedes4048a592016-03-21 17:02:03 -030057 * @retval 0 If successful.
58 * @retval Negative errno code if failure.
Baohong Liuea5b4e42016-02-22 17:10:56 -080059 */
Andrew Boie678314b2017-10-25 11:59:13 -070060__syscall int counter_start(struct device *dev);
61
62static inline int _impl_counter_start(struct device *dev)
Baohong Liuea5b4e42016-02-22 17:10:56 -080063{
Marcus Shawcroftdf35c242016-10-22 09:56:21 +010064 const struct counter_driver_api *api = dev->driver_api;
Baohong Liuea5b4e42016-02-22 17:10:56 -080065
66 return api->start(dev);
67}
68
69/**
70 * @brief Stop counter device.
71 * @param dev Pointer to the device structure for the driver instance.
72 *
Andre Guedes4048a592016-03-21 17:02:03 -030073 * @retval 0 If successful.
74 * @retval -ENODEV if the device doesn't support stopping the
Baohong Liuea5b4e42016-02-22 17:10:56 -080075 * counter.
76 */
Andrew Boie678314b2017-10-25 11:59:13 -070077__syscall int counter_stop(struct device *dev);
78
79static inline int _impl_counter_stop(struct device *dev)
Baohong Liuea5b4e42016-02-22 17:10:56 -080080{
Marcus Shawcroftdf35c242016-10-22 09:56:21 +010081 const struct counter_driver_api *api = dev->driver_api;
Baohong Liuea5b4e42016-02-22 17:10:56 -080082
83 return api->stop(dev);
84}
85
86/**
87 * @brief Read current counter value.
88 * @param dev Pointer to the device structure for the driver instance.
89 *
90 * @return 32-bit value
91 */
Andrew Boie678314b2017-10-25 11:59:13 -070092__syscall u32_t counter_read(struct device *dev);
93
94static inline u32_t _impl_counter_read(struct device *dev)
Baohong Liuea5b4e42016-02-22 17:10:56 -080095{
Marcus Shawcroftdf35c242016-10-22 09:56:21 +010096 const struct counter_driver_api *api = dev->driver_api;
Baohong Liuea5b4e42016-02-22 17:10:56 -080097
Vincenzo Frascino73ee7e22016-11-29 16:35:15 +000098 return api->read(dev);
Baohong Liuea5b4e42016-02-22 17:10:56 -080099}
100
101/**
102 * @brief Set an alarm.
103 * @param dev Pointer to the device structure for the driver instance.
Michał Kruszewski7dbbb6f2017-09-15 17:07:44 +0200104 * @param callback Pointer to the callback function. If this is NULL,
Baohong Liuea5b4e42016-02-22 17:10:56 -0800105 * this function unsets the alarm.
Michał Kruszewski7dbbb6f2017-09-15 17:07:44 +0200106 * @param count Number of counter ticks. This is relative value, meaning
107 * an alarm will be triggered count ticks in the future.
Baohong Liuea5b4e42016-02-22 17:10:56 -0800108 * @param user_data Pointer to user data.
109 *
Andre Guedes4048a592016-03-21 17:02:03 -0300110 * @retval 0 If successful.
111 * @retval -ENOTSUP if the counter was not started yet.
112 * @retval -ENODEV if the device doesn't support interrupt (e.g. free
Baohong Liuea5b4e42016-02-22 17:10:56 -0800113 * running counters).
Andre Guedes4048a592016-03-21 17:02:03 -0300114 * @retval Negative errno code if failure.
Baohong Liuea5b4e42016-02-22 17:10:56 -0800115 */
116static inline int counter_set_alarm(struct device *dev,
117 counter_callback_t callback,
Kumar Galacc334c72017-04-21 10:55:34 -0500118 u32_t count, void *user_data)
Baohong Liuea5b4e42016-02-22 17:10:56 -0800119{
Marcus Shawcroftdf35c242016-10-22 09:56:21 +0100120 const struct counter_driver_api *api = dev->driver_api;
Baohong Liuea5b4e42016-02-22 17:10:56 -0800121
122 return api->set_alarm(dev, callback, count, user_data);
123}
124
Julien Delayen74799a32016-11-03 13:43:36 +0000125/**
126 * @brief Function to get pending interrupts
127 *
128 * The purpose of this function is to return the interrupt
129 * status register for the device.
130 * This is especially useful when waking up from
131 * low power states to check the wake up source.
132 *
133 * @param dev Pointer to the device structure for the driver instance.
134 *
135 * @retval 1 if the counter interrupt is pending.
136 * @retval 0 if no counter interrupt is pending.
137 */
Andrew Boie678314b2017-10-25 11:59:13 -0700138__syscall int counter_get_pending_int(struct device *dev);
139
140static inline int _impl_counter_get_pending_int(struct device *dev)
Julien Delayen74799a32016-11-03 13:43:36 +0000141{
142 struct counter_driver_api *api;
143
144 api = (struct counter_driver_api *)dev->driver_api;
145 return api->get_pending_int(dev);
146}
147
Baohong Liuea5b4e42016-02-22 17:10:56 -0800148#ifdef __cplusplus
149}
150#endif
151
152/**
153 * @}
154 */
155
Andrew Boie678314b2017-10-25 11:59:13 -0700156#include <syscalls/counter.h>
157
Baohong Liuea5b4e42016-02-22 17:10:56 -0800158#endif /* __COUNTER_H__ */