blob: 4ea3c0c5df3275152ad46da0b3ba76aefdfe359b [file] [log] [blame]
Daniel Leung56bfe612015-09-21 15:39:22 -07001/*
2 * Copyright (c) 2015 Intel Corporation.
3 *
Javier B Perez Hernandezf7fffae2015-10-06 11:00:37 -05004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Daniel Leung56bfe612015-09-21 15:39:22 -07007 *
Javier B Perez Hernandezf7fffae2015-10-06 11:00:37 -05008 * http://www.apache.org/licenses/LICENSE-2.0
Daniel Leung56bfe612015-09-21 15:39:22 -07009 *
Javier B Perez Hernandezf7fffae2015-10-06 11:00:37 -050010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Daniel Leung56bfe612015-09-21 15:39:22 -070015 */
16
17/**
Anas Nashif629e69b2015-10-04 10:01:56 -040018 * @file
19 * @brief Public PWM Driver APIs
Daniel Leung56bfe612015-09-21 15:39:22 -070020 */
21
22#ifndef __PWM_H__
23#define __PWM_H__
24
Anas Nashif75482aa2015-10-26 06:18:44 -040025/**
26 * @brief PWM Interface
27 * @defgroup pwm_interface PWM Interface
28 * @ingroup io_interfaces
29 * @{
30 */
31
Daniel Leung56bfe612015-09-21 15:39:22 -070032#ifdef __cplusplus
33extern "C" {
34#endif
35
36#define PWM_ACCESS_BY_PIN 0
37#define PWM_ACCESS_ALL 1
38
39#include <stdint.h>
40#include <stddef.h>
41#include <device.h>
42
43/* driver API definition */
44typedef int (*pwm_config_t)(struct device *dev, int access_op,
45 uint32_t pwm, int flags);
46typedef int (*pwm_set_values_t)(struct device *dev, int access_op,
47 uint32_t pwm, uint32_t on, uint32_t off);
48typedef int (*pwm_set_duty_cycle_t)(struct device *dev, int access_op,
49 uint32_t pwm, uint8_t duty);
50typedef int (*pwm_suspend_dev_t)(struct device *dev);
51typedef int (*pwm_resume_dev_t)(struct device *dev);
52
53struct pwm_driver_api {
54 pwm_config_t config;
55 pwm_set_values_t set_values;
56 pwm_set_duty_cycle_t set_duty_cycle;
57 pwm_suspend_dev_t suspend;
58 pwm_resume_dev_t resume;
59};
60
61/**
62 * @brief Configure a single PWM output.
63 *
64 * @param dev Pointer to device structure for driver instance
65 * @param pwm PWM output
66 * @param flags PWM configuration flags
67 *
68 * @return DEV_OK if successful, otherwise failed.
69 */
70static inline int pwm_pin_configure(struct device *dev, uint8_t pwm,
71 int flags)
72{
73 struct pwm_driver_api *api;
74
75 api = (struct pwm_driver_api *)dev->driver_api;
76 return api->config(dev, PWM_ACCESS_BY_PIN, pwm, flags);
77}
78
79/**
80 * @brief Set the ON/OFF values for a single PWM output.
81 *
82 * @param dev Pointer to device structure for driver instance
83 * @param pwm PWM output
84 * @param on ON value to set the PWM to
85 * @param off OFF value to set the PWM to
86 *
87 * @return DEV_OK if successful, otherwise failed.
88 */
89static inline int pwm_pin_set_values(struct device *dev, uint32_t pwm,
90 uint32_t on, uint32_t off)
91{
92 struct pwm_driver_api *api;
93
94 api = (struct pwm_driver_api *)dev->driver_api;
95 return api->set_values(dev, PWM_ACCESS_BY_PIN, pwm, on, off);
96}
97
98/**
99 * @brief Set the duty cycle of a single PWM output.
100 *
101 * This overrides any ON/OFF values being set before.
102 *
103 * @param dev Pointer to device structure for driver instance.
104 * @param pwm PWM output
105 * @param duty Duty cycle to set the PWM to (in %, e.g. 50 => 50%)
106 *
107 * @return DEV_OK if successful, otherwise failed.
108 */
109static inline int pwm_pin_set_duty_cycle(struct device *dev, uint32_t pwm,
110 uint8_t duty)
111{
112 struct pwm_driver_api *api;
113
114 api = (struct pwm_driver_api *)dev->driver_api;
115 return api->set_duty_cycle(dev, PWM_ACCESS_BY_PIN, pwm, duty);
116}
117
118/**
119 * @brief Configure all PWM outputs.
120 *
121 * @param dev Pointer to device structure for driver instance
122 * @param flags PWM configuration flags
123 *
124 * @return DEV_OK if successful, otherwise failed.
125 */
126static inline int pwm_all_configure(struct device *dev, int flags)
127{
128 struct pwm_driver_api *api;
129
130 api = (struct pwm_driver_api *)dev->driver_api;
131 return api->config(dev, PWM_ACCESS_ALL, 0, flags);
132}
133
134/**
135 * @brief Set the ON/OFF values for all PWM outputs.
136 *
137 * @param dev Pointer to device structure for driver instance
138 * @param on ON value to set the PWM to
139 * @param off OFF value to set the PWM to
140 *
141 * @return DEV_OK if successful, otherwise failed.
142 */
143static inline int pwm_all_set_values(struct device *dev,
144 uint32_t on, uint32_t off)
145{
146 struct pwm_driver_api *api;
147
148 api = (struct pwm_driver_api *)dev->driver_api;
149 return api->set_values(dev, PWM_ACCESS_ALL, 0, on, off);
150}
151
152/**
153 * @brief Set the duty cycle of all PWM outputs.
154 *
155 * This overrides any ON/OFF values being set before.
156 *
157 * @param dev Pointer to device structure for driver instance.
Daniel Leung56bfe612015-09-21 15:39:22 -0700158 * @param duty Duty cycle to set the PWM to (in %, e.g. 50 => 50%)
159 *
160 * @return DEV_OK if successful, otherwise failed.
161 */
162static inline int pwm_all_set_duty_cycle(struct device *dev, uint8_t duty)
163{
164 struct pwm_driver_api *api;
165
166 api = (struct pwm_driver_api *)dev->driver_api;
167 return api->set_duty_cycle(dev, PWM_ACCESS_ALL, 0, duty);
168}
169
170/**
171 * @brief Save the state of the device and go to low power state
172 *
173 * @param dev Pointer to device structure for driver instance.
174 *
175 * @return DEV_OK if successful, otherwise failed.
176 */
177static inline int pwm_suspend(struct device *dev)
178{
179 struct pwm_driver_api *api;
180
181 api = (struct pwm_driver_api *)dev->driver_api;
182 return api->suspend(dev);
183}
184
185/**
186 * @brief Restore state stored during suspend and resume operation.
187 *
188 * @param dev Pointer to device structure for driver instance.
189 *
190 * @return DEV_OK if successful, otherwise failed.
191 */
192static inline int pwm_resume(struct device *dev)
193{
194 struct pwm_driver_api *api;
195
196 api = (struct pwm_driver_api *)dev->driver_api;
197 return api->resume(dev);
198}
199
200#ifdef __cplusplus
201}
202#endif
203
Anas Nashif75482aa2015-10-26 06:18:44 -0400204/**
205 * @}
206 */
207
Daniel Leung56bfe612015-09-21 15:39:22 -0700208#endif /* __PWM_H__ */