ADSP-21489 is not responding to Nested Interrupts related to SPORT-0 to 8

Hi,

I am working with ADSP-21489 Eval Kit and CCS. Initialized the SPORT0 to SPORT 8 such that:

1. Channels A and B of SPORT0, SPORT2 and SPORT4 act as UART-Transmitters (EE-191 is followed for SPORT-UART logic).

2. Channel A of SPORT1, SPORT3, SPORT5, SPORT6 and SPORT7 wok as UART Receives (EE-191 is followed for SPORT-UART logic).

Code is written to enable the Interrupts of all SPORT channels then below are issues: 

Target of the program is that continuously transmit data through Transmt Channels of SPORT-0 and 1. 

1. While Channel-A of SPORT1 is working absolutely fine by generating Interrupt upon receiving characters on serial port. Interrupts related to Channel A and B of SPORT0  and 2 are expected such that ISRs of SPORT0 loads one byte of data into transmit buffer then ISR of SPORT2 shall load one byte of data into Transmit buffers of SPORT2. Program control shall go to SPORT0 ISR then to SPORT2 ISR but the program control is always executing the ISR of SPORT-0 only.... control do not go to either Main Function or SPORT2 ISR. 

Seems issue is with nested interrupts ... please help to solve this issue.

Code written is below: 

/*****************************************************************************
 * 8SPORTs_Emulate_UARTs.c
 *****************************************************************************/

#include <sys/platform.h>
#include "adi_initialize.h"
#include "def21489.h"
#include "sru.h"
#include<stdio.h>
#include <services/int/adi_int.h>
#include<string.h>

#define SPORT_UART_BAUDRATE  9600 	// Defining the required UART Baud Rate

#define PCLK 200000000	// ADSP-21489 PCLK=200MHz, i.e input clock to Precision Clock Generator-PCG when CPU runs at 400MHz
#define	TxCLKDIV  PCLK/SPORT_UART_BAUDRATE		// Divisor factor of PCG-Channel-A to generate clocks to SPORTs Tx-Channels.
#define RxCLKDIV  PCLK/(SPORT_UART_BAUDRATE*3)	// Divisor factor of PCG-Channel-A to generate clocks to SPORTs Rx-Channels.

void Init_SPORT(void); 	//Initializes all SPORT channels as UART channels.
void InitPCG(void);		//Initializes Precision Clock Generator Module to generate baud rate clocks to SPORT Channels
void ConfigSRU(void);	//Initializes DAI Pins of ADSP-21489 as UART-Tx and Rx.

void SPORT0A_Transmit(unsigned char tx1);	//UART-1 Tx Data Send Function - Polling Method.
unsigned char SPORT1A_Receive(void);		//UART-1 Rx Data Return Function - Polling Method.

void SPORT2A_Transmit(unsigned char tx1);	//UART-2 Tx Data Send Function - Polling Method.
unsigned char SPORT3A_Receive(void);		//UART-2 Rx Data Return Function - Polling Method.

void SPORT4A_Transmit(unsigned char tx1);	//UART-3 Tx Data Send Function - Polling Method.
unsigned char SPORT5A_Receive(void);		//UART-3 Rx Data Return Function - Polling Method.

void SPORT0B_Transmit(unsigned char tx1);	//UART-4 Tx Data Send Function - Polling Method.
unsigned char SPORT6A_Receive(void);		//UART-4 Rx Data Return Function - Polling Method.

void SPORT2B_Transmit(unsigned char tx1);	//UART-5 Tx Data Send Function - Polling Method.
unsigned char SPORT7A_Receive(void);		//UART-5 Rx Data Return Function - Polling Method.

void TestSingleUART(void);

static void SPORT0Handler(uint32_t iid, void* handlerArg);
static void SPORT1Handler(void);
static void SPORT2Handler(void);
static void SPORT3Handler(void);
static void SPORT4Handler(void);
static void SPORT5Handler(void);
static void SPORT6Handler(void);
static void SPORT7Handler(void);

unsigned char SPORT_RxProcess(unsigned int temp);
unsigned short int SPORT_TxProcess(unsigned char tx1);

char strA[]="Hello\n";
int SPORT0ATxBuffLength;
char strB[]="Ananth\n";
int SPORT0BTxBuffLength;

int SPORT0A_TxByte=0xAB;
volatile int SPORT0ATxCompleted=0;
int SPORT1A_RxByte;

int SPORT2A_TxByte=0xAB;
volatile int SPORT2ATxCompleted=0;
int SPORT2ATxBuffLength;
int SPORT3A_RxByte;

int SPORT4A_TxByte;
int SPORT5A_RxByte;

int SPORT0B_TxByte;
volatile int SPORT0BTxCompleted=0;
int SPORT6A_RxByte;

int SPORT2B_TxByte=0xCD;
volatile int SPORT2BTxCompleted=0;
int SPORT2BTxBuffLength;
int SPORT7A_RxByte;

volatile int SPORT1A_RxByte_Available=0;
volatile int SPORT3A_RxByte_Available=0;
volatile int SPORT5A_RxByte_Available=0;
volatile int SPORT6A_RxByte_Available=0;
volatile int SPORT7A_RxByte_Available=0;

void main(void)
{
	unsigned char temp, TxData, RxData=0xAB; // to receive data from SPORT1 through UART Config
	volatile int dumm1=10, dumm2=20, dumm3;

	SPORT0ATxBuffLength=strlen(strA);
	SPORT0BTxBuffLength=strlen(strB);
	SPORT2ATxBuffLength=strlen(strA);

	adi_initComponents();
	ConfigSRU();
	Init_SPORT();

	//interrupt();

	adi_int_InstallHandler(ADI_CID_P6I,(ADI_INT_HANDLER_PTR ) SPORT0Handler,0,true);
	adi_int_InstallHandler(ADI_CID_P3I,(ADI_INT_HANDLER_PTR ) SPORT1Handler,0,true);
	adi_int_InstallHandler(ADI_CID_P7I,(ADI_INT_HANDLER_PTR ) SPORT2Handler,0,true);
	adi_int_InstallHandler(ADI_CID_P4I,(ADI_INT_HANDLER_PTR ) SPORT3Handler,0,true);
	adi_int_InstallHandler(ADI_CID_P8I,(ADI_INT_HANDLER_PTR ) SPORT4Handler,0,true);
	adi_int_InstallHandler(ADI_CID_P5I,(ADI_INT_HANDLER_PTR ) SPORT5Handler,0,true);
	adi_int_InstallHandler(ADI_CID_P16I,(ADI_INT_HANDLER_PTR )SPORT6Handler,0,true);
	adi_int_InstallHandler(ADI_CID_P11I,(ADI_INT_HANDLER_PTR )SPORT7Handler,0,true);

	while(1)
	{

			while(1)
		{
			if(SPORT1A_RxByte_Available==1)
			{
				SPORT1A_RxByte_Available=0;
				SPORT0ATxBuffLength=strlen(strA);
				SPORT0BTxBuffLength=strlen(strB);
				SPORT2ATxBuffLength=strlen(strA);

				RxData = SPORT_RxProcess(SPORT1A_RxByte);
				SPORT0ATxBuffLength=strlen(strA);
				SPORT0BTxBuffLength=strlen(strB);
				SPORT0ATxCompleted=0;
				SPORT0BTxCompleted=0;
				adi_int_EnableInt(ADI_CID_P6I, true);

				SPORT2ATxCompleted=0;
				SPORT2BTxCompleted=0;
				adi_int_EnableInt(ADI_CID_P7I, true);

			}
			dumm3=dumm1+dumm2;

		}
		//TestSingleUART();
	}
}


static void SPORT0Handler(uint32_t iid, void* handlerArg)
{
	volatile unsigned int status, *ptr;
	status=*pSPCTL0;

	if(SPORT0ATxCompleted==0)
	{
		if((status & 0xC0000000) == 0x00000000)	// Check DXS_A 2-Bit-Field status for Channel-A Data Buffer Status
		{
			SPORT0ATxBuffLength--;
			*pTXSP0A = SPORT_TxProcess((unsigned char)strA[SPORT0ATxBuffLength]); // Read/Write data from/to Receive/Transmit Data Buffer : RXSPxB or TXSPxB
			if(SPORT0ATxBuffLength==0)
			SPORT0ATxBuffLength=strlen(strA);
				//SPORT0ATxCompleted=1;
		}
	}

	if(SPORT0BTxCompleted==0)
	{
		if((status & 0x180000000) == 0x00000000)	// Check DXS_B 2-Bit-Field status for Channel-A Data Buffer Status
		{
			SPORT0BTxBuffLength--;
			*pTXSP0B = SPORT_TxProcess((unsigned char)strB[SPORT0BTxBuffLength]); // Read/Write data from/to Receive/Transmit Data Buffer : RXSPxB or TXSPxB
			if(SPORT0BTxBuffLength==0)
			SPORT0BTxBuffLength=strlen(strB);
		}
	}

}

static void SPORT1Handler(void)
{

	volatile unsigned int status;
	status=*pSPCTL1;

	if((status & 0xC0000000) != 0x00000000)	// Check DXS_A 2-Bit-Field status for Channel-A Data Buffer Status
	{
		SPORT1A_RxByte = *pRXSP1A; // Read/Write data from/to Receive/Transmit Data Buffer : RXSPxB or TXSPxB
		SPORT1A_RxByte_Available=1;
	}

	if(status & 0x180000000 != 0x000000000)	// Check DXS_B 2-Bit-Field status for Channel-A Data Buffer Status
	{
		//SPORT3B_RxByte = *pRXSP3B; // Read/Write data from/to Receive/Transmit Data Buffer : RXSPxB or TXSPxB
	}

}

static void SPORT2Handler(void)
{
	volatile unsigned int status;
	status=*pSPCTL2;

	if(SPORT2ATxCompleted==0)
	{
		if((status & 0xC0000000) == 0x00000000)	// Check DXS_A 2-Bit-Field status for Channel-A Data Buffer Status
		{
			SPORT2ATxBuffLength--;
			*pTXSP2A = SPORT_TxProcess((unsigned char)strB[SPORT2ATxBuffLength]); // Read/Write data from/to Receive/Transmit Data Buffer : RXSPxB or TXSPxB
			if(SPORT2ATxBuffLength==0)
			SPORT2ATxBuffLength=strlen(strA);
		}
	}

	if(SPORT2BTxCompleted==0)
	{

		if((status & 0x180000000) == 0x00000000)	// Check DXS_B 2-Bit-Field status for Channel-A Data Buffer Status
		{
			SPORT2BTxBuffLength--;
			*pTXSP2B = SPORT_TxProcess((unsigned char)strB[SPORT2BTxBuffLength]); // Read/Write data from/to Receive/Transmit Data Buffer : RXSPxB or TXSPxB
			if(SPORT2BTxBuffLength==0)
			SPORT2BTxBuffLength=strlen(strB);
		}
	}

}

static void SPORT3Handler(void)
{

	if((*pSPCTL3 & 0xC0000000) != 0x00000000)	// Check DXS_A 2-Bit-Field status for Channel-A Data Buffer Status
	{
		SPORT3A_RxByte = *pRXSP3A; // Read/Write data from/to Receive/Transmit Data Buffer : RXSPxB or TXSPxB
		SPORT3A_RxByte_Available=1;
	}

	if((*pSPCTL3 & 0x180000000) != 0x00000000)	// Check DXS_B 2-Bit-Field status for Channel-A Data Buffer Status
	{
		//SPORT3B_RxByte = *pRXSP3B; // Read/Write data from/to Receive/Transmit Data Buffer : RXSPxB or TXSPxB
	}

}

static void SPORT4Handler(void)
{
	if((*pSPCTL4 & 0xC0000000) != 0x00000000)	// Check DXS_A 2-Bit-Field status for Channel-A Data Buffer Status
	{
		*pTXSP4A = SPORT4A_TxByte; // Read/Write data from/to Receive/Transmit Data Buffer : RXSPxB or TXSPxB
	}

	if((*pSPCTL4 & 0x180000000) != 0x00000000)	// Check DXS_B 2-Bit-Field status for Channel-A Data Buffer Status
	{
		//*pTXSP2B = SPORT4B_TxByte; // Read/Write data from/to Receive/Transmit Data Buffer : RXSPxB or TXSPxB
	}

}

static void SPORT5Handler(void)
{

	if((*pSPCTL5 & 0xC0000000) != 0x00000000)	// Check DXS_A 2-Bit-Field status for Channel-A Data Buffer Status
	{
		SPORT5A_RxByte = *pRXSP5A; // Read/Write data from/to Receive/Transmit Data Buffer : RXSPxB or TXSPxB
		SPORT5A_RxByte_Available=1;
	}

	if((*pSPCTL5 & 0x180000000) != 0x00000000)	// Check DXS_B 2-Bit-Field status for Channel-A Data Buffer Status
	{
		//SPORT5B_RxByte = *pRXSP5B; // Read/Write data from/to Receive/Transmit Data Buffer : RXSPxB or TXSPxB
	}

}

static void SPORT6Handler(void)
{

	if((*pSPCTL6 & 0xC0000000) != 0x00000000)	// Check DXS_A 2-Bit-Field status for Channel-A Data Buffer Status
	{
		SPORT6A_RxByte = *pRXSP6A; // Read/Write data from/to Receive/Transmit Data Buffer : RXSPxB or TXSPxB
		SPORT6A_RxByte_Available=1;
	}

	if((*pSPCTL6 & 0x180000000) != 0x00000000)	// Check DXS_B 2-Bit-Field status for Channel-A Data Buffer Status
	{
		//SPORT6B_RxByte = *pRXSP6B; // Read/Write data from/to Receive/Transmit Data Buffer : RXSPxB or TXSPxB
	}

}

static void SPORT7Handler(void)
{

	if((*pSPCTL7 & 0xC0000000) != 0x00000000)	// Check DXS_A 2-Bit-Field status for Channel-A Data Buffer Status
	{
		SPORT7A_RxByte = *pRXSP7A; // Read/Write data from/to Receive/Transmit Data Buffer : RXSPxB or TXSPxB
		SPORT7A_RxByte_Available=1;
	}

	if((*pSPCTL7 & 0x180000000) != 0x00000000)	// Check DXS_B 2-Bit-Field status for Channel-A Data Buffer Status
	{
		//SPORT7B_RxByte = *pRXSP7B; // Read/Write data from/to Receive/Transmit Data Buffer : RXSPxB or TXSPxB
	}

}

unsigned char SPORT_RxProcess(unsigned int temp)
{
	unsigned char ch;
	unsigned int value=0;

	value+= ((temp>>23) &(1<<0));
	value+= ((temp>>19) &(1<<1));
	value+= ((temp>>15) &(1<<2));
	value+= ((temp>>11) &(1<<3));
	value+= ((temp>>7) &(1<<4));
	value+= ((temp>>3) &(1<<5));
	value+= ((temp<<1) &(1<<6));
	value+= ((temp<<5) &(1<<7));
	ch=(unsigned char)value;

	return ch;
}

unsigned short int SPORT_TxProcess(unsigned char tx1)
{
	unsigned short int tx=0x00;
	unsigned char temp=0;

	unsigned short int Ms1= 0x03FC;
	unsigned short int Ms2= 0x0401;

	tx = tx1;
	tx= tx<<2;
	tx = tx & Ms1;
	tx = tx | Ms2;

	return tx;

}


/*
 * This function initializes the SRU for below actions:
 * 1. Configure DAI-Pin-2 as SPORT0-UART Transmit Pin.
 * 2. Configure DAI-Pin-4 as SPORT1-UART Receive Pin.
 * 3. Call Precision Clock Generator Init Function to generate external clocks to SPORT-0 and 1.
 * 4. Configures the SPORT-0 and 1 for receiving external clock that is from PCG.
 * 5. Configure the DAI-Pin-4 as SPORT1 to received External Frame Sync signal from DAI-Pin-4 which is also act as UART/SPORT Data Pin
 */
void ConfigSRU(void)
{

	InitPCG();	// Precision Clock Generator generates Clocks to for Tx and Rx of SPORT0-7.

	/******* UART-1 Config:SPORT0-A with DAI_PB-2::EZ-BoardPin:15 as Tx & SPORT1-A with DAI_PB-1::EZ-BoardPin:13 as Rx **********/

		/* SPORT-0 Pins Config to transmit Data */
		SRU(HIGH,PBEN02_I);             //Enable Pin-Buffer so that DAI-Pin-2 act as SPORT-0-UART Transmit Data Pin. Tx-Output from ADSP
		SRU(SPORT0_DA_O,DAI_PB02_I);    //SPORT0 Data Pin is connected to DAI-Pin-2 as SPORT-0-UART Transmit Data Pin. Tx-Output from ADSP

		/* SPORT-1 Pins Config to Receive Data*/
		SRU(LOW,PBEN01_I);              //Disable Pin-Buffer so that DAI-Pin-4 act as SPORT-1-UART Receive Data Pin. Rx-Input to ADSP
		SRU(DAI_PB01_O,SPORT1_DA_I);    //DAI-Pin-4 is connected as SPORT1 Data Pin which is configured to received data. Receive Data Pin.

		/* SPORT-0,1 and PCG Pins Config */
		InitPCG();
		SRU(PCG_CLKA_O,SPORT0_CLK_I);       //SPORT-0_CLK_I is same as SPORT0_CLK_O.
		SRU(PCG_CLKB_O,SPORT1_CLK_I);       //SPORT-1_CLK_I is same as SPORT1_CLK_O.
		SRU(DAI_PB01_O,SPORT1_FS_I);    	//SPORT-1 Frame Sync is connected to DAI-Pin-4.

	/****UART-2 Config:SPORT2-A with DAI_PB-4::EZ-BoardPin:19 as Tx & SPORT3-A with DAI_PB-3::EZ-BoardPin:17 as Rx ****/

		/* SPORT-0 Pins Config to transmit Data */
		SRU(HIGH,PBEN04_I);             //Enable Pin-Buffer so that DAI-Pin-2 act as SPORT-0-UART Transmit Data Pin. Tx-Output from ADSP
		SRU(SPORT2_DA_O,DAI_PB04_I);    //SPORT0 Data Pin is connected to DAI-Pin-2 as SPORT-0-UART Transmit Data Pin. Tx-Output from ADSP

		/* SPORT-3 Pins Config to Receive Data*/
		SRU(LOW,PBEN03_I);              //Disable Pin-Buffer so that DAI-Pin-4 act as SPORT-1-UART Receive Data Pin. Rx-Input to ADSP
		SRU(DAI_PB03_O,SPORT3_DA_I);    //DAI-Pin-4 is connected as SPORT1 Data Pin which is configured to received data. Receive Data Pin.

		/* SPORT-0,1 and PCG Pins Config */
		SRU(PCG_CLKA_O,SPORT2_CLK_I);       //SPORT-0_CLK_I is same as SPORT2_CLK_O.
		SRU(PCG_CLKB_O,SPORT3_CLK_I);       //SPORT-1_CLK_I is same as SPORT3_CLK_O.
		SRU(DAI_PB03_O,SPORT3_FS_I);    	//SPORT-1 Frame Sync is connected to DAI-Pin-4.

	/****UART-3 Config:SPORT4-A with DAI_PB-6::EZ-BoardPin:16 as Tx & SPORT5-A with DAI_PB-5::EZ-BoardPin:14 as Rx ****/

		/* SPORT-0 Pins Config to transmit Data */
		SRU(HIGH,PBEN06_I);             //Enable Pin-Buffer so that DAI-Pin-2 act as SPORT-0-UART Transmit Data Pin. Tx-Output from ADSP
		SRU(SPORT4_DA_O,DAI_PB06_I);    //SPORT0 Data Pin is connected to DAI-Pin-2 as SPORT-0-UART Transmit Data Pin. Tx-Output from ADSP

		/* SPORT-3 Pins Config to Receive Data*/
		SRU(LOW,PBEN05_I);              //Disable Pin-Buffer so that DAI-Pin-4 act as SPORT-1-UART Receive Data Pin. Rx-Input to ADSP
		SRU(DAI_PB05_O,SPORT5_DA_I);    //DAI-Pin-4 is connected as SPORT1 Data Pin which is configured to received data. Receive Data Pin.

		/* SPORT-0,1 and PCG Pins Config */
		SRU(PCG_CLKA_O,SPORT4_CLK_I);       //SPORT-0_CLK_I is same as SPORT2_CLK_O.
		SRU(PCG_CLKB_O,SPORT5_CLK_I);       //SPORT-1_CLK_I is same as SPORT3_CLK_O.
		SRU(DAI_PB05_O,SPORT5_FS_I);    	//SPORT-1 Frame Sync is connected to DAI-Pin-4.

	/****UART-4 Config:SPORT0-B with DAI_PB-8::EZ-BoardPin:20 as Tx & SPORT6-A with DAI_PB-7::EZ-BoardPin:18 as Rx ****/

		/* SPORT-0 Pins Config to transmit Data */
		SRU(HIGH,PBEN08_I);             //Enable Pin-Buffer so that DAI-Pin-2 act as SPORT-0-UART Transmit Data Pin. Tx-Output from ADSP
		SRU(SPORT0_DB_O,DAI_PB08_I);    //SPORT0 Data Pin is connected to DAI-Pin-2 as SPORT-0-UART Transmit Data Pin. Tx-Output from ADSP

		/* SPORT-3 Pins Config to Receive Data*/
		SRU(LOW,PBEN07_I);              //Disable Pin-Buffer so that DAI-Pin-4 act as SPORT-1-UART Receive Data Pin. Rx-Input to ADSP
		SRU(DAI_PB07_O,SPORT6_DA_I);    //DAI-Pin-4 is connected as SPORT1 Data Pin which is configured to received data. Receive Data Pin.

		/* SPORT-0,1 and PCG Pins Config */
		SRU(PCG_CLKA_O,SPORT0_CLK_I);       //SPORT-0_CLK_I is same as SPORT2_CLK_O.
		SRU(PCG_CLKB_O,SPORT6_CLK_I);       //SPORT-1_CLK_I is same as SPORT3_CLK_O.
		SRU(DAI_PB07_O,SPORT6_FS_I);    	//SPORT-1 Frame Sync is connected to DAI-Pin-4.


	/****UART-5 Config:SPORT2-B with DAI_PB-10::EZ-BoardPin:43 as Tx & SPORT7-A with DAI_PB-9::EZ-BoardPin:41 as Rx ****/

		/* SPORT-0 Pins Config to transmit Data */
		SRU(HIGH,PBEN10_I);             //Enable Pin-Buffer so that DAI-Pin-2 act as SPORT-0-UART Transmit Data Pin. Tx-Output from ADSP
		SRU(SPORT2_DB_O,DAI_PB10_I);    //SPORT0 Data Pin is connected to DAI-Pin-2 as SPORT-0-UART Transmit Data Pin. Tx-Output from ADSP

		/* SPORT-3 Pins Config to Receive Data*/
		SRU(LOW,PBEN09_I);              //Disable Pin-Buffer so that DAI-Pin-4 act as SPORT-1-UART Receive Data Pin. Rx-Input to ADSP
		SRU(DAI_PB09_O,SPORT7_DA_I);    //DAI-Pin-4 is connected as SPORT1 Data Pin which is configured to received data. Receive Data Pin.

		/* SPORT-0,1 and PCG Pins Config */
		SRU(PCG_CLKA_O,SPORT2_CLK_I);       //SPORT-0_CLK_I is same as SPORT2_CLK_O.
		SRU(PCG_CLKB_O,SPORT7_CLK_I);       //SPORT-1_CLK_I is same as SPORT3_CLK_O.
		SRU(DAI_PB09_O,SPORT7_FS_I);    	//SPORT-1 Frame Sync is connected to DAI-Pin-4.

}

/*
 * This function initializes the SPORT0 Channel A to emulate the UART Transmit and SPORT1 Channel A to emulate the UART Receive.
 * The code is developed using the Analog Devices Application Notes: EE-191, EE-391, EE-1245
 */
void Init_SPORT()
{

	//Clearing the Buffers of SPORTs.
	*pSPCTL0 = 0;
	*pSPCTL1 = 0;
	*pSPCTL2 = 0;
	*pSPCTL3 = 0;
	*pSPCTL4 = 0;
	*pSPCTL5 = 0;
	*pSPCTL6 = 0;
	*pSPCTL7 = 0;

	//FSDIV needs to fix for SPORTs:: Set FSDIV as 32 or above for 29-Bits Receive and set 12 or above for 11-Bit transfer
	*pDIV0 = 0x00170000|(1<<1);	/*Divisor register for FS and CLK for SPORT-0*/
	*pDIV1 = 0x001B0000|(1<<1);	/*Divisor register for FS and CLK for SPORT-1*/
	*pDIV2 = 0x00170000|(1<<1);	/*Divisor register for FS and CLK for SPORT-2*/
	//*pDIV3 = 0x001B0000|(SPORT_CLK_DIVISOR<<1);	/*Divisor register for FS and CLK for SPORT-3*/
	//*pDIV4 = 0x00170000|(SPORT_CLK_DIVISOR<<1);	/*Divisor register for FS and CLK for SPORT-4*/
	//*pDIV5 = 0x001B0000|(SPORT_CLK_DIVISOR<<1);	/*Divisor register for FS and CLK for SPORT-5*/
	//*pDIV6 = 0x001B0000|(SPORT_CLK_DIVISOR<<1);	/*Divisor register for FS and CLK for SPORT-6*/
	//*pDIV7 = 0x001B0000|(SPORT_CLK_DIVISOR<<1);	/*Divisor register for FS and CLK for SPORT-7*/

	//UART-1 config uses SPORT1A and 2A.
	*pSPCTL0=SPEN_A |  SPEN_B | SLEN11 | CKRE |  FSR | IFS | LFS | LAFS  | SPTRAN | LSBF; //Sets SPORT0-A&B as UART1-Tx & UART4-Tx.
	*pSPCTL1=LAFS | LFS | FSR | SLEN29 | SPEN_A ;	//Sets SPORT1-A as UART1-Rx.

	//UART-2 config uses SPORT2A and 3A.
	*pSPCTL2=SPEN_A | SPEN_B | SLEN11 | CKRE |  FSR | IFS | LFS | LAFS  | SPTRAN | LSBF;   //Sets SPORT2-A&B as UART2-Tx & UART5-Tx.
	*pSPCTL3=LAFS | LFS | FSR | SLEN29 | SPEN_A;	//Sets SPORT3-A as UART2-Rx.

	//UART-3 config uses SPORT4A and 5A.
	*pSPCTL4=SPEN_A |  SPEN_B | SLEN11 | CKRE |  FSR | IFS | LFS | LAFS  | SPTRAN | LSBF;  //Sets SPORT4-A as UART3-Tx
	*pSPCTL5=LAFS | LFS | FSR | SLEN29 | SPEN_A;	//Sets SPORT3-A as UART3-Rx.

	//UART-4 config uses SPORT0B and 6A.
	*pSPCTL6=LAFS | LFS | FSR | SLEN29 | SPEN_A;	//Sets SPORT6-A as UART4-Rx.

	//UART-5 config uses SPORT2B and 7A.
	*pSPCTL7=LAFS | LFS | FSR | SLEN29 | SPEN_A | CKRE;	//Sets SPORT7-A as UART4-Rx.


	//Old setting shared with ARDE, Pune.
	/*
	if(SPORT_UART_BAUDRATE==9600 || SPORT_UART_BAUDRATE==19200 || SPORT_UART_BAUDRATE==38400 || SPORT_UART_BAUDRATE==56000|| SPORT_UART_BAUDRATE==57600)
				*pSPCTL3=LAFS | LFS | FSR | SLEN27 | SPEN_A;	// Config SPORT-1 Channel-A as Transmit for 9600 Baud Rate
			else
				*pSPCTL3=LAFS | LFS | FSR | SLEN29 | SPEN_A;	// Config SPORT-1 Channel-A as Transmit for 921600 & Above Baud Rates
	*/

}

void InitPCG(void)
{
    unsigned char n;

    /*Disable PCG = Clearing the PCG Enable Bits in order to Config PCG */
    *pPCG_CTLA0 = 0x00;
    *pPCG_CTLB0 = 0x00;

    // Wait minimum 16 cycles after disable the PCG : to give enough time to stabilize PCG.
	for (n=0; n<100; n++)
    {
        asm("NOP;");
    }

    /*PCG CLK generation */
	*pPCG_SYNC1 = (*pPCG_SYNC1) | 0x00000004;	// Configuring the Channel-A input clock as PCLK=200MHz.
	*pPCG_SYNC1 = (*pPCG_SYNC1) | 0x00040000;	// Configuring the Channel-B input clock as PCLK=200MHz.

    *pPCG_CTLA1 = TxCLKDIV & CLKADIV;	// Configuring the Channel-A to generate Clock output suitable to UART-Transmit
    *pPCG_CTLB1 = RxCLKDIV & CLKBDIV;	// Configuring the Channel-B to generate Clock output suitable to UART-Receive = 03 times of Transmit Buad Rate

    /*Enable PCG = Setting the PCG Enable Bits in order to Config PCG */
    *pPCG_CTLA0 = ENCLKA;
    *pPCG_CTLB0 = ENCLKB;

}


/*
 * This function processes the Over sampled (03 times of UART Baud Rate) SPORT received data then return extracted Byte.
 * The code is developed using the Analog Devices Application Notes: EE-191, EE-391, EE-1245
 */

//UART-1 Related Functions :: UART-1 Receive Function.
unsigned char SPORT1A_Receive(void)
{
	unsigned char ch;
	unsigned int temp, value=0;

	while((*pSPCTL1 & (RXS0_A | RXS1_A))==0);
	temp = *pRXSP1A;

	value+= ((temp>>23) &(1<<0));
	value+= ((temp>>19) &(1<<1));
	value+= ((temp>>15) &(1<<2));
	value+= ((temp>>11) &(1<<3));
	value+= ((temp>>7) &(1<<4));
	value+= ((temp>>3) &(1<<5));
	value+= ((temp<<1) &(1<<6));
	value+= ((temp<<5) &(1<<7));
	ch=(unsigned char)value;

	return ch;

}

/*
 * This function transmits the byte via SPORT but data is framed such that the data is in Standard UART data format like
 * 01 Start Bit, 8-Data Bits, 01 Stop Bit and additionally one extra Start Bit is needed to emulate SPORT as UART.
 * The code is developed using the Analog Devices Application Notes: EE-191, EE-391, EE-1245
 */
//UART-1 Related Functions :: UART-1 Transmit Function.
void SPORT0A_Transmit(unsigned char tx1)
{
	unsigned short int tx=0x00;
	unsigned char temp=0;

	unsigned short int Ms1= 0x03FC;
	unsigned short int Ms2= 0x0401;

	tx = tx1;
	tx= tx<<2;
	tx = tx & Ms1;
	tx = tx | Ms2;

	while((*pSPCTL0 & (DXS0_A & DXS1_A)) !=0);
	*pTXSP0A = tx;

}

//UART-2 Related Functions :: UART-2 Receive Function.
unsigned char SPORT3A_Receive(void)
{
	unsigned char ch;
	unsigned int temp, value=0;

	while((*pSPCTL3 & (RXS0_A | RXS1_A))==0);
	temp = *pRXSP3A;

	value+= ((temp>>23) &(1<<0));
	value+= ((temp>>19) &(1<<1));
	value+= ((temp>>15) &(1<<2));
	value+= ((temp>>11) &(1<<3));
	value+= ((temp>>7) &(1<<4));
	value+= ((temp>>3) &(1<<5));
	value+= ((temp<<1) &(1<<6));
	value+= ((temp<<5) &(1<<7));
	ch=(unsigned char)value;

	return ch;
}

//UART-2 Related Functions :: UART-2 Transmit Function.
void SPORT2A_Transmit(unsigned char tx1)
{
	unsigned short int tx=0x00;
	unsigned char temp=0;

	unsigned short int Ms1= 0x03FC;
	unsigned short int Ms2= 0x0401;

	tx = tx1;
	tx= tx<<2;
	tx = tx & Ms1;
	tx = tx | Ms2;
	while((*pSPCTL2 & (DXS0_A & DXS1_A)) !=0);
	*pTXSP2A = tx;

}


//UART-3 Related Functions :: UART-3 Receive Function.
unsigned char SPORT5A_Receive(void)
{
	unsigned char ch;
	unsigned int temp, value=0;

	while((*pSPCTL5 & (RXS0_A | RXS1_A))==0);
	temp = *pRXSP5A;

	value+= ((temp>>23) &(1<<0));
	value+= ((temp>>19) &(1<<1));
	value+= ((temp>>15) &(1<<2));
	value+= ((temp>>11) &(1<<3));
	value+= ((temp>>7) &(1<<4));
	value+= ((temp>>3) &(1<<5));
	value+= ((temp<<1) &(1<<6));
	value+= ((temp<<5) &(1<<7));
	ch=(unsigned char)value;

	return ch;
}

//UART-3 Related Functions :: UART-3 Transmit Function.
void SPORT4A_Transmit(unsigned char tx1)
{
	unsigned short int tx=0x00;
	unsigned char temp=0;

	unsigned short int Ms1= 0x03FC;
	unsigned short int Ms2= 0x0401;

	tx = tx1;
	tx= tx<<2;
	tx = tx & Ms1;
	tx = tx | Ms2;
	while((*pSPCTL4 & (DXS0_A & DXS1_A)) !=0);
	*pTXSP4A = tx;

}



//UART-4 Related Functions :: UART-4 Receive Function.
unsigned char SPORT6A_Receive(void)
{
	unsigned char ch;
	unsigned int temp, value=0;

	while((*pSPCTL6 & (RXS0_A | RXS1_A))==0);
	temp = *pRXSP6A;

	value+= ((temp>>23) &(1<<0));
	value+= ((temp>>19) &(1<<1));
	value+= ((temp>>15) &(1<<2));
	value+= ((temp>>11) &(1<<3));
	value+= ((temp>>7) &(1<<4));
	value+= ((temp>>3) &(1<<5));
	value+= ((temp<<1) &(1<<6));
	value+= ((temp<<5) &(1<<7));
	ch=(unsigned char)value;

	return ch;
}

//UART-4 Related Functions :: UART-4 Transmit Function.
void SPORT0B_Transmit(unsigned char tx1)
{
	unsigned short int tx=0x00;
	unsigned char temp=0;

	unsigned short int Ms1= 0x03FC;
	unsigned short int Ms2= 0x0401;

	tx = tx1;
	tx= tx<<2;
	tx = tx & Ms1;
	tx = tx | Ms2;
	while((*pSPCTL0 & (DXS0_B & DXS1_B)) !=0);
	*pTXSP0B = tx;

}


//UART-5 Related Functions :: UART-5 Receive Function.
unsigned char SPORT7A_Receive(void)
{
	unsigned char ch;
	unsigned int temp, value=0;

	while((*pSPCTL7 & (RXS0_A | RXS1_A))==0);
	temp = *pRXSP7A;

	value+= ((temp>>23) &(1<<0));
	value+= ((temp>>19) &(1<<1));
	value+= ((temp>>15) &(1<<2));
	value+= ((temp>>11) &(1<<3));
	value+= ((temp>>7) &(1<<4));
	value+= ((temp>>3) &(1<<5));
	value+= ((temp<<1) &(1<<6));
	value+= ((temp<<5) &(1<<7));
	ch=(unsigned char)value;

	return ch;
}

//UART-5 Related Functions :: UART-5 Transmit Function.
void SPORT2B_Transmit(unsigned char tx1)
{
	unsigned short int tx=0x00;
	unsigned char temp=0;

	unsigned short int Ms1= 0x03FC;
	unsigned short int Ms2= 0x0401;

	tx = tx1;
	tx= tx<<2;
	tx = tx & Ms1;
	tx = tx | Ms2;
	while((*pSPCTL2 & (DXS0_B & DXS1_B)) !=0);
	*pTXSP2B = tx;

}

void TestSingleUART(void)
{
		/****UART-1 uses:	SPORT0-A with DAI_PB-2::EZ-BoardPin:15 as Tx and
		 * 				    SPORT1-A with DAI_PB-1::EZ-BoardPin:13 as Rx 		****/

		/*

		RxData=SPORT1A_Receive();
		SPORT0A_Transmit(RxData);
		if((RxData!=0x55) && (RxData!=0xAA) && (RxData!=0x00) && (RxData!=0xFF))
		{
			printf("\t %x\n", RxData);
			while(1);
		}

		RxData=SPORT1A_Receive();
		SPORT0A_Transmit(RxData);
		if((RxData!=0x55) && (RxData!=0xAA) && (RxData!=0x00) && (RxData!=0xFF))
		{
			printf("\t %x\n", RxData);
			while(1);
		}

		RxData=SPORT1A_Receive();
		SPORT0A_Transmit(RxData);
		if((RxData!=0x55) && (RxData!=0xAA) && (RxData!=0x00) && (RxData!=0xFF))
		{
			printf("\t %x\n", RxData);
			while(1);
		}

		RxData=SPORT1A_Receive();
		SPORT0A_Transmit(RxData);
		if((RxData!=0x55) && (RxData!=0xAA) && (RxData!=0x00) && (RxData!=0xFF))
		{
			printf("\t %x\n", RxData);
			while(1);
		}

		*/


		/****UART-2 uses:	SPORT2-A with DAI_PB-4::EZ-BoardPin:19 as Tx and
		 * 				    SPORT3-A with DAI_PB-3::EZ-BoardPin:17 as Rx 		****/

/*
		RxData=SPORT3A_Receive();
		SPORT2A_Transmit(RxData);
		if((RxData!=0x55) && (RxData!=0xAA) && (RxData!=0x00) && (RxData!=0xFF))
		{
			printf("\t %x\n", RxData);
			while(1);
		}

		RxData=SPORT3A_Receive();
		SPORT2A_Transmit(RxData);
		if((RxData!=0x55) && (RxData!=0xAA) && (RxData!=0x00) && (RxData!=0xFF))
		{
			printf("\t %x\n", RxData);
			while(1);
		}

		RxData=SPORT3A_Receive();
		SPORT2A_Transmit(RxData);
		if((RxData!=0x55) && (RxData!=0xAA) && (RxData!=0x00) && (RxData!=0xFF))
		{
			printf("\t %x\n", RxData);
			while(1);
		}

		RxData=SPORT3A_Receive();
		SPORT2A_Transmit(RxData);
		if((RxData!=0x55) && (RxData!=0xAA) && (RxData!=0x00) && (RxData!=0xFF))
		{
			printf("\t %x\n", RxData);
			while(1);
		}

*/


		/****UART-3 uses:	SPORT4-A with DAI_PB-6::EZ-BoardPin:16 as Tx and
		 * 				    SPORT5-A with DAI_PB-5::EZ-BoardPin:14 as Rx 		****/


/*
		RxData=SPORT5A_Receive();
		SPORT4A_Transmit(RxData);
		if( !((RxData==0x55)||(RxData==0xAA)||(RxData==0x00)||(RxData==0xFF))  ) //If RxData is NOT AA or 55  or 00 or FF then IF is TRUE
		{
			printf("\t %x\n", RxData);
			while(1);
		}

		RxData=SPORT5A_Receive();
		SPORT4A_Transmit(RxData);
		if((RxData!=0x55) && (RxData!=0xAA) && (RxData!=0x00) && (RxData!=0xFF))
		{
			printf("\t %x\n", RxData);
			while(1);
		}

		RxData=SPORT5A_Receive();
		SPORT4A_Transmit(RxData);
		if((RxData!=0x55) && (RxData!=0xAA) && (RxData!=0x00) && (RxData!=0xFF))
		{
			printf("\t %x\n", RxData);
			while(1);
		}

		RxData=SPORT5A_Receive();
		SPORT4A_Transmit(RxData);
		if((RxData!=0x55) && (RxData!=0xAA) && (RxData!=0x00) && (RxData!=0xFF))
		{
			printf("\t %x\n", RxData);
			while(1);
		}

*/


		/****UART-4 uses:	SPORT0-B with DAI_PB-8::EZ-BoardPin:20 as Tx and
		 * 				    SPORT6-A with DAI_PB-7::EZ-BoardPin:18 as Rx 		****/

/*
		RxData=SPORT6A_Receive();
		SPORT0B_Transmit(RxData);
		if((RxData!=0x55) && (RxData!=0xAA) && (RxData!=0x00) && (RxData!=0xFF))
		{
			printf("\t %x\n", RxData);
			while(1);
		}

		RxData=SPORT6A_Receive();
		SPORT0B_Transmit(RxData);
		if((RxData!=0x55) && (RxData!=0xAA) && (RxData!=0x00) && (RxData!=0xFF))
		{
			printf("\t %x\n", RxData);
			while(1);
		}

		RxData=SPORT6A_Receive();
		SPORT0B_Transmit(RxData);
		if((RxData!=0x55) && (RxData!=0xAA) && (RxData!=0x00) && (RxData!=0xFF))
		{
			printf("\t %x\n", RxData);
			while(1);
		}

		RxData=SPORT6A_Receive();
		SPORT0B_Transmit(RxData);
		if((RxData!=0x55) && (RxData!=0xAA) && (RxData!=0x00) && (RxData!=0xFF))
		{
			printf("\t %x\n", RxData);
			while(1);
		}

*/

		/****UART-5 uses:	SPORT1-B with DAI_PB-10::EZ-BoardPin:43 as Tx and
		 * 				    SPORT7-A with DAI_PB-9::EZ-BoardPin:41 as Rx 		****/

/*

		RxData=SPORT7A_Receive();
		SPORT2B_Transmit(RxData);

		SPORT2A_Transmit(RxData);


		if((RxData!=0x55) && (RxData!=0xAA) && (RxData!=0x00) && (RxData!=0xFF))
		{
			printf("\t %x\n", RxData);
			while(1);
		}

		RxData=SPORT7A_Receive();
		SPORT2B_Transmit(RxData);
		if((RxData!=0x55) && (RxData!=0xAA) && (RxData!=0x00) && (RxData!=0xFF))
		{
			printf("\t %x\n", RxData);
			while(1);
		}

		RxData=SPORT7A_Receive();
		SPORT2B_Transmit(RxData);
		if((RxData!=0x55) && (RxData!=0xAA) && (RxData!=0x00) && (RxData!=0xFF))
		{
			printf("\t %x\n", RxData);
			while(1);
		}

		RxData=SPORT7A_Receive();
		SPORT2B_Transmit(RxData);
		if((RxData!=0x55) && (RxData!=0xAA) && (RxData!=0x00) && (RxData!=0xFF))
		{
			printf("\t %x\n", RxData);
			while(1);
		}

*/
}