Hardware Abstraction Layer (HAL)
All Data Structures Functions Variables Typedefs Enumerations Enumerator Modules Pages
PWM (Pulse Width Modulator)

General Description

High level interface for interacting with the pulse width modulator (PWM) hardware resource.

The PWM driver can be used to generate periodic digital waveforms with configurable frequency and duty cycle. The driver allows assigning the PWM output and an optional inverted output to supplied pins. The driver supports interrupt generation on PWM terminal count and compare events.

Features

Quick Start

See Snippet 1: Simple PWM initialization and output to pin for a code snippet that generates a signal with the specified frequency and duty cycle on the specified pin.

Code snippets

Snippet 1: Simple PWM initialization and output to pin

The clock parameter clk is optional and need not be provided (NULL), to generate and use an available clock resource with a default frequency.
The clock frequency and the duty cycle is set using mtb_hal_pwm_set_period.
mtb_hal_pwm_start starts the PWM output on the pin.

cy_rslt_t rslt;
mtb_hal_pwm_t pwm_obj;
// Specify the clock source
mtb_hal_clock_t pwm_clock;
// Initialize the PWM configuration structure
const cy_stc_tcpwm_pwm_config_t pwm_config = { 0 };
// Initialize PWM
rslt = (cy_rslt_t)Cy_TCPWM_PWM_Init(PWM_HW, PWM_CNTNUM, &pwm_config);
rslt = mtb_hal_pwm_setup(&pwm_obj, &PWM_hal_cfg, &pwm_clock);
// Set a 10us pulse with 100us period
rslt = mtb_hal_pwm_set_period(&pwm_obj, 100, 10);
// Start the PWM output
rslt = mtb_hal_pwm_start(&pwm_obj);
PWM object.
Definition: mtb_hal_hw_types_pwm_tcpwm.h:52
cy_rslt_t mtb_hal_pwm_start(mtb_hal_pwm_t *obj)
Starts the PWM generation and outputs on pin and compl_pin.
Definition: mtb_hal_pwm.c:122
cy_rslt_t mtb_hal_pwm_set_period(mtb_hal_pwm_t *obj, uint32_t period_us, uint32_t pulse_width_us)
Set the number of microseconds for the PWM period & pulse width.
Definition: mtb_hal_pwm.c:145
cy_rslt_t mtb_hal_pwm_setup(mtb_hal_pwm_t *obj, const mtb_hal_pwm_configurator_t *config, const mtb_hal_clock_t *clock)
Sets up a HAL instance to use the specified hardware resource.
Definition: mtb_hal_pwm.c:103
uint32_t cy_rslt_t
Provides the result of an operation as a structured bitfield.
Definition: cy_result.h:457
Clock object Application shall provide implementations for the functions needed by the clock object.
Definition: mtb_hal_hw_types_clock_srss.h:80

Snippet 2: Starting and stopping the PWM output

mtb_hal_pwm_start and mtb_hal_pwm_stop functions can be used after PWM initialization to start and stop the PWM output.

mtb_hal_pwm_t pwm_obj;
// Specify the clock source
mtb_hal_clock_t pwm_clock;
const cy_stc_tcpwm_pwm_config_t pwm_config = { 0 };
// Initialize PWM
rslt = (cy_rslt_t)Cy_TCPWM_PWM_Init(PWM_HW, PWM_CNTNUM, &pwm_config);
CY_ASSERT(CY_RSLT_SUCCESS == rslt);
rslt = mtb_hal_pwm_setup(&pwm_obj, &PWM_hal_cfg, &pwm_clock);
CY_ASSERT(CY_RSLT_SUCCESS == rslt);
// Set a 10us pulse with 100us period
rslt = mtb_hal_pwm_set_period(&pwm_obj, 100, 10);
while (loop)
{
// Stop the PWM output
rslt = mtb_hal_pwm_stop(&pwm_obj);
// Delay for observing the output
// (Re-)start the PWM output
rslt = mtb_hal_pwm_start(&pwm_obj);
// Delay for observing the output
}
cy_rslt_t mtb_hal_pwm_stop(mtb_hal_pwm_t *obj)
Stops the PWM generation and outputs on pin and compl_pin.
Definition: mtb_hal_pwm.c:134
cy_rslt_t mtb_hal_system_delay_ms(uint32_t milliseconds)
Requests that the current operation delays for at least the specified length of time.
Definition: mtb_hal_system.c:63
#define CY_RSLT_SUCCESS
cy_rslt_t return value indicating success
Definition: cy_result.h:484

API Reference

 PWM HAL Results
 PWM specific return codes.
 

Functions

cy_rslt_t mtb_hal_pwm_setup (mtb_hal_pwm_t *obj, const mtb_hal_pwm_configurator_t *config, const mtb_hal_clock_t *clock)
 Sets up a HAL instance to use the specified hardware resource. More...
 
cy_rslt_t mtb_hal_pwm_set_period (mtb_hal_pwm_t *obj, uint32_t period_us, uint32_t pulse_width_us)
 Set the number of microseconds for the PWM period & pulse width. More...
 
cy_rslt_t mtb_hal_pwm_start (mtb_hal_pwm_t *obj)
 Starts the PWM generation and outputs on pin and compl_pin. More...
 
cy_rslt_t mtb_hal_pwm_stop (mtb_hal_pwm_t *obj)
 Stops the PWM generation and outputs on pin and compl_pin. More...
 

Function Documentation

◆ mtb_hal_pwm_setup()

cy_rslt_t mtb_hal_pwm_setup ( mtb_hal_pwm_t obj,
const mtb_hal_pwm_configurator_t config,
const mtb_hal_clock_t clock 
)

Sets up a HAL instance to use the specified hardware resource.

This hardware resource must have already been configured via the PDL.

Parameters
[out]objThe HAL driver instance object. The caller must allocate the memory for this object, but the HAL will initialize its contents
[in]configThe configurator-generated HAL config structure for this peripheral instance
[in]clockThe HAL clock object that is connected to this peripheral instance
Returns
the status of the HAL setup

◆ mtb_hal_pwm_set_period()

cy_rslt_t mtb_hal_pwm_set_period ( mtb_hal_pwm_t obj,
uint32_t  period_us,
uint32_t  pulse_width_us 
)

Set the number of microseconds for the PWM period & pulse width.

Parameters
[in]objThe PWM object
[in]period_usThe period in microseconds
[in]pulse_width_usThe pulse width in microseconds
Returns
The status of the period request

◆ mtb_hal_pwm_start()

cy_rslt_t mtb_hal_pwm_start ( mtb_hal_pwm_t obj)

Starts the PWM generation and outputs on pin and compl_pin.

Parameters
[in]objThe PWM object
Returns
The status of the start request

◆ mtb_hal_pwm_stop()

cy_rslt_t mtb_hal_pwm_stop ( mtb_hal_pwm_t obj)

Stops the PWM generation and outputs on pin and compl_pin.

Parameters
[in]objThe PWM object
Returns
The status of the stop request