14#ifndef ZEPHYR_INCLUDE_DRIVERS_PWM_H_
15#define ZEPHYR_INCLUDE_DRIVERS_PWM_H_
54#define PWM_CAPTURE_TYPE_SHIFT 1U
55#define PWM_CAPTURE_TYPE_MASK (3U << PWM_CAPTURE_TYPE_SHIFT)
56#define PWM_CAPTURE_MODE_SHIFT 3U
57#define PWM_CAPTURE_MODE_MASK (1U << PWM_CAPTURE_MODE_SHIFT)
61#define PWM_CAPTURE_TYPE_PERIOD (1U << PWM_CAPTURE_TYPE_SHIFT)
64#define PWM_CAPTURE_TYPE_PULSE (2U << PWM_CAPTURE_TYPE_SHIFT)
67#define PWM_CAPTURE_TYPE_BOTH (PWM_CAPTURE_TYPE_PERIOD | \
68 PWM_CAPTURE_TYPE_PULSE)
71#define PWM_CAPTURE_MODE_SINGLE (0U << PWM_CAPTURE_MODE_SHIFT)
74#define PWM_CAPTURE_MODE_CONTINUOUS (1U << PWM_CAPTURE_MODE_SHIFT)
159#define PWM_DT_SPEC_GET_BY_NAME(node_id, name) \
161 .dev = DEVICE_DT_GET(DT_PWMS_CTLR_BY_NAME(node_id, name)), \
162 .channel = DT_PWMS_CHANNEL_BY_NAME(node_id, name), \
163 .period = DT_PWMS_PERIOD_BY_NAME(node_id, name), \
164 .flags = DT_PWMS_FLAGS_BY_NAME(node_id, name), \
179#define PWM_DT_SPEC_INST_GET_BY_NAME(inst, name) \
180 PWM_DT_SPEC_GET_BY_NAME(DT_DRV_INST(inst), name)
200#define PWM_DT_SPEC_GET_BY_NAME_OR(node_id, name, default_value) \
201 COND_CODE_1(DT_NODE_HAS_PROP(node_id, pwms), \
202 (PWM_DT_SPEC_GET_BY_NAME(node_id, name)), \
219#define PWM_DT_SPEC_INST_GET_BY_NAME_OR(inst, name, default_value) \
220 PWM_DT_SPEC_GET_BY_NAME_OR(DT_DRV_INST(inst), name, default_value)
264#define PWM_DT_SPEC_GET_BY_IDX(node_id, idx) \
266 .dev = DEVICE_DT_GET(DT_PWMS_CTLR_BY_IDX(node_id, idx)), \
267 .channel = DT_PWMS_CHANNEL_BY_IDX(node_id, idx), \
268 .period = DT_PWMS_PERIOD_BY_IDX(node_id, idx), \
269 .flags = DT_PWMS_FLAGS_BY_IDX(node_id, idx), \
283#define PWM_DT_SPEC_INST_GET_BY_IDX(inst, idx) \
284 PWM_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), idx)
303#define PWM_DT_SPEC_GET_BY_IDX_OR(node_id, idx, default_value) \
304 COND_CODE_1(DT_NODE_HAS_PROP(node_id, pwms), \
305 (PWM_DT_SPEC_GET_BY_IDX(node_id, idx)), \
321#define PWM_DT_SPEC_INST_GET_BY_IDX_OR(inst, idx, default_value) \
322 PWM_DT_SPEC_GET_BY_IDX_OR(DT_DRV_INST(inst), idx, default_value)
334#define PWM_DT_SPEC_GET(node_id) PWM_DT_SPEC_GET_BY_IDX(node_id, 0)
346#define PWM_DT_SPEC_INST_GET(inst) PWM_DT_SPEC_GET(DT_DRV_INST(inst))
360#define PWM_DT_SPEC_GET_OR(node_id, default_value) \
361 PWM_DT_SPEC_GET_BY_IDX_OR(node_id, 0, default_value)
375#define PWM_DT_SPEC_INST_GET_OR(inst, default_value) \
376 PWM_DT_SPEC_GET_OR(DT_DRV_INST(inst), default_value)
401 int status,
void *user_data);
408typedef int (*pwm_set_cycles_t)(
const struct device *dev,
uint32_t channel,
416typedef int (*pwm_get_cycles_per_sec_t)(
const struct device *dev,
419#ifdef CONFIG_PWM_CAPTURE
424typedef int (*pwm_configure_capture_t)(
const struct device *dev,
433typedef int (*pwm_enable_capture_t)(
const struct device *dev,
uint32_t channel);
439typedef int (*pwm_disable_capture_t)(
const struct device *dev,
444__subsystem
struct pwm_driver_api {
445 pwm_set_cycles_t set_cycles;
446 pwm_get_cycles_per_sec_t get_cycles_per_sec;
447#ifdef CONFIG_PWM_CAPTURE
448 pwm_configure_capture_t configure_capture;
449 pwm_enable_capture_t enable_capture;
450 pwm_disable_capture_t disable_capture;
489static inline int z_impl_pwm_set_cycles(
const struct device *dev,
493 const struct pwm_driver_api *api =
494 (
const struct pwm_driver_api *)dev->
api;
496 if (pulse > period) {
500 return api->set_cycles(dev, channel, period, pulse,
flags);
517static inline int z_impl_pwm_get_cycles_per_sec(
const struct device *dev,
521 const struct pwm_driver_api *api =
522 (
const struct pwm_driver_api *)dev->
api;
524 return api->get_cycles_per_sec(dev, channel, cycles);
556 period_cycles = (period * cycles_per_sec) /
NSEC_PER_SEC;
645 *usec = temp / cycles_per_sec;
678 *nsec = temp / cycles_per_sec;
683#if defined(CONFIG_PWM_CAPTURE) || defined(__DOXYGEN__)
717 const struct pwm_driver_api *api =
718 (
const struct pwm_driver_api *)dev->
api;
720 if (api->configure_capture ==
NULL) {
724 return api->configure_capture(dev, channel,
flags, cb,
749#ifdef CONFIG_PWM_CAPTURE
750static inline int z_impl_pwm_enable_capture(
const struct device *dev,
753 const struct pwm_driver_api *api =
754 (
const struct pwm_driver_api *)dev->
api;
756 if (api->enable_capture ==
NULL) {
760 return api->enable_capture(dev, channel);
780#ifdef CONFIG_PWM_CAPTURE
781static inline int z_impl_pwm_disable_capture(
const struct device *dev,
784 const struct pwm_driver_api *api =
785 (
const struct pwm_driver_api *)dev->
api;
787 if (api->disable_capture ==
NULL) {
791 return api->disable_capture(dev, channel);
863 &pulse_cycles, timeout);
918 &pulse_cycles, timeout);
957#include <zephyr/syscalls/pwm.h>
#define NSEC_PER_SEC
number of nanoseconds per second
Definition clock.h:113
#define USEC_PER_SEC
number of microseconds per second
Definition clock.h:110
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:589
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:397
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:854
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:909
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:662
static bool pwm_is_ready_dt(const struct pwm_dt_spec *spec)
Validate that the PWM device is ready.
Definition pwm.h:944
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:610
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:712
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:543
uint16_t pwm_flags_t
Provides a type to hold PWM configuration flags.
Definition pwm.h:87
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:629
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
#define NULL
Definition iar_missing_defs.h:20
flags
Definition parser.h:97
__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:510
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:516
Kernel timeout type.
Definition clock.h:65
Container for PWM information specified in devicetree.
Definition pwm.h:104
pwm_flags_t flags
Flags.
Definition pwm.h:112
uint32_t channel
Channel number.
Definition pwm.h:108
uint32_t period
Period in nanoseconds.
Definition pwm.h:110
const struct device * dev
PWM device instance.
Definition pwm.h:106