// TI File $Revision: /main/2 $
// Checkin $Date: March 15, 2007   16:54:36 $
//###########################################################################
//
// FILE:   DSP2833x_ECap.c
//
// TITLE:  DSP2833x eCAP Initialization & Support Functions.
//
//###########################################################################
// $TI Release: DSP2833x/DSP2823x C/C++ Header Files V1.31 $
// $Release Date: August 4, 2009 $
//###########################################################################

#include "DSP2833x_Device.h"     // DSP2833x Headerfile Include File
#include "DSP2833x_Examples.h"   // DSP2833x Examples Include File

void StarteCAPTimers( void );

#include "Constants.h"

#define EC_APWM_MODE 1
#define EC_ACTV_HI   0
#define EC_RUN       1
#define DUTY_95_PERCENT ((95UL * BDPWM_PERIOD) / 100UL)    // counts

//---------------------------------------------------------------------------
// InitECap:
//---------------------------------------------------------------------------
// This function initializes the eCAP(s) to a known state.
//
void InitECap(void)
{
   // Initialize eCAP1/2/3

   //tbd...
   //InitECap4Gpio();
   //InitECap5Gpio();
   //InitECap6Gpio();

	// Initialize eCAP1/2/3/4/5/6

	InitECapGpio();

	// Setup APWM mode on CAP1, set period and compare registers
	ECap1Regs.ECCTL2.bit.CAP_APWM = EC_APWM_MODE;	// Enable APWM mode
	ECap1Regs.ECCTL2.bit.APWMPOL  = EC_ACTV_HI;     // Active high
	ECap1Regs.CTRPHS              = 0x0;            // make phase zero
	ECap1Regs.CAP1                = BDPWM_PERIOD;	// Set Period value
	ECap1Regs.CAP2                = DUTY_95_PERCENT;// Set Compare value default

	// Setup APWM mode on CAP2, set period and compare registers
//	ECap2Regs.ECCTL2.bit.CAP_APWM = EC_APWM_MODE;	// Enable APWM mode
//	ECap2Regs.ECCTL2.bit.APWMPOL  = EC_ACTV_HI;     // Active high
//	ECap2Regs.CTRPHS              = 0x0;            // make phase zero
//	ECap2Regs.CAP1                = OCPWM_PERIOD;	// Set Period value
//	ECap2Regs.CAP2                = DUTY_95_PERCENT;// Set Compare value default

	// Setup APWM mode on CAP3, set period and compare registers
//	ECap3Regs.ECCTL2.bit.CAP_APWM = EC_APWM_MODE;	// Enable APWM mode
//	ECap3Regs.ECCTL2.bit.APWMPOL  = EC_ACTV_HI;     // Active high
//	ECap3Regs.CTRPHS              = 0x0;            // make phase zero
//	ECap3Regs.CAP1                = OCPWM_PERIOD;	// Set Period value
//	ECap3Regs.CAP2                = DUTY_95_PERCENT;// Set Compare value default

	// Setup APWM mode on CAP4, set period and compare registers
//	ECap4Regs.ECCTL2.bit.CAP_APWM = EC_APWM_MODE;	// Enable APWM mode
//	ECap4Regs.ECCTL2.bit.APWMPOL  = EC_ACTV_HI;     // Active high
//	ECap4Regs.CTRPHS              = 0x0;            // make phase zero
//	ECap4Regs.CAP1                = OCPWM_PERIOD;   // Set Period value
//	ECap4Regs.CAP2                = DUTY_95_PERCENT;// Set Compare value default

	// Setup APWM mode on CAP5, set period and compare registers
//	ECap5Regs.ECCTL2.bit.CAP_APWM = EC_APWM_MODE;	// Enable APWM mode
//	ECap5Regs.ECCTL2.bit.APWMPOL  = EC_ACTV_HI;     // Active high
//	ECap5Regs.CTRPHS              = 0x0;            // make phase zero
//	ECap5Regs.CAP1                = OCPWM_PERIOD;	// Set Period value
//	ECap5Regs.CAP2                = DUTY_95_PERCENT;// Set Compare value default

	// Setup APWM mode on CAP6, set period and compare registers
//	ECap6Regs.ECCTL2.bit.CAP_APWM = EC_APWM_MODE;	// Enable APWM mode
//	ECap6Regs.ECCTL2.bit.APWMPOL  = EC_ACTV_HI;     // Active high
//	ECap6Regs.CTRPHS              = 0x0;            // make phase zero
//	ECap6Regs.CAP1                = OCPWM_PERIOD;   // Set Period value
//	ECap6Regs.CAP2                = DUTY_95_PERCENT;// Set Compare value default
}

//---------------------------------------------------------------------------
// Example: InitECapGpio:
//---------------------------------------------------------------------------
// This function initializes GPIO pins to function as ECAP pins
//
// Each GPIO pin can be configured as a GPIO pin or up to 3 different
// peripheral functional pins. By default all pins come up as GPIO
// inputs after reset.
//
// Caution:
// For each eCAP peripheral
// Only one GPIO pin should be enabled for ECAP operation.
// Comment out other unwanted lines.

void InitECapGpio()
{
   InitECap1Gpio();
//   InitECap2Gpio();
//   InitECap3Gpio();
//   InitECap4Gpio();
//   InitECap5Gpio();
//   InitECap6Gpio();
}

void InitECap1Gpio(void)
{
   EALLOW;
/* Enable internal pull-up for the selected pins */
// Pull-ups can be enabled or disabled by the user.
// This will enable the pullups for the specified pins.
// Comment out other unwanted lines.

// GpioCtrlRegs.GPAPUD.bit.GPIO5 = 0;      // Enable pull-up on GPIO5 (CAP1)
//   GpioCtrlRegs.GPAPUD.bit.GPIO24 = 0;     // Enable pull-up on GPIO24 (CAP1)
    GpioCtrlRegs.GPBPUD.bit.GPIO34 = 0;     // Enable pull-up on GPIO34 (CAP1)


// Inputs are synchronized to SYSCLKOUT by default.
// Comment out other unwanted lines.

// GpioCtrlRegs.GPAQSEL1.bit.GPIO5 = 0;    // Synch to SYSCLKOUT GPIO5 (CAP1)
//   GpioCtrlRegs.GPAQSEL2.bit.GPIO24 = 0;   // Synch to SYSCLKOUT GPIO24 (CAP1)
    GpioCtrlRegs.GPBQSEL1.bit.GPIO34 = 0;   // Synch to SYSCLKOUT GPIO34 (CAP1)

/* Configure eCAP-1 pins using GPIO regs*/
// This specifies which of the possible GPIO pins will be eCAP1 functional pins.
// Comment out other unwanted lines.

// GpioCtrlRegs.GPAMUX1.bit.GPIO5 = 3;     // Configure GPIO5 as CAP1
//   GpioCtrlRegs.GPAMUX2.bit.GPIO24 = 1;    // Configure GPIO24 as CAP1
    GpioCtrlRegs.GPBMUX1.bit.GPIO34 = 1;    // Configure GPIO34 as CAP1

    EDIS;
}

void InitECap2Gpio(void)
{
   EALLOW;
/* Enable internal pull-up for the selected pins */
// Pull-ups can be enabled or disabled by the user.
// This will enable the pullups for the specified pins.
// Comment out other unwanted lines.

//   GpioCtrlRegs.GPAPUD.bit.GPIO7 = 0;     // Enable pull-up on GPIO7 (CAP2)
// GpioCtrlRegs.GPAPUD.bit.GPIO25 = 0;    // Enable pull-up on GPIO25 (CAP2)
    GpioCtrlRegs.GPBPUD.bit.GPIO37 = 0;    // Enable pull-up on GPIO37 (CAP2)

// Inputs are synchronized to SYSCLKOUT by default.
// Comment out other unwanted lines.

//   GpioCtrlRegs.GPAQSEL1.bit.GPIO7 = 0;    // Synch to SYSCLKOUT GPIO7 (CAP2)
// GpioCtrlRegs.GPAQSEL2.bit.GPIO25 = 0;   // Synch to SYSCLKOUT GPIO25 (CAP2)
    GpioCtrlRegs.GPBQSEL1.bit.GPIO37 = 0;   // Synch to SYSCLKOUT GPIO37 (CAP2)

/* Configure eCAP-2 pins using GPIO regs*/
// This specifies which of the possible GPIO pins will be eCAP2 functional pins.
// Comment out other unwanted lines.

//   GpioCtrlRegs.GPAMUX1.bit.GPIO7 = 3;    // Configure GPIO7 as CAP2
// GpioCtrlRegs.GPAMUX2.bit.GPIO25 = 1;   // Configure GPIO25 as CAP2
    GpioCtrlRegs.GPBMUX1.bit.GPIO37 = 1;   // Configure GPIO37 as CAP2

    EDIS;
}

void InitECap3Gpio(void)
{
   EALLOW;

/* Enable internal pull-up for the selected pins */
// Pull-ups can be enabled or disabled by the user.
// This will enable the pullups for the specified pins.
// Comment out other unwanted lines.

// GpioCtrlRegs.GPAPUD.bit.GPIO9 = 0;      // Enable pull-up on GPIO9 (CAP3)
  GpioCtrlRegs.GPAPUD.bit.GPIO26 = 0;     // Enable pull-up on GPIO26 (CAP3)

// Inputs are synchronized to SYSCLKOUT by default.
// Comment out other unwanted lines.

// GpioCtrlRegs.GPAQSEL1.bit.GPIO9 = 0;    // Synch to SYSCLKOUT GPIO9 (CAP3)
  GpioCtrlRegs.GPAQSEL2.bit.GPIO26 = 0;   // Synch to SYSCLKOUT GPIO26 (CAP3)

/* Configure eCAP-3 pins using GPIO regs*/
// This specifies which of the possible GPIO pins will be eCAP3 functional pins.
// Comment out other unwanted lines.

// GpioCtrlRegs.GPAMUX1.bit.GPIO9 = 3;     // Configure GPIO9 as CAP3
  GpioCtrlRegs.GPAMUX2.bit.GPIO26 = 1;    // Configure GPIO26 as CAP3

    EDIS;
}


void InitECap4Gpio(void)
{
   EALLOW;

/* Enable internal pull-up for the selected pins */
// Pull-ups can be enabled or disabled by the user.
// This will enable the pullups for the specified pins.
// Comment out other unwanted lines.

   // GpioCtrlRegs.GPAPUD.bit.GPIO11 = 0;   // Enable pull-up on GPIO11 (CAP4)
      GpioCtrlRegs.GPAPUD.bit.GPIO27 = 0;   // Enable pull-up on GPIO27 (CAP4)

   // Inputs are synchronized to SYSCLKOUT by default.
   // Comment out other unwanted lines.

   // GpioCtrlRegs.GPAQSEL1.bit.GPIO11 = 0; // Synch to SYSCLKOUT GPIO11 (CAP4)
      GpioCtrlRegs.GPAQSEL2.bit.GPIO27 = 0; // Synch to SYSCLKOUT GPIO27 (CAP4)

   /* Configure eCAP-4 pins using GPIO regs*/
   // This specifies which of the possible GPIO pins will be eCAP4 functional pins.
   // Comment out other unwanted lines.

   // GpioCtrlRegs.GPAMUX1.bit.GPIO11 = 3;  // Configure GPIO11 as CAP4
      GpioCtrlRegs.GPAMUX2.bit.GPIO27 = 1;  // Configure GPIO27 as CAP4

    EDIS;
}


void InitECap5Gpio(void)
{
   EALLOW;

/* Enable internal pull-up for the selected pins */
// Pull-ups can be enabled or disabled by the user.
// This will enable the pullups for the specified pins.
// Comment out other unwanted lines.

// GpioCtrlRegs.GPAPUD.bit.GPIO3 = 0;     // Enable pull-up on GPIO3 (CAP5)
  GpioCtrlRegs.GPBPUD.bit.GPIO48 = 0;    // Enable pull-up on GPIO48 (CAP5)

// Inputs are synchronized to SYSCLKOUT by default.
// Comment out other unwanted lines.

// GpioCtrlRegs.GPAQSEL1.bit.GPIO3 = 0;  // Synch to SYSCLKOUT GPIO3 (CAP5)
  GpioCtrlRegs.GPBQSEL2.bit.GPIO48 = 0; // Synch to SYSCLKOUT GPIO48 (CAP5)

/* Configure eCAP-5 pins using GPIO regs*/
// This specifies which of the possible GPIO pins will be eCAP5 functional pins.
// Comment out other unwanted lines.

// GpioCtrlRegs.GPAMUX1.bit.GPIO3 = 2;   // Configure GPIO3 as CAP5
  GpioCtrlRegs.GPBMUX2.bit.GPIO48 = 1;  // Configure GPIO48 as CAP5

    EDIS;
}


void InitECap6Gpio(void)
{
   EALLOW;

/* Enable internal pull-up for the selected pins */
// Pull-ups can be enabled or disabled by the user.
// This will enable the pullups for the specified pins.
// Comment out other unwanted lines.

// GpioCtrlRegs.GPAPUD.bit.GPIO1 = 0;     // Enable pull-up on GPIO1 (CAP6)
  GpioCtrlRegs.GPBPUD.bit.GPIO49 = 0;    // Enable pull-up on GPIO49 (CAP6)

// Inputs are synchronized to SYSCLKOUT by default.
// Comment out other unwanted lines.

// GpioCtrlRegs.GPAQSEL1.bit.GPIO1 = 0;  // Synch to SYSCLKOUT GPIO1 (CAP6)
  GpioCtrlRegs.GPBQSEL2.bit.GPIO49 = 0; // Synch to SYSCLKOUT GPIO49 (CAP6)

/* Configure eCAP-5 pins using GPIO regs*/
// This specifies which of the possible GPIO pins will be eCAP6 functional pins.
// Comment out other unwanted lines.

// GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 2;   // Configure GPIO1 as CAP6
  GpioCtrlRegs.GPBMUX2.bit.GPIO49 = 1;  // Configure GPIO49 as CAP6

    EDIS;
}

void StarteCAPTimers( void )
{
    ECap1Regs.ECCTL2.bit.TSCTRSTOP = EC_RUN; // Allow TSCTR to run
//    ECap2Regs.ECCTL2.bit.TSCTRSTOP = EC_RUN; // Allow TSCTR to run
//    ECap3Regs.ECCTL2.bit.TSCTRSTOP = EC_RUN; // Allow TSCTR to run
//    ECap4Regs.ECCTL2.bit.TSCTRSTOP = EC_RUN; // Allow TSCTR to run
//    ECap5Regs.ECCTL2.bit.TSCTRSTOP = EC_RUN; // Allow TSCTR to run
//    ECap6Regs.ECCTL2.bit.TSCTRSTOP = EC_RUN; // Allow TSCTR to run
}

//===========================================================================
// End of file.
//===========================================================================
