PWM Driver

Description

PWM Driver

The PWM driver uses one or more TIMER peripherals to generate one or more PWM waveform, with configurable frequency, duty cycle, and polarity. Multiple instances of the driver can be created and allocated to their own TIMER channel.

The duty cycle of the PWM waveform can be updated, while the PWM driver is running by calling sl_pwm_set_duty_cycle() , without causing glitches in the output waveform.

Note
If several PWM driver instances are set up to use the same TIMER instance, the PWM frequency of these instances must be the same.

PWM Example Code

Basic example for generating PWM waveform:

#include "sl_pwm.h"
#include "em_gpio.h"
int main( void )
{
...
sl_pwm_instance_t sl_pwm_led0 = {
.timer = TIMER0,
.channel = 0,
.port = gpioPortA,
.pin = 0,
.location = 0,
};
sl_pwm_config_t pwm_led0_config = {
.frequency = 10000,
.polarity = PWM_ACTIVE_HIGH ,
};
// Initialize PWM
sl_pwm_init (&sl_pwm_led0, &pwm_led0_config);
// Set duty cycle to 40%
sl_pwm_set_duty_cycle (&sl_pwm_led0, 40);
// Enable PWM output
sl_pwm_start (&sl_pwm_led0);
...
}

Data Structures

struct sl_pwm_instance
PWM driver instance.
struct sl_pwm_config
PWM driver configuration.

Functions

sl_status_t sl_pwm_init (sl_pwm_instance_t *pwm, sl_pwm_config_t *config)
Initialize PWM driver.
sl_status_t sl_pwm_deinit (sl_pwm_instance_t *pwm)
Deinitialize PWM driver.
void sl_pwm_start (sl_pwm_instance_t *pwm)
Start generating PWM waveform.
void sl_pwm_stop (sl_pwm_instance_t *pwm)
Stop generating PWM waveform.
void sl_pwm_set_duty_cycle (sl_pwm_instance_t *pwm, uint8_t percent)
Set duty cycle for PWM waveform.
uint8_t sl_pwm_get_duty_cycle (sl_pwm_instance_t *pwm)
Set duty cycle for PWM waveform.

Enumerations

enum sl_pwm_polarity_t {
PWM_ACTIVE_HIGH = 0,
PWM_ACTIVE_LOW = 1
}
PWM polarity selection.

Function Documentation

sl_pwm_init()

sl_status_t sl_pwm_init ( sl_pwm_instance_t * pwm,
sl_pwm_config_t * config
)

Initialize PWM driver.

Parameters
[in] pwm PWM driver instance
[in] config Driver configuration
Returns
SL_STATUS_OK if there are no errors.

sl_pwm_deinit()

sl_status_t sl_pwm_deinit ( sl_pwm_instance_t * pwm )

Deinitialize PWM driver.

Parameters
[in] pwm PWM driver instance
Returns
SL_STATUS_OK if there are no errors.

sl_pwm_start()

void sl_pwm_start ( sl_pwm_instance_t * pwm )

Start generating PWM waveform.

Parameters
[in] pwm PWM driver instance

sl_pwm_stop()

void sl_pwm_stop ( sl_pwm_instance_t * pwm )

Stop generating PWM waveform.

Parameters
[in] pwm PWM driver instance

sl_pwm_set_duty_cycle()

void sl_pwm_set_duty_cycle ( sl_pwm_instance_t * pwm,
uint8_t percent
)

Set duty cycle for PWM waveform.

Parameters
[in] pwm PWM driver instance
[in] percent Percent of the PWM period waveform is in the state defined as the active polarity in the driver configuration

sl_pwm_get_duty_cycle()

uint8_t sl_pwm_get_duty_cycle ( sl_pwm_instance_t * pwm )

Set duty cycle for PWM waveform.

Parameters
[in] pwm PWM driver instance
Returns
Percent of the PWM period waveform is in the state defined as the active polarity in the driver configuration

Enumeration Type Documentation

sl_pwm_polarity_t

PWM polarity selection.

Enumerator
PWM_ACTIVE_HIGH

PWM polarity active high.

PWM_ACTIVE_LOW

PWM polarity active low.