1 /*
2  * Copyright 2018-2020 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include "fsl_cmp.h"
9 
10 /* Component ID definition, used by tools. */
11 #ifndef FSL_COMPONENT_ID
12 #define FSL_COMPONENT_ID "platform.drivers.cmp_1"
13 #endif
14 
15 /*******************************************************************************
16  * Definitions
17  ******************************************************************************/
18 
19 /*******************************************************************************
20  * Prototypes
21  ******************************************************************************/
22 
23 /*******************************************************************************
24  * Code
25  ******************************************************************************/
26 
27 /*!
28  * @brief CMP initialization.
29  *
30  * This function enables the CMP module and do necessary settings.
31  *
32  * @param config Pointer to the configuration structure.
33  */
CMP_Init(const cmp_config_t * config)34 void CMP_Init(const cmp_config_t *config)
35 {
36     assert(NULL != config);
37 
38     uint32_t tmpReg = 0U;
39 
40 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
41     /* Enable the clock. */
42     CLOCK_EnableClock(kCLOCK_Comp);
43 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
44 
45 #if !(defined(FSL_FEATURE_CMP_HAS_NO_RESET) && FSL_TEATURE_CMP_HAS_NO_RESET)
46     /* Reset the CMP module. */
47     RESET_PeripheralReset(kCMP_RST_SHIFT_RSTn);
48 #endif /* FSL_FEATURE_CMP_HAS_NO_RESET */
49 
50     tmpReg = (PMC->COMP & ~(PMC_COMP_LOWPOWER_MASK | PMC_COMP_HYST_MASK | PMC_COMP_FILTERCGF_CLKDIV_MASK |
51                             PMC_COMP_FILTERCGF_SAMPLEMODE_MASK));
52 
53     if (true == config->enableLowPower)
54     {
55         tmpReg |= PMC_COMP_LOWPOWER_MASK;
56     }
57     else
58     {
59         tmpReg &= ~PMC_COMP_LOWPOWER_MASK;
60     }
61 
62     if (true == config->enableHysteresis)
63     {
64         tmpReg |= PMC_COMP_HYST_MASK;
65     }
66     else
67     {
68         tmpReg &= ~PMC_COMP_HYST_MASK;
69     }
70 
71     tmpReg |= (PMC_COMP_FILTERCGF_CLKDIV(config->filterClockDivider) |
72                PMC_COMP_FILTERCGF_SAMPLEMODE(config->filterSampleMode));
73 
74     PMC->COMP = tmpReg;
75 }
76 
77 /*!
78  * @brief CMP deinitialization.
79  *
80  * This function gates the clock for CMP module.
81  */
CMP_Deinit(void)82 void CMP_Deinit(void)
83 {
84 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
85     /* Disable the clock. */
86     CLOCK_DisableClock(kCLOCK_Comp);
87 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
88 }
89 
90 /*!
91  * @brief Initializes the CMP user configuration structure.
92  *
93  * This function initializes the user configuration structure to these default values.
94  * @code
95  *   config->enableHysteresis    = true;
96  *   config->enableLowPower      = true;
97  *   config->filterClockDivider  = kCMP_FilterClockDivide1;
98  *   config->filterSampleMode    = kCMP_FilterSampleMode0;
99  * @endcode
100  * @param config Pointer to the configuration structure.
101  */
CMP_GetDefaultConfig(cmp_config_t * config)102 void CMP_GetDefaultConfig(cmp_config_t *config)
103 {
104     /* Initializes the configure structure to zero. */
105     (void)memset(config, 0, sizeof(*config));
106 
107     config->enableHysteresis   = true;
108     config->enableLowPower     = true;
109     config->filterClockDivider = kCMP_FilterClockDivide1;
110     config->filterSampleMode   = kCMP_FilterSampleMode0;
111 }
112 
113 /*!
114  * @brief Configures the VREFINPUT.
115  *
116  * @param config Pointer to the configuration structure.
117  */
CMP_SetVREF(const cmp_vref_config_t * config)118 void CMP_SetVREF(const cmp_vref_config_t *config)
119 {
120     assert(NULL != config);
121     assert(config->vrefValue < 32U);
122 
123     uint32_t tmpReg = PMC->COMP & ~(PMC_COMP_VREF_MASK | PMC_COMP_VREFINPUT_MASK);
124 
125     tmpReg |= PMC_COMP_VREFINPUT(config->vrefSource) | PMC_COMP_VREF(config->vrefValue);
126 
127     PMC->COMP = tmpReg;
128 }
129