/*	 @test name 	arm_mcbsp_loop_txii_rxii_poll	*/
 /*************************************************************************/
/*
 **
 ** \note The values which are not selected in select mask can be
 **       initialized to any legal value for their data type as they will
 **       not be used for McBSP configuration.
 **
 ** \warning Executing CSL_MCBSP_CMD_REG_RESET will reset entire McBSP. Hence
 **          each test case initialization should be self sufficient and
 **          should not depend on any previously initialized value.
 **
 ** \Register Configurations:
 ** - Test Case 1:
 **   -# No Of Phase - 1,
 **   -# Word Length - 32,
 **   -# Frame Length - 1,
 **   -# Data Delay  - 2,
 **   -# Interrupt on Transmitter and Receiver ready,
 **   -# internal Transmit clock and Frame Sync,
 **   -# Digital Loop Back Mode ON,
 **   -# Internal Frame Sync Generation from Sample Rate Generator
 **   -# Frame Sync Pulse Width  - 1,
 **   -# Frame Sync Period - DXR TO XSR COPY
 **   -# Clock Divide Value - 0xFF,
 *************************************************************************/

/** Header files corresponding to MCBSP module*/
#include <csl_mcbsp.h>
/*   mcbspCommon.h contains common variable declarations and function calls. */
#include <mcbsp_common.h>
#include <csl_deviceconfig.h>
#include <cslr_deviceconfig_001.h>

#define NUM_ELEMENTS	32

/*************************************************************************/
/** \brief   An array of values to be transmitted.
 *************************************************************************
 ** This array consists of values which will be transmitted out of McBSP.
 ** It will be initialized based on the transmit word length. These values
 ** can be compared with values received by testbench or same McBSP(in
 ** case of loop back).
 *************************************************************************/
#pragma DATA_SECTION (xmitBuff, ".DDREMIF_0_BUF");
Uint32 xmitBuff[NUM_ELEMENTS];
/*************************************************************************/
/** \brief   An array of received values.
 *************************************************************************
 ** This array consists of values which are received by McBSP.
 ** These values can be compared with values transmitted by testbench or
 ** same McBSP(in case of loop back).
 *************************************************************************/
#pragma DATA_SECTION (rcvBuff, ".DDREMIF_0_BUF");
Uint32 rcvBuff [NUM_ELEMENTS];

#pragma DATA_SECTION (mcbspDebug, ".DDREMIF_1_BUF");
Uint32  mcbspDebug [10];

//  Used to initialize the array of data to be transmitted
Uint16 tx_word_length;

// This value is used for initialization of array of values to be transmitted
Uint16 val4bit= 0x1;

int Sample_rate_ClkDivide		= 0x2; //Due to QTclk/1
int Sample_rate_framePeriod     = 0x40;
int Sample_rate_pulseWidth     = 0x1;

mcbsp_doTest()
{
	Uint32 i=0, j=0, k;

	CSL_McbspRegsOvly mcbspRegs;
	// CSL system Initialization
	CSL_sysInit();

	//Initialize the MCBSP CSL
	status = CSL_mcbspInit();
	if(status != CSL_SOK)
	  failFlag++;

//setup pin muxing in order to view signals on pins
VAL_pinMux(pinMuxReg1, CSL_PINMUX1_McBSP_ENABLE);

	 // Open Call for Mcbsp0
    hMcbsp[i] = CSL_mcbspOpen(&myMcbspObj[i],CSL_MCBSP_PERI[i],CSL_EXCLUSIVE,NULL,&status);

	mcbspRegs = hMcbsp[0]->regs;
    if(status != CSL_SOK)
        failFlag++;

 	// Reset the MCBSP Channel
   	CSL_mcbspHwControl (hMcbsp[i], CSL_MCBSP_CMD_REG_RESET, 0);

	/*************************************************************************/
	/* \brief  Configure the MCBSP with following values.
	*************************************************************************
	** \note McBSP receiver, transmitter frame sync logic and sample rate
 	** generator are not enabled in this function. This is done because
 	** once these are enabled, McBSP will start its operation, which might
 	** not be required till DMA and IRQ setups, if any are done.
 	*************************************************************************/

    myglobal.ioEnableMode		= CSL_MCBSP_IOMODE_TXDIS_RXDIS;
    myglobal.dlbMode			= CSL_MCBSP_DLBMODE_ON;
    myglobal.clkStopMode		= CSL_MCBSP_CLKSTP_DISABLE;

    myrxdata.phaseNum 			= CSL_MCBSP_PHASE_SINGLE;
    myrxdata.wordLength1 	    = CSL_MCBSP_WORDLEN_32;
    myrxdata.frmLength1         = 1;
    myrxdata.wordLength2		= CSL_MCBSP_WORDLEN_32;
    myrxdata.frmLength2  	    = 0;
	myrxdata.frmSyncIgn	 		= CSL_MCBSP_FRMSYNC_DETECT;
	myrxdata.compand 			= CSL_MCBSP_COMPAND_OFF_MSB_FIRST;
	myrxdata.dataDelay 			= CSL_MCBSP_DATADELAY_2_BITS;
	myrxdata.rjust_dxenable 	= CSL_MCBSP_RJUSTDXENA_RJUST_RZF;
	myrxdata.IntEvent 			= CSL_MCBSP_INTMODE_ON_READY;
	mytxdata.wordReverse 		= CSL_MCBSP_32BIT_REVERS_DISABLE;

    mytxdata.phaseNum 			= CSL_MCBSP_PHASE_SINGLE;
    mytxdata.wordLength1 	    = CSL_MCBSP_WORDLEN_32;
    mytxdata.frmLength1         = 1;
    mytxdata.wordLength2		= CSL_MCBSP_WORDLEN_32;
    mytxdata.frmLength2  	    = 0;
	mytxdata.frmSyncIgn	 		= CSL_MCBSP_FRMSYNC_DETECT;
	mytxdata.compand 			= CSL_MCBSP_COMPAND_OFF_MSB_FIRST;
	mytxdata.dataDelay 			= CSL_MCBSP_DATADELAY_2_BITS;
	mytxdata.rjust_dxenable 	= CSL_MCBSP_RJUSTDXENA_RJUST_RZF;
	mytxdata.IntEvent 			= CSL_MCBSP_INTMODE_ON_READY;
	mytxdata.wordReverse 		= CSL_MCBSP_32BIT_REVERS_DISABLE;

	myclkset.frmSyncTxMode 		= CSL_MCBSP_FSCLKMODE_INTERNAL;
	myclkset.frmSyncRxMode		= CSL_MCBSP_FSCLKMODE_EXTERNAL;
    myclkset.clkRxMode			= CSL_MCBSP_TXRXCLKMODE_EXTERNAL;
    myclkset.clkTxMode			= CSL_MCBSP_TXRXCLKMODE_INTERNAL;
    myclkset.frmSyncTxPolarity	= CSL_MCBSP_FSPOL_ACTIVE_HIGH;
    myclkset.frmSyncRxPolarity	= CSL_MCBSP_FSPOL_ACTIVE_HIGH;
    myclkset.clkTxPolarity		= CSL_MCBSP_CLKPOL_TX_RISING_EDGE;
    myclkset.clkRxPolarity		= CSL_MCBSP_CLKPOL_RX_FALLING_EDGE;
    myclkset.srgFrmPulseWidth	= Sample_rate_pulseWidth;
    myclkset.srgFrmPeriod		= Sample_rate_framePeriod;
    myclkset.srgClkDivide		= Sample_rate_ClkDivide;
    myclkset.srgInputClkMode	= CSL_MCBSP_SRGCLK_CLKCPU;
    myclkset.srgClkPolarity		= CSL_MCBSP_CLKPOL_SRG_RISING_EDGE;
    myclkset.srgTxFrmSyncMode	= CSL_MCBSP_TXFSMODE_DXRCOPY;
    myclkset.srgClkSync			= CSL_MCBSP_CLKGSYNCMODE_OFF;

    mymulCh.rxPartition			=CSL_MCBSP_PARTMODE_2PARTITION;  //RMCME
    mymulCh.txPartition			=CSL_MCBSP_PARTMODE_2PARTITION;	 //XMCME
    mymulCh.rxMulChSel			= 0x0;		//RMCM
    mymulCh.txMulChSel			= 0x0;		//XMCM
    mymulCh.rxPartABlk			=CSL_MCBSP_PABLK_0;
    mymulCh.rxPartBBlk			=CSL_MCBSP_PBBLK_1;
    mymulCh.txPartABlk			=CSL_MCBSP_PABLK_0;
    mymulCh.txPartBBlk			=CSL_MCBSP_PBBLK_1;
    mymulCh.rx512channel		=0x0;		//REMODE
    mymulCh.tx512channel		=0x0;		//XEMODE

    mySetup.emumode					=CSL_MCBSP_EMU_STOP;
    mySetup.dxState					=CSL_MCBSP_DXSTATE_DEFAULT;
    mySetup.rxsynccount				=CSL_MCBSP_RSYNCCNT_DEFAULT;
    mySetup.txsynccount				=CSL_MCBSP_XSYNCCNT_DEFAULT;

    mySetup.global = &myglobal;
    mySetup.rxdataset = &myrxdata;
    mySetup.txdataset = &mytxdata;
    mySetup.clkset = &myclkset;
    mySetup.mulCh = &mymulCh;

   //Configure the Mcbsp registers
    CSL_mcbspHwSetup(hMcbsp[0], &mySetup);

 	/* McBSP data transmit wordlength. This is used to initialize the array of data
	   to be transmitted */
 	tx_word_length = (Uint16)mytxdata.wordLength1;

 	/* In case of companding, even though transmit word length is 8 bits,
	   The data should be written as 16 bits */
    if((mytxdata.compand == 0x2) ||
       (mytxdata.compand == 0x3))
       tx_word_length = 2;

 	/* Initialization of transmit values and array of received values */
	for(k=0;k<NUM_ELEMENTS;k++)
	{
	   /* Initialize the array of received values to all 0s */
	   rcvBuff[k] = 0;

       /* Initialize the array of values to be transmitted based on the word length */
	   xmitBuff[k] = val4bit;

	   for(j=1;j<=(tx_word_length + 2);j++)
	   {
	      xmitBuff[k] |= (Uint32)val4bit << (j*4);
	   }

	    if(mytxdata.compand == 0x2)
       xmitBuff[k] &=0xfffc;

       if(mytxdata.compand == 0x3)
       xmitBuff[k] &=0xfff8;

	   val4bit++;
	   if(val4bit > 0xf)
	      val4bit = 0;
	}

   /* Start Sample Rate Generator Clock Generation */
    grstControl (enable);
	  /* Start Sample Rate Generator Frame Generation */
    frstControl (enable);

    /* Enable Receiver */
    rrstControl  (enable);

	/* Enable Transmitter */
    xrstControl (enable);
    j = 0;  //loop counter

   	    for(j=0;j<NUM_ELEMENTS ;j++)
        {
        /* Poll for XRDY flag to acknowledge transmitter  is ready */
			while (!CSL_FEXT(mcbspRegs->SPCR, MCBSP_SPCR_XRDY));

	 		CSL_mcbspWrite (hMcbsp[i], CSL_MCBSP_WORDLEN_32, &xmitBuff[j]);
		/* Poll for RRDY flag to acknowledge receiver is ready */
			while (!CSL_FEXT(mcbspRegs->SPCR, MCBSP_SPCR_RRDY));

	 		CSL_mcbspRead (hMcbsp[i], CSL_MCBSP_WORDLEN_32, &rcvBuff[j]);
	    }

   	// Compare the transmitted data and received data to detemine whether test case has passed.

		for(j=0;j<NUM_ELEMENTS;j++)
        {
           if(xmitBuff[j]!=rcvBuff[j])
           {
             mcbspDebug[j]   = (xmitBuff[j] );
             mcbspDebug[j+1] = (rcvBuff[j] );
             mcbspDebug[j+2] = j;
			 failFlag++;
             break;
           }
        }

         	// Reset the MCBSP Channel
   	CSL_mcbspHwControl (hMcbsp[0], CSL_MCBSP_CMD_REG_RESET, 0);

	//disable MCBSP pin muxing
	VAL_pinMux(pinMuxReg1, CSL_PINMUX1_McBSP_DISABLE);
            CSL_mcbspClose (hMcbsp[0]); //close mcbsp

	// Determine if any failFlag have been encountered and returns test case result to Main
	if(!failFlag)
	{
	 return 0;
	}
	else
	{
	 return 1;
	}
}