/***************************************************************************/
/*                                                                         */
/*     dsk_lib.c                                                           */
/*                                                                         */
/*     Basic library functions for the DSK                                 */
/*                                                                         */
/***************************************************************************/
//  DSP 2 <=> TMS320C6713
#include "dsk_lib.h"
#include "dsk6713.h"



static Int16 DC_ledstate;  // variable for DCled states

void DC_LED_init()
{
    Uint8 *pdata;
    /* Turn all LEDs off */
    DC_ledstate = 0x03;

       /* Write lower 8 bits of register */
    pdata = (Uint8 *)(DSK6713_CPLD_BASE + DSK6713_DC_REG);
    *pdata = (DC_ledstate & 0xff);

}

void DC_LED_off(Uint32 LED_Num)
{
     Uint8 *pdata;  
	 /* Clear the LED bit */
     	switch(LED_Num) {
 		case DC_LED_Y:
 		    DC_ledstate = DC_ledstate | DC_LED_Yx;
 			break;
 				
  		case DC_LED_R:
 			DC_ledstate = DC_ledstate | DC_LED_Rx;
 			break;
		}


      /* Write lower 8 bits of register */
    pdata = (Uint8 *)(DSK6713_CPLD_BASE + DSK6713_DC_REG);
     *pdata = (DC_ledstate & 0xff);



}

void DC_LED_on(Uint32 LED_Num)
{
      Uint8 *pdata;   
	  
    /* Set the LED bit */

   	switch(LED_Num) {
 		case DC_LED_Y:
 		    DC_ledstate = DC_ledstate & DC_LED_Y;
 			break;
 				
  		case DC_LED_R:
 			DC_ledstate = DC_ledstate & DC_LED_R;
 			break;
		}


     /* Write lower 8 bits of register */
    pdata = (Uint8 *)(DSK6713_CPLD_BASE + DSK6713_DC_REG);
     *pdata = (DC_ledstate & 0xff);
}

void DC_LED_toggle(Uint32 LED_Num)
{
       Uint8 *pdata;  
	  /* Set the LED bit */

   	switch(LED_Num) {
 		case DC_LED_Y:
 		    DC_ledstate = DC_ledstate ^ DC_LED_Yx;
 			break;
 				
  		case DC_LED_R:
 			DC_ledstate = DC_ledstate ^ DC_LED_Rx;
 			break;
		}  
   

      /* Write lower 8 bits of register */
    pdata = (Uint8 *)(DSK6713_CPLD_BASE + DSK6713_DC_REG);
     *pdata = (DC_ledstate & 0xff);


}





void start_DSK()
{
	int i;
    //L_cfgInit();  //the CSL library must be initialized.
    
    /* Write an 8-bit value to a CPLD register */
    DSK6713_rset(DSK6713_MISC, MCBSP1SEL);  // Select the McBSP 1 for the J3 
      
           
 // Serial port 1 McBSP1 starts:
 
	MCBSP_enableSrgr(hMcbsp1); 	//Enable the sample rate generator for the given port.
	for (i=0; i<932; i++) i++;       /* Wait states after SRG starts */
        
	MCBSP_enableRcv(hMcbsp1); /* Enable McBSP port 1 as the receiver */	
    MCBSP_enableXmt(hMcbsp1);	//Enable the transmitter for the given port.								  // transmit register, DXR.
 	MCBSP_enableFsync(hMcbsp1);	//Enable the frame sync generator for the given port.
 	//MCBSP_write(hMcbsp1,0x12345678);  
  
  
  /*******************************************************************************
	* Function: irq_setup_rx
	* Map physical interrupt 11 to new frame event of McBSP 1, clear pending
	* interrupts, enable new frame event of McBSP 1.
	*******************************************************************************/
            
          
   	IRQ_map(IRQ_EVT_RINT1, 11); /* ch 11 for McBSP new frame event REVT0 */
  	IRQ_reset(IRQ_EVT_RINT1);	
  	IRQ_disable(IRQ_EVT_RINT1); 	
	IRQ_clear(IRQ_EVT_RINT1);	
	IRQ_enable(IRQ_EVT_RINT1);
	
	

  /*******************************************************************************
	* Function: int4_hwi initialization
	* Physical interrupt 4 setup for delta omega measurement, clear pending
	* interrupts, enable the int4.
	*******************************************************************************/
	
    // Enable Interrupt to catch the speed pulses
	IRQ_enable(IRQ_EVT_EXTINT4);
	
	TIMER_start(hTimer1);
	 DC_LED_init();
	
    // LOG_printf(&trace, "GO DSK! \n"); 	
}

//-----------------------------------------------------------
//-----------------------------------------------------------

extern "C" {
Void McBSPnewFrameIsr(Void) ;}

//-----------------------------------------------------------
Void McBSPnewFrameIsr(Void)  /* HWI_INT 11 */
{
	DSK6713_LED_on(1);
   	/* Reinitialize the EDMA for the framesink pulse */
	EDMA_config(hEdmaCha15, &edmaCfg_REC);
    EDMA_enableChannel(hEdmaCha15);
	  
    DSK6713_LED_off(1);
}

//-----------------------------------------------------------
//-----------------------------------------------------------
