PCNTEMLIB

Detailed Description

Pulse Counter (PCNT) Peripheral API.

This module contains functions to control the PCNT peripheral of Silicon Labs 32-bit MCUs and SoCs. The PCNT decodes incoming pulses. The module has a quadrature mode which may be used to decode the speed and direction of a mechanical shaft.

Data Structures

struct  PCNT_Init_TypeDef
 

Macros

#define PCNT0_CNT_SIZE   (8) /* PCNT0 counter is 8 bits. */
 
#define PCNT1_CNT_SIZE   (8) /* PCNT1 counter is 8 bits. */
 
#define PCNT2_CNT_SIZE   (8) /* PCNT2 counter is 8 bits. */
 
#define PCNT_INIT_DEFAULT
 

Enumerations

enum  PCNT_Mode_TypeDef {
  pcntModeDisable = _PCNT_CTRL_MODE_DISABLE,
  pcntModeOvsSingle = _PCNT_CTRL_MODE_OVSSINGLE,
  pcntModeExtSingle = _PCNT_CTRL_MODE_EXTCLKSINGLE,
  pcntModeExtQuad = _PCNT_CTRL_MODE_EXTCLKQUAD
}
 

Functions

__STATIC_INLINE uint32_t PCNT_CounterGet (PCNT_TypeDef *pcnt)
 Get pulse counter value.
 
void PCNT_CounterReset (PCNT_TypeDef *pcnt)
 Reset PCNT counters and TOP register.
 
__STATIC_INLINE void PCNT_CounterSet (PCNT_TypeDef *pcnt, uint32_t count)
 Set counter value.
 
void PCNT_CounterTopSet (PCNT_TypeDef *pcnt, uint32_t count, uint32_t top)
 Set the counter and top values.
 
void PCNT_Enable (PCNT_TypeDef *pcnt, PCNT_Mode_TypeDef mode)
 Set PCNT operational mode.
 
void PCNT_FreezeEnable (PCNT_TypeDef *pcnt, bool enable)
 PCNT register synchronization freeze control.
 
void PCNT_Init (PCNT_TypeDef *pcnt, const PCNT_Init_TypeDef *init)
 Initialize the pulse counter.
 
__STATIC_INLINE void PCNT_IntClear (PCNT_TypeDef *pcnt, uint32_t flags)
 Clear one or more pending PCNT interrupts.
 
__STATIC_INLINE void PCNT_IntDisable (PCNT_TypeDef *pcnt, uint32_t flags)
 Disable one or more PCNT interrupts.
 
__STATIC_INLINE void PCNT_IntEnable (PCNT_TypeDef *pcnt, uint32_t flags)
 Enable one or more PCNT interrupts.
 
__STATIC_INLINE uint32_t PCNT_IntGet (PCNT_TypeDef *pcnt)
 Get pending PCNT interrupt flags.
 
__STATIC_INLINE uint32_t PCNT_IntGetEnabled (PCNT_TypeDef *pcnt)
 Get enabled and pending PCNT interrupt flags.
 
__STATIC_INLINE void PCNT_IntSet (PCNT_TypeDef *pcnt, uint32_t flags)
 Set one or more pending PCNT interrupts from SW.
 
void PCNT_Reset (PCNT_TypeDef *pcnt)
 Reset PCNT to the same state that it was in after a hardware reset.
 
__STATIC_INLINE uint32_t PCNT_TopBufferGet (PCNT_TypeDef *pcnt)
 Get pulse counter top buffer value.
 
void PCNT_TopBufferSet (PCNT_TypeDef *pcnt, uint32_t val)
 Set top buffer value.
 
__STATIC_INLINE uint32_t PCNT_TopGet (PCNT_TypeDef *pcnt)
 Get pulse counter top value.
 
void PCNT_TopSet (PCNT_TypeDef *pcnt, uint32_t val)
 Set the top value.
 

Macro Definition Documentation

#define PCNT0_CNT_SIZE   (8) /* PCNT0 counter is 8 bits. */

PCNT0 Counter register size.

Definition at line 59 of file em_pcnt.h.

Referenced by PCNT_CounterTopSet(), PCNT_Init(), and PCNT_TopSet().

#define PCNT1_CNT_SIZE   (8) /* PCNT1 counter is 8 bits. */

PCNT1 Counter register size.

Definition at line 67 of file em_pcnt.h.

Referenced by PCNT_CounterTopSet(), PCNT_Init(), and PCNT_TopSet().

#define PCNT2_CNT_SIZE   (8) /* PCNT2 counter is 8 bits. */

PCNT2 Counter register size.

Definition at line 76 of file em_pcnt.h.

Referenced by PCNT_CounterTopSet(), PCNT_Init(), and PCNT_TopSet().

#define PCNT_INIT_DEFAULT
Value:
{ \
pcntModeDisable, /* Disabled by default. */ \
_PCNT_CNT_RESETVALUE, /* Default counter HW reset value. */ \
_PCNT_TOP_RESETVALUE, /* Default counter HW reset value. */ \
false, /* Use positive edge. */ \
false, /* Up-counting. */ \
false /* Filter disabled. */ \
}

Default configuration for PCNT initialization structure.

Definition at line 234 of file em_pcnt.h.

Enumeration Type Documentation

Mode selection.

Enumerator
pcntModeDisable 

Disable pulse counter.

pcntModeOvsSingle 

Single input LFACLK oversampling mode (available in EM0-EM2).

pcntModeExtSingle 

Externally clocked single input counter mode (available in EM0-EM3).

pcntModeExtQuad 

Externally clocked quadrature decoder mode (available in EM0-EM3).

Definition at line 87 of file em_pcnt.h.

Function Documentation

__STATIC_INLINE uint32_t PCNT_CounterGet ( PCNT_TypeDef *  pcnt)

Get pulse counter value.

Default configuration for PCNT initialization structure.

Parameters
[in]pcntPointer to PCNT peripheral register block.
Returns
Current pulse counter value.

Definition at line 377 of file em_pcnt.h.

void PCNT_CounterReset ( PCNT_TypeDef *  pcnt)

Reset PCNT counters and TOP register.

Note
Notice that special SYNCBUSY handling is not applicable for the RSTEN bit of the control register, so we don't need to wait for it when only modifying RSTEN. (It would mean undefined wait time if clocked by an external clock.) The SYNCBUSY bit will however be set, leading to a synchronization in the LF domain, with, in reality, no changes.
Parameters
[in]pcntA pointer to the PCNT peripheral register block.

Definition at line 141 of file em_pcnt.c.

References BUS_RegBitWrite().

__STATIC_INLINE void PCNT_CounterSet ( PCNT_TypeDef *  pcnt,
uint32_t  count 
)

Set counter value.

Pulse counter is disabled while changing counter value, and re-enabled (if originally enabled) when counter value has been set.

Note
This function will stall until synchronization to low-frequency domain is completed. For that reason, it should normally not be used when using an external clock to clock the PCNT module since stall time may be undefined in that case. The counter should normally only be set when operating in (or about to enable) pcntModeOvsSingle mode.
Parameters
[in]pcntPointer to PCNT peripheral register block.
[in]countValue to set in counter register.

Definition at line 423 of file em_pcnt.h.

References PCNT_CounterTopSet().

void PCNT_CounterTopSet ( PCNT_TypeDef *  pcnt,
uint32_t  count,
uint32_t  top 
)

Set the counter and top values.

The pulse counter is disabled while changing these values and reenabled (if originally enabled) when values have been set.

Note
This function will stall until synchronization to low-frequency domain is completed. For that reason, it should normally not be used when an external clock is used for the PCNT module, since stall time may be undefined. The counter should normally only be set when operating in (or about to enable) pcntModeOvsSingle mode.
Parameters
[in]pcntA pointer to the PCNT peripheral register block.
[in]countA value to set in the counter register.
[in]topA value to set in the top register.

Definition at line 176 of file em_pcnt.c.

References PCNT0_CNT_SIZE, PCNT1_CNT_SIZE, and PCNT2_CNT_SIZE.

Referenced by PCNT_CounterSet(), and PCNT_Init().

void PCNT_Enable ( PCNT_TypeDef *  pcnt,
PCNT_Mode_TypeDef  mode 
)

Set PCNT operational mode.

Notice that this function does not do any configuration. Setting operational mode is normally only required after initialization is done, and if not done as part of initialization or if requiring to disable/reenable pulse counter.

Note
This function may stall until synchronization to low-frequency domain is completed. For that reason, it should normally not be used when an external clock is used for the PCNT module, since stall time may be undefined.
Parameters
[in]pcntA pointer to the PCNT peripheral register block.
[in]modeAn operational mode to use for PCNT.

Definition at line 271 of file em_pcnt.c.

void PCNT_FreezeEnable ( PCNT_TypeDef *  pcnt,
bool  enable 
)

PCNT register synchronization freeze control.

Some PCNT registers require synchronization into the low-frequency (LF) domain. The freeze feature allows for several registers to be modified before passing them to the LF domain simultaneously, which takes place when the freeze mode is disabled.

Note
When enabling freeze mode, this function will wait for all current ongoing PCNT synchronization to the LF domain to complete (normally synchronization will not be in progress). However, for this reason, when using freeze mode, modifications of registers requiring the LF synchronization should be done within one freeze enable/disable block to avoid unnecessary stalling.
Parameters
[in]pcntA pointer to the PCNT peripheral register block.
[in]enable
  • True - enable freeze, modified registers are not propagated to the LF domain.
  • False - disables freeze, modified registers are propagated to LF domain.

Definition at line 356 of file em_pcnt.c.

void PCNT_Init ( PCNT_TypeDef *  pcnt,
const PCNT_Init_TypeDef init 
)

Initialize the pulse counter.

This function will configure the pulse counter. The clock selection is configured as follows, depending on operational mode:

Notice that the LFACLK must be enabled in all modes, since some basic setup is done with this clock even if the external pin clock usage mode is chosen. The pulse counter clock for the selected instance must also be enabled prior to initialization.

Notice that pins used by the PCNT module must be properly configured by the user explicitly through setting the ROUTE register for the PCNT to work as intended.

Writing to CNT will not occur in external clock modes (EXTCLKQUAD and EXTCLKSINGLE) because the external clock rate is unknown. The user should handle it manually depending on the application.

TOPB is written for all modes but in external clock mode it will take 3 external clock cycles to sync to TOP.

Note
Initializing requires synchronization into the low-frequency domain. This may cause a delay.
Parameters
[in]pcntA pointer to the PCNT peripheral register block.
[in]initA pointer to the initialization structure.

Definition at line 416 of file em_pcnt.c.

References BUS_RegBitWrite(), CMU_PCNTClockExternalSet(), PCNT_Init_TypeDef::countDown, PCNT_Init_TypeDef::counter, PCNT_Init_TypeDef::filter, PCNT_Init_TypeDef::mode, PCNT_Init_TypeDef::negEdge, PCNT0_CNT_SIZE, PCNT1_CNT_SIZE, PCNT2_CNT_SIZE, PCNT_CounterTopSet(), pcntModeExtQuad, pcntModeExtSingle, pcntModeOvsSingle, and PCNT_Init_TypeDef::top.

__STATIC_INLINE void PCNT_IntClear ( PCNT_TypeDef *  pcnt,
uint32_t  flags 
)

Clear one or more pending PCNT interrupts.

Parameters
[in]pcntPointer to PCNT peripheral register block.
[in]flagsPending PCNT interrupt source to clear. Use a bitwise logic OR combination of valid interrupt flags for the PCNT module (PCNT_IF_nnn).

Definition at line 456 of file em_pcnt.h.

__STATIC_INLINE void PCNT_IntDisable ( PCNT_TypeDef *  pcnt,
uint32_t  flags 
)

Disable one or more PCNT interrupts.

Parameters
[in]pcntPointer to PCNT peripheral register block.
[in]flagsPCNT interrupt sources to disable. Use a bitwise logic OR combination of valid interrupt flags for PCNT module (PCNT_IF_nnn).

Definition at line 472 of file em_pcnt.h.

__STATIC_INLINE void PCNT_IntEnable ( PCNT_TypeDef *  pcnt,
uint32_t  flags 
)

Enable one or more PCNT interrupts.

Note
Depending on the use, a pending interrupt may already be set prior to enabling the interrupt. To ignore a pending interrupt, consider using PCNT_IntClear() prior to enabling the interrupt.
Parameters
[in]pcntPointer to PCNT peripheral register block.
[in]flagsPCNT interrupt sources to enable. Use a bitwise logic OR combination of valid interrupt flags for PCNT module (PCNT_IF_nnn).

Definition at line 493 of file em_pcnt.h.

__STATIC_INLINE uint32_t PCNT_IntGet ( PCNT_TypeDef *  pcnt)

Get pending PCNT interrupt flags.

Note
The event bits are not cleared by the use of this function.
Parameters
[in]pcntPointer to PCNT peripheral register block.
Returns
PCNT interrupt sources pending. A bitwise logic OR combination of valid interrupt flags for PCNT module (PCNT_IF_nnn).

Definition at line 512 of file em_pcnt.h.

__STATIC_INLINE uint32_t PCNT_IntGetEnabled ( PCNT_TypeDef *  pcnt)

Get enabled and pending PCNT interrupt flags.

Useful for handling more interrupt sources in the same interrupt handler.

Note
The event bits are not cleared by the use of this function.
Parameters
[in]pcntPointer to PCNT peripheral register block.
Returns
Pending and enabled PCNT interrupt sources. The return value is the bitwise AND combination of
  • the OR combination of enabled interrupt sources in PCNT_IEN_nnn register (PCNT_IEN_nnn) and
  • the OR combination of valid interrupt flags of the PCNT module (PCNT_IF_nnn).

Definition at line 538 of file em_pcnt.h.

__STATIC_INLINE void PCNT_IntSet ( PCNT_TypeDef *  pcnt,
uint32_t  flags 
)

Set one or more pending PCNT interrupts from SW.

Parameters
[in]pcntPointer to PCNT peripheral register block.
[in]flagsPCNT interrupt sources to set to pending. Use a bitwise logic OR combination of valid interrupt flags for PCNT module (PCNT_IF_nnn).

Definition at line 561 of file em_pcnt.h.

void PCNT_Reset ( PCNT_TypeDef *  pcnt)

Reset PCNT to the same state that it was in after a hardware reset.

Notice the LFACLK must be enabled, since some basic reset is done with this clock. The pulse counter clock for the selected instance must also be enabled prior to initialization.

Note
The ROUTE register is NOT reset by this function to allow for centralized setup of this feature.
Parameters
[in]pcntA pointer to the PCNT peripheral register block.

Definition at line 612 of file em_pcnt.c.

References BUS_RegBitWrite(), CMU_PCNTClockExternalSet(), and PCNT_TopBufferSet().

__STATIC_INLINE uint32_t PCNT_TopBufferGet ( PCNT_TypeDef *  pcnt)

Get pulse counter top buffer value.

Parameters
[in]pcntPointer to PCNT peripheral register block.
Returns
Current pulse counter top buffer value.

Definition at line 578 of file em_pcnt.h.

void PCNT_TopBufferSet ( PCNT_TypeDef *  pcnt,
uint32_t  val 
)

Set top buffer value.

Note
This function may stall until synchronization to low-frequency domain is completed. For that reason, it should normally not be used when an external clock is used for the PCNT module since stall time may be undefined.
Parameters
[in]pcntA pointer to the PCNT peripheral register block.
[in]valA value to set in the top buffer register.

Definition at line 754 of file em_pcnt.c.

Referenced by PCNT_Reset().

__STATIC_INLINE uint32_t PCNT_TopGet ( PCNT_TypeDef *  pcnt)

Get pulse counter top value.

Parameters
[in]pcntPointer to PCNT peripheral register block.
Returns
Current pulse counter top value.

Definition at line 595 of file em_pcnt.h.

void PCNT_TopSet ( PCNT_TypeDef *  pcnt,
uint32_t  val 
)

Set the top value.

Note
This function will stall until synchronization to low-frequency domain is completed. For that reason, it should normally not be used when an external clock is used for the PCNT module since stall time may be undefined.
Parameters
[in]pcntA pointer to the PCNT peripheral register block.
[in]valA value to set in the top register.

Definition at line 779 of file em_pcnt.c.

References PCNT0_CNT_SIZE, PCNT1_CNT_SIZE, and PCNT2_CNT_SIZE.