// TI File $Revision: /main/3 $
// Checkin $Date: July 2, 2007   11:32:13 $
//###########################################################################
//
// FILE:    Example_281xEvTimerPeriod.c
//
// TITLE:   DSP281x Event Manager GP Timer example program.
//
// ASSUMPTIONS:
//
//          This program requires the DSP281x V1.00 header files.  
//          As supplied, this project is configured for "boot to H0" operation.
//
//          Other then boot mode pin configuration, no other hardware configuration
//          is required.  
//
// DESCRIPTION:
//
//          This program sets up EVA Timer 1, EVA Timer 2, EVB Timer 3
//          and EVB Timer 4 to fire an interrupt on a period overflow.  
//          A count is kept each time each interrupt passes through
//          the interrupt service routine. 
//
//          EVA Timer 1 has the shortest period while EVB Timer4 has the
//          longest period.
//
//          Watch Variables:
//
//                EvaTimer1InterruptCount;
//                EvaTimer2InterruptCount;
//                EvbTimer3InterruptCount;
//                EvbTimer4InterruptCount;
//
//###########################################################################
// $TI Release: DSP281x C/C++ Header Files V1.20 $
// $Release Date: July 27, 2009 $
//###########################################################################

#include "DSP281x_Device.h"     // DSP281x Headerfile Include File
#include "DSP281x_Examples.h"   // DSP281x Examples Include File

#include <281xEvTimersInit.h>
#include <f281xbmsk.h>

#include "TuneUpPlane.h"

// Prototype statements for functions found within this file.
interrupt void eva_timer1_isr(void);
interrupt void eva_timer2_isr(void);
interrupt void evb_timer3_isr(void);
interrupt void evb_timer4_isr(void);


// Global counts used in this example
Uint32  EvaTimer1InterruptCount = 0;
Uint32  EvaTimer2InterruptCount = 0;
Uint32  EvbTimer3InterruptCount = 0;
Uint32  EvbTimer4InterruptCount = 0;

unsigned int  enable_profile_led1_Timer1 = 1;
unsigned int  enable_profile_led1_Timer2 = 1;
unsigned int  enable_profile_led1_Timer3 = 1;
unsigned int  enable_profile_led1_Timer4 = 1;

unsigned int  enable_profile_led2_Timer1 = 0;
unsigned int  enable_profile_led2_Timer2 = 0;
unsigned int  enable_profile_led2_Timer3 = 0;
unsigned int  enable_profile_led2_Timer4 = 0;

//Pointers to handler functions
void (*timer1_handler)() = NULL;
void (*timer2_handler)() = NULL;
void (*timer3_handler)() = NULL;
void (*timer4_handler)() = NULL;

///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

void init_eva_timer1(int freq, void (*timer_handler)())
{
    // Initialize EVA Timer 1:
    // Setup Timer 1 Registers (EV A)
    EvaRegs.GPTCONA.all = 0;
   
    // Set the Period for the GP timer 1 to 0x0200;
    EvaRegs.T1PR = 0x0200;       // Period
    EvaRegs.T1CMPR = 0x0000;     // Compare Reg
   
    // Enable Period interrupt bits for GP timer 1
    // Count up, x128, internal clk, enable compare, use own period
    EvaRegs.EVAIMRA.bit.T1PINT = 0;//1;
    EvaRegs.EVAIFRA.all = BIT7;
//    EvaRegs.EVAIFRA.bit.T1PINT = 1;

    // Clear the counter for GP timer 1
    EvaRegs.T1CNT = 0x0000;
//    EvaRegs.T1PR = (float64)HSPCLK/(float64)(freq / 2);
    EvaRegs.T1PR = (float64)HSPCLK/(float64)(freq);
	EvaRegs.T1CON.all = FREE_RUN_FLAG + TIMER_CONT_UP + TIMER_CLK_PRESCALE_X_1 
						+ TIMER_ENABLE_BY_OWN + TIMER_ENABLE + TIMER_ENABLE_COMPARE;       //
  
    // Start EVA ADC Conversion on timer 1 Period interrupt
    EvaRegs.GPTCONA.bit.T1TOADC = 2;
	
	// Save pointer to handler in variable
	timer1_handler = timer_handler;

	EALLOW;  // This is needed to write to EALLOW protected registers
	PieVectTable.T1PINT = &eva_timer1_isr;
	EDIS;   // This is needed to disable write to EALLOW protected registers

	// Enable PIE group 2 interrupt 4 for T1PINT
//    PieCtrlRegs.PIEIER2.all = M_INT4;
    PieCtrlRegs.PIEIER2.bit.INTx4 = 1;

	// Enable CPU INT2 for T1PINT, INT3 for T2PINT, INT4 for T3PINT
    // and INT5 for T4PINT:
 //   IER |= M_INT2;
}

void stop_eva_timer1()
{
	IER &= ~(M_INT2);
	EvaRegs.EVAIMRA.bit.T1PINT = 0;
}

void start_eva_timer1()
{
	IER |= (M_INT2);
	EvaRegs.EVAIFRA.all = BIT7;
	EvaRegs.EVAIMRA.bit.T1PINT = 1;
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

void init_eva_timer2(int freq, void (*timer_handler)())
{
    // Initialize EVA Timer 2:
    // Setup Timer 2 Registers (EV A)
    EvaRegs.GPTCONA.all = 0;
   
    // Set the Period for the GP timer 2 to 0x0200;
    EvaRegs.T2PR = 0x0400;       // Period
    EvaRegs.T2CMPR = 0x0000;     // Compare Reg
   
    // Enable Period interrupt bits for GP timer 2
    // Count up, x128, internal clk, enable compare, use own period
    EvaRegs.EVAIMRB.bit.T2PINT = 0;//1;
    EvaRegs.EVAIFRB.all = BIT0;
 //   EvaRegs.EVAIFRB.bit.T2PINT = 1;

    // Clear the counter for GP timer 2
    EvaRegs.T2CNT = 0x0000;
//	EvaRegs.T2PR = (float64)HSPCLK/(float64)(freq / 2);
	EvaRegs.T2PR = (float64)HSPCLK/(float64)(freq);
	EvaRegs.T2CON.all = FREE_RUN_FLAG + TIMER_CONT_UP + TIMER_CLK_PRESCALE_X_1 
						+ TIMER_ENABLE_BY_OWN + TIMER_ENABLE + TIMER_ENABLE_COMPARE;       //
  
    // Start EVA ADC Conversion on timer 2 Period interrupt
    EvaRegs.GPTCONA.bit.T2TOADC = 2;

	// Save pointer to handler in variable
	timer2_handler = timer_handler;

	EALLOW;  // This is needed to write to EALLOW protected registers
	PieVectTable.T2PINT = &eva_timer2_isr;
	EDIS;   // This is needed to disable write to EALLOW protected registers

	// Enable PIE group 3 interrupt 1 for T2PINT
//    PieCtrlRegs.PIEIER3.all = M_INT1;
    PieCtrlRegs.PIEIER3.bit.INTx1 = 1;//M_INT1;
    
	// Enable CPU INT2 for T1PINT, INT3 for T2PINT, INT4 for T3PINT
    // and INT5 for T4PINT:
//    IER |= (M_INT3);
}

void stop_eva_timer2()
{
	IER &= ~(M_INT3);
	EvaRegs.EVAIMRB.bit.T2PINT = 0;
}

void start_eva_timer2()
{
	IER |= (M_INT3);
	EvaRegs.EVAIFRB.all = BIT0;
	EvaRegs.EVAIMRB.bit.T2PINT = 1;
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

void init_evb_timer3(int freq, void (*timer_handler)())
{
    // Initialize EVB Timer 3:
    // Setup Timer 3 Registers (EV B)
    EvbRegs.GPTCONB.all = 0;
   
    // Set the Period for the GP timer 3 to 0x0200;
    EvbRegs.T3PR = 0x0800;       // Period
    EvbRegs.T3CMPR = 0x0000;     // Compare Reg
   
    // Enable Period interrupt bits for GP timer 3
    // Count up, x128, internal clk, enable compare, use own period
    EvbRegs.EVBIMRA.bit.T3PINT = 0;//1;
    EvbRegs.EVBIFRA.all = BIT7;
 //   EvbRegs.EVBIFRA.bit.T3PINT = 1;

    // Clear the counter for GP timer 3
    EvbRegs.T3CNT = 0x0000;
//    EvbRegs.T3PR = (float64)HSPCLK/(float64)(freq / 2);
    EvbRegs.T3PR = (float64)HSPCLK/(float64)(freq);
	EvbRegs.T3CON.all = FREE_RUN_FLAG + TIMER_CONT_UP + TIMER_CLK_PRESCALE_X_1
						+ TIMER_ENABLE_BY_OWN + TIMER_ENABLE + TIMER_ENABLE_COMPARE;
//    EvbRegs.T3CON.all = SOFT_STOP_FLAG + TIMER_CONT_UP + TIMER_CLK_PRESCALE_X_1
  //                      + TIMER_ENABLE_BY_OWN + TIMER_ENABLE + TIMER_ENABLE_COMPARE;

	// Save pointer to handler in variable
	timer3_handler = timer_handler;

    // Start EVA ADC Conversion on timer 3 Period interrupt
    EvbRegs.GPTCONB.bit.T3TOADC = 2;

	EALLOW;  // This is needed to write to EALLOW protected registers
	PieVectTable.T3PINT = &evb_timer3_isr;
	EDIS;   // This is needed to disable write to EALLOW protected registers

	// Enable PIE group 4 interrupt 4 for T3PINT
//    PieCtrlRegs.PIEIER4.all = M_INT4;
    PieCtrlRegs.PIEIER4.bit.INTx4 = 1;
    
	// Enable CPU INT2 for T1PINT, INT3 for T2PINT, INT4 for T3PINT
    // and INT5 for T4PINT:
 //   IER |= M_INT4;
}

void stop_evb_timer3()
{
	IER &= ~(M_INT4);
	EvbRegs.EVBIMRA.bit.T3PINT = 0;
}

void start_evb_timer3()
{
	IER |= (M_INT4);

    // Make sure PIEACK for group 2 is clear (default after reset)
//    PieCtrlRegs.PIEACK.all = M_INT4;
	EvbRegs.EVBIFRA.all = BIT7;
	EvbRegs.EVBIMRA.bit.T3PINT = 1;
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

void init_evb_timer4(int freq, void (*timer_handler)())
{
    // Initialize EVB Timer 4:
    // Setup Timer 4 Registers (EV B)
    EvbRegs.GPTCONB.all = 0;
   
    // Set the Period for the GP timer 4 to 0x0200;
    EvbRegs.T4PR = 0x1000;       // Period
    EvbRegs.T4CMPR = 0x0000;     // Compare Reg
   
    // Enable Period interrupt bits for GP timer 4
    // Count up, x128, internal clk, enable compare, use own period
    EvbRegs.EVBIMRB.bit.T4PINT = 0;//1;
    EvbRegs.EVBIFRB.all = BIT0;
//    EvbRegs.EVBIFRB.bit.T4PINT = 1;

    // Clear the counter for GP timer 4
    EvbRegs.T4CNT = 0x0000;
//    EvbRegs.T4PR = (float64)HSPCLK/(float64)(freq / 2);
    EvbRegs.T4PR = (float64)HSPCLK/(float64)(freq);
	EvbRegs.T4CON.all = FREE_RUN_FLAG + TIMER_CONT_UP + TIMER_CLK_PRESCALE_X_1 
						+ TIMER_ENABLE_BY_OWN + TIMER_ENABLE + TIMER_ENABLE_COMPARE;

    // Start EVA ADC Conversion on timer 4 Period interrupt
    EvbRegs.GPTCONB.bit.T4TOADC = 2;

	// Save pointer to handler in variable
	timer4_handler = timer_handler;

	EALLOW;  // This is needed to write to EALLOW protected registers
	PieVectTable.T4PINT = &evb_timer4_isr;
	EDIS;   // This is needed to disable write to EALLOW protected registers

	// Enable PIE group 5 interrupt 1 for T4PINT
//    PieCtrlRegs.PIEIER5.all = M_INT1;
    PieCtrlRegs.PIEIER5.bit.INTx1 = 1;

	// Enable CPU INT2 for T1PINT, INT3 for T2PINT, INT4 for T3PINT
    // and INT5 for T4PINT:
 //   IER |= M_INT5;
} 

void stop_evb_timer4()
{
	IER &= ~(M_INT5);
	EvbRegs.EVBIMRB.bit.T4PINT = 0;
}

void start_evb_timer4()
{
	IER |= (M_INT5);
	EvbRegs.EVBIFRB.all = BIT0;
	EvbRegs.EVBIMRB.bit.T4PINT = 1;
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

#pragma CODE_SECTION(eva_timer1_isr,".fast_run2");
interrupt void eva_timer1_isr(void)
{
    // Set interrupt priority:
    volatile Uint16 TempPIEIER = PieCtrlRegs.PIEIER2.all;
    IER |= M_INT2;
    IER &= MINT2;                       // Set "global" priority
    PieCtrlRegs.PIEIER2.all &= MG24;   // Set "group"  priority
    PieCtrlRegs.PIEACK.all = 0xFFFF;   // Enable PIE interrupts

#if (_ENABLE_INTERRUPT_PROFILE_LED1)
    if (enable_profile_led1_Timer1)
    i_led1_on_off_special(1);
#endif
#if (_ENABLE_INTERRUPT_PROFILE_LED2)
    if (enable_profile_led2_Timer1)
    i_led2_on_off_special(1);
#endif
    EINT;

    // Insert ISR Code here.......
    if(timer1_handler)
    {
        timer1_handler();
    }

    // Next line for debug only (remove after inserting ISR Code):
//      ESTOP0;
    EvaRegs.EVAIFRA.all = BIT7;
    // Restore registers saved:
    DINT;
    PieCtrlRegs.PIEIER2.all = TempPIEIER;
#if (_ENABLE_INTERRUPT_PROFILE_LED1)
    if (enable_profile_led1_Timer1)
    i_led1_on_off_special(0);
#endif
#if (_ENABLE_INTERRUPT_PROFILE_LED2)
    if (enable_profile_led2_Timer1)
    i_led2_on_off_special(0);
#endif

//
//
//    IER &= ~(M_INT2);// stop_eva_timer1();
//   // Enable more interrupts from this timer
//   EvaRegs.EVAIMRA.bit.T1PINT = 1;
//
//   // Note: To be safe, use a mask value to write to the entire
//   // EVAIFRA register.  Writing to one bit will cause a read-modify-write
//   // operation that may have the result of writing 1's to clear
//   // bits other then those intended.
////   EvaRegs.EVAIFRA.bit.T1PINT = 1;
//   EvaRegs.EVAIFRA.all = BIT7;
//
//   // Acknowledge interrupt to receive more interrupts from PIE group 2
//   PieCtrlRegs.PIEACK.all = PIEACK_GROUP2;
//
// //  EINT;
//
//
//	if(timer1_handler)
//	{
//		timer1_handler();
//	}

//	DINT;
//	IER |= (M_INT2);//start_eva_timer1();
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

#pragma CODE_SECTION(eva_timer2_isr,".fast_run2");
interrupt void eva_timer2_isr(void)
{

    // Set interrupt priority:
    volatile Uint16 TempPIEIER = PieCtrlRegs.PIEIER3.all;
    IER |= M_INT3;
    IER &= MINT3;                       // Set "global" priority
    PieCtrlRegs.PIEIER3.all &= MG31;   // Set "group"  priority
    PieCtrlRegs.PIEACK.all = 0xFFFF;   // Enable PIE interrupts

#if (_ENABLE_INTERRUPT_PROFILE_LED1)
    if (enable_profile_led1_Timer2)
    i_led1_on_off_special(1);
#endif
#if (_ENABLE_INTERRUPT_PROFILE_LED2)
    if (enable_profile_led2_Timer2)
    i_led2_on_off_special(1);
#endif

    EINT;

    // Insert ISR Code here.......
    if(timer2_handler)
    {
        timer2_handler();
    }

    // Next line for debug only (remove after inserting ISR Code):
//      ESTOP0;
    EvaRegs.EVAIFRB.all = BIT0;
    // Restore registers saved:
    DINT;
    PieCtrlRegs.PIEIER3.all = TempPIEIER;

#if (_ENABLE_INTERRUPT_PROFILE_LED1)
    if (enable_profile_led1_Timer2)
    i_led1_on_off_special(0);
#endif
#if (_ENABLE_INTERRUPT_PROFILE_LED2)
    if (enable_profile_led2_Timer2)
    i_led2_on_off_special(0);
#endif



//
//
// //   IER &= ~(M_INT3);// stop_eva_timer2();
//
//  // Enable more interrupts from this timer
//  EvaRegs.EVAIMRB.bit.T2PINT = 1;
//
//  // Note: To be safe, use a mask value to write to the entire
//  // EVAIFRB register.  Writing to one bit will cause a read-modify-write
//  // operation that may have the result of writing 1's to clear
//  // bits other then those intended.
//  EvaRegs.EVAIFRB.all = BIT0;
////  EvaRegs.EVAIFRB.bit.T2PINT = 1;
//
//
//  // Acknowledge interrupt to receive more interrupts from PIE group 3
//  PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;
//
//
////  EnableInterrupts();
//
//	if(timer2_handler)
//	{
//		timer2_handler();
//	}


//   DINT;
 //  IER |= (M_INT3);//start_eva_timer2();


}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

#pragma CODE_SECTION(evb_timer3_isr,".fast_run2");
interrupt void evb_timer3_isr(void)
{
    // Set interrupt priority:
    volatile Uint16 TempPIEIER = PieCtrlRegs.PIEIER4.all;
    IER |= M_INT4;
    IER &= MINT4;                       // Set "global" priority
    PieCtrlRegs.PIEIER4.all &= MG44;   // Set "group"  priority
    PieCtrlRegs.PIEACK.all = 0xFFFF;   // Enable PIE interrupts

#if (_ENABLE_INTERRUPT_PROFILE_LED1)
    if (enable_profile_led1_Timer3)
    i_led1_on_off_special(1);
#endif
#if (_ENABLE_INTERRUPT_PROFILE_LED2)
    if (enable_profile_led2_Timer3)
    i_led2_on_off_special(1);
#endif

    EINT;

    // Insert ISR Code here.......
    if(timer3_handler)
    {
        timer3_handler();
    }

    // Next line for debug only (remove after inserting ISR Code):
//      ESTOP0;

//    EvbRegs.EVBIMRA.bit.T3PINT = 1;
    EvbRegs.EVBIFRA.all = BIT7;
//    PieCtrlRegs.PIEACK.all = PIEACK_GROUP4;   // Enable PIE interrupts
    // Restore registers saved:
    DINT;
    PieCtrlRegs.PIEIER4.all = TempPIEIER;

#if (_ENABLE_INTERRUPT_PROFILE_LED1)
    if (enable_profile_led1_Timer3)
    i_led1_on_off_special(0);
#endif
#if (_ENABLE_INTERRUPT_PROFILE_LED2)
    if (enable_profile_led2_Timer3)
    i_led2_on_off_special(0);
#endif


//
//
//    IER &= ~(M_INT4);//stop_evb_timer3();
//
//    // Enable more interrupts from this timer
//    EvbRegs.EVBIMRA.bit.T3PINT = 1;
//
//
// //   IER &= ~(M_INT4);
//  //EvbTimer3InterruptCount++;
//  // Note: To be safe, use a mask value to write to the entire
//  // EVBIFRA register.  Writing to one bit will cause a read-modify-write
//  // operation that may have the result of writing 1's to clear
//  // bits other then those intended.
//  EvbRegs.EVBIFRA.all = BIT7;
//
//  // Acknowledge interrupt to receive more interrupts from PIE group 4
//  PieCtrlRegs.PIEACK.all = PIEACK_GROUP4;
//
//  EINT;
//
//
//	if(timer3_handler)
//	{
//		timer3_handler();
//	}
// //  IFR &= ~(M_INT4);           // ������� ���� ����� ���������� ��� ������!
// //  IER |= (M_INT4);
//
//	DINT;
//	IER |= (M_INT4);//start_evb_timer3();
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

#pragma CODE_SECTION(evb_timer4_isr,".fast_run2");
interrupt void evb_timer4_isr(void)
{
    // Set interrupt priority:
    volatile Uint16 TempPIEIER = PieCtrlRegs.PIEIER5.all;
    IER |= M_INT5;
    IER &= MINT5;                       // Set "global" priority
    PieCtrlRegs.PIEIER5.all &= MG51;   // Set "group"  priority
    PieCtrlRegs.PIEACK.all = 0xFFFF;   // Enable PIE interrupts

#if (_ENABLE_INTERRUPT_PROFILE_LED1)
    if (enable_profile_led1_Timer4)
    i_led1_on_off_special(1);
#endif
#if (_ENABLE_INTERRUPT_PROFILE_LED2)
    if (enable_profile_led2_Timer4)
    i_led2_on_off_special(1);
#endif

    EINT;

    // Insert ISR Code here.......
    if(timer4_handler)
    {
        timer4_handler();
    }

    // Next line for debug only (remove after inserting ISR Code):
//    ESTOP0;
    EvbRegs.EVBIFRB.all = BIT0;
    // Restore registers saved:
    DINT;
    PieCtrlRegs.PIEIER5.all = TempPIEIER;

#if (_ENABLE_INTERRUPT_PROFILE_LED1)
    if (enable_profile_led1_Timer4)
    i_led1_on_off_special(0);
#endif
#if (_ENABLE_INTERRUPT_PROFILE_LED2)
    if (enable_profile_led2_Timer4)
    i_led2_on_off_special(0);
#endif

//
//
//
//    IER &= ~(M_INT5);//stop_evb_timer4();
//
//    EvbRegs.EVBIMRB.bit.T4PINT = 1;
//   //EvbTimer4InterruptCount++;
//   // Note: To be safe, use a mask value to write to the entire
//   // EVBIFRB register.  Writing to one bit will cause a read-modify-write
//   // operation that may have the result of writing 1's to clear
//   // bits other then those intended.
//   EvbRegs.EVBIFRB.all = BIT0;
//
//   // Acknowledge interrupt to receive more interrupts from PIE group 5
//   PieCtrlRegs.PIEACK.all = PIEACK_GROUP5;
//   EINT;
//
//
//    if(timer4_handler)
//    {
//        timer4_handler();
//    }
//
//    DINT;
//    IER |= (M_INT5);//start_evb_timer4();
}
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////


//===========================================================================
// No more.
//===========================================================================