1 /***************************************************************************//**
2  * @file
3  * @brief Low Energy Sensor (LESENSE) 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 #ifndef __EM_LESENSE_H
34 #define __EM_LESENSE_H
35 
36 #include "em_part.h"
37 
38 #if defined(LESENSE_COUNT) && (LESENSE_COUNT > 0)
39 #include <stdint.h>
40 #include <stdbool.h>
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 
47 /***************************************************************************//**
48  * @addtogroup EM_Library
49  * @{
50  ******************************************************************************/
51 
52 /***************************************************************************//**
53  * @addtogroup LESENSE
54  * @{
55  ******************************************************************************/
56 
57 /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
58 
59 
60 
61 /** @endcond */
62 
63 /*******************************************************************************
64  ********************************   ENUMS   ************************************
65  ******************************************************************************/
66 
67 /** Clock divisors for controlling the prescaling factor of the period
68  *  counter.
69  *  Note: these enumeration values are being used for different clock division
70  *  related configuration parameters (hfPresc, lfPresc, pcPresc). */
71 typedef enum
72 {
73   lesenseClkDiv_1   = 0, /**< Divide clock by 1. */
74   lesenseClkDiv_2   = 1, /**< Divide clock by 2. */
75   lesenseClkDiv_4   = 2, /**< Divide clock by 4. */
76   lesenseClkDiv_8   = 3, /**< Divide clock by 8. */
77   lesenseClkDiv_16  = 4, /**< Divide clock by 16. */
78   lesenseClkDiv_32  = 5, /**< Divide clock by 32. */
79   lesenseClkDiv_64  = 6, /**< Divide clock by 64. */
80   lesenseClkDiv_128 = 7  /**< Divide clock by 128. */
81 } LESENSE_ClkPresc_TypeDef;
82 
83 
84 /** Scan modes. */
85 typedef enum
86 {
87   /** New scan is started each time the period counter overflows. */
88   lesenseScanStartPeriodic = LESENSE_CTRL_SCANMODE_PERIODIC,
89 
90   /** Single scan is performed when LESENSE_ScanStart() is called. */
91   lesenseScanStartOneShot  = LESENSE_CTRL_SCANMODE_ONESHOT,
92 
93   /** New scan is triggered by pulse on PRS channel. */
94   lesenseScanStartPRS      = LESENSE_CTRL_SCANMODE_PRS
95 } LESENSE_ScanMode_TypeDef;
96 
97 
98 /** PRS sources.
99  *  Note: these enumeration values are being used for different PRS related
100  *  configuration parameters. */
101 typedef enum
102 {
103   lesensePRSCh0     = 0, /**< PRS channel 0. */
104   lesensePRSCh1     = 1, /**< PRS channel 1. */
105   lesensePRSCh2     = 2, /**< PRS channel 2. */
106   lesensePRSCh3     = 3, /**< PRS channel 3. */
107   lesensePRSCh4     = 4, /**< PRS channel 4. */
108   lesensePRSCh5     = 5, /**< PRS channel 5. */
109   lesensePRSCh6     = 6, /**< PRS channel 6. */
110   lesensePRSCh7     = 7  /**< PRS channel 7. */
111 } LESENSE_PRSSel_TypeDef;
112 
113 
114 /** Locations of the alternate excitation function. */
115 typedef enum
116 {
117   /** Alternate excitation is mapped to the LES_ALTEX pins. */
118   lesenseAltExMapALTEX = LESENSE_CTRL_ALTEXMAP_ALTEX,
119 
120   /** Alternate excitation is mapped to the pins of the other ACMP. */
121   lesenseAltExMapACMP  = LESENSE_CTRL_ALTEXMAP_ACMP
122 } LESENSE_AltExMap_TypeDef;
123 
124 
125 /** Result buffer interrupt and DMA trigger levels. */
126 typedef enum
127 {
128   /** DMA and interrupt flags are set when result buffer is halffull. */
129   lesenseBufTrigHalf = LESENSE_CTRL_BUFIDL_HALFFULL,
130 
131   /** DMA and interrupt flags set when result buffer is full. */
132   lesenseBufTrigFull = LESENSE_CTRL_BUFIDL_FULL
133 } LESENSE_BufTrigLevel_TypeDef;
134 
135 
136 /** Modes of operation for DMA wakeup from EM2. */
137 typedef enum
138 {
139   /** No DMA wakeup from EM2. */
140   lesenseDMAWakeUpDisable  = LESENSE_CTRL_DMAWU_DISABLE,
141 
142   /** DMA wakeup from EM2 when data is valid in the result buffer. */
143   lesenseDMAWakeUpBufValid = LESENSE_CTRL_DMAWU_BUFDATAV,
144 
145   /** DMA wakeup from EM2 when the resultbuffer is full/halffull, depending on
146    *  RESBIDL configuration in LESENSE_CTRL register (selected by
147    *  resBufTrigLevel in LESENSE_ResBufTrigLevel_TypeDef descriptor structure). */
148   lesenseDMAWakeUpBufLevel = LESENSE_CTRL_DMAWU_BUFLEVEL
149 } LESENSE_DMAWakeUp_TypeDef;
150 
151 
152 /** Bias modes. */
153 typedef enum
154 {
155   /** Duty cycle bias module between low power and high accuracy mode. */
156   lesenseBiasModeDutyCycle = LESENSE_BIASCTRL_BIASMODE_DUTYCYCLE,
157 
158   /** Bias module is always in high accuracy mode. */
159   lesenseBiasModeHighAcc   = LESENSE_BIASCTRL_BIASMODE_HIGHACC,
160 
161   /** Bias module is controlled by the EMU and not affected by LESENSE. */
162   lesenseBiasModeDontTouch = LESENSE_BIASCTRL_BIASMODE_DONTTOUCH
163 } LESENSE_BiasMode_TypeDef;
164 
165 
166 /** Scan configuration. */
167 typedef enum
168 {
169   /** The channel configuration registers (CHx_CONF) used are directly mapped to
170    *  the channel number. */
171   lesenseScanConfDirMap = LESENSE_CTRL_SCANCONF_DIRMAP,
172 
173   /** The channel configuration registers used are CHx+8_CONF for channels 0-7
174    *  and CHx-8_CONF for channels 8-15. */
175   lesenseScanConfInvMap = LESENSE_CTRL_SCANCONF_INVMAP,
176 
177   /** The channel configuration registers used toggles between CHX_SCANCONF and
178    *  CHX+8_SCANCONF when channel x triggers. */
179   lesenseScanConfToggle = LESENSE_CTRL_SCANCONF_TOGGLE,
180 
181   /** The decoder state defines the channel configuration register (CHx_CONF) to
182    *  be used. */
183   lesenseScanConfDecDef = LESENSE_CTRL_SCANCONF_DECDEF
184 } LESENSE_ScanConfSel_TypeDef;
185 
186 
187 /** DAC CHx data control configuration. */
188 typedef enum
189 {
190   /** DAC channel x data is defined by DAC_CHxDATA register.
191    *  Note: this value could be used for both DAC Ch0 and Ch1. */
192   lesenseDACIfData = _LESENSE_PERCTRL_DACCH0DATA_DACDATA,
193 
194   /** DAC channel x data is defined by ACMPTHRES in LESENSE_CHx_INTERACT.
195    *  Note: this value could be used for both DAC Ch0 and Ch1. */
196   lesenseACMPThres = _LESENSE_PERCTRL_DACCH0DATA_ACMPTHRES
197 } LESENSE_ControlDACData_TypeDef;
198 
199 
200 /** DAC channel x conversion mode configuration. */
201 typedef enum
202 {
203   /** LESENSE doesn't control DAC channel x.
204    *  Note: this value could be used for both DAC Ch0 and Ch1. */
205   lesenseDACConvModeDisable    = _LESENSE_PERCTRL_DACCH0CONV_DISABLE,
206 
207   /** DAC channel x is driven in continuous mode.
208    *  Note: this value could be used for both DAC Ch0 and Ch1. */
209   lesenseDACConvModeContinuous = _LESENSE_PERCTRL_DACCH0CONV_CONTINUOUS,
210 
211   /** DAC channel x is driven in sample hold mode.
212    *  Note: this value could be used for both DAC Ch0 and Ch1. */
213   lesenseDACConvModeSampleHold = _LESENSE_PERCTRL_DACCH0CONV_SAMPLEHOLD,
214 
215   /** DAC channel x is driven in sample off mode.
216    *  Note: this value could be used for both DAC Ch0 and Ch1. */
217   lesenseDACConvModeSampleOff  = _LESENSE_PERCTRL_DACCH0CONV_SAMPLEOFF
218 } LESENSE_ControlDACConv_TypeDef;
219 
220 
221 /** DAC channel x output mode configuration. */
222 typedef enum
223 {
224   /** DAC CHx output to pin and ACMP/ADC disabled.
225    *  Note: this value could be used for both DAC Ch0 and Ch1. */
226   lesenseDACOutModeDisable    = _LESENSE_PERCTRL_DACCH0OUT_DISABLE,
227 
228   /** DAC CHx output to pin enabled, output to ADC and ACMP disabled.
229    *  Note: this value could be used for both DAC Ch0 and Ch1. */
230   lesenseDACOutModePin        = _LESENSE_PERCTRL_DACCH0OUT_PIN,
231 
232   /** DAC CHx output to pin disabled, output to ADC and ACMP enabled.
233    *  Note: this value could be used for both DAC Ch0 and Ch1. */
234   lesenseDACOutModeADCACMP    = _LESENSE_PERCTRL_DACCH0OUT_ADCACMP,
235 
236   /** DAC CHx output to pin, ADC, and ACMP enabled.
237    *  Note: this value could be used for both DAC Ch0 and Ch1. */
238   lesenseDACOutModePinADCACMP = _LESENSE_PERCTRL_DACCH0OUT_PINADCACMP
239 } LESENSE_ControlDACOut_TypeDef;
240 
241 
242 /**  DAC reference configuration. */
243 typedef enum
244 {
245   /** DAC uses VDD reference. */
246   lesenseDACRefVdd     = LESENSE_PERCTRL_DACREF_VDD,
247 
248   /** DAC uses bandgap reference. */
249   lesenseDACRefBandGap = LESENSE_PERCTRL_DACREF_BANDGAP
250 } LESENSE_DACRef_TypeDef;
251 
252 
253 /** ACMPx control configuration. */
254 typedef enum
255 {
256   /** LESENSE does not control the ACMPx.
257    *  Note: this value could be used for both ACMP0 and ACMP1. */
258   lesenseACMPModeDisable  = _LESENSE_PERCTRL_ACMP0MODE_DISABLE,
259 
260   /** LESENSE controls the input mux of ACMPx.
261    *  Note: this value could be used for both ACMP0 and ACMP1. */
262   lesenseACMPModeMux      = _LESENSE_PERCTRL_ACMP0MODE_MUX,
263 
264   /** LESENSE controls the input mux of and the threshold value of ACMPx.
265    *  Note: this value could be used for both ACMP0 and ACMP1. */
266   lesenseACMPModeMuxThres = _LESENSE_PERCTRL_ACMP0MODE_MUXTHRES
267 } LESENSE_ControlACMP_TypeDef;
268 
269 
270 /** Warm up modes. ACMP and DAC duty cycle mode configuration. */
271 typedef enum
272 {
273   /** ACMPs and DACs are shut down when LESENSE is idle. */
274   lesenseWarmupModeNormal   = LESENSE_PERCTRL_WARMUPMODE_NORMAL,
275 
276   /** ACMPs are kept powered up when LESENSE is idle. */
277   lesenseWarmupModeACMP     = LESENSE_PERCTRL_WARMUPMODE_KEEPACMPWARM,
278 
279   /** The DAC is kept powered up when LESENSE is idle. */
280   lesenseWarmupModeDAC      = LESENSE_PERCTRL_WARMUPMODE_KEEPDACWARM,
281 
282   /** ACMPs and the DAC are kept powered up when LESENSE is idle. */
283   lesenseWarmupModeKeepWarm = LESENSE_PERCTRL_WARMUPMODE_KEEPACMPDACWARM
284 } LESENSE_WarmupMode_TypeDef;
285 
286 
287 /** Decoder input source configuration. */
288 typedef enum
289 {
290   /** The SENSORSTATE register is used as input to the decoder. */
291   lesenseDecInputSensorSt = LESENSE_DECCTRL_INPUT_SENSORSTATE,
292 
293   /** PRS channels are used as input to the decoder. */
294   lesenseDecInputPRS      = LESENSE_DECCTRL_INPUT_PRS
295 } LESENSE_DecInput_TypeDef;
296 
297 
298 /** Compare source selection for sensor sampling. */
299 typedef enum
300 {
301   /** Counter output will be used in comparison. */
302   lesenseSampleModeCounter = LESENSE_CH_INTERACT_SAMPLE_COUNTER,
303 
304   /** ACMP output will be used in comparison. */
305   lesenseSampleModeACMP    = LESENSE_CH_INTERACT_SAMPLE_ACMP
306 } LESENSE_ChSampleMode_TypeDef;
307 
308 
309 /** Interrupt generation setup for CHx interrupt flag. */
310 typedef enum
311 {
312   /** No interrupt is generated. */
313   lesenseSetIntNone    = LESENSE_CH_INTERACT_SETIF_NONE,
314 
315   /** Set interrupt flag if the sensor triggers. */
316   lesenseSetIntLevel   = LESENSE_CH_INTERACT_SETIF_LEVEL,
317 
318   /** Set interrupt flag on positive edge of the sensor state. */
319   lesenseSetIntPosEdge = LESENSE_CH_INTERACT_SETIF_POSEDGE,
320 
321   /** Set interrupt flag on negative edge of the sensor state. */
322   lesenseSetIntNegEdge = LESENSE_CH_INTERACT_SETIF_NEGEDGE
323 } LESENSE_ChIntMode_TypeDef;
324 
325 
326 /** Channel pin mode for the excitation phase of the scan sequence. */
327 typedef enum
328 {
329   /** Channel pin is disabled. */
330   lesenseChPinExDis    = LESENSE_CH_INTERACT_EXMODE_DISABLE,
331 
332   /** Channel pin is configured as push-pull, driven HIGH. */
333   lesenseChPinExHigh   = LESENSE_CH_INTERACT_EXMODE_HIGH,
334 
335   /** Channel pin is configured as push-pull, driven LOW. */
336   lesenseChPinExLow    = LESENSE_CH_INTERACT_EXMODE_LOW,
337 
338   /** DAC output (only available on channel 0, 1, 2, 3, 12, 13, 14 and 15) */
339   lesenseChPinExDACOut = LESENSE_CH_INTERACT_EXMODE_DACOUT
340 } LESENSE_ChPinExMode_TypeDef;
341 
342 
343 /** Channel pin mode for the idle phase of the scan sequence. */
344 typedef enum
345 {
346   /** Channel pin is disabled in idle phase.
347    *  Note: this value could be used for all channels. */
348   lesenseChPinIdleDis    = _LESENSE_IDLECONF_CH0_DISABLE,
349 
350   /** Channel pin is configured as push-pull, driven HIGH in idle phase.
351    *  Note: this value could be used for all channels. */
352   lesenseChPinIdleHigh   = _LESENSE_IDLECONF_CH0_HIGH,
353 
354   /** Channel pin is configured as push-pull, driven LOW in idle phase.
355    *  Note: this value could be used for all channels. */
356   lesenseChPinIdleLow    = _LESENSE_IDLECONF_CH0_LOW,
357 
358   /** Channel pin is connected to DAC CH0 output in idle phase.
359    *  Note: only applies to channel 0, 1, 2, 3. */
360   lesenseChPinIdleDACCh0 = _LESENSE_IDLECONF_CH0_DACCH0,
361 
362   /** Channel pin is connected to DAC CH1 output in idle phase.
363    *  Note: only applies to channel 12, 13, 14, 15. */
364   lesenseChPinIdleDACCh1 = _LESENSE_IDLECONF_CH12_DACCH1
365 } LESENSE_ChPinIdleMode_TypeDef;
366 
367 
368 /** Clock used for excitation and sample delay timing. */
369 typedef enum
370 {
371   /** LFACLK (LF clock) is used. */
372   lesenseClkLF = _LESENSE_CH_INTERACT_EXCLK_LFACLK,
373 
374   /** AUXHFRCO (HF clock) is used. */
375   lesenseClkHF = _LESENSE_CH_INTERACT_EXCLK_AUXHFRCO
376 } LESENSE_ChClk_TypeDef;
377 
378 
379 /** Compare modes for counter comparison. */
380 typedef enum
381 {
382   /** Set interrupt flag if counter value is less than CTRTHRESHOLD, or if the
383    *  ACMP output is 0. */
384   lesenseCompModeLess        = LESENSE_CH_EVAL_COMP_LESS,
385 
386   /** Set interrupt flag if counter value is greater than, or equal to
387    *  CTRTHRESHOLD, or if the ACMP output is 1. */
388   lesenseCompModeGreaterOrEq = LESENSE_CH_EVAL_COMP_GE
389 } LESENSE_ChCompMode_TypeDef;
390 
391 
392 /** Idle phase configuration of alternate excitation channels. */
393 typedef enum
394 {
395   /** ALTEX output is disabled in idle phase.
396    *  Note: this value could be used for all alternate excitation channels. */
397   lesenseAltExPinIdleDis  = _LESENSE_ALTEXCONF_IDLECONF0_DISABLE,
398 
399   /** ALTEX output is high in idle phase.
400    *  Note: this value could be used for all alternate excitation channels. */
401   lesenseAltExPinIdleHigh = _LESENSE_ALTEXCONF_IDLECONF0_HIGH,
402 
403   /** ALTEX output is low in idle phase.
404    *  Note: this value could be used for all alternate excitation channels. */
405   lesenseAltExPinIdleLow  = _LESENSE_ALTEXCONF_IDLECONF0_LOW
406 } LESENSE_AltExPinIdle_TypeDef;
407 
408 
409 /** Transition action modes. */
410 typedef enum
411 {
412   /** No PRS pulses generated (if PRSCOUNT == 0).
413    *  Do not count (if PRSCOUNT == 1). */
414   lesenseTransActNone        = LESENSE_ST_TCONFA_PRSACT_NONE,
415 
416   /** Generate pulse on LESPRS0 (if PRSCOUNT == 0). */
417   lesenseTransActPRS0        = LESENSE_ST_TCONFA_PRSACT_PRS0,
418 
419   /** Generate pulse on LESPRS1 (if PRSCOUNT == 0). */
420   lesenseTransActPRS1        = LESENSE_ST_TCONFA_PRSACT_PRS1,
421 
422   /** Generate pulse on LESPRS0 and LESPRS1 (if PRSCOUNT == 0). */
423   lesenseTransActPRS01       = LESENSE_ST_TCONFA_PRSACT_PRS01,
424 
425   /** Generate pulse on LESPRS2 (for both PRSCOUNT == 0 and PRSCOUNT == 1). */
426   lesenseTransActPRS2        = LESENSE_ST_TCONFA_PRSACT_PRS2,
427 
428   /** Generate pulse on LESPRS0 and LESPRS2 (if PRSCOUNT == 0). */
429   lesenseTransActPRS02       = LESENSE_ST_TCONFA_PRSACT_PRS02,
430 
431   /** Generate pulse on LESPRS1 and LESPRS2 (if PRSCOUNT == 0). */
432   lesenseTransActPRS12       = LESENSE_ST_TCONFA_PRSACT_PRS12,
433 
434   /** Generate pulse on LESPRS0, LESPRS1 and LESPRS2  (if PRSCOUNT == 0). */
435   lesenseTransActPRS012      = LESENSE_ST_TCONFA_PRSACT_PRS012,
436 
437   /** Count up (if PRSCOUNT == 1). */
438   lesenseTransActUp          = LESENSE_ST_TCONFA_PRSACT_UP,
439 
440   /** Count down (if PRSCOUNT == 1). */
441   lesenseTransActDown        = LESENSE_ST_TCONFA_PRSACT_DOWN,
442 
443   /** Count up and generate pulse on LESPRS2 (if PRSCOUNT == 1). */
444   lesenseTransActUpAndPRS2   = LESENSE_ST_TCONFA_PRSACT_UPANDPRS2,
445 
446   /** Count down and generate pulse on LESPRS2 (if PRSCOUNT == 1). */
447   lesenseTransActDownAndPRS2 = LESENSE_ST_TCONFA_PRSACT_DOWNANDPRS2
448 } LESENSE_StTransAct_TypeDef;
449 
450 
451 /*******************************************************************************
452  *******************************   STRUCTS   ***********************************
453  ******************************************************************************/
454 
455 /** Core control (LESENSE_CTRL) descriptor structure. */
456 typedef struct
457 {
458   /** Select scan start mode to control how the scan start is being triggered.*/
459   LESENSE_ScanMode_TypeDef     scanStart;
460 
461   /** Select PRS source for scan start if scanMode is set to lesensePrsPulse. */
462   LESENSE_PRSSel_TypeDef       prsSel;
463 
464   /** Select scan configuration register usage strategy. */
465   LESENSE_ScanConfSel_TypeDef  scanConfSel;
466 
467   /** Set to true to invert ACMP0 output. */
468   bool                         invACMP0;
469 
470   /** Set to true to invert ACMP1 output. */
471   bool                         invACMP1;
472 
473   /** Set to true to sample both ACMPs simultaneously. */
474   bool                         dualSample;
475 
476   /** Set to true in order to to store SCANRES in RAM (accessible via RESDATA)
477    *  after each scan. */
478   bool                         storeScanRes;
479 
480   /** Set to true in order to always make LESENSE write to the result buffer,
481    *  even if it is full. */
482   bool                         bufOverWr;
483 
484   /** Select trigger conditions for interrupt and DMA. */
485   LESENSE_BufTrigLevel_TypeDef bufTrigLevel;
486 
487   /** Configure trigger condition for DMA wakeup from EM2. */
488   LESENSE_DMAWakeUp_TypeDef    wakeupOnDMA;
489 
490   /** Select bias mode. */
491   LESENSE_BiasMode_TypeDef     biasMode;
492 
493   /** Set to true to keep LESENSE running in debug mode. */
494   bool                         debugRun;
495 } LESENSE_CoreCtrlDesc_TypeDef;
496 
497 /** Default configuration for LESENSE_CtrlDesc_TypeDef structure. */
498 #define LESENSE_CORECTRL_DESC_DEFAULT                                                                 \
499   {                                                                                                   \
500     lesenseScanStartPeriodic,  /* Start new scan each time the period counter overflows. */           \
501     lesensePRSCh0,             /* Default PRS channel is selected. */                                 \
502     lesenseScanConfDirMap,     /* Direct mapping SCANCONF register usage strategy. */                 \
503     false,                     /* Don't invert ACMP0 output. */                                       \
504     false,                     /* Don't invert ACMP1 output. */                                       \
505     false,                     /* Disable dual sampling. */                                           \
506     true,                      /* Store scan result after each scan. */                               \
507     true,                      /* Overwrite result buffer register even if it is full. */             \
508     lesenseBufTrigHalf,        /* Trigger interrupt and DMA request if result buffer is half full. */ \
509     lesenseDMAWakeUpDisable,   /* Don't wake up on DMA from EM2. */                                   \
510     lesenseBiasModeDontTouch,  /* Don't touch bias configuration. */                                  \
511     true                       /* Keep LESENSE running in debug mode. */                              \
512   }
513 
514 
515 /** LESENSE timing control descriptor structure. */
516 typedef struct
517 {
518   /** Set the number of LFACLK cycles to delay sensor interaction on
519    *  each channel. Valid range: 0-3 (2 bit). */
520   uint8_t startDelay;
521 } LESENSE_TimeCtrlDesc_TypeDef;
522 
523 /** Default configuration for LESENSE_TimeCtrlDesc_TypeDef structure. */
524 #define LESENSE_TIMECTRL_DESC_DEFAULT    0U
525 
526 
527 /** LESENSE peripheral control descriptor structure. */
528 typedef struct
529 {
530   /** Configure DAC channel 0 data control. */
531   LESENSE_ControlDACData_TypeDef dacCh0Data;
532 
533   /** Configure how LESENSE controls conversion on DAC channel 0. */
534   LESENSE_ControlDACConv_TypeDef dacCh0ConvMode;
535 
536   /** Configure how LESENSE controls output on DAC channel 0. */
537   LESENSE_ControlDACOut_TypeDef  dacCh0OutMode;
538 
539   /** Configure DAC channel 1 data control. */
540   LESENSE_ControlDACData_TypeDef dacCh1Data;
541 
542   /** Configure how LESENSE controls conversion on DAC channel 1. */
543   LESENSE_ControlDACConv_TypeDef dacCh1ConvMode;
544 
545   /** Configure how LESENSE controls output on DAC channel 1. */
546   LESENSE_ControlDACOut_TypeDef  dacCh1OutMode;
547 
548   /** Configure the prescaling factor for the LESENSE - DAC interface.
549    *  Valid range: 0-31 (5bit). */
550   uint8_t                        dacPresc;
551 
552   /** Configure the DAC reference to be used. Set to #lesenseDACRefVdd to use
553    *  VDD and set to #lesenseDACRefBandGap to use bandgap as reference. */
554   LESENSE_DACRef_TypeDef         dacRef;
555 
556   /** Configure how LESENSE controls ACMP 0. */
557   LESENSE_ControlACMP_TypeDef    acmp0Mode;
558 
559   /** Configure how LESENSE controls ACMP 1. */
560   LESENSE_ControlACMP_TypeDef    acmp1Mode;
561 
562   /** Configure how LESENSE controls ACMPs and the DAC in idle mode. */
563   LESENSE_WarmupMode_TypeDef     warmupMode;
564 } LESENSE_PerCtrlDesc_TypeDef;
565 
566 /** Default configuration for LESENSE_PerCtrl_TypeDef structure. */
567 #define LESENSE_PERCTRL_DESC_DEFAULT \
568   {                                  \
569     lesenseDACIfData,          /**/  \
570     lesenseDACConvModeDisable, /**/  \
571     lesenseDACOutModeDisable,  /**/  \
572     lesenseDACIfData,          /**/  \
573     lesenseDACConvModeDisable, /**/  \
574     lesenseDACOutModeDisable,  /**/  \
575     0U,                        /**/  \
576     lesenseDACRefVdd,          /**/  \
577     lesenseACMPModeMuxThres,   /**/  \
578     lesenseACMPModeMuxThres,   /**/  \
579     lesenseWarmupModeKeepWarm, /**/  \
580   }
581 
582 
583 /** LESENSE decoder control descriptor structure. */
584 typedef struct
585 {
586   /** Select the input to the LESENSE decoder. */
587   LESENSE_DecInput_TypeDef decInput;
588 
589   /** Initial state of the LESENSE decoder. */
590   uint32_t                 initState;
591 
592   /** Set to enable the decoder to check the present state in addition
593    *  to the states defined in DECCONF. */
594   bool                     chkState;
595 
596   /** When set, a transition from state x in the decoder will set interrupt flag
597    *  CHx. */
598   bool                     intMap;
599 
600   /** Set to enable hysteresis in the decoder for suppressing changes on PRS
601    *  channel 0. */
602   bool                     hystPRS0;
603 
604   /** Set to enable hysteresis in the decoder for suppressing changes on PRS
605    *  channel 1. */
606   bool                     hystPRS1;
607 
608   /** Set to enable hysteresis in the decoder for suppressing changes on PRS
609    *  channel 2. */
610   bool                     hystPRS2;
611 
612   /** Set to enable hysteresis in the decoder for suppressing interrupt
613    *  requests. */
614   bool                     hystIRQ;
615 
616   /** Set to enable count mode on decoder PRS channels 0 and 1 to produce
617    *  outputs which can be used by a PCNT to count up or down. */
618   bool                     prsCount;
619 
620   /** Select PRS channel input for bit 0 of the LESENSE decoder. */
621   LESENSE_PRSSel_TypeDef   prsChSel0;
622 
623   /** Select PRS channel input for bit 1 of the LESENSE decoder. */
624   LESENSE_PRSSel_TypeDef   prsChSel1;
625 
626   /** Select PRS channel input for bit 2 of the LESENSE decoder. */
627   LESENSE_PRSSel_TypeDef   prsChSel2;
628 
629   /** Select PRS channel input for bit 3 of the LESENSE decoder. */
630   LESENSE_PRSSel_TypeDef   prsChSel3;
631 } LESENSE_DecCtrlDesc_TypeDef;
632 
633 /** Default configuration for LESENSE_PerCtrl_TypeDef structure. */
634 #define LESENSE_DECCTRL_DESC_DEFAULT \
635   {                                  \
636     lesenseDecInputSensorSt, /**/    \
637     0U,                      /**/    \
638     false,                   /**/    \
639     true,                    /**/    \
640     true,                    /**/    \
641     true,                    /**/    \
642     true,                    /**/    \
643     true,                    /**/    \
644     false,                   /**/    \
645     lesensePRSCh0,           /**/    \
646     lesensePRSCh1,           /**/    \
647     lesensePRSCh2,           /**/    \
648     lesensePRSCh3,           /**/    \
649   }
650 
651 
652 /** LESENSE module initialization structure. */
653 typedef struct
654 {
655   /** LESENSE core configuration parameters. */
656   LESENSE_CoreCtrlDesc_TypeDef coreCtrl;
657 
658   /** LESENSE timing configuration parameters. */
659   LESENSE_TimeCtrlDesc_TypeDef timeCtrl;
660 
661   /** LESENSE peripheral configuration parameters. */
662   LESENSE_PerCtrlDesc_TypeDef  perCtrl;
663 
664   /** LESENSE decoder configuration parameters. */
665   LESENSE_DecCtrlDesc_TypeDef  decCtrl;
666 } LESENSE_Init_TypeDef;
667 
668 /** Default configuration for LESENSE_Init_TypeDef structure. */
669 #define LESENSE_INIT_DEFAULT                        \
670   {                                                 \
671     .coreCtrl = LESENSE_CORECTRL_DESC_DEFAULT, /* Default core control parameters. */ \
672     .timeCtrl = LESENSE_TIMECTRL_DESC_DEFAULT, /* Default time control parameters. */ \
673     .perCtrl  = LESENSE_PERCTRL_DESC_DEFAULT,  /* Default peripheral control parameters. */ \
674     .decCtrl  = LESENSE_DECCTRL_DESC_DEFAULT   /* Default decoder control parameters. */ \
675   }
676 
677 
678 /** Channel descriptor structure. */
679 typedef struct
680 {
681   /** Set to enable scan channel CHx. */
682   bool                          enaScanCh;
683 
684   /** Set to enable CHx pin. */
685   bool                          enaPin;
686 
687   /** Enable/disable channel interrupts after configuring all the sensor channel
688    *  parameters. */
689   bool                          enaInt;
690 
691   /** Configure channel pin mode for the excitation phase of the scan sequence.
692    *  Note: OPAOUT is only available on channels 2, 3, 4, and 5. */
693   LESENSE_ChPinExMode_TypeDef   chPinExMode;
694 
695   /** Configure channel pin idle setup in LESENSE idle phase. */
696   LESENSE_ChPinIdleMode_TypeDef chPinIdleMode;
697 
698   /** Set to use alternate excite pin for excitation. */
699   bool                          useAltEx;
700 
701   /** Set to enable the result from this channel being shifted into the decoder
702    *  register. */
703   bool                          shiftRes;
704 
705   /** Set to invert the result bit stored in SCANRES register. */
706   bool                          invRes;
707 
708   /** Set to store the counter value in RAM (accessible via RESDATA) and make
709    *  the comparison result available in the SCANRES register. */
710   bool                          storeCntRes;
711 
712   /** Select clock used for excitation timing. */
713   LESENSE_ChClk_TypeDef         exClk;
714 
715   /** Select clock used for sample delay timing. */
716   LESENSE_ChClk_TypeDef         sampleClk;
717 
718   /** Configure excitation time. Excitation will last exTime+1 excitation clock
719    *  cycles. Valid range: 0-63 (6 bits). */
720   uint8_t                       exTime;
721 
722   /** Configure sample delay. Sampling will occur after sampleDelay+1 sample
723    *  clock cycles. Valid range: 0-127 (7 bits). */
724   uint8_t                       sampleDelay;
725 
726   /** Configure measure delay. Sensor measuring is delayed for measDelay
727    *  excitation clock cycles. Valid range: 0-127 (7 bits). */
728   uint8_t                       measDelay;
729 
730   /** Configure ACMP threshold.
731    *  If perCtrl.dacCh0Data or perCtrl.dacCh1Data is set to #lesenseDACIfData,
732    *  acmpThres defines the 12-bit DAC data in the corresponding data register
733    *  of the DAC interface (DACn_CH0DATA and DACn_CH1DATA).
734    *  In this case, the valid range is: 0-4095 (12 bits).
735    *  If perCtrl.dacCh0Data or perCtrl.dacCh1Data is set to #lesenseACMPThres,
736    *  acmpThres defines the 6-bit Vdd scaling factor of ACMP negative input
737    *  (VDDLEVEL in ACMP_INPUTSEL register).
738    *  In this case, the valid range is: 0-63 (6 bits). */
739   uint16_t                     acmpThres;
740 
741   /** Select if ACMP output or counter output should be used in comparison. */
742   LESENSE_ChSampleMode_TypeDef sampleMode;
743 
744   /** Configure interrupt generation mode for CHx interrupt flag. */
745   LESENSE_ChIntMode_TypeDef    intMode;
746 
747   /** Configure decision threshold for counter comparison.
748    *  Valid range: 0-65535 (16 bits). */
749   uint16_t                     cntThres;
750 
751   /** Select mode for counter comparison. */
752   LESENSE_ChCompMode_TypeDef   compMode;
753 } LESENSE_ChDesc_TypeDef;
754 
755 
756 /** Configuration structure for all scan channels. */
757 typedef struct
758 {
759   /** Channel descriptor for all 16 channels. */
760   LESENSE_ChDesc_TypeDef Ch[16];
761 } LESENSE_ChAll_TypeDef;
762 
763 /** Default configuration for scan channel. */
764 #define LESENSE_CH_CONF_DEFAULT                                                                       \
765   {                                                                                                   \
766     true,                  /* Enable scan channel. */                                                 \
767     true,                  /* Enable the assigned pin on scan channel. */                             \
768     true,                  /* Enable interrupts on channel. */                                        \
769     lesenseChPinExHigh,    /* Channel pin is high during the excitation period. */                    \
770     lesenseChPinIdleLow,   /* Channel pin is low during the idle period. */                           \
771     false,                 /* Don't use alternate excitation pins for excitation. */                  \
772     false,                 /* Disabled to shift results from this channel to the decoder register. */ \
773     false,                 /* Disabled to invert the scan result bit. */                              \
774     false,                 /* Disabled to store counter value in the result buffer. */                \
775     lesenseClkLF,          /* Use the LF clock for excitation timing. */                              \
776     lesenseClkLF,          /* Use the LF clock for sample timing. */                                  \
777     0x03U,                 /* Excitation time is set to 3(+1) excitation clock cycles. */             \
778     0x09U,                 /* Sample delay is set to 9(+1) sample clock cycles. */                    \
779     0x06U,                 /* Measure delay is set to 6 excitation clock cycles.*/                    \
780     0x00U,                 /* ACMP threshold has been set to 0. */                                    \
781     lesenseSampleModeACMP, /* ACMP output will be used in comparison. */                              \
782     lesenseSetIntNone,     /* No interrupt is generated by the channel. */                            \
783     0xFFU,                 /* Counter threshold has bee set to 0xFF. */                               \
784     lesenseCompModeLess    /* Compare mode has been set to trigger interrupt on "less". */            \
785   }
786 
787 /** Default configuration for all sensor channels. */
788 #define LESENSE_SCAN_CONF_DEFAULT                     \
789   {                                                   \
790     {                                                 \
791       LESENSE_CH_CONF_DEFAULT, /* Scan channel 0. */  \
792       LESENSE_CH_CONF_DEFAULT, /* Scan channel 1. */  \
793       LESENSE_CH_CONF_DEFAULT, /* Scan channel 2. */  \
794       LESENSE_CH_CONF_DEFAULT, /* Scan channel 3. */  \
795       LESENSE_CH_CONF_DEFAULT, /* Scan channel 4. */  \
796       LESENSE_CH_CONF_DEFAULT, /* Scan channel 5. */  \
797       LESENSE_CH_CONF_DEFAULT, /* Scan channel 6. */  \
798       LESENSE_CH_CONF_DEFAULT, /* Scan channel 7. */  \
799       LESENSE_CH_CONF_DEFAULT, /* Scan channel 8. */  \
800       LESENSE_CH_CONF_DEFAULT, /* Scan channel 9. */  \
801       LESENSE_CH_CONF_DEFAULT, /* Scan channel 10. */ \
802       LESENSE_CH_CONF_DEFAULT, /* Scan channel 11. */ \
803       LESENSE_CH_CONF_DEFAULT, /* Scan channel 12. */ \
804       LESENSE_CH_CONF_DEFAULT, /* Scan channel 13. */ \
805       LESENSE_CH_CONF_DEFAULT, /* Scan channel 14. */ \
806       LESENSE_CH_CONF_DEFAULT, /* Scan channel 15. */ \
807     }                                                 \
808   }
809 
810 
811 /** Alternate excitation descriptor structure. */
812 typedef struct
813 {
814   /** Configure alternate excitation pins. If set, the corresponding alternate
815    *  excitation pin is enabled. */
816   bool                         enablePin;
817 
818   /** Configure idle phase setup of alternate excitation pins. */
819   LESENSE_AltExPinIdle_TypeDef idleConf;
820 
821   /** Configure how to control the external alternate excitation pins. Only
822   *  applies if altExMap has been set to lesenseAltExMapALTEX.
823   *  If true, the excitation happens on the corresponding alternate excitation
824   *  pin during the excitation periods of all enabled channels.
825   *  If false, the excitation happens on the corresponding alternate excitation
826   *  pin ONLY during the excitation period of the corresponding channel. */
827   bool                         alwaysEx;
828 } LESENSE_AltExDesc_TypeDef;
829 
830 
831 /** Configuration structure for alternate excitation. */
832 typedef struct
833 {
834   /** Select alternate excitation mapping. */
835   LESENSE_AltExMap_TypeDef  altExMap;
836 
837   /** Alternate excitation channel descriptors. */
838   LESENSE_AltExDesc_TypeDef AltEx[8];
839 } LESENSE_ConfAltEx_TypeDef;
840 
841 /** Default configuration for alternate excitation channel. */
842 #define LESENSE_ALTEX_CH_CONF_DEFAULT                                          \
843   {                                                                            \
844     true,                  /* Alternate excitation enabled.*/                  \
845     lesenseAltExIdleDis,   /* Alternate excitation pin is disabled in idle. */ \
846     false                  /* Excite only for corresponding channel. */        \
847   }
848 
849 /** Default configuration for all alternate excitation channels. */
850 #define LESENSE_ALTEX_CONF_DEFAULT                                         \
851   {                                                                        \
852     lesenseAltExMapACMP,                                                   \
853     {                                                                      \
854       LESENSE_ALTEX_CH_CONF_DEFAULT, /* Alternate excitation channel 0. */ \
855       LESENSE_ALTEX_CH_CONF_DEFAULT, /* Alternate excitation channel 1. */ \
856       LESENSE_ALTEX_CH_CONF_DEFAULT, /* Alternate excitation channel 2. */ \
857       LESENSE_ALTEX_CH_CONF_DEFAULT, /* Alternate excitation channel 3. */ \
858       LESENSE_ALTEX_CH_CONF_DEFAULT, /* Alternate excitation channel 4. */ \
859       LESENSE_ALTEX_CH_CONF_DEFAULT, /* Alternate excitation channel 5. */ \
860       LESENSE_ALTEX_CH_CONF_DEFAULT, /* Alternate excitation channel 6. */ \
861       LESENSE_ALTEX_CH_CONF_DEFAULT  /* Alternate excitation channel 7. */ \
862     }                                                                      \
863   }
864 
865 
866 /** Decoder state condition descriptor structure. */
867 typedef struct
868 {
869   /** Configure compare value. State transition is triggered when sensor state
870    *  equals to this value. Valid range: 0-15 (4 bits). */
871   uint8_t                    compVal;
872 
873   /** Configure compare mask. Set bit X to exclude sensor X from evaluation.
874    *  Note: decoder can handle sensor inputs from up to 4 sensors, therefore
875    *  this mask is 4 bit long. */
876   uint8_t                    compMask;
877 
878   /** Configure index of state to be entered if the sensor state equals to
879    *  compVal. Valid range: 0-15 (4 bits). */
880   uint8_t                    nextState;
881 
882   /** Configure which PRS action to perform when sensor state equals to
883    *  compVal. */
884   LESENSE_StTransAct_TypeDef prsAct;
885 
886   /** If enabled, interrupt flag is set when sensor state equals to compVal. */
887   bool                       setInt;
888 } LESENSE_DecStCond_TypeDef;
889 
890 /** Default configuration for decoder state condition. */
891 #define LESENSE_ST_CONF_DEFAULT                                          \
892   {                                                                      \
893     0x0FU,               /* Compare value set to 0x0F. */                \
894     0x00U,               /* All decoder inputs masked. */                \
895     0U,                  /* Next state is state 0. */                    \
896     lesenseTransActNone, /* No PRS action performed on compare match. */ \
897     false                /* No interrupt triggered on compare match. */  \
898   }
899 
900 
901 /** Decoder state x configuration structure. */
902 typedef struct
903 {
904   /** If enabled, the state descriptor pair in the next location will also be
905    *  evaluated. */
906   bool                      chainDesc;
907 
908   /** State condition descriptor A (high level descriptor of
909    *  LESENSE_STx_DECCONFA). */
910   LESENSE_DecStCond_TypeDef confA;
911 
912   /** State condition descriptor B (high level descriptor of
913    *  LESENSE_STx_DECCONFB). */
914   LESENSE_DecStCond_TypeDef confB;
915 } LESENSE_DecStDesc_TypeDef;
916 
917 
918 /** Configuration structure for the decoder. */
919 typedef struct
920 {
921   /** Descriptor of the 16 decoder states. */
922   LESENSE_DecStDesc_TypeDef St[16];
923 } LESENSE_DecStAll_TypeDef;
924 
925 /** Default configuration for all decoder states. */
926 #define LESENSE_DECODER_CONF_DEFAULT                                                                                 \
927   {  /* chain |   Descriptor A         |   Descriptor B   */ \
928     {                                                                                                                \
929       { false, LESENSE_ST_CONF_DEFAULT, LESENSE_ST_CONF_DEFAULT }, /* Decoder state 0. */                            \
930       { false, LESENSE_ST_CONF_DEFAULT, LESENSE_ST_CONF_DEFAULT }, /* Decoder state 1. */                            \
931       { false, LESENSE_ST_CONF_DEFAULT, LESENSE_ST_CONF_DEFAULT }, /* Decoder state 2. */                            \
932       { false, LESENSE_ST_CONF_DEFAULT, LESENSE_ST_CONF_DEFAULT }, /* Decoder state 3. */                            \
933       { false, LESENSE_ST_CONF_DEFAULT, LESENSE_ST_CONF_DEFAULT }, /* Decoder state 4. */                            \
934       { false, LESENSE_ST_CONF_DEFAULT, LESENSE_ST_CONF_DEFAULT }, /* Decoder state 5. */                            \
935       { false, LESENSE_ST_CONF_DEFAULT, LESENSE_ST_CONF_DEFAULT }, /* Decoder state 6. */                            \
936       { false, LESENSE_ST_CONF_DEFAULT, LESENSE_ST_CONF_DEFAULT }, /* Decoder state 7. */                            \
937       { false, LESENSE_ST_CONF_DEFAULT, LESENSE_ST_CONF_DEFAULT }, /* Decoder state 8. */                            \
938       { false, LESENSE_ST_CONF_DEFAULT, LESENSE_ST_CONF_DEFAULT }, /* Decoder state 9. */                            \
939       { false, LESENSE_ST_CONF_DEFAULT, LESENSE_ST_CONF_DEFAULT }, /* Decoder state 10. */                           \
940       { false, LESENSE_ST_CONF_DEFAULT, LESENSE_ST_CONF_DEFAULT }, /* Decoder state 11. */                           \
941       { false, LESENSE_ST_CONF_DEFAULT, LESENSE_ST_CONF_DEFAULT }, /* Decoder state 12. */                           \
942       { false, LESENSE_ST_CONF_DEFAULT, LESENSE_ST_CONF_DEFAULT }, /* Decoder state 13. */                           \
943       { false, LESENSE_ST_CONF_DEFAULT, LESENSE_ST_CONF_DEFAULT }, /* Decoder state 14. */                           \
944       { false, LESENSE_ST_CONF_DEFAULT, LESENSE_ST_CONF_DEFAULT } /* Decoder state 15. */                            \
945     }                                                                                                                \
946   }
947 
948 /*******************************************************************************
949  *****************************   PROTOTYPES   **********************************
950  ******************************************************************************/
951 void LESENSE_Init(LESENSE_Init_TypeDef const *init, bool const reqReset);
952 void LESENSE_Reset(void);
953 
954 uint32_t LESENSE_ScanFreqSet(uint32_t refFreq, uint32_t const scanFreq);
955 void LESENSE_ScanModeSet(LESENSE_ScanMode_TypeDef const scanMode,
956                          bool const start);
957 
958 void LESENSE_StartDelaySet(uint8_t const startDelay);
959 
960 void LESENSE_ClkDivSet(LESENSE_ChClk_TypeDef const clk,
961                        LESENSE_ClkPresc_TypeDef const clkDiv);
962 
963 void LESENSE_ChannelAllConfig(LESENSE_ChAll_TypeDef const *confChAll);
964 void LESENSE_ChannelConfig(LESENSE_ChDesc_TypeDef const *confCh,
965                            uint32_t const chIdx);
966 void LESENSE_ChannelEnable(uint8_t const chIdx,
967                            bool const enaScanCh,
968                            bool const enaPin);
969 void LESENSE_ChannelEnableMask(uint16_t chMask, uint16_t pinMask);
970 void LESENSE_ChannelTimingSet(uint8_t const chIdx,
971                               uint8_t const exTime,
972                               uint8_t const sampleDelay,
973                               uint8_t const measDelay);
974 void LESENSE_ChannelThresSet(uint8_t const chIdx,
975                              uint16_t const acmpThres,
976                              uint16_t const cntThres);
977 
978 void LESENSE_AltExConfig(LESENSE_ConfAltEx_TypeDef const *confAltEx);
979 
980 void LESENSE_DecoderStateAllConfig(LESENSE_DecStAll_TypeDef const *confDecStAll);
981 void LESENSE_DecoderStateConfig(LESENSE_DecStDesc_TypeDef const *confDecSt,
982                                 uint32_t const decSt);
983 void LESENSE_DecoderStateSet(uint32_t decSt);
984 uint32_t LESENSE_DecoderStateGet(void);
985 
986 __STATIC_INLINE void LESENSE_ScanStart(void);
987 __STATIC_INLINE void LESENSE_ScanStop(void);
988 __STATIC_INLINE void LESENSE_DecoderStart(void);
989 __STATIC_INLINE void LESENSE_DecoderStop(void);
990 __STATIC_INLINE void LESENSE_ResultBufferClear(void);
991 
992 __STATIC_INLINE uint32_t LESENSE_StatusGet(void);
993 __STATIC_INLINE void LESENSE_StatusWait(uint32_t flag);
994 __STATIC_INLINE uint32_t LESENSE_ChannelActiveGet(void);
995 __STATIC_INLINE uint32_t LESENSE_ScanResultGet(void);
996 __STATIC_INLINE uint32_t LESENSE_ScanResultDataGet(void);
997 __STATIC_INLINE uint32_t LESENSE_ScanResultDataBufferGet(uint32_t idx);
998 __STATIC_INLINE uint32_t LESENSE_SensorStateGet(void);
999 __STATIC_INLINE void LESENSE_RAMPowerDown(void);
1000 
1001 __STATIC_INLINE void LESENSE_IntClear(uint32_t flags);
1002 __STATIC_INLINE void LESENSE_IntEnable(uint32_t flags);
1003 __STATIC_INLINE void LESENSE_IntDisable(uint32_t flags);
1004 __STATIC_INLINE void LESENSE_IntSet(uint32_t flags);
1005 __STATIC_INLINE uint32_t LESENSE_IntGet(void);
1006 __STATIC_INLINE uint32_t LESENSE_IntGetEnabled(void);
1007 
1008 
1009 /***************************************************************************//**
1010  * @brief
1011  *   Start scanning of sensors.
1012  ******************************************************************************/
LESENSE_ScanStart(void)1013 __STATIC_INLINE void LESENSE_ScanStart(void)
1014 {
1015   /* Start scanning of sensors */
1016   LESENSE->CMD = LESENSE_CMD_START;
1017 }
1018 
1019 
1020 /***************************************************************************//**
1021  * @brief
1022  *   Stop scanning of sensors.
1023  *
1024  * @note
1025  *   If issued during a scan, the command takes effect after scan completion.
1026  ******************************************************************************/
LESENSE_ScanStop(void)1027 __STATIC_INLINE void LESENSE_ScanStop(void)
1028 {
1029   /* Stop scanning of sensors */
1030   LESENSE->CMD = LESENSE_CMD_STOP;
1031 }
1032 
1033 
1034 /***************************************************************************//**
1035  * @brief
1036  *   Start LESENSE decoder.
1037  ******************************************************************************/
LESENSE_DecoderStart(void)1038 __STATIC_INLINE void LESENSE_DecoderStart(void)
1039 {
1040   /* Start decoder */
1041   LESENSE->CMD = LESENSE_CMD_DECODE;
1042 }
1043 
1044 
1045 /***************************************************************************//**
1046  * @brief
1047  *   Stop LESENSE decoder.
1048  *
1049  * @details
1050  *   This function disables the LESENSE decoder by setting the command to the
1051  *   LESENSE_DECCTRL register.
1052  ******************************************************************************/
LESENSE_DecoderStop(void)1053 __STATIC_INLINE void LESENSE_DecoderStop(void)
1054 {
1055   /* Stop the decoder */
1056   LESENSE->DECCTRL |= LESENSE_DECCTRL_DISABLE;
1057 }
1058 
1059 
1060 /***************************************************************************//**
1061  * @brief
1062  *   Clear result buffer.
1063  ******************************************************************************/
LESENSE_ResultBufferClear(void)1064 __STATIC_INLINE void LESENSE_ResultBufferClear(void)
1065 {
1066   LESENSE->CMD = LESENSE_CMD_CLEARBUF;
1067 }
1068 
1069 
1070 /***************************************************************************//**
1071  * @brief
1072  *   Get the current status of LESENSE.
1073  *
1074  * @return
1075  *   This function returns the value of LESENSE_STATUS register that
1076  *   contains the OR combination of the following status bits:
1077  *   @li LESENSE_STATUS_RESV - Result data valid. Set when data is available
1078  *   in the result buffer. Cleared when the buffer is empty.
1079  *   @li LESENSE_STATUS_RESFULL - Result buffer full. Set when the result
1080  *   buffer is full.
1081  *   @li LESENSE_STATUS_RUNNING - LESENSE is active.
1082  *   @li LESENSE_STATUS_SCANACTIVE - LESENSE is currently interfacing sensors.
1083  ******************************************************************************/
LESENSE_StatusGet(void)1084 __STATIC_INLINE uint32_t LESENSE_StatusGet(void)
1085 {
1086   return LESENSE->STATUS;
1087 }
1088 
1089 
1090 /***************************************************************************//**
1091  * @brief
1092  *   Wait until the status of LESENSE is equal to what requested.
1093  *
1094  * @details
1095  *   This function is polling the LESENSE_STATUS register and waits until the
1096  *   requested combination of flags are set.
1097  *
1098  * @param[in] flag
1099  *   The OR combination of the following status bits:
1100  *   @li LESENSE_STATUS_BUFDATAV - Result data valid. Set when data is available
1101  *   in the result buffer. Cleared when the buffer is empty.
1102  *   @li LESENSE_STATUS_BUFHALFFULL - Result buffer half full. Set when the
1103  *   result buffer is half full.
1104  *   @li LESENSE_STATUS_BUFFULL - Result buffer full. Set when the result
1105  *   buffer is full.
1106  *   @li LESENSE_STATUS_RUNNING - LESENSE is active.
1107  *   @li LESENSE_STATUS_SCANACTIVE - LESENSE is currently interfacing sensors.
1108  *   @li LESENSE_STATUS_DACACTIVE - The DAC interface is currently active.
1109  ******************************************************************************/
LESENSE_StatusWait(uint32_t flag)1110 __STATIC_INLINE void LESENSE_StatusWait(uint32_t flag)
1111 {
1112   while (!(LESENSE->STATUS & flag))
1113     ;
1114 }
1115 
1116 
1117 /***************************************************************************//**
1118  * @brief
1119  *   Get the currently active channel index.
1120  *
1121  * @return
1122  *   This function returns the value of LESENSE_CHINDEX register that
1123  *   contains the index of the currently active channel (0-15).
1124  ******************************************************************************/
LESENSE_ChannelActiveGet(void)1125 __STATIC_INLINE uint32_t LESENSE_ChannelActiveGet(void)
1126 {
1127   return LESENSE->CURCH;
1128 }
1129 
1130 
1131 /***************************************************************************//**
1132  * @brief
1133  *   Get the latest scan comparison result (1 bit / channel).
1134  *
1135  * @return
1136  *   This function returns the value of LESENSE_SCANRES register that
1137  *   contains the comparison result of the last scan on all channels.
1138  *   Bit x is set if a comparison triggered on channel x, which means that the
1139  *   LESENSE counter met the comparison criteria set in LESENSE_CHx_EVAL by
1140  *   COMPMODE and CNTTHRES.
1141  ******************************************************************************/
LESENSE_ScanResultGet(void)1142 __STATIC_INLINE uint32_t LESENSE_ScanResultGet(void)
1143 {
1144   return LESENSE->SCANRES;
1145 }
1146 
1147 
1148 /***************************************************************************//**
1149  * @brief
1150  *   Get the oldest unread data from the result buffer.
1151  *
1152  * @note
1153  *   Make sure that the STORERES bit is set in LESENSE_CHx_EVAL, or
1154  *   STRSCANRES bit is set in LESENSE_CTRL, otherwise this function will return
1155  *   undefined value.
1156  *
1157  * @return
1158  *   This function returns the value of LESENSE_RESDATA register that
1159  *   contains the oldest unread counter result from the result buffer.
1160  ******************************************************************************/
LESENSE_ScanResultDataGet(void)1161 __STATIC_INLINE uint32_t LESENSE_ScanResultDataGet(void)
1162 {
1163   return LESENSE->BUFDATA;
1164 }
1165 
1166 
1167 /***************************************************************************//**
1168  * @brief
1169  *   Get data from the result data buffer.
1170  *
1171  * @note
1172  *   Make sure that the STORERES bit is set in LESENSE_CHx_EVAL, or
1173  *   STRSCANRES bit is set in LESENSE_CTRL, otherwise this function will return
1174  *   undefined value.
1175  *
1176  * @param[in]
1177  *   Result data buffer index. Valid range: 0-15.
1178  *
1179  * @return
1180  *   This function returns the selected word from the result data buffer.
1181  ******************************************************************************/
LESENSE_ScanResultDataBufferGet(uint32_t idx)1182 __STATIC_INLINE uint32_t LESENSE_ScanResultDataBufferGet(uint32_t idx)
1183 {
1184   /* Note: masking is needed to avoid over-indexing! */
1185   return LESENSE->BUF[idx & 0x0FU].DATA;
1186 }
1187 
1188 /***************************************************************************//**
1189  * @brief
1190  *   Get the current state of the LESENSE sensor.
1191  *
1192  * @return
1193  *   This function returns the value of LESENSE_SENSORSTATE register that
1194  *   represents the current state of the LESENSE sensor.
1195  ******************************************************************************/
LESENSE_SensorStateGet(void)1196 __STATIC_INLINE uint32_t LESENSE_SensorStateGet(void)
1197 {
1198   return LESENSE->SENSORSTATE;
1199 }
1200 
1201 
1202 /***************************************************************************//**
1203  * @brief
1204  *   Shut off power to the LESENSE RAM, disables LESENSE.
1205  *
1206  * @details
1207  *   This function shuts off the LESENSE RAM in order to decrease the leakage
1208  *   current of EFM32 if LESENSE is not used in your application.
1209  *
1210  * @note
1211  *   Warning! Once the LESENSE RAM is powered down, it cannot be powered up
1212  *   again.
1213  ******************************************************************************/
LESENSE_RAMPowerDown(void)1214 __STATIC_INLINE void LESENSE_RAMPowerDown(void)
1215 {
1216   /* Power down LESENSE RAM */
1217   LESENSE->POWERDOWN = LESENSE_POWERDOWN_RAM;
1218 }
1219 
1220 
1221 /***************************************************************************//**
1222  * @brief
1223  *   Clear one or more pending LESENSE interrupts.
1224  *
1225  * @param[in] flags
1226  *   Pending LESENSE interrupt sources to clear. Use a set of interrupt flags
1227  *   OR-ed together to clear multiple interrupt sources of the LESENSE module
1228  *   (LESENSE_IF_nnn).
1229  ******************************************************************************/
LESENSE_IntClear(uint32_t flags)1230 __STATIC_INLINE void LESENSE_IntClear(uint32_t flags)
1231 {
1232   LESENSE->IFC = flags;
1233 }
1234 
1235 
1236 /***************************************************************************//**
1237  * @brief
1238  *   Enable one or more LESENSE interrupts.
1239  *
1240  * @param[in] flags
1241  *   LESENSE interrupt sources to enable. Use a set of interrupt flags OR-ed
1242  *   together to enable multiple interrupt sources of the LESENSE module
1243  *   (LESENSE_IF_nnn).
1244  ******************************************************************************/
LESENSE_IntEnable(uint32_t flags)1245 __STATIC_INLINE void LESENSE_IntEnable(uint32_t flags)
1246 {
1247   LESENSE->IEN |= flags;
1248 }
1249 
1250 
1251 /***************************************************************************//**
1252  * @brief
1253  *   Disable one or more LESENSE interrupts.
1254  *
1255  * @param[in] flags
1256  *   LESENSE interrupt sources to disable. Use a set of interrupt flags OR-ed
1257  *   together to disable multiple interrupt sources of the LESENSE module
1258  *   (LESENSE_IF_nnn).
1259  ******************************************************************************/
LESENSE_IntDisable(uint32_t flags)1260 __STATIC_INLINE void LESENSE_IntDisable(uint32_t flags)
1261 {
1262   LESENSE->IEN &= ~(flags);
1263 }
1264 
1265 
1266 /***************************************************************************//**
1267  * @brief
1268  *   Set one or more pending LESENSE interrupts from SW.
1269  *
1270  * @param[in] flags
1271  *   LESENSE interrupt sources to set to pending. Use a set of interrupt
1272  *   flags OR-ed together to set multiple interrupt sources of the LESENSE
1273  *   module (LESENSE_IFS_nnn).
1274  ******************************************************************************/
LESENSE_IntSet(uint32_t flags)1275 __STATIC_INLINE void LESENSE_IntSet(uint32_t flags)
1276 {
1277   LESENSE->IFS = flags;
1278 }
1279 
1280 
1281 /***************************************************************************//**
1282  * @brief
1283  *   Get pending LESENSE interrupt flags.
1284  *
1285  * @note
1286  *   The event bits are not cleared by the use of this function.
1287  *
1288  * @return
1289  *   Pending LESENSE interrupt sources. The OR combination of valid interrupt
1290  *   flags of the LESENSE module (LESENSE_IF_nnn).
1291  ******************************************************************************/
LESENSE_IntGet(void)1292 __STATIC_INLINE uint32_t LESENSE_IntGet(void)
1293 {
1294   return LESENSE->IF;
1295 }
1296 
1297 
1298 /***************************************************************************//**
1299  * @brief
1300  *   Get enabled and pending LESENSE interrupt flags.
1301  *
1302  * @details
1303  *   Useful for handling more interrupt sources in the same interrupt handler.
1304  *
1305  * @note
1306  *   The event bits are not cleared by the use of this function.
1307  *
1308  * @return
1309  *   Pending and enabled LESENSE interrupt sources.
1310  *   The return value is the bitwise AND combination of
1311  *   - the OR combination of enabled interrupt sources in LESENSE_IEN_nnn
1312  *   register (LESENSE_IEN_nnn) and
1313  *   - the OR combination of valid interrupt flags of the LESENSE module
1314  *   (LESENSE_IF_nnn).
1315  ******************************************************************************/
LESENSE_IntGetEnabled(void)1316 __STATIC_INLINE uint32_t LESENSE_IntGetEnabled(void)
1317 {
1318   uint32_t tmp;
1319 
1320   /* Store LESENSE->IEN in temporary variable in order to define explicit order
1321    * of volatile accesses. */
1322   tmp = LESENSE->IEN;
1323 
1324   /* Bitwise AND of pending and enabled interrupts */
1325   return LESENSE->IF & tmp;
1326 }
1327 
1328 
1329 /** @} (end addtogroup LESENSE) */
1330 /** @} (end addtogroup EM_Library) */
1331 
1332 #ifdef __cplusplus
1333 }
1334 #endif
1335 
1336 #endif /* defined(LESENSE_COUNT) && (LESENSE_COUNT > 0) */
1337 
1338 #endif /* __EM_LESENSE_H */
1339