PWM (TCPWM)

group group_tcpwm_pwm

Driver API for PWM.

The functions and other declarations used in this part of the driver are in cy_tcpwm_pwm.h. You can also include cy_pdl.h to get access to all functions and declarations in the PDL.

Use PWM mode when an output square wave is needed with a specific period and duty cycle, such as:

  • Creating arbitrary square wave outputs

  • Driving an LED (changing the brightness)

  • Driving Motors (dead time assertion available)

The PWM has the following features:

  • 16- or 32-bit Counter.

  • Two Programmable Period registers that can be swapped.

  • Two Output Compare registers that can be swapped on overflow and/or underflow.

  • Left Aligned, Right Aligned, Center Aligned, and Asymmetric Aligned modes

  • Continuous or One Shot run modes.

  • Pseudo Random mode.

  • Two PWM outputs with Dead Time insertion, and programmable polarity.

  • Interrupt on Terminal Count and Compare or Capture 0. Interrupt on Compare or Capture 1 is only available in TCPWM Version 2.

  • Start, Reload, Stop, Swap (Capture), and Count Inputs.

  • Immediate Kill, Kill 0 Input, Kill 1 Input, PWM output on Disable, PWM outputs, PWM Dead Time Clocks, Taps Enabled. All these features are only available in TCPWM Version 2.

  • Multiple Components can be synchronized together for applications. such as three phase motor control.

  • Two programmable trigger outputs, trig_out0 and trig_out1 that generate a trigger on one of the following events Overflow, Underflow, Terminal Count, Compare Match 0/1, and PWM output. The available triggers are dependent on the PWM mode. All these features are only available in TCPWM Version 2.

  • Overflow, Underflow, and Comapre(cc_match) trigger outputs are only available in TCPWM Version 1.

  • Second Compare Match (CC_MATCH1) event is only available in TCPWM Version 2.

Configuration Considerations

The PWM configuration can be divided to number of sequential steps listed below:

  • Configure PWM

  • Assign Clock Divider

  • Enable PWM

  • Start PWM

Configure PWM

To configure PWM, provide the configuration parameters in the cy_stc_tcpwm_pwm_config_t structure. The Configuration structure can be modified through software, but if the configurator in ModusToolbox is used then the configuration structure will be updated with the users input. To initialize the driver, call Cy_TCPWM_PWM_Init function providing a pointer to the populated cy_stc_tcpwm_pwm_config_t structure.

For TCPWM V1 Configuration

    /* Scenario: there is need to initialize
     * the first (index = 0) PWM of the TCPWM0 block
     * with the below configuration settings
     */
    cy_stc_tcpwm_pwm_config_t tcpwm_v1_pwm_config =
    {
        /* .pwmMode               = */ CY_TCPWM_PWM_MODE_PWM, /* Simple PWM Mode */
        /* .clockPrescaler        = */ CY_TCPWM_PWM_PRESCALER_DIVBY_4, /* Clk_counter = Clk_input / 4 */
        /* .pwmAlignment          = */ CY_TCPWM_PWM_LEFT_ALIGN, /* PWM signal is left aligned, meaning it starts high */
        /* .deadTimeClocks        = */ 0UL, /* dead time feature is disabled */
        /* .runMode               = */ CY_TCPWM_PWM_CONTINUOUS, /* Wrap around at terminal count. */
        /* .period0               = */ 99UL, /* Period of 100 (0-99). Terminal count event when rolls over back to 0. */
        /* .period1               = */ 199UL, /* Period of 200 (0-199). Terminal count event when rolls over back to 0. */
        /* .enablePeriodSwap      = */ true, /* period swapping feature is enabled */
        /* .compare0              = */ 33UL, /* duty cycle 33% (with period 100) */
        /* .compare1              = */ 66UL, /* duty cycle 33% (with period 200) */
        /* .enableCompareSwap = */ true, /* compare swapping feature is enabled */
        /* .interruptSources  = */ CY_TCPWM_INT_ON_TC, /* interrupt will rise on terminal count */
        /* .invertPWMOut          = */ 0UL, /* Not invert */
        /* .invertPWMOutN         = */ 0UL, /* Not invert */
        /* .killMode              = */ CY_TCPWM_PWM_STOP_ON_KILL, /* PWM stops counting on kill */
        /* .swapInputMode         = */ CY_TCPWM_INPUT_RISINGEDGE, /* This input is NOT used, leave it in default state (CY_TCPWM_INPUT_RISINGEDGE = 0UL) */
        /* .swapInput             = */ CY_TCPWM_INPUT_0,
        /* .reloadInputMode       = */ CY_TCPWM_INPUT_RISINGEDGE, /* This input is NOT used, leave it in default state (CY_TCPWM_INPUT_RISINGEDGE = 0UL) */
        /* .reloadInput           = */ CY_TCPWM_INPUT_0,
        /* .startInputMode        = */ CY_TCPWM_INPUT_RISINGEDGE, /* This input is NOT used, leave it in default state (CY_TCPWM_INPUT_RISINGEDGE = 0UL) */
        /* .startInput            = */ CY_TCPWM_INPUT_0,
        /* .killInputMode         = */ CY_TCPWM_INPUT_RISINGEDGE, /* This input is NOT used, leave it in default state (CY_TCPWM_INPUT_RISINGEDGE = 0UL) */
        /* .killInput             = */ CY_TCPWM_INPUT_0,
        /* .countInputMode        = */ CY_TCPWM_INPUT_LEVEL, /* Set this input to LEVEL and 1 (high logic level) */
        /* .countInput            = */ CY_TCPWM_INPUT_1, /* So the PWM will be enabled forever */
        /* .swapOverflowUnderflow = */ false, /* Use default overflow/underflow behavior */
    };

    #define MY_TCPWM_PWM_NUM   (0UL)
    #define MY_TCPWM_PWM_MASK  (1UL << MY_TCPWM_PWM_NUM)

    if (CY_TCPWM_SUCCESS != Cy_TCPWM_PWM_Init(TCPWM0, MY_TCPWM_PWM_NUM, &tcpwm_v1_pwm_config))
    {
        /* Handle possible errors */
    }

    /* Enable the initialized PWM */
    Cy_TCPWM_PWM_Enable(TCPWM0, MY_TCPWM_PWM_NUM);

    /* Then start the PWM */
    Cy_TCPWM_TriggerReloadOrIndex_Single(TCPWM0, MY_TCPWM_PWM_MASK);
For TCPWM V2 Configuration
    /* Scenario: there is need to initialize
     * the first (index = 0) PWM of the TCPWM0 block
     * with the below configuration settings
     */
    cy_stc_tcpwm_pwm_config_t tcpwm_v2_pwm_config =
    {
        /* .pwmMode               = */ CY_TCPWM_PWM_MODE_PWM, /* Simple PWM Mode */
        /* .clockPrescaler        = */ CY_TCPWM_PWM_PRESCALER_DIVBY_4, /* Clk_counter = Clk_input / 4 */
        /* .pwmAlignment          = */ CY_TCPWM_PWM_LEFT_ALIGN, /* PWM signal is left aligned, meaning it starts high */
        /* .deadTimeClocks        = */ 0UL, /* dead time feature is disabled */
        /* .runMode               = */ CY_TCPWM_PWM_CONTINUOUS, /* Wrap around at terminal count. */
        /* .period0               = */ 99UL, /* Period of 100 (0-99). Terminal count event when rolls over back to 0. */
        /* .period1               = */ 199UL, /* Period of 200 (0-199). Terminal count event when rolls over back to 0. */
        /* .enablePeriodSwap      = */ true, /* period swapping feature is enabled */
        /* .compare0              = */ 33UL, /* duty cycle 33% (with period 100) */
        /* .compare1              = */ 66UL, /* duty cycle 33% (with period 200) */
        /* .enableCompareSwap     = */ true, /* compare swapping feature is enabled */
        /* .interruptSources      = */ CY_TCPWM_INT_ON_TC, /* interrupt will rise on terminal count */
        /* .invertPWMOut          = */ 0UL, /* Not invert */
        /* .invertPWMOutN         = */ 0UL, /* Not invert */
        /* .killMode              = */ CY_TCPWM_PWM_STOP_ON_KILL, /* PWM stops counting on kill */
        /* .swapInputMode         = */ CY_TCPWM_INPUT_RISINGEDGE, /* This input is NOT used, leave it in default state (CY_TCPWM_INPUT_RISINGEDGE = 0UL) */
        /* .swapInput             = */ CY_TCPWM_INPUT_0,
        /* .reloadInputMode       = */ CY_TCPWM_INPUT_RISINGEDGE, /* This input is NOT used, leave it in default state (CY_TCPWM_INPUT_RISINGEDGE = 0UL) */
        /* .reloadInput           = */ CY_TCPWM_INPUT_0,
        /* .startInputMode        = */ CY_TCPWM_INPUT_RISINGEDGE, /* This input is NOT used, leave it in default state (CY_TCPWM_INPUT_RISINGEDGE = 0UL) */
        /* .startInput            = */ CY_TCPWM_INPUT_0,
        /* .killInputMode         = */ CY_TCPWM_INPUT_RISINGEDGE, /* This input is NOT used, leave it in default state (CY_TCPWM_INPUT_RISINGEDGE = 0UL) */
        /* .killInput             = */ CY_TCPWM_INPUT_0,
        /* .countInputMode        = */ CY_TCPWM_INPUT_LEVEL, /* Set this input to LEVEL and 1 (high logic level) */
        /* .countInput            = */ CY_TCPWM_INPUT_1, /* So the PWM will be enabled forever */
        /* .swapOverflowUnderflow = */ false, /* Use default overflow/underflow behavior */
        /* .immediateKill       = */ false,  /* Specifies whether the kill event immediately deactivates the dt_line_out and dt_line_compl_out or with the next module clock */
        /* .tapsEnabled         = */ 0x00UL, /* In pseudo random mode this sets the enabled taps.*/
        /* .compare2            = */ 33UL, /* duty cycle 33% (with period 100) */
        /* .compare3            = */ 66UL, /* duty cycle 33% (with period 200) */
        /* .enableCompare1Swap  = */ true, /* Not implemented yet*/
        /* .compare0MatchUp     = */ true, /* Not implemented yet*/
        /* .compare0MatchDown   = */ true, /* Not implemented yet*/
        /* .compare1MatchUp     = */ true, /* Not implemented yet*/
        /* .compare1MatchDown   = */ true, /* Not implemented yet*/
        /* .kill1InputMode      = */ CY_TCPWM_INPUT_RISINGEDGE, /* This input is NOT used, leave it in default s tate (CY_TCPWM_INPUT_RISINGEDGE = 0UL) */
        /* .kill1Input          = */ CY_TCPWM_INPUT_0,
        /* .pwmOnDisable        = */ CY_TCPWM_PWM_OUTPUT_HIGHZ,     /* Default High impedance mode*/
        /* .trigger0Event       = */ CY_TCPWM_CNT_TRIGGER_ON_DISABLED, /* Disable output trigger0 event generation*/
        /* .trigger1Event       = */ CY_TCPWM_CNT_TRIGGER_ON_DISABLED, /* Disable output trigger1 event generation*/
    };

    #define MY_TCPWM_PWM_NUM   (0UL)
    #define MY_TCPWM_PWM_MASK  (1UL << MY_TCPWM_PWM_NUM)

    if (CY_TCPWM_SUCCESS != Cy_TCPWM_PWM_Init(TCPWM0, MY_TCPWM_PWM_NUM, &tcpwm_v2_pwm_config))
    {
        /* Handle possible errors */
    }

    /* Enable the initialized PWM */
    Cy_TCPWM_PWM_Enable(TCPWM0, MY_TCPWM_PWM_NUM);

    /* Then start the PWM */
    Cy_TCPWM_TriggerReloadOrIndex_Single(TCPWM0, MY_TCPWM_PWM_MASK);

Assign Clock Divider

The clock source must be connected to proper working. Any of the peripheral clock dividers could be used. Use the SysClk (System Clock) driver API to do that.


    /* Scenario: Assign the first 8-bit peripheral clock divider with
     * divide ratio = 100 to the first counter of the TCPWM0 block:
     */

    Cy_SysClk_PeriphAssignDivider(PCLK_TCPWM0_CLOCKS0, CY_SYSCLK_DIV_8_BIT, 0UL);
    Cy_SysClk_PeriphSetDivider(CY_SYSCLK_DIV_8_BIT, 0UL, 99UL);
    Cy_SysClk_PeriphEnableDivider(CY_SYSCLK_DIV_8_BIT, 0UL);

Enable PWM

PWM has to be enabled before starting

    /* Scenario: Enable PWM */
    #define MY_TCPWM_PWM_NUM   (0UL)

    Cy_TCPWM_PWM_Enable(TCPWM0, MY_TCPWM_PWM_NUM);

Start PWM

PWM has to be started

    /* Scenario: Start PWM operation */
    #define MY_TCPWM_PWM_NUM   (0UL)

    Cy_TCPWM_TriggerReloadOrIndex_Single(TCPWM0, MY_TCPWM_PWM_NUM);