1 /******************************************************************************
2  *  Filename:       lpcmp.h
3  *
4  *  Description:    Defines and prototypes for the LPCMP peripheral.
5  *
6  *  Copyright (c) 2022, Texas Instruments Incorporated
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions are met:
11  *
12  *  1) Redistributions of source code must retain the above copyright notice,
13  *     this list of conditions and the following disclaimer.
14  *
15  *  2) Redistributions in binary form must reproduce the above copyright notice,
16  *     this list of conditions and the following disclaimer in the documentation
17  *     and/or other materials provided with the distribution.
18  *
19  *  3) Neither the name of the ORGANIZATION nor the names of its contributors may
20  *     be used to endorse or promote products derived from this software without
21  *     specific prior written permission.
22  *
23  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  *  POSSIBILITY OF SUCH DAMAGE.
34  *
35  ******************************************************************************/
36 
37 #ifndef __LPCMP_H__
38 #define __LPCMP_H__
39 
40 //*****************************************************************************
41 //
42 //! \addtogroup peripheral_group
43 //! @{
44 //! \addtogroup lpcmp_api
45 //! @{
46 //
47 //*****************************************************************************
48 
49 #include <stdbool.h>
50 #include <stdint.h>
51 #include "../inc/hw_types.h"
52 #include "../inc/hw_memmap.h"
53 #include "../inc/hw_sys0.h"
54 
55 //*****************************************************************************
56 //
57 // If building with a C++ compiler, make all of the definitions in this header
58 // have a C binding.
59 //
60 //*****************************************************************************
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64 
65 //*****************************************************************************
66 //
67 // Defines that can be passed to LPCMPSetDividerPath() to select the voltage
68 // divider path.
69 //
70 //*****************************************************************************
71 #define LPCMP_DIVISION_PATH_N SYS0_LPCMPCFG_DIVPATH_NSIDE //!< Apply voltage divider to the negative input
72 #define LPCMP_DIVISION_PATH_P SYS0_LPCMPCFG_DIVPATH_PSIDE //!< Apply voltage divider to the positive input
73 
74 //*****************************************************************************
75 //
76 // Defines that can be passed to LPCMPSetDividerRatio() to select which ratio
77 // should be used by the voltage divider.
78 //
79 //*****************************************************************************
80 #define LPCMP_DIVISION_FACTOR_1_1 SYS0_LPCMPCFG_DIV_VAL0 //!< Apply a 1/1 voltage division ratio
81 #define LPCMP_DIVISION_FACTOR_3_4 SYS0_LPCMPCFG_DIV_VAL1 //!< Apply a 3/4 voltage division ratio
82 #define LPCMP_DIVISION_FACTOR_1_2 SYS0_LPCMPCFG_DIV_VAL2 //!< Apply a 1/2 voltage division ratio
83 #define LPCMP_DIVISION_FACTOR_1_3 SYS0_LPCMPCFG_DIV_VAL3 //!< Apply a 1/3 voltage division ratio
84 #define LPCMP_DIVISION_FACTOR_1_4 SYS0_LPCMPCFG_DIV_VAL4 //!< Apply a 1/4 voltage division ratio
85 
86 //*****************************************************************************
87 //
88 // Defines that can be passed to LPCMPSetPolarity() to select which edge type
89 // should generate a LPCMP event.
90 //
91 //*****************************************************************************
92 #define LPCMP_POLARITY_FALL SYS0_LPCMPCFG_EDGCFG_FALL //!< Select falling edge for event generation
93 #define LPCMP_POLARITY_RISE SYS0_LPCMPCFG_EDGCFG_RISE //!< Select rising edge for event generation
94 
95 //*****************************************************************************
96 //
97 // Defines that can be passed to LPCMPNegativeInputSelect() to select which
98 // signal should be muxed to LPCMP negative input.
99 //
100 //*****************************************************************************
101 #define LPCMP_NEG_INPUT_VDDD SYS0_LPCMPCFG_NSEL_VDDD      //!< Select VDDD as negative input
102 #define LPCMP_NEG_INPUT_VDDA SYS0_LPCMPCFG_NSEL_VDDA      //!< Select VDDA as negative input
103 #define LPCMP_NEG_INPUT_A2   SYS0_LPCMPCFG_NSEL_VA_PAD_A2 //!< Select PAD_A2 as negative input
104 #define LPCMP_NEG_INPUT_A3   SYS0_LPCMPCFG_NSEL_VA_PAD_A3 //!< Select PAD_A3 as negative input
105 #define LPCMP_NEG_INPUT_OPEN SYS0_LPCMPCFG_NSEL_OPEN      //!< Leave negative input floating
106 
107 //*****************************************************************************
108 //
109 // Defines that can be passed to LPCMPPositiveInputSelect() to select which
110 // signal should be muxed to LPCMP positive input.
111 //
112 //*****************************************************************************
113 #define LPCMP_POS_INPUT_A1   SYS0_LPCMPCFG_PSEL_VA_PAD_A1 //!< Select PAD_A1 as positive input
114 #define LPCMP_POS_INPUT_A2   SYS0_LPCMPCFG_PSEL_VA_PAD_A2 //!< Select PAD_A2 as positive input
115 #define LPCMP_POS_INPUT_A3   SYS0_LPCMPCFG_PSEL_VA_PAD_A3 //!< Select PAD_A3 as positive input
116 #define LPCMP_POS_INPUT_VDDA SYS0_LPCMPCFG_PSEL_VDDA      //!< Selects VDDA as positive input
117 #define LPCMP_POS_INPUT_OPEN SYS0_LPCMPCFG_PSEL_OPEN      //!< Leave negative positive floating
118 
119 //*****************************************************************************
120 //
121 // API Functions and prototypes
122 //
123 //*****************************************************************************
124 
125 //*****************************************************************************
126 //
127 //! \brief Enables LPCMP.
128 //!
129 //! This function enables LPCMP and it must be called after having configured
130 //! the peripheral's inputs, voltage divider and event generation logic.
131 //
132 //*****************************************************************************
LPCMPEnable(void)133 __STATIC_INLINE void LPCMPEnable(void)
134 {
135     HWREG(SYS0_BASE + SYS0_O_LPCMPCFG) |= SYS0_LPCMPCFG_EN;
136 }
137 
138 //*****************************************************************************
139 //
140 //! \brief Disables LPCMP.
141 //!
142 //! This function disables LPCMP after it has been enabled using \ref LPCMPEnable().
143 //
144 //*****************************************************************************
LPCMPDisable(void)145 __STATIC_INLINE void LPCMPDisable(void)
146 {
147     HWREG(SYS0_BASE + SYS0_O_LPCMPCFG) &= ~SYS0_LPCMPCFG_EN;
148 }
149 
150 //*****************************************************************************
151 //
152 //! \brief Enables LPCMP event generation.
153 //!
154 //! This function enables LPCMP event generation to the event fabric.
155 //! Polarity must be selected using \ref LPCMPSetPolarity() to determine on which
156 //! edge type of LPCMP output an event should be generated. LPCMP doesn't have a
157 //! dedicated interrupt line, therefore a configurable interrupt line must be
158 //! configured to propagate LPCMP events.
159 //
160 //*****************************************************************************
LPCMPEnableEvent(void)161 __STATIC_INLINE void LPCMPEnableEvent(void)
162 {
163     HWREG(SYS0_BASE + SYS0_O_LPCMPCFG) |= SYS0_LPCMPCFG_EVTEN;
164 }
165 
166 //*****************************************************************************
167 //
168 //! \brief Disables LPCMP event generation.
169 //!
170 //! This function disables LPCMP event generation to the event fabric.
171 //
172 //*****************************************************************************
LPCMPDisableEvent(void)173 __STATIC_INLINE void LPCMPDisableEvent(void)
174 {
175     HWREG(SYS0_BASE + SYS0_O_LPCMPCFG) &= ~SYS0_LPCMPCFG_EVTEN;
176 }
177 
178 //*****************************************************************************
179 //
180 //! \brief Clear LPCMP event flag.
181 //!
182 //! This function clears LPCMP event flag and should be called from the ISR
183 //! registered to service LPCMP interrupts.
184 //
185 //*****************************************************************************
LPCMPClearEvent(void)186 __STATIC_INLINE void LPCMPClearEvent(void)
187 {
188     HWREG(SYS0_BASE + SYS0_O_LPCMPCFG) &= ~SYS0_LPCMPCFG_EVTIFG;
189 }
190 
191 //*****************************************************************************
192 //
193 //! \brief Enables LPCMP as wakeup source from standby.
194 //!
195 //! This function enables LPCMP as one of the sources that can wakeup the device
196 //! from standby.
197 //
198 //*****************************************************************************
LPCMPEnableWakeup(void)199 __STATIC_INLINE void LPCMPEnableWakeup(void)
200 {
201     HWREG(SYS0_BASE + SYS0_O_LPCMPCFG) |= SYS0_LPCMPCFG_WUENSB;
202 }
203 
204 //*****************************************************************************
205 //
206 //! \brief Disables LPCMP as wakeup source from standby.
207 //!
208 //! This function disables LPCMP as one of the sources that can wakeup the device
209 //! from standby.
210 //
211 //*****************************************************************************
LPCMPDisableWakeup(void)212 __STATIC_INLINE void LPCMPDisableWakeup(void)
213 {
214     HWREG(SYS0_BASE + SYS0_O_LPCMPCFG) &= ~SYS0_LPCMPCFG_WUENSB;
215 }
216 
217 //*****************************************************************************
218 //
219 //! \brief Selects the negative input of LPCMP.
220 //!
221 //! This function selects one of the available sources as the negative input of
222 //! LPCMP. This function must be called before enabling LPCMP using \ref LPCMPEnable().
223 //!
224 //! \param input
225 //!     LPCMP negative input source:
226 //!     - \ref LPCMP_NEG_INPUT_VDDD
227 //!     - \ref LPCMP_NEG_INPUT_VDDA
228 //!     - \ref LPCMP_NEG_INPUT_A2
229 //!     - \ref LPCMP_NEG_INPUT_A3
230 //!     - \ref LPCMP_NEG_INPUT_OPEN
231 //
232 //*****************************************************************************
LPCMPSelectNegativeInput(uint32_t input)233 __STATIC_INLINE void LPCMPSelectNegativeInput(uint32_t input)
234 {
235     uint32_t lpcmpcfg;
236 
237     lpcmpcfg                           = HWREG(SYS0_BASE + SYS0_O_LPCMPCFG);
238     HWREG(SYS0_BASE + SYS0_O_LPCMPCFG) = (lpcmpcfg & ~SYS0_LPCMPCFG_NSEL_M) | (input & SYS0_LPCMPCFG_NSEL_M);
239 }
240 
241 //*****************************************************************************
242 //
243 //! \brief Verifies if the raw output of LPCMP is high.
244 //!
245 //! This function returns a boolean value indicating whether the raw output of
246 //! LPCMP is high or low
247 //!
248 //! \return status of LPCMP raw output:
249 //! - \c true  : LPCMP raw output is high.
250 //! - \c false : LPCMP raw output is low.
251 //
252 //*****************************************************************************
LPCMPIsOutputHigh(void)253 __STATIC_INLINE bool LPCMPIsOutputHigh(void)
254 {
255     if (HWREG(SYS0_BASE + SYS0_O_LPCMPCFG) & SYS0_LPCMPCFG_COUT_HIGH)
256     {
257         return true;
258     }
259     else
260     {
261         return false;
262     }
263 }
264 
265 //*****************************************************************************
266 //
267 //! \brief Selects the positive input of LPCMP.
268 //!
269 //! This function selects one of the available sources as the positive input of
270 //! LPCMP. This function must be called before enabling LPCMP using \ref LPCMPEnable().
271 //!
272 //! \param input
273 //!     LPCMP positive input source:
274 //!     - \ref LPCMP_POS_INPUT_A1
275 //!     - \ref LPCMP_POS_INPUT_A2
276 //!     - \ref LPCMP_POS_INPUT_A3
277 //!     - \ref LPCMP_POS_INPUT_VDDA
278 //!     - \ref LPCMP_POS_INPUT_OPEN
279 //
280 //*****************************************************************************
LPCMPSelectPositiveInput(uint32_t input)281 __STATIC_INLINE void LPCMPSelectPositiveInput(uint32_t input)
282 {
283     uint32_t lpcmpcfg;
284 
285     lpcmpcfg                           = HWREG(SYS0_BASE + SYS0_O_LPCMPCFG);
286     HWREG(SYS0_BASE + SYS0_O_LPCMPCFG) = (lpcmpcfg & ~SYS0_LPCMPCFG_PSEL_M) | (input & SYS0_LPCMPCFG_PSEL_M);
287 }
288 
289 //*****************************************************************************
290 //
291 //! \brief Selects the edge polarity of LPCMP output on which an event
292 //! is triggered.
293 //!
294 //! This function selects whether a rising or falling edge of LPCMP output
295 //! will trigger an event propagated through the event fabric.
296 //!
297 //! \param polarity
298 //!     Edge polarity of LPCMP output on which the event is triggered:
299 //!     - \ref LPCMP_POLARITY_RISE
300 //!     - \ref LPCMP_POLARITY_FALL
301 //
302 //*****************************************************************************
LPCMPSetPolarity(uint32_t polarity)303 __STATIC_INLINE void LPCMPSetPolarity(uint32_t polarity)
304 {
305     if (polarity == LPCMP_POLARITY_FALL)
306     {
307         HWREG(SYS0_BASE + SYS0_O_LPCMPCFG) |= SYS0_LPCMPCFG_EDGCFG;
308     }
309     else
310     {
311         HWREG(SYS0_BASE + SYS0_O_LPCMPCFG) &= ~SYS0_LPCMPCFG_EDGCFG;
312     }
313 }
314 
315 //*****************************************************************************
316 //
317 //! \brief Selects the ratio used by LPCMP voltage divider
318 //!
319 //! This function selects the ratio used by LPCMP voltage divider to divide one
320 //! of the two comparator inputs. Use \ref LPCMPSetDividerPath() to select if
321 //! the ratio should be applied to the negative or positive input of LPCMP.
322 //!
323 //! \param divider
324 //!     Divider ratio used by LPCMP voltage divider:
325 //!     - \ref LPCMP_DIVISION_FACTOR_1_1
326 //!     - \ref LPCMP_DIVISION_FACTOR_3_4
327 //!     - \ref LPCMP_DIVISION_FACTOR_1_2
328 //!     - \ref LPCMP_DIVISION_FACTOR_1_3
329 //!     - \ref LPCMP_DIVISION_FACTOR_1_4
330 //
331 //*****************************************************************************
LPCMPSetDividerRatio(uint32_t divider)332 __STATIC_INLINE void LPCMPSetDividerRatio(uint32_t divider)
333 {
334     uint32_t lpcmpcfg;
335 
336     lpcmpcfg                           = HWREG(SYS0_BASE + SYS0_O_LPCMPCFG);
337     HWREG(SYS0_BASE + SYS0_O_LPCMPCFG) = (lpcmpcfg & ~SYS0_LPCMPCFG_DIV_M) | (divider & SYS0_LPCMPCFG_DIV_M);
338 }
339 
340 //*****************************************************************************
341 //
342 //! \brief Selects the input path on which the LPCMP voltage divider is applied
343 //!
344 //! This function selects the input path on which the LPCMP voltage divider is
345 //! applied. Use \ref LPCMPSetDividerRatio() to set the division ratio applied
346 //! by the LPCMP voltage divider on the selected path.
347 //!
348 //! \param path
349 //!     Input path the LPCMP voltage divider is applied to:
350 //!     - \ref LPCMP_DIVISION_PATH_N
351 //!     - \ref LPCMP_DIVISION_PATH_P
352 //
353 //*****************************************************************************
LPCMPSetDividerPath(uint32_t path)354 __STATIC_INLINE void LPCMPSetDividerPath(uint32_t path)
355 {
356     if (path == LPCMP_DIVISION_PATH_P)
357     {
358         HWREG(SYS0_BASE + SYS0_O_LPCMPCFG) |= SYS0_LPCMPCFG_DIVPATH;
359     }
360     else
361     {
362         HWREG(SYS0_BASE + SYS0_O_LPCMPCFG) &= ~SYS0_LPCMPCFG_DIVPATH;
363     }
364 }
365 
366 //*****************************************************************************
367 //
368 // Mark the end of the C bindings section for C++ compilers.
369 //
370 //*****************************************************************************
371 #ifdef __cplusplus
372 }
373 #endif
374 
375 //*****************************************************************************
376 //
377 //! Close the Doxygen group.
378 //! @}
379 //! @}
380 //
381 //*****************************************************************************
382 
383 #endif // __LPCMP_H__
384