1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  *   of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  *   list of conditions and the following disclaimer in the documentation and/or
13  *   other materials provided with the distribution.
14  *
15  * o Neither the name of the copyright holder nor the names of its
16  *   contributors may be used to endorse or promote products derived from this
17  *   software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef _FSL_VREF_H_
32 #define _FSL_VREF_H_
33 
34 #include "fsl_common.h"
35 
36 /*!
37  * @addtogroup vref
38  * @{
39  */
40 
41 
42 /******************************************************************************
43  * Definitions
44  ******************************************************************************/
45 
46 /*! @name Driver version */
47 /*@{*/
48 #define FSL_VREF_DRIVER_VERSION (MAKE_VERSION(2, 1, 0)) /*!< Version 2.1.0. */
49 /*@}*/
50 
51 /* Those macros below defined to support SoC family which have VREFL (0.4V) reference */
52 #if defined(FSL_FEATURE_VREF_HAS_LOW_REFERENCE) && FSL_FEATURE_VREF_HAS_LOW_REFERENCE
53 #define VREF_SC_MODE_LV VREF_VREFH_SC_MODE_LV
54 #define VREF_SC_REGEN VREF_VREFH_SC_REGEN
55 #define VREF_SC_VREFEN VREF_VREFH_SC_VREFEN
56 #define VREF_SC_ICOMPEN VREF_VREFH_SC_ICOMPEN
57 #define VREF_SC_REGEN_MASK VREF_VREFH_SC_REGEN_MASK
58 #define VREF_SC_VREFST_MASK VREF_VREFH_SC_VREFST_MASK
59 #define VREF_SC_VREFEN_MASK VREF_VREFH_SC_VREFEN_MASK
60 #define VREF_SC_MODE_LV_MASK VREF_VREFH_SC_MODE_LV_MASK
61 #define VREF_SC_ICOMPEN_MASK VREF_VREFH_SC_ICOMPEN_MASK
62 #define TRM VREFH_TRM
63 #define VREF_TRM_TRIM VREF_VREFH_TRM_TRIM
64 #define VREF_TRM_CHOPEN_MASK VREF_VREFH_TRM_CHOPEN_MASK
65 #define VREF_TRM_TRIM_MASK VREF_VREFH_TRM_TRIM_MASK
66 #define VREF_TRM_CHOPEN_SHIFT VREF_VREFH_TRM_CHOPEN_SHIFT
67 #define VREF_TRM_TRIM_SHIFT VREF_VREFH_TRM_TRIM_SHIFT
68 #define VREF_SC_MODE_LV_SHIFT VREF_VREFH_SC_MODE_LV_SHIFT
69 #define VREF_SC_REGEN_SHIFT VREF_VREFH_SC_REGEN_SHIFT
70 #define VREF_SC_VREFST_SHIFT VREF_VREFH_SC_VREFST_SHIFT
71 #define VREF_SC_ICOMPEN_SHIFT VREF_VREFH_SC_ICOMPEN_SHIFT
72 #endif /* FSL_FEATURE_VREF_HAS_LOW_REFERENCE */
73 
74 /*!
75  * @brief VREF modes.
76  */
77 typedef enum _vref_buffer_mode
78 {
79     kVREF_ModeBandgapOnly = 0U, /*!< Bandgap on only, for stabilization and startup */
80 #if defined(FSL_FEATURE_VREF_MODE_LV_TYPE) && FSL_FEATURE_VREF_MODE_LV_TYPE
81     kVREF_ModeHighPowerBuffer = 1U, /*!< High-power buffer mode enabled */
82     kVREF_ModeLowPowerBuffer = 2U   /*!< Low-power buffer mode enabled */
83 #else
84     kVREF_ModeTightRegulationBuffer = 2U /*!< Tight regulation buffer enabled */
85 #endif /* FSL_FEATURE_VREF_MODE_LV_TYPE */
86 } vref_buffer_mode_t;
87 
88 /*!
89  * @brief The description structure for the VREF module.
90  */
91 typedef struct _vref_config
92 {
93     vref_buffer_mode_t bufferMode; /*!< Buffer mode selection */
94 #if defined(FSL_FEATURE_VREF_HAS_LOW_REFERENCE) && FSL_FEATURE_VREF_HAS_LOW_REFERENCE
95     bool enableLowRef;          /*!< Set VREFL (0.4 V) reference buffer enable or disable */
96     bool enableExternalVoltRef; /*!< Select external voltage reference or not (internal) */
97 #endif                          /* FSL_FEATURE_VREF_HAS_LOW_REFERENCE */
98 #if defined(FSL_FEATURE_VREF_HAS_TRM4) && FSL_FEATURE_VREF_HAS_TRM4
99     bool enable2V1VoltRef; /*!< Enable Internal Voltage Reference (2.1V) */
100 #endif                     /* FSL_FEATURE_VREF_HAS_TRM4 */
101 } vref_config_t;
102 
103 /******************************************************************************
104  * API
105  ******************************************************************************/
106 
107 #if defined(__cplusplus)
108 extern "C" {
109 #endif /* __cplusplus */
110 
111 /*!
112  * @name VREF functional operation
113  * @{
114  */
115 
116 /*!
117  * @brief Enables the clock gate and configures the VREF module according to the configuration structure.
118  *
119  * This function must be called before calling all other VREF driver functions,
120  * read/write registers, and configurations with user-defined settings.
121  * The example below shows how to set up  vref_config_t parameters and
122  * how to call the VREF_Init function by passing in these parameters.
123  * This is an example.
124  * @code
125  *   vref_config_t vrefConfig;
126  *   vrefConfig.bufferMode = kVREF_ModeHighPowerBuffer;
127  *   vrefConfig.enableExternalVoltRef = false;
128  *   vrefConfig.enableLowRef = false;
129  *   VREF_Init(VREF, &vrefConfig);
130  * @endcode
131  *
132  * @param base VREF peripheral address.
133  * @param config Pointer to the configuration structure.
134  */
135 void VREF_Init(VREF_Type *base, const vref_config_t *config);
136 
137 /*!
138  * @brief Stops and disables the clock for the VREF module.
139  *
140  * This function should be called to shut down the module.
141  * This is an example.
142  * @code
143  *   vref_config_t vrefUserConfig;
144  *   VREF_Init(VREF);
145  *   VREF_GetDefaultConfig(&vrefUserConfig);
146  *   ...
147  *   VREF_Deinit(VREF);
148  * @endcode
149  *
150  * @param base VREF peripheral address.
151  */
152 void VREF_Deinit(VREF_Type *base);
153 
154 /*!
155  * @brief Initializes the VREF configuration structure.
156  *
157  * This function initializes the VREF configuration structure to default values.
158  * This is an example.
159  * @code
160  *   vrefConfig->bufferMode = kVREF_ModeHighPowerBuffer;
161  *   vrefConfig->enableExternalVoltRef = false;
162  *   vrefConfig->enableLowRef = false;
163  * @endcode
164  *
165  * @param config Pointer to the initialization structure.
166  */
167 void VREF_GetDefaultConfig(vref_config_t *config);
168 
169 /*!
170  * @brief Sets a TRIM value for the reference voltage.
171  *
172  * This function sets a TRIM value for the reference voltage.
173  * Note that the TRIM value maximum is 0x3F.
174  *
175  * @param base VREF peripheral address.
176  * @param trimValue Value of the trim register to set the output reference voltage (maximum 0x3F (6-bit)).
177  */
178 void VREF_SetTrimVal(VREF_Type *base, uint8_t trimValue);
179 
180 /*!
181  * @brief Reads the value of the TRIM meaning output voltage.
182  *
183  * This function gets the TRIM value from the TRM register.
184  *
185  * @param base VREF peripheral address.
186  * @return Six-bit value of trim setting.
187  */
VREF_GetTrimVal(VREF_Type * base)188 static inline uint8_t VREF_GetTrimVal(VREF_Type *base)
189 {
190     return (base->TRM & VREF_TRM_TRIM_MASK);
191 }
192 
193 #if defined(FSL_FEATURE_VREF_HAS_TRM4) && FSL_FEATURE_VREF_HAS_TRM4
194 /*!
195  * @brief Sets a TRIM value for the reference voltage (2V1).
196  *
197  * This function sets a TRIM value for the reference voltage (2V1).
198  * Note that the TRIM value maximum is 0x3F.
199  *
200  * @param base VREF peripheral address.
201  * @param trimValue Value of the trim register to set the output reference voltage (maximum 0x3F (6-bit)).
202  */
203 void VREF_SetTrim2V1Val(VREF_Type *base, uint8_t trimValue);
204 
205 /*!
206  * @brief Reads the value of the TRIM meaning output voltage (2V1).
207  *
208  * This function gets the TRIM value from the VREF_TRM4 register.
209  *
210  * @param base VREF peripheral address.
211  * @return Six-bit value of trim setting.
212  */
VREF_GetTrim2V1Val(VREF_Type * base)213 static inline uint8_t VREF_GetTrim2V1Val(VREF_Type *base)
214 {
215     return (base->TRM4 & VREF_TRM4_TRIM2V1_MASK);
216 }
217 #endif /* FSL_FEATURE_VREF_HAS_TRM4 */
218 
219 #if defined(FSL_FEATURE_VREF_HAS_LOW_REFERENCE) && FSL_FEATURE_VREF_HAS_LOW_REFERENCE
220 
221 /*!
222  * @brief Sets the TRIM value for the low voltage reference.
223  *
224  * This function sets the TRIM value for low reference voltage.
225  * Note the following.
226  *      - The TRIM value maximum is 0x05U
227  *      - The values 111b and 110b are not valid/allowed.
228  *
229  * @param base VREF peripheral address.
230  * @param trimValue Value of the trim register to set output low reference voltage (maximum 0x05U (3-bit)).
231  */
232 void VREF_SetLowReferenceTrimVal(VREF_Type *base, uint8_t trimValue);
233 
234 /*!
235  * @brief Reads the value of the TRIM meaning output voltage.
236  *
237  * This function gets the TRIM value from the VREFL_TRM register.
238  *
239  * @param base VREF peripheral address.
240  * @return Three-bit value of the trim setting.
241  */
VREF_GetLowReferenceTrimVal(VREF_Type * base)242 static inline uint8_t VREF_GetLowReferenceTrimVal(VREF_Type *base)
243 {
244     return (base->VREFL_TRM & VREF_VREFL_TRM_VREFL_TRIM_MASK);
245 }
246 #endif /* FSL_FEATURE_VREF_HAS_LOW_REFERENCE */
247 
248 /*@}*/
249 
250 #if defined(__cplusplus)
251 }
252 #endif /* __cplusplus */
253 
254 /*! @}*/
255 
256 #endif /* _FSL_VREF_H_ */
257