Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
PWM Interface

PWM Interface. More...

Data Structures

struct  pwm_driver_api
 PWM driver API definition. More...
 

Typedefs

typedef uint8_t pwm_flags_t
 Provides a type to hold PWM configuration flags. More...
 
typedef int(* pwm_pin_set_t) (const struct device *dev, uint32_t pwm, uint32_t period_cycles, uint32_t pulse_cycles, pwm_flags_t flags)
 Callback API upon setting the pin See pwm_pin_set_cycles() for argument description. More...
 
typedef void(* pwm_capture_callback_handler_t) (const struct device *dev, uint32_t pwm, uint32_t period_cycles, uint32_t pulse_cycles, int status, void *user_data)
 PWM capture callback handler function signature. More...
 
typedef int(* pwm_pin_configure_capture_t) (const struct device *dev, uint32_t pwm, pwm_flags_t flags, pwm_capture_callback_handler_t cb, void *user_data)
 Callback API upon configuring PWM pin capture See pwm_pin_configure_capture() for argument description. More...
 
typedef int(* pwm_pin_enable_capture_t) (const struct device *dev, uint32_t pwm)
 Callback API upon enabling PWM pin capture See pwm_pin_enable_capture() for argument description. More...
 
typedef int(* pwm_pin_disable_capture_t) (const struct device *dev, uint32_t pwm)
 Callback API upon disabling PWM pin capture See pwm_pin_disable_capture() for argument description. More...
 
typedef int(* pwm_get_cycles_per_sec_t) (const struct device *dev, uint32_t pwm, uint64_t *cycles)
 Callback API upon getting cycles per second See pwm_get_cycles_per_sec() for argument description. More...
 

Functions

int pwm_pin_set_cycles (const struct device *dev, uint32_t pwm, uint32_t period, uint32_t pulse, pwm_flags_t flags)
 Set the period and pulse width for a single PWM output. More...
 
static int pwm_pin_configure_capture (const struct device *dev, uint32_t pwm, pwm_flags_t flags, pwm_capture_callback_handler_t cb, void *user_data)
 Configure PWM period/pulse width capture for a single PWM input. More...
 
int pwm_pin_enable_capture (const struct device *dev, uint32_t pwm)
 Enable PWM period/pulse width capture for a single PWM input. More...
 
int pwm_pin_disable_capture (const struct device *dev, uint32_t pwm)
 Disable PWM period/pulse width capture for a single PWM input. More...
 
int pwm_pin_capture_cycles (const struct device *dev, uint32_t pwm, 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. More...
 
int pwm_get_cycles_per_sec (const struct device *dev, uint32_t pwm, uint64_t *cycles)
 Get the clock rate (cycles per second) for a single PWM output. More...
 
static int pwm_pin_set_usec (const struct device *dev, uint32_t pwm, uint32_t period, uint32_t pulse, pwm_flags_t flags)
 Set the period and pulse width for a single PWM output. More...
 
static int pwm_pin_set_nsec (const struct device *dev, uint32_t pwm, uint32_t period, uint32_t pulse, pwm_flags_t flags)
 Set the period and pulse width for a single PWM output. More...
 
static int pwm_pin_cycles_to_usec (const struct device *dev, uint32_t pwm, uint32_t cycles, uint64_t *usec)
 Convert from PWM cycles to microseconds. More...
 
static int pwm_pin_cycles_to_nsec (const struct device *dev, uint32_t pwm, uint32_t cycles, uint64_t *nsec)
 Convert from PWM cycles to nanoseconds. More...
 
static int pwm_pin_capture_usec (const struct device *dev, uint32_t pwm, 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. More...
 
static int pwm_pin_capture_nsec (const struct device *dev, uint32_t pwm, 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. More...
 

PWM capture configuration flags

#define PWM_CAPTURE_TYPE_PERIOD   (1U << PWM_CAPTURE_TYPE_SHIFT)
 
#define PWM_CAPTURE_TYPE_PULSE   (2U << PWM_CAPTURE_TYPE_SHIFT)
 
#define PWM_CAPTURE_TYPE_BOTH
 
#define PWM_CAPTURE_MODE_SINGLE   (0U << PWM_CAPTURE_MODE_SHIFT)
 
#define PWM_CAPTURE_MODE_CONTINUOUS   (1U << PWM_CAPTURE_MODE_SHIFT)
 

Detailed Description

PWM Interface.

Macro Definition Documentation

◆ PWM_CAPTURE_MODE_CONTINUOUS

#define PWM_CAPTURE_MODE_CONTINUOUS   (1U << PWM_CAPTURE_MODE_SHIFT)

#include <include/drivers/pwm.h>

PWM pin capture captures period/pulse width continuously.

◆ PWM_CAPTURE_MODE_SINGLE

#define PWM_CAPTURE_MODE_SINGLE   (0U << PWM_CAPTURE_MODE_SHIFT)

#include <include/drivers/pwm.h>

PWM pin capture captures a single period/pulse width.

◆ PWM_CAPTURE_TYPE_BOTH

#define PWM_CAPTURE_TYPE_BOTH

#include <include/drivers/pwm.h>

Value:
PWM_CAPTURE_TYPE_PULSE)
#define PWM_CAPTURE_TYPE_PERIOD
Definition: pwm.h:48

PWM pin capture captures both period and pulse width.

◆ PWM_CAPTURE_TYPE_PERIOD

#define PWM_CAPTURE_TYPE_PERIOD   (1U << PWM_CAPTURE_TYPE_SHIFT)

#include <include/drivers/pwm.h>

PWM pin capture captures period.

◆ PWM_CAPTURE_TYPE_PULSE

#define PWM_CAPTURE_TYPE_PULSE   (2U << PWM_CAPTURE_TYPE_SHIFT)

#include <include/drivers/pwm.h>

PWM pin capture captures pulse width.

Typedef Documentation

◆ pwm_capture_callback_handler_t

pwm_capture_callback_handler_t

#include <include/drivers/pwm.h>

PWM capture callback handler function signature.

Note
The callback handler will be called in interrupt context.
embed:rst:inline :kconfig:`CONFIG_PWM_CAPTURE` 
must be selected to enable PWM capture support.
Parameters
devPointer to the device structure for the driver instance.
pwmPWM pin.
period_cyclesCaptured PWM period width (in clock cycles). HW specific.
pulse_cyclesCaptured PWM pulse width (in clock cycles). HW specific.
statusStatus for the PWM capture (0 if no error, negative errno otherwise. See pwm_pin_capture_cycles() return value descriptions for details).
user_dataUser data passed to pwm_pin_configure_capture()

◆ pwm_flags_t

#include <include/drivers/pwm.h>

Provides a type to hold PWM configuration flags.

◆ pwm_get_cycles_per_sec_t

pwm_get_cycles_per_sec_t

#include <include/drivers/pwm.h>

Callback API upon getting cycles per second See pwm_get_cycles_per_sec() for argument description.

◆ pwm_pin_configure_capture_t

pwm_pin_configure_capture_t

#include <include/drivers/pwm.h>

Callback API upon configuring PWM pin capture See pwm_pin_configure_capture() for argument description.

◆ pwm_pin_disable_capture_t

pwm_pin_disable_capture_t

#include <include/drivers/pwm.h>

Callback API upon disabling PWM pin capture See pwm_pin_disable_capture() for argument description.

◆ pwm_pin_enable_capture_t

pwm_pin_enable_capture_t

#include <include/drivers/pwm.h>

Callback API upon enabling PWM pin capture See pwm_pin_enable_capture() for argument description.

◆ pwm_pin_set_t

pwm_pin_set_t

#include <include/drivers/pwm.h>

Callback API upon setting the pin See pwm_pin_set_cycles() for argument description.

Function Documentation

◆ pwm_get_cycles_per_sec()

int pwm_get_cycles_per_sec ( const struct device dev,
uint32_t  pwm,
uint64_t cycles 
)

#include <include/drivers/pwm.h>

Get the clock rate (cycles per second) for a single PWM output.

Parameters
devPointer to the device structure for the driver instance.
pwmPWM pin.
cyclesPointer to the memory to store clock rate (cycles per sec). HW specific.
Return values
0If successful.
Negativeerrno code if failure.

◆ pwm_pin_capture_cycles()

int pwm_pin_capture_cycles ( const struct device dev,
uint32_t  pwm,
pwm_flags_t  flags,
uint32_t period,
uint32_t pulse,
k_timeout_t  timeout 
)

#include <include/drivers/pwm.h>

Capture a single PWM period/pulse width in clock cycles for a single PWM input.

This API function wraps calls to pwm_pin_configure_capture(), pwm_pin_enable_capture(), and pwm_pin_disable_capture() and passes the capture result to the caller. The function is blocking until either the PWM capture is completed or a timeout occurs.

Note
embed:rst:inline :kconfig:`CONFIG_PWM_CAPTURE` 
must be selected for this function to be available.
Parameters
devPointer to the device structure for the driver instance.
pwmPWM pin.
flagsPWM capture flags.
periodPointer to the memory to store the captured PWM period width (in clock cycles). HW specific.
pulsePointer to the memory to store the captured PWM pulse width (in clock cycles). HW specific.
timeoutWaiting period for the capture to complete.
Return values
0If successful.
-EBUSYPWM capture already in progress.
-EAGAINWaiting period timed out.
-EIOIO error while capturing.
-ERANGEIf result is too large.

◆ pwm_pin_capture_nsec()

static int pwm_pin_capture_nsec ( const struct device dev,
uint32_t  pwm,
pwm_flags_t  flags,
uint64_t period,
uint64_t pulse,
k_timeout_t  timeout 
)
inlinestatic

#include <include/drivers/pwm.h>

Capture a single PWM period/pulse width in nanoseconds for a single PWM input.

This API function wraps calls to pwm_pin_capture_cycles() and pwm_pin_cycles_to_nsec() and passes the capture result to the caller. The function is blocking until either the PWM capture is completed or a timeout occurs.

Note
embed:rst:inline :kconfig:`CONFIG_PWM_CAPTURE` 
must be selected for this function to be available.
Parameters
devPointer to the device structure for the driver instance.
pwmPWM pin.
flagsPWM capture flags.
periodPointer to the memory to store the captured PWM period width (in nsec).
pulsePointer to the memory to store the captured PWM pulse width (in nsec).
timeoutWaiting period for the capture to complete.
Return values
0If successful.
-EBUSYPWM capture already in progress.
-EAGAINWaiting period timed out.
-EIOIO error while capturing.
-ERANGEIf result is too large.

◆ pwm_pin_capture_usec()

static int pwm_pin_capture_usec ( const struct device dev,
uint32_t  pwm,
pwm_flags_t  flags,
uint64_t period,
uint64_t pulse,
k_timeout_t  timeout 
)
inlinestatic

#include <include/drivers/pwm.h>

Capture a single PWM period/pulse width in microseconds for a single PWM input.

This API function wraps calls to pwm_pin_capture_cycles() and pwm_pin_cycles_to_usec() and passes the capture result to the caller. The function is blocking until either the PWM capture is completed or a timeout occurs.

Note
embed:rst:inline :kconfig:`CONFIG_PWM_CAPTURE` 
must be selected for this function to be available.
Parameters
devPointer to the device structure for the driver instance.
pwmPWM pin.
flagsPWM capture flags.
periodPointer to the memory to store the captured PWM period width (in usec).
pulsePointer to the memory to store the captured PWM pulse width (in usec).
timeoutWaiting period for the capture to complete.
Return values
0If successful.
-EBUSYPWM capture already in progress.
-EAGAINWaiting period timed out.
-EIOIO error while capturing.
-ERANGEIf result is too large.

◆ pwm_pin_configure_capture()

static int pwm_pin_configure_capture ( const struct device dev,
uint32_t  pwm,
pwm_flags_t  flags,
pwm_capture_callback_handler_t  cb,
void user_data 
)
inlinestatic

#include <include/drivers/pwm.h>

Configure PWM period/pulse width capture for a single PWM input.

After configuring PWM capture using this function, the capture can be enabled/disabled using pwm_pin_enable_capture() and pwm_pin_disable_capture().

Note
This API function cannot be invoked from user space due to the use of a function callback. In user space, one of the simpler API functions (pwm_pin_capture_cycles(), pwm_pin_capture_usec(), or pwm_pin_capture_nsec()) can be used instead.
embed:rst:inline :kconfig:`CONFIG_PWM_CAPTURE` 
must be selected for this function to be available.
Parameters
devPointer to the device structure for the driver instance.
pwmPWM pin.
flagsPWM capture flags
cbApplication callback handler function to be called upon capture
user_dataUser data to pass to the application callback handler function
Return values
-EINVALif invalid function parameters were given
-ENOSYSif PWM capture is not supported or the given flags are not supported
-EIOif IO error occurred while configuring
-EBUSYif PWM capture is already in progress

◆ pwm_pin_cycles_to_nsec()

static int pwm_pin_cycles_to_nsec ( const struct device dev,
uint32_t  pwm,
uint32_t  cycles,
uint64_t nsec 
)
inlinestatic

#include <include/drivers/pwm.h>

Convert from PWM cycles to nanoseconds.

Parameters
devPointer to the device structure for the driver instance.
pwmPWM pin.
cyclesCycles to be converted.
nsecPointer to the memory to store the calculated nsec.
Return values
0If successful.
-EIOIf cycles per second cannot be determined.
-ERANGEIf result is too large.

◆ pwm_pin_cycles_to_usec()

static int pwm_pin_cycles_to_usec ( const struct device dev,
uint32_t  pwm,
uint32_t  cycles,
uint64_t usec 
)
inlinestatic

#include <include/drivers/pwm.h>

Convert from PWM cycles to microseconds.

Parameters
devPointer to the device structure for the driver instance.
pwmPWM pin.
cyclesCycles to be converted.
usecPointer to the memory to store calculated usec.
Return values
0If successful.
-EIOIf cycles per second cannot be determined.
-ERANGEIf result is too large.

◆ pwm_pin_disable_capture()

int pwm_pin_disable_capture ( const struct device dev,
uint32_t  pwm 
)

#include <include/drivers/pwm.h>

Disable PWM period/pulse width capture for a single PWM input.

Note
embed:rst:inline :kconfig:`CONFIG_PWM_CAPTURE` 
must be selected for this function to be available.
Parameters
devPointer to the device structure for the driver instance.
pwmPWM pin.
Return values
0If successful.
-EINVALif invalid function parameters were given
-ENOSYSif PWM capture is not supported
-EIOif IO error occurred while disabling PWM capture

◆ pwm_pin_enable_capture()

int pwm_pin_enable_capture ( const struct device dev,
uint32_t  pwm 
)

#include <include/drivers/pwm.h>

Enable PWM period/pulse width capture for a single PWM input.

The PWM pin must be configured using pwm_pin_configure_capture() prior to calling this function.

Note
embed:rst:inline :kconfig:`CONFIG_PWM_CAPTURE` 
must be selected for this function to be available.
Parameters
devPointer to the device structure for the driver instance.
pwmPWM pin.
Return values
0If successful.
-EINVALif invalid function parameters were given
-ENOSYSif PWM capture is not supported
-EIOif IO error occurred while enabling PWM capture
-EBUSYif PWM capture is already in progress

◆ pwm_pin_set_cycles()

int pwm_pin_set_cycles ( const struct device dev,
uint32_t  pwm,
uint32_t  period,
uint32_t  pulse,
pwm_flags_t  flags 
)

#include <include/drivers/pwm.h>

Set the period and pulse width for a single PWM output.

The PWM period and pulse width will synchronously be set to the new values without glitches in the PWM signal, but the call will not block for the change to take effect.

Note
Not all PWM controllers support synchronous, glitch-free updates of the PWM period and pulse width. Depending on the hardware, changing the PWM period and/or pulse width may cause a glitch in the generated PWM signal.
Some multi-channel PWM controllers share the PWM period across all channels. Depending on the hardware, changing the PWM period for one channel may affect the PWM period for the other channels of the same PWM controller.

Passing 0 as pulse will cause the pin to be driven to a constant inactive level. Passing a non-zero pulse equal to period will cause the pin to be driven to a constant active level.

Parameters
devPointer to the device structure for the driver instance.
pwmPWM pin.
periodPeriod (in clock cycle) set to the PWM. HW specific.
pulsePulse width (in clock cycle) set to the PWM. HW specific.
flagsFlags for pin configuration (polarity).
Return values
0If successful.
Negativeerrno code if failure.

◆ pwm_pin_set_nsec()

static int pwm_pin_set_nsec ( const struct device dev,
uint32_t  pwm,
uint32_t  period,
uint32_t  pulse,
pwm_flags_t  flags 
)
inlinestatic

#include <include/drivers/pwm.h>

Set the period and pulse width for a single PWM output.

Parameters
devPointer to the device structure for the driver instance.
pwmPWM pin.
periodPeriod (in nanoseconds) set to the PWM.
pulsePulse width (in nanoseconds) set to the PWM.
flagsFlags for pin configuration (polarity).
Return values
0If successful.
Negativeerrno code if failure.

◆ pwm_pin_set_usec()

static int pwm_pin_set_usec ( const struct device dev,
uint32_t  pwm,
uint32_t  period,
uint32_t  pulse,
pwm_flags_t  flags 
)
inlinestatic

#include <include/drivers/pwm.h>

Set the period and pulse width for a single PWM output.

Parameters
devPointer to the device structure for the driver instance.
pwmPWM pin.
periodPeriod (in microseconds) set to the PWM.
pulsePulse width (in microseconds) set to the PWM.
flagsFlags for pin configuration (polarity).
Return values
0If successful.
Negativeerrno code if failure.