Daniel Leung | 56bfe61 | 2015-09-21 15:39:22 -0700 | [diff] [blame] | 1 | /* |
Jeff Blais | fa095a6 | 2016-02-26 15:23:55 -0500 | [diff] [blame] | 2 | * Copyright (c) 2016 Intel Corporation. |
Daniel Leung | 56bfe61 | 2015-09-21 15:39:22 -0700 | [diff] [blame] | 3 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 4 | * SPDX-License-Identifier: Apache-2.0 |
Daniel Leung | 56bfe61 | 2015-09-21 15:39:22 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /** |
Anas Nashif | 629e69b | 2015-10-04 10:01:56 -0400 | [diff] [blame] | 8 | * @file |
| 9 | * @brief Public PWM Driver APIs |
Daniel Leung | 56bfe61 | 2015-09-21 15:39:22 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #ifndef __PWM_H__ |
| 13 | #define __PWM_H__ |
| 14 | |
Anas Nashif | 75482aa | 2015-10-26 06:18:44 -0400 | [diff] [blame] | 15 | /** |
| 16 | * @brief PWM Interface |
| 17 | * @defgroup pwm_interface PWM Interface |
| 18 | * @ingroup io_interfaces |
| 19 | * @{ |
| 20 | */ |
| 21 | |
Daniel Leung | 56bfe61 | 2015-09-21 15:39:22 -0700 | [diff] [blame] | 22 | #ifdef __cplusplus |
| 23 | extern "C" { |
| 24 | #endif |
| 25 | |
| 26 | #define PWM_ACCESS_BY_PIN 0 |
| 27 | #define PWM_ACCESS_ALL 1 |
| 28 | |
Andre Guedes | 245e140 | 2016-03-09 14:54:42 -0300 | [diff] [blame] | 29 | #include <errno.h> |
Kumar Gala | 7890816 | 2017-04-19 10:32:08 -0500 | [diff] [blame] | 30 | #include <zephyr/types.h> |
Daniel Leung | 56bfe61 | 2015-09-21 15:39:22 -0700 | [diff] [blame] | 31 | #include <stddef.h> |
| 32 | #include <device.h> |
| 33 | |
Inaky Perez-Gonzalez | 0518063 | 2016-06-15 14:18:38 -0700 | [diff] [blame] | 34 | /** |
Baohong Liu | 56e0b53 | 2016-09-20 11:57:50 -0700 | [diff] [blame] | 35 | * @typedef pwm_pin_set_t |
| 36 | * @brief Callback API upon setting the pin |
| 37 | * See @a pwm_pin_set_cycles() for argument description |
| 38 | */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 39 | typedef int (*pwm_pin_set_t)(struct device *dev, u32_t pwm, |
| 40 | u32_t period_cycles, u32_t pulse_cycles); |
Baohong Liu | 56e0b53 | 2016-09-20 11:57:50 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * @typedef pwm_get_cycles_per_sec_t |
| 44 | * @brief Callback API upon getting cycles per second |
| 45 | * See @a pwm_get_cycles_per_sec() for argument description |
| 46 | */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 47 | typedef int (*pwm_get_cycles_per_sec_t)(struct device *dev, u32_t pwm, |
| 48 | u64_t *cycles); |
Baohong Liu | 56e0b53 | 2016-09-20 11:57:50 -0700 | [diff] [blame] | 49 | |
Rodrigo Caballero | a7738f4 | 2016-02-07 12:56:47 -0600 | [diff] [blame] | 50 | /** @brief PWM driver API definition. */ |
Daniel Leung | 56bfe61 | 2015-09-21 15:39:22 -0700 | [diff] [blame] | 51 | struct pwm_driver_api { |
Baohong Liu | 56e0b53 | 2016-09-20 11:57:50 -0700 | [diff] [blame] | 52 | pwm_pin_set_t pin_set; |
| 53 | pwm_get_cycles_per_sec_t get_cycles_per_sec; |
Daniel Leung | 56bfe61 | 2015-09-21 15:39:22 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | /** |
Baohong Liu | 56e0b53 | 2016-09-20 11:57:50 -0700 | [diff] [blame] | 57 | * @brief Set the period and pulse width for a single PWM output. |
| 58 | * |
| 59 | * @param dev Pointer to the device structure for the driver instance. |
| 60 | * @param pwm PWM pin. |
| 61 | * @param period Period (in clock cycle) set to the PWM. HW specific. |
| 62 | * @param pulse Pulse width (in clock cycle) set to the PWM. HW specific. |
| 63 | * |
| 64 | * @retval 0 If successful. |
| 65 | * @retval Negative errno code if failure. |
| 66 | */ |
Andrew Boie | 0f66b9f | 2017-10-25 11:27:37 -0700 | [diff] [blame] | 67 | __syscall int pwm_pin_set_cycles(struct device *dev, u32_t pwm, |
| 68 | u32_t period, u32_t pulse); |
| 69 | |
| 70 | static inline int _impl_pwm_pin_set_cycles(struct device *dev, u32_t pwm, |
| 71 | u32_t period, u32_t pulse) |
Baohong Liu | 56e0b53 | 2016-09-20 11:57:50 -0700 | [diff] [blame] | 72 | { |
| 73 | struct pwm_driver_api *api; |
| 74 | |
| 75 | api = (struct pwm_driver_api *)dev->driver_api; |
| 76 | return api->pin_set(dev, pwm, period, pulse); |
| 77 | } |
| 78 | |
| 79 | /** |
Andrew Boie | 0f66b9f | 2017-10-25 11:27:37 -0700 | [diff] [blame] | 80 | * @brief Get the clock rate (cycles per second) for a single PWM output. |
| 81 | * |
| 82 | * @param dev Pointer to the device structure for the driver instance. |
| 83 | * @param pwm PWM pin. |
| 84 | * @param cycles Pointer to the memory to store clock rate (cycles per sec). |
| 85 | * HW specific. |
| 86 | * |
| 87 | * @retval 0 If successful. |
| 88 | * @retval Negative errno code if failure. |
| 89 | */ |
| 90 | |
| 91 | __syscall int pwm_get_cycles_per_sec(struct device *dev, u32_t pwm, |
| 92 | u64_t *cycles); |
| 93 | |
| 94 | static inline int _impl_pwm_get_cycles_per_sec(struct device *dev, u32_t pwm, |
| 95 | u64_t *cycles) |
| 96 | { |
| 97 | struct pwm_driver_api *api; |
| 98 | |
| 99 | api = (struct pwm_driver_api *)dev->driver_api; |
| 100 | return api->get_cycles_per_sec(dev, pwm, cycles); |
| 101 | } |
| 102 | |
| 103 | /** |
Baohong Liu | 56e0b53 | 2016-09-20 11:57:50 -0700 | [diff] [blame] | 104 | * @brief Set the period and pulse width for a single PWM output. |
| 105 | * |
| 106 | * @param dev Pointer to the device structure for the driver instance. |
| 107 | * @param pwm PWM pin. |
| 108 | * @param period Period (in micro second) set to the PWM. |
| 109 | * @param pulse Pulse width (in micro second) set to the PWM. |
| 110 | * |
| 111 | * @retval 0 If successful. |
| 112 | * @retval Negative errno code if failure. |
| 113 | */ |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 114 | static inline int pwm_pin_set_usec(struct device *dev, u32_t pwm, |
| 115 | u32_t period, u32_t pulse) |
Baohong Liu | 56e0b53 | 2016-09-20 11:57:50 -0700 | [diff] [blame] | 116 | { |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 117 | u64_t period_cycles, pulse_cycles, cycles_per_sec; |
Baohong Liu | 56e0b53 | 2016-09-20 11:57:50 -0700 | [diff] [blame] | 118 | |
Andrew Boie | 0f66b9f | 2017-10-25 11:27:37 -0700 | [diff] [blame] | 119 | if (pwm_get_cycles_per_sec(dev, pwm, &cycles_per_sec) != 0) { |
Baohong Liu | 56e0b53 | 2016-09-20 11:57:50 -0700 | [diff] [blame] | 120 | return -EIO; |
| 121 | } |
| 122 | |
| 123 | period_cycles = (period * cycles_per_sec) / USEC_PER_SEC; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 124 | if (period_cycles >= ((u64_t)1 << 32)) { |
Baohong Liu | 56e0b53 | 2016-09-20 11:57:50 -0700 | [diff] [blame] | 125 | return -ENOTSUP; |
| 126 | } |
| 127 | |
| 128 | pulse_cycles = (pulse * cycles_per_sec) / USEC_PER_SEC; |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 129 | if (pulse_cycles >= ((u64_t)1 << 32)) { |
Baohong Liu | 56e0b53 | 2016-09-20 11:57:50 -0700 | [diff] [blame] | 130 | return -ENOTSUP; |
| 131 | } |
| 132 | |
Andrew Boie | 0f66b9f | 2017-10-25 11:27:37 -0700 | [diff] [blame] | 133 | return pwm_pin_set_cycles(dev, pwm, (u32_t)period_cycles, |
| 134 | (u32_t)pulse_cycles); |
Baohong Liu | 56e0b53 | 2016-09-20 11:57:50 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Daniel Leung | 56bfe61 | 2015-09-21 15:39:22 -0700 | [diff] [blame] | 137 | #ifdef __cplusplus |
| 138 | } |
| 139 | #endif |
| 140 | |
Anas Nashif | 75482aa | 2015-10-26 06:18:44 -0400 | [diff] [blame] | 141 | /** |
| 142 | * @} |
| 143 | */ |
| 144 | |
Andrew Boie | 0f66b9f | 2017-10-25 11:27:37 -0700 | [diff] [blame] | 145 | #include <syscalls/pwm.h> |
| 146 | |
Daniel Leung | 56bfe61 | 2015-09-21 15:39:22 -0700 | [diff] [blame] | 147 | #endif /* __PWM_H__ */ |