1 /***************************************************************************//**
\r
3 * @brief Pulse Counter (PCNT) peripheral API
\r
5 *******************************************************************************
\r
7 * <b>(C) Copyright 2014 Silicon Labs, http://www.silabs.com</b>
\r
8 *******************************************************************************
\r
10 * Permission is granted to anyone to use this software for any purpose,
\r
11 * including commercial applications, and to alter it and redistribute it
\r
12 * freely, subject to the following restrictions:
\r
14 * 1. The origin of this software must not be misrepresented; you must not
\r
15 * claim that you wrote the original software.
\r
16 * 2. Altered source versions must be plainly marked as such, and must not be
\r
17 * misrepresented as being the original software.
\r
18 * 3. This notice may not be removed or altered from any source distribution.
\r
20 * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no
\r
21 * obligation to support this Software. Silicon Labs is providing the
\r
22 * Software "AS IS", with no express or implied warranties of any kind,
\r
23 * including, but not limited to, any implied warranties of merchantability
\r
24 * or fitness for any particular purpose or warranties against infringement
\r
25 * of any proprietary rights of a third party.
\r
27 * Silicon Labs will not be liable for any consequential, incidental, or
\r
28 * special damages, or any other relief, or for any claim by any third party,
\r
29 * arising from your use of this Software.
\r
31 ******************************************************************************/
\r
34 #ifndef __SILICON_LABS_EM_PCNT_H_
\r
35 #define __SILICON_LABS_EM_PCNT_H_
\r
37 #include "em_device.h"
\r
38 #if defined(PCNT_COUNT) && (PCNT_COUNT > 0)
\r
40 #include <stdbool.h>
\r
46 /***************************************************************************//**
\r
47 * @addtogroup EM_Library
\r
49 ******************************************************************************/
\r
51 /***************************************************************************//**
\r
54 ******************************************************************************/
\r
56 /*******************************************************************************
\r
57 ******************************* DEFINES ***********************************
\r
58 ******************************************************************************/
\r
59 /** PCNT Counter register sizes. */
\r
60 #if defined _EFM32_GECKO_FAMILY
\r
61 #define PCNT0_CNT_SIZE (8) /** PCNT0 counter is 8 bits. */
\r
63 #define PCNT0_CNT_SIZE (16) /** PCNT0 counter is 16 bits. */
\r
66 #define PCNT1_CNT_SIZE (8) /** PCNT1 counter is 8 bits. */
\r
69 #define PCNT2_CNT_SIZE (8) /** PCNT2 counter is 8 bits. */
\r
73 /*******************************************************************************
\r
74 ******************************** ENUMS ************************************
\r
75 ******************************************************************************/
\r
77 /** Mode selection. */
\r
80 /** Disable pulse counter. */
\r
81 pcntModeDisable = _PCNT_CTRL_MODE_DISABLE,
\r
83 /** Single input LFACLK oversampling mode (available in EM0-EM2). */
\r
84 pcntModeOvsSingle = _PCNT_CTRL_MODE_OVSSINGLE,
\r
86 /** Externally clocked single input counter mode (available in EM0-EM3). */
\r
87 pcntModeExtSingle = _PCNT_CTRL_MODE_EXTCLKSINGLE,
\r
89 /** Externally clocked quadrature decoder mode (available in EM0-EM3). */
\r
90 pcntModeExtQuad = _PCNT_CTRL_MODE_EXTCLKQUAD
\r
91 } PCNT_Mode_TypeDef;
\r
94 #if defined( _PCNT_CTRL_CNTEV_MASK)
\r
95 /** Counter event selection.
\r
96 * Note: unshifted values are being used for enumeration because multiple
\r
97 * configuration structure members use this type definition. */
\r
100 /** Counts up on up-count and down on down-count events. */
\r
101 pcntCntEventBoth = _PCNT_CTRL_CNTEV_BOTH,
\r
103 /** Only counts up on up-count events. */
\r
104 pcntCntEventUp = _PCNT_CTRL_CNTEV_UP,
\r
106 /** Only counts down on down-count events. */
\r
107 pcntCntEventDown = _PCNT_CTRL_CNTEV_DOWN,
\r
109 /** Never counts. */
\r
110 pcntCntEventNone = _PCNT_CTRL_CNTEV_NONE
\r
111 } PCNT_CntEvent_TypeDef;
\r
115 #if defined( _PCNT_INPUT_MASK )
\r
116 /** PRS sources for @p s0PRS and @p s1PRS. */
\r
119 pcntPRSCh0 = 0, /**< PRS channel 0. */
\r
120 pcntPRSCh1 = 1, /**< PRS channel 1. */
\r
121 pcntPRSCh2 = 2, /**< PRS channel 2. */
\r
122 pcntPRSCh3 = 3, /**< PRS channel 3. */
\r
123 #if defined( PCNT_INPUT_S0PRSSEL_PRSCH4 )
\r
124 pcntPRSCh4 = 4, /**< PRS channel 4. */
\r
126 #if defined( PCNT_INPUT_S0PRSSEL_PRSCH5 )
\r
127 pcntPRSCh5 = 5, /**< PRS channel 5. */
\r
129 #if defined( PCNT_INPUT_S0PRSSEL_PRSCH6 )
\r
130 pcntPRSCh6 = 6, /**< PRS channel 6. */
\r
132 #if defined( PCNT_INPUT_S0PRSSEL_PRSCH7 )
\r
133 pcntPRSCh7 = 7, /**< PRS channel 7. */
\r
135 #if defined( PCNT_INPUT_S0PRSSEL_PRSCH8 )
\r
136 pcntPRSCh8 = 8, /**< PRS channel 8. */
\r
138 #if defined( PCNT_INPUT_S0PRSSEL_PRSCH9 )
\r
139 pcntPRSCh9 = 9, /**< PRS channel 9. */
\r
141 #if defined( PCNT_INPUT_S0PRSSEL_PRSCH10 )
\r
142 pcntPRSCh10 = 10, /**< PRS channel 10. */
\r
144 #if defined( PCNT_INPUT_S0PRSSEL_PRSCH11 )
\r
145 pcntPRSCh11 = 11 /**< PRS channel 11. */
\r
147 } PCNT_PRSSel_TypeDef;
\r
150 /** PRS inputs of PCNT. */
\r
153 pcntPRSInputS0 = 0, /** PRS input 0. */
\r
154 pcntPRSInputS1 = 1 /** PRS input 1. */
\r
155 } PCNT_PRSInput_TypeDef;
\r
159 /*******************************************************************************
\r
160 ******************************* STRUCTS ***********************************
\r
161 ******************************************************************************/
\r
163 /** Init structure. */
\r
166 /** Mode to operate in. */
\r
167 PCNT_Mode_TypeDef mode;
\r
169 /** Initial counter value (refer to reference manual for max value allowed).
\r
170 * Only used for #pcntModeOvsSingle (and possibly #pcntModeDisable) modes.
\r
171 * If using #pcntModeExtSingle or #pcntModeExtQuad modes, the counter
\r
172 * value is reset to HW reset value. */
\r
175 /** Initial top value (refer to reference manual for max value allowed).
\r
176 * Only used for #pcntModeOvsSingle (and possibly #pcntModeDisable) modes.
\r
177 * If using #pcntModeExtSingle or #pcntModeExtQuad modes, the top
\r
178 * value is reset to HW reset value. */
\r
181 /** Polarity of incoming edge.
\r
182 * @li #pcntModeExtSingle mode - if false, positive edges are counted,
\r
183 * otherwise negative edges.
\r
184 * @li #pcntModeExtQuad mode - if true, counting direction is inverted. */
\r
187 /** Counting direction, only applicable for #pcntModeOvsSingle and
\r
188 * #pcntModeExtSingle modes. */
\r
191 /** Enable filter, only available in #pcntModeOvsSingle mode. */
\r
194 #if defined( PCNT_CTRL_HYST )
\r
195 /** Set to true to enable hysteresis. When its enabled, the PCNT will always
\r
196 * overflow and underflow to TOP/2. */
\r
199 /** Set to true to enable S1 to determine the direction of counting in
\r
200 * OVSSINGLE or EXTCLKSINGLE modes.
\r
201 * When S1 is high, the count direction is given by CNTDIR, and when S1 is
\r
202 * low, the count direction is the opposite. */
\r
205 /** Selects whether the regular counter responds to up-count events,
\r
206 * down-count events, both or none. */
\r
207 PCNT_CntEvent_TypeDef cntEvent;
\r
209 /** Selects whether the auxiliary counter responds to up-count events,
\r
210 * down-count events, both or none. */
\r
211 PCNT_CntEvent_TypeDef auxCntEvent;
\r
213 /** Select PRS channel as input to S0IN in PCNTx_INPUT register. */
\r
214 PCNT_PRSSel_TypeDef s0PRS;
\r
216 /** Select PRS channel as input to S1IN in PCNTx_INPUT register. */
\r
217 PCNT_PRSSel_TypeDef s1PRS;
\r
219 } PCNT_Init_TypeDef;
\r
221 #if !defined ( PCNT_CTRL_HYST )
\r
222 /** Default config for PCNT init structure. */
\r
223 #define PCNT_INIT_DEFAULT \
\r
224 { pcntModeDisable, /* Disabled by default. */ \
\r
225 _PCNT_CNT_RESETVALUE, /* Default counter HW reset value. */ \
\r
226 _PCNT_TOP_RESETVALUE, /* Default counter HW reset value. */ \
\r
227 false, /* Use positive edge. */ \
\r
228 false, /* Up-counting. */ \
\r
229 false /* Filter disabled. */ \
\r
232 /** Default config for PCNT init structure. */
\r
233 #define PCNT_INIT_DEFAULT \
\r
234 { pcntModeDisable, /* Disabled by default. */ \
\r
235 _PCNT_CNT_RESETVALUE, /* Default counter HW reset value. */ \
\r
236 _PCNT_TOP_RESETVALUE, /* Default counter HW reset value. */ \
\r
237 false, /* Use positive edge. */ \
\r
238 false, /* Up-counting. */ \
\r
239 false, /* Filter disabled. */ \
\r
240 false, /* Hysteresis disabled. */ \
\r
241 true, /* Counter direction is given by CNTDIR. */ \
\r
242 pcntCntEventUp, /* Regular counter counts up on upcount events. */ \
\r
243 pcntCntEventNone, /* Auxiliary counter doesn't respond to events. */ \
\r
244 pcntPRSCh0, /* PRS channel 0 selected as S0IN. */ \
\r
245 pcntPRSCh0 /* PRS channel 0 selected as S1IN. */ \
\r
250 /*******************************************************************************
\r
251 ***************************** PROTOTYPES **********************************
\r
252 ******************************************************************************/
\r
254 /***************************************************************************//**
\r
256 * Get pulse counter value.
\r
259 * Pointer to PCNT peripheral register block.
\r
262 * Current pulse counter value.
\r
263 ******************************************************************************/
\r
264 __STATIC_INLINE uint32_t PCNT_CounterGet(PCNT_TypeDef *pcnt)
\r
270 #if defined( _PCNT_AUXCNT_MASK )
\r
271 /***************************************************************************//**
\r
273 * Get auxiliary counter value.
\r
276 * Pointer to PCNT peripheral register block.
\r
279 * Current auxiliary counter value.
\r
280 ******************************************************************************/
\r
281 __STATIC_INLINE uint32_t PCNT_AuxCounterGet(PCNT_TypeDef *pcnt)
\r
283 return pcnt->AUXCNT;
\r
288 void PCNT_CounterReset(PCNT_TypeDef *pcnt);
\r
289 void PCNT_CounterTopSet(PCNT_TypeDef *pcnt, uint32_t count, uint32_t top);
\r
292 /***************************************************************************//**
\r
294 * Set counter value.
\r
297 * The pulse counter is disabled while changing counter value, and reenabled
\r
298 * (if originally enabled) when counter value has been set.
\r
301 * This function will stall until synchronization to low frequency domain is
\r
302 * completed. For that reason, it should normally not be used when using
\r
303 * an external clock to clock the PCNT module, since stall time may be
\r
304 * undefined in that case. The counter should normally only be set when
\r
305 * operating in (or about to enable) #pcntModeOvsSingle mode.
\r
308 * Pointer to PCNT peripheral register block.
\r
311 * Value to set in counter register.
\r
312 ******************************************************************************/
\r
313 __STATIC_INLINE void PCNT_CounterSet(PCNT_TypeDef *pcnt, uint32_t count)
\r
315 PCNT_CounterTopSet(pcnt, count, pcnt->TOP);
\r
319 void PCNT_Enable(PCNT_TypeDef *pcnt, PCNT_Mode_TypeDef mode);
\r
320 void PCNT_FreezeEnable(PCNT_TypeDef *pcnt, bool enable);
\r
321 void PCNT_Init(PCNT_TypeDef *pcnt, const PCNT_Init_TypeDef *init);
\r
323 #if defined( _PCNT_INPUT_MASK )
\r
324 void PCNT_PRSInputEnable(PCNT_TypeDef *pcnt,
\r
325 PCNT_PRSInput_TypeDef prsInput,
\r
330 /***************************************************************************//**
\r
332 * Clear one or more pending PCNT interrupts.
\r
335 * Pointer to PCNT peripheral register block.
\r
338 * Pending PCNT interrupt source to clear. Use a bitwise logic OR combination
\r
339 * of valid interrupt flags for the PCNT module (PCNT_IF_nnn).
\r
340 ******************************************************************************/
\r
341 __STATIC_INLINE void PCNT_IntClear(PCNT_TypeDef *pcnt, uint32_t flags)
\r
347 /***************************************************************************//**
\r
349 * Disable one or more PCNT interrupts.
\r
352 * Pointer to PCNT peripheral register block.
\r
355 * PCNT interrupt sources to disable. Use a bitwise logic OR combination of
\r
356 * valid interrupt flags for the PCNT module (PCNT_IF_nnn).
\r
357 ******************************************************************************/
\r
358 __STATIC_INLINE void PCNT_IntDisable(PCNT_TypeDef *pcnt, uint32_t flags)
\r
360 pcnt->IEN &= ~(flags);
\r
364 /***************************************************************************//**
\r
366 * Enable one or more PCNT interrupts.
\r
369 * Depending on the use, a pending interrupt may already be set prior to
\r
370 * enabling the interrupt. Consider using PCNT_IntClear() prior to enabling
\r
371 * if such a pending interrupt should be ignored.
\r
374 * Pointer to PCNT peripheral register block.
\r
377 * PCNT interrupt sources to enable. Use a bitwise logic OR combination of
\r
378 * valid interrupt flags for the PCNT module (PCNT_IF_nnn).
\r
379 ******************************************************************************/
\r
380 __STATIC_INLINE void PCNT_IntEnable(PCNT_TypeDef *pcnt, uint32_t flags)
\r
382 pcnt->IEN |= flags;
\r
386 /***************************************************************************//**
\r
388 * Get pending PCNT interrupt flags.
\r
391 * The event bits are not cleared by the use of this function.
\r
394 * Pointer to PCNT peripheral register block.
\r
397 * PCNT interrupt sources pending. A bitwise logic OR combination of valid
\r
398 * interrupt flags for the PCNT module (PCNT_IF_nnn).
\r
399 ******************************************************************************/
\r
400 __STATIC_INLINE uint32_t PCNT_IntGet(PCNT_TypeDef *pcnt)
\r
406 /***************************************************************************//**
\r
408 * Get enabled and pending PCNT interrupt flags.
\r
411 * Useful for handling more interrupt sources in the same interrupt handler.
\r
414 * The event bits are not cleared by the use of this function.
\r
417 * Pointer to PCNT peripheral register block.
\r
420 * Pending and enabled PCNT interrupt sources.
\r
421 * The return value is the bitwise AND combination of
\r
422 * - the OR combination of enabled interrupt sources in PCNT_IEN_nnn
\r
423 * register (PCNT_IEN_nnn) and
\r
424 * - the OR combination of valid interrupt flags of the PCNT module
\r
426 ******************************************************************************/
\r
427 __STATIC_INLINE uint32_t PCNT_IntGetEnabled(PCNT_TypeDef *pcnt)
\r
432 /* Store pcnt->IEN in temporary variable in order to define explicit order
\r
433 * of volatile accesses. */
\r
436 /* Bitwise AND of pending and enabled interrupts */
\r
437 return pcnt->IF & tmp;
\r
441 /***************************************************************************//**
\r
443 * Set one or more pending PCNT interrupts from SW.
\r
446 * Pointer to PCNT peripheral register block.
\r
449 * PCNT interrupt sources to set to pending. Use a bitwise logic OR combination
\r
450 * of valid interrupt flags for the PCNT module (PCNT_IF_nnn).
\r
451 ******************************************************************************/
\r
452 __STATIC_INLINE void PCNT_IntSet(PCNT_TypeDef *pcnt, uint32_t flags)
\r
457 void PCNT_Reset(PCNT_TypeDef *pcnt);
\r
460 /***************************************************************************//**
\r
462 * Get pulse counter top buffer value.
\r
465 * Pointer to PCNT peripheral register block.
\r
468 * Current pulse counter top buffer value.
\r
469 ******************************************************************************/
\r
470 __STATIC_INLINE uint32_t PCNT_TopBufferGet(PCNT_TypeDef *pcnt)
\r
475 void PCNT_TopBufferSet(PCNT_TypeDef *pcnt, uint32_t val);
\r
477 /***************************************************************************//**
\r
479 * Get pulse counter top value.
\r
482 * Pointer to PCNT peripheral register block.
\r
485 * Current pulse counter top value.
\r
486 ******************************************************************************/
\r
487 __STATIC_INLINE uint32_t PCNT_TopGet(PCNT_TypeDef *pcnt)
\r
492 void PCNT_TopSet(PCNT_TypeDef *pcnt, uint32_t val);
\r
495 /** @} (end addtogroup PCNT) */
\r
496 /** @} (end addtogroup EM_Library) */
\r
502 #endif /* defined(PCNT_COUNT) && (PCNT_COUNT > 0) */
\r
503 #endif /* __SILICON_LABS_EM_PCNT_H_ */
\r