1 /*
2  * Copyright 2021-2022, 2024 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 #ifndef FSL_OPAMP_H_
8 #define FSL_OPAMP_H_
9 
10 #include "fsl_common.h"
11 
12 /*!
13  * @addtogroup opamp
14  * @{
15  */
16 
17 /*******************************************************************************
18  * Definitions
19  *******************************************************************************/
20 
21 /*! @name Driver version */
22 /*! @{ */
23 /*! @brief OPAMP driver version. */
24 #define FSL_OPAMP_DRIVER_VERSION (MAKE_VERSION(2, 2, 0))
25 /*! @} */
26 
27 /*!
28  * @brief The enumeration of OPAMP mode, including low noise mode and high speed mode.
29  */
30 typedef enum _opamp_mode
31 {
32     kOPAMP_LowNoiseMode = 0U, /*!< Set opamp mode as low noise mode. */
33     kOPAMP_HighSpeedMode,     /*!< Set opamp mode as high speed mode. */
34 } opamp_mode_t;
35 
36 /*!
37  * @brief The enumeration of bias current trim option.
38  */
39 typedef enum _opamp_bias_current_trim_option
40 {
41     kOPAMP_TrimOptionDefault = 0U,           /*!< Default Bias current trim option. */
42     kOPAMP_TrimOptionIncreaseCurrent,        /*!< Trim option selected as increase current. */
43     kOPAMP_TrimOptionDecreaseCurrent,        /*!< Trim option selected as decrease current. */
44     kOPAMP_TrimOptionFurtherDecreaseCurrent, /*!< Trim option selected as further decrease current. */
45 } opamp_bias_current_trim_option_t;
46 
47 /*!
48  * @brief The enumeration of internal reference voltage.
49  */
50 typedef enum _opamp_internal_ref_voltage
51 {
52     kOPAMP_IntRefVoltVddaDiv2 = 0U, /*!< Internal reference voltage selected as Vdda/2. */
53     kOPAMP_IntRefVoltVdda3V,        /*!< Internal reference voltage selected as Vdda_3V. */
54     kOPAMP_IntRefVoltVssa3V,        /*!< Internal reference voltage selected as Vssa_3V. */
55     kOPAMP_IntRefVoltNotAllowed,    /*!< Internal reference voltage not allowed. */
56 } opamp_internal_ref_voltage_t;
57 
58 /*!
59  * @brief The enumeration of positive reference voltage(please refer to manual use).
60  */
61 typedef enum _opamp_positive_ref_voltage
62 {
63     kOPAMP_PosRefVoltVrefh3 = 0U, /*!< Positive part reference voltage select Vrefh3, connected from DAC output. */
64     kOPAMP_PosRefVoltVrefh0 = 1U, /*!< Positive part reference voltage select Vrefh0, connected from VDDA supply. */
65     kOPAMP_PosRefVoltVrefh1 = 2U, /*!< Positive part reference voltage select Vrefh1,
66                                         connected from Voltage reference output. */
67     kOPAMP_PosRefVoltVrefh4 = 3U, /*!< Positive part reference voltage select 520mv or reserved.*/
68 } opamp_positive_ref_voltage_t;
69 
70 /*!
71  * @brief The enumeration of positive programmable gain (please refer to manual use).
72  */
73 typedef enum _opamp_positive_gain
74 {
75     kOPAMP_PosGainReserved     = 0U, /*!< Positive Gain reserved. */
76     kOPAMP_PosGainNonInvert1X  = 1U, /*!< Positive non-inverting gain application 1X. */
77     kOPAMP_PosGainNonInvert2X  = 2U, /*!< Positive non-inverting gain application 2X. */
78     kOPAMP_PosGainNonInvert4X  = 3U, /*!< Positive non-inverting gain application 4X. */
79     kOPAMP_PosGainNonInvert8X  = 4U, /*!< Positive non-inverting gain application 8X. */
80     kOPAMP_PosGainNonInvert16X = 5U, /*!< Positive non-inverting gain application 16X. */
81     kOPAMP_PosGainNonInvert33X = 6U, /*!< Positive non-inverting gain application 33X. */
82     kOPAMP_PosGainNonInvert64X = 7U, /*!< Positive non-inverting gain application 64X. */
83 #if defined(FSL_FEATURE_OPAMP_HAS_SUPPORT_REFERENCE_BUFFER) && FSL_FEATURE_OPAMP_HAS_SUPPORT_REFERENCE_BUFFER
84     /* The following gain selections are for basic OPAMP only, and signal inputs are
85     supported on the non-inverting and inverting terminals.*/
86     kOPAMP_PosGainNonInvertDisableBuffer2X  = 8U,  /*!< Positive non-inverting gain application 2X. */
87     kOPAMP_PosGainNonInvertDisableBuffer3X  = 9U,  /*!< Positive non-inverting gain application 3X. */
88     kOPAMP_PosGainNonInvertDisableBuffer5X  = 10U, /*!< Positive non-inverting gain application 5X. */
89     kOPAMP_PosGainNonInvertDisableBuffer9X  = 11U, /*!< Positive non-inverting gain application 9X. */
90     kOPAMP_PosGainNonInvertDisableBuffer17X = 12U, /*!< Positive non-inverting gain application 17X. */
91     kOPAMP_PosGainNonInvertDisableBuffer34X = 13U, /*!< Positive non-inverting gain application 34X. */
92     kOPAMP_PosGainNonInvertDisableBuffer65X = 14U, /*!< Positive non-inverting gain application 65X. */
93 #endif                                             /* FSL_FEATURE_OPAMP_HAS_SUPPORT_REFERENCE_BUFFER */
94 } opamp_positive_gain_t;
95 
96 /*!
97  * @brief The enumeration of negative programmable gain.
98  */
99 typedef enum _opamp_negative_gain
100 {
101     kOPAMP_NegGainBufferMode = 0U, /*!< Negative Buffer Mode. */
102     kOPAMP_NegGainInvert1X,        /*!< Negative inverting gain application -1X. */
103     kOPAMP_NegGainInvert2X,        /*!< Negative inverting gain application -2X. */
104     kOPAMP_NegGainInvert4X,        /*!< Negative inverting gain application -4X. */
105     kOPAMP_NegGainInvert8X,        /*!< Negative inverting gain application -8X. */
106     kOPAMP_NegGainInvert16X,       /*!< Negative inverting gain application -16X. */
107     kOPAMP_NegGainInvert33X,       /*!< Negative inverting gain application -33X. */
108     kOPAMP_NegGainInvert64X,       /*!< Negative inverting gain application -64X. */
109 } opamp_negative_gain_t;
110 
111 #if defined(FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_INPSEL) && FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_INPSEL
112 /*!
113  * @brief The enumeration of positive input channel selection.
114  */
115 typedef enum _opamp_positive_input_channel_selection
116 {
117     kOPAMP_PosInputChannel0 = 0U, /*!< When OPAMP not in trigger mode, select positive input 0 (INP0).*/
118     kOPAMP_PosInputChannel1 = 1U, /*!< When OPAMP not in trigger mode, select positive input 1 (INP1).*/
119 } opamp_positive_input_channel_selection_t;
120 #endif
121 /*!
122  * @brief OPAMP configuraion, including mode, internal reference voltage, positive gain, negative gain and so on.
123  */
124 typedef struct _opamp_config
125 {
126     bool enable;       /*!< Enable/disable OPAMP.  */
127     opamp_mode_t mode; /*!< Opamp mode, available values are @ref kOPAMP_LowNoiseMode and @ref kOPAMP_HighSpeedMode. */
128     opamp_bias_current_trim_option_t trimOption; /*!< Bias current trim option, please refer to
129                                                       @ref opamp_bias_current_trim_option_t.  */
130     opamp_internal_ref_voltage_t intRefVoltage;  /*!< Internal reference voltage, please refer to
131                                                      @ref opamp_internal_ref_voltage_t. */
132     opamp_positive_ref_voltage_t posRefVoltage;  /*!< Positive part reference voltage, please refer
133                                                      to @ref opamp_positive_ref_voltage_t. */
134     /* Positive part configuration. */
135     opamp_positive_gain_t posGain; /*!< Positive part programmable gain, please refer
136                                        to @ref opamp_positive_gain_t. */
137     /* Negative part configuration. */
138     opamp_negative_gain_t negGain; /*!< Negative part programmable gain, please refer
139                                        to @ref opamp_negative_gain_t. */
140 #if defined(FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_OUTSW) && FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_OUTSW
141     bool enableOutputSwitch;       /*!< OPAMP out to negative gain resistor ladder switch.*/
142 #endif                             /* FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_OUTSW */
143 #if defined(FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_ADCSW1) && FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_ADCSW1
144     bool enablePosADCSw1;          /*!< Positive part reference voltage switch to ADC channel or not.
145                                       - \b true Positive part reference voltage switch to ADC channel.
146                                       - \b false Positive part reference voltage do not switch to ADC channel. */
147 #else
148     bool enablePosADCSw; /*!< Positive part reference voltage switch to ADC channel or not.
149                              - \b true Positive part reference voltage switch to ADC channel.
150                              - \b false Positive part reference voltage do not switch to ADC channel. */
151 #endif                             /* FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_ADCSW1 */
152 #if defined(FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_ADCSW2) && FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_ADCSW2
153     bool enablePosADCSw2;          /*!< Positive part reference voltage switch to ADC channel or not.
154                                       - \b true Positive part reference voltage switch to ADC channel.
155                                       - \b false Positive part reference voltage do not switch to ADC channel. */
156 #endif                             /* FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_ADCSW2 */
157 #if defined(FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_BUFEN) && FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_BUFEN
158     bool enableRefBuffer;          /*!< Reference buffer enable.*/
159 #endif                             /* FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_BUFEN */
160 #if defined(FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_INPSEL) && FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_INPSEL
161     opamp_positive_input_channel_selection_t PosInputChannelSelection; /*!< Positive Input Channel Selection*/
162 #endif                                                                 /* FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_INPSEL */
163 #if defined(FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_TRIGMD) && FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_TRIGMD
164     bool enableTriggerMode;                                            /*!< Trigger Mode Enable.*/
165 #endif                                                                 /* FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_TRIGMD */
166 } opamp_config_t;
167 
168 /*******************************************************************************
169  * API
170  *******************************************************************************/
171 
172 #if defined(__cplusplus)
173 extern "C" {
174 #endif
175 
176 /*!
177  * @name Initialization and deinitialization
178  * @{
179  */
180 
181 /*!
182  * @brief Initialize OPAMP instance.
183  *
184  * @param base OPAMP peripheral base address.
185  * @param config The pointer to @ref opamp_config_t.
186  */
187 void OPAMP_Init(OPAMP_Type *base, const opamp_config_t *config);
188 
189 /*!
190  * @brief De-initialize OPAMP instance.
191  *
192  * @param base OPAMP peripheral base address.
193  */
194 void OPAMP_Deinit(OPAMP_Type *base);
195 
196 /*!
197  * @brief Get default configuration of OPAMP.
198  *
199  * @code
200  *     config->enable        = false;
201  *     config->mode          = kOPAMP_LowNoiseMode;
202  *     config->trimOption    = kOPAMP_TrimOptionDefault;
203  *     config->intRefVoltage = kOPAMP_IntRefVoltVddaDiv2;
204  *     config->enablePosADCSw = false;
205  *     config->posRefVoltage = kOPAMP_PosRefVoltVrefh3;
206  *     config->posGain       = kOPAMP_PosGainReserved;
207  *     config->negGain       = kOPAMP_NegGainBufferMode;
208  * @endcode
209  *
210  * @param config The pointer to @ref opamp_config_t.
211  */
212 void OPAMP_GetDefaultConfig(opamp_config_t *config);
213 
214 /*! @} */
215 
216 /*!
217  * @name Positive port gain and negative gain configuration.
218  * @{
219  */
220 /*!
221  * @brief Configure OPAMP positive port gain.
222  *
223  * @param base OPAMP peripheral base address.
224  * @param option OPAMP positive port gain.
225  */
OPAMP_DoPosGainConfig(OPAMP_Type * base,opamp_positive_gain_t option)226 static inline void OPAMP_DoPosGainConfig(OPAMP_Type *base, opamp_positive_gain_t option)
227 {
228     base->OPAMP_CTR = (((base->OPAMP_CTR) & (~OPAMP_OPAMP_CTR_PGAIN_MASK)) | OPAMP_OPAMP_CTR_PGAIN(option));
229 }
230 
231 /*!
232  * @brief Configure OPAMP negative port gain.
233  *
234  * @param base OPAMP peripheral base address.
235  * @param option OPAMP negative port gain.
236  */
OPAMP_DoNegGainConfig(OPAMP_Type * base,opamp_negative_gain_t option)237 static inline void OPAMP_DoNegGainConfig(OPAMP_Type *base, opamp_negative_gain_t option)
238 {
239     base->OPAMP_CTR = (((base->OPAMP_CTR) & (~OPAMP_OPAMP_CTR_NGAIN_MASK)) | OPAMP_OPAMP_CTR_NGAIN(option));
240 }
241 /*! @} */
242 
243 #if defined(FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_BUFEN) && FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_BUFEN
244 /*!
245  * @name Reference Buffer Control Interface
246  * @{
247  */
248 
249 /*!
250  * @brief Enable reference buffer.
251  *
252  * @param base OPAMP peripheral base address.
253  * @param enable true to enable and false to disable.
254  */
OPAMP_EnableRefBuffer(OPAMP_Type * base,bool enable)255 static inline void OPAMP_EnableRefBuffer(OPAMP_Type *base, bool enable)
256 {
257     if (enable)
258     {
259         base->OPAMP_CTR |= OPAMP_OPAMP_CTR_BUFEN_MASK;
260     }
261     else
262     {
263         base->OPAMP_CTR &= ~(uint32_t)OPAMP_OPAMP_CTR_BUFEN_MASK;
264     }
265 }
266 /*! @} */
267 #endif /* FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_BUFEN */
268 
269 #if defined(FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_TRIGMD) && FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_TRIGMD
270 /*!
271  * @name Trigger Mode Control Interface
272  * @{
273  */
274 
275 /*!
276  * @brief Enable OPAMP trigger mode.
277  *
278  * @param base OPAMP peripheral base address.
279  * @param enable true to enable and false to disable.
280  */
OPAMP_EnableTriggerMode(OPAMP_Type * base,bool enable)281 static inline void OPAMP_EnableTriggerMode(OPAMP_Type *base, bool enable)
282 {
283     if (enable)
284     {
285         base->OPAMP_CTR |= OPAMP_OPAMP_CTR_TRIGMD_MASK;
286     }
287     else
288     {
289         base->OPAMP_CTR &= ~(uint32_t)OPAMP_OPAMP_CTR_TRIGMD_MASK;
290     }
291 }
292 /*! @} */
293 #endif /* FSL_FEATURE_OPAMP_HAS_OPAMP_CTR_TRIGMD */
294 
295 #if defined(__cplusplus)
296 }
297 #endif
298 
299 /*! @} */
300 
301 #endif /* FSL_OPAMP_H_ */
302