Peripheral features and how this peripheral works
Peripheral features
The comparator (CMP) module provides below features:
- Support two 8-to-1 input channel
- Inputs may range from rail to rail(the input voltage amplitude can reach the upper and lower limits of the power supply voltage)
- A 8 bit CMP internal DAC output can be used as comparator's input. Selectable supply voltage reference source from VN1 or VIN2.
- Programmable input hysteresis level of the comparator Support 0 - 3 hysteresis level
- Programmable performance levels of the comparator
- Shorter propagation delay at the expense of higher power
- Low power, with longer propagation delay
- Selectable inversion on comparator output
- Support below work modes
- The window function is enabled and external sampling mode is disabled Window function will control the comparator's output by a external WINDOW signal and the bus clocks.
- The comparator output will be sampled on every bus clock when WINDOW = 1
- The sampling doesn't occur when WINDOW = 0
- The output of window function can be last latched value when WINDOW signal changes from 1 to 0
- The output of window function can be forced to zero when WINDOW signal changes from 1 to 0
- Note
- The WINDOW signal should be routed to a source(such as PWM, PIT, etc) by Inter-Peripheral Crossbar
- The window function is disabled and external sampling mode is enabled
- The window function have no functionality in this mode
- A external sample signal will be used to re-sample the comparator's output.
- Note
- The external SAMPLE signal should be routed to a source(such as PWM, PIT, etc) by Inter-Peripheral Crossbar
- The window function is disabled and external sampling mode is disabled
- Both the window function and external sampling signal has no functionality
- Note
- the window function and external sampling mode are mutually exclusive, since the WINDOW signal and external SAMPLE signal are comes from the same source.
- Support a internal filter The internal filter function can be used to take several counts(count number is programmable) of samples from comparator's output in every filter period(period is programmable).
- The internal filter will be enabled if the filter count > 0 and filter period is a nonzero value, otherwise the filter will be disabled.
- The internal filter can be enabled together with above work modes which external sampling mode is disabled, since internal filter and the external sampling mode are mutually exclusive.
- Selectable interrupt/dma request on rising-edge, falling-edge, or both edges of the comparator output
- Note
- When the DMA and the interrupt are both enabled, the corresponding change on COUT forces a DMA transfer request rather than a CPU interrupt instead
- The CMP module output can be routed to other internal peripherals and a CMP output PAD.
- The CMP output to the PAD is selectable within below sources.
- COUTA
- COUTA can comes from the comparator output directly when the window function is disabled.
- COUTA can comes from the window function output.when the window function is enabled
- COUT
- COUT is COUTA when internal filter is bypassed and external sampling mode disabled.
- COUT can be a output of internal filter which source from COUTA.
- COUT can be a result of COUTA re-sampled by a external sample signal.
- The COUT can be routed to other internal peripherals.
How this peripheral works
Once the CMP is configured and enabled, the comparator will compare two analog input voltages applied to INP and INM. CMPO is high when the non-inverting input is greater than the inverting input, and is low when the non-inverting input is less than the inverting input.
The output of the comparator will be controlled by the window function or external sampling signal or internal filter per different configurations.
If there is a change on the COUT,
- The CMP will generate a interrupt if the interrupt is enabled
- The CMP will generate a DMA transfer request if the DMA is enabled
How this driver is designed to make this peripheral works
The CMP driver designed several configuration structures and function groups to help user on enable the module .
- CMP driver designed a configuration structure cmp_config_t which cover full configurations of the module. User need to pay attention to the below items,
- The driver designed three work modes for user selection by configure the member eWorkMode in the structure.
- Since the interrupt and dma request are mutually exclusive, so the configuration structure provide a union(cmp_dma_interrupt_config_t) member for interrupt and dma request configuration which is named uDmaInterruptConfig.
- CMP driver provide a CMP Initialization and de-initialization interfaces
- User can have all the CMP configuration be configured through this interfaces with a cmp configuration provided by cmp_config_t..
- The interfaces provide a helper function for the default value of the cmp_config_t.
- It is assumed that the most of the configurations of the CMP is constant during runtime.
- CMP driver provided a functional interface which is target for runtime configurations update, such as the input mux, internal DAC configurations, etc.
- The CMP interrupt interfaces The interface used to enable/disable the interrupt request.
- The CMP DMA interfaces The interfaces used to enable/disable DMA transfer request.
- The CMP status interfaces The interfaces used to get/clear the the comparator status flag.
How to use this driver
- Configuration option on compiling
- FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL: Defined of this macro means this driver will not handle CMP peripheral clock. Application code need to handle the CMP clock by itself
- User must be aware of below configurations should be done before driver interfaces been invoked.
- The selected input mux pins must be configured properly before start the comparison.
- [Optional]The CMP request source must be configured properly in the Direct Memory Access Multiplexer(DMAMUX) if application is going to using DMA for reading CMP result
- [Optional]The internal DAC voltage reference source should be configured properly if it is selected as the input of CMP
- [Optional]The external DAC should be configured properly if it is selected as the input of CMP.
- [Optional]The external WINDOW signal should be configured properly if the window is enabled
- [Optional]The external SAMPLE signal should be configured properly if the external sampling mode is using.
- Driver Interface invoking sequence
- Initialize the CMP module
- Using the cmp_config_t for CMP initialization configuration preparation
- Application can call CMP_GetDefaultConfig to get the default configurations
- Application must overwrite the default input plus/minus mux configurations(ePlusInput/eMinusInput in cmp_config_t) according to the pins selected by application.
- [Optional]If CMP internal DAC is used as input of CMP, please make sure internal DAC configurations is done by configure member sDacConfig in cmp_config_t.
- User should pay attention to the three work modes provide by CMP driver and make sure proper work mode is selected by configure the eWorkMode in cmp_config_t.
- kCMP_WorkModeWindowBypassAndNoExternalSample
- The internal filter function can be enabled together with this mode selected, then application should configure the filter count and period by member eInternalFilterCount and u8InteralFilterPeriod in cmp_config_t.
- The members eExternalSampleCount and eWindowOutput is ignored when this mode is selected.
- kCMP_WorkModeWindowBypassAndExternalSample
- The sample count should be configured properly by the member eExternalSampleCount(>=1) in cmp_config_t,
- The members eInternalFilterCount/u8InteralFilterPeriod/eWindowOutput are ignored when this mode is selected.
- kCMP_WorkModeWindowEnabledAndNoExternalSample
- The window output mode should be configured properly by eWindowOutputMode in cmp_config_t
- The internal filter function can be enabled together with this mode selected, then application should configure the filter count and period by member eInternalFilterCount and u8InteralFilterPeriod in cmp_config_t.
- The member eExternalSampleCount is ignored when this mode is selected.
- [Optional]Enable DMA or interrupt The dma/interrupt request can be configured by uDmaInterruptConfig cmp_config_t.
- Call CMP_Init for the CMP module initialization
- [Optional]Enable the CMP module Once the CMP configuration is done, application can using CMP_Enable to start the comparison anytime if the CMP is not enabled in CMP_Init by setting bEnableCMP in the cmp_config_t to true.
- [Optional]Runtime configurations update Once the CMP configuration done, application can using the CMP functional interfaces for the configuration update runtime, such as the comparator input mux, internal DAC configurations, etc.
- Get comparator output Application can get the comparator result by calling api CMP_GetComparatorOutput.
- De-initialization While the CMP module is in idle state, application should call CMP_Deinit to disable the CMP module and gate the CMP clock(if application handing the CMP clock) for lower power consumption
Typical Use Cases
- Work mode kCMP_WorkModeWindowEnabledAndNoExternalSample, the window function is enabled and external sampling mode is disabled
* config->ePlusInput = xxx
* config->eMinusInput = xxx
*
- Work mode kCMP_WorkModeWindowEnabledAndNoExternalSample, the window function is enabled and external sampling mode is disabled, internal filter is enabled
* config->ePlusInput = xxx
* config->eMinusInput = xxx
* config->u8FilterPeriod = xxx
*
- Work mode kCMP_WorkModeWindowBypassAndExternalSample, the window function is bypassed and external sampling mode is enabled.
* config->ePlusInput = xxx
* config->eMinusInput = xxx
*
*
- Work mode kCMP_WorkModeWindowBypassAndNoExternalSample, the window function is disabled and external sampling mode is disabled bus clock.
* config->ePlusInput = xxx
* config->eMinusInput = xxx
*
* config->bCMPEnable = true
*
*
- Work mode kCMP_WorkModeWindowBypassAndNoExternalSample, the window function is disabled and external sampling mode is disabled, internal filter is enabled
* config->ePlusInput = xxx
* config->eMinusInput = xxx
* config->u8FilterPeriod = xxx
*
* config->bCMPEnable = true
*
*