PWM Driver#

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 "sl_gpio.h"

int main( void )
{

  ...

  sl_pwm_instance_t sl_pwm_led0 = {
    .timer    = TIMER0,
    .channel  = 0,
    .port     = SL_GPIO_PORT_A,
    .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);

  ...

}

Modules#

sl_pwm_instance_t

sl_pwm_config_t

Enumerations#

enum
PWM_ACTIVE_HIGH = 0
PWM_ACTIVE_LOW = 1
}

PWM polarity selection.

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.

Enumeration Documentation#

sl_pwm_polarity_t#

sl_pwm_polarity_t

PWM polarity selection.

Enumerator
PWM_ACTIVE_HIGH

PWM polarity active high.

PWM_ACTIVE_LOW

PWM polarity active low.


Definition at line 106 of file platform/driver/pwm/inc/sl_pwm.h

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.


Definition at line 145 of file platform/driver/pwm/inc/sl_pwm.h

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.


Definition at line 157 of file platform/driver/pwm/inc/sl_pwm.h

sl_pwm_start#

void sl_pwm_start (sl_pwm_instance_t * pwm)

Start generating PWM waveform.

Parameters
[in]pwm

PWM driver instance


Definition at line 166 of file platform/driver/pwm/inc/sl_pwm.h

sl_pwm_stop#

void sl_pwm_stop (sl_pwm_instance_t * pwm)

Stop generating PWM waveform.

Parameters
[in]pwm

PWM driver instance


Definition at line 175 of file platform/driver/pwm/inc/sl_pwm.h

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


Definition at line 188 of file platform/driver/pwm/inc/sl_pwm.h

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


Definition at line 201 of file platform/driver/pwm/inc/sl_pwm.h