PSoC 6 Hardware Abstraction Layer (HAL)
PWM (Pulse Width Modulator)

General Description

High level interface for interacting with the Cypress PWM.

This block is able to create arbitrary square waves based off of an input clock. It can also be used to control the brightness of an LED by manipulating the duty cycle.

Macros

#define CYHAL_PWM_RSLT_BAD_ARGUMENT   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_PWM, 0))
 
#define CYHAL_PWM_RSLT_FAILED_CLOCK_INIT   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_PWM, 1))
 
#define CYHAL_PWM_RSLT_FAILED_INIT   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_PWM, 2))
 

Typedefs

typedef void(* cyhal_pwm_event_callback_t) (void *callback_arg, cyhal_pwm_event_t event)
 

Enumerations

enum  cyhal_pwm_event_t { CYHAL_PWM_IRQ_NONE = 0, CYHAL_PWM_IRQ_TERMINAL_COUNT = 1 << 0, CYHAL_PWM_IRQ_CAPTURE_COMPARE = 1 << 1, CYHAL_PWM_IRQ_ALL = (1 << 2) - 1 }
 

Functions

cy_rslt_t cyhal_pwm_init (cyhal_pwm_t *obj, cyhal_gpio_t pin, const cyhal_clock_divider_t *clk)
 
void cyhal_pwm_free (cyhal_pwm_t *obj)
 
cy_rslt_t cyhal_pwm_set_period (cyhal_pwm_t *obj, uint32_t period_us, uint32_t pulse_width_us)
 
cy_rslt_t cyhal_pwm_set_duty_cycle (cyhal_pwm_t *obj, float duty_cycle, uint32_t frequencyhal_hz)
 
cy_rslt_t cyhal_pwm_start (cyhal_pwm_t *obj)
 
cy_rslt_t cyhal_pwm_stop (cyhal_pwm_t *obj)
 
void cyhal_pwm_register_callback (cyhal_pwm_t *obj, cyhal_pwm_event_callback_t callback, void *callback_arg)
 
void cyhal_pwm_enable_event (cyhal_pwm_t *obj, cyhal_pwm_event_t event, uint8_t intrPriority, bool enable)
 

Macro Definition Documentation

◆ CYHAL_PWM_RSLT_BAD_ARGUMENT

#define CYHAL_PWM_RSLT_BAD_ARGUMENT   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_PWM, 0))

Bad argument

◆ CYHAL_PWM_RSLT_FAILED_CLOCK_INIT

#define CYHAL_PWM_RSLT_FAILED_CLOCK_INIT   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_PWM, 1))

Failed to initialize PWM clock

◆ CYHAL_PWM_RSLT_FAILED_INIT

#define CYHAL_PWM_RSLT_FAILED_INIT   (CY_RSLT_CREATE(CY_RSLT_TYPE_ERROR, CYHAL_RSLT_MODULE_PWM, 2))

Failed to initialize PWM

Typedef Documentation

◆ cyhal_pwm_event_callback_t

typedef void(* cyhal_pwm_event_callback_t) (void *callback_arg, cyhal_pwm_event_t event)

Handler for PWM interrupts

Enumeration Type Documentation

◆ cyhal_pwm_event_t

PWM interrupt triggers

Function Documentation

◆ cyhal_pwm_init()

cy_rslt_t cyhal_pwm_init ( cyhal_pwm_t *  obj,
cyhal_gpio_t  pin,
const cyhal_clock_divider_t *  clk 
)

Initialize the PWM out peripheral and configure the pin

Parameters
[out]objThe PWM object to initialize
[in]pinThe PWM pin to initialize
[in]clkThe clock to use can be shared, if not provided a new clock will be allocated
Returns
The status of the init request

◆ cyhal_pwm_free()

void cyhal_pwm_free ( cyhal_pwm_t *  obj)

Deinitialize the PWM object

Parameters
[in,out]objThe PWM object

◆ cyhal_pwm_set_period()

cy_rslt_t cyhal_pwm_set_period ( cyhal_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

◆ cyhal_pwm_set_duty_cycle()

cy_rslt_t cyhal_pwm_set_duty_cycle ( cyhal_pwm_t *  obj,
float  duty_cycle,
uint32_t  frequencyhal_hz 
)

Set the PWM pulsewidth specified in microseconds, keeping the period the same.

Parameters
[in]objThe PWM object
[in]duty_cycleThe percentage of time the output is high
[in]frequencyhal_hzThe frequency of the PWM
Returns
The status of the pulsewidth request

◆ cyhal_pwm_start()

cy_rslt_t cyhal_pwm_start ( cyhal_pwm_t *  obj)

Starts the PWM with the provided period and pulsewidth

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

◆ cyhal_pwm_stop()

cy_rslt_t cyhal_pwm_stop ( cyhal_pwm_t *  obj)

Stops the PWM from running

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

◆ cyhal_pwm_register_callback()

void cyhal_pwm_register_callback ( cyhal_pwm_t *  obj,
cyhal_pwm_event_callback_t  callback,
void *  callback_arg 
)

The PWM interrupt handler registration

Parameters
[in]objThe PWM object
[in]callbackThe callback handler which will be invoked when the event occurs
[in]callback_argGeneric argument that will be provided to the callback when called

◆ cyhal_pwm_enable_event()

void cyhal_pwm_enable_event ( cyhal_pwm_t *  obj,
cyhal_pwm_event_t  event,
uint8_t  intrPriority,
bool  enable 
)

Configure PWM event enablement.

Parameters
[in]objThe PWM object
[in]eventThe PWM event type
[in]intrPriorityThe priority for NVIC interrupt events
[in]enableTrue to turn on events, False to turn off