/*
This demo will be QC program for backplane

驗證過程只會顯示 LOOP CNT 
如果過程中有發生錯誤會顯示出來

Test Items

1.啟動 new timer function, 時間間隔為 3000 ms
中斷副程式工作內容
:GetTimeTicks() function
測試方式:
比對底板 timeTicks

:COM 0 communication
測試方式:
slot 0 ~ slot 3 or slot 0 ~ slot 7 都插上 i-87K 模組測試 i-87K 通訊是否正常	
	對 ChangeSlot(sendSlot); COM0 送出 $00M 命令
	startFlag =1 ; 完成 timer ISR工作 離開 ISR 回到主迴圈
	
測試程式主迴圈
:Date Time 
測試方式:
	讀取 DateTime , 比對之前的數值,看差距是否正常誤差 5秒內
	
:NVRAM read/write
測試方式:
	寫入 31 bytes 資料,比對,必須無誤

:Battery Backup SRAM
測試方式:
	每次寫入 500 BYTES資料, 總共會寫入 500*256 = 128 kb ,分256 次寫入
	

:COM 3 communication
:COM 4 communication
測試方式:
對送 500 bytes 不同資料,全雙工測試,三秒內必須收送完畢
比對資料必須無誤

:Battery low voltage detection
測試方式:
讀取 battery 的狀態,看電壓是否會受到溫度影響





[11 May,2009] by martin
----------------------------------------------------------------------
*/

#include "var.h"



unsigned long lData[8][32];
unsigned long Flash_MultiRead(unsigned int iSeg);


unsigned long loopCnt=0;
void sendCmd(void);
int iPort=0, lTimeout=200, iChksum=0,iSlot=0;
unsigned char cCmd[256];
int startFlag=0;
unsigned long t1,t0,t3,t4,t5;
int showDateFlag=0;
TIME_DATE  t2;
unsigned short yy,mm,dd,hh,min,sec,tt;
void TestRTC(void);

// variables for SRAM

#define BUFFER_SIZE (500)
uchar Wdata[BUFFER_SIZE];
uchar Rdata[2048],C2Buf[1024],C3Buf[1024],C4Buf[1024],CBuf[1024];
long c3Index=0,c4Index=0,c2Index=0;
int c3Head=0,c4Head=0,c2Head=0;
int MaxBlock=256;
unsigned char TC=128,bIndex=0;
void TestNVR(void);
void TestRAM(int block, unsigned char tCh);
unsigned short interval=3000;
void CheckBattery(void);
int totalSlot;
int sendSlot=0;
int c3OK=0,c4OK=0,c2OK=0;
int RS_232 =0,RS_485=0;
int c3Toc2=1,c2Toc3=0, iLength,er;
    
    char sInBuffer[2048],str[2048];	
unsigned char item2,item3,item4; 

void TestCOM34( unsigned char tCh);
void RecCOM34(void);
void RecCOM23(void);
void TestMMI(int num);
void TestKey(void);
void TestNetID(void);
unsigned char oldKeyStatus=0xff; 
void TestEEP(void);

int COM3_232=0,COM4_232=0,COM3_485=0,COM2_485=0;

unsigned freq[]={
//b5:191.78
//b6:215.27
242,
256,
287,
323,
342,
384,
431,
483,
512,
575,
645
};



char ID[]="!0587024";
int TestTicks(void)
{
	unsigned long dt1;
    int iRet;
	
	char c,r;
	int i,j;
	int ret;
    InitLib();
    
    totalSlot=GetNumberOfSlot();
    
    printf("Sound Test Do,Re,Mi,Fa,So,La,Si,Do\n");
	for(i=1;i<=8;i++){
		Sound(freq[i],1000);
		DelayMs(1000);
	}
	
	NoSound();
	//
    InstallUserTimerFunction_ms(interval,sendCmd); //begin UserTimerFunction time unit is 1 ms.
    t0 = GetTimeTicks();
    t1 = GetTimeTicks();
    t3 = GetTimeTicks();
    // Please change InstallCom0 to InstallCom_DMA_0 to prevent data lost.
    
    
    
    
    printf("Start Timer Function...\t");
    GetTimeDate(&t2);   // read initial date & time
    yy = t2.year;
    mm = t2.month;
    dd = t2.day;
    hh = t2.hour;
    min= t2.minute;
    sec= t2.sec;
    
	printf("Start %04d/%02d/%02d %02d:%02d:%02d\n\r", 
              t2.year, t2.month, t2.day, t2.hour, t2.minute, t2.sec );    
    for(;;)
    {
    	//printf( "4444444\n\r" );
    	//doServer();
    	
        
       	
    	//printf( "5555555\n\r" );
    	TestKey();
    	if(RS_232==1)
    	{
    		RecCOM34();

	    }
	    if(RS_485==1) // RS-485 Test
	    {
	    	
	    	    
			RecCOM23();
	    	
	    }	        
	   // printf("C3 %d C4 %d\r",c3Index,c4Index);         	
		
    	if(startFlag==1)
    	{
    		if(showDateFlag==0)
    		{
    			
    			printf("Loop Count %lu\n\r",loopCnt);
    			
    			//Check Date Time

			    
			    TestNVR();
				TestRTC();
		    	
		    	// Check Battery
				CheckBattery();
				
				//Check Battery Backup SRAM   
				TestEEP();
				TestRAM( bIndex,TC);
				TestCOM34(TC);
				TestMMI(bIndex);
				TestNetID();
				Flash_MultiRead(bIndex %8);
				iLength=Ethernet_Receive(iSocket,sInBuffer, 50, 0);
				bIndex++;
				TC++;	       		
		       				    			
				showDateFlag=1;    			
    		}
	    	if(WaitTransmitOver0()==0)
	    	{
	       		ret=ReceiveResponseFrom7000_1(iPort,  cCmd, lTimeout, iChksum);
	       		
				sendSlot++;
				if(sendSlot >=totalSlot)
					sendSlot=0;	            
	            startFlag=0;				 
	        }
	    }
		if (IsCom1()) 
		{
		 	c=ReadCom1(); 
		 	if ((c=='e') || (c=='E')) 
		 	{		 		
		 		StopUserTimerFun(); 
		 		
		 		return 1;
		 			
		 	}
		 	
		}         
    }
}

//送出命令同時檢查 timeTicks 是否有亂掉
void sendCmd(void)  
{
	unsigned long dt;
	_asm cli
	t0=GetTimeTicks();
	_asm sti
	
	dt=GetTimeTicks()-t1;
	t1=t0;
	if(dt>interval)
	{
		printf("\nError Ticks %lu\n",dt);
		exit(0);
	}
	else
	{
		printf("Check TimeTicks OK dT[%ld] \r",dt);
	}

		
	if(plugInSlot[sendSlot]==1)
	{
		cCmd[0]=0;
		ChangeToSlot(sendSlot);
	//	SendCmdTo7000( iPort,"$00M", iChksum);
	}

	
	showDateFlag=0;
	startFlag=1;
	loopCnt++;
}


// S512 size =512
// 1 block 2048 2KB
// max block 256 block

void TestCOM34( unsigned char tCh)
{
	int i;
	
	for(i=0;i<BUFFER_SIZE;i++){
		Wdata[i]=tCh;//random(256);
	}
    memset( CBuf, '*', BUFFER_SIZE );
    CBuf[0]='$';
    CBuf[BUFFER_SIZE-1]='@';	
	ToComBufn(3,CBuf,BUFFER_SIZE); 
	
    memset( CBuf, ':', BUFFER_SIZE );
    CBuf[0]='#';
    CBuf[BUFFER_SIZE-1]='~';	
	ToComBufn(4,CBuf,BUFFER_SIZE);
	c3Index=0;
	c4Index=0;
	RS_232=1;
	//printf("COM3 <--> COM4 500Byte Full Duplex Test\n");	
}

void TestRAM(int addr, unsigned char tCh)
{ 
	
	int i;
  	//unsigned long error=0,x1;
	unsigned long error=0,MaxRamSize;

	MaxRamSize=512*1024UL;
	//MaxRamSize=size*1024UL;
	//srand(GetTimeTicks());
	//printf("W SRAM[%d][%02X]\n\r",block,tCh);
	
	for(i=0;i<BUFFER_SIZE;i++){
		Wdata[i]=tCh;//random(256);
	}
	

	t4=GetTimeTicks();
	

	//for(i=0,addr=0;i<MaxBlock;i++,addr+=2048){
		//printf("\rBlock %d",block);
	S256_WriteFn(addr,BUFFER_SIZE,Wdata);
	//}
	//printf("\n\rRead & Check data.\n\r");
	//for(i=0,addr=0;i<MaxBlock;i++,addr+=2048){
		//printf("\rBlock %d",block);
	S256_ReadFn(addr,BUFFER_SIZE,Rdata);
	//printf("dt R/W SRAM[%lu] =%lu\n",BUFFER_SIZE,GetTimeTicks()-t4);
	for(i=0;i<BUFFER_SIZE;i++)
	{
		if(Wdata[i] != Rdata[i]) 
		{
			error++;
			printf("E [%d:][%d][%02x][%02x][%02x]\n",i,addr,tCh,Wdata[i],Rdata[i]);
			exit(0);
		}
		else
		{
			//printf("%02X ",Rdata[i]);
		}
	}
	
	for(i=0;i<BUFFER_SIZE;i++){
		Wdata[i]=0;//random(256);
	}	
	S256_WriteFn(addr,BUFFER_SIZE,Wdata);
	//}

}




void RecCOM34(void)
{
	if(c3OK==1 && c4OK==1)
	{
		ClearCom2();
		ClearCom3();
		ClearCom4();
		
		printf("COM3 <--> COM4 Test OK\n");
		//printf("=====================================\n\n");
		
		//printf("COM2 \n");
		if(c3Toc2==1)
		{
			//printf("COM3 --> COM2 RS-485 500B \n");
			memset( CBuf, '*', BUFFER_SIZE );
			CBuf[0]='$';
		    CBuf[BUFFER_SIZE-1]='@';	
			ToComBufn(3,CBuf,BUFFER_SIZE);
			//c3Toc2=0;
			
		}
		else //if(c2Toc3==1)
		{
			//printf("COM2 --> COM3 RS-485 500B\n");
		    memset( CBuf, ':', BUFFER_SIZE );
		    CBuf[0]='#';
		    CBuf[BUFFER_SIZE-1]='~';	
			ToComBufn(2,CBuf,BUFFER_SIZE);
			//c3Toc2=1;	
		}				
		
		RS_232=0;
		RS_485=1;
		c3OK=0;
		c4OK=0;
	} 
                                   
    if(IsCom(3))                                                  
    { 
    	                                                         
        item3=ReadCom(3); 
        //printf("%c\n",item3);
        if( c3Index==0)// check head
        {
        	c3Head=1;
        	if(item3!='#')
        	{
        		printf("\nCOM3 Head  %c [%d] Error\n",item3,c3Index);
        		exit(0);
        	}
        	else
        	{
        	//	printf("COM3 Head %c [%d]\n",item3,c3Index);
        	}
        	C3Buf[c3Index]=item3;
        	//printf("%c\n",C3Buf[c3Index]);
        	c3Index++; 
        	
        }
        else // check end
        {
        	if(c3Head==1)
        	{
            	if(item3=='~')
            	{
            		C3Buf[c3Index]=item3;
            		//printf("%c\n",C3Buf[c3Index]);
            		c3Index++;
            		if(c3Index!=BUFFER_SIZE)
            		{
            			printf("\nCOM3 End %c [%d] Error\r\n\n",item3,c3Index); 
            			exit(0);
            		}
            		else
            		{
            			//printf("COM3 End %c [%d] \r\n\n",item3,c3Index); 
            		}
            		c3OK=1;
            		c3Index=0;
            		c3Head=0;
            	}
            	else
            	{
            		if(item3!=':')
            		{
            			printf("\nCOM3 Body Error %c\n",item3);
            			exit(0);
            		}
            		else
            		{
            			//printf("COM3 Body %c [%d]\n",item3,c3Index);
            		}
            		C3Buf[c3Index]=item3;
            		//printf("%c\n",C3Buf[c3Index]);
            		c3Index++;
            	}
            }
        } 
        
                              
    }
    if(IsCom(4))                                                  
    {                                                            
        item4=ReadCom(4); 
        if( c4Index==0)// check head
        {
        	c4Head=1;
        	if(item4!='$')
        	{
        		printf("\nCOM4 Head  %c [%d] Error\n",item4,c4Index);
        		exit(0);
        	}
        	else
        	{
        		//printf("COM4 Head %c [%d]\n",item4,c4Index);
        	}
        	C4Buf[c4Index]=item4;
        	//printf("%c\n",C4Buf[c4Index]);
        	c4Index++; 
        	
        }
        else // check end
        {
        	if(c4Head==1)
        	{
            	if(item4=='@')
            	{
            		C4Buf[c4Index]=item4;
            		//printf("%c\n",C4Buf[c4Index]);
            		c4Index++;
            		if(c4Index!=BUFFER_SIZE)
            		{
            			printf("\nCOM4 End %c [%d] Error\r\n\n",item4,c4Index); 
            			exit(0);
            		}
            		else
            		{
            			//printf("COM4 End %c [%d] \r\n\n",item4,c4Index); 
            		}
            		c4OK=1;
            		c4Index=0;
            		c4Head=0;
            	}
            	else
            	{
            		if(item4!='*')
            		{
            			printf("\nCOM4 Body Error %c\n",item4);
            			exit(0);
            		}
            		else
            		{
            			//printf("COM4 Body %c [%d]\n",item4,c4Index);
            		}
            		C4Buf[c4Index]=item4;
            		//printf("%c\n",C4Buf[c4Index]);
            		c4Index++;
            	}
            }
        } 
    }	
}

void RecCOM23(void)
{
	//c3Toc2 =0 com3 will receive data
    if(IsCom(3))                                                  
    { 
    	if(c3Toc2==1)  // 會從 COM3 送資料, COM2 會收到資料
    		printf("[%d] COM 3 can not receive any data here\n",c3Toc2);
    	                                                         
        item3=ReadCom(3); 
        //printf("%c\n",item3);
        if( c3Index==0)// check head
        {
        	c3Head=1;
        	if(item3!='#')
        	{
        		printf("\nCOM3 Head  %c [%d] Error\n",item3,c3Index);
        		exit(0);
        	}
        	else
        	{
        		//printf("COM3 Head %c [%d]\n",item3,c3Index);
        	}
        	C3Buf[c3Index]=item3;
        	//printf("%c\n",C3Buf[c3Index]);
        	c3Index++; 
        	
        }
        else // check end
        {
        	if(c3Head==1)
        	{
            	if(item3=='~')
            	{
            		C3Buf[c3Index]=item3;
            		//printf("%c\n",C3Buf[c3Index]);
            		c3Index++;
            		if(c3Index!=BUFFER_SIZE)
            		{
            			printf("\nCOM3 End %c [%d] Error\r\n",item3,c3Index); 
            			exit(0);
            		}
            		else
            		{
            			printf("COM2 --> COM3 Test OK [%d]B \r\n\n",c3Index);
            			
            			//printf("COM3 End %c [%d] \r\n\n",item3,c3Index); 
            		}
            		printf("=====================================\n\n");
            		ClearCom2();
            		ClearCom3();
            		ClearCom4();
            		//RS_232=1;
            		RS_485=0;
            		c3Index=0;
            		c3Head=0;
            		c3Toc2=1;
            	}
            	else
            	{
            		if(item3!=':')
            		{
            			printf("\nCOM3 Body Error %c\n",item3);
            			exit(0);
            		}
            		else
            		{
            			//printf("COM3 Body %c [%d]\n",item3,c3Index);
            		}
            		C3Buf[c3Index]=item3;
            		//printf("%c\n",C3Buf[c3Index]);
            		c3Index++;
            	}
            }
        } 
        
                              
    }
    if(IsCom(2))                                                  
    {     
    	if(c3Toc2==0)
    	{
    		printf("[%d] COM 2 can not receive any data here\n",c3Toc2);	
    		exit(0);
    	}        	                                                       
        item2=ReadCom(2); 
        if( c2Index==0)// check head
        {
        	c2Head=1;
        	if(item2!='$')
        	{
        		printf("\nCOM2 Head  %c [%d] Error\n",item2,c2Index);
        		exit(0);
        	}
        	else
        	{
        		//printf("COM2 Head %c [%d]\n",item2,c2Index);
        	}
        	C2Buf[c2Index]=item2;
        	//printf("%c\n",C4Buf[c4Index]);
        	c2Index++; 
        	
        }
        else // check end
        {
        	if(c2Head==1)
        	{
            	if(item2=='@')
            	{
            		C2Buf[c2Index]=item2;
            		//printf("%c\n",C4Buf[c4Index]);
            		c2Index++;
            		if(c2Index!=BUFFER_SIZE)
            		{
            			printf("\nCOM2 End %c [%d] Error\r\n\n",item2,c2Index); 
            			exit(0);
            		}
            		else
            		{
            			printf("COM3 --> COM2 Test OK [%d]B \r\n\n",c2Index);
            			//printf("COM2 End %c [%d] \r\n\n",item2,c2Index); 
            		}
            		//printf("=====================================\n\n");
            		//RS_232=1;
            		RS_485=0;
            		c2Index=0;
            		c2Head=0;
            		ClearCom2();
            		ClearCom3();		            				            		
            		ClearCom4();
            		c3Toc2=0;	            		
            	}
            	else
            	{
            		if(item2!='*')
            		{
            			printf("\nCOM2 Body Error %c\n",item2);
            			exit(0);
            		}
            		else
            		{
            			//printf("COM2 Body %c [%d]\n",item2,c2Index);
            		}
            		C2Buf[c2Index]=item2;
            		//printf("%c\n",C4Buf[c4Index]);
            		c2Index++;
            	}
            }
        } 
    }	    	
}	



void TestMMI(int num)
{
	
        
        
        //Show any segment of the 5-Digit Led
        printf("SMMI %d.%d.%d.%d.%d\n",num % 16,num % 16,num % 16,num % 16,num % 16);
        Show5DigitLedWithDot(1,num % 16);
        Show5DigitLedWithDot(2,num % 16);
        Show5DigitLedWithDot(3,num % 16);
        
        //Show any segment of the 5-Digit Led and also can show the DOT '.'.
        Show5DigitLedWithDot(4,num % 16);    //Show the fourth led is 'A'. 
        Show5DigitLedWithDot(5,num % 16);    //Show the fifth led is 'E'.
        
        
        
}	
void TestKey(void)
{
    if(IsSystemKey()) 
    {    			
        switch(GetSystemKey())
        {
           case SKEY_UP:
              // 
               if(oldKeyStatus & SKEY_UP_DOWN)    
               {   
               		SetLedL2(1);
					printf("UP KEY\n\r");                  
               }                                                                         
           break;
           case SKEY_DOWN:
              //     
               if(oldKeyStatus & SKEY_DOWN_DOWN)  
               {   
               	   	SetLedL3(1);   
               	   	printf("DOWN KEY\n\r");
               }
           break;
           case SKEY_SET:
                //   
               if(oldKeyStatus & SKEY_SET_DOWN) // SET Key as show function
               {  
               		printf("SET KEY\n\r");                    
       				SetLedL1(0);
       				SetLedL2(0);
       				SetLedL3(0);  
               	}                                           
           break;
           case SKEY_MODE:
               // 按下 Mode Key 時主要是使 netConfigFlag=1
               if(oldKeyStatus & SKEY_MODE_DOWN)
               {  
               		SetLedL1(1);
                    printf("Mode KEY\n\r");
               }                                
           break;
        }    	
    } 
}	

void TestNetID(void)
{
	char str[2][32];
	strcpy(str[1],"CLOSE");
	strcpy(str[0],"OPEN");
	printf("\nNetID= %02X\tINIT PIN =%s\n",GetNetId(),str[ReadInitPin()]);
	

}

// for 512K 要在 256 次內讀完,必須一次檢查 2K
// iSeg 0 ~ 8 , iOffset 0~ 31
unsigned long Flash_MultiRead(unsigned int iSeg)
{
    unsigned long sum=0;
    int c;
    unsigned int i;
    
    t5=GetTimeTicks();
    for(i=0;i<2048;i++)
    {
        c=FlashReadB((iSeg+0x8)*0x1000 ,i );  
        //printf("[%X][%d][%d]\n", (iSeg+0x8)*0x1000,i,c);     
        sum+=(unsigned char)c;
    }
    //lData[iSeg][iOffset]=sum;
   // printf("\nFlash Read = %lu\n",GetTimeTicks()-t5);
    printf("S[%d]=[%08lX]\n",iSeg,sum);
    return sum;
}
/*
void EE_WriteProtect(void);
void EE_WriteEnable(void);
int EE_RandomRead(int Block,unsigned Addr);
unsigned char EE_ReadNext(int Block);
int EE_MultiRead(int StartBlock,unsigned StartAddr,int no,void *databuf);
int EE_RandomWrite(int Block,unsigned Addr,int Data);
int EE_MultiWrite(int Block,unsigned Addr,int no,const void *src);
int EE_MultiWrite_A(int Block,unsigned Addr,unsigned no,const void *src);
int EE_MultiWrite_L(unsigned address,unsigned no,const void *src);
int EE_MultiRead_L(unsigned address,unsigned no,void *databuf);
*/
unsigned int eepAddr=0;
void TestEEP(void)
{
	
    int iTemp;
	
	int errorflag=0;
    unsigned char v0,v1,v2;

    EE_MultiRead_L(eepAddr,1,(char*)&v0);  // 原先的數值
    
    v1= v0 + 1;
    	
    EE_WriteEnable();
	
    EE_MultiWrite_L(eepAddr,1,(char*)&v1); //改變後的數值
    
    EE_MultiRead_L(eepAddr,1,(char*)&v2);  // 改變後的數值
    
    if(v1!= v2)
    {
    	errorflag=1;
    	printf("EEP[%u]v1[%02X]v2[%02X] error\n",eepAddr,v1,v2);
    }
    else
    {
    	//printf("EEP[%u]v1[%02X]v2[%02X] OK\n",eepAddr,v1,v2);  
    }   
    
    EE_MultiWrite_L(eepAddr,1,(char*)&v0); //改變後的數值
    
    EE_MultiRead_L(eepAddr,1,(char*)&v2);  // 改變後的數值
    
    if(v0!= v2)
    {
    	errorflag=1;
    	printf("EEP[%u]v0[%02X]v2[%02X] error\n",eepAddr,v0,v2);
    }  
    else
    {
    //	printf("EEP[%u]v0[%02X]v2[%02X] OK\n",eepAddr,v0,v2);  
    }
    if(errorflag==0)
    	printf("EEP[%u] Test OK\n");
    else
    	exit(0);
    /*
    block=iBlock
    offset=iNo*2 ===> each integer occupy 2 bytes
    byte count=2 ===> each integer occupy 2 bytes
    pointer to the data= (char*)&iValue
    */
	
    EE_WriteProtect();

    //Read back value from EEPROM to confirm the value is 
    //wrote to EEPROM successfully.
    eepAddr ++;
}

void TestNVR(void)
{
	int j;
	int errorflag=0;
    // Check NVRAM
	for(j=0;j<31;j++)
   		WriteNVRAM(j,j+0x31);	
   		
	for(j=0;j<31;j++)
	{
		if(ReadNVRAM(j)!= j+0x31)
		{
			errorflag=1;
   			printf("\nError NVR[%d][%c]\n",j,ReadNVRAM(j));
   		}
   		else
   		{
   			//printf("NVR[%d][%c]\n",j,ReadNVRAM(j));	
   		}
   	
	}	
	if(errorflag==0)
		printf("NVRAM Test OK\n");	
	else
		exit(0);
}


void TestRTC(void)
{
	int errorflag=0;
	GetTimeDate(&t2);   // read initial date & time
	if(t2.year - yy >1)
	{	
		errorflag=1;
		printf("\nError year %04d [%04d]\n", t2.year, yy );
		//return ErrY;
	}
	if(t2.month -mm >1)
	{
		errorflag=1;
		printf("\nError month %02d [%02d]\n", t2.month, mm );
		//return ErrM;
	}
	if(t2.day -dd >1)
	{
		errorflag=1;
		printf("\nError day %02d [%02d]\n", t2.day, dd );
		//return ErrD;
	}
	if(t2.hour==0 && hh==23)
		tt=24;
	else
		tt=t2.hour;										
	if(tt -hh >1)
	{
		errorflag=1;
		printf("\nError hour %02d [%02d]\n", t2.hour, hh );
		//return ErrH;
	}
	if(t2.minute==0 && min==59)
		tt=60;
	else
		tt=t2.minute;				
	if(tt -min >1)
	{
		errorflag=1;
		printf("\nError minute %02d [%02d]\n", t2.minute, min );
		//return ErrMIN;
	}
	if(t2.sec<=3 && sec>=57 )
		tt=60;
	else
		tt=t2.sec;
	if(tt -sec >5)
	{
		errorflag=1;
		printf("\nError sec %02d [%02d]\n", t2.sec, sec );
		//return ErrS;	
	}
	else
	{																			
	
		//printf("\nnow %04d/%02d/%02d %02d:%02d:%02d\r", 
	             // t2.year, t2.month, t2.day, t2.hour, t2.minute, t2.sec );
	}
    yy = t2.year;
    mm = t2.month;
    dd = t2.day;
    hh = t2.hour;
    min= t2.minute;
    sec= t2.sec;	
    if(errorflag==0)
		printf("RTC DateTime Test OK\n");
	else
		exit(0);	
	
}

void CheckBattery(void)
{
int status;	
    status=GetBatteryStatus();
	if(status & BATTERY_LOW_1)
	{
		printf("\npower of battery 1 is LOW\n\r");
		exit(0);
		//power of battery 1 is LOW
	}
	else 
	{
		//printf("\npower of battery 1 is OK\n\r");
	}
	if(status & BATTERY_LOW_2)
	{
		printf("\npower of battery 2 is LOW\n\r");	
		exit(0);
	}
	else
	{
		//printf("\npower of battery 2 is OK\n\r");
	}	
}	