16#ifndef ZEPHYR_INCLUDE_DRIVERS_PWM_H_
17#define ZEPHYR_INCLUDE_DRIVERS_PWM_H_
57#define PWM_CAPTURE_TYPE_SHIFT 1U
58#define PWM_CAPTURE_TYPE_MASK (3U << PWM_CAPTURE_TYPE_SHIFT)
59#define PWM_CAPTURE_MODE_SHIFT 3U
60#define PWM_CAPTURE_MODE_MASK (1U << PWM_CAPTURE_MODE_SHIFT)
64#define PWM_CAPTURE_TYPE_PERIOD (1U << PWM_CAPTURE_TYPE_SHIFT)
67#define PWM_CAPTURE_TYPE_PULSE (2U << PWM_CAPTURE_TYPE_SHIFT)
70#define PWM_CAPTURE_TYPE_BOTH (PWM_CAPTURE_TYPE_PERIOD | \
71 PWM_CAPTURE_TYPE_PULSE)
74#define PWM_CAPTURE_MODE_SINGLE (0U << PWM_CAPTURE_MODE_SHIFT)
77#define PWM_CAPTURE_MODE_CONTINUOUS (1U << PWM_CAPTURE_MODE_SHIFT)
87#define PWM_EVENT_TYPE_SHIFT 0U
90#define PWM_EVENT_TYPE_PERIOD (1U << PWM_EVENT_TYPE_SHIFT)
98#define PWM_EVENT_TYPE_FAULT (2U << PWM_EVENT_TYPE_SHIFT)
191#define PWM_DT_SPEC_GET_BY_NAME(node_id, name) \
193 .dev = DEVICE_DT_GET(DT_PWMS_CTLR_BY_NAME(node_id, name)), \
194 .channel = DT_PWMS_CHANNEL_BY_NAME(node_id, name), \
195 .period = DT_PWMS_PERIOD_BY_NAME(node_id, name), \
196 .flags = DT_PWMS_FLAGS_BY_NAME(node_id, name), \
211#define PWM_DT_SPEC_INST_GET_BY_NAME(inst, name) \
212 PWM_DT_SPEC_GET_BY_NAME(DT_DRV_INST(inst), name)
232#define PWM_DT_SPEC_GET_BY_NAME_OR(node_id, name, default_value) \
233 COND_CODE_1(DT_NODE_HAS_PROP(node_id, pwms), \
234 (PWM_DT_SPEC_GET_BY_NAME(node_id, name)), \
251#define PWM_DT_SPEC_INST_GET_BY_NAME_OR(inst, name, default_value) \
252 PWM_DT_SPEC_GET_BY_NAME_OR(DT_DRV_INST(inst), name, default_value)
296#define PWM_DT_SPEC_GET_BY_IDX(node_id, idx) \
298 .dev = DEVICE_DT_GET(DT_PWMS_CTLR_BY_IDX(node_id, idx)), \
299 .channel = DT_PWMS_CHANNEL_BY_IDX(node_id, idx), \
300 .period = DT_PWMS_PERIOD_BY_IDX(node_id, idx), \
301 .flags = DT_PWMS_FLAGS_BY_IDX(node_id, idx), \
315#define PWM_DT_SPEC_INST_GET_BY_IDX(inst, idx) \
316 PWM_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), idx)
335#define PWM_DT_SPEC_GET_BY_IDX_OR(node_id, idx, default_value) \
336 COND_CODE_1(DT_NODE_HAS_PROP(node_id, pwms), \
337 (PWM_DT_SPEC_GET_BY_IDX(node_id, idx)), \
353#define PWM_DT_SPEC_INST_GET_BY_IDX_OR(inst, idx, default_value) \
354 PWM_DT_SPEC_GET_BY_IDX_OR(DT_DRV_INST(inst), idx, default_value)
366#define PWM_DT_SPEC_GET(node_id) PWM_DT_SPEC_GET_BY_IDX(node_id, 0)
378#define PWM_DT_SPEC_INST_GET(inst) PWM_DT_SPEC_GET(DT_DRV_INST(inst))
392#define PWM_DT_SPEC_GET_OR(node_id, default_value) \
393 PWM_DT_SPEC_GET_BY_IDX_OR(node_id, 0, default_value)
407#define PWM_DT_SPEC_INST_GET_OR(inst, default_value) \
408 PWM_DT_SPEC_GET_OR(DT_DRV_INST(inst), default_value)
433 int status,
void *user_data);
486typedef int (*pwm_set_cycles_t)(
const struct device *dev,
uint32_t channel,
494typedef int (*pwm_get_cycles_per_sec_t)(
const struct device *dev,
497#ifdef CONFIG_PWM_CAPTURE
502typedef int (*pwm_configure_capture_t)(
const struct device *dev,
511typedef int (*pwm_enable_capture_t)(
const struct device *dev,
uint32_t channel);
517typedef int (*pwm_disable_capture_t)(
const struct device *dev,
521#ifdef CONFIG_PWM_EVENT
527typedef int (*pwm_manage_event_callback_t)(
const struct device *dev,
531#ifdef CONFIG_PWM_WITH_DMA
536typedef int (*pwm_enable_dma_t)(
const struct device *dev,
uint32_t channel);
542typedef int (*pwm_disable_dma_t)(
const struct device *dev,
uint32_t channel);
546__subsystem
struct pwm_driver_api {
547 pwm_set_cycles_t set_cycles;
548 pwm_get_cycles_per_sec_t get_cycles_per_sec;
549#ifdef CONFIG_PWM_CAPTURE
550 pwm_configure_capture_t configure_capture;
551 pwm_enable_capture_t enable_capture;
552 pwm_disable_capture_t disable_capture;
554#ifdef CONFIG_PWM_EVENT
555 pwm_manage_event_callback_t manage_event_callback;
557#ifdef CONFIG_PWM_WITH_DMA
558 pwm_enable_dma_t enable_dma;
559 pwm_disable_dma_t disable_dma;
598static inline int z_impl_pwm_set_cycles(
const struct device *dev,
602 const struct pwm_driver_api *api =
603 (
const struct pwm_driver_api *)dev->
api;
605 if (pulse > period) {
609 return api->set_cycles(dev, channel, period, pulse,
flags);
626static inline int z_impl_pwm_get_cycles_per_sec(
const struct device *dev,
630 const struct pwm_driver_api *api =
631 (
const struct pwm_driver_api *)dev->
api;
633 return api->get_cycles_per_sec(dev, channel, cycles);
665 period_cycles = (period * cycles_per_sec) /
NSEC_PER_SEC;
754 *usec = temp / cycles_per_sec;
787 *nsec = temp / cycles_per_sec;
792#if defined(CONFIG_PWM_CAPTURE) || defined(__DOXYGEN__)
826 const struct pwm_driver_api *api =
827 (
const struct pwm_driver_api *)dev->
api;
829 if (api->configure_capture ==
NULL) {
833 return api->configure_capture(dev, channel,
flags, cb,
858#ifdef CONFIG_PWM_CAPTURE
859static inline int z_impl_pwm_enable_capture(
const struct device *dev,
862 const struct pwm_driver_api *api =
863 (
const struct pwm_driver_api *)dev->
api;
865 if (api->enable_capture ==
NULL) {
869 return api->enable_capture(dev, channel);
889#ifdef CONFIG_PWM_CAPTURE
890static inline int z_impl_pwm_disable_capture(
const struct device *dev,
893 const struct pwm_driver_api *api =
894 (
const struct pwm_driver_api *)dev->
api;
896 if (api->disable_capture ==
NULL) {
900 return api->disable_capture(dev, channel);
904#if defined(CONFIG_PWM_WITH_DMA) || defined(__DOXYGEN__)
918static inline int z_impl_pwm_enable_dma(
const struct device *dev,
uint32_t channel)
920 const struct pwm_driver_api *api = (
const struct pwm_driver_api *)dev->
api;
922 if (api->enable_dma ==
NULL) {
926 return api->enable_dma(dev, channel);
942static inline int z_impl_pwm_disable_dma(
const struct device *dev,
uint32_t channel)
944 const struct pwm_driver_api *api = (
const struct pwm_driver_api *)dev->
api;
946 if (api->disable_dma ==
NULL) {
950 return api->disable_dma(dev, channel);
1022 &pulse_cycles, timeout);
1077 &pulse_cycles, timeout);
1095#if defined(CONFIG_PWM_EVENT) || defined(__DOXYGEN__)
1108 __ASSERT_NO_MSG(callback !=
NULL);
1109 __ASSERT_NO_MSG(handler !=
NULL);
1131 const struct pwm_driver_api *api = (
const struct pwm_driver_api *)dev->
api;
1133 if (api->manage_event_callback ==
NULL) {
1137 return api->manage_event_callback(dev, callback,
true);
1153 const struct pwm_driver_api *api = (
const struct pwm_driver_api *)dev->
api;
1155 if (api->manage_event_callback ==
NULL) {
1159 return api->manage_event_callback(dev, callback,
false);
1184#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.
static int pwm_add_event_callback(const struct device *dev, struct pwm_event_callback *callback)
Add an application event callback.
Definition pwm.h:1128
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:698
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:429
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:1013
uint16_t pwm_events_t
Provides a type to hold PWM events.
Definition pwm.h:119
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:1068
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:771
static bool pwm_is_ready_dt(const struct pwm_dt_spec *spec)
Validate that the PWM device is ready.
Definition pwm.h:1171
static int pwm_remove_event_callback(const struct device *dev, struct pwm_event_callback *callback)
Remove an application event callback.
Definition pwm.h:1150
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:719
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:821
int pwm_disable_dma(const struct device *dev, uint32_t channel)
Disable DMA requests triggered by PWM cycles for a single PWM channel.
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:652
uint16_t pwm_flags_t
Provides a type to hold PWM configuration flags.
Definition pwm.h:111
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:738
int pwm_enable_dma(const struct device *dev, uint32_t channel)
Enable DMA requests triggered by PWM cycles for a single PWM channel.
void(* pwm_event_callback_handler_t)(const struct device *dev, struct pwm_event_callback *callback, uint32_t channel, pwm_events_t events)
PWM event callback handler function signature.
Definition pwm.h:451
static void pwm_init_event_callback(struct pwm_event_callback *callback, pwm_event_callback_handler_t handler, uint32_t channel, pwm_events_t event_mask)
Helper to initialize a struct pwm_event_callback properly.
Definition pwm.h:1104
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.
struct _snode sys_snode_t
Single-linked list node structure.
Definition slist.h:39
#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:513
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:519
Kernel timeout type.
Definition clock.h:65
Container for PWM information specified in devicetree.
Definition pwm.h:136
pwm_flags_t flags
Flags.
Definition pwm.h:144
uint32_t channel
Channel number.
Definition pwm.h:140
uint32_t period
Period in nanoseconds.
Definition pwm.h:142
const struct device * dev
PWM device instance.
Definition pwm.h:138
PWM event callback structure.
Definition pwm.h:466
uint32_t channel
Channel the callback is interested in.
Definition pwm.h:475
pwm_events_t event_mask
A mask of events the callback is interested in.
Definition pwm.h:478
pwm_event_callback_handler_t handler
Actual callback function being called when relevant.
Definition pwm.h:472