1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2019 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef FSL_VREF_H_
10 #define FSL_VREF_H_
11 
12 #include "fsl_common.h"
13 
14 /*!
15  * @addtogroup vref
16  * @{
17  */
18 
19 /******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 
23 /*! @name Driver version */
24 /*! @{ */
25 #define FSL_VREF_DRIVER_VERSION (MAKE_VERSION(2, 1, 2)) /*!< Version 2.1.2. */
26 /*! @} */
27 
28 /* Those macros below defined to support SoC family which have VREFL (0.4V) reference */
29 #if defined(FSL_FEATURE_VREF_HAS_LOW_REFERENCE) && FSL_FEATURE_VREF_HAS_LOW_REFERENCE
30 #define VREF_SC_MODE_LV       VREF_VREFH_SC_MODE_LV
31 #define VREF_SC_REGEN         VREF_VREFH_SC_REGEN
32 #define VREF_SC_VREFEN        VREF_VREFH_SC_VREFEN
33 #define VREF_SC_ICOMPEN       VREF_VREFH_SC_ICOMPEN
34 #define VREF_SC_REGEN_MASK    VREF_VREFH_SC_REGEN_MASK
35 #define VREF_SC_VREFST_MASK   VREF_VREFH_SC_VREFST_MASK
36 #define VREF_SC_VREFEN_MASK   VREF_VREFH_SC_VREFEN_MASK
37 #define VREF_SC_MODE_LV_MASK  VREF_VREFH_SC_MODE_LV_MASK
38 #define VREF_SC_ICOMPEN_MASK  VREF_VREFH_SC_ICOMPEN_MASK
39 #define TRM                   VREFH_TRM
40 #define VREF_TRM_TRIM         VREF_VREFH_TRM_TRIM
41 #define VREF_TRM_CHOPEN_MASK  VREF_VREFH_TRM_CHOPEN_MASK
42 #define VREF_TRM_TRIM_MASK    VREF_VREFH_TRM_TRIM_MASK
43 #define VREF_TRM_CHOPEN_SHIFT VREF_VREFH_TRM_CHOPEN_SHIFT
44 #define VREF_TRM_TRIM_SHIFT   VREF_VREFH_TRM_TRIM_SHIFT
45 #define VREF_SC_MODE_LV_SHIFT VREF_VREFH_SC_MODE_LV_SHIFT
46 #define VREF_SC_REGEN_SHIFT   VREF_VREFH_SC_REGEN_SHIFT
47 #define VREF_SC_VREFST_SHIFT  VREF_VREFH_SC_VREFST_SHIFT
48 #define VREF_SC_ICOMPEN_SHIFT VREF_VREFH_SC_ICOMPEN_SHIFT
49 #endif /* FSL_FEATURE_VREF_HAS_LOW_REFERENCE */
50 
51 /*!
52  * @brief VREF modes.
53  */
54 typedef enum _vref_buffer_mode
55 {
56     kVREF_ModeBandgapOnly = 0U, /*!< Bandgap on only, for stabilization and startup */
57 #if defined(FSL_FEATURE_VREF_MODE_LV_TYPE) && FSL_FEATURE_VREF_MODE_LV_TYPE
58     kVREF_ModeHighPowerBuffer = 1U, /*!< High-power buffer mode enabled */
59     kVREF_ModeLowPowerBuffer  = 2U  /*!< Low-power buffer mode enabled */
60 #else
61     kVREF_ModeTightRegulationBuffer = 2U /*!< Tight regulation buffer enabled */
62 #endif /* FSL_FEATURE_VREF_MODE_LV_TYPE */
63 } vref_buffer_mode_t;
64 
65 /*!
66  * @brief The description structure for the VREF module.
67  */
68 typedef struct _vref_config
69 {
70     vref_buffer_mode_t bufferMode; /*!< Buffer mode selection */
71 #if defined(FSL_FEATURE_VREF_HAS_LOW_REFERENCE) && FSL_FEATURE_VREF_HAS_LOW_REFERENCE
72     bool enableLowRef;          /*!< Set VREFL (0.4 V) reference buffer enable or disable */
73     bool enableExternalVoltRef; /*!< Select external voltage reference or not (internal) */
74 #endif                          /* FSL_FEATURE_VREF_HAS_LOW_REFERENCE */
75 #if defined(FSL_FEATURE_VREF_HAS_TRM4) && FSL_FEATURE_VREF_HAS_TRM4
76     bool enable2V1VoltRef; /*!< Enable Internal Voltage Reference (2.1V) */
77 #endif                     /* FSL_FEATURE_VREF_HAS_TRM4 */
78 } vref_config_t;
79 
80 /******************************************************************************
81  * API
82  ******************************************************************************/
83 
84 #if defined(__cplusplus)
85 extern "C" {
86 #endif /* __cplusplus */
87 
88 /*!
89  * @name VREF functional operation
90  * @{
91  */
92 
93 /*!
94  * @brief Enables the clock gate and configures the VREF module according to the configuration structure.
95  *
96  * This function must be called before calling all other VREF driver functions,
97  * read/write registers, and configurations with user-defined settings.
98  * The example below shows how to set up  vref_config_t parameters and
99  * how to call the VREF_Init function by passing in these parameters.
100  * This is an example.
101  * @code
102  *   vref_config_t vrefConfig;
103  *   vrefConfig.bufferMode = kVREF_ModeHighPowerBuffer;
104  *   vrefConfig.enableExternalVoltRef = false;
105  *   vrefConfig.enableLowRef = false;
106  *   VREF_Init(VREF, &vrefConfig);
107  * @endcode
108  *
109  * @param base VREF peripheral address.
110  * @param config Pointer to the configuration structure.
111  */
112 void VREF_Init(VREF_Type *base, const vref_config_t *config);
113 
114 /*!
115  * @brief Stops and disables the clock for the VREF module.
116  *
117  * This function should be called to shut down the module.
118  * This is an example.
119  * @code
120  *   vref_config_t vrefUserConfig;
121  *   VREF_Init(VREF);
122  *   VREF_GetDefaultConfig(&vrefUserConfig);
123  *   ...
124  *   VREF_Deinit(VREF);
125  * @endcode
126  *
127  * @param base VREF peripheral address.
128  */
129 void VREF_Deinit(VREF_Type *base);
130 
131 /*!
132  * @brief Initializes the VREF configuration structure.
133  *
134  * This function initializes the VREF configuration structure to default values.
135  * This is an example.
136  * @code
137  *   vrefConfig->bufferMode = kVREF_ModeHighPowerBuffer;
138  *   vrefConfig->enableExternalVoltRef = false;
139  *   vrefConfig->enableLowRef = false;
140  * @endcode
141  *
142  * @param config Pointer to the initialization structure.
143  */
144 void VREF_GetDefaultConfig(vref_config_t *config);
145 
146 /*!
147  * @brief Sets a TRIM value for the reference voltage.
148  *
149  * This function sets a TRIM value for the reference voltage.
150  * Note that the TRIM value maximum is 0x3F.
151  *
152  * @param base VREF peripheral address.
153  * @param trimValue Value of the trim register to set the output reference voltage (maximum 0x3F (6-bit)).
154  */
155 void VREF_SetTrimVal(VREF_Type *base, uint8_t trimValue);
156 
157 /*!
158  * @brief Reads the value of the TRIM meaning output voltage.
159  *
160  * This function gets the TRIM value from the TRM register.
161  *
162  * @param base VREF peripheral address.
163  * @return Six-bit value of trim setting.
164  */
VREF_GetTrimVal(VREF_Type * base)165 static inline uint8_t VREF_GetTrimVal(VREF_Type *base)
166 {
167     return (base->TRM & VREF_TRM_TRIM_MASK);
168 }
169 
170 #if defined(FSL_FEATURE_VREF_HAS_TRM4) && FSL_FEATURE_VREF_HAS_TRM4
171 /*!
172  * @brief Sets a TRIM value for the reference voltage (2V1).
173  *
174  * This function sets a TRIM value for the reference voltage (2V1).
175  * Note that the TRIM value maximum is 0x3F.
176  *
177  * @param base VREF peripheral address.
178  * @param trimValue Value of the trim register to set the output reference voltage (maximum 0x3F (6-bit)).
179  */
180 void VREF_SetTrim2V1Val(VREF_Type *base, uint8_t trimValue);
181 
182 /*!
183  * @brief Reads the value of the TRIM meaning output voltage (2V1).
184  *
185  * This function gets the TRIM value from the VREF_TRM4 register.
186  *
187  * @param base VREF peripheral address.
188  * @return Six-bit value of trim setting.
189  */
VREF_GetTrim2V1Val(VREF_Type * base)190 static inline uint8_t VREF_GetTrim2V1Val(VREF_Type *base)
191 {
192     return (base->TRM4 & VREF_TRM4_TRIM2V1_MASK);
193 }
194 #endif /* FSL_FEATURE_VREF_HAS_TRM4 */
195 
196 #if defined(FSL_FEATURE_VREF_HAS_LOW_REFERENCE) && FSL_FEATURE_VREF_HAS_LOW_REFERENCE
197 
198 /*!
199  * @brief Sets the TRIM value for the low voltage reference.
200  *
201  * This function sets the TRIM value for low reference voltage.
202  * Note the following.
203  *      - The TRIM value maximum is 0x05U
204  *      - The values 111b and 110b are not valid/allowed.
205  *
206  * @param base VREF peripheral address.
207  * @param trimValue Value of the trim register to set output low reference voltage (maximum 0x05U (3-bit)).
208  */
209 void VREF_SetLowReferenceTrimVal(VREF_Type *base, uint8_t trimValue);
210 
211 /*!
212  * @brief Reads the value of the TRIM meaning output voltage.
213  *
214  * This function gets the TRIM value from the VREFL_TRM register.
215  *
216  * @param base VREF peripheral address.
217  * @return Three-bit value of the trim setting.
218  */
VREF_GetLowReferenceTrimVal(VREF_Type * base)219 static inline uint8_t VREF_GetLowReferenceTrimVal(VREF_Type *base)
220 {
221     return (base->VREFL_TRM & VREF_VREFL_TRM_VREFL_TRIM_MASK);
222 }
223 #endif /* FSL_FEATURE_VREF_HAS_LOW_REFERENCE */
224 
225 /*! @} */
226 
227 #if defined(__cplusplus)
228 }
229 #endif /* __cplusplus */
230 
231 /*! @}*/
232 
233 #endif /* FSL_VREF_H_ */
234