1 //*****************************************************************************
2 //
3 //  am_hal_vcomp.c
4 //! @file
5 //!
6 //! @brief Functions for operating the on-chip Voltage Comparator
7 //!
8 //! @addtogroup vcomp2 Voltage Comparator (VCOMP)
9 //! @ingroup apollo2hal
10 //! @{
11 //
12 //*****************************************************************************
13 
14 //*****************************************************************************
15 //
16 // Copyright (c) 2017, Ambiq Micro
17 // All rights reserved.
18 //
19 // Redistribution and use in source and binary forms, with or without
20 // modification, are permitted provided that the following conditions are met:
21 //
22 // 1. Redistributions of source code must retain the above copyright notice,
23 // this list of conditions and the following disclaimer.
24 //
25 // 2. Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // 3. Neither the name of the copyright holder nor the names of its
30 // contributors may be used to endorse or promote products derived from this
31 // software without specific prior written permission.
32 //
33 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
34 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
37 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
38 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
39 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
40 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
41 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43 // POSSIBILITY OF SUCH DAMAGE.
44 //
45 // This is part of revision 1.2.11 of the AmbiqSuite Development Package.
46 //
47 //*****************************************************************************
48 
49 #include <stdint.h>
50 #include <stdbool.h>
51 #include "am_mcu_apollo.h"
52 
53 //*****************************************************************************
54 //
55 //! @brief Configure the Voltage Comparator module.
56 //!
57 //! @param psConfig is a structure containing configuration information for the
58 //! voltage comparator.
59 //!
60 //! This function configures the positive and negative input signals for the
61 //! voltage comparator.
62 //!
63 //! @return None
64 //
65 //*****************************************************************************
66 void
am_hal_vcomp_config(const am_hal_vcomp_config_t * psConfig)67 am_hal_vcomp_config(const am_hal_vcomp_config_t *psConfig)
68 {
69     //
70     // The configuration word should be a simple OR of the components of the
71     // configuration structure.
72     //
73     AM_REG(VCOMP, CFG) = (psConfig->ui32LevelSelect |
74                           psConfig->ui32PosInput |
75                           psConfig->ui32NegInput);
76 }
77 
78 //*****************************************************************************
79 //
80 //! @brief Set the Voltage Comparator DAC Level Select in Configuration Reg.
81 //!
82 //! @param ui32Level - DAC voltage selector (use macros enumerations)
83 //!
84 //! This function sets the DAC level select in the configuration register.
85 //!
86 //! @return None
87 //
88 //*****************************************************************************
89 void
am_hal_vcomp_dac_level_set(uint32_t ui32Level)90 am_hal_vcomp_dac_level_set(uint32_t ui32Level)
91 {
92     //
93     // Insert the supplied level into the vcomp configuration register
94     //
95     AM_BFW(VCOMP, CFG, LVLSEL, ui32Level >> AM_REG_VCOMP_CFG_LVLSEL_S);
96 }
97 
98 //*****************************************************************************
99 //
100 //! @brief Read the state of the voltage comparator.
101 //!
102 //! This function extracts the comparator state from the status register.
103 //!
104 //! @return the voltage comparator state
105 //
106 //*****************************************************************************
107 bool
am_hal_vcomp_read(void)108 am_hal_vcomp_read(void)
109 {
110     return (AM_BFR(VCOMP,  STAT, CMPOUT) == 1);
111 }
112 
113 //*****************************************************************************
114 //
115 //! @brief Enable the voltage comparator.
116 //!
117 //! This function powers up the voltage comparator.
118 //!
119 //! @return None
120 //
121 //*****************************************************************************
122 void
am_hal_vcomp_enable(void)123 am_hal_vcomp_enable(void)
124 {
125     AM_REG(VCOMP, PWDKEY) = 0;
126 }
127 
128 //*****************************************************************************
129 //
130 //! @brief Disable the voltage comparator.
131 //!
132 //! This function powers down the voltage comparator.
133 //!
134 //! @return None
135 //
136 //*****************************************************************************
137 void
am_hal_vcomp_disable(void)138 am_hal_vcomp_disable(void)
139 {
140     AM_REG(VCOMP, PWDKEY) = AM_REG_VCOMP_PWDKEY_KEYVAL;
141 }
142 
143 //*****************************************************************************
144 //
145 //! @brief Read the state of the voltage comparator interrupt status bits.
146 //!
147 //! @param bEnabledOnly - return the status of only the enabled interrupts.
148 //!
149 //! This function extracts the interrupt status bits and returns the raw or
150 //! only the enabled based on bEnabledOnly.
151 //!
152 //! @return Bitwise representation of the current interrupt status.
153 //!
154 //! The return value will be the logical OR of one or more of the following
155 //! values:
156 //!
157 //! AM_HAL_VCOMP_INT_OUTHI
158 //! AM_HAL_VCOMP_INT_OUTLO
159 //
160 //*****************************************************************************
161 uint32_t
am_hal_vcomp_int_status_get(bool bEnabledOnly)162 am_hal_vcomp_int_status_get(bool bEnabledOnly)
163 {
164     if (bEnabledOnly)
165     {
166         uint32_t u32RetVal = AM_REG(VCOMP, INTSTAT);
167         return u32RetVal & AM_REG(VCOMP, INTEN);
168     }
169     else
170     {
171         return AM_REG(VCOMP, INTSTAT);
172     }
173 }
174 
175 //*****************************************************************************
176 //
177 //! @brief Set the state of the voltage comparator interrupt status bits.
178 //!
179 //! @param ui32Interrupt - interrupts to be set.
180 //!
181 //! This function sets the specified interrupt status bits.
182 //!
183 //! ui32Interrupt should be a logical or of:
184 //!
185 //! AM_HAL_VCOMP_INT_OUTHI
186 //! AM_HAL_VCOMP_INT_OUTLO
187 //!
188 //! @return None
189 //
190 //*****************************************************************************
191 void
am_hal_vcomp_int_set(uint32_t ui32Interrupt)192 am_hal_vcomp_int_set(uint32_t ui32Interrupt)
193 {
194     AM_REG(VCOMP, INTSET) = ui32Interrupt;
195 }
196 
197 //*****************************************************************************
198 //
199 //! @brief Clear the state of the voltage comparator interrupt status bits.
200 //!
201 //! @param ui32Interrupt - interrupts to be cleared.
202 //!
203 //! This function clears the specified interrupt status bits.
204 //!
205 //! ui32Interrupt should be a logical or of:
206 //!
207 //! AM_HAL_VCOMP_INT_OUTHI
208 //! AM_HAL_VCOMP_INT_OUTLO
209 //!
210 //! @return None
211 //
212 //*****************************************************************************
213 void
am_hal_vcomp_int_clear(uint32_t ui32Interrupt)214 am_hal_vcomp_int_clear(uint32_t ui32Interrupt)
215 {
216     AM_REG(VCOMP, INTCLR) = ui32Interrupt;
217 }
218 
219 //*****************************************************************************
220 //
221 //! @brief Enable the voltage comparator interrupt status bits.
222 //!
223 //! @param ui32Interrupt - interrupts to be enabled.
224 //!
225 //! This function enables desired interrupt status bits.
226 //!
227 //! ui32Interrupt should be a logical or of:
228 //!
229 //! AM_HAL_VCOMP_INT_OUTHI
230 //! AM_HAL_VCOMP_INT_OUTLO
231 //!
232 //! @return None
233 //
234 //*****************************************************************************
235 void
am_hal_vcomp_int_enable(uint32_t ui32Interrupt)236 am_hal_vcomp_int_enable(uint32_t ui32Interrupt)
237 {
238     AM_REG(VCOMP, INTEN) |= ui32Interrupt;
239 }
240 
241 //*****************************************************************************
242 //
243 //! @brief Return the enabled, voltage comparator interrupt status bits.
244 //!
245 //! This function returns the enabled interrupt status bits
246 //!
247 //! @return returns the enabled interrupt status bits. The return is a logical
248 //! or of:
249 //!
250 //! AM_HAL_VCOMP_INT_OUTHI
251 //! AM_HAL_VCOMP_INT_OUTLO
252 //
253 //*****************************************************************************
254 uint32_t
am_hal_vcomp_int_enable_get(void)255 am_hal_vcomp_int_enable_get(void)
256 {
257     return AM_REG(VCOMP, INTEN);
258 }
259 
260 //*****************************************************************************
261 //
262 //! @brief Disable the voltage comparator interrupt status bits.
263 //!
264 //! @param ui32Interrupt - interrupts to be disabled.
265 //!
266 //! This function disables desired interrupt status bits.
267 //!
268 //! ui32Interrupt should be a logical or of:
269 //!
270 //! AM_HAL_VCOMP_INT_OUTHI
271 //! AM_HAL_VCOMP_INT_OUTLO
272 //!
273 //! @return None
274 //
275 //*****************************************************************************
276 void
am_hal_vcomp_int_disable(uint32_t ui32Interrupt)277 am_hal_vcomp_int_disable(uint32_t ui32Interrupt)
278 {
279     AM_REG(VCOMP, INTEN) &= ~ui32Interrupt;
280 }
281 
282 //*****************************************************************************
283 //
284 // End Doxygen group.
285 //! @}
286 //
287 //*****************************************************************************
288