13#ifndef ZEPHYR_INCLUDE_DRIVERS_PWM_H_
14#define ZEPHYR_INCLUDE_DRIVERS_PWM_H_
48#define PWM_CAPTURE_TYPE_SHIFT 1U
49#define PWM_CAPTURE_TYPE_MASK (3U << PWM_CAPTURE_TYPE_SHIFT)
50#define PWM_CAPTURE_MODE_SHIFT 3U
51#define PWM_CAPTURE_MODE_MASK (1U << PWM_CAPTURE_MODE_SHIFT)
55#define PWM_CAPTURE_TYPE_PERIOD (1U << PWM_CAPTURE_TYPE_SHIFT)
58#define PWM_CAPTURE_TYPE_PULSE (2U << PWM_CAPTURE_TYPE_SHIFT)
61#define PWM_CAPTURE_TYPE_BOTH (PWM_CAPTURE_TYPE_PERIOD | \
62 PWM_CAPTURE_TYPE_PULSE)
65#define PWM_CAPTURE_MODE_SINGLE (0U << PWM_CAPTURE_MODE_SHIFT)
68#define PWM_CAPTURE_MODE_CONTINUOUS (1U << PWM_CAPTURE_MODE_SHIFT)
153#define PWM_DT_SPEC_GET_BY_NAME(node_id, name) \
155 .dev = DEVICE_DT_GET(DT_PWMS_CTLR_BY_NAME(node_id, name)), \
156 .channel = DT_PWMS_CHANNEL_BY_NAME(node_id, name), \
157 .period = DT_PWMS_PERIOD_BY_NAME(node_id, name), \
158 .flags = DT_PWMS_FLAGS_BY_NAME(node_id, name), \
173#define PWM_DT_SPEC_INST_GET_BY_NAME(inst, name) \
174 PWM_DT_SPEC_GET_BY_NAME(DT_DRV_INST(inst), name)
194#define PWM_DT_SPEC_GET_BY_NAME_OR(node_id, name, default_value) \
195 COND_CODE_1(DT_NODE_HAS_PROP(node_id, pwms), \
196 (PWM_DT_SPEC_GET_BY_NAME(node_id, name)), \
213#define PWM_DT_SPEC_INST_GET_BY_NAME_OR(inst, name, default_value) \
214 PWM_DT_SPEC_GET_BY_NAME_OR(DT_DRV_INST(inst), name, default_value)
258#define PWM_DT_SPEC_GET_BY_IDX(node_id, idx) \
260 .dev = DEVICE_DT_GET(DT_PWMS_CTLR_BY_IDX(node_id, idx)), \
261 .channel = DT_PWMS_CHANNEL_BY_IDX(node_id, idx), \
262 .period = DT_PWMS_PERIOD_BY_IDX(node_id, idx), \
263 .flags = DT_PWMS_FLAGS_BY_IDX(node_id, idx), \
277#define PWM_DT_SPEC_INST_GET_BY_IDX(inst, idx) \
278 PWM_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), idx)
297#define PWM_DT_SPEC_GET_BY_IDX_OR(node_id, idx, default_value) \
298 COND_CODE_1(DT_NODE_HAS_PROP(node_id, pwms), \
299 (PWM_DT_SPEC_GET_BY_IDX(node_id, idx)), \
315#define PWM_DT_SPEC_INST_GET_BY_IDX_OR(inst, idx, default_value) \
316 PWM_DT_SPEC_GET_BY_IDX_OR(DT_DRV_INST(inst), idx, default_value)
328#define PWM_DT_SPEC_GET(node_id) PWM_DT_SPEC_GET_BY_IDX(node_id, 0)
340#define PWM_DT_SPEC_INST_GET(inst) PWM_DT_SPEC_GET(DT_DRV_INST(inst))
354#define PWM_DT_SPEC_GET_OR(node_id, default_value) \
355 PWM_DT_SPEC_GET_BY_IDX_OR(node_id, 0, default_value)
369#define PWM_DT_SPEC_INST_GET_OR(inst, default_value) \
370 PWM_DT_SPEC_GET_OR(DT_DRV_INST(inst), default_value)
402typedef int (*pwm_set_cycles_t)(
const struct device *dev,
uint32_t channel,
410typedef int (*pwm_get_cycles_per_sec_t)(
const struct device *dev,
413#ifdef CONFIG_PWM_CAPTURE
418typedef int (*pwm_configure_capture_t)(
const struct device *dev,
427typedef int (*pwm_enable_capture_t)(
const struct device *dev,
uint32_t channel);
433typedef int (*pwm_disable_capture_t)(
const struct device *dev,
438__subsystem
struct pwm_driver_api {
439 pwm_set_cycles_t set_cycles;
440 pwm_get_cycles_per_sec_t get_cycles_per_sec;
441#ifdef CONFIG_PWM_CAPTURE
442 pwm_configure_capture_t configure_capture;
443 pwm_enable_capture_t enable_capture;
444 pwm_disable_capture_t disable_capture;
483static inline int z_impl_pwm_set_cycles(
const struct device *dev,
487 const struct pwm_driver_api *api =
488 (
const struct pwm_driver_api *)dev->
api;
490 if (pulse > period) {
494 return api->set_cycles(dev, channel, period, pulse,
flags);
511static inline int z_impl_pwm_get_cycles_per_sec(
const struct device *dev,
515 const struct pwm_driver_api *api =
516 (
const struct pwm_driver_api *)dev->
api;
518 return api->get_cycles_per_sec(dev, channel, cycles);
550 period_cycles = (period * cycles_per_sec) /
NSEC_PER_SEC;
639 *usec = temp / cycles_per_sec;
672 *nsec = temp / cycles_per_sec;
677#if defined(CONFIG_PWM_CAPTURE) || defined(__DOXYGEN__)
711 const struct pwm_driver_api *api =
712 (
const struct pwm_driver_api *)dev->
api;
714 if (api->configure_capture == NULL) {
718 return api->configure_capture(dev, channel,
flags, cb,
743#ifdef CONFIG_PWM_CAPTURE
744static inline int z_impl_pwm_enable_capture(
const struct device *dev,
747 const struct pwm_driver_api *api =
748 (
const struct pwm_driver_api *)dev->
api;
750 if (api->enable_capture == NULL) {
754 return api->enable_capture(dev, channel);
774#ifdef CONFIG_PWM_CAPTURE
775static inline int z_impl_pwm_disable_capture(
const struct device *dev,
778 const struct pwm_driver_api *api =
779 (
const struct pwm_driver_api *)dev->
api;
781 if (api->disable_capture == NULL) {
785 return api->disable_capture(dev, channel);
951#include <zephyr/syscalls/pwm.h>
ZTEST_BMEM int timeout
Definition main.c:31
#define NSEC_PER_SEC
number of nanoseconds per second
Definition sys_clock.h:107
#define USEC_PER_SEC
number of microseconds per second
Definition sys_clock.h:104
bool device_is_ready(const struct device *dev)
Verify that a device is ready for use.
int pwm_capture_cycles(const struct device *dev, uint32_t channel, pwm_flags_t flags, uint32_t *period, uint32_t *pulse, k_timeout_t timeout)
Capture a single PWM period/pulse width in clock cycles for a single PWM input.
int pwm_disable_capture(const struct device *dev, uint32_t channel)
Disable PWM period/pulse width capture for a single PWM input.
static int pwm_set_dt(const struct pwm_dt_spec *spec, uint32_t period, uint32_t pulse)
Set the period and pulse width in nanoseconds from a struct pwm_dt_spec (with custom period).
Definition pwm.h:583
void(* pwm_capture_callback_handler_t)(const struct device *dev, uint32_t channel, uint32_t period_cycles, uint32_t pulse_cycles, int status, void *user_data)
PWM capture callback handler function signature.
Definition pwm.h:391
int pwm_get_cycles_per_sec(const struct device *dev, uint32_t channel, uint64_t *cycles)
Get the clock rate (cycles per second) for a single PWM output.
static int pwm_capture_usec(const struct device *dev, uint32_t channel, pwm_flags_t flags, uint64_t *period, uint64_t *pulse, k_timeout_t timeout)
Capture a single PWM period/pulse width in microseconds for a single PWM input.
Definition pwm.h:848
static int pwm_capture_nsec(const struct device *dev, uint32_t channel, pwm_flags_t flags, uint64_t *period, uint64_t *pulse, k_timeout_t timeout)
Capture a single PWM period/pulse width in nanoseconds for a single PWM input.
Definition pwm.h:903
static int pwm_cycles_to_nsec(const struct device *dev, uint32_t channel, uint32_t cycles, uint64_t *nsec)
Convert from PWM cycles to nanoseconds.
Definition pwm.h:656
static bool pwm_is_ready_dt(const struct pwm_dt_spec *spec)
Validate that the PWM device is ready.
Definition pwm.h:938
static int pwm_set_pulse_dt(const struct pwm_dt_spec *spec, uint32_t pulse)
Set the period and pulse width in nanoseconds from a struct pwm_dt_spec.
Definition pwm.h:604
static int pwm_configure_capture(const struct device *dev, uint32_t channel, pwm_flags_t flags, pwm_capture_callback_handler_t cb, void *user_data)
Configure PWM period/pulse width capture for a single PWM input.
Definition pwm.h:706
int pwm_enable_capture(const struct device *dev, uint32_t channel)
Enable PWM period/pulse width capture for a single PWM input.
static int pwm_set(const struct device *dev, uint32_t channel, uint32_t period, uint32_t pulse, pwm_flags_t flags)
Set the period and pulse width in nanoseconds for a single PWM output.
Definition pwm.h:537
uint16_t pwm_flags_t
Provides a type to hold PWM configuration flags.
Definition pwm.h:81
static int pwm_cycles_to_usec(const struct device *dev, uint32_t channel, uint32_t cycles, uint64_t *usec)
Convert from PWM cycles to microseconds.
Definition pwm.h:623
int pwm_set_cycles(const struct device *dev, uint32_t channel, uint32_t period, uint32_t pulse, pwm_flags_t flags)
Set the period and pulse width for a single PWM output.
#define EINVAL
Invalid argument.
Definition errno.h:60
#define ENOSYS
Function not implemented.
Definition errno.h:82
#define ENOTSUP
Unsupported value.
Definition errno.h:114
#define ERANGE
Result too large.
Definition errno.h:72
flags
Definition parser.h:96
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
#define UINT32_MAX
Definition stdint.h:29
Runtime device structure (in ROM) per driver instance.
Definition device.h:403
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:409
Kernel timeout type.
Definition sys_clock.h:65
Container for PWM information specified in devicetree.
Definition pwm.h:98
pwm_flags_t flags
Flags.
Definition pwm.h:106
uint32_t channel
Channel number.
Definition pwm.h:102
uint32_t period
Period in nanoseconds.
Definition pwm.h:104
const struct device * dev
PWM device instance.
Definition pwm.h:100
Variables needed for system clock.
static const intptr_t user_data[5]
Definition main.c:588