Low-level PWM peripheral driver. More...

Detailed Description

Low-level PWM peripheral driver.

This interface enables access to CPU peripherals generating PWM signals. On most platforms, this interface will be implemented based on hardware timers, though some CPUs provide dedicated PWM peripherals.

The characteristics of a PWM signal can be defined by three basic parameters, namely the frequency, the duty cycle, and the operational mode. This interface supports basic PWM generation in left-aligned, right-aligned, and center mode. Additionally the interface supports the definition of the used resolution, defining the granularity with which one can specify the duty cycle. This brings more flexibility to the configuration of the frequency, especially on systems with low system clocks.

Typically, a single PWM device (e.g. hardware timer) supports PWM signal generation on multiple pins in parallel. While the duty cycle is selectable for each channel individually, the frequency and resolution are shared for all channels.

The mapping/configuration of PWM devices (timers) and the used pins has to be done in the board configuration (the board's `periph_conf.h).

When using the PWM interface, first thing you have to do is initialize the PWM device with the targeted mode, frequency, and resolution settings. Once the device is initialized, it will start the generation of PWM signals on all configured pins immediately, with an initial duty cycle of 0. Use the pwm_set() function to change the duty cycle for a given channel. If you want to disable the PWM generation again, simply call pwm_poweroff().

(Low-)Power implications

After initialization, the a PWM peripheral should be powered on and active. When manually stopped using the pwm_poweroff() function, the PWM generation should be stopped for all channels and the PWM peripheral should be fully power off (e.g. through peripheral clock gating). Once being re-enabled by calling the pwm_poweron() function, the PWM peripheral should transparently continue its previously configured operation, including the last active duty cycle values.

While a PWM device is active, some implementations might need to block certain power modes.

Files

file  pwm.h
 Low-level PWM peripheral driver interface definitions.
 

Macros

#define PWM_DEV(x)   (x)
 Default PWM access macro.
 
#define PWM_UNDEF   (UINT_MAX)
 Default PWM undefined value.
 

Typedefs

typedef unsigned int pwm_t
 Default PWM type definition.
 

Enumerations

enum  pwm_mode_t {
  PWM_LEFT = PWM_MODE(0, 1), PWM_RIGHT = PWM_MODE(0, 0), PWM_CENTER = PWM_MODE(1, 1), PWM_CENTER_INV = PWM_MODE(1, 0),
  PWM_LEFT = (TPM_CnSC_MSB_MASK | TPM_CnSC_ELSB_MASK), PWM_RIGHT = (TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK), PWM_CENTER = (TPM_CnSC_MSB_MASK), PWM_LEFT,
  PWM_RIGHT, PWM_CENTER
}
 Default PWM mode definition. More...
 

Functions

uint32_t pwm_init (pwm_t dev, pwm_mode_t mode, uint32_t freq, uint16_t res)
 Initialize a PWM device. More...
 
uint8_t pwm_channels (pwm_t dev)
 Get the number of available channels. More...
 
void pwm_set (pwm_t dev, uint8_t channel, uint16_t value)
 Set the duty-cycle for a given channel of the given PWM device. More...
 
void pwm_poweron (pwm_t dev)
 Resume PWM generation on the given device. More...
 
void pwm_poweroff (pwm_t dev)
 Stop PWM generation on the given device. More...
 

Enumeration Type Documentation

◆ pwm_mode_t

enum pwm_mode_t

Default PWM mode definition.

Enumerator
PWM_LEFT 

left aligned PWM

PWM_RIGHT 

right aligned PWM

PWM_CENTER 

not supported

PWM_CENTER_INV 

not supported

PWM_LEFT 

left aligned

PWM_RIGHT 

right aligned

PWM_CENTER 

center aligned

Definition at line 99 of file pwm.h.

Function Documentation

◆ pwm_channels()

uint8_t pwm_channels ( pwm_t  dev)

Get the number of available channels.

Parameters
[in]devPWM device
Returns
Number of channels available for the given device

◆ pwm_init()

uint32_t pwm_init ( pwm_t  dev,
pwm_mode_t  mode,
uint32_t  freq,
uint16_t  res 
)

Initialize a PWM device.

The PWM module is based on virtual PWM devices, which can have one or more channels. The PWM devices can be configured to run with a given frequency and resolution, which are always identical for the complete device, hence for every channel on a device.

The desired frequency and resolution may not be possible on a given device when chosen too large. In this case the PWM driver will always keep the resolution and decrease the frequency if needed. To verify the correct settings compare the returned value which is the actually set frequency.

Parameters
[in]devPWM device to initialize
[in]modePWM mode, left, right or center aligned
[in]freqPWM frequency in Hz
[in]resPWM resolution
Returns
actual PWM frequency on success
0 on error

◆ pwm_poweroff()

void pwm_poweroff ( pwm_t  dev)

Stop PWM generation on the given device.

This function stops the PWM generation on all configured channels for the given device and powers down the given PWM peripheral.

Parameters
[in]devdevice to stop

◆ pwm_poweron()

void pwm_poweron ( pwm_t  dev)

Resume PWM generation on the given device.

When this function is called, the given PWM device is powered on and continues its previously configured operation. The duty cycle of each channel will be the value that was last set.

This function must not be called before the PWM device was initialized.

Parameters
[in]devdevice to start

◆ pwm_set()

void pwm_set ( pwm_t  dev,
uint8_t  channel,
uint16_t  value 
)

Set the duty-cycle for a given channel of the given PWM device.

The duty-cycle is set in relation to the chosen resolution of the given device. If value > resolution, value is set to resolution.

Parameters
[in]devthe PWM device to set
[in]channelthe channel of the given device to set
[in]valuethe desired duty-cycle to set