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

Overview

The MCUXpresso SDK provides a peripheral driver for the Analog Comparator (CMP) module of MCUXpresso SDK devices.

The CMP driver is a basic comparator with advanced features. The APIs for the basic comparator enable the CMP to compare the two voltages of the two input channels and create the output of the comparator result. The APIs for advanced features can be used as the plug-in functions based on the basic comparator. They can process the comparator's output with hardware support.

Typical use case

Polling Configuration

int main(void)
{
cmp_config_t mCmpConfigStruct;
cmp_dac_config_t mCmpDacConfigStruct;
// ...
// Configures the comparator.
CMP_Init(DEMO_CMP_INSTANCE);
CMP_GetDefaultConfig(&mCmpConfigStruct);
CMP_Configure(DEMO_CMP_INSTANCE, &mCmpConfigStruct);
// Configures the DAC channel.
mCmpDacConfigStruct.referenceVoltageSource = kCMP_VrefSourceVin2; // VCC.
mCmpDacConfigStruct.DACValue = 32U; // Half voltage of logic high-level.
CMP_SetDACConfig(DEMO_CMP_INSTANCE, &mCmpDacConfigStruct);
CMP_SetInputChannels(DEMO_CMP_INSTANCE, DEMO_CMP_USER_CHANNEL, DEMO_CMP_DAC_CHANNEL);
while (1)
{
if (0U != (kCMP_OutputAssertEventFlag & CMP_GetStatusFlags(DEMO_CMP_INSTANCE)))
{
// Do something.
}
else
{
// Do something.
}
}
}

Interrupt Configuration

volatile uint32_t g_CmpFlags = 0U;
// ...
void DEMO_CMP_IRQ_HANDLER_FUNC(void)
{
g_CmpFlags = CMP_GetStatusFlags(DEMO_CMP_INSTANCE);
if (0U != (g_CmpFlags & kCMP_OutputRisingEventFlag))
{
// Do something.
}
else if (0U != (g_CmpFlags & kCMP_OutputFallingEventFlag))
{
// Do something.
}
}
int main(void)
{
cmp_config_t mCmpConfigStruct;
cmp_dac_config_t mCmpDacConfigStruct;
// ...
EnableIRQ(DEMO_CMP_IRQ_ID);
// ...
// Configures the comparator.
CMP_Init(DEMO_CMP_INSTANCE);
CMP_GetDefaultConfig(&mCmpConfigStruct);
CMP_Configure(DEMO_CMP_INSTANCE, &mCmpConfigStruct);
// Configures the DAC channel.
mCmpDacConfigStruct.referenceVoltageSource = kCMP_VrefSourceVin2; // VCC.
mCmpDacConfigStruct.DACValue = 32U; // Half voltage of logic high-level.
CMP_SetDACConfig(DEMO_CMP_INSTANCE, &mCmpDacConfigStruct);
CMP_SetInputChannels(DEMO_CMP_INSTANCE, DEMO_CMP_USER_CHANNEL, DEMO_CMP_DAC_CHANNEL);
// Enables the output rising and falling interrupts.
while (1)
{
}
}

Data Structures

struct  cmp_config_t
 Configures the comparator. More...
 
struct  cmp_filter_config_t
 Configures the filter. More...
 
struct  cmp_dac_config_t
 Configures the internal DAC. More...
 

Enumerations

enum  _cmp_interrupt_enable {
  kCMP_OutputRisingInterruptEnable = CMP_SCR_IER_MASK,
  kCMP_OutputFallingInterruptEnable = CMP_SCR_IEF_MASK
}
 Interrupt enable/disable mask. More...
 
enum  _cmp_status_flags {
  kCMP_OutputRisingEventFlag = CMP_SCR_CFR_MASK,
  kCMP_OutputFallingEventFlag = CMP_SCR_CFF_MASK,
  kCMP_OutputAssertEventFlag = CMP_SCR_COUT_MASK
}
 Status flags' mask. More...
 
enum  cmp_hysteresis_mode_t {
  kCMP_HysteresisLevel0 = 0U,
  kCMP_HysteresisLevel1 = 1U,
  kCMP_HysteresisLevel2 = 2U,
  kCMP_HysteresisLevel3 = 3U
}
 CMP Hysteresis mode. More...
 
enum  cmp_reference_voltage_source_t {
  kCMP_VrefSourceVin1 = 0U,
  kCMP_VrefSourceVin2 = 1U
}
 CMP Voltage Reference source. More...
 

Driver version

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

Initialization

void CMP_Init (CMP_Type *base, const cmp_config_t *config)
 Initializes the CMP. More...
 
void CMP_Deinit (CMP_Type *base)
 De-initializes the CMP module. More...
 
static void CMP_Enable (CMP_Type *base, bool enable)
 Enables/disables the CMP module. More...
 
void CMP_GetDefaultConfig (cmp_config_t *config)
 Initializes the CMP user configuration structure. More...
 
void CMP_SetInputChannels (CMP_Type *base, uint8_t positiveChannel, uint8_t negativeChannel)
 Sets the input channels for the comparator. More...
 

Advanced Features

void CMP_EnableDMA (CMP_Type *base, bool enable)
 Enables/disables the DMA request for rising/falling events. More...
 
static void CMP_EnableWindowMode (CMP_Type *base, bool enable)
 Enables/disables the window mode. More...
 
void CMP_SetFilterConfig (CMP_Type *base, const cmp_filter_config_t *config)
 Configures the filter. More...
 
void CMP_SetDACConfig (CMP_Type *base, const cmp_dac_config_t *config)
 Configures the internal DAC. More...
 
void CMP_EnableInterrupts (CMP_Type *base, uint32_t mask)
 Enables the interrupts. More...
 
void CMP_DisableInterrupts (CMP_Type *base, uint32_t mask)
 Disables the interrupts. More...
 

Results

uint32_t CMP_GetStatusFlags (CMP_Type *base)
 Gets the status flags. More...
 
void CMP_ClearStatusFlags (CMP_Type *base, uint32_t mask)
 Clears the status flags. More...
 

Data Structure Documentation

struct cmp_config_t

Data Fields

bool enableCmp
 Enable the CMP module. More...
 
cmp_hysteresis_mode_t hysteresisMode
 CMP Hysteresis mode. More...
 
bool enableHighSpeed
 Enable High-speed (HS) comparison mode. More...
 
bool enableInvertOutput
 Enable the inverted comparator output. More...
 
bool useUnfilteredOutput
 Set the compare output(COUT) to equal COUTA(true) or COUT(false). More...
 
bool enablePinOut
 The comparator output is available on the associated pin. More...
 
bool enableTriggerMode
 Enable the trigger mode. More...
 

Field Documentation

bool cmp_config_t::enableCmp
cmp_hysteresis_mode_t cmp_config_t::hysteresisMode
bool cmp_config_t::enableHighSpeed
bool cmp_config_t::enableInvertOutput
bool cmp_config_t::useUnfilteredOutput
bool cmp_config_t::enablePinOut
bool cmp_config_t::enableTriggerMode
struct cmp_filter_config_t

Data Fields

bool enableSample
 Using the external SAMPLE as a sampling clock input or using a divided bus clock. More...
 
uint8_t filterCount
 Filter Sample Count. More...
 
uint8_t filterPeriod
 Filter Sample Period. More...
 

Field Documentation

bool cmp_filter_config_t::enableSample
uint8_t cmp_filter_config_t::filterCount

Available range is 1-7; 0 disables the filter.

uint8_t cmp_filter_config_t::filterPeriod

The divider to the bus clock. Available range is 0-255.

struct cmp_dac_config_t

Data Fields

cmp_reference_voltage_source_t referenceVoltageSource
 Supply voltage reference source. More...
 
uint8_t DACValue
 Value for the DAC Output Voltage. More...
 

Field Documentation

cmp_reference_voltage_source_t cmp_dac_config_t::referenceVoltageSource
uint8_t cmp_dac_config_t::DACValue

Available range is 0-63.

Macro Definition Documentation

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

Enumeration Type Documentation

Enumerator
kCMP_OutputRisingInterruptEnable 

Comparator interrupt enable rising.

kCMP_OutputFallingInterruptEnable 

Comparator interrupt enable falling.

Enumerator
kCMP_OutputRisingEventFlag 

Rising-edge on the comparison output has occurred.

kCMP_OutputFallingEventFlag 

Falling-edge on the comparison output has occurred.

kCMP_OutputAssertEventFlag 

Return the current value of the analog comparator output.

Enumerator
kCMP_HysteresisLevel0 

Hysteresis level 0.

kCMP_HysteresisLevel1 

Hysteresis level 1.

kCMP_HysteresisLevel2 

Hysteresis level 2.

kCMP_HysteresisLevel3 

Hysteresis level 3.

Enumerator
kCMP_VrefSourceVin1 

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

kCMP_VrefSourceVin2 

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

Function Documentation

void CMP_Init ( CMP_Type *  base,
const cmp_config_t config 
)

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

  • Enabling the clock for CMP module.
  • Configuring the comparator.
  • Enabling the CMP module. Note that for some devices, multiple CMP instances share the same clock gate. In this case, to enable the clock for any instance enables all CMPs. See the appropriate MCU reference manual for the clock assignment of the CMP.
Parameters
baseCMP peripheral base address.
configPointer 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.

This function disables the clock for the CMP. Note that for some devices, multiple CMP instances share the same clock gate. In this case, before disabling the clock for the CMP, ensure that all the CMP instances are not used.

Parameters
baseCMP peripheral base address.
static void CMP_Enable ( CMP_Type *  base,
bool  enable 
)
inlinestatic
Parameters
baseCMP peripheral base address.
enableEnables or disables the module.
void CMP_GetDefaultConfig ( cmp_config_t config)

This function initializes the user configuration structure to these default values.

* config->enableCmp = true;
* config->hysteresisMode = kCMP_HysteresisLevel0;
* config->enableHighSpeed = false;
* config->enableInvertOutput = false;
* config->useUnfilteredOutput = false;
* config->enablePinOut = false;
* config->enableTriggerMode = false;
*
Parameters
configPointer to the configuration structure.
void CMP_SetInputChannels ( CMP_Type *  base,
uint8_t  positiveChannel,
uint8_t  negativeChannel 
)

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.
positiveChannelPositive side input channel number. Available range is 0-7.
negativeChannelNegative side input channel number. Available range is 0-7.
void CMP_EnableDMA ( CMP_Type *  base,
bool  enable 
)

This function enables/disables the DMA request for rising/falling events. Either event triggers the generation of the DMA request from CMP if the DMA feature is enabled. Both events are ignored for generating the DMA request from the CMP if the DMA is disabled.

Parameters
baseCMP peripheral base address.
enableEnables or disables the feature.
static void CMP_EnableWindowMode ( CMP_Type *  base,
bool  enable 
)
inlinestatic
Parameters
baseCMP peripheral base address.
enableEnables or disables the feature.
void CMP_SetFilterConfig ( CMP_Type *  base,
const cmp_filter_config_t config 
)
Parameters
baseCMP peripheral base address.
configPointer to the configuration structure.
void CMP_SetDACConfig ( CMP_Type *  base,
const cmp_dac_config_t config 
)
Parameters
baseCMP peripheral base address.
configPointer to the configuration structure. "NULL" disables the feature.
void CMP_EnableInterrupts ( CMP_Type *  base,
uint32_t  mask 
)
Parameters
baseCMP peripheral base address.
maskMask value for interrupts. See "_cmp_interrupt_enable".
void CMP_DisableInterrupts ( CMP_Type *  base,
uint32_t  mask 
)
Parameters
baseCMP peripheral base address.
maskMask value for interrupts. See "_cmp_interrupt_enable".
uint32_t CMP_GetStatusFlags ( CMP_Type *  base)
Parameters
baseCMP peripheral base address.
Returns
Mask value for the asserted flags. See "_cmp_status_flags".
void CMP_ClearStatusFlags ( CMP_Type *  base,
uint32_t  mask 
)
Parameters
baseCMP peripheral base address.
maskMask value for the flags. See "_cmp_status_flags".