1 /***************************************************************************//**
2  * @file
3  * @brief Voltage Comparator (VCMP) peripheral API
4  * @author Energy Micro AS
5  * @version 3.0.0
6  *******************************************************************************
7  * @section License
8  * <b>(C) Copyright 2012 Energy Micro AS, http://www.energymicro.com</b>
9  *******************************************************************************
10  *
11  * Permission is granted to anyone to use this software for any purpose,
12  * including commercial applications, and to alter it and redistribute it
13  * freely, subject to the following restrictions:
14  *
15  * 1. The origin of this software must not be misrepresented; you must not
16  *    claim that you wrote the original software.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  *    misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  *
21  * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
22  * obligation to support this Software. Energy Micro AS is providing the
23  * Software "AS IS", with no express or implied warranties of any kind,
24  * including, but not limited to, any implied warranties of merchantability
25  * or fitness for any particular purpose or warranties against infringement
26  * of any proprietary rights of a third party.
27  *
28  * Energy Micro AS will not be liable for any consequential, incidental, or
29  * special damages, or any other relief, or for any claim by any third party,
30  * arising from your use of this Software.
31  *
32  ******************************************************************************/
33 #include "em_assert.h"
34 #include "em_vcmp.h"
35 
36 /***************************************************************************//**
37  * @addtogroup EM_Library
38  * @{
39  ******************************************************************************/
40 
41 /***************************************************************************//**
42  * @addtogroup VCMP
43  * @brief Voltage Comparator (VCMP) Peripheral API
44  * @{
45  ******************************************************************************/
46 
47 /***************************************************************************//**
48  * @brief
49  *   Configure and enable Voltage Comparator
50  *
51  * @param[in] vcmpInit
52  *   VCMP Initialization structure
53  ******************************************************************************/
VCMP_Init(const VCMP_Init_TypeDef * vcmpInit)54 void VCMP_Init(const VCMP_Init_TypeDef *vcmpInit)
55 {
56   /* Verify input */
57   EFM_ASSERT((vcmpInit->inactive == 0) || (vcmpInit->inactive == 1));
58   EFM_ASSERT((vcmpInit->biasProg >= 0) && (vcmpInit->biasProg < 16));
59 
60   /* Configure Half Bias setting */
61   if (vcmpInit->halfBias)
62   {
63     VCMP->CTRL |= VCMP_CTRL_HALFBIAS;
64   }
65   else
66   {
67     VCMP->CTRL &= ~(VCMP_CTRL_HALFBIAS);
68   }
69 
70   /* Configure bias prog */
71   VCMP->CTRL &= ~(_VCMP_CTRL_BIASPROG_MASK);
72   VCMP->CTRL |= (vcmpInit->biasProg << _VCMP_CTRL_BIASPROG_SHIFT);
73 
74   /* Configure sense for falling edge */
75   if (vcmpInit->irqFalling)
76   {
77     VCMP->CTRL |= VCMP_CTRL_IFALL;
78   }
79   else
80   {
81     VCMP->CTRL &= ~(VCMP_CTRL_IFALL);
82   }
83 
84   /* Configure sense for rising edge */
85   if (vcmpInit->irqRising)
86   {
87     VCMP->CTRL |= VCMP_CTRL_IRISE;
88   }
89   else
90   {
91     VCMP->CTRL &= ~(VCMP_CTRL_IRISE);
92   }
93 
94   /* Configure warm-up time */
95   VCMP->CTRL &= ~(_VCMP_CTRL_WARMTIME_MASK);
96   VCMP->CTRL |= (vcmpInit->warmup << _VCMP_CTRL_WARMTIME_SHIFT);
97 
98   /* Configure hysteresis */
99   switch (vcmpInit->hyst)
100   {
101   case vcmpHyst20mV:
102     VCMP->CTRL |= VCMP_CTRL_HYSTEN;
103     break;
104   case vcmpHystNone:
105     VCMP->CTRL &= ~(VCMP_CTRL_HYSTEN);
106     break;
107   default:
108     break;
109   }
110 
111   /* Configure inactive output value */
112   VCMP->CTRL |= (vcmpInit->inactive << _VCMP_CTRL_INACTVAL_SHIFT);
113 
114   /* Configure trigger level */
115   VCMP_TriggerSet(vcmpInit->triggerLevel);
116 
117   /* Enable or disable VCMP */
118   if (vcmpInit->enable)
119   {
120     VCMP->CTRL |= VCMP_CTRL_EN;
121   }
122   else
123   {
124     VCMP->CTRL &= ~(VCMP_CTRL_EN);
125   }
126 
127   /* If Low Power Reference is enabled, wait until VCMP is ready */
128   /* before enabling it, see reference manual for deatils        */
129   /* Configuring Low Power Ref without enable has no effect      */
130   if(vcmpInit->lowPowerRef && vcmpInit->enable)
131   {
132     /* Poll for VCMP ready */
133     while(!VCMP_Ready());
134     VCMP_LowPowerRefSet(vcmpInit->lowPowerRef);
135   }
136 
137   /* Clear edge interrupt */
138   VCMP_IntClear(VCMP_IF_EDGE);
139 }
140 
141 
142 /***************************************************************************//**
143  * @brief
144  *    Enable or disable Low Power Reference setting
145  *
146  * @param[in] enable
147  *    If true, enables low power reference, if false disable low power reference
148  ******************************************************************************/
VCMP_LowPowerRefSet(bool enable)149 void VCMP_LowPowerRefSet(bool enable)
150 {
151   if (enable)
152   {
153     VCMP->INPUTSEL |= VCMP_INPUTSEL_LPREF;
154   }
155   else
156   {
157     VCMP->INPUTSEL &= ~(VCMP_INPUTSEL_LPREF);
158   }
159 }
160 
161 
162 /***************************************************************************//**
163  * @brief
164  *    Configure trigger level of voltage comparator
165  *
166  * @param[in] level
167  *    Trigger value, in range 0-63
168  ******************************************************************************/
VCMP_TriggerSet(int level)169 void VCMP_TriggerSet(int level)
170 {
171   /* Trigger range is 6 bits, value from 0-63 */
172   EFM_ASSERT((level > 0) && (level < 64));
173 
174   /* Set trigger level */
175   VCMP->INPUTSEL = (VCMP->INPUTSEL & ~(_VCMP_INPUTSEL_TRIGLEVEL_MASK)) |
176                    (level << _VCMP_INPUTSEL_TRIGLEVEL_SHIFT);
177 }
178 
179 
180 /** @} (end addtogroup VCMP) */
181 /** @} (end addtogroup EM_Library) */
182