3.41.13 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

NameDescription
PWMx_InitializeInitializes given instance of PWM peripheral
PWMx_ChannelsStartStarts the given PWM channels
PWMx_ChannelsStopStops the given PWM channels
PWMx_ChannelPeriodSetSets the period value of given PWM channel
PWMx_ChannelPeriodGetReads the period value of given PWM channel
PWMx_ChannelDutySetWrites the duty cycle value of given PWM channel
PWMx_ChannelDeadTimeSetWrites dead time values of given PWM channel
PWMx_CompareValueSetWrites the compare value for given PWM peripheral and given comparison unit
PWMx_ChannelCounterEventEnableEnables counter event of given channels
PWMx_ChannelCounterEventDisableDisables counter event of given channels
PWMx_ChannelCounterEventStatusGetDisables counter event of given channels
PWMx_SyncUpdateEnableThis sets the synchronous update unlock bit
PWMx_FaultStatusClearThis function clears the status of the given fault id
PWMx_CallbackRegisterRegisters the function to be called from interrupt
PWMx_ChannelOverrideEnableThis function overrides the PWM output
PWMx_ChannelOverrideDisableThis function select dead-time output as the PWM output

Data types and constants

NameTypeDescription
PWM_CALLBACKTypedefDefines the data type and function signature for the PWM peripheral callback function
PWM_CHANNEL_NUMEnumIdentifies PWM channel number in a PWM peripheral
PWM_CHANNEL_MASKEnumIdentifies PWM channel mask
PWM_COMPAREEnumIdentifies PWM compare unit
PWM_FAULT_IDEnumIdentifies PWM fault input ids