iLLD_TC27xD  1.0
How to use the GPT12 Incremental Encoder Driver
Collaboration diagram for How to use the GPT12 Incremental Encoder Driver:

The IncrEnc interface can be used in one of the following configurations T3 as core or T2 as core

Setup with T3 as core

Setup with T2 as core

Preparation

Include Files

Include following header file into your C code:

#include <Gpt12/Gpt/IfxGpt12_IncrEnc.h>

Variables

// used globally
static IfxGpt12_IncrEnc gpt12;

Interrupt Handler Installation

See also How to define Interrupts?

Define priorities for the Interrrupt handlers. This is normally done in the Ifx_IntPrioDef.h file:

// priorities are normally defined in Ifx_IntPrioDef.h
#define ISR_PRIORITY_INCRENC_ZERO 6

Add the interrupt service routines to your C code. They have to call the GPT12 interrupt handlers by passing the gpt12 handle:

IFX_INTERRUPT(ISR_IncrIncZero, 0, ISR_PRIORITY_INCRENC_ZERO)
{
}

Finally install the interrupt handlers in your initialisation function:

// install interrupt handlers
IfxCpu_Irq_installInterruptHandler(&ISR_IncrIncZero, ISR_PRIORITY_INCRENC_ZERO);

Module Initialisation

The module initialisation can be done in the same function. Here an example:

// Initialize global clocks
IfxGpt12_enableModule(&MODULE_GPT120);
// create module config
IfxGpt12_IncrEnc_initConfig(&gpt12Config , &MODULE_GPT120);
// implementation with T3 as core
gpt12Config.base.offset = 0;
gpt12Config.base.reversed = FALSE;
gpt12Config.base.resolution = 2048;
gpt12Config.base.periodPerRotation = 1;
gpt12Config.base.updatePeriod = 100e-6;
gpt12Config.base.speedModeThreshold = 200;
gpt12Config.base.minSpeed = 10;
gpt12Config.base.maxSpeed = 500;
gpt12Config.zeroIsrPriority = ISR_PRIORITY(INTERRUPT_INCRINC_ZERO);
gpt12Config.zeroIsrProvider = ISR_PROVIDER(INTERRUPT_INCRINC_ZERO);
gpt12Config.base.speedFilerCutOffFrequency = config.base.maxSpeed / 2 * IFX_PI * 2;
// initialize module
//IfxGpt12_IncrEnc gpt12; // defined globally
IfxGpt12_IncrEnc_init(&gpt12, &gpt12Config);

Update

speed, position and direction of the incremental encoder can be collected by the following

float32 speed, rawPosition;
IfxStdIf_Pos_Dir direction;
Ifx_TickTime tickRefresh = gpt12.updatePeriod * TimeConst_1s;
Ifx_TickTime refreshDeadLine = now();
if (isDeadLine(refreshDeadLine))
{
refreshDeadLine = addTTime(refreshDeadLine, tickRefresh);
speed = IfxGpt12_IncrEnc_getSpeed(&gpt12);
rawPosition = IfxGpt12_IncrEnc_getRawPosition(&gpt12);
direction = IfxGpt12_IncrEnc_getDirection(&gpt12);
}