Skip to content

Commit

Permalink
Add configuration to use second encoder (EQEP2) (#46)
Browse files Browse the repository at this point in the history
* Add setting to use second encoder.

* Got EQEP2 working.  Rolled version number.
  • Loading branch information
clough42 authored Jan 13, 2020
1 parent adf633e commit 0837df2
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 26 deletions.
6 changes: 6 additions & 0 deletions els-f280049c/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,14 @@
// four times the number of pulses. For example, if you have a 1024 P/R
// encoder, you need to enter 4096 here.
//================================================================================

// Encoder resolution (counts per revolution)
#define ENCODER_RESOLUTION 4096

// Which encoder input to use
#define ENCODER_USE_EQEP1
//#define ENCODER_USE_EQEP2




Expand Down
66 changes: 42 additions & 24 deletions els-f280049c/Encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,45 +38,63 @@ void Encoder :: initHardware(void)
{
EALLOW;

GpioCtrlRegs.GPBPUD.bit.GPIO35 = 0; // Enable pull-up on GPIO20 (EQEP1A)
GpioCtrlRegs.GPBPUD.bit.GPIO37 = 0; // Enable pull-up on GPIO21 (EQEP1B)
GpioCtrlRegs.GPBPUD.bit.GPIO59 = 0; // Enable pull-up on GPIO23 (EQEP1I)
#ifdef ENCODER_USE_EQEP1
GpioCtrlRegs.GPBPUD.bit.GPIO35 = 0; // Enable pull-up on GPIO35 (EQEP1A)
GpioCtrlRegs.GPBPUD.bit.GPIO37 = 0; // Enable pull-up on GPIO371 (EQEP1B)
GpioCtrlRegs.GPBPUD.bit.GPIO59 = 0; // Enable pull-up on GPIO59 (EQEP1I)

GpioCtrlRegs.GPBQSEL1.bit.GPIO35 = 0; // Sync to SYSCLKOUT GPIO20 (EQEP1A)
GpioCtrlRegs.GPBQSEL1.bit.GPIO37 = 0; // Sync to SYSCLKOUT GPIO21 (EQEP1B)
GpioCtrlRegs.GPBQSEL2.bit.GPIO59 = 0; // Sync to SYSCLKOUT GPIO23 (EQEP1I)
GpioCtrlRegs.GPBQSEL1.bit.GPIO35 = 0; // Sync to SYSCLKOUT GPIO35 (EQEP1A)
GpioCtrlRegs.GPBQSEL1.bit.GPIO37 = 0; // Sync to SYSCLKOUT GPIO37 (EQEP1B)
GpioCtrlRegs.GPBQSEL2.bit.GPIO59 = 0; // Sync to SYSCLKOUT GPIO59 (EQEP1I)

GpioCtrlRegs.GPBMUX1.bit.GPIO35 = 1; // Configure GPIO20 as EQEP1A
GpioCtrlRegs.GPBMUX1.bit.GPIO35 = 1; // Configure GPIO35 as EQEP1A
GpioCtrlRegs.GPBGMUX1.bit.GPIO35 = 2;
GpioCtrlRegs.GPBMUX1.bit.GPIO37 = 1; // Configure GPIO20 as EQEP1A
GpioCtrlRegs.GPBMUX1.bit.GPIO37 = 1; // Configure GPIO37 as EQEP1B
GpioCtrlRegs.GPBGMUX1.bit.GPIO37 = 2;
GpioCtrlRegs.GPBMUX2.bit.GPIO59 = 3; // Configure GPIO20 as EQEP1A
GpioCtrlRegs.GPBMUX2.bit.GPIO59 = 3; // Configure GPIO59 as EQEP1I
GpioCtrlRegs.GPBGMUX2.bit.GPIO59 = 2;
#endif
#ifdef ENCODER_USE_EQEP2
GpioCtrlRegs.GPAPUD.bit.GPIO14 = 0; // Enable pull-up on GPIO14 (EQEP2A)
GpioCtrlRegs.GPAPUD.bit.GPIO15 = 0; // Enable pull-up on GPIO15 (EQEP2B)
GpioCtrlRegs.GPAPUD.bit.GPIO26 = 0; // Enable pull-up on GPIO26 (EQEP2I)

GpioCtrlRegs.GPAQSEL1.bit.GPIO14 = 0; // Sync to SYSCLKOUT GPIO14 (EQEP2A)
GpioCtrlRegs.GPAQSEL1.bit.GPIO15 = 0; // Sync to SYSCLKOUT GPIO15 (EQEP2B)
GpioCtrlRegs.GPAQSEL2.bit.GPIO26 = 0; // Sync to SYSCLKOUT GPIO26 (EQEP2I)

GpioCtrlRegs.GPAMUX1.bit.GPIO14 = 2; // Configure GPIO14 as EQEP2A
GpioCtrlRegs.GPAGMUX1.bit.GPIO14 = 2;
GpioCtrlRegs.GPAMUX1.bit.GPIO15 = 2; // Configure GPIO15 as EQEP2B
GpioCtrlRegs.GPAGMUX1.bit.GPIO15 = 2;
GpioCtrlRegs.GPAMUX2.bit.GPIO26 = 2; // Configure GPIO26 as EQEP2I
GpioCtrlRegs.GPAGMUX2.bit.GPIO26 = 0;
#endif

EDIS;

EQep1Regs.QDECCTL.bit.QSRC = 0; // QEP quadrature count mode
EQep1Regs.QDECCTL.bit.IGATE = 1; // gate the index pin
EQep1Regs.QDECCTL.bit.QAP = 1; // invert A input
EQep1Regs.QDECCTL.bit.QBP = 1; // invert B input
EQep1Regs.QDECCTL.bit.QIP = 1; // invert index input
EQep1Regs.QEPCTL.bit.FREE_SOFT = 2; // unaffected by emulation suspend
EQep1Regs.QEPCTL.bit.PCRM = 1; // position count reset on maximum position
EQep1Regs.QPOSMAX = _ENCODER_MAX_COUNT; // Max position count
ENCODER_REGS.QDECCTL.bit.QSRC = 0; // QEP quadrature count mode
ENCODER_REGS.QDECCTL.bit.IGATE = 1; // gate the index pin
ENCODER_REGS.QDECCTL.bit.QAP = 1; // invert A input
ENCODER_REGS.QDECCTL.bit.QBP = 1; // invert B input
ENCODER_REGS.QDECCTL.bit.QIP = 1; // invert index input
ENCODER_REGS.QEPCTL.bit.FREE_SOFT = 2; // unaffected by emulation suspend
ENCODER_REGS.QEPCTL.bit.PCRM = 1; // position count reset on maximum position
ENCODER_REGS.QPOSMAX = _ENCODER_MAX_COUNT; // Max position count

EQep1Regs.QUPRD = CPU_CLOCK_HZ / RPM_CALC_RATE_HZ; // Unit Timer latch at RPM_CALC_RATE_HZ Hz
EQep1Regs.QEPCTL.bit.UTE=1; // Unit Timeout Enable
EQep1Regs.QEPCTL.bit.QCLM=1; // Latch on unit time out
ENCODER_REGS.QUPRD = CPU_CLOCK_HZ / RPM_CALC_RATE_HZ; // Unit Timer latch at RPM_CALC_RATE_HZ Hz
ENCODER_REGS.QEPCTL.bit.UTE=1; // Unit Timeout Enable
ENCODER_REGS.QEPCTL.bit.QCLM=1; // Latch on unit time out

EQep1Regs.QEPCTL.bit.QPEN=1; // QEP enable
ENCODER_REGS.QEPCTL.bit.QPEN=1; // QEP enable

}

Uint16 Encoder :: getRPM(void)
{
if(EQep1Regs.QFLG.bit.UTO==1) // If unit timeout (one 10Hz period)
if(ENCODER_REGS.QFLG.bit.UTO==1) // If unit timeout (one 10Hz period)
{
Uint32 current = EQep1Regs.QPOSLAT;
Uint32 current = ENCODER_REGS.QPOSLAT;
Uint32 count = (current > previous) ? current - previous : previous - current;

// deal with over/underflow
Expand All @@ -87,7 +105,7 @@ Uint16 Encoder :: getRPM(void)
rpm = count * 60 * RPM_CALC_RATE_HZ / ENCODER_RESOLUTION;

previous = current;
EQep1Regs.QCLR.bit.UTO=1; // Clear interrupt flag
ENCODER_REGS.QCLR.bit.UTO=1; // Clear interrupt flag
}

return rpm;
Expand Down
10 changes: 9 additions & 1 deletion els-f280049c/Encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
#define __ENCODER_H

#include "F28x_Project.h"
#include "Configuration.h"

#ifdef ENCODER_USE_EQEP1
#define ENCODER_REGS EQep1Regs
#endif
#ifdef ENCODER_USE_EQEP2
#define ENCODER_REGS EQep2Regs
#endif

#define _ENCODER_MAX_COUNT 0x00ffffff

Expand All @@ -50,7 +58,7 @@ class Encoder

inline Uint32 Encoder :: getPosition(void)
{
return EQep1Regs.QPOSCNT;
return ENCODER_REGS.QPOSCNT;
}

inline Uint32 Encoder :: getMaxCount(void)
Expand Down
4 changes: 4 additions & 0 deletions els-f280049c/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
#endif
#endif

#if defined(ENCODER_USE_EQEP1) && defined (ENCODER_USE_EQEP2)
#error Define only one of ENCODER_USE_EQEP1 or ENCODER_USE_EQEP2
#endif



#endif // __SANITYCHECK_H
2 changes: 1 addition & 1 deletion els-f280049c/UserInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

const MESSAGE STARTUP_MESSAGE_2 =
{
.message = { LETTER_E, LETTER_L, LETTER_S, DASH, ONE | POINT, ONE | POINT, ZERO, ONE },
.message = { LETTER_E, LETTER_L, LETTER_S, DASH, ONE | POINT, ONE | POINT, ZERO, TWO },
.displayTime = UI_REFRESH_RATE_HZ * 1.5
};

Expand Down

0 comments on commit 0837df2

Please sign in to comment.