EFM32 Happy Gecko Software Documentation  efm32hg-doc-5.1.2
em_csen.c
Go to the documentation of this file.
1 /***************************************************************************/
33 #include "em_csen.h"
34 #if defined( CSEN_COUNT ) && ( CSEN_COUNT > 0 )
35 
36 #include "em_assert.h"
37 #include "em_cmu.h"
38 #include <stddef.h>
39 
40 /***************************************************************************/
45 /***************************************************************************/
50 /*******************************************************************************
51  ******************************* DEFINES ***********************************
52  ******************************************************************************/
53 
57 #define CSEN_REF_VALID(ref) ((ref) == CSEN)
58 
61 /*******************************************************************************
62  ************************** GLOBAL FUNCTIONS *******************************
63  ******************************************************************************/
64 
65 /***************************************************************************/
88 void CSEN_DMBaselineSet(CSEN_TypeDef *csen, uint32_t up, uint32_t down)
89 {
90  EFM_ASSERT(up < 0x10000);
91  EFM_ASSERT(down < 0x10000);
92 
93  csen->DMBASELINE = (up << _CSEN_DMBASELINE_BASELINEUP_SHIFT)
94  | (down << _CSEN_DMBASELINE_BASELINEDN_SHIFT);
95 }
96 
97 
98 /***************************************************************************/
115 void CSEN_Init(CSEN_TypeDef *csen, const CSEN_Init_TypeDef *init)
116 {
117  uint32_t tmp;
118 
119  EFM_ASSERT(CSEN_REF_VALID(csen));
120  EFM_ASSERT(init->warmUpCount < 4);
121 
122  /* Initialize CTRL. This will stop any conversion in progress. */
123  tmp = CSEN_CTRL_STM_DEFAULT;
124 
125  if (init->cpAccuracyHi)
126  {
127  tmp |= CSEN_CTRL_CPACCURACY_HI;
128  }
129 
130  if (init->localSense)
131  {
132  tmp |= _CSEN_CTRL_LOCALSENS_MASK;
133  }
134 
135  if (init->keepWarm)
136  {
137  tmp |= CSEN_CTRL_WARMUPMODE_KEEPCSENWARM;
138  }
139 
140  csen->CTRL = tmp;
141 
142  /* Initialize TIMCTRL. */
143  csen->TIMCTRL = (init->warmUpCount << _CSEN_TIMCTRL_WARMUPCNT_SHIFT)
144  | (init->pcReload << _CSEN_TIMCTRL_PCTOP_SHIFT)
145  | (init->pcPrescale << _CSEN_TIMCTRL_PCPRESC_SHIFT);
146 
147  /* PRSSEL only has one field */
148  csen->PRSSEL = init->prsSel << _CSEN_PRSSEL_PRSSEL_SHIFT;
149 
150  /* Set input selections for inputs 0 to 31 */
151  csen->SCANINPUTSEL0 = (init->input0To7 << _CSEN_SCANINPUTSEL0_INPUT0TO7SEL_SHIFT)
152  | (init->input8To15 << _CSEN_SCANINPUTSEL0_INPUT8TO15SEL_SHIFT)
153  | (init->input16To23 << _CSEN_SCANINPUTSEL0_INPUT16TO23SEL_SHIFT)
154  | (init->input24To31 << _CSEN_SCANINPUTSEL0_INPUT24TO31SEL_SHIFT);
155 
156  /* Set input selections for inputs 32 to 63 */
157  csen->SCANINPUTSEL1 = (init->input32To39 << _CSEN_SCANINPUTSEL1_INPUT32TO39SEL_SHIFT)
158  | (init->input40To47 << _CSEN_SCANINPUTSEL1_INPUT40TO47SEL_SHIFT)
159  | (init->input48To55 << _CSEN_SCANINPUTSEL1_INPUT48TO55SEL_SHIFT)
160  | (init->input56To63 << _CSEN_SCANINPUTSEL1_INPUT56TO63SEL_SHIFT);
161 }
162 
163 
164 /***************************************************************************/
186 void CSEN_InitMode(CSEN_TypeDef *csen, const CSEN_InitMode_TypeDef *init)
187 {
188  uint32_t tmp;
189 
190  EFM_ASSERT(CSEN_REF_VALID(csen));
191  EFM_ASSERT(init->dmIterPerCycle < 0x10);
192  EFM_ASSERT(init->dmCycles < 0x10);
193 
194  /* Initialize CTRL. This will stop any conversion in progress.
195  * These composite inputs set multiple fields. They do not need
196  * to be shifted. */
197  tmp = ((uint32_t)init->sampleMode
198  | (uint32_t)init->convSel
199  | (uint32_t)init->cmpMode);
200 
201  tmp |= (init->trigSel << _CSEN_CTRL_STM_SHIFT)
202  | (init->accMode << _CSEN_CTRL_ACU_SHIFT)
203  | (init->sarRes << _CSEN_CTRL_SARCR_SHIFT);
204 
205  if (init->enableDma)
206  {
207  tmp |= CSEN_CTRL_DMAEN_ENABLE;
208  }
209 
210  if (init->sumOnly)
211  {
212  tmp |= CSEN_CTRL_DRSF_ENABLE;
213  }
214 
215  if (init->autoGnd)
216  {
217  tmp |= CSEN_CTRL_AUTOGND_ENABLE;
218  }
219 
220  /* Preserve the fields that were initialized by CSEN_Init(). */
221  tmp |= csen->CTRL & (_CSEN_CTRL_CPACCURACY_MASK
222  | _CSEN_CTRL_LOCALSENS_MASK
223  | _CSEN_CTRL_WARMUPMODE_MASK);
224 
225  csen->CTRL = tmp;
226 
227  /* EMACTRL only has one field */
228  csen->EMACTRL = init->emaSample << _CSEN_EMACTRL_EMASAMPLE_SHIFT;
229 
230  /* CMPTHR only has one field */
231  csen->CMPTHR = init->cmpThr << _CSEN_CMPTHR_CMPTHR_SHIFT;
232 
233  /* SINGLECTRL only has one field */
234  csen->SINGLECTRL = init->singleSel << _CSEN_SINGLECTRL_SINGLESEL_SHIFT;
235 
236  /* Set all input enables */
237  csen->SCANMASK0 = init->inputMask0;
238  csen->SCANMASK1 = init->inputMask1;
239 
240  /* Initialize DMCFG. */
241  tmp = (init->dmRes << _CSEN_DMCFG_CRMODE_SHIFT)
242  | (init->dmCycles << _CSEN_DMCFG_DMCR_SHIFT)
243  | (init->dmIterPerCycle << _CSEN_DMCFG_DMR_SHIFT)
244  | (init->dmDelta << _CSEN_DMCFG_DMG_SHIFT);
245 
246  if (init->dmFixedDelta)
247  {
248  tmp |= CSEN_DMCFG_DMGRDIS;
249  }
250 
251  csen->DMCFG = tmp;
252 
253  /* Initialize ANACTRL. */
254  csen->ANACTRL = (init->resetPhase << _CSEN_ANACTRL_TRSTPROG_SHIFT)
255  | (init->driveSel << _CSEN_ANACTRL_IDACIREFS_SHIFT)
256  | (init->gainSel << _CSEN_ANACTRL_IREFPROG_SHIFT);
257 }
258 
259 
260 /***************************************************************************/
267 void CSEN_Reset(CSEN_TypeDef *csen)
268 {
269  EFM_ASSERT(CSEN_REF_VALID(csen));
270 
271  /* Resetting CTRL stops any conversion in progress. */
272  csen->CTRL = _CSEN_CTRL_RESETVALUE;
273  csen->TIMCTRL = _CSEN_TIMCTRL_RESETVALUE;
274  csen->PRSSEL = _CSEN_PRSSEL_RESETVALUE;
275  csen->DATA = _CSEN_DATA_RESETVALUE;
276  csen->SCANMASK0 = _CSEN_SCANMASK0_RESETVALUE;
277  csen->SCANINPUTSEL0 = _CSEN_SCANINPUTSEL0_RESETVALUE;
278  csen->SCANMASK1 = _CSEN_SCANMASK1_RESETVALUE;
279  csen->SCANINPUTSEL1 = _CSEN_SCANINPUTSEL1_RESETVALUE;
280  csen->CMPTHR = _CSEN_CMPTHR_RESETVALUE;
281  csen->EMA = _CSEN_EMA_RESETVALUE;
282  csen->EMACTRL = _CSEN_EMACTRL_RESETVALUE;
283  csen->SINGLECTRL = _CSEN_SINGLECTRL_RESETVALUE;
284  csen->DMBASELINE = _CSEN_DMBASELINE_RESETVALUE;
285  csen->DMCFG = _CSEN_DMCFG_RESETVALUE;
286  csen->ANACTRL = _CSEN_ANACTRL_RESETVALUE;
287  csen->IEN = _CSEN_IEN_RESETVALUE;
288  csen->IFC = _CSEN_IF_MASK;
289 }
290 
291 
294 #endif /* defined(CSEN_COUNT) && (CSEN_COUNT > 0) */
Clock management unit (CMU) API.
Emlib peripheral API "assert" implementation.
Capacitive Sense Module (CSEN) peripheral API.