Kinetis SDK v.1.3 API Reference Manual  Rev. 0
Freescale Semiconductor, Inc.
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
CMP Peripheral Driver

Overview

This section describes the programming interface of the CMP Peripheral driver. The CMP peripheral driver configures the CMP (Comparator). It handles initialization and configuration of CMP module.

CMP Driver model building

CMP driver has three parts:

APIs are separate for each part and can be customized according to the application requirements.

CMP Call diagram

  1. Ensure that the pin mux settings are ready before using the driver.
  2. Call the "CMP_DRV_Init()" function to initialize the basic comparator. A configuration structure "cmp_user_config_t" type is required to hold the initialization information. The structure is populated by the application for a specific case, or by the "CMP_DRV_StructInitUserConfigDefault" API with available settings. A memory block is required and should be represented as a variable of the "cmp_state_t" type to keep state with the CMP_DRV_Init() function.
  3. Optionally, call the "CMP_DRV_EnableDac()" function to configure the internal 6-bit DAC if it is used as one of the input channels. A configuration structure of the "cmp_dac_config_t" type is required.
  4. Optionally, call the "CMP_DRV_ConfigSampleFilter()" function to configure the Sample/Filter. A configuration structure of the "cmp_sample_filter_config_t" type is required to keep the configuration.
  5. Optionally, call the "CMP_DRV_InstallCallback" function to install the user-defined callback function into the interrupt routine service. When the CMP is configured to enable the interrupt, the installed callback function is called after the interrupt event occurs.
  6. Finally, the CMP automatically responds to the external events.

This is an example to initialize and configure the CMP driver for typical use cases.

// cmp_test.c //
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include "fsl_cmp_driver.h"
#include <board.h>
#include "fsl_device_registers.h"
#include "fsl_os_abstraction.h"
#define TEST_CMP_INSTANCE BOARD_CMP_INSTANCE
#define TEST_TIME_OUT_MS (4000U)
cmp_state_t TestCmpStateStruct;
// Normal Interrupt mode. //
volatile bool bRisingEvent = false;
volatile bool bFallingEvent = false;
volatile uint32_t TimeMs;
static void CMP_TEST_ISR(void);
static void CMP_TEST_InitIO(void);
extern void CMP_TEST_InstallCallback(uint32_t instance, void (*callbackFunc)(void) );
int main(void)
{
cmp_comparator_config_t testCmpUserConfigStruct;
cmp_sample_filter_config_t testCmpSampleFilterConfigStruct;
cmp_dac_config_t testCmpDacConfigStruct;
// Init IO for CMP0_IN0. Booad don't have pull up resister, so internal resister need to be enabled//
#if defined(TWR_K64F120M)
PORT_HAL_SetPullMode(PORTC,6U,kPortPullUp);
PORT_HAL_SetPullCmd(PORTC,6U,true);
#else
#endif
PRINTF("CMP PD TEST: Start...\r\n");
// Init the CMP comparator. //
CMP_DRV_StructInitUserConfigDefault(&testCmpUserConfigStruct, (cmp_chn_mux_mode_t)BOARD_CMP_CHANNEL, kCmpInputChnDac);
testCmpUserConfigStruct.risingIntEnable = true;
testCmpUserConfigStruct.fallingIntEnable = true;
CMP_DRV_Init(TEST_CMP_INSTANCE, &testCmpStateStruct, &testCmpUserConfigStruct);
// Configure the internal DAC when in used. //
testCmpDacConfigStruct.dacEnable = true;
testCmpDacConfigStruct.dacValue = 32U; // 0U - 63U //
testCmpDacConfigStruct.refVoltSrcMode = kCmpDacRefVoltSrcOf2;
CMP_DRV_ConfigDacChn(TEST_CMP_INSTANCE, &testCmpDacConfigStruct);
// Configure the Sample/Filter Mode. //
testCmpSampleFilterConfigStruct.workMode = kCmpContinuousMode;
testCmpSampleFilterConfigStruct.useExtSampleOrWindow = false;
testCmpSampleFilterConfigStruct.filterClkDiv = 16U;
testCmpSampleFilterConfigStruct.filterCount = kCmpFilterCountSampleOf4;
CMP_DRV_ConfigSampleFilter(TEST_CMP_INSTANCE, &testCmpSampleFilterConfigStruct);
// Install the callback into interrupt. //
CMP_TEST_InstallCallback(TEST_CMP_INSTANCE, CMP_TEST_ISR);
// Start the CMP function. //
CMP_DRV_Start(TEST_CMP_INSTANCE);
PRINTF("Please input signal in %d ms\r\n", TEST_TIME_OUT_MS);
TimeMs = OSA_TimeGetMsec();
while (1)
{
if ( (TimeMs+TEST_TIME_OUT_MS) <= OSA_TimeGetMsec() )
{
break;
}
if (bRisingEvent)
{
PRINTF("^ CMP output rising event occur!\r\n");
PRINTF(" CMP output level %d\r\n", CMP_DRV_GetOutputLogic(TEST_CMP_INSTANCE) );
bRisingEvent = false;
}
if (bFallingEvent)
{
PRINTF("v CMP output failing event occur!\r\n");
PRINTF(" CMP output level %d\r\n", CMP_DRV_GetOutputLogic(TEST_CMP_INSTANCE) );
bFallingEvent = false;
}
}
CMP_DRV_Deinit(TEST_CMP_INSTANCE);
PRINTF("CMP PD Test ");
PRINTF("Succeed\r\n");
while(1){}
}
static void CMP_TEST_ISR(void)
{
if (CMP_DRV_GetFlag(TEST_CMP_INSTANCE, kCmpFlagOfCoutRising) )
{
if (!bRisingEvent)
{
bRisingEvent = true;
}
}
if (CMP_DRV_GetFlag(TEST_CMP_INSTANCE, kCmpFlagOfCoutFalling) )
{
if (!bFallingEvent)
{
bFallingEvent = true;
}
}
}

Data Structures

struct  cmp_state_t
 Internal driver state information. More...
 

Enumerations

enum  cmp_flag_t {
  kCmpFlagOfCoutRising = 0U,
  kCmpFlagOfCoutFalling = 1U
}
 Defines type of flags for the CMP event. More...
 

Functions

cmp_status_t CMP_DRV_StructInitUserConfigDefault (cmp_comparator_config_t *userConfigPtr, cmp_chn_mux_mode_t plusInput, cmp_chn_mux_mode_t minusInput)
 Populates the initial user configuration with default settings. More...
 
cmp_status_t CMP_DRV_Init (uint32_t instance, cmp_state_t *userStatePtr, const cmp_comparator_config_t *userConfigPtr)
 Initializes the CMP module. More...
 
cmp_status_t CMP_DRV_Deinit (uint32_t instance)
 De-initializes the CMP module. More...
 
void CMP_DRV_Start (uint32_t instance)
 Starts the CMP module. More...
 
void CMP_DRV_Stop (uint32_t instance)
 Stops the CMP module. More...
 
cmp_status_t CMP_DRV_ConfigDacChn (uint32_t instance, const cmp_dac_config_t *dacConfigPtr)
 Enables the internal DAC in the CMP module. More...
 
cmp_status_t CMP_DRV_ConfigSampleFilter (uint32_t instance, const cmp_sample_filter_config_t *configPtr)
 Configures the Sample feature in the CMP module. More...
 
bool CMP_DRV_GetOutputLogic (uint32_t instance)
 Gets the output of the CMP module. More...
 
bool CMP_DRV_GetFlag (uint32_t instance, cmp_flag_t flag)
 Gets the state of the CMP module. More...
 
void CMP_DRV_ClearFlag (uint32_t instance, cmp_flag_t flag)
 Clears the event record of the CMP module. More...
 

Variables

CMP_Type *const g_cmpBase []
 Table of base addresses for CMP instances. More...
 
const IRQn_Type g_cmpIrqId [CMP_INSTANCE_COUNT]
 Table to save CMP IRQ enumeration numbers defined in CMSIS header file. More...
 

Data Structure Documentation

struct cmp_state_t

The contents of this structure are internal to the driver and should not be modified by users. Also, contents of the structure are subject to change in future releases.

Enumeration Type Documentation

enum cmp_flag_t
Enumerator
kCmpFlagOfCoutRising 

Identifier to indicate if the COUT change from logic zero to one.

kCmpFlagOfCoutFalling 

Identifier to indicate if the COUT change from logic one to zero.

Function Documentation

cmp_status_t CMP_DRV_StructInitUserConfigDefault ( cmp_comparator_config_t userConfigPtr,
cmp_chn_mux_mode_t  plusInput,
cmp_chn_mux_mode_t  minusInput 
)

This function populates the initial user configuration with default settings. The default settings enable the CMP module to operate as a comparator. The settings are :

  • .hystersisMode = kCmpHystersisOfLevel0
  • .pinoutEnable = true
  • .pinoutUnfilteredEnable = true
  • .invertEnable = false
  • .highSpeedEnable = false
  • .dmaEnable = false
  • .risingIntEnable = false
  • .fallingIntEnable = false
  • .triggerEnable = false However, it is still recommended to fill some fields of structure such as channel mux according to the application requirements. Note that this function does not set the configuration to hardware.
Parameters
userConfigPtrPointer to structure of configuration. See "cmp_user_config_t".
plusInputPlus Input mux selection. See "cmp_chn_mux_mode_t".
minusInputMinus Input mux selection. See "cmp_chn_mux_mode_t".
Returns
Execution status.
cmp_status_t CMP_DRV_Init ( uint32_t  instance,
cmp_state_t userStatePtr,
const cmp_comparator_config_t userConfigPtr 
)

This function initializes the CMP module, enables the clock, and sets the interrupt switcher. The CMP module is configured as a basic comparator.

Parameters
instanceCMP instance ID.
userStatePtrPointer to structure of context. See "cmp_state_t".
userConfigPtrPointer to structure of configuration. See "cmp_user_config_t".
Returns
Execution status.
cmp_status_t CMP_DRV_Deinit ( uint32_t  instance)

This function de-initializes the CMP module. It shuts down the CMP clock and disables the interrupt. This API should be called when CMP is no longer used in the application. It also reduces power consumption.

Parameters
instanceCMP instance ID.
Returns
Execution status.
void CMP_DRV_Start ( uint32_t  instance)

This function starts the CMP module. The configuration does not take effect until the module is started.

Parameters
instanceCMP instance ID.
void CMP_DRV_Stop ( uint32_t  instance)

This function stops the CMP module. Note that this function does not shut down the module, but only pauses the features.

Parameters
instanceCMP instance ID.
cmp_status_t CMP_DRV_ConfigDacChn ( uint32_t  instance,
const cmp_dac_config_t dacConfigPtr 
)

This function enables the internal DAC in the CMP module. It takes effect only when the internal DAC has been chosen as an input channel for the comparator. Then, the DAC channel can be programmed to provide a reference voltage level.

Parameters
instanceCMP instance ID.
dacConfigPtrPointer to structure of configuration. See "cmp_dac_config_t".
Returns
Execution status.
cmp_status_t CMP_DRV_ConfigSampleFilter ( uint32_t  instance,
const cmp_sample_filter_config_t configPtr 
)

This function configures the CMP working in Sample modes. These modes are advanced features in addition to the basic comparator such as Window Mode, Filter Mode, etc. See "cmp_sample_filter_config_t"for detailed description.

Parameters
instanceCMP instance ID.
configPtrPointer to a structure of configurations. See "cmp_sample_filter_config_t".
Returns
Execution status.
bool CMP_DRV_GetOutputLogic ( uint32_t  instance)

This function gets the output of the CMP module. The output source depends on the configuration when initializing the comparator. When the cmp_user_config_t.pinoutUnfilteredEnable is false, the output is processed by the filter. Otherwise, the output is that the signal did not pass the filter.

Parameters
instanceCMP instance ID.
Returns
Output logic's assertion. When not inverted, plus side > minus side, it is true.
bool CMP_DRV_GetFlag ( uint32_t  instance,
cmp_flag_t  flag 
)

This function gets the state of the CMP module. It returns if the indicated event has been detected.

Parameters
instanceCMP instance ID.
flagRepresent events or states. See "cmp_flag_t".
Returns
Assertion if indicated event occurs.
void CMP_DRV_ClearFlag ( uint32_t  instance,
cmp_flag_t  flag 
)

This function clears the event record of the CMP module.

Parameters
instanceCMP instance ID.
flagRepresent events or states. See "cmp_flag_t".

Variable Documentation

CMP_Type* const g_cmpBase[]
const IRQn_Type g_cmpIrqId[CMP_INSTANCE_COUNT]