MCUXpresso SDK API Reference Manual  Rev. 0
NXP Semiconductors
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
GPT: General Purpose Timer

Overview

The MCUXpresso SDK provides a driver for the General Purpose Timer (GPT) of MCUXpresso SDK devices.

Function groups

The gpt driver supports the generation of PWM signals, input capture, and setting up the timer match conditions.

Initialization and deinitialization

The function GPT_Init() initializes the gpt with specified configurations. The function GPT_GetDefaultConfig() gets the default configurations. The initialization function configures the restart/free-run mode and input selection when running.

The function GPT_Deinit() stops the timer and turns off the module clock.

Typical use case

GPT interrupt example

Set up a channel to trigger a periodic interrupt after every 1 second. Refer to the driver examples codes located at <SDK_ROOT>/boards/<BOARD>/driver_examples/gpt

Data Structures

struct  gpt_config_t
 Structure to configure the running mode. More...
 

Enumerations

enum  gpt_clock_source_t {
  kGPT_ClockSource_Off = 0U,
  kGPT_ClockSource_Periph = 1U,
  kGPT_ClockSource_HighFreq = 2U,
  kGPT_ClockSource_Ext = 3U,
  kGPT_ClockSource_LowFreq = 4U,
  kGPT_ClockSource_Osc = 5U
}
 List of clock sources. More...
 
enum  gpt_input_capture_channel_t {
  kGPT_InputCapture_Channel1 = 0U,
  kGPT_InputCapture_Channel2 = 1U
}
 List of input capture channel number. More...
 
enum  gpt_input_operation_mode_t {
  kGPT_InputOperation_Disabled = 0U,
  kGPT_InputOperation_RiseEdge = 1U,
  kGPT_InputOperation_FallEdge = 2U,
  kGPT_InputOperation_BothEdge = 3U
}
 List of input capture operation mode. More...
 
enum  gpt_output_compare_channel_t {
  kGPT_OutputCompare_Channel1 = 0U,
  kGPT_OutputCompare_Channel2 = 1U,
  kGPT_OutputCompare_Channel3 = 2U
}
 List of output compare channel number. More...
 
enum  gpt_output_operation_mode_t {
  kGPT_OutputOperation_Disconnected = 0U,
  kGPT_OutputOperation_Toggle = 1U,
  kGPT_OutputOperation_Clear = 2U,
  kGPT_OutputOperation_Set = 3U,
  kGPT_OutputOperation_Activelow = 4U
}
 List of output compare operation mode. More...
 
enum  gpt_interrupt_enable_t {
  kGPT_OutputCompare1InterruptEnable = GPT_IR_OF1IE_MASK,
  kGPT_OutputCompare2InterruptEnable = GPT_IR_OF2IE_MASK,
  kGPT_OutputCompare3InterruptEnable = GPT_IR_OF3IE_MASK,
  kGPT_InputCapture1InterruptEnable = GPT_IR_IF1IE_MASK,
  kGPT_InputCapture2InterruptEnable = GPT_IR_IF2IE_MASK,
  kGPT_RollOverFlagInterruptEnable = GPT_IR_ROVIE_MASK
}
 List of GPT interrupts. More...
 
enum  gpt_status_flag_t {
  kGPT_OutputCompare1Flag = GPT_SR_OF1_MASK,
  kGPT_OutputCompare2Flag = GPT_SR_OF2_MASK,
  kGPT_OutputCompare3Flag = GPT_SR_OF3_MASK,
  kGPT_InputCapture1Flag = GPT_SR_IF1_MASK,
  kGPT_InputCapture2Flag = GPT_SR_IF2_MASK,
  kGPT_RollOverFlag = GPT_SR_ROV_MASK
}
 Status flag. More...
 

Driver version

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

Initialization and deinitialization

void GPT_Init (GPT_Type *base, const gpt_config_t *initConfig)
 Initialize GPT to reset state and initialize running mode. More...
 
void GPT_Deinit (GPT_Type *base)
 Disables the module and gates the GPT clock. More...
 
void GPT_GetDefaultConfig (gpt_config_t *config)
 Fills in the GPT configuration structure with default settings. More...
 

Software Reset

static void GPT_SoftwareReset (GPT_Type *base)
 Software reset of GPT module. More...
 

Clock source and frequency control

static void GPT_SetClockSource (GPT_Type *base, gpt_clock_source_t source)
 Set clock source of GPT. More...
 
static gpt_clock_source_t GPT_GetClockSource (GPT_Type *base)
 Get clock source of GPT. More...
 
static void GPT_SetClockDivider (GPT_Type *base, uint32_t divider)
 Set pre scaler of GPT. More...
 
static uint32_t GPT_GetClockDivider (GPT_Type *base)
 Get clock divider in GPT module. More...
 
static void GPT_SetOscClockDivider (GPT_Type *base, uint32_t divider)
 OSC 24M pre-scaler before selected by clock source. More...
 
static uint32_t GPT_GetOscClockDivider (GPT_Type *base)
 Get OSC 24M clock divider in GPT module. More...
 

Timer Start and Stop

static void GPT_StartTimer (GPT_Type *base)
 Start GPT timer. More...
 
static void GPT_StopTimer (GPT_Type *base)
 Stop GPT timer. More...
 

Read the timer period

static uint32_t GPT_GetCurrentTimerCount (GPT_Type *base)
 Reads the current GPT counting value. More...
 

GPT Input/Output Signal Control

static void GPT_SetInputOperationMode (GPT_Type *base, gpt_input_capture_channel_t channel, gpt_input_operation_mode_t mode)
 Set GPT operation mode of input capture channel. More...
 
static gpt_input_operation_mode_t GPT_GetInputOperationMode (GPT_Type *base, gpt_input_capture_channel_t channel)
 Get GPT operation mode of input capture channel. More...
 
static uint32_t GPT_GetInputCaptureValue (GPT_Type *base, gpt_input_capture_channel_t channel)
 Get GPT input capture value of certain channel. More...
 
static void GPT_SetOutputOperationMode (GPT_Type *base, gpt_output_compare_channel_t channel, gpt_output_operation_mode_t mode)
 Set GPT operation mode of output compare channel. More...
 
static gpt_output_operation_mode_t GPT_GetOutputOperationMode (GPT_Type *base, gpt_output_compare_channel_t channel)
 Get GPT operation mode of output compare channel. More...
 
static void GPT_SetOutputCompareValue (GPT_Type *base, gpt_output_compare_channel_t channel, uint32_t value)
 Set GPT output compare value of output compare channel. More...
 
static uint32_t GPT_GetOutputCompareValue (GPT_Type *base, gpt_output_compare_channel_t channel)
 Get GPT output compare value of output compare channel. More...
 
static void GPT_ForceOutput (GPT_Type *base, gpt_output_compare_channel_t channel)
 Force GPT output action on output compare channel, ignoring comparator. More...
 

GPT Interrupt and Status Interface

static void GPT_EnableInterrupts (GPT_Type *base, uint32_t mask)
 Enables the selected GPT interrupts. More...
 
static void GPT_DisableInterrupts (GPT_Type *base, uint32_t mask)
 Disables the selected GPT interrupts. More...
 
static uint32_t GPT_GetEnabledInterrupts (GPT_Type *base)
 Gets the enabled GPT interrupts. More...
 

Status Interface

static uint32_t GPT_GetStatusFlags (GPT_Type *base, gpt_status_flag_t flags)
 Get GPT status flags. More...
 
static void GPT_ClearStatusFlags (GPT_Type *base, gpt_status_flag_t flags)
 Clears the GPT status flags. More...
 

Data Structure Documentation

struct gpt_config_t

Data Fields

gpt_clock_source_t clockSource
 clock source for GPT module. More...
 
uint32_t divider
 clock divider (prescaler+1) from clock source to counter. More...
 
bool enableFreeRun
 true: FreeRun mode, false: Restart mode. More...
 
bool enableRunInWait
 GPT enabled in wait mode. More...
 
bool enableRunInStop
 GPT enabled in stop mode. More...
 
bool enableRunInDoze
 GPT enabled in doze mode. More...
 
bool enableRunInDbg
 GPT enabled in debug mode. More...
 
bool enableMode
 
 true:  counter reset to 0 when enabled;

false: counter retain its value when enabled. More...

 

Field Documentation

gpt_clock_source_t gpt_config_t::clockSource
uint32_t gpt_config_t::divider
bool gpt_config_t::enableFreeRun
bool gpt_config_t::enableRunInWait
bool gpt_config_t::enableRunInStop
bool gpt_config_t::enableRunInDoze
bool gpt_config_t::enableRunInDbg
bool gpt_config_t::enableMode

Enumeration Type Documentation

Note
Actual number of clock sources is SoC dependent
Enumerator
kGPT_ClockSource_Off 

GPT Clock Source Off.

kGPT_ClockSource_Periph 

GPT Clock Source from Peripheral Clock.

kGPT_ClockSource_HighFreq 

GPT Clock Source from High Frequency Reference Clock.

kGPT_ClockSource_Ext 

GPT Clock Source from external pin.

kGPT_ClockSource_LowFreq 

GPT Clock Source from Low Frequency Reference Clock.

kGPT_ClockSource_Osc 

GPT Clock Source from Crystal oscillator.

Enumerator
kGPT_InputCapture_Channel1 

GPT Input Capture Channel1.

kGPT_InputCapture_Channel2 

GPT Input Capture Channel2.

Enumerator
kGPT_InputOperation_Disabled 

Don't capture.

kGPT_InputOperation_RiseEdge 

Capture on rising edge of input pin.

kGPT_InputOperation_FallEdge 

Capture on falling edge of input pin.

kGPT_InputOperation_BothEdge 

Capture on both edges of input pin.

Enumerator
kGPT_OutputCompare_Channel1 

Output Compare Channel1.

kGPT_OutputCompare_Channel2 

Output Compare Channel2.

kGPT_OutputCompare_Channel3 

Output Compare Channel3.

Enumerator
kGPT_OutputOperation_Disconnected 

Don't change output pin.

kGPT_OutputOperation_Toggle 

Toggle output pin.

kGPT_OutputOperation_Clear 

Set output pin low.

kGPT_OutputOperation_Set 

Set output pin high.

kGPT_OutputOperation_Activelow 

Generate a active low pulse on output pin.

Enumerator
kGPT_OutputCompare1InterruptEnable 

Output Compare Channel1 interrupt enable.

kGPT_OutputCompare2InterruptEnable 

Output Compare Channel2 interrupt enable.

kGPT_OutputCompare3InterruptEnable 

Output Compare Channel3 interrupt enable.

kGPT_InputCapture1InterruptEnable 

Input Capture Channel1 interrupt enable.

kGPT_InputCapture2InterruptEnable 

Input Capture Channel1 interrupt enable.

kGPT_RollOverFlagInterruptEnable 

Counter rolled over interrupt enable.

Enumerator
kGPT_OutputCompare1Flag 

Output compare channel 1 event.

kGPT_OutputCompare2Flag 

Output compare channel 2 event.

kGPT_OutputCompare3Flag 

Output compare channel 3 event.

kGPT_InputCapture1Flag 

Input Capture channel 1 event.

kGPT_InputCapture2Flag 

Input Capture channel 2 event.

kGPT_RollOverFlag 

Counter reaches maximum value and rolled over to 0 event.

Function Documentation

void GPT_Init ( GPT_Type *  base,
const gpt_config_t initConfig 
)
Parameters
baseGPT peripheral base address.
initConfigGPT mode setting configuration.
void GPT_Deinit ( GPT_Type *  base)
Parameters
baseGPT peripheral base address.
void GPT_GetDefaultConfig ( gpt_config_t config)

The default values are:

* config->clockSource = kGPT_ClockSource_Periph;
* config->divider = 1U;
* config->enableRunInStop = true;
* config->enableRunInWait = true;
* config->enableRunInDoze = false;
* config->enableRunInDbg = false;
* config->enableFreeRun = false;
* config->enableMode = true;
*
Parameters
configPointer to the user configuration structure.
static void GPT_SoftwareReset ( GPT_Type *  base)
inlinestatic
Parameters
baseGPT peripheral base address.
static void GPT_SetClockSource ( GPT_Type *  base,
gpt_clock_source_t  source 
)
inlinestatic
Parameters
baseGPT peripheral base address.
sourceClock source (see gpt_clock_source_t typedef enumeration).
static gpt_clock_source_t GPT_GetClockSource ( GPT_Type *  base)
inlinestatic
Parameters
baseGPT peripheral base address.
Returns
clock source (see gpt_clock_source_t typedef enumeration).
static void GPT_SetClockDivider ( GPT_Type *  base,
uint32_t  divider 
)
inlinestatic
Parameters
baseGPT peripheral base address.
dividerDivider of GPT (1-4096).
static uint32_t GPT_GetClockDivider ( GPT_Type *  base)
inlinestatic
Parameters
baseGPT peripheral base address.
Returns
clock divider in GPT module (1-4096).
static void GPT_SetOscClockDivider ( GPT_Type *  base,
uint32_t  divider 
)
inlinestatic
Parameters
baseGPT peripheral base address.
dividerOSC Divider(1-16).
static uint32_t GPT_GetOscClockDivider ( GPT_Type *  base)
inlinestatic
Parameters
baseGPT peripheral base address.
Returns
OSC clock divider in GPT module (1-16).
static void GPT_StartTimer ( GPT_Type *  base)
inlinestatic
Parameters
baseGPT peripheral base address.
static void GPT_StopTimer ( GPT_Type *  base)
inlinestatic
Parameters
baseGPT peripheral base address.
static uint32_t GPT_GetCurrentTimerCount ( GPT_Type *  base)
inlinestatic
Parameters
baseGPT peripheral base address.
Returns
Current GPT counter value.
static void GPT_SetInputOperationMode ( GPT_Type *  base,
gpt_input_capture_channel_t  channel,
gpt_input_operation_mode_t  mode 
)
inlinestatic
Parameters
baseGPT peripheral base address.
channelGPT capture channel (see gpt_input_capture_channel_t typedef enumeration).
modeGPT input capture operation mode (see gpt_input_operation_mode_t typedef enumeration).
static gpt_input_operation_mode_t GPT_GetInputOperationMode ( GPT_Type *  base,
gpt_input_capture_channel_t  channel 
)
inlinestatic
Parameters
baseGPT peripheral base address.
channelGPT capture channel (see gpt_input_capture_channel_t typedef enumeration).
Returns
GPT input capture operation mode (see gpt_input_operation_mode_t typedef enumeration).
static uint32_t GPT_GetInputCaptureValue ( GPT_Type *  base,
gpt_input_capture_channel_t  channel 
)
inlinestatic
Parameters
baseGPT peripheral base address.
channelGPT capture channel (see gpt_input_capture_channel_t typedef enumeration).
Returns
GPT input capture value.
static void GPT_SetOutputOperationMode ( GPT_Type *  base,
gpt_output_compare_channel_t  channel,
gpt_output_operation_mode_t  mode 
)
inlinestatic
Parameters
baseGPT peripheral base address.
channelGPT output compare channel (see gpt_output_compare_channel_t typedef enumeration).
modeGPT output operation mode (see gpt_output_operation_mode_t typedef enumeration).
static gpt_output_operation_mode_t GPT_GetOutputOperationMode ( GPT_Type *  base,
gpt_output_compare_channel_t  channel 
)
inlinestatic
Parameters
baseGPT peripheral base address.
channelGPT output compare channel (see gpt_output_compare_channel_t typedef enumeration).
Returns
GPT output operation mode (see gpt_output_operation_mode_t typedef enumeration).
static void GPT_SetOutputCompareValue ( GPT_Type *  base,
gpt_output_compare_channel_t  channel,
uint32_t  value 
)
inlinestatic
Parameters
baseGPT peripheral base address.
channelGPT output compare channel (see gpt_output_compare_channel_t typedef enumeration).
valueGPT output compare value.
static uint32_t GPT_GetOutputCompareValue ( GPT_Type *  base,
gpt_output_compare_channel_t  channel 
)
inlinestatic
Parameters
baseGPT peripheral base address.
channelGPT output compare channel (see gpt_output_compare_channel_t typedef enumeration).
Returns
GPT output compare value.
static void GPT_ForceOutput ( GPT_Type *  base,
gpt_output_compare_channel_t  channel 
)
inlinestatic
Parameters
baseGPT peripheral base address.
channelGPT output compare channel (see gpt_output_compare_channel_t typedef enumeration).
static void GPT_EnableInterrupts ( GPT_Type *  base,
uint32_t  mask 
)
inlinestatic
Parameters
baseGPT peripheral base address.
maskThe interrupts to enable. This is a logical OR of members of the enumeration gpt_interrupt_enable_t
static void GPT_DisableInterrupts ( GPT_Type *  base,
uint32_t  mask 
)
inlinestatic
Parameters
baseGPT peripheral base address
maskThe interrupts to disable. This is a logical OR of members of the enumeration gpt_interrupt_enable_t
static uint32_t GPT_GetEnabledInterrupts ( GPT_Type *  base)
inlinestatic
Parameters
baseGPT peripheral base address
Returns
The enabled interrupts. This is the logical OR of members of the enumeration gpt_interrupt_enable_t
static uint32_t GPT_GetStatusFlags ( GPT_Type *  base,
gpt_status_flag_t  flags 
)
inlinestatic
Parameters
baseGPT peripheral base address.
flagsGPT status flag mask (see gpt_status_flag_t for bit definition).
Returns
GPT status, each bit represents one status flag.
static void GPT_ClearStatusFlags ( GPT_Type *  base,
gpt_status_flag_t  flags 
)
inlinestatic
Parameters
baseGPT peripheral base address.
flagsGPT status flag mask (see gpt_status_flag_t for bit definition).