MCUXpresso SDK API Reference Manual: PWM: Pulse Width Modulation Driver

| MCUXpresso SDK API Reference Manual Rev 2.11.1 NXP Semiconductors

- Main Page
- Related Pages
- API Reference

All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
Enumerations | Functions
PWM: Pulse Width Modulation Driver

## Overview

The MCUXpresso SDK provides a peripheral driver for the Pulse Width Modulation (PWM) module of MCUXpresso SDK devices.

# PWM Driver

## Initialization and deinitialization

The function PWM_Init() initializes the PWM with a specified configurations. The function PWM_GetDefaultConfig() gets the default configurations.The initialization function configures the PWM for the requested register update mode for registers with buffers.

The function PWM_Deinit() disables the PWM counter and turns off the module clock.

# Typical use case

## PWM output

Output PWM signal on PWM3 module with different dutycycles. Periodically update the PWM signal duty cycle. Refer to the driver examples codes located at <SDK_ROOT>/boards/<BOARD>/driver_examples/pwm

## Enumerations
enum | pwm_clock_source_t { kPWM_PeripheralClock = 1U, kPWM_HighFrequencyClock, kPWM_LowFrequencyClock }
| PWM clock source select. More...
enum | pwm_fifo_water_mark_t { kPWM_FIFOWaterMark_1 = 0U, kPWM_FIFOWaterMark_2, kPWM_FIFOWaterMark_3, kPWM_FIFOWaterMark_4 }
| PWM FIFO water mark select. More...
enum | pwm_byte_data_swap_t { kPWM_ByteNoSwap = 0U, kPWM_ByteSwap }
| PWM byte data swap select. More...
enum | pwm_half_word_data_swap_t { kPWM_HalfWordNoSwap = 0U, kPWM_HalfWordSwap }
| PWM half-word data swap select. More...
enum | pwm_output_configuration_t { kPWM_SetAtRolloverAndClearAtcomparison = 0U, kPWM_ClearAtRolloverAndSetAtcomparison, kPWM_NoConfigure }
| PWM Output Configuration. More...
enum | pwm_sample_repeat_t { kPWM_EachSampleOnce = 0u, kPWM_EachSampletwice, kPWM_EachSampleFourTimes, kPWM_EachSampleEightTimes }
| PWM FIFO sample repeat It determines the number of times each sample from the FIFO is to be used. More...
enum | pwm_interrupt_enable_t { kPWM_FIFOEmptyInterruptEnable = (1U << 0), kPWM_RolloverInterruptEnable = (1U << 1), kPWM_CompareInterruptEnable = (1U << 2) }
| List of PWM interrupt options. More...
enum | pwm_status_flags_t { kPWM_FIFOEmptyFlag = (1U << 3), kPWM_RolloverFlag = (1U << 4), kPWM_CompareFlag = (1U << 5), kPWM_FIFOWriteErrorFlag }
| List of PWM status flags. More...
enum | pwm_fifo_available_t { kPWM_NoDataInFIFOFlag = 0U, kPWM_OneWordInFIFOFlag, kPWM_TwoWordsInFIFOFlag, kPWM_ThreeWordsInFIFOFlag, kPWM_FourWordsInFIFOFlag }
| List of PWM FIFO available. More...

## Functions
static void | PWM_SoftwareReset (PWM_Type *base)
| Sofrware reset. More...
static void | PWM_SetPeriodValue (PWM_Type *base, uint32_t value)
| Sets the PWM period value. More...
static uint32_t | PWM_GetPeriodValue (PWM_Type *base)
| Gets the PWM period value. More...
static uint32_t | PWM_GetCounterValue (PWM_Type *base)
| Gets the PWM counter value. More...

## Driver version
#define | FSL_PWM_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
| Version 2.0.0.

## Initialization and deinitialization
status_t | PWM_Init (PWM_Type *base, const pwm_config_t *config)
| Ungates the PWM clock and configures the peripheral for basic operation. More...
void | PWM_Deinit (PWM_Type *base)
| Gate the PWM submodule clock. More...
void | PWM_GetDefaultConfig (pwm_config_t *config)
| Fill in the PWM config struct with the default settings. More...

## PWM start and stop.
static void | PWM_StartTimer (PWM_Type *base)
| Starts the PWM counter when the PWM is enabled. More...
static void | PWM_StopTimer (PWM_Type *base)
| Stops the PWM counter when the pwm is disabled. More...

## Interrupt Interface
static void | PWM_EnableInterrupts (PWM_Type *base, uint32_t mask)
| Enables the selected PWM interrupts. More...
static void | PWM_DisableInterrupts (PWM_Type *base, uint32_t mask)
| Disables the selected PWM interrupts. More...
static uint32_t | PWM_GetEnabledInterrupts (PWM_Type *base)
| Gets the enabled PWM interrupts. More...

## Status Interface
static uint32_t | PWM_GetStatusFlags (PWM_Type *base)
| Gets the PWM status flags. More...
static void | PWM_clearStatusFlags (PWM_Type *base, uint32_t mask)
| Clears the PWM status flags. More...
static uint32_t | PWM_GetFIFOAvailable (PWM_Type *base)
| Gets the PWM FIFO available. More...

## Sample Interface
static void | PWM_SetSampleValue (PWM_Type *base, uint32_t value)
| Sets the PWM sample value. More...
static uint32_t | PWM_GetSampleValue (PWM_Type *base)
| Gets the PWM sample value. More...

## Enumeration Type Documentation

enum pwm_clock_source_t

Enumerator
kPWM_PeripheralClock | The Peripheral clock is used as the clock.
kPWM_HighFrequencyClock | High-frequency reference clock is used as the clock.
kPWM_LowFrequencyClock | Low-frequency reference clock(32KHz) is used as the clock.

enum pwm_fifo_water_mark_t

Sets the data level at which the FIFO empty flag will be set

Enumerator
kPWM_FIFOWaterMark_1 | FIFO empty flag is set when there are more than or equal to 1 empty slots.
kPWM_FIFOWaterMark_2 | FIFO empty flag is set when there are more than or equal to 2 empty slots.
kPWM_FIFOWaterMark_3 | FIFO empty flag is set when there are more than or equal to 3 empty slots.
kPWM_FIFOWaterMark_4 | FIFO empty flag is set when there are more than or equal to 4 empty slots.

enum pwm_byte_data_swap_t

It determines the byte ordering of the 16-bit data when it goes into the FIFO from the sample register.

Enumerator
kPWM_ByteNoSwap | byte ordering remains the same
kPWM_ByteSwap | byte ordering is reversed

enum pwm_half_word_data_swap_t

Enumerator
kPWM_HalfWordNoSwap | Half word swapping does not take place.
kPWM_HalfWordSwap | Half word from write data bus are swapped.

enum pwm_output_configuration_t

Enumerator
kPWM_SetAtRolloverAndClearAtcomparison | Output pin is set at rollover and cleared at comparison.
kPWM_ClearAtRolloverAndSetAtcomparison | Output pin is cleared at rollover and set at comparison.
kPWM_NoConfigure | PWM output is disconnected.

enum pwm_sample_repeat_t

Enumerator
kPWM_EachSampleOnce | Use each sample once.
kPWM_EachSampletwice | Use each sample twice.
kPWM_EachSampleFourTimes | Use each sample four times.
kPWM_EachSampleEightTimes | Use each sample eight times.

enum pwm_interrupt_enable_t

Enumerator
kPWM_FIFOEmptyInterruptEnable | This bit controls the generation of the FIFO Empty interrupt.
kPWM_RolloverInterruptEnable | This bit controls the generation of the Rollover interrupt.
kPWM_CompareInterruptEnable | This bit controls the generation of the Compare interrupt.

enum pwm_status_flags_t

Enumerator
kPWM_FIFOEmptyFlag | This bit indicates the FIFO data level in comparison to the water level set by FWM field in the control register.
kPWM_RolloverFlag | This bit shows that a roll-over event has occurred.
kPWM_CompareFlag | This bit shows that a compare event has occurred.
kPWM_FIFOWriteErrorFlag | This bit shows that an attempt has been made to write FIFO when it is full.

enum pwm_fifo_available_t

Enumerator
kPWM_NoDataInFIFOFlag | No data available.
kPWM_OneWordInFIFOFlag | 1 word of data in FIFO
kPWM_TwoWordsInFIFOFlag | 2 word of data in FIFO
kPWM_ThreeWordsInFIFOFlag | 3 word of data in FIFO
kPWM_FourWordsInFIFOFlag | 4 word of data in FIFO

## Function Documentation

status_t PWM_Init | ( | PWM_Type * | base,
|  | const pwm_config_t * | config
| ) |  |

Note
  This API should be called at the beginning of the application using the PWM driver.

Parameters

  base | PWM peripheral base address
  config | Pointer to user's PWM config structure.

Returns
  kStatus_Success means success; else failed.

void PWM_Deinit | ( | PWM_Type * | base | ) |

Parameters

  base | PWM peripheral base address

void PWM_GetDefaultConfig | ( | pwm_config_t * | config | ) |

The default values are:

* config->enableStopMode = false;
* config->enableDozeMode = false;
* config->enableWaitMode = false;
* config->enableDozeMode = false;
* config->clockSource = kPWM_LowFrequencyClock;
* config->prescale = 0U;
* config->outputConfig = kPWM_SetAtRolloverAndClearAtcomparison;
* config->fifoWater = kPWM_FIFOWaterMark_2;
* config->sampleRepeat = kPWM_EachSampleOnce;
* config->byteSwap = kPWM_ByteNoSwap;
* config->halfWordSwap = kPWM_HalfWordNoSwap;
*

Parameters

  config | Pointer to user's PWM config structure.

static void PWM_StartTimer | ( | PWM_Type * | base | ) |
| inlinestatic

When the PWM is enabled, it begins a new period, the output pin is set to start a new period while the prescaler and counter are released and counting begins.

Parameters

  base | PWM peripheral base address

static void PWM_StopTimer | ( | PWM_Type * | base | ) |
| inlinestatic

Parameters

  base | PWM peripheral base address

static void PWM_SoftwareReset | ( | PWM_Type * | base | ) |
| inlinestatic

PWM is reset when this bit is set to 1. It is a self clearing bit. Setting this bit resets all the registers to their reset values except for the STOPEN, DOZEN, WAITEN, and DBGEN bits in this control register.

Parameters

  base | PWM peripheral base address

static void PWM_EnableInterrupts | ( | PWM_Type * | base,
|  | uint32_t | mask
| ) |  |
| inlinestatic

Parameters

  base | PWM peripheral base address
  mask | The interrupts to enable. This is a logical OR of members of the enumeration pwm_interrupt_enable_t

static void PWM_DisableInterrupts | ( | PWM_Type * | base,
|  | uint32_t | mask
| ) |  |
| inlinestatic

Parameters

  base | PWM peripheral base address
  mask | The interrupts to disable. This is a logical OR of members of the enumeration pwm_interrupt_enable_t

static uint32_t PWM_GetEnabledInterrupts | ( | PWM_Type * | base | ) |
| inlinestatic

Parameters

  base | PWM peripheral base address

Returns
  The enabled interrupts. This is the logical OR of members of the enumeration pwm_interrupt_enable_t

static uint32_t PWM_GetStatusFlags | ( | PWM_Type * | base | ) |
| inlinestatic

Parameters

  base | PWM peripheral base address

Returns
  The status flags. This is the logical OR of members of the enumeration pwm_status_flags_t

static void PWM_clearStatusFlags | ( | PWM_Type * | base,
|  | uint32_t | mask
| ) |  |
| inlinestatic

Parameters

  base | PWM peripheral base address
  mask | The status flags to clear. This is a logical OR of members of the enumeration pwm_status_flags_t

static uint32_t PWM_GetFIFOAvailable | ( | PWM_Type * | base | ) |
| inlinestatic

Parameters

  base | PWM peripheral base address

Returns
  The status flags. This is the logical OR of members of the enumeration pwm_fifo_available_t

static void PWM_SetSampleValue | ( | PWM_Type * | base,
|  | uint32_t | value
| ) |  |
| inlinestatic

Parameters

  base | PWM peripheral base address
  value | The sample value. This is the input to the 4x16 FIFO. The value in this register denotes the value of the sample being currently used.

static uint32_t PWM_GetSampleValue | ( | PWM_Type * | base | ) |
| inlinestatic

Parameters

  base | PWM peripheral base address

Returns
  The sample value. It can be read only when the PWM is enable.

static void PWM_SetPeriodValue | ( | PWM_Type * | base,
|  | uint32_t | value
| ) |  |
| inlinestatic

Parameters

  base | PWM peripheral base address
  value | The period value. The PWM period register (PWM_PWMPR) determines the period of the PWM output signal. Writing 0xFFFF to this register will achieve the same result as writing 0xFFFE. PWMO (Hz) = PCLK(Hz) / (period +2)

static uint32_t PWM_GetPeriodValue | ( | PWM_Type * | base | ) |
| inlinestatic

Parameters

  base | PWM peripheral base address

Returns
  The period value. The PWM period register (PWM_PWMPR) determines the period of the PWM output signal.

static uint32_t PWM_GetCounterValue | ( | PWM_Type * | base | ) |
| inlinestatic

Parameters

  base | PWM peripheral base address

Returns
  The counter value. The current count value.