/*  ============================================================================
 *   Copyright (c) Texas Instruments Inc 2002, 2003, 2004, 2005, 2006
 *
 *   Use of this software is controlled by the terms and conditions found in the
 *   license agreement under which this software has been supplied.
 * =============================================================================
 *  Revision History
 *  ===============
 *  16-Mar-06 xxx  file created
 *  28-Dec-06 bf  modified for EMAC performance test
 * =============================================================================
 */

#include <stdio.h>
#include <soc.h>
 
#include <csl_tmr.h>
#include <csl_intc.h>
 
#define 	CPU_SPEED_KHZ 	(1000000)

extern void interrupt EmacTimerTick(void);
extern void interrupt EmacHwInt(void);

 
/****** Static variable declarations ***********/
 
/* Intc objects */
static CSL_IntcContext context;
static CSL_IntcEventHandlerRecord Record[13];
 
/* timer interrupt object */
static CSL_IntcObj intcTimer;
static CSL_IntcObj intcEMAC;

static CSL_IntcHandle hIntcTimer; 
static CSL_IntcHandle hIntcEMAC; 

 
/* Timer object */
static CSL_TmrObj     tmrObj;
/* handle for Timer */
static CSL_TmrHandle  hTimer;


static volatile Bool bRun = TRUE;   // boolean that indicating if testcase is complete
 
/******* local function definitions **********/
/*configure the timer0 for 100ms ticks*/ 
void EmacTimerInitialization(void)
{  
   CSL_Status     status;  
   CSL_TmrHwSetup hwSetup;
   CSL_TmrEnamode tmr_ena_mode = CSL_TMR_ENAMODE_CONT;
 
   hTimer   = CSL_tmrOpen(&tmrObj, CSL_TMR_0, NULL, &status);
   /* Start Timer 0 - And Enable */  
   status  = CSL_tmrHwControl(hTimer, CSL_TMR_CMD_RESET_TIMLO, &tmr_ena_mode );
   status  = CSL_tmrHwControl(hTimer, CSL_TMR_CMD_RESET_TIMHI, &tmr_ena_mode );
   /* Get existing hwSetup values from Timer Registers */
   status   = CSL_tmrGetHwSetup(hTimer, &hwSetup);
   /* Timer 0 HW Setup Modifications */
   hwSetup.tmrTimerPeriodHi = 0;
   hwSetup.tmrTimerPeriodLo = CPU_SPEED_KHZ*100/6; 
   hwSetup.tmrTimerCounterHi = 0x0;
   hwSetup.tmrTimerCounterLo = 0x0;
   hwSetup.tmrIpGateLo = CSL_TMR_CLOCK_INP_NOGATE;
   hwSetup.tmrClksrcLo= CSL_TMR_CLKSRC_INTERNAL;
   hwSetup.tmrClockPulseLo = CSL_TMR_CP_CLOCK;      
   hwSetup.tmrInvOutpHi    = CSL_TMR_INVOUTP_UNINVERTED;      
   hwSetup.tmrInvOutpLo    = CSL_TMR_INVOUTP_UNINVERTED;      
   hwSetup.tmrTimerMode    = CSL_TMR_TIMMODE_DUAL_UNCHAINED;
 
   /* Program Timer 0 HW based on hwSetup Structure Modifications*/
   status   = CSL_tmrHwSetup(hTimer, &hwSetup);
   /* Start Timer 0 - And Enable */  
   status  = CSL_tmrHwControl(hTimer, CSL_TMR_CMD_START64, &tmr_ena_mode );
  
}
 
/*********************************************************
 * Intc setup
 *
 ********************************************************/
void EmacInterruptsInitialization(void)
{
   CSL_IntcParam vectId1;
   CSL_IntcParam vectId2;

   CSL_IntcGlobalEnableState state;
   
   /* Setup the global Interrupt */
   context.numEvtEntries = 13;
   context.eventhandlerRecord = Record; 

   /* VectorID for the Global Emac and Timer Event  */
   vectId1 = CSL_INTC_VECTID_4;
   vectId2 = CSL_INTC_VECTID_5;
	
   CSL_intcInit(&context);
   /* Enable NMIs  */
   CSL_intcGlobalNmiEnable();
   /* Enable Global Interrupts  */
   CSL_intcGlobalEnable(&state);
   
   /* Opening a handle for the timer */
   hIntcTimer = CSL_intcOpen(&intcTimer, CSL_INTC_EVENTID_TINTLO0, &vectId1, NULL);
 
   /* Opening a handle for EMAC Rx interrupt */
   hIntcEMAC = CSL_intcOpen(&intcEMAC, CSL_INTC_EVENTID_MACINT, &vectId2, NULL);

   /* Hook the ISRs */
   CSL_intcHookIsr(vectId1,&EmacTimerTick);
   CSL_intcHookIsr(vectId2,&EmacHwInt);
  
   /* Clear the Interrupt */
   CSL_intcHwControl(hIntcTimer, CSL_INTC_CMD_EVTCLEAR, NULL);
   CSL_intcHwControl(hIntcEMAC, CSL_INTC_CMD_EVTCLEAR, NULL);
     
    /* Enable the Event & the interrupt */
   CSL_intcHwControl(hIntcTimer, CSL_INTC_CMD_EVTENABLE, NULL);   
   CSL_intcHwControl(hIntcEMAC, CSL_INTC_CMD_EVTENABLE, NULL);   
}

/*********************************************************
 * Intc close 
 *
 ********************************************************/

void EmacInterruptEnd(void)
{
   CSL_intcGlobalDisable(NULL);
   CSL_intcClose(hIntcEMAC);
   CSL_intcClose(hIntcTimer);
}

