All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
CMP: Analog Comparator Driver

## Overview

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

The CMP driver is a basic comparator with advanced features. The APIs for the basic comparator enable the CMP as a general comparator, which compares two voltages of the two input channels and creates the output of the comparator result. The APIs for advanced features can be used as the plug-in function 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);
CMP_ClearStatusFlags(DEMO_CMP_INSTANCE, kCMP_OutputRisingEventFlag | kCMP_OutputFallingEventFlag);
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.
CMP_EnableInterrupts(DEMO_CMP_INSTANCE, kCMP_OutputRisingInterruptEnable | kCMP_OutputFallingInterruptEnable);
while (1)
{
}
}

## Data Structures

struct | cmp_config_t
| Configuration for the comparator. More...
struct | cmp_filter_config_t
| Configuration for the filter. More...
struct | cmp_dac_config_t
| Configuration for 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_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 comparison mode. More...
bool | enableInvertOutput
| Enable inverted comparator output. More...
bool | useUnfilteredOutput
| Set compare output(COUT) to equal COUTA(true) or COUT(false). More...
bool | enablePinOut
| The comparator output is available on the associated pin. 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

struct cmp_filter_config_t

### Data Fields

uint8_t | filterCount
| Filter Sample Count. More...
uint8_t | filterPeriod
| Filter Sample Period. More...

#### Field Documentation

uint8_t cmp_filter_config_t::filterCount

Available range is 1-7, 0 would cause the filter disabled.

uint8_t cmp_filter_config_t::filterPeriod

The divider to 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 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

enum _cmp_interrupt_enable

Enumerator
kCMP_OutputRisingInterruptEnable | Comparator interrupt enable rising.
kCMP_OutputFallingInterruptEnable | Comparator interrupt enable falling.

enum _cmp_status_flags

Enumerator
kCMP_OutputRisingEventFlag | Rising-edge on compare output has occurred.
kCMP_OutputFallingEventFlag | Falling-edge on compare output has occurred.
kCMP_OutputAssertEventFlag | Return the current value of the analog comparator output.

enum cmp_hysteresis_mode_t

Enumerator
kCMP_HysteresisLevel0 | Hysteresis level 0.
kCMP_HysteresisLevel1 | Hysteresis level 1.
kCMP_HysteresisLevel2 | Hysteresis level 2.
kCMP_HysteresisLevel3 | Hysteresis level 3.

enum cmp_reference_voltage_source_t

Enumerator
kCMP_VrefSourceVin1 | Vin1 is selected as resistor ladder network supply reference Vin.
kCMP_VrefSourceVin2 | Vin2 is selected as 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:

- Enabling the clock for CMP module.
- Configuring the comparator.
- Enabling the CMP module. Note: For some devices, multiple CMP instance share the same clock gate. In this case, to enable the clock for any instance enables all the CMPs. Check the chip reference manual for the clock assignment of the CMP.

Parameters

  base | CMP peripheral base address.
  config | Pointer to configuration structure.

void CMP_Deinit | ( | CMP_Type * | base | ) |

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

- Disabling the CMP module.
- Disabling the clock for CMP module.

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

Parameters

  base | CMP peripheral base address.

static void CMP_Enable | ( | CMP_Type * | base,
|  | bool | enable
| ) |  |

inlinestatic

Parameters

  base | CMP peripheral base address.
  enable | Enable the module or not.

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

  config | Pointer 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 as same 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

  base | CMP peripheral base address.
  positiveChannel | Positive side input channel number. Available range is 0-7.
  negativeChannel | Negative side input channel number. Available range is 0-7.

void CMP_SetFilterConfig | ( | CMP_Type * | base,
|  | const cmp_filter_config_t * | config
| ) |  |

Parameters

  base | CMP peripheral base address.
  config | Pointer to configuration structure.

void CMP_SetDACConfig | ( | CMP_Type * | base,
|  | const cmp_dac_config_t * | config
| ) |  |

Parameters

  base | CMP peripheral base address.
  config | Pointer to configuration structure. "NULL" is for disabling the feature.

void CMP_EnableInterrupts | ( | CMP_Type * | base,
|  | uint32_t | mask
| ) |  |

Parameters

  base | CMP peripheral base address.
  mask | Mask value for interrupts. See "_cmp_interrupt_enable".

void CMP_DisableInterrupts | ( | CMP_Type * | base,
|  | uint32_t | mask
| ) |  |

Parameters

  base | CMP peripheral base address.
  mask | Mask value for interrupts. See "_cmp_interrupt_enable".

uint32_t CMP_GetStatusFlags | ( | CMP_Type * | base | ) |

Parameters

  base | CMP peripheral base address.

Returns
  Mask value for the asserted flags. See "_cmp_status_flags".

void CMP_ClearStatusFlags | ( | CMP_Type * | base,
|  | uint32_t | mask
| ) |  |

Parameters

  base | CMP peripheral base address.
  mask | Mask value for the flags. See "_cmp_status_flags".