EFM32 Happy Gecko Software Documentation  efm32hg-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 */
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) */
#define _VCMP_INPUTSEL_TRIGLEVEL_SHIFT
Definition: efm32hg_vcmp.h:118
void VCMP_Init(const VCMP_Init_TypeDef *vcmpInit)
Configure and enable Voltage Comparator.
Definition: em_vcmp.c:60
Emlib peripheral API "assert" implementation.
#define VCMP
Voltage Comparator (VCMP) peripheral API.
VCMP_Hysteresis_TypeDef hyst
Definition: em_vcmp.h:110
__STATIC_INLINE void VCMP_IntClear(uint32_t flags)
Clear one or more pending VCMP interrupts.
Definition: em_vcmp.h:243
#define VCMP_CTRL_HYSTEN
Definition: efm32hg_vcmp.h:70
#define _VCMP_CTRL_BIASPROG_SHIFT
Definition: efm32hg_vcmp.h:105
void VCMP_TriggerSet(int level)
Configure trigger level of voltage comparator.
Definition: em_vcmp.c:175
__STATIC_INLINE bool VCMP_Ready(void)
VCMP output is ready.
Definition: em_vcmp.h:221
#define VCMP_IF_EDGE
Definition: efm32hg_vcmp.h:159
void VCMP_LowPowerRefSet(bool enable)
Enable or disable Low Power Reference setting.
Definition: em_vcmp.c:155
#define _VCMP_CTRL_WARMTIME_MASK
Definition: efm32hg_vcmp.h:76
#define VCMP_CTRL_EN
Definition: efm32hg_vcmp.h:60
#define _VCMP_CTRL_INACTVAL_SHIFT
Definition: efm32hg_vcmp.h:66
#define VCMP_CTRL_HALFBIAS
Definition: efm32hg_vcmp.h:109
VCMP_WarmTime_TypeDef warmup
Definition: em_vcmp.h:108
#define VCMP_CTRL_IFALL
Definition: efm32hg_vcmp.h:100
#define _VCMP_INPUTSEL_TRIGLEVEL_MASK
Definition: efm32hg_vcmp.h:119
#define VCMP_CTRL_IRISE
Definition: efm32hg_vcmp.h:95
#define VCMP_INPUTSEL_LPREF
Definition: efm32hg_vcmp.h:122
#define _VCMP_CTRL_BIASPROG_MASK
Definition: efm32hg_vcmp.h:106
#define _VCMP_CTRL_WARMTIME_SHIFT
Definition: efm32hg_vcmp.h:75