13#ifndef ZEPHYR_INCLUDE_DRIVERS_PWM_H_
14#define ZEPHYR_INCLUDE_DRIVERS_PWM_H_
53#define PWM_CAPTURE_TYPE_SHIFT 1U
54#define PWM_CAPTURE_TYPE_MASK (3U << PWM_CAPTURE_TYPE_SHIFT)
55#define PWM_CAPTURE_MODE_SHIFT 3U
56#define PWM_CAPTURE_MODE_MASK (1U << PWM_CAPTURE_MODE_SHIFT)
60#define PWM_CAPTURE_TYPE_PERIOD (1U << PWM_CAPTURE_TYPE_SHIFT)
63#define PWM_CAPTURE_TYPE_PULSE (2U << PWM_CAPTURE_TYPE_SHIFT)
66#define PWM_CAPTURE_TYPE_BOTH (PWM_CAPTURE_TYPE_PERIOD | \
67 PWM_CAPTURE_TYPE_PULSE)
70#define PWM_CAPTURE_MODE_SINGLE (0U << PWM_CAPTURE_MODE_SHIFT)
73#define PWM_CAPTURE_MODE_CONTINUOUS (1U << PWM_CAPTURE_MODE_SHIFT)
158#define PWM_DT_SPEC_GET_BY_NAME(node_id, name) \
160 .dev = DEVICE_DT_GET(DT_PWMS_CTLR_BY_NAME(node_id, name)), \
161 .channel = DT_PWMS_CHANNEL_BY_NAME(node_id, name), \
162 .period = DT_PWMS_PERIOD_BY_NAME(node_id, name), \
163 .flags = DT_PWMS_FLAGS_BY_NAME(node_id, name), \
178#define PWM_DT_SPEC_INST_GET_BY_NAME(inst, name) \
179 PWM_DT_SPEC_GET_BY_NAME(DT_DRV_INST(inst), name)
199#define PWM_DT_SPEC_GET_BY_NAME_OR(node_id, name, default_value) \
200 COND_CODE_1(DT_NODE_HAS_PROP(node_id, pwms), \
201 (PWM_DT_SPEC_GET_BY_NAME(node_id, name)), \
218#define PWM_DT_SPEC_INST_GET_BY_NAME_OR(inst, name, default_value) \
219 PWM_DT_SPEC_GET_BY_NAME_OR(DT_DRV_INST(inst), name, default_value)
263#define PWM_DT_SPEC_GET_BY_IDX(node_id, idx) \
265 .dev = DEVICE_DT_GET(DT_PWMS_CTLR_BY_IDX(node_id, idx)), \
266 .channel = DT_PWMS_CHANNEL_BY_IDX(node_id, idx), \
267 .period = DT_PWMS_PERIOD_BY_IDX(node_id, idx), \
268 .flags = DT_PWMS_FLAGS_BY_IDX(node_id, idx), \
282#define PWM_DT_SPEC_INST_GET_BY_IDX(inst, idx) \
283 PWM_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), idx)
302#define PWM_DT_SPEC_GET_BY_IDX_OR(node_id, idx, default_value) \
303 COND_CODE_1(DT_NODE_HAS_PROP(node_id, pwms), \
304 (PWM_DT_SPEC_GET_BY_IDX(node_id, idx)), \
320#define PWM_DT_SPEC_INST_GET_BY_IDX_OR(inst, idx, default_value) \
321 PWM_DT_SPEC_GET_BY_IDX_OR(DT_DRV_INST(inst), idx, default_value)
333#define PWM_DT_SPEC_GET(node_id) PWM_DT_SPEC_GET_BY_IDX(node_id, 0)
345#define PWM_DT_SPEC_INST_GET(inst) PWM_DT_SPEC_GET(DT_DRV_INST(inst))
359#define PWM_DT_SPEC_GET_OR(node_id, default_value) \
360 PWM_DT_SPEC_GET_BY_IDX_OR(node_id, 0, default_value)
374#define PWM_DT_SPEC_INST_GET_OR(inst, default_value) \
375 PWM_DT_SPEC_GET_OR(DT_DRV_INST(inst), default_value)
400 int status,
void *user_data);
407typedef int (*pwm_set_cycles_t)(
const struct device *dev,
uint32_t channel,
415typedef int (*pwm_get_cycles_per_sec_t)(
const struct device *dev,
418#ifdef CONFIG_PWM_CAPTURE
423typedef int (*pwm_configure_capture_t)(
const struct device *dev,
432typedef int (*pwm_enable_capture_t)(
const struct device *dev,
uint32_t channel);
438typedef int (*pwm_disable_capture_t)(
const struct device *dev,
443__subsystem
struct pwm_driver_api {
444 pwm_set_cycles_t set_cycles;
445 pwm_get_cycles_per_sec_t get_cycles_per_sec;
446#ifdef CONFIG_PWM_CAPTURE
447 pwm_configure_capture_t configure_capture;
448 pwm_enable_capture_t enable_capture;
449 pwm_disable_capture_t disable_capture;
488static inline int z_impl_pwm_set_cycles(
const struct device *dev,
492 const struct pwm_driver_api *api =
493 (
const struct pwm_driver_api *)dev->
api;
495 if (pulse > period) {
499 return api->set_cycles(dev, channel, period, pulse,
flags);
516static inline int z_impl_pwm_get_cycles_per_sec(
const struct device *dev,
520 const struct pwm_driver_api *api =
521 (
const struct pwm_driver_api *)dev->
api;
523 return api->get_cycles_per_sec(dev, channel, cycles);
555 period_cycles = (period * cycles_per_sec) /
NSEC_PER_SEC;
644 *usec = temp / cycles_per_sec;
677 *nsec = temp / cycles_per_sec;
682#if defined(CONFIG_PWM_CAPTURE) || defined(__DOXYGEN__)
716 const struct pwm_driver_api *api =
717 (
const struct pwm_driver_api *)dev->
api;
719 if (api->configure_capture ==
NULL) {
723 return api->configure_capture(dev, channel,
flags, cb,
748#ifdef CONFIG_PWM_CAPTURE
749static inline int z_impl_pwm_enable_capture(
const struct device *dev,
752 const struct pwm_driver_api *api =
753 (
const struct pwm_driver_api *)dev->
api;
755 if (api->enable_capture ==
NULL) {
759 return api->enable_capture(dev, channel);
779#ifdef CONFIG_PWM_CAPTURE
780static inline int z_impl_pwm_disable_capture(
const struct device *dev,
783 const struct pwm_driver_api *api =
784 (
const struct pwm_driver_api *)dev->
api;
786 if (api->disable_capture ==
NULL) {
790 return api->disable_capture(dev, channel);
862 &pulse_cycles, timeout);
917 &pulse_cycles, timeout);
956#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:588
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:396
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:853
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:908
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:661
static bool pwm_is_ready_dt(const struct pwm_dt_spec *spec)
Validate that the PWM device is ready.
Definition pwm.h:943
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:609
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:711
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:542
uint16_t pwm_flags_t
Provides a type to hold PWM configuration flags.
Definition pwm.h:86
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:628
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:103
pwm_flags_t flags
Flags.
Definition pwm.h:111
uint32_t channel
Channel number.
Definition pwm.h:107
uint32_t period
Period in nanoseconds.
Definition pwm.h:109
const struct device * dev
PWM device instance.
Definition pwm.h:105