Kinetis SDK v.1.3 API Reference Manual  Rev. 0
Freescale Semiconductor, Inc.
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages

Overview

The section describes the programming interface of the ENC Peripheral driver.

Overview

The enhanced Quadrature Encoder/Decoder module provides interfacing capability to position/speed sensors used in industrial motor control applications. It has four input signals: PHASEA, PHASEB, INDEX, and HOME. This module is used to decode shaft position, revolution count, and speed.

Initialization

To initialize the ENC module, call the ENC_DRV_Init() function and pass the configuration data structure (enc_user_config_t), which can be filled by the ENC_DRV_StructInitUserConfigNormal() function with the default settings for the encoder. After it is initialized, the ENC module can function as an ENC decoder. The default settings are as follows.

enc_state_t encUserState;
enc_user_config_t encUserConfig;
encUserConfig.operationMode = kEncNormalMode; /* Operation mode: Normal mode, modulo counting mode or bypass (signal phase count) mode.
encUserConfig.reverseCounting = false; /* Counting direction: Normal (false) or reverse (true) counting direction.
encUserConfig.indexInputNegativeEdge = false; /* Type of transition edge of INDEX input signal: positive (false) or negative (true).
encUserConfig.homeInputNegativeEdge = false; /* Type of transition edge of HOME input signal: positive (false) or negative (true).
encUserConfig.indexPulsePosInit = true; /* To use HOME (false) or INDEX (true) input to initialize position counter to value in Initialization Register.
encUserConfig.posCntInitValue = 0; /* Value to put in Initialization Register.
encUserConfig.posCmpValue = 0xFFFF; /* Value to put in Position Compare Register.
encUserConfig.moduloValue = 0; /* Value to put in Modulus Register.
encUserConfig.triggerUpdateHoldRegEnable = false; /* Enable/disable updating hold registers on TRIGGER input.
encUserConfig.triggerClearPosRegEnable = false; /* Enable/disable clear of position registers on TRIGGER input.
encUserConfig.moduloRevolutionCounting = false; /* Type of revolution counter - index pulse counting (on false) or modulo counting (on true).
encUserConfig.outputControlOnReading = false; /* Used to control the behaviour of the POSMATCH output signal. True - output control on reading position register, false - OC on match position register.
encUserConfig.watchdogTimeout = 0; /* Value to put in Watchdog Timeout Register.
encUserConfig.filterCount = 0; /* Value represents the number of consecutive samples that must agree prior to the input filter accepting an input transition.
encUserConfig.filterPeriod = 0; /* Value represents the sampling period (in IPBus clock cycles) of the decoder input signals.


Ensure that any default settings are changed after calling the ENC_DRV_StructInitUserConfigNormal() function. For example, changing INDEX input edge type and position compare register value:

encUserConfig.indexPulsePosInit = true;
encUserConfig.posCmpValue = 0x1010;


After that, the initialization is completed by calling the initialization function:

ENC_DRV_Init(instance, &encUserConfig, &encUserState);


To de-initialize the ENC peripheral, call the ENC_DRV_Deinit() function which shuts down the ENC clock to reduce the power consumption.

Testing ENC module

The ENC peripheral provides a test module to drive the ENC quadrature inputs which can provide a mechanism for customers to test the ENC module is functioning without being connected in the intended "incremental position sensor" for motor control.

To initialize the ENC Test module, call the ENC_DRV_TestInit() function and pass the configuration data structure (enc_test_config_t), which can be filled as the example shows.

enc_test_config_t encTestConfig;
encTestConfig.testNegativeSignalEnable = false;
encTestConfig.testCount = 100;
encTestConfig.testPeriod = 10;
ENC_DRV_TestInit(instance, &encTestConfig);


Test module generates 100 quadrature advances into quadrature inputs with period 10 IPBus clock cycles.

Input monitor

ENC peripheral also provides monitoring of input signals. It can be used as follows.

enc_input_monitor_t encInputMonitor;
ENC_DRV_ReadInputMonitorRegister(instance, true, &encInputMonitor);


The encInputMonitor structure contains the values of the raw (if true) or filtered (if false) PHASEA, PHASEB, INDEX and HOME input signals and the reset value of the raw and filtered values of PHASEA, PHASEB, INDEX, and HOME. If the input pins are connected to a pull-up, IMR 0-7 bits are set to one (1). If these input pins are connected to a pull-down device, the 0-7 bits are set to zeroes (0).

Interrupts

Interrupt may be enabled/disabled by calling the ENC_DRV_SetIntMode() function and passing the interrupt source enc_interrupt_source_t argument. The user callback function can be installed by calling the ENC_DRV_InstallCallback() function and passing the name as the type enc_callback_t argument. The parameter of user callback function may be passed too.
ENC peripheral provides these type of interrupts:

  1. Position compare interrupt
  2. HOME signal transition interrupt
  3. Watchdog time-out interrupt
  4. INDEX pulse transition interrupt
  5. Roll-under interrupt
  6. Roll-over interrupt
  7. Simultaneous PHASEA and PHASEB change interrupt

To get a status of any interrupt source use the ENC_DRV_GetIntMode() function. The flag status reading is provided by the ENC_DRV_GetStatusFlag() function and by clearing the flag with the ENC_DRV_ClearStatusFlag() function.

Reading Counters

To read the counters, call the ENC_DRV_ReadCounters() function and pass the argument of the enc_counter_t type. This function reads the position difference counter register that causes snapshots of the position, position difference counter and revolution counter registers are each placed into their respective hold registers. Then, the position counter value and revolution counter value are read from their hold registers. These three values are stored into a structure and passed as an argument of the function.
To reset counters (position, position difference, and revolution) use the ENC_DRV_ResetCounters() function.

Reading Hold Registers

To read an action, such as the trigger input signal, which causes getting snapshots of the position counter, position difference counter, and revolution counter register to their respective hold registers, use the ENC_DRV_ReadHoldRegisters() function.

Data Structures

struct  enc_user_config_t
 User configuration structure for ENC driver. More...
 
struct  enc_test_config_t
 User configuration structure for ENC driver - ENC test module configuration. More...
 
struct  enc_counter_t
 Counter registers structure for ENC driver. More...
 
struct  enc_input_monitor_t
 Input monitor structure for ENC driver. More...
 

Configuration

enc_status_t ENC_DRV_StructInitUserConfigNormal (enc_user_config_t *userConfigPtr)
 Fills the initial user configuration for the ENC module without the interrupts enablement. More...
 
enc_status_t ENC_DRV_Init (uint32_t instance, const enc_user_config_t *userConfigPtr)
 Initializes an ENC instance for operation. More...
 
void ENC_DRV_Deinit (uint32_t instance)
 De-initializes the ENC peripheral. More...
 
enc_status_t ENC_DRV_TestInit (uint32_t instance, const enc_test_config_t *userConfigPtr)
 Initializes an ENC test module. More...
 
void ENC_DRV_TestDeinit (uint32_t instance)
 Shuts down the ENC test module, disables test counter, and clears the test period and test count values. More...
 
enc_status_t ENC_DRV_SetIntMode (uint32_t instance, enc_int_source_t intSrc, bool enable)
 Enables/disables the selected ENC interrupt. More...
 
bool ENC_DRV_GetIntMode (uint32_t instance, enc_int_source_t intSrc)
 Gets the configuration of the selected ENC interrupt. More...
 
bool ENC_DRV_GetStatusFlag (uint32_t instance, enc_status_flag_t flag)
 Gets the interrupt status flag of the selected interrupt source. More...
 
void ENC_DRV_ClearStatusFlag (uint32_t instance, enc_status_flag_t flag)
 Clears the status flag of the selected status source. More...
 
enc_status_t ENC_DRV_ReadCounters (uint32_t instance, enc_counter_t *countRegPtr)
 Reads the actual values of the ENC counter registers. More...
 
enc_status_t ENC_DRV_ReadHoldReg (uint32_t instance, enc_counter_t *holdRegPtr)
 Reads the ENC hold registers. More...
 
void ENC_DRV_ResetCounters (uint32_t instance)
 Resets the ENC counter registers. More...
 
enc_status_t ENC_DRV_ReadInputMonitor (uint32_t instance, bool inputMonitorRaw, enc_input_monitor_t *inputMonitorPtr)
 Reads the ENC input monitor register. More...
 

Data Structure Documentation

struct enc_user_config_t

Use an instance of this structure with the ENC_DRV_Init()function. This enables configuration of the most common settings of the ENC peripheral with a single function call.

Data Fields

uint32_t posCntInitValue
 Value to put in Initialization Register.
 
uint32_t posCmpValue
 Value to put in Position Compare Register.
 
uint32_t moduloValue
 Value to put in Modulus Register.
 
uint16_t watchdogTimeout
 Value to put in Watchdog Timeout Register.
 
uint8_t filterCount
 Value represents the number of consecutive samples that must agree prior to the input filter accepting an input transition.
 
uint8_t filterPeriod
 Value represents the sampling period (in IPBus clock cycles) of the decoder input signals.
 
enc_operation_mode_t operationMode
 Operation mode: Normal mode, modulo counting mode or bypass (signal phase count) mode.
 
bool reverseCounting
 Counting direction: Normal (false) or reverse (true) counting direction.
 
bool indexInputNegativeEdge
 Type of transition edge of INDEX input signal: positive (false) or negative (true).
 
bool homeInputNegativeEdge
 Type of transition edge of HOME input signal: positive (false) or negative (true).
 
bool indexPulsePosInit
 To use HOME (false) or INDEX (true) input to initialize position counter to value in Initialization Register.
 
bool triggerUpdateHoldRegEnable
 Enable/disable updating hold registers on TRIGGER input.
 
bool triggerClearPosRegEnable
 Enable/disable clear of position registers on TRIGGER input.
 
bool moduloRevolutionCounting
 Type of revolution counter - index pulse counting (on false) or modulo counting (on true).
 
bool outputControlOnReading
 Used to control the behaviour of the POSMATCH output signal. More...
 

Field Documentation

bool enc_user_config_t::outputControlOnReading

True - output control on reading position register, false - OC on match position register.

struct enc_test_config_t

Use an instance of this structure with the ENC_DRV_TestInit()function. This enables configuration of the Test module of the ENC peripheral with a single function call.

Data Fields

uint8_t testCount
 Test count - holds the number of quadrature advances to generate. More...
 
uint8_t testPeriod
 Test period - holds the period of quadrature phase in IPBus clock cycles. More...
 
bool testNegativeSignalEnable
 Test signal type, positive (false) or negative (true). More...
 

Field Documentation

uint8_t enc_test_config_t::testCount
uint8_t enc_test_config_t::testPeriod
bool enc_test_config_t::testNegativeSignalEnable
struct enc_counter_t

Use an instance of this structure with the ENC_DRV_ReadHoldRegisters() or ENC_DRV_ReadCounters() functions. This reads counters and hold registers of Position, PositionDifference, Revolution Counter.

Data Fields

int32_t position
 Position Counter/Hold Register. More...
 
int16_t posDiff
 Position Difference Counter/Hold Register. More...
 
int16_t revolution
 Revolution Counter/Hold Register. More...
 

Field Documentation

int32_t enc_counter_t::position
int16_t enc_counter_t::posDiff
int16_t enc_counter_t::revolution
struct enc_input_monitor_t

Use an instance of this structure with the ENC_DRV_ReadInputMonitorRegister(). This reads Input Monitor register that contains the values of the raw or filtered PHASEA, PHASEB, INDEX and HOME input signals.

Data Fields

bool phaseA
 PHASEA input. More...
 
bool phaseB
 PHASEB input. More...
 
bool index
 INDEX input. More...
 
bool home
 HOME input. More...
 

Field Documentation

bool enc_input_monitor_t::phaseA
bool enc_input_monitor_t::phaseB
bool enc_input_monitor_t::index
bool enc_input_monitor_t::home

Function Documentation

enc_status_t ENC_DRV_StructInitUserConfigNormal ( enc_user_config_t userConfigPtr)

This function fills the initial user configuration. Calling the initialization function with the filled parameter configures the ENC module to function as a simple Quadrature Encoder. The settings are:

encUserConfig.operationMode = kEncNormalMode;
encUserConfig.reverseCounting = false;
encUserConfig.indexInputNegativeEdge = false;
encUserConfig.homeInputNegativeEdge = false;
encUserConfig.indexPulsePosInit = true;
encUserConfig.posCntInitValue = 0U;
encUserConfig.posCmpValue = 0xFFFFU;
encUserConfig.moduloValue = 0U;
encUserConfig.triggerUpdateHoldRegEnable = false;
encUserConfig.triggerClearPosRegEnable = false;
encUserConfig.moduloRevolutionCounting = false;
encUserConfig.outputControlOnReading = false;
encUserConfig.watchdogTimeout = 0U;
encUserConfig.filterCount = 0U;
encUserConfig.filterPeriod = 0U;
Parameters
userConfigPtrPointer to the user configuration structure.
Returns
Execution status.
enc_status_t ENC_DRV_Init ( uint32_t  instance,
const enc_user_config_t userConfigPtr 
)

This function initializes the run-time state structure to un-gate the clock to the ENC module, initializes the module to user-defined settings and default settings, configures the IRQ state structure, and enables the module-level interrupt to the core. This example shows how to set up the enc_state_t and the enc_user_config_t parameters and how to call the ENC_DRV_Init function by passing in these parameters:

enc_user_config_t encUserConfig;
encUserConfig.operationMode = kEncNormalMode;
encUserConfig.reverseCounting = false;
encUserConfig.indexInputNegativeEdge = false;
encUserConfig.homeInputNegativeEdge = false;
encUserConfig.indexPulsePosInit = true;
encUserConfig.posCntInitValue = 0U;
encUserConfig.posCmpValue = 0xFFFFU;
encUserConfig.moduloValue = 0U;
encUserConfig.triggerUpdateHoldRegEnable = false;
encUserConfig.triggerClearPosRegEnable = false;
encUserConfig.moduloRevolutionCounting = false;
encUserConfig.outputControlOnReading = false;
encUserConfig.watchdogTimeout = 0U;
encUserConfig.filterCount = 0U;
encUserConfig.filterPeriod = 0U;
ENC_DRV_Init(&encUserConfig);
Parameters
instanceENC instance ID.
userConfigPtrThe user configuration structure of type enc_user_config_t. The user is responsible to fill out the members of this structure and to pass the pointer of this structure into this function.
Returns
Execution status.
void ENC_DRV_Deinit ( uint32_t  instance)

This function shuts down the ENC clock to reduce power consumption.

Parameters
instanceENC instance ID.
enc_status_t ENC_DRV_TestInit ( uint32_t  instance,
const enc_test_config_t userConfigPtr 
)

This function initializes the run-time state structure to enable the test module and sets the test period and test count values. This example shows how to set up the enc_test_config_t parameters and how to call the ENC_DRV_TestInit function by passing in these parameters:

enc_test_config_t encTestConfig;
encTestConfig.testNegativeSignalEnable = false;
encTestConfig.testCount = 100;
encTestConfig.testPeriod = 10;
ENC_DRV_TestInit(&encTestConfig);
Parameters
instanceENC instance ID.
userConfigPtrThe user configuration structure of type enc_test_config_t.
Returns
Execution status.
void ENC_DRV_TestDeinit ( uint32_t  instance)
Parameters
instanceENC instance ID.
enc_status_t ENC_DRV_SetIntMode ( uint32_t  instance,
enc_int_source_t  intSrc,
bool  enable 
)

The interrupt source is passing as argument of type enc_interrupt_source_t.

Parameters
instanceENC instance ID.
intSrcThe type of interrupt to enable/disable.
enableBool parameter to enable/disable.
Returns
Execution status.
bool ENC_DRV_GetIntMode ( uint32_t  instance,
enc_int_source_t  intSrc 
)

The interrupt type is passing as an argument of type enc_int_source_t.

Parameters
instanceENC instance ID.
intSrcThe type of interrupt to get configuration.
Returns
Configuration of selected interrupt source.
bool ENC_DRV_GetStatusFlag ( uint32_t  instance,
enc_status_flag_t  flag 
)
Parameters
instanceENC instance ID.
flagSelected type of status flag.
Returns
State of selected flag.
void ENC_DRV_ClearStatusFlag ( uint32_t  instance,
enc_status_flag_t  flag 
)
Parameters
instanceENC instance ID.
flagSelected type of status flag.
enc_status_t ENC_DRV_ReadCounters ( uint32_t  instance,
enc_counter_t countRegPtr 
)
Parameters
instanceENC instance ID.
countRegPtrThe structure of ENC counter registers.
Returns
Execution status.
enc_status_t ENC_DRV_ReadHoldReg ( uint32_t  instance,
enc_counter_t holdRegPtr 
)
Parameters
instanceENC instance ID.
holdRegPtrThe structure of ENC hold registers.
Returns
Execution status.
void ENC_DRV_ResetCounters ( uint32_t  instance)
Parameters
instanceENC instance ID.
enc_status_t ENC_DRV_ReadInputMonitor ( uint32_t  instance,
bool  inputMonitorRaw,
enc_input_monitor_t inputMonitorPtr 
)
Parameters
instanceENC instance ID.
inputMonitorRawThe type of input monitor - raw (true) / filtered (false).
inputMonitorPtrThe structure of ENC Monitor register variables.
Returns
Execution status.