PSOC E8XXGP Device Support Library

| PSOC E8XXGP Device Support Library

API Reference | Macros
PWM (TCPWM)
PDL API Reference » TCPWM (Timer Counter PWM)

## General Description

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 Compare(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_TriggerStart_Single(TCPWM0, MY_TCPWM_PWM_NUM);
cy_stc_tcpwm_pwm_config_t
PWM configuration structure.
Definition: cy_tcpwm_pwm.h:183
CY_TCPWM_SUCCESS
@ CY_TCPWM_SUCCESS
Successful.
Definition: cy_tcpwm.h:402
Cy_TCPWM_TriggerStart_Single
__STATIC_INLINE void Cy_TCPWM_TriggerStart_Single(TCPWM_Type *base, uint32_t cntNum)
Triggers a software start on the selected TCPWM.
Definition: cy_tcpwm.h:891
Cy_TCPWM_PWM_Init
cy_en_tcpwm_status_t Cy_TCPWM_PWM_Init(TCPWM_Type *base, uint32_t cntNum, cy_stc_tcpwm_pwm_config_t const *config)
Initializes the counter in the TCPWM block for the PWM operation.
Definition: cy_tcpwm_pwm.c:76
Cy_TCPWM_PWM_Enable
__STATIC_INLINE void Cy_TCPWM_PWM_Enable(TCPWM_Type *base, uint32_t cntNum)
Enables the counter in the TCPWM block for the PWM operation.
Definition: cy_tcpwm_pwm.h:521
CY_TCPWM_INPUT_RISINGEDGE
#define CY_TCPWM_INPUT_RISINGEDGE
A rising edge triggers the event (Capture, Start, Reload, etc..)
Definition: cy_tcpwm.h:266
CY_TCPWM_INPUT_LEVEL
#define CY_TCPWM_INPUT_LEVEL
The event is triggered on each edge of the TCPWM clock if the input is high.
Definition: cy_tcpwm.h:272
CY_TCPWM_INPUT_1
#define CY_TCPWM_INPUT_1
Input is tied to logic 1.
Definition: cy_tcpwm.h:229
CY_TCPWM_INPUT_0
#define CY_TCPWM_INPUT_0
Input is tied to logic 0.
Definition: cy_tcpwm.h:228
CY_TCPWM_INT_ON_TC
#define CY_TCPWM_INT_ON_TC
Interrupt on Terminal count(TC)
Definition: cy_tcpwm.h:306
CY_TCPWM_PWM_LEFT_ALIGN
#define CY_TCPWM_PWM_LEFT_ALIGN
PWM is left aligned, meaning it starts high.
Definition: cy_tcpwm_pwm.h:285
CY_TCPWM_PWM_PRESCALER_DIVBY_4
#define CY_TCPWM_PWM_PRESCALER_DIVBY_4
Divide by 4.
Definition: cy_tcpwm_pwm.h:328
CY_TCPWM_PWM_STOP_ON_KILL
#define CY_TCPWM_PWM_STOP_ON_KILL
PWM stops counting on kill.
Definition: cy_tcpwm_pwm.h:299
CY_TCPWM_PWM_MODE_PWM
#define CY_TCPWM_PWM_MODE_PWM
Standard PWM Mode.
Definition: cy_tcpwm_pwm.h:276
CY_TCPWM_PWM_CONTINUOUS
#define CY_TCPWM_PWM_CONTINUOUS
Counter runs forever.
Definition: cy_tcpwm_pwm.h:269

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*/
/* .reloadLineSelect = */ true, /* Configures how the reload line select */
/* line_out_sel = */ CY_TCPWM_OUTPUT_CONSTANT_0,
/* linecompl_out_sel = */ CY_TCPWM_OUTPUT_CONSTANT_0,
/* line_out_sel_buff = */ CY_TCPWM_OUTPUT_CONSTANT_0,
/* linecompl_out_sel_buff = */ CY_TCPWM_OUTPUT_CONSTANT_0,
/* deadTimeClocks_linecompl_out = */ 5U,
};
#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_TriggerStart_Single(TCPWM0, MY_TCPWM_PWM_NUM);
CY_TCPWM_OUTPUT_CONSTANT_0
@ CY_TCPWM_OUTPUT_CONSTANT_0
Output signal is 0.
Definition: cy_tcpwm_pwm.h:172
CY_TCPWM_CNT_TRIGGER_ON_DISABLED
#define CY_TCPWM_CNT_TRIGGER_ON_DISABLED
Output trigger disabled.
Definition: cy_tcpwm.h:297
CY_TCPWM_PWM_OUTPUT_HIGHZ
#define CY_TCPWM_PWM_OUTPUT_HIGHZ
PWM output (default) high impedance.
Definition: cy_tcpwm_pwm.h:308

## 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.

## 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_TriggerStart_Single(TCPWM0, MY_TCPWM_PWM_NUM);

## API Reference
| Macros
| Functions
| Data Structures

## Macros
#define | Cy_TCPWM_PWM_SetCompare0 Cy_TCPWM_PWM_SetCompare0Val
#define | Cy_TCPWM_PWM_GetCompare0 Cy_TCPWM_PWM_GetCompare0Val
#define | Cy_TCPWM_PWM_SetCompare1 Cy_TCPWM_PWM_SetCompare0BufVal
#define | Cy_TCPWM_PWM_GetCompare1 Cy_TCPWM_PWM_GetCompare0BufVal
#define | Cy_TCPWM_PWM_EnableCompareSwap Cy_TCPWM_PWM_EnableCompare0Swap