EFR32 Mighty Gecko 1 Software Documentation  efr32mg1-doc-5.1.2
em_vcmp.c
Go to the documentation of this file.
1 /***************************************************************************/
33 #include "em_vcmp.h"
34 #if defined(VCMP_COUNT) && (VCMP_COUNT > 0)
35 
36 #include "em_assert.h"
37 
38 /***************************************************************************/
43 /***************************************************************************/
53 /***************************************************************************/
60 void VCMP_Init(const VCMP_Init_TypeDef *vcmpInit)
61 {
62  /* Verify input */
63  EFM_ASSERT((vcmpInit->inactive == 0) || (vcmpInit->inactive == 1));
64  EFM_ASSERT((vcmpInit->biasProg >= 0) && (vcmpInit->biasProg < 16));
65 
66  /* Configure Half Bias setting */
67  if (vcmpInit->halfBias)
68  {
69  VCMP->CTRL |= VCMP_CTRL_HALFBIAS;
70  }
71  else
72  {
73  VCMP->CTRL &= ~(VCMP_CTRL_HALFBIAS);
74  }
75 
76  /* Configure bias prog */
77  VCMP->CTRL &= ~(_VCMP_CTRL_BIASPROG_MASK);
78  VCMP->CTRL |= (vcmpInit->biasProg << _VCMP_CTRL_BIASPROG_SHIFT);
79 
80  /* Configure sense for falling edge */
81  if (vcmpInit->irqFalling)
82  {
83  VCMP->CTRL |= VCMP_CTRL_IFALL;
84  }
85  else
86  {
87  VCMP->CTRL &= ~(VCMP_CTRL_IFALL);
88  }
89 
90  /* Configure sense for rising edge */
91  if (vcmpInit->irqRising)
92  {
93  VCMP->CTRL |= VCMP_CTRL_IRISE;
94  }
95  else
96  {
97  VCMP->CTRL &= ~(VCMP_CTRL_IRISE);
98  }
99 
100  /* Configure warm-up time */
101  VCMP->CTRL &= ~(_VCMP_CTRL_WARMTIME_MASK);
102  VCMP->CTRL |= (vcmpInit->warmup << _VCMP_CTRL_WARMTIME_SHIFT);
103 
104  /* Configure hysteresis */
105  switch (vcmpInit->hyst)
106  {
107  case vcmpHyst20mV:
108  VCMP->CTRL |= VCMP_CTRL_HYSTEN;
109  break;
110  case vcmpHystNone:
111  VCMP->CTRL &= ~(VCMP_CTRL_HYSTEN);
112  break;
113  default:
114  break;
115  }
116 
117  /* Configure inactive output value */
118  VCMP->CTRL |= (vcmpInit->inactive << _VCMP_CTRL_INACTVAL_SHIFT);
119 
120  /* Configure trigger level */
121  VCMP_TriggerSet(vcmpInit->triggerLevel);
122 
123  /* Enable or disable VCMP */
124  if (vcmpInit->enable)
125  {
126  VCMP->CTRL |= VCMP_CTRL_EN;
127  }
128  else
129  {
130  VCMP->CTRL &= ~(VCMP_CTRL_EN);
131  }
132 
133  /* If Low Power Reference is enabled, wait until VCMP is ready */
134  /* before enabling it, see reference manual for deatils */
135  /* Configuring Low Power Ref without enable has no effect */
136  if(vcmpInit->lowPowerRef && vcmpInit->enable)
137  {
138  /* Poll for VCMP ready */
139  while(!VCMP_Ready());
140  VCMP_LowPowerRefSet(vcmpInit->lowPowerRef);
141  }
142 
143  /* Clear edge interrupt */
144  VCMP_IntClear(VCMP_IF_EDGE);
145 }
146 
147 
148 /***************************************************************************/
155 void VCMP_LowPowerRefSet(bool enable)
156 {
157  if (enable)
158  {
159  VCMP->INPUTSEL |= VCMP_INPUTSEL_LPREF;
160  }
161  else
162  {
163  VCMP->INPUTSEL &= ~VCMP_INPUTSEL_LPREF;
164  }
165 }
166 
167 
168 /***************************************************************************/
175 void VCMP_TriggerSet(int level)
176 {
177  /* Trigger range is 6 bits, value from 0-63 */
178  EFM_ASSERT((level > 0) && (level < 64));
179 
180  /* Set trigger level */
181  VCMP->INPUTSEL = (VCMP->INPUTSEL & ~(_VCMP_INPUTSEL_TRIGLEVEL_MASK))
182  | (level << _VCMP_INPUTSEL_TRIGLEVEL_SHIFT);
183 }
184 
185 
188 #endif /* defined(VCMP_COUNT) && (VCMP_COUNT > 0) */
Emlib peripheral API "assert" implementation.
Voltage Comparator (VCMP) peripheral API.