Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:22:58

0001 /*
0002  * Copyright (c) 2015, Freescale Semiconductor, Inc.
0003  * Copyright 2016-2019 NXP
0004  * All rights reserved.
0005  *
0006  * SPDX-License-Identifier: BSD-3-Clause
0007  */
0008 
0009 #ifndef _FSL_CMP_H_
0010 #define _FSL_CMP_H_
0011 
0012 #include "fsl_common.h"
0013 
0014 /*!
0015  * @addtogroup cmp
0016  * @{
0017  */
0018 
0019 /*******************************************************************************
0020  * Definitions
0021  ******************************************************************************/
0022 
0023 /*! @name Driver version */
0024 /*@{*/
0025 /*! @brief CMP driver version 2.0.2. */
0026 #define FSL_CMP_DRIVER_VERSION (MAKE_VERSION(2, 0, 2))
0027 /*@}*/
0028 
0029 /*!
0030  * @brief Interrupt enable/disable mask.
0031  */
0032 enum _cmp_interrupt_enable
0033 {
0034     kCMP_OutputRisingInterruptEnable  = CMP_SCR_IER_MASK, /*!< Comparator interrupt enable rising. */
0035     kCMP_OutputFallingInterruptEnable = CMP_SCR_IEF_MASK, /*!< Comparator interrupt enable falling. */
0036 };
0037 
0038 /*!
0039  * @brief Status flags' mask.
0040  */
0041 enum _cmp_status_flags
0042 {
0043     kCMP_OutputRisingEventFlag  = CMP_SCR_CFR_MASK,  /*!< Rising-edge on the comparison output has occurred. */
0044     kCMP_OutputFallingEventFlag = CMP_SCR_CFF_MASK,  /*!< Falling-edge on the comparison output has occurred. */
0045     kCMP_OutputAssertEventFlag  = CMP_SCR_COUT_MASK, /*!< Return the current value of the analog comparator output. */
0046 };
0047 
0048 /*!
0049  * @brief CMP Hysteresis mode.
0050  */
0051 typedef enum _cmp_hysteresis_mode
0052 {
0053     kCMP_HysteresisLevel0 = 0U, /*!< Hysteresis level 0. */
0054     kCMP_HysteresisLevel1 = 1U, /*!< Hysteresis level 1. */
0055     kCMP_HysteresisLevel2 = 2U, /*!< Hysteresis level 2. */
0056     kCMP_HysteresisLevel3 = 3U, /*!< Hysteresis level 3. */
0057 } cmp_hysteresis_mode_t;
0058 
0059 /*!
0060  * @brief CMP Voltage Reference source.
0061  */
0062 typedef enum _cmp_reference_voltage_source
0063 {
0064     kCMP_VrefSourceVin1 = 0U, /*!< Vin1 is selected as a resistor ladder network supply reference Vin. */
0065     kCMP_VrefSourceVin2 = 1U, /*!< Vin2 is selected as a resistor ladder network supply reference Vin. */
0066 } cmp_reference_voltage_source_t;
0067 
0068 /*!
0069  * @brief Configures the comparator.
0070  */
0071 typedef struct _cmp_config
0072 {
0073     bool enableCmp;                       /*!< Enable the CMP module. */
0074     cmp_hysteresis_mode_t hysteresisMode; /*!< CMP Hysteresis mode. */
0075     bool enableHighSpeed;                 /*!< Enable High-speed (HS) comparison mode. */
0076     bool enableInvertOutput;              /*!< Enable the inverted comparator output. */
0077     bool useUnfilteredOutput;             /*!< Set the compare output(COUT) to equal COUTA(true) or COUT(false). */
0078     bool enablePinOut;                    /*!< The comparator output is available on the associated pin. */
0079 #if defined(FSL_FEATURE_CMP_HAS_TRIGGER_MODE) && FSL_FEATURE_CMP_HAS_TRIGGER_MODE
0080     bool enableTriggerMode; /*!< Enable the trigger mode. */
0081 #endif                      /* FSL_FEATURE_CMP_HAS_TRIGGER_MODE */
0082 } cmp_config_t;
0083 
0084 /*!
0085  * @brief Configures the filter.
0086  */
0087 typedef struct _cmp_filter_config
0088 {
0089 #if defined(FSL_FEATURE_CMP_HAS_EXTERNAL_SAMPLE_SUPPORT) && FSL_FEATURE_CMP_HAS_EXTERNAL_SAMPLE_SUPPORT
0090     bool enableSample;    /*!< Using the external SAMPLE as a sampling clock input or using a divided bus clock. */
0091 #endif                    /* FSL_FEATURE_CMP_HAS_EXTERNAL_SAMPLE_SUPPORT */
0092     uint8_t filterCount;  /*!< Filter Sample Count. Available range is 1-7; 0 disables the filter.*/
0093     uint8_t filterPeriod; /*!< Filter Sample Period. The divider to the bus clock. Available range is 0-255. */
0094 } cmp_filter_config_t;
0095 
0096 /*!
0097  * @brief Configures the internal DAC.
0098  */
0099 typedef struct _cmp_dac_config
0100 {
0101     cmp_reference_voltage_source_t referenceVoltageSource; /*!< Supply voltage reference source. */
0102     uint8_t DACValue; /*!< Value for the DAC Output Voltage. Available range is 0-63.*/
0103 } cmp_dac_config_t;
0104 
0105 #if defined(__cplusplus)
0106 extern "C" {
0107 #endif
0108 
0109 /*******************************************************************************
0110  * API
0111  ******************************************************************************/
0112 
0113 /*!
0114  * @name Initialization
0115  * @{
0116  */
0117 
0118 /*!
0119  * @brief Initializes the CMP.
0120  *
0121  * This function initializes the CMP module. The operations included are as follows.
0122  * - Enabling the clock for CMP module.
0123  * - Configuring the comparator.
0124  * - Enabling the CMP module.
0125  * Note that for some devices, multiple CMP instances share the same clock gate. In this case, to enable the clock for
0126  * any instance enables all CMPs. See the appropriate MCU reference manual for the clock assignment of the CMP.
0127  *
0128  * @param base   CMP peripheral base address.
0129  * @param config Pointer to the configuration structure.
0130  */
0131 void CMP_Init(CMP_Type *base, const cmp_config_t *config);
0132 
0133 /*!
0134  * @brief De-initializes the CMP module.
0135  *
0136  * This function de-initializes the CMP module. The operations included are as follows.
0137  * - Disabling the CMP module.
0138  * - Disabling the clock for CMP module.
0139  *
0140  * This function disables the clock for the CMP.
0141  * Note that for some devices, multiple CMP instances share the same clock gate. In this case, before disabling the
0142  * clock for the CMP, ensure that all the CMP instances are not used.
0143  *
0144  * @param base CMP peripheral base address.
0145  */
0146 void CMP_Deinit(CMP_Type *base);
0147 
0148 /*!
0149  * @brief Enables/disables the CMP module.
0150  *
0151  * @param base CMP peripheral base address.
0152  * @param enable Enables or disables the module.
0153  */
0154 static inline void CMP_Enable(CMP_Type *base, bool enable)
0155 {
0156     if (enable)
0157     {
0158         base->CR1 |= CMP_CR1_EN_MASK;
0159     }
0160     else
0161     {
0162         base->CR1 &= ~(uint8_t)CMP_CR1_EN_MASK;
0163     }
0164 }
0165 
0166 /*!
0167  * @brief Initializes the CMP user configuration structure.
0168  *
0169  * This function initializes the user configuration structure to these default values.
0170  * @code
0171  *   config->enableCmp           = true;
0172  *   config->hysteresisMode      = kCMP_HysteresisLevel0;
0173  *   config->enableHighSpeed     = false;
0174  *   config->enableInvertOutput  = false;
0175  *   config->useUnfilteredOutput = false;
0176  *   config->enablePinOut        = false;
0177  *   config->enableTriggerMode   = false;
0178  * @endcode
0179  * @param config Pointer to the configuration structure.
0180  */
0181 void CMP_GetDefaultConfig(cmp_config_t *config);
0182 
0183 /*!
0184  * @brief  Sets the input channels for the comparator.
0185  *
0186  * This function sets the input channels for the comparator.
0187  * Note that two input channels cannot be set the same way in the application. When the user selects the same input
0188  * from the analog mux to the positive and negative port, the comparator is disabled automatically.
0189  *
0190  * @param  base            CMP peripheral base address.
0191  * @param  positiveChannel Positive side input channel number. Available range is 0-7.
0192  * @param  negativeChannel Negative side input channel number. Available range is 0-7.
0193  */
0194 void CMP_SetInputChannels(CMP_Type *base, uint8_t positiveChannel, uint8_t negativeChannel);
0195 
0196 /* @} */
0197 
0198 /*!
0199  * @name Advanced Features
0200  * @{
0201  */
0202 
0203 #if defined(FSL_FEATURE_CMP_HAS_DMA) && FSL_FEATURE_CMP_HAS_DMA
0204 /*!
0205  * @brief Enables/disables the DMA request for rising/falling events.
0206  *
0207  * This function enables/disables the DMA request for rising/falling events. Either event triggers the generation of
0208  * the DMA request from CMP if the DMA feature is enabled. Both events are ignored for generating the DMA request from
0209  * the CMP
0210  * if the DMA is disabled.
0211  *
0212  * @param base CMP peripheral base address.
0213  * @param enable Enables or disables the feature.
0214  */
0215 void CMP_EnableDMA(CMP_Type *base, bool enable);
0216 #endif /* FSL_FEATURE_CMP_HAS_DMA */
0217 
0218 #if defined(FSL_FEATURE_CMP_HAS_WINDOW_MODE) && FSL_FEATURE_CMP_HAS_WINDOW_MODE
0219 /*!
0220  * @brief Enables/disables the window mode.
0221  *
0222  * @param base CMP peripheral base address.
0223  * @param enable Enables or disables the feature.
0224  */
0225 static inline void CMP_EnableWindowMode(CMP_Type *base, bool enable)
0226 {
0227     if (enable)
0228     {
0229         base->CR1 |= CMP_CR1_WE_MASK;
0230     }
0231     else
0232     {
0233         base->CR1 &= (uint8_t)(~CMP_CR1_WE_MASK);
0234     }
0235 }
0236 #endif /* FSL_FEATURE_CMP_HAS_WINDOW_MODE */
0237 
0238 #if defined(FSL_FEATURE_CMP_HAS_PASS_THROUGH_MODE) && FSL_FEATURE_CMP_HAS_PASS_THROUGH_MODE
0239 /*!
0240  * @brief Enables/disables the pass through mode.
0241  *
0242  * @param base CMP peripheral base address.
0243  * @param enable Enables or disables the feature.
0244  */
0245 static inline void CMP_EnablePassThroughMode(CMP_Type *base, bool enable)
0246 {
0247     if (enable)
0248     {
0249         base->MUXCR |= CMP_MUXCR_PSTM_MASK;
0250     }
0251     else
0252     {
0253         base->MUXCR &= (uint8_t)(~CMP_MUXCR_PSTM_MASK);
0254     }
0255 }
0256 #endif /* FSL_FEATURE_CMP_HAS_PASS_THROUGH_MODE */
0257 
0258 /*!
0259  * @brief  Configures the filter.
0260  *
0261  * @param  base   CMP peripheral base address.
0262  * @param  config Pointer to the configuration structure.
0263  */
0264 void CMP_SetFilterConfig(CMP_Type *base, const cmp_filter_config_t *config);
0265 
0266 /*!
0267  * @brief Configures the internal DAC.
0268  *
0269  * @param base   CMP peripheral base address.
0270  * @param config Pointer to the configuration structure. "NULL" disables the feature.
0271  */
0272 void CMP_SetDACConfig(CMP_Type *base, const cmp_dac_config_t *config);
0273 
0274 /*!
0275  * @brief Enables the interrupts.
0276  *
0277  * @param base    CMP peripheral base address.
0278  * @param mask    Mask value for interrupts. See "_cmp_interrupt_enable".
0279  */
0280 void CMP_EnableInterrupts(CMP_Type *base, uint32_t mask);
0281 
0282 /*!
0283  * @brief Disables the interrupts.
0284  *
0285  * @param base    CMP peripheral base address.
0286  * @param mask    Mask value for interrupts. See "_cmp_interrupt_enable".
0287  */
0288 void CMP_DisableInterrupts(CMP_Type *base, uint32_t mask);
0289 
0290 /* @} */
0291 
0292 /*!
0293  * @name Results
0294  * @{
0295  */
0296 
0297 /*!
0298  * @brief  Gets the status flags.
0299  *
0300  * @param  base     CMP peripheral base address.
0301  *
0302  * @return          Mask value for the asserted flags. See "_cmp_status_flags".
0303  */
0304 uint32_t CMP_GetStatusFlags(CMP_Type *base);
0305 
0306 /*!
0307  * @brief Clears the status flags.
0308  *
0309  * @param base     CMP peripheral base address.
0310  * @param mask     Mask value for the flags. See "_cmp_status_flags".
0311  */
0312 void CMP_ClearStatusFlags(CMP_Type *base, uint32_t mask);
0313 
0314 /* @} */
0315 #if defined(__cplusplus)
0316 }
0317 #endif
0318 /*!
0319  * @}
0320  */
0321 #endif /* _FSL_CMP_H_ */