1.8.14 Pulse Width Modulation Controller (PWM)

The Pulse Width Modulation Controller (PWM) generates output pulses on four channels independently according to parameters defined per channel. Each channel controls two complementary square output waveforms. Characteristics of the output waveforms such as period, duty-cycle, polarity and dead-times are configured through the user interface. Each channel selects and uses one of the clocks provided by the clock generator. The clock generator provides several clocks resulting from the division of the PWM peripheral clock. External triggers can be managed to allow output pulses to be modified in real time. Channels can be linked together as synchronous channels to be able to update their duty-cycle or dead-time at the same time. The PWM provides eight independent comparison units capable of comparing a programmed value to the counter of the synchronous channels (counter of channel 0). These comparisons are intended to generate software interrupts, to trigger pulses on the two independent event lines (in order to synchronize ADC conversions with a lot of flexibility independently of the PWM outputs) and to trigger DMA Controller transfer requests. The PWM provides a fault protection mechanism with eight fault inputs, capable to detect a fault condition and to override the PWM outputs asynchronously (outputs forced to ‘0’, ‘1’ or Hi-Z).

Using The Library

This example shows how to configure the PWM to generate three phase PWM signals with dead time for a motor control application. The duty cycle of the PWM is updated in the interrupt handler.

/* Duty cycle increment value */
#define DUTY_INCREMENT (10U)

/* Save PWM period */
uint16_t period;

/* This function is called after PWM0 counter event */
void PWM_CounterEventHandler(uintptr_t context)
{
    static uint16_t duty0 = 0U;
    static uint16_t duty1 = 2500U;
    static uint16_t duty2 = 5000U;

    PWMx_ChannelDutySet(PWM_CHANNEL_0, duty0);
    PWMx_ChannelDutySet(PWM_CHANNEL_1, duty1);
    PWMx_ChannelDutySet(PWM_CHANNEL_2, duty2);

    /* Increment duty cycle values */
    duty0 += DUTY_INCREMENT;
    duty1 += DUTY_INCREMENT;
    duty2 += DUTY_INCREMENT;

    if (duty0 > period)
        duty0 = 0U;
    if (duty1 > period)
        duty1 = 0U;
    if (duty2 > period)
        duty2 = 0U;
}

int main ( void )
{
    /* Register callback function for Channel 0 counter event */
    PWMx_CallbackRegister(PWM_CounterEventHandler, (uintptr_t)NULL);

    /* Read the period */
    period = PWM0_ChannelPeriodGet(PWM_CHANNEL_0);

    /* Start all synchronous channels by starting channel 0*/
    PWMx_ChannelsStart(PWM_CHANNEL_0_MASK);
}

Library Interface

Pulse Width Modulation Controller peripheral library provides the following interfaces:

Functions

Name Description
PWMx_Initialize Initializes given instance of PWM peripheral
PWMx_ChannelsStart Starts the given PWM channels
PWMx_ChannelsStop Stops the given PWM channels
PWMx_ChannelPeriodSet Sets the period value of given PWM channel
PWMx_ChannelPeriodGet Reads the period value of given PWM channel
PWMx_ChannelDutySet Writes the duty cycle value of given PWM channel
PWMx_ChannelDeadTimeSet Writes dead time values of given PWM channel
PWMx_CompareValueSet Writes the compare value for given PWM peripheral and given comparison unit
PWMx_ChannelCounterEventEnable Enables counter event of given channels
PWMx_ChannelCounterEventDisable Disables counter event of given channels
PWMx_ChannelCounterEventStatusGet Disables counter event of given channels
PWMx_SyncUpdateEnable This sets the synchronous update unlock bit
PWMx_FaultStatusClear This function clears the status of the given fault id
PWMx_CallbackRegister Registers the function to be called from interrupt
PWMx_ChannelOverrideEnable This function overrides the PWM output
PWMx_ChannelOverrideDisable This function select dead-time output as the PWM output

Data types and constants

Name Type Description
PWM_CALLBACK Typedef Defines the data type and function signature for the PWM peripheral callback function
PWM_CHANNEL_NUM Enum Identifies PWM channel number in a PWM peripheral
PWM_CHANNEL_MASK Enum Identifies PWM channel mask
PWM_COMPARE Enum Identifies PWM compare unit
PWM_FAULT_ID Enum Identifies PWM fault input ids