MCUXpresso SDK API Reference Manual  Rev. 0
NXP Semiconductors
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
ENC: Quadrature Encoder/Decoder

Overview

The MCUXpresso SDK provides a peripheral driver for the Quadrature Encoder/Decoder (ENC) module of MCUXpresso SDK devices.

This section describes the programming interface of the ENC Peripheral driver. The ENC driver configures the ENC module and provides a functional interface for the user to build the ENC application.

Function groups

Initialization and De-initialization

This function group initializes default configuration structure for the ENC counter and initializes ENC counter with the normal configuration and de-initialize ENC module. Some APIs are also created to control the features.

Status

This function group get/clear the ENC status.

Interrupts

This function group enable/disable the ENC interrupts.

Value Operation

This function group get the counter/hold value of positions.

Typical use case

Polling Configuration

Refer to the driver examples codes located at <SDK_ROOT>/boards/<BOARD>/driver_examples/enc

Data Structures

struct  enc_config_t
 Define user configuration structure for ENC module. More...
 
struct  enc_self_test_config_t
 Define configuration structure for self test module. More...
 

Enumerations

enum  _enc_interrupt_enable {
  kENC_HOMETransitionInterruptEnable = (1U << 0U),
  kENC_INDEXPulseInterruptEnable = (1U << 1U),
  kENC_WatchdogTimeoutInterruptEnable = (1U << 2U),
  kENC_PositionCompareInerruptEnable = (1U << 3U),
  kENC_SimultBothPhaseChangeInterruptEnable,
  kENC_PositionRollOverInterruptEnable = (1U << 5U),
  kENC_PositionRollUnderInterruptEnable = (1U << 6U)
}
 Interrupt enable/disable mask. More...
 
enum  _enc_status_flags {
  kENC_HOMETransitionFlag = (1U << 0U),
  kENC_INDEXPulseFlag = (1U << 1U),
  kENC_WatchdogTimeoutFlag = (1U << 2U),
  kENC_PositionCompareFlag = (1U << 3U),
  kENC_SimultBothPhaseChangeFlag = (1U << 4U),
  kENC_PositionRollOverFlag = (1U << 5U),
  kENC_PositionRollUnderFlag = (1U << 6U),
  kENC_LastCountDirectionFlag = (1U << 7U)
}
 Status flag mask. More...
 
enum  _enc_signal_status_flags {
  kENC_RawHOMEStatusFlag = ENC_IMR_HOME_MASK,
  kENC_RawINDEXStatusFlag = ENC_IMR_INDEX_MASK,
  kENC_RawPHBStatusFlag = ENC_IMR_PHB_MASK,
  kENC_RawPHAEXStatusFlag = ENC_IMR_PHA_MASK,
  kENC_FilteredHOMEStatusFlag = ENC_IMR_FHOM_MASK,
  kENC_FilteredINDEXStatusFlag = ENC_IMR_FIND_MASK,
  kENC_FilteredPHBStatusFlag = ENC_IMR_FPHB_MASK,
  kENC_FilteredPHAStatusFlag = ENC_IMR_FPHA_MASK
}
 Signal status flag mask. More...
 
enum  enc_home_trigger_mode_t {
  kENC_HOMETriggerDisabled = 0U,
  kENC_HOMETriggerOnRisingEdge,
  kENC_HOMETriggerOnFallingEdge
}
 Define HOME signal's trigger mode. More...
 
enum  enc_index_trigger_mode_t {
  kENC_INDEXTriggerDisabled = 0U,
  kENC_INDEXTriggerOnRisingEdge,
  kENC_INDEXTriggerOnFallingEdge
}
 Define INDEX signal's trigger mode. More...
 
enum  enc_decoder_work_mode_t {
  kENC_DecoderWorkAsNormalMode = 0U,
  kENC_DecoderWorkAsSignalPhaseCountMode
}
 Define type for decoder work mode. More...
 
enum  enc_position_match_mode_t {
  kENC_POSMATCHOnPositionCounterEqualToComapreValue = 0U,
  kENC_POSMATCHOnReadingAnyPositionCounter
}
 Define type for the condition of POSMATCH pulses. More...
 
enum  enc_revolution_count_condition_t {
  kENC_RevolutionCountOnINDEXPulse = 0U,
  kENC_RevolutionCountOnRollOverModulus
}
 Define type for determining how the revolution counter (REV) is incremented/decremented. More...
 
enum  enc_self_test_direction_t {
  kENC_SelfTestDirectionPositive = 0U,
  kENC_SelfTestDirectionNegative
}
 Define type for direction of self test generated signal. More...
 

Variables

bool enc_config_t::enableReverseDirection
 Enable reverse direction counting. More...
 
enc_decoder_work_mode_t enc_config_t::decoderWorkMode
 Enable signal phase count mode. More...
 
enc_home_trigger_mode_t enc_config_t::HOMETriggerMode
 Enable HOME to initialize position counters. More...
 
enc_index_trigger_mode_t enc_config_t::INDEXTriggerMode
 Enable INDEX to initialize position counters. More...
 
bool enc_config_t::enableTRIGGERClearPositionCounter
 Clear POSD, REV, UPOS and LPOS on rising edge of TRIGGER, or not. More...
 
bool enc_config_t::enableTRIGGERClearHoldPositionCounter
 Enable update of hold registers on rising edge of TRIGGER, or not.
 
bool enc_config_t::enableWatchdog
 Enable the watchdog to detect if the target is moving or not. More...
 
uint16_t enc_config_t::watchdogTimeoutValue
 Watchdog timeout count value. More...
 
uint16_t enc_config_t::filterCount
 Input Filter Sample Count. More...
 
uint16_t enc_config_t::filterSamplePeriod
 Input Filter Sample Period. More...
 
enc_position_match_mode_t enc_config_t::positionMatchMode
 The condition of POSMATCH pulses. More...
 
uint32_t enc_config_t::positionCompareValue
 Position compare value. More...
 
enc_revolution_count_condition_t enc_config_t::revolutionCountCondition
 Revolution Counter Modulus Enable. More...
 
bool enc_config_t::enableModuloCountMode
 Enable Modulo Counting. More...
 
uint32_t enc_config_t::positionModulusValue
 Position modulus value. More...
 
uint32_t enc_config_t::positionInitialValue
 Position initial value. More...
 
enc_self_test_direction_t enc_self_test_config_t::signalDirection
 Direction of self test generated signal. More...
 
uint16_t enc_self_test_config_t::signalCount
 Hold the number of quadrature advances to generate. More...
 
uint16_t enc_self_test_config_t::signalPeriod
 Hold the period of quadrature phase in IPBus clock cycles. More...
 

Initialization and De-initialization

void ENC_Init (ENC_Type *base, const enc_config_t *config)
 Initialization for the ENC module. More...
 
void ENC_Deinit (ENC_Type *base)
 De-initialization for the ENC module. More...
 
void ENC_GetDefaultConfig (enc_config_t *config)
 Get an available pre-defined settings for ENC's configuration. More...
 
void ENC_DoSoftwareLoadInitialPositionValue (ENC_Type *base)
 Load the initial position value to position counter. More...
 
void ENC_SetSelfTestConfig (ENC_Type *base, const enc_self_test_config_t *config)
 Enable and configure the self test function. More...
 
void ENC_EnableWatchdog (ENC_Type *base, bool enable)
 Enable watchdog for ENC module. More...
 
void ENC_SetInitialPositionValue (ENC_Type *base, uint32_t value)
 Set initial position value for ENC module. More...
 

Status

uint32_t ENC_GetStatusFlags (ENC_Type *base)
 Get the status flags. More...
 
void ENC_ClearStatusFlags (ENC_Type *base, uint32_t mask)
 Clear the status flags. More...
 
static uint16_t ENC_GetSignalStatusFlags (ENC_Type *base)
 Get the signals' real-time status. More...
 

Interrupts

void ENC_EnableInterrupts (ENC_Type *base, uint32_t mask)
 Enable the interrupts. More...
 
void ENC_DisableInterrupts (ENC_Type *base, uint32_t mask)
 Disable the interrupts. More...
 
uint32_t ENC_GetEnabledInterrupts (ENC_Type *base)
 Get the enabled interrupts' flags. More...
 

Value Operation

uint32_t ENC_GetPositionValue (ENC_Type *base)
 Get the current position counter's value. More...
 
uint32_t ENC_GetHoldPositionValue (ENC_Type *base)
 Get the hold position counter's value. More...
 
static uint16_t ENC_GetPositionDifferenceValue (ENC_Type *base)
 Get the position difference counter's value. More...
 
static uint16_t ENC_GetHoldPositionDifferenceValue (ENC_Type *base)
 Get the hold position difference counter's value. More...
 
static uint16_t ENC_GetRevolutionValue (ENC_Type *base)
 Get the position revolution counter's value. More...
 
static uint16_t ENC_GetHoldRevolutionValue (ENC_Type *base)
 Get the hold position revolution counter's value. More...
 

Data Structure Documentation

struct enc_config_t

Data Fields

bool enableReverseDirection
 Enable reverse direction counting. More...
 
enc_decoder_work_mode_t decoderWorkMode
 Enable signal phase count mode. More...
 
enc_home_trigger_mode_t HOMETriggerMode
 Enable HOME to initialize position counters. More...
 
enc_index_trigger_mode_t INDEXTriggerMode
 Enable INDEX to initialize position counters. More...
 
bool enableTRIGGERClearPositionCounter
 Clear POSD, REV, UPOS and LPOS on rising edge of TRIGGER, or not. More...
 
bool enableTRIGGERClearHoldPositionCounter
 Enable update of hold registers on rising edge of TRIGGER, or not.
 
bool enableWatchdog
 Enable the watchdog to detect if the target is moving or not. More...
 
uint16_t watchdogTimeoutValue
 Watchdog timeout count value. More...
 
uint16_t filterCount
 Input Filter Sample Count. More...
 
uint16_t filterSamplePeriod
 Input Filter Sample Period. More...
 
enc_position_match_mode_t positionMatchMode
 The condition of POSMATCH pulses. More...
 
uint32_t positionCompareValue
 Position compare value. More...
 
enc_revolution_count_condition_t revolutionCountCondition
 Revolution Counter Modulus Enable. More...
 
bool enableModuloCountMode
 Enable Modulo Counting. More...
 
uint32_t positionModulusValue
 Position modulus value. More...
 
uint32_t positionInitialValue
 Position initial value. More...
 
struct enc_self_test_config_t

The self test module provides a quadrature test signal to the inputs of the quadrature decoder module. This is a factory test feature. It is also useful to customers' software development and testing.

Data Fields

enc_self_test_direction_t signalDirection
 Direction of self test generated signal. More...
 
uint16_t signalCount
 Hold the number of quadrature advances to generate. More...
 
uint16_t signalPeriod
 Hold the period of quadrature phase in IPBus clock cycles. More...
 

Enumeration Type Documentation

Enumerator
kENC_HOMETransitionInterruptEnable 

HOME interrupt enable.

kENC_INDEXPulseInterruptEnable 

INDEX pulse interrupt enable.

kENC_WatchdogTimeoutInterruptEnable 

Watchdog timeout interrupt enable.

kENC_PositionCompareInerruptEnable 

Position compare interrupt enable.

kENC_SimultBothPhaseChangeInterruptEnable 

Simultaneous PHASEA and PHASEB change interrupt enable.

kENC_PositionRollOverInterruptEnable 

Roll-over interrupt enable.

kENC_PositionRollUnderInterruptEnable 

Roll-under interrupt enable.

These flags indicate the counter's events.

Enumerator
kENC_HOMETransitionFlag 

HOME signal transition interrupt request.

kENC_INDEXPulseFlag 

INDEX Pulse Interrupt Request.

kENC_WatchdogTimeoutFlag 

Watchdog timeout interrupt request.

kENC_PositionCompareFlag 

Position compare interrupt request.

kENC_SimultBothPhaseChangeFlag 

Simultaneous PHASEA and PHASEB change interrupt request.

kENC_PositionRollOverFlag 

Roll-over interrupt request.

kENC_PositionRollUnderFlag 

Roll-under interrupt request.

kENC_LastCountDirectionFlag 

Last count was in the up direction, or the down direction.

These flags indicate the counter's signal.

Enumerator
kENC_RawHOMEStatusFlag 

Raw HOME input.

kENC_RawINDEXStatusFlag 

Raw INDEX input.

kENC_RawPHBStatusFlag 

Raw PHASEB input.

kENC_RawPHAEXStatusFlag 

Raw PHASEA input.

kENC_FilteredHOMEStatusFlag 

The filtered version of HOME input.

kENC_FilteredINDEXStatusFlag 

The filtered version of INDEX input.

kENC_FilteredPHBStatusFlag 

The filtered version of PHASEB input.

kENC_FilteredPHAStatusFlag 

The filtered version of PHASEA input.

The ENC would count the trigger from HOME signal line.

Enumerator
kENC_HOMETriggerDisabled 

HOME signal's trigger is disabled.

kENC_HOMETriggerOnRisingEdge 

Use positive going edge-to-trigger initialization of position counters.

kENC_HOMETriggerOnFallingEdge 

Use negative going edge-to-trigger initialization of position counters.

The ENC would count the trigger from INDEX signal line.

Enumerator
kENC_INDEXTriggerDisabled 

INDEX signal's trigger is disabled.

kENC_INDEXTriggerOnRisingEdge 

Use positive going edge-to-trigger initialization of position counters.

kENC_INDEXTriggerOnFallingEdge 

Use negative going edge-to-trigger initialization of position counters.

The normal work mode uses the standard quadrature decoder with PHASEA and PHASEB. When in signal phase count mode, a positive transition of the PHASEA input generates a count signal while the PHASEB input and the reverse direction control the counter direction. If the reverse direction is not enabled, PHASEB = 0 means counting up and PHASEB = 1 means counting down. Otherwise, the direction is reversed.

Enumerator
kENC_DecoderWorkAsNormalMode 

Use standard quadrature decoder with PHASEA and PHASEB.

kENC_DecoderWorkAsSignalPhaseCountMode 

PHASEA input generates a count signal while PHASEB input control the direction.

Enumerator
kENC_POSMATCHOnPositionCounterEqualToComapreValue 

POSMATCH pulses when a match occurs between the position counters (POS) and the compare value (COMP).

kENC_POSMATCHOnReadingAnyPositionCounter 

POSMATCH pulses when any position counter register is read.

Enumerator
kENC_RevolutionCountOnINDEXPulse 

Use INDEX pulse to increment/decrement revolution counter.

kENC_RevolutionCountOnRollOverModulus 

Use modulus counting roll-over/under to increment/decrement revolution counter.

Enumerator
kENC_SelfTestDirectionPositive 

Self test generates the signal in positive direction.

kENC_SelfTestDirectionNegative 

Self test generates the signal in negative direction.

Function Documentation

void ENC_Init ( ENC_Type *  base,
const enc_config_t config 
)

This function is to make the initialization for the ENC module. It should be called firstly before any operation to the ENC with the operations like:

  • Enable the clock for ENC module.
  • Configure the ENC's working attributes.
Parameters
baseENC peripheral base address.
configPointer to configuration structure. See to "enc_config_t".
void ENC_Deinit ( ENC_Type *  base)

This function is to make the de-initialization for the ENC module. It could be called when ENC is no longer used with the operations like:

  • Disable the clock for ENC module.
Parameters
baseENC peripheral base address.
void ENC_GetDefaultConfig ( enc_config_t config)

This function initializes the ENC configuration structure with an available settings, the default value are:

* config->enableReverseDirection = false;
* config->decoderWorkMode = kENC_DecoderWorkAsNormalMode;
* config->HOMETriggerMode = kENC_HOMETriggerDisabled;
* config->INDEXTriggerMode = kENC_INDEXTriggerDisabled;
* config->enableTRIGGERClearPositionCounter = false;
* config->enableTRIGGERClearHoldPositionCounter = false;
* config->enableWatchdog = false;
* config->watchdogTimeoutValue = 0U;
* config->filterCount = 0U;
* config->filterSamplePeriod = 0U;
* config->positionCompareValue = 0xFFFFFFFFU;
* config->revolutionCountCondition = kENC_RevolutionCountOnINDEXPulse;
* config->enableModuloCountMode = false;
* config->positionModulusValue = 0U;
* config->positionInitialValue = 0U;
*
Parameters
configPointer to a variable of configuration structure. See to "enc_config_t".
void ENC_DoSoftwareLoadInitialPositionValue ( ENC_Type *  base)

This function is to transfer the initial position value (UINIT and LINIT) contents to position counter (UPOS and LPOS), so that to provide the consistent operation the position counter registers.

Parameters
baseENC peripheral base address.
void ENC_SetSelfTestConfig ( ENC_Type *  base,
const enc_self_test_config_t config 
)

This function is to enable and configuration the self test function. It controls and sets the frequency of a quadrature signal generator. It provides a quadrature test signal to the inputs of the quadrature decoder module. It is a factory test feature; however, it may be useful to customers' software development and testing.

Parameters
baseENC peripheral base address.
configPointer to configuration structure. See to "enc_self_test_config_t". Pass "NULL" to disable.
void ENC_EnableWatchdog ( ENC_Type *  base,
bool  enable 
)
Parameters
baseENC peripheral base address
enableEnables or disables the watchdog
void ENC_SetInitialPositionValue ( ENC_Type *  base,
uint32_t  value 
)
Parameters
baseENC peripheral base address
valuePositive initial value
uint32_t ENC_GetStatusFlags ( ENC_Type *  base)
Parameters
baseENC peripheral base address.
Returns
Mask value of status flags. For available mask, see to "_enc_status_flags".
void ENC_ClearStatusFlags ( ENC_Type *  base,
uint32_t  mask 
)
Parameters
baseENC peripheral base address.
maskMask value of status flags to be cleared. For available mask, see to "_enc_status_flags".
static uint16_t ENC_GetSignalStatusFlags ( ENC_Type *  base)
inlinestatic
Parameters
baseENC peripheral base address.
Returns
Mask value of signals' real-time status. For available mask, see to "_enc_signal_status_flags"
void ENC_EnableInterrupts ( ENC_Type *  base,
uint32_t  mask 
)
Parameters
baseENC peripheral base address.
maskMask value of interrupts to be enabled. For available mask, see to "_enc_interrupt_enable".
void ENC_DisableInterrupts ( ENC_Type *  base,
uint32_t  mask 
)
Parameters
baseENC peripheral base address.
maskMask value of interrupts to be disabled. For available mask, see to "_enc_interrupt_enable".
uint32_t ENC_GetEnabledInterrupts ( ENC_Type *  base)
Parameters
baseENC peripheral base address.
Returns
Mask value of enabled interrupts.
uint32_t ENC_GetPositionValue ( ENC_Type *  base)
Parameters
baseENC peripheral base address.
Returns
Current position counter's value.
uint32_t ENC_GetHoldPositionValue ( ENC_Type *  base)

When any of the counter registers is read, the contents of each counter register is written to the corresponding hold register. Taking a snapshot of the counters' values provides a consistent view of a system position and a velocity to be attained.

Parameters
baseENC peripheral base address.
Returns
Hold position counter's value.
static uint16_t ENC_GetPositionDifferenceValue ( ENC_Type *  base)
inlinestatic
Parameters
baseENC peripheral base address.
Returns
The position difference counter's value.
static uint16_t ENC_GetHoldPositionDifferenceValue ( ENC_Type *  base)
inlinestatic

When any of the counter registers is read, the contents of each counter register is written to the corresponding hold register. Taking a snapshot of the counters' values provides a consistent view of a system position and a velocity to be attained.

Parameters
baseENC peripheral base address.
Returns
Hold position difference counter's value.
static uint16_t ENC_GetRevolutionValue ( ENC_Type *  base)
inlinestatic
Parameters
baseENC peripheral base address.
Returns
The position revolution counter's value.
static uint16_t ENC_GetHoldRevolutionValue ( ENC_Type *  base)
inlinestatic

When any of the counter registers is read, the contents of each counter register is written to the corresponding hold register. Taking a snapshot of the counters' values provides a consistent view of a system position and a velocity to be attained.

Parameters
baseENC peripheral base address.
Returns
Hold position revolution counter's value.

Variable Documentation

bool enc_config_t::enableReverseDirection
enc_decoder_work_mode_t enc_config_t::decoderWorkMode
enc_home_trigger_mode_t enc_config_t::HOMETriggerMode
enc_index_trigger_mode_t enc_config_t::INDEXTriggerMode
bool enc_config_t::enableTRIGGERClearPositionCounter
bool enc_config_t::enableWatchdog
uint16_t enc_config_t::watchdogTimeoutValue

It stores the timeout count for the quadrature decoder module watchdog timer. This field is only available when "enableWatchdog" = true. The available value is a 16-bit unsigned number.

uint16_t enc_config_t::filterCount

This value should be chosen to reduce the probability of noisy samples causing an incorrect transition to be recognized. The value represent the number of consecutive samples that must agree prior to the input filter accepting an input transition. A value of 0x0 represents 3 samples. A value of 0x7 represents 10 samples. The Available range is 0 - 7.

uint16_t enc_config_t::filterSamplePeriod

This value should be set such that the sampling period is larger than the period of the expected noise. This value represents the sampling period (in IPBus clock cycles) of the decoder input signals. The available range is 0 - 255.

enc_position_match_mode_t enc_config_t::positionMatchMode
uint32_t enc_config_t::positionCompareValue

The available value is a 32-bit number.

enc_revolution_count_condition_t enc_config_t::revolutionCountCondition
bool enc_config_t::enableModuloCountMode
uint32_t enc_config_t::positionModulusValue

This value would be available only when "enableModuloCountMode" = true. The available value is a 32-bit number.

uint32_t enc_config_t::positionInitialValue

The available value is a 32-bit number.

enc_self_test_direction_t enc_self_test_config_t::signalDirection
uint16_t enc_self_test_config_t::signalCount

The available range is 0 - 255.

uint16_t enc_self_test_config_t::signalPeriod

The available range is 0 - 31.