Skip to content

PWM

Pulse Width Modulation API.

The PWM API can be used to control the duty-cycle of a PWM peripheral Since the timing-parameters are passed in microseconds, the maximum meaningful PWM frequency would be in the range of 100kHz, allowing for 100 different PWM "steps". Functionality like inverting the PWM signal and setting the maximum frequency, if available from the HAL driver, is provided via SysConfig parameters.

Sysprops

syspropdescription
polarity_invertset this to 1 to have the PWM generate an active-low signal

Types

Name
typedef floatpwm_duty_percent_t

Functions Overview

Name
CW_DRIVER_FUNCpwm_enable(int hDev, uint16_t channel)
enable the PWM channel
CW_DRIVER_FUNCpwm_disable(int hDev, uint16_t channel)
disable the PWM output.
CW_DRIVER_FUNCpwm_set_duty_cycle(int hDev, uint16_t channel, pwm_duty_percent_t duty_cycle, uint32_t period_us)
Set the duty cycle and period of a specific PWM channel.
CW_DRIVER_FUNCpwm_set_duty_cycles(int hDev, uint16_t channel, pwm_duty_percent_t * duty_cycles, size_t num_cycles, uint32_t period_us)
Set the duty cycles for a PWM channel.
CW_DRIVER_FUNCpwm_set_duty_cycles_synced(int hDev, pwm_duty_percent_t * duty_ch0, pwm_duty_percent_t * duty_ch1, size_t num_cycles, uint32_t period_us)
Set the duty cycles for a sequence of PWM channels synchronously.

Types Documentation

typedef pwm_duty_percent_t

cpp
typedef float pwm_duty_percent_t;

Function Details

function pwm_enable

cpp
CW_DRIVER_FUNC pwm_enable(
    int hDev,
    uint16_t channel
)

enable the PWM channel

Parameters:

  • hDev device handle as returned by open(const char*)
  • channel some PWM peripheral drivers differentiate different pin outputs via channels. the pin-to-channel assignments will be set in the SysConf. If this is not required by the HAL driver, simply pass 0 here.

when enabled, the pin will output the PWM signal configured by a previous call to pwm_set_duty_cycle or to "0%" if the channel wasn't configured before.

function pwm_disable

cpp
CW_DRIVER_FUNC pwm_disable(
    int hDev,
    uint16_t channel
)

disable the PWM output.

Parameters:

  • hDev device handle as returned by open(const char*)
  • channel some PWM peripheral drivers differentiate different pin outputs via channels. the pin-to-channel assignments will be set in the SysConf. If this is not required by the HAL driver, simply pass 0 here.

disabling the output will effectively result in a low level on the pin, or if the driver supports inversion and it is enabled in the SysConf a high level, respectively.

function pwm_set_duty_cycle

cpp
CW_DRIVER_FUNC pwm_set_duty_cycle(
    int hDev,
    uint16_t channel,
    pwm_duty_percent_t duty_cycle,
    uint32_t period_us
)

Set the duty cycle and period of a specific PWM channel.

Parameters:

  • hDev The handle of the PWM device.
  • channel The channel number of the PWM channel.
  • duty_cycle The duty cycle as a percentage between 0 and 1.
  • period_us The period of the PWM signal in microseconds.

Return: A status code indicating the success of the operation.

This function sets the duty cycle (expressed as a value between 0 and 1) of a specific PWM channel. The PWM channel is identified by its handle and the duty cycle value is provided as a percentage. The period of the PWM signal is given in microseconds. The function behaves identical to separate call of pwm_set_frequency and pwm_set_duty_percentage.

function pwm_set_duty_cycles

cpp
CW_DRIVER_FUNC pwm_set_duty_cycles(
    int hDev,
    uint16_t channel,
    pwm_duty_percent_t * duty_cycles,
    size_t num_cycles,
    uint32_t period_us
)

Set the duty cycles for a PWM channel.

Parameters:

  • hDev The device handle
  • channel The PWM channel number
  • duty_cycles Array of duty cycles (represented as pwm_duty_percent_t values)
  • num_cycles Number of cycles in the array
  • period_us The desired period in microseconds

Return: cw_driver_return_t CW_DRIVER_SUCCESS on success, or an error code on failure

Note:

  • The maximum frequency of the PWM signal depends on the timing parameters passed in. A maximum frequency of approximately 100kHz is recommended.
  • Functionality like inverting the PWM signal and setting the maximum frequency may be available via SysConfig parameters.

This function sets the duty cycles for a specified PWM channel. The duty cycles are represented as an array of pwm_duty_percent_t values, where each value corresponds to a specific cycle. The number of cycles in the array is specified by the num_cycles parameter. The PWM will output the duty cycles consecutively in a loop.

function pwm_set_duty_cycles_synced

cpp
CW_DRIVER_FUNC pwm_set_duty_cycles_synced(
    int hDev,
    pwm_duty_percent_t * duty_ch0,
    pwm_duty_percent_t * duty_ch1,
    size_t num_cycles,
    uint32_t period_us
)

Set the duty cycles for a sequence of PWM channels synchronously.

Parameters:

  • hDev The device handle for the PWM peripheral.
  • channel The channel number of the PWM peripheral.
  • duty_ch0 Pointer to the array of duty cycle values for channel 0.
  • duty_ch1 Pointer to the array of duty cycle values for channel 1.
  • num_cycles The number of cycles in the sequence.
  • period_us The period in microseconds.

Return: CW_DRIVER_OK if the duty cycles were successfully set, an error code otherwise.

This function sets the duty cycles for the synchronized sequential output of a duty-cycle sequence alternating between two channels. This can be used to safely drive an H-bridge for an AC inverter circuit. In that case and since the cycles are output alternatingly, it is sufficient to pass the same half-wave to both channel-buffer.

The duty cycle values are specified as percentages (values 0 .. 1). The function requires a device handle and the channel number to identify the PWM peripheral and channel. The duty cycle values for each channel are passed as arrays. The number of cycles and the period in microseconds are also provided.