2015-09-22 06:39:22 +08:00
|
|
|
/*
|
2016-02-27 04:23:55 +08:00
|
|
|
* Copyright (c) 2016 Intel Corporation.
|
2015-09-22 06:39:22 +08:00
|
|
|
*
|
2017-01-19 09:01:01 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2015-09-22 06:39:22 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2015-10-04 22:01:56 +08:00
|
|
|
* @file
|
|
|
|
* @brief Public PWM Driver APIs
|
2015-09-22 06:39:22 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __PWM_H__
|
|
|
|
#define __PWM_H__
|
|
|
|
|
2015-10-26 18:18:44 +08:00
|
|
|
/**
|
|
|
|
* @brief PWM Interface
|
|
|
|
* @defgroup pwm_interface PWM Interface
|
|
|
|
* @ingroup io_interfaces
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2015-09-22 06:39:22 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define PWM_ACCESS_BY_PIN 0
|
|
|
|
#define PWM_ACCESS_ALL 1
|
|
|
|
|
2016-03-10 01:54:42 +08:00
|
|
|
#include <errno.h>
|
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>
|
2015-09-22 06:39:22 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <device.h>
|
|
|
|
|
2016-09-21 02:57:50 +08:00
|
|
|
/**
|
|
|
|
* @typedef pwm_pin_set_t
|
|
|
|
* @brief Callback API upon setting the pin
|
|
|
|
* See @a pwm_pin_set_cycles() for argument description
|
|
|
|
*/
|
2017-04-21 23:55:34 +08:00
|
|
|
typedef int (*pwm_pin_set_t)(struct device *dev, u32_t pwm,
|
|
|
|
u32_t period_cycles, u32_t pulse_cycles);
|
2016-09-21 02:57:50 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef pwm_get_cycles_per_sec_t
|
|
|
|
* @brief Callback API upon getting cycles per second
|
|
|
|
* See @a pwm_get_cycles_per_sec() for argument description
|
|
|
|
*/
|
2017-04-21 23:55:34 +08:00
|
|
|
typedef int (*pwm_get_cycles_per_sec_t)(struct device *dev, u32_t pwm,
|
|
|
|
u64_t *cycles);
|
2016-09-21 02:57:50 +08:00
|
|
|
|
2016-02-08 02:56:47 +08:00
|
|
|
/** @brief PWM driver API definition. */
|
2015-09-22 06:39:22 +08:00
|
|
|
struct pwm_driver_api {
|
2016-09-21 02:57:50 +08:00
|
|
|
pwm_pin_set_t pin_set;
|
|
|
|
pwm_get_cycles_per_sec_t get_cycles_per_sec;
|
2015-09-22 06:39:22 +08:00
|
|
|
};
|
|
|
|
|
2016-09-21 02:57:50 +08:00
|
|
|
/**
|
|
|
|
* @brief Set the period and pulse width for a single PWM output.
|
|
|
|
*
|
|
|
|
* @param dev Pointer to the device structure for the driver instance.
|
|
|
|
* @param pwm PWM pin.
|
|
|
|
* @param period Period (in clock cycle) set to the PWM. HW specific.
|
|
|
|
* @param pulse Pulse width (in clock cycle) set to the PWM. HW specific.
|
|
|
|
*
|
|
|
|
* @retval 0 If successful.
|
|
|
|
* @retval Negative errno code if failure.
|
|
|
|
*/
|
2017-04-21 23:55:34 +08:00
|
|
|
static inline int pwm_pin_set_cycles(struct device *dev, u32_t pwm,
|
|
|
|
u32_t period, u32_t pulse)
|
2016-09-21 02:57:50 +08:00
|
|
|
{
|
|
|
|
struct pwm_driver_api *api;
|
|
|
|
|
|
|
|
api = (struct pwm_driver_api *)dev->driver_api;
|
|
|
|
return api->pin_set(dev, pwm, period, pulse);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the period and pulse width for a single PWM output.
|
|
|
|
*
|
|
|
|
* @param dev Pointer to the device structure for the driver instance.
|
|
|
|
* @param pwm PWM pin.
|
|
|
|
* @param period Period (in micro second) set to the PWM.
|
|
|
|
* @param pulse Pulse width (in micro second) set to the PWM.
|
|
|
|
*
|
|
|
|
* @retval 0 If successful.
|
|
|
|
* @retval Negative errno code if failure.
|
|
|
|
*/
|
2017-04-21 23:55:34 +08:00
|
|
|
static inline int pwm_pin_set_usec(struct device *dev, u32_t pwm,
|
|
|
|
u32_t period, u32_t pulse)
|
2016-09-21 02:57:50 +08:00
|
|
|
{
|
|
|
|
struct pwm_driver_api *api;
|
2017-04-21 23:55:34 +08:00
|
|
|
u64_t period_cycles, pulse_cycles, cycles_per_sec;
|
2016-09-21 02:57:50 +08:00
|
|
|
|
|
|
|
api = (struct pwm_driver_api *)dev->driver_api;
|
|
|
|
|
|
|
|
if (api->get_cycles_per_sec(dev, pwm, &cycles_per_sec) != 0) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
period_cycles = (period * cycles_per_sec) / USEC_PER_SEC;
|
2017-04-21 23:55:34 +08:00
|
|
|
if (period_cycles >= ((u64_t)1 << 32)) {
|
2016-09-21 02:57:50 +08:00
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
pulse_cycles = (pulse * cycles_per_sec) / USEC_PER_SEC;
|
2017-04-21 23:55:34 +08:00
|
|
|
if (pulse_cycles >= ((u64_t)1 << 32)) {
|
2016-09-21 02:57:50 +08:00
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2017-04-21 23:55:34 +08:00
|
|
|
return api->pin_set(dev, pwm, (u32_t)period_cycles,
|
|
|
|
(u32_t)pulse_cycles);
|
2016-09-21 02:57:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the clock rate (cycles per second) for a single PWM output.
|
|
|
|
*
|
|
|
|
* @param dev Pointer to the device structure for the driver instance.
|
|
|
|
* @param pwm PWM pin.
|
|
|
|
* @param cycles Pointer to the memory to store clock rate (cycles per sec).
|
|
|
|
* HW specific.
|
|
|
|
*
|
|
|
|
* @retval 0 If successful.
|
|
|
|
* @retval Negative errno code if failure.
|
|
|
|
*/
|
2017-04-21 23:55:34 +08:00
|
|
|
static inline int pwm_get_cycles_per_sec(struct device *dev, u32_t pwm,
|
|
|
|
u64_t *cycles)
|
2016-09-21 02:57:50 +08:00
|
|
|
{
|
|
|
|
struct pwm_driver_api *api;
|
|
|
|
|
|
|
|
api = (struct pwm_driver_api *)dev->driver_api;
|
|
|
|
return api->get_cycles_per_sec(dev, pwm, cycles);
|
|
|
|
}
|
|
|
|
|
2016-02-27 04:23:55 +08:00
|
|
|
|
2015-09-22 06:39:22 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-10-26 18:18:44 +08:00
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2015-09-22 06:39:22 +08:00
|
|
|
#endif /* __PWM_H__ */
|