Baohong Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 Intel Corporation |
| 3 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 4 | * SPDX-License-Identifier: Apache-2.0 |
Baohong Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 5 | */ |
| 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 Leung | f9f10f3 | 2016-04-15 11:18:46 -0700 | [diff] [blame] | 18 | * @ingroup io_interfaces |
Baohong Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 19 | * @{ |
| 20 | */ |
| 21 | |
Kumar Gala | 7890816 | 2017-04-19 10:32:08 -0500 | [diff] [blame] | 22 | #include <zephyr/types.h> |
Baohong Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 23 | #include <stddef.h> |
| 24 | #include <device.h> |
| 25 | |
| 26 | #ifdef __cplusplus |
| 27 | extern "C" { |
| 28 | #endif |
| 29 | |
| 30 | typedef void (*counter_callback_t)(struct device *dev, void *user_data); |
| 31 | |
| 32 | typedef int (*counter_api_start)(struct device *dev); |
| 33 | typedef int (*counter_api_stop)(struct device *dev); |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 34 | typedef u32_t (*counter_api_read)(struct device *dev); |
Baohong Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 35 | typedef int (*counter_api_set_alarm)(struct device *dev, |
| 36 | counter_callback_t callback, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 37 | u32_t count, void *user_data); |
| 38 | typedef u32_t (*counter_api_get_pending_int)(struct device *dev); |
Baohong Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 39 | |
| 40 | struct 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 Delayen | 74799a3 | 2016-11-03 13:43:36 +0000 | [diff] [blame] | 45 | counter_api_get_pending_int get_pending_int; |
Baohong Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 46 | }; |
| 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 Guedes | 4048a59 | 2016-03-21 17:02:03 -0300 | [diff] [blame] | 57 | * @retval 0 If successful. |
| 58 | * @retval Negative errno code if failure. |
Baohong Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 59 | */ |
Andrew Boie | 678314b | 2017-10-25 11:59:13 -0700 | [diff] [blame] | 60 | __syscall int counter_start(struct device *dev); |
| 61 | |
| 62 | static inline int _impl_counter_start(struct device *dev) |
Baohong Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 63 | { |
Marcus Shawcroft | df35c24 | 2016-10-22 09:56:21 +0100 | [diff] [blame] | 64 | const struct counter_driver_api *api = dev->driver_api; |
Baohong Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 65 | |
| 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 Guedes | 4048a59 | 2016-03-21 17:02:03 -0300 | [diff] [blame] | 73 | * @retval 0 If successful. |
| 74 | * @retval -ENODEV if the device doesn't support stopping the |
Baohong Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 75 | * counter. |
| 76 | */ |
Andrew Boie | 678314b | 2017-10-25 11:59:13 -0700 | [diff] [blame] | 77 | __syscall int counter_stop(struct device *dev); |
| 78 | |
| 79 | static inline int _impl_counter_stop(struct device *dev) |
Baohong Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 80 | { |
Marcus Shawcroft | df35c24 | 2016-10-22 09:56:21 +0100 | [diff] [blame] | 81 | const struct counter_driver_api *api = dev->driver_api; |
Baohong Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 82 | |
| 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 Boie | 678314b | 2017-10-25 11:59:13 -0700 | [diff] [blame] | 92 | __syscall u32_t counter_read(struct device *dev); |
| 93 | |
| 94 | static inline u32_t _impl_counter_read(struct device *dev) |
Baohong Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 95 | { |
Marcus Shawcroft | df35c24 | 2016-10-22 09:56:21 +0100 | [diff] [blame] | 96 | const struct counter_driver_api *api = dev->driver_api; |
Baohong Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 97 | |
Vincenzo Frascino | 73ee7e2 | 2016-11-29 16:35:15 +0000 | [diff] [blame] | 98 | return api->read(dev); |
Baohong Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @brief Set an alarm. |
| 103 | * @param dev Pointer to the device structure for the driver instance. |
Michał Kruszewski | 7dbbb6f | 2017-09-15 17:07:44 +0200 | [diff] [blame] | 104 | * @param callback Pointer to the callback function. If this is NULL, |
Baohong Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 105 | * this function unsets the alarm. |
Michał Kruszewski | 7dbbb6f | 2017-09-15 17:07:44 +0200 | [diff] [blame] | 106 | * @param count Number of counter ticks. This is relative value, meaning |
| 107 | * an alarm will be triggered count ticks in the future. |
Baohong Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 108 | * @param user_data Pointer to user data. |
| 109 | * |
Andre Guedes | 4048a59 | 2016-03-21 17:02:03 -0300 | [diff] [blame] | 110 | * @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 Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 113 | * running counters). |
Andre Guedes | 4048a59 | 2016-03-21 17:02:03 -0300 | [diff] [blame] | 114 | * @retval Negative errno code if failure. |
Baohong Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 115 | */ |
| 116 | static inline int counter_set_alarm(struct device *dev, |
| 117 | counter_callback_t callback, |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 118 | u32_t count, void *user_data) |
Baohong Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 119 | { |
Marcus Shawcroft | df35c24 | 2016-10-22 09:56:21 +0100 | [diff] [blame] | 120 | const struct counter_driver_api *api = dev->driver_api; |
Baohong Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 121 | |
| 122 | return api->set_alarm(dev, callback, count, user_data); |
| 123 | } |
| 124 | |
Julien Delayen | 74799a3 | 2016-11-03 13:43:36 +0000 | [diff] [blame] | 125 | /** |
| 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 Boie | 678314b | 2017-10-25 11:59:13 -0700 | [diff] [blame] | 138 | __syscall int counter_get_pending_int(struct device *dev); |
| 139 | |
| 140 | static inline int _impl_counter_get_pending_int(struct device *dev) |
Julien Delayen | 74799a3 | 2016-11-03 13:43:36 +0000 | [diff] [blame] | 141 | { |
| 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 Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 148 | #ifdef __cplusplus |
| 149 | } |
| 150 | #endif |
| 151 | |
| 152 | /** |
| 153 | * @} |
| 154 | */ |
| 155 | |
Andrew Boie | 678314b | 2017-10-25 11:59:13 -0700 | [diff] [blame] | 156 | #include <syscalls/counter.h> |
| 157 | |
Baohong Liu | ea5b4e4 | 2016-02-22 17:10:56 -0800 | [diff] [blame] | 158 | #endif /* __COUNTER_H__ */ |