drivers: pwm: add support for inverted PWM signals

Add support for requesting an inverted PWM pulse (active-low) when
setting up the period and pulse width of a PWM pin. This is useful
when driving external, active-low circuitry (e.g. an LED) with a PWM
signal.

All in-tree PWM drivers is updated to match the new API signature, but
no driver support for inverted PWM signals is added yet.

All in-tree PWM consumers are updated to pass a flags value of 0
(0 meaning default, which is normal PWM polarity).

Fixes #21384.

Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
This commit is contained in:
Henrik Brix Andersen 2019-11-13 14:46:37 +01:00 committed by Carles Cufí
commit db611e6781
24 changed files with 152 additions and 38 deletions

View file

@ -23,18 +23,25 @@
#include <zephyr/types.h>
#include <stddef.h>
#include <device.h>
#include <dt-bindings/pwm/pwm.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Provides a type to hold PWM configuration flags.
*/
typedef u8_t pwm_flags_t;
/**
* @typedef pwm_pin_set_t
* @brief Callback API upon setting the pin
* See @a pwm_pin_set_cycles() for argument description
*/
typedef int (*pwm_pin_set_t)(struct device *dev, u32_t pwm,
u32_t period_cycles, u32_t pulse_cycles);
u32_t period_cycles, u32_t pulse_cycles,
pwm_flags_t flags);
/**
* @typedef pwm_get_cycles_per_sec_t
@ -57,20 +64,22 @@ struct pwm_driver_api {
* @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.
* @param flags Flags for pin configuration (polarity).
*
* @retval 0 If successful.
* @retval Negative errno code if failure.
*/
__syscall int pwm_pin_set_cycles(struct device *dev, u32_t pwm,
u32_t period, u32_t pulse);
u32_t period, u32_t pulse, pwm_flags_t flags);
static inline int z_impl_pwm_pin_set_cycles(struct device *dev, u32_t pwm,
u32_t period, u32_t pulse)
u32_t period, u32_t pulse,
pwm_flags_t flags)
{
struct pwm_driver_api *api;
api = (struct pwm_driver_api *)dev->driver_api;
return api->pin_set(dev, pwm, period, pulse);
return api->pin_set(dev, pwm, period, pulse, flags);
}
/**
@ -104,12 +113,14 @@ static inline int z_impl_pwm_get_cycles_per_sec(struct device *dev, u32_t pwm,
* @param pwm PWM pin.
* @param period Period (in microseconds) set to the PWM.
* @param pulse Pulse width (in microseconds) set to the PWM.
* @param flags Flags for pin configuration (polarity).
*
* @retval 0 If successful.
* @retval Negative errno code if failure.
*/
static inline int pwm_pin_set_usec(struct device *dev, u32_t pwm,
u32_t period, u32_t pulse)
u32_t period, u32_t pulse,
pwm_flags_t flags)
{
u64_t period_cycles, pulse_cycles, cycles_per_sec;
@ -128,7 +139,7 @@ static inline int pwm_pin_set_usec(struct device *dev, u32_t pwm,
}
return pwm_pin_set_cycles(dev, pwm, (u32_t)period_cycles,
(u32_t)pulse_cycles);
(u32_t)pulse_cycles, flags);
}
/**
@ -138,12 +149,14 @@ static inline int pwm_pin_set_usec(struct device *dev, u32_t pwm,
* @param pwm PWM pin.
* @param period Period (in nanoseconds) set to the PWM.
* @param pulse Pulse width (in nanoseconds) set to the PWM.
* @param flags Flags for pin configuration (polarity).
*
* @retval 0 If successful.
* @retval Negative errno code if failure.
*/
static inline int pwm_pin_set_nsec(struct device *dev, u32_t pwm,
u32_t period, u32_t pulse)
u32_t period, u32_t pulse,
pwm_flags_t flags)
{
u64_t period_cycles, pulse_cycles, cycles_per_sec;
@ -162,7 +175,7 @@ static inline int pwm_pin_set_nsec(struct device *dev, u32_t pwm,
}
return pwm_pin_set_cycles(dev, pwm, (u32_t)period_cycles,
(u32_t)pulse_cycles);
(u32_t)pulse_cycles, flags);
}
#ifdef __cplusplus

View file

@ -0,0 +1,27 @@
/*
* Copyright (c) 2019 Vestas Wind Systems A/S
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_DT_BINDINGS_PWM_PWM_H_
#define ZEPHYR_INCLUDE_DT_BINDINGS_PWM_PWM_H_
/**
* @name PWM polarity flags
* The `PWM_POLARITY_*` flags are used with pwm_pin_set_cycles(),
* pwm_pin_set_usec(), or pwm_pin_set_nsec() to specify the polarity
* of a PWM pin.
* @{
*/
/** PWM pin normal polarity (active-high pulse). */
#define PWM_POLARITY_NORMAL (0 << 0)
/** PWM pin inverted polarity (active-low pulse). */
#define PWM_POLARITY_INVERTED (1 << 0)
/** @cond INTERNAL_HIDDEN */
#define PWM_POLARITY_MASK 0x1
/** @endcond */
/** @} */
#endif /* ZEPHYR_INCLUDE_DT_BINDINGS_PWM_PWM_H_ */