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:
- Basic Comparator - This part handles the mechanism that compares the voltage from the two input channels and outputs the assertion if the plus side voltage is higher than the minus side voltage.
- Internal 6-bit DAC - The internal 6-bit DAC can be configured as one of the input channel for the basic comparator. It can provide a reference voltage level when comparing with the other voltage signal.
- Sample/Filter - The Sample/Filter is an additional feature to improve the signal of the comparator's output. A sample clock, window mode and accumulation filter can be used to configure the Sample/Filter. When configuring the Sample/Filter, limited setting are available. See the "cmp_sample_filter_mode_t" definition in the "fsl_cmp_driver.h" file, or a chip reference manual for detailed information.
APIs are separate for each part and can be customized according to the application requirements.
CMP Call diagram
- Ensure that the pin mux settings are ready before using the driver.
- 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.
- 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.
- 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.
- 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.
- Finally, the CMP automatically responds to the external events.
This is an example to initialize and configure the CMP driver for typical use cases.
#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)
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)
{
#if defined(TWR_K64F120M)
PORT_HAL_SetPullCmd(PORTC,6U,true);
#else
#endif
PRINTF("CMP PD TEST: Start...\r\n");
CMP_DRV_Init(TEST_CMP_INSTANCE, &testCmpStateStruct, &testCmpUserConfigStruct);
CMP_TEST_InstallCallback(TEST_CMP_INSTANCE, CMP_TEST_ISR);
PRINTF("Please input signal in %d ms\r\n", TEST_TIME_OUT_MS);
while (1)
{
{
break;
}
if (bRisingEvent)
{
PRINTF("^ CMP output rising event occur!\r\n");
bRisingEvent = false;
}
if (bFallingEvent)
{
PRINTF("v CMP output failing event occur!\r\n");
bFallingEvent = false;
}
}
PRINTF("CMP PD Test ");
PRINTF("Succeed\r\n");
while(1){}
}
static void CMP_TEST_ISR(void)
{
{
if (!bRisingEvent)
{
bRisingEvent = true;
}
}
{
if (!bFallingEvent)
{
bFallingEvent = true;
}
}
}
|
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...
|
|
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.
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.
|
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
-
userConfigPtr | Pointer to structure of configuration. See "cmp_user_config_t". |
plusInput | Plus Input mux selection. See "cmp_chn_mux_mode_t". |
minusInput | Minus Input mux selection. See "cmp_chn_mux_mode_t". |
- Returns
- Execution status.
This function initializes the CMP module, enables the clock, and sets the interrupt switcher. The CMP module is configured as a basic comparator.
- Parameters
-
instance | CMP instance ID. |
userStatePtr | Pointer to structure of context. See "cmp_state_t". |
userConfigPtr | Pointer to structure of configuration. See "cmp_user_config_t". |
- Returns
- Execution status.
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
-
- 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
-
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
-
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
-
instance | CMP instance ID. |
dacConfigPtr | Pointer to structure of configuration. See "cmp_dac_config_t". |
- Returns
- Execution status.
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
-
instance | CMP instance ID. |
configPtr | Pointer 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
-
- 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
-
instance | CMP instance ID. |
flag | Represent 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
-
instance | CMP instance ID. |
flag | Represent events or states. See "cmp_flag_t". |
CMP_Type* const g_cmpBase[] |
const IRQn_Type g_cmpIrqId[CMP_INSTANCE_COUNT] |