// TI File $Revision: /main/2 $
// Checkin $Date: April 29, 2005   11:11:22 $
//###########################################################################
//
// FILE:	DSP281x_CpuTimers.c
//
// TITLE:	DSP281x CPU 32-bit Timers Initialization & Support Functions.
//
// NOTES:   CpuTimer1 and CpuTimer2 are reserved for use with DSP BIOS and
//          other realtime operating systems.  
//
//          Do not use these two timers in your application if you ever plan
//          on integrating DSP-BIOS or another realtime OS. 
//
//          For this reason, the code to manipulate these two timers is
//          commented out and not used in these examples.
//           
//###########################################################################
// $TI Release:$
// $Release Date:$
//###########################################################################

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


struct CPUTIMER_VARS CpuTimer0;
struct CPUTIMER_VARS CpuTimer1;
struct CPUTIMER_VARS CpuTimer2;

// CpuTimer 1 and CpuTimer2 are reserved for DSP BIOS & other RTOS

//---------------------------------------------------------------------------
// InitCpuTimers: 
//---------------------------------------------------------------------------
// This function initializes all three CPU timers to a known state.
//
void CpuTimers_Init(void)
	{
	EvaRegs.GPTCONA.all = 0x0000;  // Clear all bits of GP Timer control register A
	EvaRegs.T1CON.all   = 0x0000;  // Clear all bits of Timer 1 control register
	EvaRegs.T2CON.all   = 0x0000;  // Clear all bits of Timer 2 control register
	EvaRegs.EXTCONA.all = 0x0000;  // Clear all bits EVA Extension control register A
   	EvaRegs.EVAIMRA.all = 0x0000;  // Clear all bits EVA Interrupt mask register A
   	EvaRegs.EVAIMRB.all = 0x0000;  // Clear all bits EVA Interrupt mask register B
   	EvaRegs.EVAIMRC.all = 0x0000;  // Clear all bits EVA Interrupt mask register C
   	EvaRegs.EVAIFRA.all = 0xFFFF;  // Reset all flags EVA Interrupt flag register A
   	EvaRegs.EVAIFRB.all = 0xFFFF;  // Reset all flags EVA Interrupt flag register B
   	EvaRegs.EVAIFRC.all = 0xFFFF;  // Reset all flags EVA Interrupt flag register C

	EvbRegs.GPTCONB.all = 0x0000;  // Clear all bits of GP Timer control register B
	EvbRegs.T3CON.all   = 0x0000;  // Clear all bits of Timer 1 control register
	EvbRegs.T4CON.all   = 0x0000;  // Clear all bits of Timer 2 control register
	EvbRegs.EXTCONB.all = 0x0000;  // Clear all bits EVB Extension control register B
   	EvbRegs.EVBIMRA.all = 0x0000;  // Clear all bits EVB Interrupt mask register A
   	EvbRegs.EVBIMRB.all = 0x0000;  // Clear all bits EVB Interrupt mask register B
   	EvbRegs.EVBIMRC.all = 0x0000;  // Clear all bits EVB Interrupt mask register C
   	EvbRegs.EVBIFRA.all = 0xFFFF;  // Reset all flags EVB Interrupt flag register A
   	EvbRegs.EVBIFRB.all = 0xFFFF;  // Reset all flags EVB Interrupt flag register B
   	EvbRegs.EVBIFRC.all = 0xFFFF;  // Reset all flags EVB Interrupt flag register C

   	// ---- ---- ---- ----

	EvbRegs.T4PR   = 25000;  // 4166//8332-360;//3124;//25000 - 120       // Timer4 period
	EvbRegs.T4CMPR = 0;      // Timer4 compare
	EvbRegs.T4CNT  = 0;      // Timer4 counter

	EvbRegs.T4CON.bit.SET3PR  = 0;	// Use own period register
	EvbRegs.T4CON.bit.TECMPR  = 0;	// Disable timer compare operation
	EvbRegs.T4CON.bit.TCLD10  = 1;	// Enable timer compare operation
	EvbRegs.T4CON.bit.TCLKS10 = 0;	// Clock source Internal (i.e., HSPCLK);
	EvbRegs.T4CON.bit.TENABLE = 0;	// Disable timer operation
	EvbRegs.T4CON.bit.T4SWT3  = 0;	// Use own TENABLE bit
	EvbRegs.T4CON.bit.TPS     = 0;	// Input clock prescaler Input clock prescaler	x/1 (x = HSPCLK)
	EvbRegs.T4CON.bit.TMODE   = 2;	// Continuous-Up Count Mode
	EvbRegs.T4CON.bit.SOFT    = 0;	// Stop after current timer period is complete on emulation suspend
	EvbRegs.T4CON.bit.FREE    = 0;

	EvbRegs.GPTCONB.bit.T4TOADC = 2; // 2 setting period interrupt flag starts ADC ()  // Не забудь разрешить запуск ацп Ивент менеджером "B"    AdcRegs.ADCTRL2.bit.EVB_SOC_SEQ = 1;  // При этом разрешать вектор прерывания для таймера 4 не обязательно

    // CPU Timer 0
	// Initialize address pointers to respective timer registers:
	CpuTimer0.RegsAddr = &CpuTimer0Regs;
	// Initialize timer period to maximum:	
	CpuTimer0Regs.PRD.all  = 0xFFFFFFFF;
	// Initialize pre-scale counter to divide by 1 (SYSCLKOUT):	
	CpuTimer0Regs.TPR.all  = 0;
	CpuTimer0Regs.TPRH.all = 0;
	// Make sure timer is stopped:
	CpuTimer0Regs.TCR.bit.TSS = 1;
	// Reload all counter register with period value:
	CpuTimer0Regs.TCR.bit.TRB = 1;
	// Reset interrupt counters:
	CpuTimer0.InterruptCount = 0;

	// CPU Timer 1
	CpuTimer1.RegsAddr = &CpuTimer1Regs;
	// Initialize timer period to maximum:
	CpuTimer1Regs.PRD.all  = 0xFFFFFFFF;
	// Initialize pre-scale counter to divide by 1 (SYSCLKOUT):
	CpuTimer1Regs.TPR.all  = 0;
	CpuTimer1Regs.TPRH.all = 0;
	// Make sure timer is stopped:
	CpuTimer1Regs.TCR.bit.TSS = 1;
	// Reload all counter register with period value:
	CpuTimer1Regs.TCR.bit.TRB = 1;
	// Reset interrupt counters:
	CpuTimer1.InterruptCount = 0;

	// CPU Timer 1
	CpuTimer2.RegsAddr = &CpuTimer2Regs;
	// Initialize timer period to maximum:
	CpuTimer2Regs.PRD.all  = 0xFFFFFFFF;
	// Initialize pre-scale counter to divide by 1 (SYSCLKOUT):
	CpuTimer2Regs.TPR.all  = 0;
	CpuTimer2Regs.TPRH.all = 0;
	// Make sure timer is stopped:
	CpuTimer2Regs.TCR.bit.TSS = 1;
	// Reload all counter register with period value:
	CpuTimer2Regs.TCR.bit.TRB = 1;
	// Reset interrupt counters:
	CpuTimer2.InterruptCount = 0;
}	
	
//---------------------------------------------------------------------------
// ConfigCpuTimer: 
//---------------------------------------------------------------------------
// This function initializes the selected timer to the period specified
// by the "Freq" and "Period" parameters. The "Freq" is entered as "MHz"
// and the period in "uSeconds". The timer is held in the stopped state
// after configuration.
//

void ConfigCpuTimer(struct CPUTIMER_VARS *Timer, float Freq, float Period)
{
	Uint32 	temp;
	
	// Initialize timer period:	
	Timer->CPUFreqInMHz = Freq;
	Timer->PeriodInUSec = Period;
	temp = ((long) (Freq * Period));
	Timer->RegsAddr->PRD.all = temp;

	// Set pre-scale counter to divide by 1 (SYSCLKOUT):	
	Timer->RegsAddr->TPR.all  = 0;
	Timer->RegsAddr->TPRH.all  = 0;
	
	// Initialize timer control register:
	Timer->RegsAddr->TCR.bit.TSS = 1;      // 1 = Stop timer, 0 = Start/Restart Timer 
	Timer->RegsAddr->TCR.bit.TRB = 1;      // 1 = reload timer
	Timer->RegsAddr->TCR.bit.SOFT = 1;
	Timer->RegsAddr->TCR.bit.FREE = 1;     // Timer Free Run
	Timer->RegsAddr->TCR.bit.TIE = 1;      // 0 = Disable/ 1 = Enable Timer Interrupt
	
	// Reset interrupt counter:
	Timer->InterruptCount = 0;
}

interrupt void cpu_timer1_isr(void)
{
    //---UART---
    //--------------------
}

interrupt void cpu_timer2_isr(void)
{
}
//===========================================================================
// No more.
//===========================================================================

