15#ifndef ZEPHYR_INCLUDE_DRIVERS_PWM_H_
16#define ZEPHYR_INCLUDE_DRIVERS_PWM_H_
56#define PWM_CAPTURE_TYPE_SHIFT 1U
57#define PWM_CAPTURE_TYPE_MASK (3U << PWM_CAPTURE_TYPE_SHIFT)
58#define PWM_CAPTURE_MODE_SHIFT 3U
59#define PWM_CAPTURE_MODE_MASK (1U << PWM_CAPTURE_MODE_SHIFT)
63#define PWM_CAPTURE_TYPE_PERIOD (1U << PWM_CAPTURE_TYPE_SHIFT)
66#define PWM_CAPTURE_TYPE_PULSE (2U << PWM_CAPTURE_TYPE_SHIFT)
69#define PWM_CAPTURE_TYPE_BOTH (PWM_CAPTURE_TYPE_PERIOD | \
70 PWM_CAPTURE_TYPE_PULSE)
73#define PWM_CAPTURE_MODE_SINGLE (0U << PWM_CAPTURE_MODE_SHIFT)
76#define PWM_CAPTURE_MODE_CONTINUOUS (1U << PWM_CAPTURE_MODE_SHIFT)
86#define PWM_EVENT_TYPE_SHIFT 0U
89#define PWM_EVENT_TYPE_PERIOD (1U << PWM_EVENT_TYPE_SHIFT)
97#define PWM_EVENT_TYPE_FAULT (2U << PWM_EVENT_TYPE_SHIFT)
190#define PWM_DT_SPEC_GET_BY_NAME(node_id, name) \
192 .dev = DEVICE_DT_GET(DT_PWMS_CTLR_BY_NAME(node_id, name)), \
193 .channel = DT_PWMS_CHANNEL_BY_NAME(node_id, name), \
194 .period = DT_PWMS_PERIOD_BY_NAME(node_id, name), \
195 .flags = DT_PWMS_FLAGS_BY_NAME(node_id, name), \
210#define PWM_DT_SPEC_INST_GET_BY_NAME(inst, name) \
211 PWM_DT_SPEC_GET_BY_NAME(DT_DRV_INST(inst), name)
231#define PWM_DT_SPEC_GET_BY_NAME_OR(node_id, name, default_value) \
232 COND_CODE_1(DT_NODE_HAS_PROP(node_id, pwms), \
233 (PWM_DT_SPEC_GET_BY_NAME(node_id, name)), \
250#define PWM_DT_SPEC_INST_GET_BY_NAME_OR(inst, name, default_value) \
251 PWM_DT_SPEC_GET_BY_NAME_OR(DT_DRV_INST(inst), name, default_value)
295#define PWM_DT_SPEC_GET_BY_IDX(node_id, idx) \
297 .dev = DEVICE_DT_GET(DT_PWMS_CTLR_BY_IDX(node_id, idx)), \
298 .channel = DT_PWMS_CHANNEL_BY_IDX(node_id, idx), \
299 .period = DT_PWMS_PERIOD_BY_IDX(node_id, idx), \
300 .flags = DT_PWMS_FLAGS_BY_IDX(node_id, idx), \
314#define PWM_DT_SPEC_INST_GET_BY_IDX(inst, idx) \
315 PWM_DT_SPEC_GET_BY_IDX(DT_DRV_INST(inst), idx)
334#define PWM_DT_SPEC_GET_BY_IDX_OR(node_id, idx, default_value) \
335 COND_CODE_1(DT_NODE_HAS_PROP(node_id, pwms), \
336 (PWM_DT_SPEC_GET_BY_IDX(node_id, idx)), \
352#define PWM_DT_SPEC_INST_GET_BY_IDX_OR(inst, idx, default_value) \
353 PWM_DT_SPEC_GET_BY_IDX_OR(DT_DRV_INST(inst), idx, default_value)
365#define PWM_DT_SPEC_GET(node_id) PWM_DT_SPEC_GET_BY_IDX(node_id, 0)
377#define PWM_DT_SPEC_INST_GET(inst) PWM_DT_SPEC_GET(DT_DRV_INST(inst))
391#define PWM_DT_SPEC_GET_OR(node_id, default_value) \
392 PWM_DT_SPEC_GET_BY_IDX_OR(node_id, 0, default_value)
406#define PWM_DT_SPEC_INST_GET_OR(inst, default_value) \
407 PWM_DT_SPEC_GET_OR(DT_DRV_INST(inst), default_value)
432 int status,
void *user_data);
485typedef int (*pwm_set_cycles_t)(
const struct device *dev,
uint32_t channel,
493typedef int (*pwm_get_cycles_per_sec_t)(
const struct device *dev,
496#ifdef CONFIG_PWM_CAPTURE
501typedef int (*pwm_configure_capture_t)(
const struct device *dev,
510typedef int (*pwm_enable_capture_t)(
const struct device *dev,
uint32_t channel);
516typedef int (*pwm_disable_capture_t)(
const struct device *dev,
520#ifdef CONFIG_PWM_EVENT
526typedef int (*pwm_manage_event_callback_t)(
const struct device *dev,
531__subsystem
struct pwm_driver_api {
532 pwm_set_cycles_t set_cycles;
533 pwm_get_cycles_per_sec_t get_cycles_per_sec;
534#ifdef CONFIG_PWM_CAPTURE
535 pwm_configure_capture_t configure_capture;
536 pwm_enable_capture_t enable_capture;
537 pwm_disable_capture_t disable_capture;
539#ifdef CONFIG_PWM_EVENT
540 pwm_manage_event_callback_t manage_event_callback;
579static inline int z_impl_pwm_set_cycles(
const struct device *dev,
583 const struct pwm_driver_api *api =
584 (
const struct pwm_driver_api *)dev->
api;
586 if (pulse > period) {
590 return api->set_cycles(dev, channel, period, pulse,
flags);
607static inline int z_impl_pwm_get_cycles_per_sec(
const struct device *dev,
611 const struct pwm_driver_api *api =
612 (
const struct pwm_driver_api *)dev->
api;
614 return api->get_cycles_per_sec(dev, channel, cycles);
646 period_cycles = (period * cycles_per_sec) /
NSEC_PER_SEC;
735 *usec = temp / cycles_per_sec;
768 *nsec = temp / cycles_per_sec;
773#if defined(CONFIG_PWM_CAPTURE) || defined(__DOXYGEN__)
807 const struct pwm_driver_api *api =
808 (
const struct pwm_driver_api *)dev->
api;
810 if (api->configure_capture ==
NULL) {
814 return api->configure_capture(dev, channel,
flags, cb,
839#ifdef CONFIG_PWM_CAPTURE
840static inline int z_impl_pwm_enable_capture(
const struct device *dev,
843 const struct pwm_driver_api *api =
844 (
const struct pwm_driver_api *)dev->
api;
846 if (api->enable_capture ==
NULL) {
850 return api->enable_capture(dev, channel);
870#ifdef CONFIG_PWM_CAPTURE
871static inline int z_impl_pwm_disable_capture(
const struct device *dev,
874 const struct pwm_driver_api *api =
875 (
const struct pwm_driver_api *)dev->
api;
877 if (api->disable_capture ==
NULL) {
881 return api->disable_capture(dev, channel);
953 &pulse_cycles, timeout);
1008 &pulse_cycles, timeout);
1026#if defined(CONFIG_PWM_EVENT) || defined(__DOXYGEN__)
1039 __ASSERT_NO_MSG(callback !=
NULL);
1040 __ASSERT_NO_MSG(handler !=
NULL);
1062 const struct pwm_driver_api *api = (
const struct pwm_driver_api *)dev->
api;
1064 if (api->manage_event_callback ==
NULL) {
1068 return api->manage_event_callback(dev, callback,
true);
1084 const struct pwm_driver_api *api = (
const struct pwm_driver_api *)dev->
api;
1086 if (api->manage_event_callback ==
NULL) {
1090 return api->manage_event_callback(dev, callback,
false);
1115#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:1059
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:679
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:428
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:944
uint16_t pwm_events_t
Provides a type to hold PWM events.
Definition pwm.h:118
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:999
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:752
static bool pwm_is_ready_dt(const struct pwm_dt_spec *spec)
Validate that the PWM device is ready.
Definition pwm.h:1102
static int pwm_remove_event_callback(const struct device *dev, struct pwm_event_callback *callback)
Remove an application event callback.
Definition pwm.h:1081
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:700
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:802
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:633
uint16_t pwm_flags_t
Provides a type to hold PWM configuration flags.
Definition pwm.h:110
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:719
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:450
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:1035
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:135
pwm_flags_t flags
Flags.
Definition pwm.h:143
uint32_t channel
Channel number.
Definition pwm.h:139
uint32_t period
Period in nanoseconds.
Definition pwm.h:141
const struct device * dev
PWM device instance.
Definition pwm.h:137
PWM event callback structure.
Definition pwm.h:465
uint32_t channel
Channel the callback is interested in.
Definition pwm.h:474
pwm_events_t event_mask
A mask of events the callback is interested in.
Definition pwm.h:477
pwm_event_callback_handler_t handler
Actual callback function being called when relevant.
Definition pwm.h:471