MCUXpresso SDK API Reference Manual  Rev. 0
NXP Semiconductors
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
PWM: Pulse Width Modulator

Overview

The SDK provides a driver for the Pulse Width Modulator (PWM).

The function PWM_Init() initializes the PWM module with specified configurations, the function PWM_GetDefaultConfig() could help to get the default configurations. The initialization function configures the module to use the specified clock for PWM operation.

The function PWM_SetupPwm() sets up the PWM channel for PWM output. The function can set up PWM signal property the channel. The PWM has 10 channels: 0 to 9. Each channel has its own period, compare match value, and polarity specified. The settings are applied to the specified channel requesting PWM output. The period and compare match are 16-bit values. At the compare match value, within the period, the PWM output toggles. The period value is loaded to downcounter, which decrements to 0. Once it reaches 0, it reloads the count and starts the signal out again, until the PWM channel is stopped. The function also sets up the channel output level after the channel is disabled. The 11th channel (ChannelAll) is a special channel which outputs the same output signals on other 10 channels (0 to 9) when it is set up and enabled.

The function PWM_ReadPeriodValue() reads the current period (downcounter value) for the PWM channel. The function PWM_ReadCompareValue() reads the compare match value for the PWM channel.

The function PWM_StartTimer() can be used to start the PWM channel. The function PWM_StopTimer() can be used to stop the PWM channel.

Provide functions to get and clear the PWM status.

Provide functions to enable/disable PWM interrupts and get current enabled interrupts.

Typical use case

PWM output

Configures PWM channel to output PWM signal.

int main(void)
{
/* Structure of initialize PWM */
pwm_config_t pwmConfig;
pwm_setup_t pwmChan0;
uint32_t pwmClockFrq;
uint32_t pwmChan0Clk;
/* Board pin, clock, debug console initialization */
BOARD_InitHardware();
PRINTF("PWM driver example\n");
pwmClockFrq = CLOCK_GetFreq(kCLOCK_Pwm);
PWM_GetDefaultConfig(&pwmConfig);
/* Use 32MHz clock */
pwmConfig.clk_sel = kPWM_Osc32Mclk;
/* Initialize PWM */
if (PWM_Init(BOARD_PWM_BASEADDR, &pwmConfig) != kStatus_Success)
{
PRINTF("PWM initialization failed\n");
return 1;
}
/* Set up PWM channel 0 to generate PWM pulse of 100 us with 50% duty cycle */
pwmChan0.prescaler_val = 0;
pwmChan0Clk = pwmClockFrq / (1 + pwmChan0.prescaler_val);
pwmChan0.period_val = USEC_TO_COUNT(100, pwmChan0Clk);
pwmChan0.comp_val = pwmChan0.period_val / 2;
if(PWM_SetupPwm (BOARD_PWM_BASEADDR, kPWM_Pwm0, &pwmChan0) != kStatus_Success)
{
PRINTF("PWM chan0 setup failed\n");
return 1;
}
/* Start the PWM generation channel 0 */
PWM_StartTimer(BOARD_PWM_BASEADDR, kPWM_Pwm0);
while (1U)
{
;
}
}

Files

file  fsl_pwm.h
 

Data Structures

struct  pwm_config_t
 PWM configuration structure. More...
 
struct  pwm_setup_t
 PWM channel setup structure. More...
 

Enumerations

enum  pwm_channels_t {
  kPWM_Pwm0 = 0x0,
  kPWM_Pwm1,
  kPWM_Pwm2,
  kPWM_Pwm3,
  kPWM_Pwm4,
  kPWM_Pwm5,
  kPWM_Pwm6,
  kPWM_Pwm7,
  kPWM_Pwm8,
  kPWM_Pwm9,
  kPWM_PwmAll
}
 PWM channel selection values. More...
 
enum  pwm_polarity_control_t {
  kPWM_SetHighOnMatchLowOnPeriod = 0x0,
  kPWM_SetLowOnMatchHighOnPeriod
}
 PWM channel polarity control values. More...
 
enum  pwm_dis_output_level_t {
  kPWM_SetLow = 0x0,
  kPWM_SetHigh
}
 PWM channel disable output level values. More...
 
enum  pwm_interrupt_enable_t {
  kPWM_InterruptDisabled = 0x0,
  kPWM_InterruptEnabled
}
 PWM channel interrupt enable flags. More...
 
enum  pwm_interrupt_status_t {
  kPWM_NoInterrupt = 0x0,
  kPWM_InterruptPendig
}
 PWM channel interrupt status flags. More...
 

Driver version

#define FSL_PWM_DRIVER_VERSION   (MAKE_VERSION(2, 0, 0))
 PWM driver version 2.0.0. More...
 

Initialization and deinitialization

void PWM_GetDefaultConfig (pwm_config_t *userConfig)
 Fill in the PWM config struct with the default settings. More...
 
status_t PWM_Init (PWM_Type *base, const pwm_config_t *userConfig)
 Initializes the PWM module. More...
 
void PWM_Deinit (PWM_Type *base)
 Gate the PWM module clock. More...
 

PWM module output

status_t PWM_SetupPwm (PWM_Type *base, pwm_channels_t pwm_chan, pwm_setup_t *pwmSetup)
 Sets up the PWM channel. More...
 

PWM Interrupts Interface

static void PWM_EnableInterrupts (PWM_Type *base, pwm_channels_t pwm_chan)
 Enable PWM channel interrupt. More...
 
static void PWM_DisableInterrupts (PWM_Type *base, pwm_channels_t pwm_chan)
 Disable PWM channel interrupt. More...
 
static uint32_t PWM_GetEnabledInterrupts (PWM_Type *base, pwm_channels_t pwm_chan)
 Gets the enabled PWM interrupts. More...
 

Status Interface

uint32_t PWM_GetStatusFlags (PWM_Type *base, pwm_channels_t pwm_chan)
 Gets the PWM status flags. More...
 
void PWM_ClearStatusFlags (PWM_Type *base, pwm_channels_t pwm_chan)
 Clears the PWM status flags. More...
 

Timer Start and Stop

static void PWM_StartTimer (PWM_Type *base, pwm_channels_t pwm_chan)
 Start PWM channel. More...
 
static void PWM_StopTimer (PWM_Type *base, pwm_channels_t pwm_chan)
 Stop PWM channel. More...
 
uint16_t PWM_ReadPeriodValue (PWM_Type *base, pwm_channels_t pwm_chan)
 Read current period value for PWM channel. More...
 
uint16_t PWM_ReadCompareValue (PWM_Type *base, pwm_channels_t pwm_chan)
 Read compare match value for PWM channel. More...
 

Data Structure Documentation

struct pwm_config_t

Data Fields

pwm_clock_source_t clk_sel
 PWM clock select value.
 
struct pwm_setup_t

Data Fields

pwm_polarity_control_t pol_ctrl
 Channel polarity control.
 
pwm_dis_output_level_t dis_out_level
 Channel disable output level.
 
uint16_t prescaler_val
 Channel Prescaler value. More...
 
uint16_t period_val
 Channel PWM period value.
 
uint16_t comp_val
 Channel compare match value.
 

Field Documentation

uint16_t pwm_setup_t::prescaler_val
  • 10 bit value

Macro Definition Documentation

#define FSL_PWM_DRIVER_VERSION   (MAKE_VERSION(2, 0, 0))

Enumeration Type Documentation

Enumerator
kPWM_Pwm0 

Channel 0.

kPWM_Pwm1 

Channel 1.

kPWM_Pwm2 

Channel 2.

kPWM_Pwm3 

Channel 3.

kPWM_Pwm4 

Channel 4.

kPWM_Pwm5 

Channel 5.

kPWM_Pwm6 

Channel 6.

kPWM_Pwm7 

Channel 7.

kPWM_Pwm8 

Channel 8.

kPWM_Pwm9 

Channel 9.

kPWM_PwmAll 

Channel 10 - All the channels will output same output programmed in this channel.

Enumerator
kPWM_SetHighOnMatchLowOnPeriod 

Set high on compare match, set low at end of PWM period.

kPWM_SetLowOnMatchHighOnPeriod 

Set low on compare match, set high at end of PWM period.

Enumerator
kPWM_SetLow 

Set to Low level.

kPWM_SetHigh 

Set to High level.

Enumerator
kPWM_InterruptDisabled 

PWM channel interrupt disabled.

kPWM_InterruptEnabled 

PWM channel interrupt enabled.

Enumerator
kPWM_NoInterrupt 

PWM channel interrupt not occurred.

kPWM_InterruptPendig 

PWM channel interrupt pending.

Function Documentation

void PWM_GetDefaultConfig ( pwm_config_t userConfig)

The default values are:

* userConfig->clk_sel = kPWM_Osc32Mclk;
*
Parameters
userConfigPointer to user's PWM config structure.
status_t PWM_Init ( PWM_Type *  base,
const pwm_config_t userConfig 
)

Call this API to ungate the PWM clock and configure the PWM HW.

Note
This API should be called at the beginning of the application to use the PWM driver, or any operation to the PWM module could cause hard fault because PWM module clock is not enabled. The configuration structure can be filled by user from scratch, or be set with default values by PWM_GetDefaultConfig(). After calling this API, the application can configure PWM channels to generate PWM outputs. Example:
* pwm_config_t userConfig = {
* .clk_sel = kPWM_Fro48Mclk,
* };
* PWM_Init(PWM, &userConfig);
*
Parameters
basePWM base address
userConfigpointer to user configuration structure
Returns
kStatus_Success - Success
kStatus_InvalidArgument - Invalid input parameter
void PWM_Deinit ( PWM_Type *  base)
Parameters
basePWM base address
status_t PWM_SetupPwm ( PWM_Type *  base,
pwm_channels_t  pwm_chan,
pwm_setup_t pwmSetup 
)

The function initializes the PWM channel according to the parameters passed in by the user. The function sets up the PWM compare match register & period registers.

Parameters
basePWM base address
pwm_chanPWM channel select value
pwmSetupPointer to PWM user setup structure
Returns
kStatus_Success - Success
kStatus_InvalidArgument - Invalid input parameter
static void PWM_EnableInterrupts ( PWM_Type *  base,
pwm_channels_t  pwm_chan 
)
inlinestatic

This function enables the interrupt for the specified PWM channel.

Parameters
basePWM base address
pwm_chanPWM channel select value
static void PWM_DisableInterrupts ( PWM_Type *  base,
pwm_channels_t  pwm_chan 
)
inlinestatic

This function disables the interrupt for the specified PWM channel.

Parameters
basePWM base address
pwm_chanPWM channel select value
static uint32_t PWM_GetEnabledInterrupts ( PWM_Type *  base,
pwm_channels_t  pwm_chan 
)
inlinestatic
Parameters
basePWM base address
pwm_chanPWM channel select value
Returns
PWM interrupt enabled status. This is the one of the values specified in enumeration pwm_interrupt_enable_t
uint32_t PWM_GetStatusFlags ( PWM_Type *  base,
pwm_channels_t  pwm_chan 
)
Parameters
basePWM base address
pwm_chanPWM channel select value
Returns
The status flags. This is the one of the value of members of the enumeration pwm_interrupt_status_t
void PWM_ClearStatusFlags ( PWM_Type *  base,
pwm_channels_t  pwm_chan 
)
Parameters
basePWM base address
pwm_chanPWM channel select value
static void PWM_StartTimer ( PWM_Type *  base,
pwm_channels_t  pwm_chan 
)
inlinestatic

The API will start PWM channel output on the pin. Before calling this API, make sure that the PWM channel is set up using PWM_SetupPwm() API.

Parameters
basePWM base address
pwm_chanPWM channel select value
static void PWM_StopTimer ( PWM_Type *  base,
pwm_channels_t  pwm_chan 
)
inlinestatic

The API will stop PWM channel output on the pin.

Parameters
basePWM base address
pwm_chanPWM channel select value
uint16_t PWM_ReadPeriodValue ( PWM_Type *  base,
pwm_channels_t  pwm_chan 
)

The API will read the current period value set for the PWM channel.

Parameters
basePWM base address
pwm_chanPWM channel select value
Returns
16-bit period value
uint16_t PWM_ReadCompareValue ( PWM_Type *  base,
pwm_channels_t  pwm_chan 
)

The API will read the compare match value set for the PWM channel.

Parameters
basePWM base address
pwm_chanPWM channel select value
Returns
16-bit period value