1 /*
2 * Copyright 2017-2020 NXP
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7 #ifndef FSL_ACOMP_H__
8 #define FSL_ACOMP_H__
9
10 #include "fsl_common.h"
11 /*! @addtogroup lpc_acomp */
12 /*! @{*/
13
14 /*! @file */
15
16 /*******************************************************************************
17 * Definitions
18 ******************************************************************************/
19 /*! @name Driver version */
20 /*! @{ */
21 /*! @brief ACOMP driver version 2.1.0. */
22 #define FSL_ACOMP_DRIVER_VERSION (MAKE_VERSION(2, 1, 0))
23 /*! @} */
24
25 /*!
26 * @brief The ACOMP ladder reference voltage.
27 */
28 typedef enum _acomp_ladder_reference_voltage
29 {
30 kACOMP_LadderRefVoltagePinVDD = 0U, /*!< Supply from pin VDD. */
31 kACOMP_LadderRefVoltagePinVDDCMP = 1U, /*!< Supply from pin VDDCMP. */
32 } acomp_ladder_reference_voltage_t;
33
34 /*!
35 * @brief The ACOMP interrupts enable.
36 */
37 typedef enum _acomp_interrupt_enable
38 {
39 kACOMP_InterruptsFallingEdgeEnable = 0U, /*!< Enable the falling edge interrupts. */
40 kACOMP_InterruptsRisingEdgeEnable = 1U, /*!< Enable the rising edge interrupts. */
41 kACOMP_InterruptsBothEdgesEnable = 2U, /*!< Enable the both edges interrupts. */
42 #if defined(FSL_FEATURE_ACOMP_HAS_CTRL_INTENA) && FSL_FEATURE_ACOMP_HAS_CTRL_INTENA
43 kACOMP_InterruptsDisable = 3U, /*!< Disable the interrupts. */
44 #endif /*FSL_FEATURE_ACOMP_HAS_CTRL_INTENA*/
45 } acomp_interrupt_enable_t;
46
47 /*!
48 * @brief The ACOMP hysteresis selection.
49 */
50 typedef enum _acomp_hysteresis_selection
51 {
52 kACOMP_HysteresisNoneSelection = 0U, /*!< None (the output will switch as the voltages cross). */
53 kACOMP_Hysteresis5MVSelection = 1U, /*!< 5mV. */
54 kACOMP_Hysteresis10MVSelection = 2U, /*!< 10mV. */
55 kACOMP_Hysteresis20MVSelection = 3U, /*!< 20mV. */
56 } acomp_hysteresis_selection_t;
57
58 /*!
59 * @brief The structure for ACOMP basic configuration.
60 */
61 typedef struct _acomp_config
62 {
63 bool enableSyncToBusClk; /*!< If true, Comparator output is synchronized to the bus clock for
64 output to other modules. If false, Comparator output is used directly. */
65 acomp_hysteresis_selection_t hysteresisSelection; /*!< Controls the hysteresis of the comparator. */
66 } acomp_config_t;
67
68 /*!
69 * @brief The structure for ACOMP voltage ladder.
70 */
71 typedef struct _acomp_ladder_config
72 {
73 uint8_t ladderValue; /*!< Voltage ladder value. 00000 = Vss, 00001 = 1*Vref/31, ..., 11111 = Vref. */
74 acomp_ladder_reference_voltage_t
75 referenceVoltage; /*!< Selects the reference voltage(Vref) for the voltage ladder. */
76 } acomp_ladder_config_t;
77 /*******************************************************************************
78 * API
79 ******************************************************************************/
80 #if defined(__cplusplus)
81 extern "C" {
82 #endif /* __cplusplus*/
83
84 /*!
85 * @name Initialization
86 * @{
87 */
88
89 /*!
90 * @brief Initialize the ACOMP module.
91 *
92 * @param base ACOMP peripheral base address.
93 * @param config Pointer to "acomp_config_t" structure.
94 */
95 void ACOMP_Init(ACOMP_Type *base, const acomp_config_t *config);
96
97 /*!
98 * @brief De-initialize the ACOMP module.
99 *
100 * @param base ACOMP peripheral base address.
101 */
102 void ACOMP_Deinit(ACOMP_Type *base);
103
104 /*!
105 * @brief Gets an available pre-defined settings for the ACOMP's configuration.
106 *
107 * This function initializes the converter configuration structure with available settings. The default values are:
108 * @code
109 * config->enableSyncToBusClk = false;
110 * config->hysteresisSelection = kACOMP_hysteresisNoneSelection;
111 * @endcode
112 * In default configuration, the ACOMP's output would be used directly and switch as the voltages cross.
113 *
114 * @param config Pointer to the configuration structure.
115 */
116 void ACOMP_GetDefaultConfig(acomp_config_t *config);
117
118 /*!
119 * @brief Enable ACOMP interrupts.
120 *
121 * @param base ACOMP peripheral base address.
122 * @param enable Enable/Disable interrupt feature.
123 */
124 void ACOMP_EnableInterrupts(ACOMP_Type *base, acomp_interrupt_enable_t enable);
125
126 /*!
127 * @brief Get interrupts status flags.
128 *
129 * @param base ACOMP peripheral base address.
130 * @return Reflect the state ACOMP edge-detect status, true or false.
131 */
ACOMP_GetInterruptsStatusFlags(ACOMP_Type * base)132 static inline bool ACOMP_GetInterruptsStatusFlags(ACOMP_Type *base)
133 {
134 return ((ACOMP_CTRL_COMPEDGE_MASK & base->CTRL) != 0UL) ? true : false;
135 }
136
137 /*!
138 * @brief Clear the ACOMP interrupts status flags.
139 *
140 * @param base ACOMP peripheral base address.
141 */
ACOMP_ClearInterruptsStatusFlags(ACOMP_Type * base)142 static inline void ACOMP_ClearInterruptsStatusFlags(ACOMP_Type *base)
143 {
144 base->CTRL |= ACOMP_CTRL_EDGECLR_MASK;
145 base->CTRL &= ~ACOMP_CTRL_EDGECLR_MASK;
146 }
147
148 /*!
149 * @brief Get ACOMP output status flags.
150 *
151 * @param base ACOMP peripheral base address.
152 * @return Reflect the state of the comparator output, true or false.
153 */
ACOMP_GetOutputStatusFlags(ACOMP_Type * base)154 static inline bool ACOMP_GetOutputStatusFlags(ACOMP_Type *base)
155 {
156 return ((ACOMP_CTRL_COMPSTAT_MASK & base->CTRL) != 0UL) ? true : false;
157 }
158
159 /*!
160 * @brief Set the ACOMP postive and negative input channel.
161 *
162 * @param base ACOMP peripheral base address.
163 * @param postiveInputChannel The index of postive input channel.
164 * @param negativeInputChannel The index of negative input channel.
165 */
ACOMP_SetInputChannel(ACOMP_Type * base,uint32_t postiveInputChannel,uint32_t negativeInputChannel)166 static inline void ACOMP_SetInputChannel(ACOMP_Type *base, uint32_t postiveInputChannel, uint32_t negativeInputChannel)
167 {
168 base->CTRL = (base->CTRL & ~(ACOMP_CTRL_COMP_VP_SEL_MASK | ACOMP_CTRL_COMP_VM_SEL_MASK)) |
169 (ACOMP_CTRL_COMP_VP_SEL(postiveInputChannel) | ACOMP_CTRL_COMP_VM_SEL(negativeInputChannel));
170 }
171
172 /*!
173 * @brief Set the voltage ladder configuration.
174 *
175 * @param base ACOMP peripheral base address.
176 * @param config The structure for voltage ladder. If the config is NULL, voltage ladder would be diasbled,
177 * otherwise the voltage ladder would be configured and enabled.
178 */
179 void ACOMP_SetLadderConfig(ACOMP_Type *base, const acomp_ladder_config_t *config);
180
181 /*! @} */
182
183 #if defined(__cplusplus)
184 }
185 #endif /* __cplusplus*/
186
187 /*! @} */
188
189 #endif /* FSL_ACOMP_H_ */
190