MCUXpresso SDK API Reference Manual  Rev. 0
NXP Semiconductors
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
CMP: Comparator Driver

Read Guidance

This document consists of sections titled with Driver Overview, Data Structures, Enumerations, Functions, etc., each with an overview list and detailed documentation. It is recommended to read the Driver Overview first for it includes a comprehensive description of the peripheral, driver and driver changes. Other sections give detailed information for APIs, enums, macros, etc., for your further reference.

Driver Overview

 CMP Peripheral and Driver Overview
 Content including 1) peripheral features, work logic and work method; 2) driver design logic and use method; 3) typical use case.
 
 The Driver Change Log
 The current cmp driver version is 2.0.0.
 

Data Structures

struct  cmp_dac_config_t
 CMP internal DAC configuration structure. More...
 
union  cmp_dma_interrupt_config_t
 CMP dma/interrupt configure union. More...
 
struct  cmp_config_t
 CMP configuration structure. More...
 

Enumerations

enum  cmp_interrupt_request_t {
  kCMP_InterruptRequestDisabled = 0U,
  kCMP_InterruptRequestEnableOutputRisingEdge,
  kCMP_InterruptRequestEnableOutputFallingEdge,
  kCMP_InterrruptRequestEnableAll
}
 CMP Interrupt request type definition. More...
 
enum  cmp_dma_request_t {
  kCMP_DMARequestDisabled = 0U,
  kCMP_DMARequestEnableOutputRisingEdge,
  kCMP_DMARequestEnableOutputFallingEdge,
  kCMP_DMARequestEnableAll
}
 CMP DMA request type definition. More...
 
enum  cmp_output_flag_t {
  kCMP_OutputFlagRisingEdge = CMP_SCR_CFR_MASK,
  kCMP_OutputFlagFallingEdge = CMP_SCR_CFF_MASK,
  kCMP_OutputFlagBothEdge
}
 CMP output flags' mask. More...
 
enum  cmp_hysteresis_level_t {
  kCMP_HysteresisLevel0 = 0U,
  kCMP_HysteresisLevel1 = 1U,
  kCMP_HysteresisLevel2 = 2U,
  kCMP_HysteresisLevel3 = 3U
}
 CMP Hysteresis level. More...
 
enum  cmp_comparasion_speed_mode_t {
  kCMP_ComparsionModeLowSpeed = 0U,
  kCMP_ComparsionModeHighSpeed = 1U
}
 CMP compassion speed mode enumerator. More...
 
enum  cmp_dac_vref_source_t {
  kCMP_DACVrefSourceVin1 = 0U,
  kCMP_DACVrefSourceVin2 = 1U
}
 CMP DAC Voltage Reference source. More...
 
enum  cmp_window_output_mode_t {
  kCMP_WindowOuputLastLatchedValue,
  kCMP_WindowOutputZeroValue = 1U
}
 CMP output value of window. More...
 
enum  cmp_filter_count_t {
  kCMP_FilterCountDisable = 0U,
  kCMP_FilterCount1 = 1U,
  kCMP_FilterCount2 = 2U,
  kCMP_FilterCount3 = 3U,
  kCMP_FilterCount4 = 4U,
  kCMP_FilterCount5 = 5U,
  kCMP_FilterCount6 = 6U,
  kCMP_FilterCount7 = 7U
}
 CMP filter count. More...
 
enum  cmp_external_sample_count_t {
  kCMP_ExternalSampleCount1 = 1U,
  kCMP_ExternalSampleCount2 = 2U,
  kCMP_ExternalSampleCount3 = 3U,
  kCMP_ExternalSampleCount4 = 4U,
  kCMP_ExternalSampleCount5 = 5U,
  kCMP_ExternalSampleCount6 = 6U,
  kCMP_ExternalSampleCount7 = 7U
}
 CMP external sample count. More...
 
enum  cmp_output_source_t {
  kCMP_OutputSourceFromFilterCOUT = 0U,
  kCMP_OutputSourceFromUnfilteredCOUTA = 1U
}
 CMP output source enumerator. More...
 
enum  cmp_work_mode_t {
  kCMP_WorkModeWindowBypassAndNoExternalSample = 0U,
  kCMP_WorkModeWindowBypassAndExternalSample = 1U,
  kCMP_WorkModeWindowEnabledAndNoExternalSample = 2U
}
 CMP work mode definition. More...
 

Driver version

#define FSL_CMP_DRIVER_VERSION   (MAKE_VERSION(2, 0, 0))
 CMP driver version 2.0.0. More...
 

CMP Initialization and de-initialization interfaces

void CMP_GetDefaultConfig (cmp_config_t *psConfig)
 Initializes the CMP user configuration structure. More...
 
void CMP_Init (CMP_Type *base, const cmp_config_t *psConfig)
 Initializes the CMP. More...
 
void CMP_Deinit (CMP_Type *base)
 De-initializes the CMP module. More...
 

CMP functional interfaces

static void CMP_Enable (CMP_Type *base, bool bEnable)
 Enables/disables the CMP module. More...
 
static void CMP_SetInputChannel (CMP_Type *base, cmp_input_mux_t ePlusChannel, cmp_input_mux_t eMinusChannel)
 Sets the input channels for the comparator. More...
 
static void CMP_SelectOutputSource (CMP_Type *base, cmp_output_source_t eOutputSource)
 Select comparator output source. More...
 
static void CMP_EnableOuputPin (CMP_Type *base, bool bEnable)
 Enable/Disable Comparator output pin. More...
 
static uint8_t CMP_GetComparatorOutput (CMP_Type *base)
 Get Comparator output. More...
 
static void CMP_SetHysteresisLevel (CMP_Type *base, cmp_hysteresis_level_t eHysteresisLevel)
 Sets hysteresis level. More...
 
static void CMP_SetComparasionSpeedMode (CMP_Type *base, cmp_comparasion_speed_mode_t eComparatorSpeedMode)
 Sets comparison speed mode. More...
 
static void CMP_EnableInvertOutput (CMP_Type *base, bool bEnable)
 Enable/Disable comparator invert feature. More...
 
static void CMP_EnableWindow (CMP_Type *base, bool bEnable)
 Enable the window function. More...
 
static void CMP_SetWindowOutputMode (CMP_Type *base, cmp_window_output_mode_t eWindowOutputMode)
 Set the window output mode. More...
 
static void CMP_EnableExternalSampleMode (CMP_Type *base, bool bEnable)
 Enable/Disable external Sample mode. More...
 
static void CMP_SetExternalSampleCount (CMP_Type *base, cmp_external_sample_count_t eSampleCount)
 Sets external sample count. More...
 
static void CMP_SetInternalFilterCount (CMP_Type *base, cmp_filter_count_t eFilterCount)
 Sets internal filter count. More...
 
static void CMP_SetInternalFilterPeriod (CMP_Type *base, uint8_t u8FilterPeriod)
 Sets the internal filter period. More...
 
void CMP_SetDACConfig (CMP_Type *base, const cmp_dac_config_t *psConfig)
 Configures the internal DAC. More...
 
static void CMP_SetDACOutputVoltage (CMP_Type *base, uint8_t u8OutputVoltageDivider)
 Sets DAC output voltage. More...
 
static void CMP_EnableInternalDAC (CMP_Type *base, bool bEnable)
 Enable/Disable internal DAC. More...
 
static void CMP_SetDACReferenceVoltageSource (CMP_Type *base, cmp_dac_vref_source_t eDACVrefSource)
 Sets internal DAC's reference voltage source. More...
 

CMP interrupt interfaces

static void CMP_EnableInterrupt (CMP_Type *base, cmp_interrupt_request_t eInterruptRequest)
 Interrupt request to enable. More...
 

CMP status interfaces

static cmp_output_flag_t CMP_GetStatusFlags (CMP_Type *base)
 Gets the status flags. More...
 
static void CMP_ClearStatusFlags (CMP_Type *base, cmp_output_flag_t eOutputFlag)
 Clears the status flags. More...
 

CMP DMA interfaces

static void CMP_EnableDMA (CMP_Type *base, cmp_dma_request_t eDMARequestType)
 Enables CMP DMA request. More...
 
static void * CMP_GetComparatorResultRegisterAddress (CMP_Type *base)
 Get CMP result register address for DMA access. More...
 

Data Structure Documentation

struct cmp_dac_config_t

Data Fields

cmp_dac_vref_source_t eDACVrefSource
 DAC reference voltage source. More...
 
uint8_t u8DACOutputVoltageDivider
 divider Value for the DAC Output Voltage, DAC output voltage = (VREF / 256) * (u8DACOutputVoltageDivider + 1). More...
 
bool bEnableInternalDAC
 flag to control if the internal DAC need to be enabled
 

Field Documentation

cmp_dac_vref_source_t cmp_dac_config_t::eDACVrefSource
uint8_t cmp_dac_config_t::u8DACOutputVoltageDivider
union cmp_dma_interrupt_config_t
Note
, the interrupt request and dma request cannot be used at the same time, that is to say When DMA support is enabled by setting SCR[DMAEN] and the interrupt is enabled by setting SCR[IER], SCR[IEF], or both, the corresponding change on COUT forces a DMA transfer request rather than a CPU interrupt instead

Data Fields

cmp_dma_request_t eDMARequest
 dma request type
 
cmp_interrupt_request_t eInterruptRequest
 interrupt request type
 
struct cmp_config_t

Data Fields

cmp_hysteresis_level_t eHysteresisLevel
 CMP hysteresis leveL.
 
cmp_comparasion_speed_mode_t eComparasionSpeedMode
 CMP comparison speed mode.
 
cmp_work_mode_t eWorkMode
 CMP work mode.
 
cmp_input_mux_t ePlusInput
 CMP plus input mux, the definition of this enum is in soc header file.
 
cmp_input_mux_t eMinusInput
 CMP minus input mux, the definition of this enum is in soc header file.
 
cmp_dac_config_t sDacConfig
 CMP internal DAC configuration structure cmp_dac_config_t.
 
bool bInvertComparatorOutputPolarity
 Inverted comparator output polarity. More...
 
cmp_window_output_mode_t eWindowOutputMode
 only works when cmp work mode is kCMP_WorkModeWindowEnabledAndNoExternalSample
 
cmp_filter_count_t eFilterCount
 Filter Count.Available range is 0-7, 0 is disable internal filter can be used in internal sampling mode only. More...
 
uint8_t u8FilterPeriod
 Filter Period. More...
 
cmp_external_sample_count_t eExternalSampleCount
 Available range is 1 - 7, used in external sampling mode only.
 
cmp_output_source_t eOutputSource
 cmp output source
 
bool bEnableOutputPin
 the comparator output(CMPO) is driven out on the associated CMPO output pin
 
cmp_dma_interrupt_config_t uDmaInterruptConfig
 CMP interrupt/dma configuration.
 
bool bCMPEnable
 flag to control if CMP module start immediately when the configuration is done
 

Field Documentation

bool cmp_config_t::bInvertComparatorOutputPolarity
cmp_filter_count_t cmp_config_t::eFilterCount
uint8_t cmp_config_t::u8FilterPeriod

The divider to the bus clock. Available range is 0-255, can be used in internal sampling mode. When the filter clock from internal divided bus clock, setting the sample period to 0 will disable the filter

Macro Definition Documentation

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

Enumeration Type Documentation

Enumerator
kCMP_InterruptRequestDisabled 

interrupt disabled

kCMP_InterruptRequestEnableOutputRisingEdge 

Comparator interrupt request enable rising edge.

kCMP_InterruptRequestEnableOutputFallingEdge 

Comparator interrupt request enable falling edge.

kCMP_InterrruptRequestEnableAll 

comparator interrupt request enable on rising edge or falling edge

Enumerator
kCMP_DMARequestDisabled 

DMA disabled.

kCMP_DMARequestEnableOutputRisingEdge 

Comparator dma request enable on rising edge.

kCMP_DMARequestEnableOutputFallingEdge 

Comparator dnma request enable on falling edge.

kCMP_DMARequestEnableAll 

comparator dma request enable on rising edge or falling edge

Enumerator
kCMP_OutputFlagRisingEdge 

Rising-edge on the comparison output has occurred.

kCMP_OutputFlagFallingEdge 

Falling-edge on the comparison output has occurred.

kCMP_OutputFlagBothEdge 

Rising-edge and Falling-edge on the comparison output has occurred.

Enumerator
kCMP_HysteresisLevel0 

Hysteresis level 0.

kCMP_HysteresisLevel1 

Hysteresis level 1.

kCMP_HysteresisLevel2 

Hysteresis level 2.

kCMP_HysteresisLevel3 

Hysteresis level 3.

Enumerator
kCMP_ComparsionModeLowSpeed 

Low-Speed Comparison mode has lower current consumption.

kCMP_ComparsionModeHighSpeed 

High-Speed Comparison mode has higher current consumption.

Enumerator
kCMP_DACVrefSourceVin1 

Vin1 is selected as a resistor ladder network supply reference Vin.

kCMP_DACVrefSourceVin2 

Vin2 is selected as a resistor ladder network supply reference Vin.

Enumerator
kCMP_WindowOuputLastLatchedValue 

When WINDOW signal changes from 1 to 0, COUTA output holds the last latched value before WINDOW signal falls to 0.

kCMP_WindowOutputZeroValue 

When WINDOW signal changes from 1 to 0, COUTA output is forced to 0.

Enumerator
kCMP_FilterCountDisable 

filter is disabled

kCMP_FilterCount1 

1 sample must agrees, the comparator output is simply sampled

kCMP_FilterCount2 

2 consecutive samples must agrees

kCMP_FilterCount3 

3 consecutive samples must agrees

kCMP_FilterCount4 

4 consecutive samples must agrees

kCMP_FilterCount5 

5 consecutive samples must agrees

kCMP_FilterCount6 

6 consecutive samples must agrees

kCMP_FilterCount7 

7 consecutive samples must agrees

Enumerator
kCMP_ExternalSampleCount1 

1 sample must agrees, the comparator output is simply sampled

kCMP_ExternalSampleCount2 

2 consecutive samples must agrees

kCMP_ExternalSampleCount3 

3 consecutive samples must agrees

kCMP_ExternalSampleCount4 

4 consecutive samples must agrees

kCMP_ExternalSampleCount5 

5 consecutive samples must agrees

kCMP_ExternalSampleCount6 

6 consecutive samples must agrees

kCMP_ExternalSampleCount7 

7 consecutive samples must agrees

Enumerator
kCMP_OutputSourceFromFilterCOUT 

Set the filtered comparator output to equal COUT.

kCMP_OutputSourceFromUnfilteredCOUTA 

Set the unfiltered comparator output to equal COUTA.

Enumerator
kCMP_WorkModeWindowBypassAndNoExternalSample 

window block bypassed, external sampling mode disabled

kCMP_WorkModeWindowBypassAndExternalSample 

window block bypassed, external SAMPLE mode enable

kCMP_WorkModeWindowEnabledAndNoExternalSample 

window block enabled, external sampling mode disabled

Function Documentation

void CMP_GetDefaultConfig ( cmp_config_t psConfig)

This function initializes the user configuration structure to the default values. It is corresponding to the continuous mode configurations.

Parameters
psConfigpointer of cmp_config_t.
void CMP_Init ( CMP_Type *  base,
const cmp_config_t psConfig 
)

This function initializes the CMP module. The operations included are as follows.

  • Enable the clock for CMP module.
  • Configure the comparator according to the CMP configuration structure.
Parameters
baseCMP peripheral base address.
psConfigPointer to the configuration structure.
void CMP_Deinit ( CMP_Type *  base)

This function de-initializes the CMP module. The operations included are as follows.

  • Disabling the CMP module.
  • Disabling the clock for CMP module.
Parameters
baseCMP peripheral base address.
static void CMP_Enable ( CMP_Type *  base,
bool  bEnable 
)
inlinestatic
Parameters
baseCMP peripheral base address.
enableEnables or disables the module.
static void CMP_SetInputChannel ( CMP_Type *  base,
cmp_input_mux_t  ePlusChannel,
cmp_input_mux_t  eMinusChannel 
)
inlinestatic

This function sets the input channels for the comparator. Note that two input channels cannot be set the same way in the application. When the user selects the same input from the analog mux to the positive and negative port, the comparator is disabled automatically.

Parameters
baseCMP peripheral base address.
ePlusChannelPlus side input channel number, cmp_input_mux_t.
eMinusChannelMinus side input channel number, cmp_input_mux_t.
static void CMP_SelectOutputSource ( CMP_Type *  base,
cmp_output_source_t  eOutputSource 
)
inlinestatic
Parameters
baseCMP peripheral base address.
eOutputSourceThe output signal to be set, please reference cmp_output_source_t for details.
static void CMP_EnableOuputPin ( CMP_Type *  base,
bool  bEnable 
)
inlinestatic
Parameters
baseCMP peripheral base address.
bEnableEnable/Disable comparator output pin. true – CMPO is available on the associate CMPO output pin. false – CMPO is not available on the associate CMPO output pin.
static uint8_t CMP_GetComparatorOutput ( CMP_Type *  base)
inlinestatic
Parameters
baseCMP peripheral base address.
Return values
currentanalog comparator output 0 or 1
static void CMP_SetHysteresisLevel ( CMP_Type *  base,
cmp_hysteresis_level_t  eHysteresisLevel 
)
inlinestatic
Parameters
baseCMP peripheral base address.
eHysteresisLevelThe programmable hysteresis level to be set, please refer to cmp_hysteresis_level_t for details.
static void CMP_SetComparasionSpeedMode ( CMP_Type *  base,
cmp_comparasion_speed_mode_t  eComparatorSpeedMode 
)
inlinestatic
Parameters
baseCMP peripheral base address.
eComparatorSpeedModeThe comparison speed mode, please reference cmp_comparasion_speed_mode_t for details.
static void CMP_EnableInvertOutput ( CMP_Type *  base,
bool  bEnable 
)
inlinestatic
Parameters
baseCMP peripheral base address.
bEnableEnable/Disable comparator invert feature. true – Inverts the comparator output. false – Does not invert the comparator output.
static void CMP_EnableWindow ( CMP_Type *  base,
bool  bEnable 
)
inlinestatic
Parameters
baseCMP peripheral base address.
bEnabletrue is enable, false is disable.
static void CMP_SetWindowOutputMode ( CMP_Type *  base,
cmp_window_output_mode_t  eWindowOutputMode 
)
inlinestatic
Parameters
baseCMP peripheral base address.
eWindowOutputModecmp_window_output_mode_t.
static void CMP_EnableExternalSampleMode ( CMP_Type *  base,
bool  bEnable 
)
inlinestatic
Parameters
baseCMP peripheral base address.
bEnabletrue is using external sample mode, false is using interface sample mode.
static void CMP_SetExternalSampleCount ( CMP_Type *  base,
cmp_external_sample_count_t  eSampleCount 
)
inlinestatic
Parameters
baseCMP peripheral base address.
eSampleCountThe number of consecutive samples that must agree prior to the comparator output filter accepting a new output state, cmp_external_sample_count_t.
static void CMP_SetInternalFilterCount ( CMP_Type *  base,
cmp_filter_count_t  eFilterCount 
)
inlinestatic
Parameters
baseCMP peripheral base address.
eFilterCountThe number of consecutive samples that must agree prior to the comparator output filter accepting a new output state, cmp_filter_count_t.
static void CMP_SetInternalFilterPeriod ( CMP_Type *  base,
uint8_t  u8FilterPeriod 
)
inlinestatic

It is used as the divider to bus clock.

Parameters
baseCMP peripheral base address.
u8FilterPeriodFilter Period. The divider to the bus clock. Available range is 0-255.
void CMP_SetDACConfig ( CMP_Type *  base,
const cmp_dac_config_t psConfig 
)
Parameters
baseCMP peripheral base address.
psConfigPointer to the configuration structure.
static void CMP_SetDACOutputVoltage ( CMP_Type *  base,
uint8_t  u8OutputVoltageDivider 
)
inlinestatic
Parameters
baseCMP peripheral base address.
u8OutputVoltageDividerThe digital value which is related to the desired DAC output voltage,
static void CMP_EnableInternalDAC ( CMP_Type *  base,
bool  bEnable 
)
inlinestatic
Parameters
baseCMP peripheral base address.
bEnableEnable/Disable internal DAC. true – Enable internal DAC. false – Disable internal DAC.
static void CMP_SetDACReferenceVoltageSource ( CMP_Type *  base,
cmp_dac_vref_source_t  eDACVrefSource 
)
inlinestatic
Parameters
baseCMP peripheral base address.
eDACVrefSourcereference voltage source, please cmp_dac_vref_source_t
static void CMP_EnableInterrupt ( CMP_Type *  base,
cmp_interrupt_request_t  eInterruptRequest 
)
inlinestatic
Parameters
baseCMP peripheral base address.
eInterruptRequestMask value for interrupts. See cmp_interrupt_request_t.
static cmp_output_flag_t CMP_GetStatusFlags ( CMP_Type *  base)
inlinestatic
Parameters
baseCMP peripheral base address.
Return values
Maskvalue for the asserted flags. cmp_output_flag_t.
static void CMP_ClearStatusFlags ( CMP_Type *  base,
cmp_output_flag_t  eOutputFlag 
)
inlinestatic
Parameters
baseCMP peripheral base address.
eOutputFlagMask value for the output flags, cmp_output_flag_t
static void CMP_EnableDMA ( CMP_Type *  base,
cmp_dma_request_t  eDMARequestType 
)
inlinestatic
Parameters
baseCMP peripheral base address.
eDMARequestTypecmp_dma_request_t
static void* CMP_GetComparatorResultRegisterAddress ( CMP_Type *  base)
inlinestatic
Parameters
baseCMP peripheral base address.