//----------------------------------------------------------------------------
// C main line
// Jworked on - July 7th 2009 
//----------------------------------------------------------------------------

#include <m8c.h>        // part specific constants and macros
#include "PSoCAPI.h"    // PSoC API definitions for all User Modules
#include "nvSRAM.h"
#include "ports.h"
#include "Package_Type.h" //This file sets Chip select line for 8/16 SOIC packages

#pragma interrupt_handler PSoC_GPIO_INT

BYTE bRxData; 
BOOL fWaitToReceiveByte(void);
char * strPtr; 
void Initialize(void);
char theStr[] = "SPI nvSRAM Test";                // Define RAM string    
BYTE OPCODE=0xFF;
BYTE addrMSB, addrISB, addrLSB;
BYTE data=0x55;
BYTE cData=0xAA;
BYTE Data[255];
BYTE dataLen=0x00;
BYTE cmdLen;
BOOL RTCLoopOut=FALSE;
BYTE ReadDataBuf[127]; ///Read Data buffer array - Max Read for 255 bytes of data

void makeBytes(void);
void nvSRAMCompleteTest(void);
void RTCCheck(void);
void Delay(int);  //will introduce 2xms milisecond delay at 6MHz CPU
void main()

{   
    int i=0; 
	BYTE j=0;
	BYTE HR=0x00, MIN=0x00, SEC=0x00;
	BYTE readdata, rtcData;
	BYTE bE2ERR;
	BOOL e2error;
	BYTE *SE2Flag =0x00;
	
	Initialize(); 
	
	E2PROM_E2Read( 10, SE2Flag, 1); /// Read Autostore Enable Flag from E2PROM address 0
	///Flag in EEPROM is set to remember Autostore Enable and Disable Flag by the controller to perform
	//Appropriate operation without changing user code and automated entire memory test.
	 
	 if(*SE2Flag ==59)
	 
	 {
	   UART_CPutString("\r\n AutoStoreEnable is Set In Previous Power Down\r\n");
	 }
     
	 else if(*SE2Flag ==19)
	 
	 {
	   UART_CPutString("\r\n AutoStoreEnable is Disabled In Previous Power Down\r\n");
	 }

	
	RTCCheck(); // Check RTC Flag and control register and display on screen
	UART_CPutString("\r\n**************Serial nvSRAM test**************\r\n");  
    
	if(SOIC8)
	{UART_CPutString("\r\n 8 PIN  Package is Enabled\r\n");
    Port0_0(1); ///Disable Chip select for 16 pin pkg
	}
	if(SOIC16)
	{UART_CPutString("\r\n 16 PIN  Package is Enabled\r\n");
    Port2_4(1);///Disable Chip select for 8 pin pkg
	}
    /********This will display command on Hyperterminal***/
		UART_CPutString("\r\n******************Serial nvSRAM Test ***************\r\n");
		UART_CPutString("Command Table - Command entry without space\r\n");
		UART_CPutString("e.g.- 0xOpcodeAdd1Add2Add3Data1Data2..\r\n");
		UART_CPutString("Opcode:06 - (WREN) Set Write Enable Latch\r\n");
		UART_CPutString("Opcode:04 - (WRDI) Reset Write Enable Latch\r\n");
		UART_CPutString("Opcode:05 - (RDSR) Read Status Register\r\n");
		UART_CPutString("Opcode:01 - (WRSR) Write Status Register\r\n\n");
		
		UART_CPutString("Opcode:02 - (Write) nvSRAM Write (Max Data byte lengh is 123 Bytes\r\n");
		UART_CPutString("Opcode:03 - (Read)  nvSRAM Read; Format:03Addr(MSB ISB LSB)(Max 127 Bytes)\r\n\n");
		UART_CPutString("Opcode:3C - (Store) Software Store \r\n");
		UART_CPutString("Opcode:60 - (Recall)Software Recall\r\n");
		UART_CPutString("Opcode:59 - (ASENB)Austore Enable  \r\n");
		UART_CPutString("Opcode:19 - (ASDISB)Austore Disable \r\n\n");
		
		UART_CPutString("Opcode:12 - (WRRTC)Write RTC registers\r\n");
		UART_CPutString("Opcode:13 - (RDRTC)Read RTC Registers\r\n");
		
		UART_CPutString("Other (non Instruction Set) Executions\r\n");
		UART_CPutString("Opcode:00 - nvSRAM Check - Status Register Validate\r\n");
		UART_CPutString("Opcode:19FF - Austore Disable (Permanent) \r\n");
		UART_CPutString("Opcode:FF - nvSRAM Full Test \r\n");
		UART_CPutString("Opcode:FE - Display RTC Registers Minute: Sec : Format\r\n");
		UART_CPutString("Opcode:F0 - Display RTC Registers DD:MM:YYYY:DAY \r\n");
		UART_CPutString(">>");
		/****************************************************/
	while(1) 
	{ 
	if(UART_bCmdCheck())   // Wait for command  
	   
	{ while(strPtr = UART_szGetParam()) 
		{  
		
		cmdLen = UART_bCmdLength(); // Returns the length of entered data through UART port
		makeBytes(); //This function converts ASCII char to hex and compact into byte format
		             // Rejects any char which is non hex digit
	    RTCLoopOut=TRUE;
		switch (Data[0])
		{
		
		case 0x00: // Write 0x00 to the status register
			WRSR(0x00); 					
	    	CS_LO;	//CS pin 
			while(!(SPIM_bReadStatus() & SPIM_SPIM_TX_BUFFER_EMPTY));
				{
	    			SPIM_SendTxData(0x06); //dummy write
				}
			CS_HI;	//CS pin 
			UART_CPutString("\r\n Status Register- Expected: 02\r\n");  
			UART_CPutString("\r\n Status Register- Actual: ");
			readdata=RDSR();
			UART_PutSHexByte(readdata); // read status register, it should read back 0x04
		break;
				
		case 0x02: // Opcode for Write		
			if(cmdLen/2<=4)
			{
			UART_CPutString("\r\n Insufficient Data to execute - Please enter again\r\n");
			break;
			} 
			//nvSRAMBURSTWRITE_ARR(Data[3], Data[2], Data[1], (cmdLen/2)-4, Data,1); ///first 4 bytes are opcode and address Opcode_MSB_ISB_LSB
		    nvSRAMBURSTWRITE_ARR(Data[1], Data[2], Data[3], (cmdLen/2)-4, Data,1); 
		break; 
		
		case 0x03: //Opcode for Read
			if(cmdLen/2<=4)
			{
			UART_CPutString("\r\n Insufficient Data to execute - Please enter again\r\n");
			break;
			} 
		 	 if(0) ///For debug -Reset data buffer to zero prior to every execution
		 	{
		  	for (i=0;i<Data[4];i++)
		 	{ ReadDataBuf[i]=0x00;
		  	UART_CPutString("\r\n Databuffer initialize to 0");
		  	UART_PutSHexByte(ReadDataBuf[i]);
		  	}
		  	}
			//nvSRAMBURSTREAD_ARR(Data[3], Data[2], Data[1], Data[4],&ReadDataBuf[0],0);
			nvSRAMBURSTREAD_ARR(Data[1], Data[2], Data[3], Data[4],&ReadDataBuf[0],0); // LSB ISB MSB
			UART_CPutString("\r\n Read Data\r\n");  
			for (i=0;i<Data[4];i++)
			UART_PutSHexByte(ReadDataBuf[i]); 
		break; 		
		
		case 0x3C:  ///Opcode for Software Store
			nvSRAMSTORE();
			UART_CPutString("\r\n Software Store Executed\r\n");
		break;
				
		case 0x60:  ///Opcode for Software Recall
			nvSRAMRECALL();
			UART_CPutString("\r\n Software Recall Executed\r\n");
		break;
				
		case 0x59:  ///Opcode for Autostore Enable
			AutoStoreEnable();		
			UART_CPutString("\r\n Autostore Enabled \r\n");
			e2error=E2PROM_bE2Write(10,Data,1,25);
			
		break;
		
		case 0x19:  ///Opcode for Autostore Disable
			if(Data[1]==0xFF)
			{
			AutoStoreDisable();
			nvSRAMSTORE(); ///This disables Autostore untill enabled by issuing ASENB command
			}
			else
			AutoStoreDisable();
			UART_CPutString("\r\n Autostore Disabled\r\n");
			e2error=E2PROM_bE2Write(10,Data,1,25);
			
		break;
		
		case 0xFF:
			nvSRAMCompleteTest();
			UART_CPutString("\r\n nvSRAM fullchip test Completed with above result\r\n");
		break;
		
		case 0x06: //Set Write En Latch
			WREN();
		break;
		
		case 0x04: //Reset Write En Latch
			WRDI();
		break;
		
		case 0x05: //Read Status Reg
			UART_CPutString("\r\n Status Reg :0X");
			UART_PutSHexByte(RDSR()); 
		break;
		
		case 0x01: //Write Status Reg
			if(cmdLen/2<2)
			{
			UART_CPutString("\r\n Insufficient Data to execute - Please enter again\r\n");
			break;
			}
			WRSR(Data[1]);
		break;
		
		case 0x13: //Read RTC Register 0x13 Addr
			if(cmdLen/2<2)
			{
			UART_CPutString("\r\n Insufficient Data to execute - Please enter again\r\n");
			break;
			}
			
			if (Data[1]==0xff)
			{
			 UART_CPutString("\r\n Reading All RTC Registers\r\n");
			 for (j=0; j<=0x0f; j++)
			 {
			 UART_CPutString("\r\n RTC Register:[");
			 UART_PutSHexByte(j);
			 UART_CPutString("]= 0x");
			 UART_PutSHexByte(RDRTC(j,0));
			 }
			 break;
			}
			if(Data[1]>0x0f)
			{
			UART_CPutString("\r\n Invalid RTC Address; Please ender a valid address\r\n");
			break;
			}
			UART_CPutString("\r\n RTC Reg:");
			
			UART_PutSHexByte(RDRTC(Data[1],0));
		break;
		
		case 0x12: //Write RTC Register 0x12 Addr Data
			if(cmdLen/2<3)
			{
			UART_CPutString("\r\n Insufficient Data to execute - Please enter again\r\n");
			break;
			}
			if(Data[1]>0x0f)
			{
			UART_CPutString("\r\n Invalid RTC Address; Please ender a valid address\r\n");
			break;
			}
			rtcData=RDRTC(0x00,0);
			rtcData=rtcData|0x02; //Set W bit to 1 prior to writing to RTC regs
			WRRTC (0x00,rtcData); 
			WRRTC (Data[1],Data[2]);
		break;
		
		case 0xFE:
			LCD_Start();                  // Initialize LCD  
			LCD_Position(0,0);            // Place LCD cursor at row 0, col 5.  
			LCD_PrCString ("Hr:Min:Sec");
			UART_CPutString("\r\n To Exit This Loop: Press SW to toggle P3_1 -> Hi to Low"); 
			while (RTCLoopOut)
			{
			rtcData=RDRTC(0x00,0);
			rtcData=rtcData&0xfe;
			WRRTC (0x00,rtcData); // Reset R bit to zero prior to read 
			
			LCD_Position(1,7);
			SEC =RDRTC(0x09,0);
			LCD_PrHexByte(SEC); 
			
			LCD_Position(1,3);
			MIN =RDRTC(0x0a,0);
			LCD_PrHexByte(MIN);
			
			LCD_Position(1,0);
			HR = RDRTC(0x0b,0);
			LCD_PrHexByte(HR); 				
			}
		break;
		
		case 0xF0:
			LCD_Start();                  // Initialize LCD  
			LCD_Position(0,0);            // Place LCD cursor at row 0, col 5.  
			LCD_PrCString ("DD:MM:YY:DAY");
			UART_CPutString("\r\n To Exit This Loop: Press SW to toggle P3_1 -> Hi to Low"); 
			while (RTCLoopOut)
			{
			rtcData=RDRTC(0x00,0);
			rtcData=rtcData&0xfe;
			WRRTC (0x00,rtcData); // Reset R bit to zero prior to read 
			
			LCD_Position(1,0);
			SEC =RDRTC(0x0D,0);
			LCD_PrHexByte(SEC); 
			
			LCD_Position(1,3);
			MIN =RDRTC(0x0E,0);
			LCD_PrHexByte(MIN);
			
			LCD_Position(1,6);
			HR = RDRTC(0x0F,0);
			LCD_PrHexByte(HR); 
			
			LCD_Position(1,9);
			HR =RDRTC(0x0C,0); 
			
			if(HR==1)
			LCD_PrCString ("SUN");
			
			else if (HR==2)
			LCD_PrCString ("MON");
		
			
			else if(HR==3)
			LCD_PrCString ("TUE");
	
			else if(HR==4)
			LCD_PrCString ("WED");
		
			else if(HR==5)
			LCD_PrCString ("THR");
			
			else if(HR==6)
			LCD_PrCString ("FRI");
		
			else if(HR==7)
			LCD_PrCString ("SAT");
			
			else
			LCD_PrCString ("Invad Date");
			
		
		}
		break;
		
		default:
			UART_CPutString("\r\n Invalid Command - Enter again\r\n");  
			break;
		}
	}
		UART_CmdReset();                          // Reset command buffer
		*strPtr=0x00; // This is to ensure that previous data buffer is reset to zero- Optional
    	 /********This will display command on Hyperterminal***/
		//UART_CPutString("\r\n******************Serial nvSRAM Test ***************\r\n");
		UART_CPutString("\r\n\nCommand Table - Command entry without space\r\n");
		//UART_CPutString("e.g.- 0xOpcodeAdd1Add2Add3Data1Data2..\r\n");
		UART_CPutString("Opcode:06 - (WREN) Set Write Enable Latch\r\n");
		UART_CPutString("Opcode:04 - (WRDI) Reset Write Enable Latch\r\n");
		UART_CPutString("Opcode:05 - (RDSR) Read Status Register\r\n");
		UART_CPutString("Opcode:01 - (WRSR) Write Status Register\r\n\n");
		
		UART_CPutString("Opcode:02 - (Write) nvSRAM Write(Max Data byte lengh 123 Bytes\r\n");
		//UART_CPutString("Opcode:03 - (Read)  nvSRAM Read; Format:03Addr(MSB ISB LSB)(Max 127 Bytes)\r\n\n");
		UART_CPutString("Opcode:03 - (Read)  nvSRAM Read (Max 127 Bytes)\r\n");
		UART_CPutString("Opcode:3C - (Store) Software Store \r\n");
		UART_CPutString("Opcode:60 - (Recall)Software Recall\r\n");
		UART_CPutString("Opcode:59 - (ASENB)Austore Enable  \r\n");
		UART_CPutString("Opcode:19 - (ASDISB)Austore Disable \r\n\n");
		
		UART_CPutString("Opcode:12 - (WRRTC)Write RTC registers\r\n");
		UART_CPutString("Opcode:13 - (RDRTC)Read RTC Registers\r\n");
		UART_CPutString("Opcode:13ff - Read All RTC Registers\r\n");
		
	//	UART_CPutString("Other (non Instruction Set) Executions\r\n");
		UART_CPutString("Opcode:00 - nvSRAM Check - Status Register Validate\r\n");
	//	UART_CPutString("Opcode:19FF - Austore Disable (Permanent) \r\n");
		UART_CPutString("Opcode:FF - nvSRAM Full Test \r\n");
	//	UART_CPutString("Opcode:FE - Display RTC Registers Minute: Sec : Format\r\n");
		UART_CPutString(">>");
		/****************************************************/
	} 
   }
 }  
/// This function intialize PSoC user modules applied in the project.
//Any newly added user module should be avoid to keep main function clean and better readable format 
void Initialize(void) 
{
	UART_CmdReset();                      // Initialize receiver/cmd  
    UART_IntCntl(UART_ENABLE_RX_INT);     // Enable RX interrupts  
    UART_Start(UART_PARITY_NONE);         // Enable UART  
    M8C_EnableGInt ;                      // Turn on interrupts  
	M8C_EnableIntMask(INT_MSK0,INT_MSK0_GPIO); ///Enable GPIO Interrupt
	SPIM_Start(SPIM_SPIM_MODE_0 | SPIM_SPIM_MSB_FIRST); // Set SPI to MODE 0 and MSB first
	LCD_Start();                  // Initialize LCD  
   	LCD_Position(0,0);            // Place LCD cursor at row 0, col 5.  
   	LCD_PrString(theStr); 
    LCD_Position(1,0);
	E2PROM_Start(); 
	Port0_4(1); 			//Set HOLD pin HIGH
	CS_HI;				//Set CS pin high 
	Port0_5(1);
	Port0_6(1); // Interrup - Inout to PSoC
	Port0_7(1);
}

void makeBytes(void)

{
  int i=0;
  for (i=0;i<cmdLen;i++)
  {
     
   if((strPtr[i]>=0x41)&&(strPtr[i]<=0x46))
   strPtr[i]=strPtr[i]+ 0x09;

   else if ((strPtr[i]>=0x61)&&(strPtr[i]<=0x66))
   strPtr[i]=strPtr[i]+ 0x09;
   
   else if ((strPtr[i]>=0x30)&&(strPtr[i]<=0x39))
   strPtr[i]=strPtr[i];
  
   else
   {
   UART_CPutString("Data is not valid Hex data"); 
   return;
   }
   }
   
for (i=0;i<cmdLen/2;i++)
  {
Data[i]=((strPtr[2*i]& 0x0f)<<4)|((strPtr[2*i+1])& 0x0f);
  }
}

void RTCCheck(void)
{
BYTE rtcData=0x00;

rtcData=RDRTC(0x00,0);
UART_CPutString("\r\nRTC Flag Register(Addr@0x00):");
UART_PutSHexByte(rtcData);

rtcData=RDRTC(0x06,0);
UART_CPutString("\r\n RTC Interrupt/Status control Register(Addr@0x06):");
UART_PutSHexByte(rtcData);

rtcData=RDRTC(0x08,0);
UART_CPutString("\r\n RTC Calibration Register(Addr@0x08):");
UART_PutSHexByte(rtcData);
}

void PSoC_GPIO_INT()
{
   M8C_DisableGInt;                                 //Disables any interrupt when pin is held high
   //   while(PRT1DR & 0x10){                           //polls for the switch if its high
   RTCLoopOut=FALSE;
   if(0)
   UART_CPutString("\r\n Within GPIO ISR\n");
   M8C_EnableGInt;                                    //enable interrupts for further interrupts
}

void Delay (int ms)

{
  int i=0,j=0;
  
  for(j=0;j<ms;j++)
  for(i=0;i<1000;i++);

}

void nvSRAMCompleteTest(void)

{
BYTE rdsr;
BOOL flag=TRUE;
BYTE i=0x00;
BYTE tmpData=0x00;

////Check Status Register
WRSR(0x00); 
rdsr = RDSR(); 
 if(rdsr!=0x00)
 {
 UART_CPutString("\r\nStatus Reg check fialed for WRSR(0x00)\r\n");
 UART_CPutString("\r\nExpected 0x00 ==> Actual :\r\n");
 UART_PutSHexByte(rdsr);
 flag=FALSE;
 }

WREN();

rdsr = RDSR(); 
 if(rdsr!=0x02)
 {
 UART_CPutString("\r\nStatus Reg check fialed for WREN()\r\n");
 UART_CPutString("\r\nExpected 0x02 ==> Actual :\r\n");
 UART_PutSHexByte(rdsr);
 flag=FALSE;
 }

WRDI();
rdsr = RDSR(); 
 if(rdsr!=0x00)
 {
 UART_CPutString("\r\nStatus Reg check fialed for WRDI()\r\n");
 UART_CPutString("\r\nExpected 0x02 ==> Actual :\r\n");
 UART_PutSHexByte(rdsr);
 flag=FALSE;
 }

if(flag)
UART_CPutString("\r\nStatus Register Test : Pass\r\n");
///End of Status Reg check
//Reset Flag
flag =FALSE;

//Full Memory Test

UART_CPutString("\r\nFull Memory Write with 0xAA, Read and Verify-- Waiting\r\n");
nvSRAMBURSTWRITE_DI(0x00, 0x00, 0x00, 0x01FFFF, 0xAA);  //Write Entire memory with AA
flag = nvSRAMBURSTREAD_COMP(0x00, 0x00, 0x00, 0x01FFFF, 0xAA); // Read and Verify Entire memory
if(flag)
UART_CPutString("\r\n Full Memory Write 0xAA & Readback 0xAA : Pass\r\n");

UART_CPutString("\r\nFull Memory Write with 0x55, Read and Verify-- Waiting\r\n");
nvSRAMBURSTWRITE_DI(0x00, 0x00, 0x00, 0x01FFFF, 0x55);  //Write Entire memory with AA
flag = nvSRAMBURSTREAD_COMP(0x00, 0x00, 0x00, 0x01FFFF, 0x55); // Read and Verify Entire memory
if(flag)
UART_CPutString("\r\n Full Memory Write 0xAA & Readback 0x55 : Pass\r\n");

if(flag)
UART_CPutString("\r\n Full Memory Read /Write Test : Pass \r\n");
//Full Memory Test Complete
//Reset Flasg
flag =FALSE;

///Block Protect Function Test
nvSRAMBURSTWRITE_DI(0x00, 0x00, 0x00, 0x000010, 0xa5);  //Write memory with A5
nvSRAMBURSTWRITE_DI(0x01, 0x00, 0x00, 0x000010, 0xa5);  //Write memory with A5
nvSRAMBURSTWRITE_DI(0x01, 0x80, 0x00, 0x000010, 0xa5);  //Write memory with A5
WRSR(0x0C); //Full Block protect

nvSRAMBURSTWRITE_DI(0x00, 0x00, 0x00, 0x000010, 0x5a);  //Write memory with A5
nvSRAMBURSTWRITE_DI(0x01, 0x00, 0x00, 0x000010, 0x5a);  //Write memory with A5
nvSRAMBURSTWRITE_DI(0x01, 0x80, 0x00, 0x000010, 0x5a);  //Write memory with A5
for (i=0; i<0x0a; i++)
	{
 		tmpData = nvSRAMREAD(0x00, 0x00, i);
 		if(tmpData ==0xa5)
 				flag =TRUE;
 		else
 			{
  				flag =FALSE;
  				break;
 			}	 
			
		tmpData = nvSRAMREAD(0x01, 0x00, i);
 		if(tmpData ==0xa5)
 				flag =TRUE;
 		else
 			{
  				flag =FALSE;
  				break;
 			}	 
			
		tmpData = nvSRAMREAD(0x01, 0x80, i);
 		if(tmpData ==0xa5)
 				flag =TRUE;
 		else
 			{
  				flag =FALSE;
  				break;
 			}	 
			
 	}
	
	
	
if(!flag)
 UART_CPutString("\r\nFull Block Write Protect Failed\r\n");	
 if(flag)
 UART_CPutString("\r\n Full Memory Write Protect : Pass \r\n");
 //Reset Flasg
 flag =FALSE;
 
 
WRSR(0x08); //Half Block protect

nvSRAMBURSTWRITE_DI(0x00, 0x00, 0x00, 0x000010, 0x5a);  //Write memory with A5
nvSRAMBURSTWRITE_DI(0x01, 0x00, 0x00, 0x000010, 0x5a);  //Write memory with A5
nvSRAMBURSTWRITE_DI(0x01, 0x80, 0x00, 0x000010, 0x5a);  //Write memory with A5
for (i=0; i<0x0a; i++)
	{
 		tmpData = nvSRAMREAD(0x00, 0x00, i);
 		if(tmpData ==0x5a)
 				flag =TRUE;
 		else
 			{
  				flag =FALSE;
  				break;
 			}	 
			
		tmpData = nvSRAMREAD(0x01, 0x00, i);
 		if(tmpData ==0xa5)
 				flag =TRUE;
 		else
 			{
  				flag =FALSE;
  				break;
 			}	 
			
		tmpData = nvSRAMREAD(0x01, 0x80, i);
 		if(tmpData ==0xa5)
 				flag =TRUE;
 		else
 			{
  				flag =FALSE;
  				break;
 			}	 
			
 	}
	
	
	
if(!flag)
 UART_CPutString("\r\n Half Block Write Protect Failed\r\n");	
 if(flag)
 UART_CPutString("\r\n Half Memory Write Protect : Pass \r\n");
 //Reset Flasg
 flag =FALSE;
 
 WRSR(0x04); //Quarter Block protect

nvSRAMBURSTWRITE_DI(0x00, 0x00, 0x00, 0x000010, 0x5a);  //Write memory with A5
nvSRAMBURSTWRITE_DI(0x01, 0x00, 0x00, 0x000010, 0x5a);  //Write memory with A5
nvSRAMBURSTWRITE_DI(0x01, 0x80, 0x00, 0x000010, 0x5a);  //Write memory with A5
for (i=0; i<0x0a; i++)
	{
 		tmpData = nvSRAMREAD(0x00, 0x00, i);
 		if(tmpData ==0x5a)
 				flag =TRUE;
 		else
 			{
  				flag =FALSE;
  				break;
 			}	 
			
		tmpData = nvSRAMREAD(0x01, 0x00, i);
 		if(tmpData ==0x5a)
 				flag =TRUE;
 		else
 			{
  				flag =FALSE;
  				break;
 			}	 
			
		tmpData = nvSRAMREAD(0x01, 0x80, i);
 		if(tmpData ==0xa5)
 				flag =TRUE;
 		else
 			{
  				flag =FALSE;
  				break;
 			}	 
			
 	}
	
 if(!flag)
 UART_CPutString("\r\n Quarter Block Write Protect Failed\r\n");	
 if(flag)
 UART_CPutString("\r\n Quarter Memory Write Protect : Pass \r\n");
 //Reset Flasg
 flag =FALSE;
 
 WRSR(0x00); // Block protect Disabled

nvSRAMBURSTWRITE_DI(0x00, 0x00, 0x00, 0x000010, 0x5a);  //Write memory with A5
nvSRAMBURSTWRITE_DI(0x01, 0x00, 0x00, 0x000010, 0x5a);  //Write memory with A5
nvSRAMBURSTWRITE_DI(0x01, 0x80, 0x00, 0x000010, 0x5a);  //Write memory with A5
for (i=0; i<0x0a; i++)
	{
 		tmpData = nvSRAMREAD(0x00, 0x00, i);
 		if(tmpData ==0x5a)
 				flag =TRUE;
 		else
 			{
  				flag =FALSE;
  				break;
 			}	 
			
		tmpData = nvSRAMREAD(0x01, 0x00, i);
 		if(tmpData ==0x5a)
 				flag =TRUE;
 		else
 			{
  				flag =FALSE;
  				break;
 			}	 
			
		tmpData = nvSRAMREAD(0x01, 0x80, i);
 		if(tmpData ==0x5a)
 				flag =TRUE;
 		else
 			{
  				flag =FALSE;
  				break;
 			}	 
			
 	}
	
 if(!flag)
 UART_CPutString("\r\n  Block Write Protect Disable Failed\r\n");	
 if(flag)
 UART_CPutString("\r\n Block Write Protect Disable : Pass \r\n");
 //Reset Flag
 if(flag)
 UART_CPutString("\r\n Block Write Protections : Pass \r\n");
 
 
///Block Protect check Completed
///Reset flag
flag =FALSE;

///Software Store / Recall function Test

nvSRAMBURSTWRITE_DI(0x00, 0x00, 0x00, 0x000010, 0x55);  //Write memory with 55

for (i=0; i<0x0a; i++)
	{
 		tmpData = nvSRAMREAD(0x00, 0x00, i);
 		if(tmpData ==0x55)
 				flag =TRUE;
		else 
		break;
 	}	

nvSRAMSTORE();
flag =FALSE;

Delay(20);  //provides approx 40 ms delay @ 6 MHz CPU CLK
		
nvSRAMBURSTWRITE_DI(0x00, 0x00, 0x00, 0x000010, 0xCD);  //Write memory with A5
for (i=0; i<0x0a; i++)
	{
 		tmpData = nvSRAMREAD(0x00, 0x00, i);
 		if(tmpData ==0xCD)
 				flag =TRUE;
		else 
		break;
 	}			
			
if(!flag)
UART_CPutString("\r\n nvSRAM Write Fail during Software Store function test\r\n");


nvSRAMRECALL();
flag =FALSE;
Delay(1);  //Not necessary but provided

for (i=0; i<0x0a; i++)
	{
 		tmpData = nvSRAMREAD(0x00, 0x00, i);
 		if(tmpData ==0x55)
 				flag =TRUE;
		else 
		break;
 	}	
if(flag)
UART_CPutString("\r\n nvSRAM Software Store /Recall Test : Pass\r\n");

if(!flag)
UART_CPutString("\r\n nvSRAM Software Store /Recall Test : Failed\r\n");


}
