#include "Config.h"
#include "MainCtl.h"
#include "gvalue.h"
#include "DescpWrite.h"
#include "Gui.h"
#include "DevProperty.h"
	
extern "C"{
	#include "HW_Config.h"
	#include "uart_lp_test_drv.h"
	#include "file_cache.h"
	#include "user_norflash.h"
	#include "uart_key_drv.h"
	#include "usb_cdc_task.h"
	#include "user_eeprom.h"
	#include "task.h"
}

#define SDRAM_BUF_CACHE_ADDRESS			((uint32_t)0xC3F00000)
#define SDRAM_BUF_DATA_ADDRESS			((uint32_t)0xC3F80000)

#define DevInLoopCountMax 				250
#define DispInLoopCountMax 				16
#define LoopCountMax 							40
#define KeyInBusCountMax 					128
#define BusCountMax 							20
#define KeyInDirCountMax 					8
#define DirCountMax 							20
#define AreaCountMax 							1024
#define LinkCountMax 							2048

#define CFG_FILE_LINE_BUF_SIZE			((uint32_t)0x400)

#define dCtlCFG_BufCount 			((uint32_t)56)
#define dLoopDevCFG_BufCount 	((uint32_t)48)//cmd 0x11 0x15
#define dBusKeyCFG_BufCount 	((uint32_t)44)//cmd 0x21 0x25
#define dDirKeyCFG_BufCount 	((uint32_t)44)//cmd 0x31 0x35
#define dAreaCFG_BufCount 		((uint32_t)56)//cmd 0x41 0x45
#define dLinkUartCFG_BufCount ((uint32_t)20)//cmd 0x61 0x55
#define dLoopDispCFG_BufCount ((uint32_t)20)//cmd 0x61 0x65

#define CFG_LOAD_BUF_SIZE			((uint32_t)0x40)
#define CtlCfgBufAddr	 				dAddrSdram_CfgBuf             //0xC2400000
#define LoopDevCfgBufAddr	 		(CtlCfgBufAddr + CFG_LOAD_BUF_SIZE)//0xC2400040
#define BusKeyCfgBufAddr	 		(LoopDevCfgBufAddr + (DevInLoopCountMax * LoopCountMax * CFG_LOAD_BUF_SIZE))//0xC249C440
#define DirKeyCfgBufAddr 			(BusKeyCfgBufAddr + (KeyInBusCountMax * BusCountMax * CFG_LOAD_BUF_SIZE))//0xC24C4440
#define AreaCfgBufAddr 				(DirKeyCfgBufAddr + (KeyInDirCountMax * DirCountMax * CFG_LOAD_BUF_SIZE))//0xC24C6C40   END 0xC24D6C40
#define LoopDispCfgBufAddr 		(AreaCfgBufAddr + (AreaCountMax * CFG_LOAD_BUF_SIZE))//0xC24D6C40   END 0xC24D7000

#define CFG_TAB_BUF_SIZE			((uint32_t)0x100)
#define LoopDevCfgTabAddr 		(dAddrSdram_CfgBuf + 0x000E0000)
#define BusKeyCfgTabAddr 			(dAddrSdram_CfgBuf + 0x000E8000)
#define DirKeyCfgTabAddr 			(dAddrSdram_CfgBuf + 0x000F0000)
#define AreaCfgTabAddr 				(dAddrSdram_CfgBuf + 0x000F8000)

#define dLinkCfg_BufCount_Tx 	36
#define dLinkCfg_BufEntrySize_Tx 	32
#define dAddrSdram_LinkCfgTxBuf 	0xC24D8000//END 0xC24D7000

#define CFG_CTL_PATH 					"cfg01/ctl.csv"
#define CFG_LOOP_PATH 				"cfg01/loop.csv"
#define CFG_LOOP_DEV_PATH 		"cfg01/loop01.csv"
#define CFG_DIR_CTL_PATH 			"cfg01/dir.csv"
#define CFG_DIR_PATH 					"cfg01/dir01.csv"
#define CFG_BUS_CTL_PATH 			"cfg01/bus.csv"
#define CFG_BUS_PATH 					"cfg01/bus01.csv"
#define CFG_AREA_PATH 				"cfg01/area.csv"
#define CFG_LINK_PATH 				"cfg01/link.csv"

static unsigned int const sdramCacheBufAddr = SDRAM_BUF_CACHE_ADDRESS;

char readPath[20] = "cfg01/";

TConfigCtlData * pConfigCtlDataTX;
TConfigLoopData * pConfigLoopDataTX;
TConfigLoopDispData * pConfigLoopDispDataTX;
TConfigBusPanelData * pConfigBusDataTX;
TConfigDirPanelData * pConfigDirDataTX;
TConfigAreaData * pConfigAreaDataTX;
TUartCfgLinkData pUartCfgLinkDataTX;
TEquation * pLinkEquationTX;

TUartCfgLinkData * pUartCfgLinkDataRX;

unsigned int haveSaveCfgFlag = 0;

void TConfig::Init()
{
	unsigned int i,x,aAddr;
	
	pSectorTmpAddr = sdramCacheBufAddr;
	
	pCfgTxPkg = (TUartConfigInfo *)cfgRpyBuf;
	
	//pCfgRxPkg = (TUartConfigInfo *)uart_info_8bit[TEST_UART].rx_buf;
	//pUartCfgLinkDataRX = (TUartCfgLinkData *)&uart_info_8bit[TEST_UART].rx_buf;
	
	pCfgRxPkg = (TUartConfigInfo *)usb_cdc_data.rx_buf;
	pUartCfgLinkDataRX = (TUartCfgLinkData *)usb_cdc_data.rx_buf;
	
	pConfigCtlDataTX = (TConfigCtlData *)pCfgTxPkg->prm.data;
	pConfigLoopDataTX = (TConfigLoopData *)pCfgTxPkg->prm.data;
	pConfigLoopDispDataTX = (TConfigLoopDispData *)pCfgTxPkg->prm.data;
	pConfigBusDataTX = (TConfigBusPanelData *)pCfgTxPkg->prm.data;
	pConfigDirDataTX = (TConfigDirPanelData *)pCfgTxPkg->prm.data;
	pConfigAreaDataTX = (TConfigAreaData *)pCfgTxPkg->prm.data;
	
	uartRxFlag = 0;
	usbCdcRxFlag = 0;
	fileAnalysisErr = 0;
	dataAnalysisErr = 0;
	
	iLinkBufIndex = 0;
	oLinkBufIndex = 0;
	
	for(i=0; i<dLinkCfg_BufCount_Tx; i++){
		aAddr = dAddrSdram_LinkCfgTxBuf + (i * dLinkCfg_BufEntrySize_Tx);
		for(x = 0;x<(dLinkCfg_BufEntrySize_Tx/4);x++)
			*(volatile unsigned int *)(aAddr + (x*4)) = 0;
	}
}	

void TConfig::CfgPrmInit()
{
	unsigned int i = 0;
	
	pSectorTmpAddr = sdramCacheBufAddr;
	fileAnalysisErr = 0;
	dataAnalysisErr = 0;
	
	for(i = 0;i <= LoopCountMax;i++){
		loopDevCount[i] = 0;
		loopDevDispCount[i] = 0;
		loopCfgDevDispExistTab[i] = 0;
	}
	for(i = 0;i < BusCountMax;i++)busKeyCount[i] = 0;
	for(i = 0;i < DirCountMax;i++)dirKeyCount[i] = 0;
	areaCount = 0;
	
	for(i = 0;i < DevInLoopCountMax;i++)loopCfgDevTab[i] = 0;
	for(i = 0;i < KeyInBusCountMax;i++)busCfgKeyTab[i] = 0;
	for(i = 0;i < KeyInDirCountMax;i++)dirCfgKeyTab[i] = 0;
	for(i = 0;i < AreaCountMax;i++)areaCfgTab[i] = 0;
}	

void TConfig::DelayMs(unsigned int aMs)
{
	volatile unsigned int mTick;
	volatile unsigned int Tick;
	for(Tick=0; Tick<aMs; Tick++){
		for(mTick=0; mTick<160000; mTick++);
		sys_error_led_task();
	}
}

void TConfig::iLinkTXBuf(unsigned char * pData)
{
	unsigned int x,aAddr;
	
	iLinkBufIndex++;
	if(iLinkBufIndex >= dLinkCfg_BufCount_Tx)iLinkBufIndex = 0;
	
	aAddr = dAddrSdram_LinkCfgTxBuf + (iLinkBufIndex * dLinkCfg_BufEntrySize_Tx);
	for(x = 0;x<(dLinkCfg_BufEntrySize_Tx/4);x++){
		*(volatile unsigned int *)(aAddr + (x*4)) = *(volatile unsigned int *)pData;
		pData+=4;
	}
}

void TConfig::oLinkTXBuf()
{
	unsigned int x,aAddr,len;
	
	if(iLinkBufIndex != oLinkBufIndex){
		oLinkBufIndex++;
		if(oLinkBufIndex >= dLinkCfg_BufCount_Tx)oLinkBufIndex = 0;
		
		aAddr = dAddrSdram_LinkCfgTxBuf + (oLinkBufIndex * dLinkCfg_BufEntrySize_Tx);
		for(x = 0;x<(dLinkCfg_BufEntrySize_Tx/4);x++){
			pCfgTxPkg->D32[x] = *(volatile unsigned int *)(aAddr + (x*4));
		}
		
		oBufProcess();
	}
}

void TConfig::oBufProcess()
{
	//set_test_tx_data(pCfgTxPkg->D8, (pCfgTxPkg->prm.len + 4));
	usb_cdc_tx(pCfgTxPkg->D8, (pCfgTxPkg->prm.len + 4));
}

void TConfig::WaitChipIdle(unsigned int aCnt)
{
	volatile unsigned int Sta;
	do{
		DelayMs(aCnt);
		Sta = nor_get_busy_state(dFlashLinkageExpChipX);
	}while(Sta);
}

unsigned short  TConfig::Crc16Data(unsigned char * apData,unsigned int aLen)
{
	return 1;
}

unsigned int TConfig::CheckCrc16(unsigned char * apData,unsigned int aLen)
{
	return 1;
}

unsigned int TConfig::AsiiToData(unsigned char * apResult, char * apData,unsigned int aLen)
{
	unsigned char value = 0;
	unsigned int i;
	
	if(aLen > 3)return 1;

	for(i=0;i<aLen;i++){
		if((apData[i]>'9')||(apData[i]<'0')){
			return 1;
		}else{
			value = value * 10;
			value += apData[i] - '0';
		}
	}
	*apResult = value;
	return 0;
}

unsigned int TConfig::AsiiToData(unsigned short * apResult, char* apData,unsigned int aLen)
{
	unsigned short value = 0;
	unsigned int i;
	
	if(aLen > 5)return 1;
	
	for(i=0;i<aLen;i++){
		if((apData[i]>'9')||(apData[i]<'0')){
			return 1;
		}else{
			value = value * 10;
			value += apData[i] - '0';
		}
	}
	*apResult = value;
	return 0;
}

unsigned int TConfig::AsiiToData(unsigned int * apResult, char * apData,unsigned int aLen)
{
	unsigned int value = 0;
	unsigned int i;
	
	if(aLen > 10)return 1;
	
	for(i=0;i<aLen;i++){
		if((apData[i]>'9')||(apData[i]<'0')){
			return 1;
		}else{
			value = value * 10;
			value += apData[i] - '0';
		}
	}
	*apResult = value;
	return 0;
}

unsigned int TConfig::AsiiToData(__UINT64_TYPE__ * apResult, char* apData,unsigned int aLen)
{
	__UINT64_TYPE__ value = 0;
	unsigned int i;
	
	if(aLen > 20)return 1;
	
	for(i=0;i<aLen;i++){
		if((apData[i]>'9')||(apData[i]<'0')){
			return 1;
		}else{
			value = value * 10;
			value += apData[i] - '0';
		}
	}
	*apResult = value;
	return 0;
}

void TConfig::GetDesc16(char * apSetData, char * apGetData)
{
	unsigned int i;
	
	for(i=0;i<16;i++){
		apSetData[i] = apGetData[i];
		if(NULL == apGetData[i]){
			return;
		}
	}
}

void TConfig::GetDesc16(char * apSetData, unsigned char * apGetData)
{
	unsigned int i;
	
	for(i=0;i<16;i++){
		apSetData[i] = apGetData[i];
		if(NULL == apGetData[i]){
			return;
		}
	}
}

void TConfig::GetDesc32(char * apSetData, char * apGetData)
{
	unsigned int i;
	
	for(i=0;i<32;i++){
		apSetData[i] = apGetData[i];
		if(NULL == apGetData[i]){
			return;
		}
	}
}

void TConfig::GetDesc32(char * apSetData, unsigned char * apGetData)
{
	unsigned int i;
	
	for(i=0;i<32;i++){
		apSetData[i] = apGetData[i];
		if(NULL == apGetData[i]){
			return;
		}
	}
}

unsigned int TConfig::GetUsbState(void)
{
	return 1;
}

int TConfig::ReadFile(char* apName)
{
	int x;
	fileSize = load_file_to_sdramcache((const TCHAR*) apName, pSectorTmpAddr, &x);
	return x;
}

void TConfig::GetFileLine()
{
	unsigned char * pDataAddr;
	unsigned char * pGetDataAddr;
	unsigned int i,x,a;
	unsigned int pGet;
	unsigned int getLen;
	unsigned int count;
	
	pDataAddr = (unsigned char *)pSectorTmpAddr;
	pGetDataAddr = (unsigned char *)SDRAM_BUF_DATA_ADDRESS;
	count = 0;
	pGet = 0;
	a = 0;
	
	if(fileSize){
		for(i=0;i<fileSize;i++){
			if((0x0D == pDataAddr[i])&&(0x0A == pDataAddr[i+1])){
				for(x = pGet; x<i; x++){
					pGetDataAddr[a] = pDataAddr[x];
					a++;
				}
				pGetDataAddr[a] = 0;//add string ending NULL
				pGetDataAddr += CFG_FILE_LINE_BUF_SIZE;
				*((unsigned int *)(pGetDataAddr - 4)) = a;//last 4byte is valid data length
				pGet = i+2;
				a = 0;
				count++;
			}
		}
	}else{
		//---------------file error------------------
		fileAnalysisErr = 1;
	}
}

void TConfig::GetFileLine(unsigned int * apItemCount)
{
	unsigned char * pDataAddr;
	unsigned char * pGetDataAddr;
	unsigned int i,x,a;
	unsigned int pGet;
	unsigned int getLen;
	unsigned int count;
	
	pDataAddr = (unsigned char *)pSectorTmpAddr;
	pGetDataAddr = (unsigned char *)SDRAM_BUF_DATA_ADDRESS;
	count = 0;
	pGet = 0;
	a = 0;
	
	if(fileSize){
		for(i=0;i<fileSize;i++){
			if((0x0D == pDataAddr[i])&&(0x0A == pDataAddr[i+1])){
				for(x = pGet; x<i; x++){
					pGetDataAddr[a] = pDataAddr[x];
					a++;
				}
				pGetDataAddr[a] = 0;//add string ending NULL
				pGetDataAddr += CFG_FILE_LINE_BUF_SIZE;
				*((unsigned int *)(pGetDataAddr - 4)) = a;//last 4byte is valid data length
				pGet = i+2;
				a = 0;
				count++;
			}
		}
		if(count)(*apItemCount) = (count-1);
	}else{
		//---------------file error------------------
	}
}

void TConfig::GetCtlCfgPrm()
{
	char * pData;
	unsigned int ord = 0,i = 0,sP = 0,a;
	unsigned int len = 0;
	
	pData = (char *)(SDRAM_BUF_DATA_ADDRESS + CFG_FILE_LINE_BUF_SIZE);
	
	for(i=0;i<CFG_FILE_LINE_BUF_SIZE;i++){
		len++;
		if(',' == pData[i]){
			if(len <= 1)dataAnalysisErr = 1;
			else len -=1;
			
			if(0 == ord){
				dataAnalysisErr = AsiiToData(&ctlPrm.cfg.loopCount,&pData[sP],len);
				if(ctlPrm.cfg.loopCount > 32)dataAnalysisErr = 1;
				len = 0;
				sP = i+1;
			}else
			if(1 == ord){
				dataAnalysisErr = AsiiToData(&ctlPrm.cfg.busPanelCount,&pData[sP],len);
				if(ctlPrm.cfg.busPanelCount > BusCountMax)dataAnalysisErr = 1;
				len = 0;
				sP = i+1;
			}else
			if(2 == ord){
				dataAnalysisErr = AsiiToData(&ctlPrm.cfg.dirPanelCount,&pData[sP],len);
				if(ctlPrm.cfg.dirPanelCount > DirCountMax)dataAnalysisErr = 1;
				len = 0;
				sP = i+1;
			}else
			if(3 == ord){
				dataAnalysisErr = AsiiToData(&ctlPrm.cfg.areaCount,&pData[sP],len);
				if(ctlPrm.cfg.areaCount > AreaCountMax)dataAnalysisErr = 1;
				len = 0;
				sP = i+1;
			}else
			if(4 == ord){
				dataAnalysisErr = AsiiToData(&ctlPrm.cfg.linkCount,&pData[sP],len);
				if(ctlPrm.cfg.linkCount > LinkCountMax)dataAnalysisErr = 1;
				len = 0;
				sP = i+1;
			}else
			if(5 == ord){
				dataAnalysisErr = AsiiToData(&ctlPrm.cfg.loopCfgTab,&pData[sP],len);
				len = 0;
				sP = i+1;
			}else
			if(6 == ord){
				dataAnalysisErr = AsiiToData(&ctlPrm.cfg.busCfgTab,&pData[sP],len);
				len = 0;
				sP = i+1;
			}else
			if(7 == ord){
				dataAnalysisErr = AsiiToData(&ctlPrm.cfg.dirCfgTab,&pData[sP],len);
				len = 0;
				sP = i+1;
				if((*((unsigned int *)(pData + CFG_FILE_LINE_BUF_SIZE - 4))) < sP)dataAnalysisErr = 1;
				for(a=0;a<32;a++)loopPrm.cfg.desc[a] = 0;
				GetDesc32(ctlPrm.cfg.desc,&pData[sP]);
			}
			ord++;
			if(dataAnalysisErr)return;
		}else
		if(NULL == pData[i]){
			if(ord < 7)dataAnalysisErr = 1;
			return;
		}
	}
}

void TConfig::GetLoopCfgDevPrm(unsigned int aLineNum)
{
	char * pData;
	unsigned int ord = 0,i = 0,sP = 0,a;
	unsigned int len = 0;
	
	if(!aLineNum)return;
	
	pData = (char *)(SDRAM_BUF_DATA_ADDRESS + (aLineNum * CFG_FILE_LINE_BUF_SIZE));
	
	for(i=0;i<CFG_FILE_LINE_BUF_SIZE;i++){
		len++;
		if(',' == pData[i]){
			if(len <= 1)dataAnalysisErr = 1;
			else len -=1;
			
			if(0 == ord){
				dataAnalysisErr = AsiiToData(&loopPrm.cfg.id,&pData[sP],len);
				if((loopPrm.cfg.id < 1)||(loopPrm.cfg.id > LoopCountMax))dataAnalysisErr = 1;
				len = 0;
				sP = i+1;
			}else
			if(1 == ord){
				dataAnalysisErr = AsiiToData(&loopPrm.cfg.devId,&pData[sP],len);
				if((loopPrm.cfg.devId < 1)||(loopPrm.cfg.id > DevInLoopCountMax))dataAnalysisErr = 1;
				len = 0;
				sP = i+1;
			}else
			if(2 == ord){
				dataAnalysisErr = AsiiToData(&loopPrm.cfg.devType,&pData[sP],len);
				len = 0;
				sP = i+1;
			}else
			if(3 == ord){
				dataAnalysisErr = AsiiToData(&loopPrm.cfg.assignType,&pData[sP],len);
				len = 0;
				sP = i+1;
			}else
			if(4 == ord){
				dataAnalysisErr = AsiiToData(&loopPrm.cfg.devProperty,&pData[sP],len);
				len = 0;
				sP = i+1;
			}else
			if(5 == ord){
				dataAnalysisErr = AsiiToData(&loopPrm.cfg.areaId,&pData[sP],len);
				len = 0;
				sP = i+1;
			}else
			if(6 == ord){
				dataAnalysisErr = AsiiToData(&loopPrm.cfg.delayTime,&pData[sP],len);
				len = 0;
				sP = i+1;
			}else
			if(7 == ord){
				dataAnalysisErr = AsiiToData(&loopPrm.cfg.devNum,&pData[sP],len);
				sP = i+1;
				if((*((unsigned int *)(pData + CFG_FILE_LINE_BUF_SIZE - 4))) < sP)dataAnalysisErr = 1;
				for(a=0;a<32;a++)loopPrm.cfg.desc[a] = 0;
				GetDesc32(loopPrm.cfg.desc,&pData[sP]);
				loopCfgDevTab[loopPrm.cfg.devId - 1] = 1;
			}
			ord++;
			if(dataAnalysisErr)return;
		}else
		if(NULL == pData[i]){
			if(ord < 7)dataAnalysisErr = 1;
			return;
		}
	}
}

void TConfig::GetLoopCfgDispPrm(unsigned int aLineNum)
{
	char * pData;
	unsigned int ord = 0,i = 0,sP = 0,a,b;
	unsigned int len = 0;
	unsigned int ser_char;
	
	if(!aLineNum)return;
	
	pData = (char *)(SDRAM_BUF_DATA_ADDRESS + (aLineNum * CFG_FILE_LINE_BUF_SIZE));
	
	for(i=0;i<CFG_FILE_LINE_BUF_SIZE;i++){
		len++;
		if(',' == pData[i]){
			if(len <= 1)dataAnalysisErr = 1;
			else len -=1;
			
			if(0 == ord){
				dataAnalysisErr = AsiiToData(&loopDispPrm.cfg.id,&pData[sP],len);
				if((loopDispPrm.cfg.id < 1)||(loopDispPrm.cfg.id > LoopCountMax))dataAnalysisErr = 1;
				len = 0;
				sP = i+1;
			}else
			if(1 == ord){
				dataAnalysisErr = AsiiToData(&loopDispPrm.cfg.devId,&pData[sP],len);
				if((loopDispPrm.cfg.devId < 1)||(loopDispPrm.cfg.id > DevInLoopCountMax))dataAnalysisErr = 1;
				len = 0;
				sP = i+1;
			}else
			if(2 == ord){
				dataAnalysisErr = AsiiToData(&loopDispPrm.cfg.devType,&pData[sP],len);
				len = 0;
				sP = i+1;
			}else
			if(3 == ord){
				dataAnalysisErr = AsiiToData(&loopDispPrm.cfg.dispMode,&pData[sP],len);
				len = 0;
				sP = i+1;
			}else
			if(4 == ord){
				for(a=0;a<3;a++){
					loopDispPrm.cfg.areaId[a] = 0;
					loopDispPrm.cfg.devNum[a] = 0;
				}
				a=0;
				
				if((':' != pData[sP+a])||(':' != pData[sP+a+5])||(':' != pData[sP+a+10])||(15 != len)){
					dataAnalysisErr = 1;
				}else{
					dataAnalysisErr = AsiiToData(&loopDispPrm.cfg.areaId[0],&pData[sP+a+1],4);
					dataAnalysisErr = AsiiToData(&loopDispPrm.cfg.areaId[1],&pData[sP+a+1+5],4);
					dataAnalysisErr = AsiiToData(&loopDispPrm.cfg.areaId[2],&pData[sP+a+1+10],4);
					
					sP+=(15+1);
					if((*((unsigned int *)(pData + CFG_FILE_LINE_BUF_SIZE - 4))) < sP){
						dataAnalysisErr = 1;
					}else{
						len = (*((unsigned int *)(pData + CFG_FILE_LINE_BUF_SIZE - 4))) - sP;
						if((':' != pData[sP+0])||(':' != pData[sP+9])||(18 != len)){
							dataAnalysisErr = 1;
						}else{
							sP++;
							for(a=0;a<8;a++){
								if(!(((pData[sP+a] >= '0')&&(pData[sP+a] <= '9'))||(pData[sP+a] == '*'))){
									dataAnalysisErr = 1;
								}else{
									if(pData[sP+a] == '*'){
										ser_char = 0x0A;
									}else{
										ser_char = pData[sP+a] - '0';
									}
									loopDispPrm.cfg.devNum[0] |= (ser_char<<((8 - a -1)*4));
								}
							}
							sP+=9;
							for(a=0;a<8;a++){
								if(!(((pData[sP+a] >= '0')&&(pData[sP+a] <= '9'))||(pData[sP+a] == '*'))){
									dataAnalysisErr = 1;
								}else{
									if(pData[sP+a] == '*'){
										ser_char = 0x0A;
									}else{
										ser_char = pData[sP+a] - '0';
									}
									loopDispPrm.cfg.devNum[1] |= (ser_char<<((8 - a -1)*4));
								}
							}
							loopDispPrm.cfg.ord++;
						}
					}
				}
				len = 0;
				sP = i+1;
			}
			ord++;
			if(dataAnalysisErr)return;
		}else
		if(NULL == pData[i]){
			if(ord < 4)dataAnalysisErr = 1;
			return;
		}
	}
}

void TConfig::GetBusCfgKeyPrm(unsigned int aLineNum)
{
	char * pData;
	unsigned int ord = 0,i = 0,sP = 0,a;
	unsigned int len = 0;
	unsigned int ser_char;
	
	if(!aLineNum)return;
	
	pData = (char *)(SDRAM_BUF_DATA_ADDRESS + (aLineNum * CFG_FILE_LINE_BUF_SIZE));
	
	for(i=0;i<CFG_FILE_LINE_BUF_SIZE;i++){
		len++;
		if(',' == pData[i]){
			if(len <= 1)dataAnalysisErr = 1;
			else len -=1;
			
			if(0 == ord){
				dataAnalysisErr = AsiiToData(&busPrm.cfg.id,&pData[sP],len);
				if((busPrm.cfg.id < 1)||(busPrm.cfg.id > BusCountMax))dataAnalysisErr = 1;
				len = 0;
				sP = i+1;
			}else
			if(1 == ord){
				dataAnalysisErr = AsiiToData(&busPrm.cfg.keyId,&pData[sP],len);
				if((busPrm.cfg.keyId < 1)||(busPrm.cfg.keyId > KeyInBusCountMax))dataAnalysisErr = 1;
				len = 0;
				sP = i+1;
			}else
			if(2 == ord){
				dataAnalysisErr = AsiiToData(&busPrm.cfg.ctlMode,&pData[sP],len);
				if(busPrm.cfg.ctlMode > 2)dataAnalysisErr = 1;
				len = 0;
				sP = i+1;
			}else
			if(3 == ord){
				if((!len)||(len > 8))dataAnalysisErr = 1;
				else{
					busPrm.cfg.serNum = 0;
					for(a=0;a<len;a++){
						if(!(((pData[sP+a] >= '0')&&(pData[sP+a] <= '9'))||(pData[sP+a] == '*'))){
							dataAnalysisErr = 1;
						}else{
							if(pData[sP+a] == '*'){
								ser_char = 0x0A;
							}else{
								ser_char = pData[sP+a] - '0';
							}
							busPrm.cfg.serNum |= (ser_char<<((len - a -1)*4));
						}
					}
				}
				len = 0;
				sP = i+1;
			}else
			if(4 == ord){
				dataAnalysisErr = AsiiToData(&busPrm.cfg.areaId,&pData[sP],len);
				len = 0;
				sP = i+1;
			}else
			if(5 == ord){
				dataAnalysisErr = AsiiToData(&busPrm.cfg.devType,&pData[sP],len);
				sP = i+1;
				if((*((unsigned int *)(CFG_FILE_LINE_BUF_SIZE - 4))) < sP)dataAnalysisErr = 1;
				for(a=0;a<32;a++)busPrm.cfg.desc[a] = 0;
				GetDesc32(busPrm.cfg.desc,&pData[sP]);
				busCfgKeyTab[busPrm.cfg.keyId - 1] = 1;
			}
			ord++;
			if(dataAnalysisErr)return;
		}else
		if(NULL == pData[i]){
			if(ord < 5)dataAnalysisErr = 1;
			return;
		}
	}
}

void TConfig::GetDirCfgKeyPrm(unsigned int aLineNum)
{
	char * pData;
	unsigned int ord = 0,i = 0,sP = 0,a;
	unsigned int len = 0;
	
	if(!aLineNum)return;
	
	pData = (char *)(SDRAM_BUF_DATA_ADDRESS + (aLineNum * CFG_FILE_LINE_BUF_SIZE));
	
	for(i=0;i<CFG_FILE_LINE_BUF_SIZE;i++){
		len++;
		if(',' == pData[i]){
			if(len <= 1)dataAnalysisErr = 1;
			else len -=1;
			
			if(0 == ord){
				dataAnalysisErr = AsiiToData(&dirPrm.cfg.id,&pData[sP],len);
				if((dirPrm.cfg.id < 1)||(dirPrm.cfg.id > DirCountMax))dataAnalysisErr = 1;
				len = 0;
				sP = i+1;
			}else
			if(1 == ord){
				dataAnalysisErr = AsiiToData(&dirPrm.cfg.keyId,&pData[sP],len);
				if((dirPrm.cfg.keyId < 1)||(dirPrm.cfg.keyId > KeyInDirCountMax))dataAnalysisErr = 1;
				len = 0;
				sP = i+1;
			}else
			if(2 == ord){
				dataAnalysisErr = AsiiToData(&dirPrm.cfg.isAble,&pData[sP],len);
				len = 0;
				sP = i+1;
			}else
			if(3 == ord){
				dataAnalysisErr = AsiiToData(&dirPrm.cfg.errDetFlag,&pData[sP],len);
				len = 0;
				sP = i+1;
			}else
			if(4 == ord){
				dataAnalysisErr = AsiiToData(&dirPrm.cfg.outProp,&pData[sP],len);
				len = 0;
				sP = i+1;
			}else
			if(5 == ord){
				dataAnalysisErr = AsiiToData(&dirPrm.cfg.ctlDevType,&pData[sP],len);
				len = 0;
				sP = i+1;
			}else
			if(6 == ord){
				dataAnalysisErr = AsiiToData(&dirPrm.cfg.areaId,&pData[sP],len);
				len = 0;
				sP = i+1;
			}else
			if(7 == ord){
				dataAnalysisErr = AsiiToData(&dirPrm.cfg.serNum,&pData[sP],len);
				sP = i+1;
				if((*((unsigned int *)(CFG_FILE_LINE_BUF_SIZE - 4))) < sP)dataAnalysisErr = 1;
				for(a=0;a<32;a++)dirPrm.cfg.desc[a] = 0;
				GetDesc32(dirPrm.cfg.desc,&pData[sP]);
				dirCfgKeyTab[dirPrm.cfg.keyId - 1] = 1;
			}
			ord++;
			if(dataAnalysisErr)return;
		}else
		if(NULL == pData[i]){
			if(ord < 7)dataAnalysisErr = 1;
			return;
		}
	}
}

void TConfig::GetAreaCfgPrm(unsigned int aLineNum)
{
	char * pData;
	unsigned int ord = 0,i = 0,sP = 0,a;
	unsigned int len = 0;
	
	if(!aLineNum)return;
	
	pData = (char *)(SDRAM_BUF_DATA_ADDRESS + (aLineNum * CFG_FILE_LINE_BUF_SIZE));
	
	for(i=0;i<CFG_FILE_LINE_BUF_SIZE;i++){
		len++;
		if(',' == pData[i]){
			if(len <= 1)dataAnalysisErr = 1;
			else len -=1;
			
			if(0 == ord){
				dataAnalysisErr = AsiiToData(&areaPrm.cfg.id,&pData[sP],len);
				if((areaPrm.cfg.id < 1)||(areaPrm.cfg.id > AreaCountMax))dataAnalysisErr = 1;
				len = 0;
				sP = i+1;
			}else
			if(1 == ord){
				dataAnalysisErr = AsiiToData(&areaPrm.cfg.serNum,&pData[sP],len);
				len = 0;
				sP = i+1;
			}else
			if(2 == ord){
				if(len > 16)dataAnalysisErr = 1;
				for(a=0;a<16;a++)areaPrm.cfg.profile[a] = 0;
				for(a=0;a<len;a++)areaPrm.cfg.profile[a] = pData[sP+a];
				sP = i+1;
				if((*((unsigned int *)(pData + CFG_FILE_LINE_BUF_SIZE - 4))) < sP)dataAnalysisErr = 1;
				for(a=0;a<32;a++)areaPrm.cfg.desc[a] = 0;
				GetDesc32(areaPrm.cfg.desc,&pData[sP]);
				areaCfgTab[areaPrm.cfg.id - 1] = 1;
			}
			ord++;
			if(dataAnalysisErr)return;
		}else
		if(NULL == pData[i]){
			if(ord < 2)dataAnalysisErr = 1;
			return;
		}
	}
}

void TConfig::GetLinkCfgPrm(unsigned int aLineNum)
{
	char * pData;
	unsigned int ord = 0,i = 0,sP = 0,a,b;
	unsigned int len = 0;
	unsigned int formOrd = 0, index = 0;
	
	if(!aLineNum)return;
	
	pData = (char *)(SDRAM_BUF_DATA_ADDRESS + (aLineNum * CFG_FILE_LINE_BUF_SIZE));
	
	linkPrm.cfg.inCount = 0;
	linkPrm.cfg.outCount = 0;
	linkPrm.cfg.inTab = 0;
	linkPrm.cfg.outTab = 0;
	
	for(i=0;i<CFG_FILE_LINE_BUF_SIZE;i++){
		len++;
		if(',' == pData[i]){
			if(len <= 1)dataAnalysisErr = 1;
			else len -=1;
			
			if(0 == ord){
				dataAnalysisErr = AsiiToData(&linkPrm.cfg.id,&pData[sP],len);
				if((linkPrm.cfg.id < 1)||(linkPrm.cfg.id > LinkCountMax))dataAnalysisErr = 1;
				len = 0;
				sP = i+1;
			}else
			if(1 == ord){
				dataAnalysisErr = AsiiToData(&linkPrm.cfg.isStop,&pData[sP],len);
				if(linkPrm.cfg.isStop > 1)dataAnalysisErr = 1;
				len = 0;
				sP = i+1;
			}else
			if(2 == ord){
				dataAnalysisErr = AsiiToData(&linkPrm.cfg.isMask,&pData[sP],len);
				if(linkPrm.cfg.isMask > 1)dataAnalysisErr = 1;
				len = 0;
				sP = i+1;
			}else
			if(3 == ord){
				dataAnalysisErr = AsiiToData(&linkPrm.cfg.isForceAutoExec,&pData[sP],len);
				if(linkPrm.cfg.isForceAutoExec > 1)dataAnalysisErr = 1;
				len = 0;
				sP = i+1;
			}else
			if(4 == ord){
				dataAnalysisErr = AsiiToData(&linkPrm.cfg.inTab,&pData[sP],len);
				for(a=0;a<16;a++){
					if(linkPrm.cfg.inTab & (1<<a))linkPrm.cfg.inCount++;
				}
				len = 0;
				sP = i+1;
			}else
			if(5 == ord){
				dataAnalysisErr = AsiiToData(&linkPrm.cfg.outTab,&pData[sP],len);
				for(a=0;a<16;a++){
					if(linkPrm.cfg.outTab & (1<<a))linkPrm.cfg.outCount++;
				}
				len = 0;
				sP = i+1;
			}else
			if(6 == ord){
				if((len < 9)||(len > 272)){
					dataAnalysisErr = 1;
				}else{
					for(a=0;a<16;a++){
						for(b=0;b<16;b++){
							linkPrm.cfg.eqn.prm.inEqn[a][b] = 0;
						}
					}
					formOrd = 0;
					for(a=0;a<len;a+=17){
						if(':' != pData[sP+a]){
							dataAnalysisErr = 1;
						}else{
							for(index = formOrd;index < 16;index++){
								if(linkPrm.cfg.inTab & (1<<index)){
									formOrd++;
									break;
								}
							}
							if(index > 15)index = 15;
							for(b=0;b<16;b++){
								if((a+1+b) >= len)break;
								linkPrm.cfg.eqn.prm.inEqn[index][b] = pData[sP+a+1+b];
							}
							linkPrm.cfg.eqn.prm.inEqn[index][8] = linkPrm.cfg.eqn.prm.inEqn[index][9];
							linkPrm.cfg.eqn.prm.inEqn[index][9] = linkPrm.cfg.eqn.prm.inEqn[index][10];
							linkPrm.cfg.eqn.prm.inEqn[index][10] = linkPrm.cfg.eqn.prm.inEqn[index][11];
							linkPrm.cfg.eqn.prm.inEqn[index][11] = linkPrm.cfg.eqn.prm.inEqn[index][13];
							linkPrm.cfg.eqn.prm.inEqn[index][12] = linkPrm.cfg.eqn.prm.inEqn[index][14];
							linkPrm.cfg.eqn.prm.inEqn[index][13] = linkPrm.cfg.eqn.prm.inEqn[index][15];
							linkPrm.cfg.eqn.prm.inEqn[index][14] = 0;
							linkPrm.cfg.eqn.prm.inEqn[index][15] = 0;
							
							//linkPrm.cfg.inTab |= (1<<linkPrm.cfg.inCount);
							//linkPrm.cfg.inCount++;
						}
					}
				}
				
				sP = i+1;
				if((*((unsigned int *)(pData + CFG_FILE_LINE_BUF_SIZE - 4))) < sP){
					dataAnalysisErr = 1;
				}else{
					for(a=0;a<16;a++){
						for(b=0;b<16;b++){
							linkPrm.cfg.eqn.prm.outEqn[a][b] = 0;
						}
					}
					a=0;
					formOrd = 0;
					while((':' == pData[sP+a])||(a>=272)||(sP+a > (*((unsigned int *)(pData + CFG_FILE_LINE_BUF_SIZE - 4))))){
						for(index = formOrd;index < 16;index++){
							if(linkPrm.cfg.outTab & (1<<index)){
								formOrd++;
								break;
							}
						}
						if(index > 15)index = 15;
						for(b=0;b<16;b++){
							linkPrm.cfg.eqn.prm.outEqn[index][b] = pData[sP+a+1+b];
						}
						linkPrm.cfg.eqn.prm.outEqn[index][8] = linkPrm.cfg.eqn.prm.outEqn[index][9];
						linkPrm.cfg.eqn.prm.outEqn[index][9] = linkPrm.cfg.eqn.prm.outEqn[index][10];
						linkPrm.cfg.eqn.prm.outEqn[index][10] = linkPrm.cfg.eqn.prm.outEqn[index][11];
						linkPrm.cfg.eqn.prm.outEqn[index][11] = linkPrm.cfg.eqn.prm.outEqn[index][13];
						linkPrm.cfg.eqn.prm.outEqn[index][12] = linkPrm.cfg.eqn.prm.outEqn[index][14];
						linkPrm.cfg.eqn.prm.outEqn[index][13] = linkPrm.cfg.eqn.prm.outEqn[index][15];
						linkPrm.cfg.eqn.prm.outEqn[index][14] = 0;
						linkPrm.cfg.eqn.prm.outEqn[index][15] = 0;
						
						//linkPrm.cfg.outTab |= (1<<linkPrm.cfg.outCount);
						//linkPrm.cfg.outCount++;
						
						a+=17;
					}
					linkCfgTab[linkPrm.cfg.id - 1] = 1;
				}
			}
			ord++;
			if(dataAnalysisErr)return;
		}else
		if(NULL == pData[i]){
			if(ord < 6)dataAnalysisErr = 1;
			return;
		}
	}
}

//--------------------------write-----------------------------------------------------------------------
void TConfig::WriteCtlPrmToSdram(TConfigCtlData * pCtlPrm)
{
	unsigned int aLoadAddr;
	unsigned int i;
	
	aLoadAddr = CtlCfgBufAddr;
	for(i=0; i<dCtlCFG_BufCount/4; i++){
		*(volatile unsigned int *)(aLoadAddr + (i*4)) = pCtlPrm->D32[i];
	}
}

unsigned int TConfig::WriteLoopDevPrmToSdram(TConfigLoopData * pLoopPrm)
{
	unsigned int aLoadAddr;
	unsigned int x,y,i;
	
	if((pLoopPrm->cfg.id < 1)||(pLoopPrm->cfg.id > LoopCountMax))return 1;
	if((pLoopPrm->cfg.devId < 1)||(pLoopPrm->cfg.devId > DevInLoopCountMax))return 1;
	
	y = pLoopPrm->cfg.id - 1;
	x = pLoopPrm->cfg.devId - 1;
	
	aLoadAddr = LoopDevCfgBufAddr + (((y * DevInLoopCountMax) + x) * CFG_LOAD_BUF_SIZE);
	for(i=0; i<dLoopDevCFG_BufCount/4; i++){
		*(volatile unsigned int *)(aLoadAddr + (i*4)) = pLoopPrm->D32[i];
	}
	
	return 0;
}

unsigned int TConfig::WriteLoopDevDispPrmToSdram(TConfigLoopDispData * pLoopDispPrm)
{
	unsigned int aLoadAddr;
	unsigned int x,y,i;
	
	if((pLoopDispPrm->cfg.ord < 1)||(pLoopDispPrm->cfg.ord > 16))return 1;
	if((pLoopDispPrm->cfg.id < 1)||(pLoopDispPrm->cfg.id > LoopCountMax))return 1;
	if((pLoopDispPrm->cfg.devId < 1)||(pLoopDispPrm->cfg.devId > DevInLoopCountMax))return 1;
	
	y = pLoopDispPrm->cfg.id - 1;
	x = pLoopDispPrm->cfg.ord - 1;
	
	aLoadAddr = LoopDispCfgBufAddr + (((y * DispInLoopCountMax) + x) * CFG_LOAD_BUF_SIZE);
	for(i=0; i<dLoopDispCFG_BufCount/4; i++){
		*(volatile unsigned int *)(aLoadAddr + (i*4)) = pLoopDispPrm->D32[i];
	}
	
	return 0;
}

void TConfig::ClearLoopCfgDevTabInSdram(unsigned int aId)
{
	unsigned int aLoadAddr;
	unsigned int x,i;
	
	if((!aId)||(aId > LoopCountMax))return;
	x = aId - 1;
	aLoadAddr = LoopDevCfgTabAddr + (x * CFG_TAB_BUF_SIZE);
	for(i=0; i<DevInLoopCountMax; i++){
		*(volatile unsigned char *)(aLoadAddr + i) = 0;
	}
}

void TConfig::WriteLoopCfgDevTabToSdram(unsigned int aId, unsigned char * pLoopCfgDevTab)
{
	unsigned int aLoadAddr;
	unsigned int x,i;
	
	if((!aId)||(aId > LoopCountMax))return;
	x = aId - 1;
	aLoadAddr = LoopDevCfgTabAddr + (x * CFG_TAB_BUF_SIZE);
	for(i=0; i<DevInLoopCountMax; i++){
		*(volatile unsigned char *)(aLoadAddr + i) = *(pLoopCfgDevTab + i);
	}
}

void TConfig::WriteLoopCfgOneDevInTabToSdram(unsigned int aId, unsigned int aDevId)
{
	unsigned int aLoadAddr;
	unsigned int x,y;
	
	if((0 == aId)||(aId > LoopCountMax))return;
	if((0 == aDevId)||(aDevId > DevInLoopCountMax))return;
	x = aId - 1;
	y = aDevId - 1;
	aLoadAddr = LoopDevCfgTabAddr + (x * CFG_TAB_BUF_SIZE);
	*(volatile unsigned char *)(aLoadAddr + y) = 1;
}

unsigned int TConfig::WriteBusKeyPrmToSdram(TConfigBusPanelData * pBusPrm)
{
	unsigned int aLoadAddr;
	unsigned int x,y,i;
	
	if((pBusPrm->cfg.id < 1)||(pBusPrm->cfg.id > BusCountMax))return 1;
	if((pBusPrm->cfg.keyId < 1)||(pBusPrm->cfg.keyId > KeyInBusCountMax))return 1;
	
	y = pBusPrm->cfg.id - 1;
	x = pBusPrm->cfg.keyId - 1;
	
	aLoadAddr = BusKeyCfgBufAddr + (((y * KeyInBusCountMax) + x) * CFG_LOAD_BUF_SIZE);
	for(i=0; i<dBusKeyCFG_BufCount/4; i++){
		*(volatile unsigned int *)(aLoadAddr + (i*4)) = pBusPrm->D32[i];
	}
	
	return 0;
}

void TConfig::ClearBusCfgKeyTabToSdram(unsigned int aId)
{
	unsigned int aLoadAddr;
	unsigned int x,i;
	
	if((!aId)||(aId > BusCountMax))return;
	x = aId - 1;
	aLoadAddr = BusKeyCfgTabAddr + (x * CFG_TAB_BUF_SIZE);
	for(i=0; i<KeyInBusCountMax; i++){
		*(volatile unsigned char *)(aLoadAddr + i) = 0;
	}
}

void TConfig::WriteBusCfgKeyTabToSdram(unsigned int aId, unsigned char * pBusCfgKeyTab)
{
	unsigned int aLoadAddr;
	unsigned int x,i;
	
	if((!aId)||(aId > BusCountMax))return;
	x = aId - 1;
	aLoadAddr = BusKeyCfgTabAddr + (x * CFG_TAB_BUF_SIZE);
	for(i=0; i<KeyInBusCountMax; i++){
		*(volatile unsigned char *)(aLoadAddr + i) = *(pBusCfgKeyTab + i);
	}
}

void TConfig::WriteBusCfgOneKeyInTabToSdram(unsigned int aId, unsigned int aKeyId)
{
	unsigned int aLoadAddr;
	unsigned int x,y;
	
	if((!aId)||(aId > LoopCountMax))return;
	if((!aKeyId)||(aKeyId > KeyInBusCountMax))return;
	x = aId - 1;
	y = aKeyId - 1;
	aLoadAddr = BusKeyCfgTabAddr + (x * CFG_TAB_BUF_SIZE);
	*(volatile unsigned char *)(aLoadAddr + y) = 1;
}

unsigned int TConfig::WriteDirKeyPrmToSdram(TConfigDirPanelData * pDirPrm)
{
	unsigned int aLoadAddr;
	unsigned int x,y,i;
	
	if((pDirPrm->cfg.id < 1)||(pDirPrm->cfg.id > DirCountMax))return 1;
	if((pDirPrm->cfg.keyId < 1)||(pDirPrm->cfg.keyId > KeyInDirCountMax))return 1;
	
	y = pDirPrm->cfg.id - 1;
	x = pDirPrm->cfg.keyId - 1;
	
	aLoadAddr = DirKeyCfgBufAddr + ((y * KeyInDirCountMax + x) * CFG_LOAD_BUF_SIZE);
	for(i=0; i<dDirKeyCFG_BufCount/4; i++){
		*(volatile unsigned int *)(aLoadAddr + (i*4)) = pDirPrm->D32[i];
	}
	
	return 0;
}
		
void TConfig::ClearDirCfgKeyTabToSdram(unsigned int aId)
{
	unsigned int aLoadAddr;
	unsigned int x,i;
	
	if((!aId)||(aId > DirCountMax))return;
	x = aId - 1;
	aLoadAddr = DirKeyCfgTabAddr + (x * CFG_TAB_BUF_SIZE);
	for(i=0; i<KeyInDirCountMax; i++){
		*(volatile unsigned char *)(aLoadAddr + i) = 0;
	}
}

void TConfig::WriteDirCfgKeyTabToSdram(unsigned int aId, unsigned char * pDirCfgKeyTab)
{
	unsigned int aLoadAddr;
	unsigned int x,i;
	
	if((!aId)||(aId > DirCountMax))return;
	x = aId - 1;
	aLoadAddr = DirKeyCfgTabAddr + (x * CFG_TAB_BUF_SIZE);
	for(i=0; i<KeyInDirCountMax; i++){
		*(volatile unsigned char *)(aLoadAddr + i) = *(pDirCfgKeyTab + i);
	}
}

void TConfig::WriteDirCfgOneKeyInTabToSdram(unsigned int aId, unsigned int aKeyId)
{
	unsigned int aLoadAddr;
	unsigned int x,y;
	
	if((!aId)||(aId > DirCountMax))return;
	if((!aKeyId)||(aKeyId > KeyInDirCountMax))return;
	x = aId - 1;
	y = aKeyId - 1;
	aLoadAddr = DirKeyCfgTabAddr + (x * CFG_TAB_BUF_SIZE);
	*(volatile unsigned char *)(aLoadAddr + y) = 1;
}

unsigned int TConfig::WriteAreaPrmToSdram(TConfigAreaData * pAreaPrm)
{
	unsigned int aLoadAddr;
	unsigned int x,i;
	
	if((pAreaPrm->cfg.id < 1)||(pAreaPrm->cfg.id > AreaCountMax))return 1;
	
	x = pAreaPrm->cfg.id - 1;
	
	aLoadAddr = AreaCfgBufAddr + (x * CFG_LOAD_BUF_SIZE);
	for(i=0; i<dAreaCFG_BufCount/4; i++){
		*(volatile unsigned int *)(aLoadAddr + (i*4)) = pAreaPrm->D32[i];
	}
	
	return 0;
}

void TConfig::WriteAreaCfgTabToSdram(unsigned char * pAreaCfgTab)
{
	unsigned int aLoadAddr;
	unsigned int i;
}

//____________________________________________________________________________________________________________________
void TConfig::ClearOneLinkCfgToSdram(unsigned int aId)
{
	unsigned int x;
	unsigned int aInx;
	unsigned int aLoadAddr;
	
	if((aId < 1)||(aId > LinkCountMax))return;
	aInx = aId - 1;
	
	aLoadAddr = dSdAddrLinkageExp + (aInx * 1024);
	
	for(x = 0;x<64;x+=4){
		*(volatile unsigned int *)(aLoadAddr + (x * 4)) = 0;
	}
}

unsigned int TConfig::WriteLinkCfgToSdram(TConfigLinkData * pLinkPrm)
{
	unsigned int a,x;
	unsigned int aInx;
	unsigned int aLoadAddr;
	
	if((0 == pLinkPrm->cfg.id)||(pLinkPrm->cfg.id > LinkCountMax))return 1;
	if(pLinkPrm->cfg.inCount > 16)return 1;
	if(pLinkPrm->cfg.outCount > 16)return 1;
	aInx = pLinkPrm->cfg.id - 1;
	
	aLoadAddr = dSdAddrLinkageExp + (aInx * 1024);
	*(volatile unsigned char *)(aLoadAddr + 0) = 'T';
	*(volatile unsigned char *)(aLoadAddr + 1) = 'R';
	*(volatile unsigned char *)(aLoadAddr + 2) = 'U';
	*(volatile unsigned char *)(aLoadAddr + 3) = 'E';
	
	if(pLinkPrm->cfg.isMask)(*(volatile unsigned char *)(aLoadAddr + 4)) = 'M';
	else (*(volatile unsigned char *)(aLoadAddr + 4)) = 0;
	if(pLinkPrm->cfg.isStop)(*(volatile unsigned char *)(aLoadAddr + 5)) = 'S';
	else *(volatile unsigned char *)(aLoadAddr + 5) = 0;
	if(pLinkPrm->cfg.isForceAutoExec)(*(volatile unsigned char *)(aLoadAddr + 6)) = 'F';
	else (*(volatile unsigned char *)(aLoadAddr + 6)) = 0;
	
	//------intput---Tab----
	for(x=0; x<16; x++){
		if(pLinkPrm->cfg.inTab & (1<<x)){
			for(a=0; a<4; a++)
				*(volatile unsigned int *)(aLoadAddr + 16 + (x*16) + (a*4)) = pLinkPrm->cfg.eqn.D32[(x*4) + a];
		}else{
			for(a=0; a<4; a++)
				*(volatile unsigned int *)(aLoadAddr + 16 + (x*16) + (a*4)) = 0;
		}
	}
	
	//------output---Tab----
	for(x=0; x<16; x++){
		if(pLinkPrm->cfg.outTab & (1<<x)){
			for(a=0; a<4; a++)
				*(volatile unsigned int *)(aLoadAddr + 16 + (16*16) + (x*16) + (a*4)) = pLinkPrm->cfg.eqn.D32[(16*4) + (x*4) + a];
		}else{
			for(a=0; a<4; a++)
				*(volatile unsigned int *)(aLoadAddr + 16 + (16*16) + (x*16) + (a*4)) = 0;
		}
	}
		
	return 0;
}

void TConfig::Erase4LinkIndex(unsigned int aInx)
{
	unsigned int fAddr4K;
	fAddr4K = dFlashAddrLinkageExp + ((aInx * 1024) & 0x00FFF000);
	WaitChipIdle(5);
	nor_erase_sector_4k(dFlashLinkageExpChipX,fAddr4K);
	WaitChipIdle(20);
}

void TConfig::Erase64LinkIndex(unsigned int aInx)
{
	unsigned int fAddr64K;
	fAddr64K = dFlashAddrLinkageExp + ((aInx * 1024) & 0x00FF0000);
	WaitChipIdle(5);
	nor_erase_sector_64k(dFlashLinkageExpChipX,fAddr64K);
	WaitChipIdle(100);
}

void TConfig::Write4LinkIndex(unsigned int aInx)
{
	unsigned int fAddr4K;
	unsigned int fAddr4Index;
	unsigned int sAddr4K;
	unsigned int SysLinkAddr;
	int i,x, ord;
	fAddr4K = dFlashAddrLinkageExp + ((aInx * 1024) & 0x00FF'F000);
	sAddr4K = dSdAddrLinkageExp + ((aInx * 1024) & 0x00FFF000);
	for(i=0; i<4; i++){
		
		fAddr4Index = fAddr4K + (i * 1024);
		SysLinkAddr = sAddr4K + (i * 1024);

		for(ord=0; ord<4; ord++){
			for(x=0; x<64; x++){
				nFlashPageData.D32[x] = *(volatile unsigned int *)(SysLinkAddr + (x * 4) );
			}
			WaitChipIdle(5);
			nor_write_page(dFlashLinkageExpChipX, fAddr4Index, nFlashPageData.D8);
			fAddr4Index+=256;
			SysLinkAddr+=256;
		}
	}
}

void TConfig::Write1LinkIndex(unsigned int aInx)
{
	unsigned int fAddr1Index;
	unsigned int sAddr;
	unsigned int SysLinkAddr;
	int i,x, ord;
	fAddr1Index = dFlashAddrLinkageExp + (aInx * 1024);
	sAddr = dSdAddrLinkageExp + (aInx * 1024);
	
	SysLinkAddr = sAddr;

	for(ord=0; ord<4; ord++){
		for(x=0; x<64; x++){
			nFlashPageData.D32[x] = *(volatile unsigned int *)(SysLinkAddr + (x * 4) );
		}
		WaitChipIdle(5);
		nor_write_page(dFlashLinkageExpChipX, fAddr1Index, nFlashPageData.D8);
		fAddr1Index+=256;
		SysLinkAddr+=256;
	}
}

void TConfig::Clear4Index(unsigned int aGroup)
{
	volatile unsigned  int aFlashState;
	unsigned int fAddr4K;
	unsigned int fAddr4Index;
	unsigned int sAddr4K;
	unsigned int SysLinkAddr;
	int i,x, ord;
	TDMix tCrc;
	unsigned int ToWriteInx = aGroup * 4;
	
	ItemList.ReadWriteData.Reset();
	
	fAddr4K = dFlashAddrLinkageExp + ((ToWriteInx * 1024) & 0x00FF'F000);
	for(i=0; i<4; i++){
		Crc32Reset();
		fAddr4Index = fAddr4K + (i * 1024);
		SysLinkAddr = sAddr4K + (i * 1024);
		for(ord=0; ord<4; ord++){
			for(x=0; x<64; x++){
				ItemList.ReadWriteData.D32[x] = *(volatile unsigned int *)(SysLinkAddr + (x * 4) );
				if(ord<3 or x<63)Crc32Feed(ItemList.ReadWriteData.D32[x]);
			}
			if(ord==3)ItemList.ReadWriteData.D32[63]=Crc32GetResult();
			do{
				DelayMs(2);
				aFlashState = nor_get_busy_state(dFlashLinkageExpChipX);
			}while(aFlashState);
			nor_write_page(dFlashLinkageExpChipX, fAddr4Index, ItemList.ReadWriteData.D8);
			fAddr4Index+=256;
			SysLinkAddr+=256;
		}
		do{
			DelayMs(2);
			aFlashState = nor_get_busy_state(dFlashLinkageExpChipX);
		}while(aFlashState);
		InterLink.CheckStorageCrc(ToWriteInx+1);
		do{
			DelayMs(2);
			aFlashState = nor_get_busy_state(dFlashLinkageExpChipX);
		}while(aFlashState);
		ToWriteInx++;
		LoopCheck1S();
	}
}

void TConfig::WriteAllLinkCfgToNorFlash()
{
	unsigned int x,a,SysLinkAddr,isExist;
	Tint Val;
	
	for(x = 0;x < LinkCountMax;x+=4){
		if((1 == linkCfgTab[x])||(1 == linkCfgTab[x+1])||(1 == linkCfgTab[x+2])||(1 == linkCfgTab[x+3])){
			LinkageProgram.Erase4Index(x);
			if(0 == linkCfgTab[x]){
				ClearOneLinkCfgToSdram(x+1);
			}
			if(0 == linkCfgTab[x+1]){
				ClearOneLinkCfgToSdram(x+2);
			}
			if(0 == linkCfgTab[x+2]){
				ClearOneLinkCfgToSdram(x+3);
			}
			if(0 == linkCfgTab[x+3]){
				ClearOneLinkCfgToSdram(x+4);
			}
			LinkageProgram.Write4Index(x);
		}else{
			isExist = 0;
			for(a=0;a<4;a++){
				SysLinkAddr = dSdAddrLinkageExp + ((x+a)*1024);
				Val.D32 = *(volatile unsigned int *)(SysLinkAddr);
				if((Val.D8[0] == 'T') && (Val.D8[1] == 'R') && (Val.D8[2] == 'U') && (Val.D8[3] == 'E')){
					ClearOneLinkCfgToSdram(x+a);
					isExist = 1;
				}
			}
			if(isExist){
				LinkageProgram.Erase4Index(x);
				Clear4Index(x/4);
			}
		}
	}
	LinkageProgram.ReScanExpViaRam();
	InterLink.ReScanExpViaRam();
}

void TConfig::WriteOneLinkCfgToNorFlash(unsigned int aLinkId)
{
	if((aLinkId < 1)||(aLinkId > LinkCountMax))return;
	
	unsigned int x = aLinkId - 1;
	
	LinkageProgram.Erase4Index(x - (x%4));
	LinkageProgram.Write4Index(x);
	
	LinkageProgram.ReScanExpViaRam();
	InterLink.ReScanExpViaRam();
}

//---------------------------------read---------------------------------------------------------------------------
unsigned int TConfig::ReadCtlPrmFromSdram(TConfigCtlData * pCtlPrm)
{
	unsigned int aLoadAddr;
	unsigned int i;
	
	aLoadAddr = CtlCfgBufAddr;
	for(i=0; i<dCtlCFG_BufCount/4; i++){
		pCtlPrm->D32[i] = *(volatile unsigned int *)(aLoadAddr + (i*4));
	}
	
	if(pCtlPrm->cfg.loopCount > LoopCountMax)return 1;
	if(pCtlPrm->cfg.busPanelCount > BusCountMax)return 1;
	if(pCtlPrm->cfg.dirPanelCount > DirCountMax)return 1;
	if(pCtlPrm->cfg.areaCount > AreaCountMax)return 1;
	
	return 0;
}

unsigned int TConfig::ReadLoopDevPrmFromSdram(unsigned char aId, unsigned char aDevId, TConfigLoopData * pLoopPrm)
{
	unsigned int aGetAddr;
	unsigned int x,y,i;
	
	if((aId < 1)||(aId > LoopCountMax))return 1 ;
	if((aDevId < 1)||(aDevId > DevInLoopCountMax))return 1;
	
	y = aId - 1;
	x = aDevId - 1;
	
	aGetAddr = LoopDevCfgBufAddr + (((y * DevInLoopCountMax) + x) * CFG_LOAD_BUF_SIZE);
	for(i=0; i<dLoopDevCFG_BufCount/4; i++){
		pLoopPrm->D32[i] = *(volatile unsigned int *)(aGetAddr + (i*4));
	}
	
	if((aId != pLoopPrm->cfg.id)||(aDevId != pLoopPrm->cfg.devId))return 1;
	
	return 0;
}

void TConfig::ReadLoopCfgDevTabFromSdram(unsigned int ord, unsigned char * pLoopCfgDevTab)
{
	unsigned int aGetAddr;
	unsigned int x,y,i;
	
	if((!ord)||(ord > LoopCountMax))return;
	x = ord - 1;
	aGetAddr = LoopDevCfgTabAddr + (x * CFG_TAB_BUF_SIZE);
	for(i=0; i<DevInLoopCountMax; i++){
		*(pLoopCfgDevTab + i) = *(volatile unsigned char *)(aGetAddr + i);
	}
}

void TConfig::ReadBusCfgKeyTabFromSdram(unsigned int ord, unsigned char * pBusCfgKeyTab)
{
	unsigned int aGetAddr;
	unsigned int x,y,i;
	
	if((!ord)||(ord > BusCountMax))return;
	x = ord - 1;
	aGetAddr = BusKeyCfgTabAddr + (x * CFG_TAB_BUF_SIZE);
	for(i=0; i<KeyInBusCountMax; i++){
		*(pBusCfgKeyTab + i) = *(volatile unsigned char *)(aGetAddr + i);
	}
}

void TConfig::ReadDirCfgKeyTabFromSdram(unsigned int ord, unsigned char * pDirCfgKeyTab)
{
	unsigned int aGetAddr;
	unsigned int x,y,i;
	
	if((!ord)||(ord > DirCountMax))return;
	x = ord - 1;
	aGetAddr = DirKeyCfgTabAddr + (x * CFG_TAB_BUF_SIZE);
	for(i=0; i<KeyInDirCountMax; i++){
		*(pDirCfgKeyTab + i) = *(volatile unsigned char *)(aGetAddr + i);
	}
}

unsigned int TConfig::ReadBusKeyPrmFromSdram(unsigned char aId, unsigned char aKeyId, TConfigBusPanelData * pBusPrm)
{
	unsigned int aGetAddr;
	unsigned int x,y,i;
	
	if((aId < 1)||(aId > BusCountMax))return 1 ;
	if((aKeyId < 1)||(aKeyId > KeyInBusCountMax))return 1;
	
	y = aId - 1;
	x = aKeyId - 1;
	
	aGetAddr = BusKeyCfgBufAddr + (((y * KeyInBusCountMax) + x) * CFG_LOAD_BUF_SIZE);
	for(i=0; i<dBusKeyCFG_BufCount/4; i++){
		pBusPrm->D32[i] = *(volatile unsigned int *)(aGetAddr + (i*4));
	}
	
	if((aId != pBusPrm->cfg.id)||(aKeyId != pBusPrm->cfg.keyId))return 1;
	
	return 0;
}

unsigned int TConfig::ReadDirKeyPrmFromSdram(unsigned char aId, unsigned char aKeyId, TConfigDirPanelData * pDirPrm)
{
	unsigned int aGetAddr;
	unsigned int x,y,i;
	
	if((aId < 1)||(aId > DirCountMax))return 1;
	if((aKeyId < 1)||(aKeyId > KeyInDirCountMax))return 1;
	
	y = aId - 1;
	x = aKeyId - 1;
	
	aGetAddr = DirKeyCfgBufAddr + (((y * KeyInDirCountMax) + x) * CFG_LOAD_BUF_SIZE);
	for(i=0; i<dDirKeyCFG_BufCount/4; i++){
		pDirPrm->D32[i] = *(volatile unsigned int *)(aGetAddr + (i*4));
	}
	
	if((aId != pDirPrm->cfg.id)||(aKeyId != pDirPrm->cfg.keyId))return 1;
	
	return 0;
}

unsigned int TConfig::ReadAreaPrmFromSdram(unsigned int aId, TConfigAreaData * pAreaPrm)
{
	unsigned int aLoadAddr;
	unsigned int i;
	unsigned int x;
	
	if((aId < 1)||(aId > AreaCountMax))return 1;
	
	x = aId - 1;
	
	aLoadAddr = AreaCfgBufAddr + (x * CFG_LOAD_BUF_SIZE);
	for(i=0; i<dAreaCFG_BufCount/4; i++){
		pAreaPrm->D32[i] = *(volatile unsigned int *)(aLoadAddr + (i*4));
	}
	
	if(aId != pAreaPrm->cfg.id)return 1;
	
	return 0;
}

int TConfig::LoadCfgFile()
{
	unsigned int i,ord;
	unsigned int a;
	int tSta =0;
	int tTmpSta;
	
	CfgPrmInit();
		
	//----------------ctl prm-----------------------------
	readPath[3] = '0' + (MainCtl.fData.Split.MyNum/10);//cfg01/ctl.csv
	readPath[4] = '0' + (MainCtl.fData.Split.MyNum%10);
	readPath[6] = 'c';
	readPath[7] = 't';
	readPath[8] = 'l';
	readPath[9] = '.';
	readPath[10]= 'c';
	readPath[11]= 's';
	readPath[12]= 'v';
	readPath[13]= 0;
	tTmpSta = ReadFile(readPath);
	if(tSta==0)tSta=tTmpSta;
	GetFileLine();
	GetCtlCfgPrm();
	WriteCtlPrmToSdram(&ctlPrm);
	
	//----------------loop prm-----------------------------
	readPath[6] = 'l';//loop01.csv
	readPath[7] = 'o';
	readPath[8] = 'o';
	readPath[9] = 'p';
	readPath[10]= '0';
	readPath[11]= '1';
	readPath[12]= '.';
	readPath[13]= 'c';
	readPath[14]= 's';
	readPath[15]= 'v';
	readPath[16]= 0;
	i=0;
	ord =0;
	while(i < ctlPrm.cfg.loopCount){
		if(ctlPrm.cfg.loopCfgTab & (((__UINT64_TYPE__)1)<<ord)){
			i++;
			readPath[10] = '0' + ((ord+1)/10);
			readPath[11] = '0' + ((ord+1)%10);
		
			for(a = 0;a < DevInLoopCountMax;a++)loopCfgDevTab[a] = 0;
			tTmpSta = ReadFile(readPath);
			if(tSta==0)tSta=tTmpSta;
			GetFileLine(&loopDevCount[ord+1]);//return each loop dev count
			if(fileAnalysisErr)break;
		
			for(a = 1;a <= loopDevCount[ord+1];a++){
				GetLoopCfgDevPrm(a);//return loop num of dev prm
				WriteLoopDevPrmToSdram(&loopPrm);//write prm to sdram
				if((DevDispType == loopPrm.cfg.devType)||(DevDispType == loopPrm.cfg.assignType)){//火灾显示盘 
					loopCfgDevDispExistTab[ord] = 1;
				}
			}
			
			WriteLoopCfgDevTabToSdram((ord+1), loopCfgDevTab);
		}
		if(ord < 32)ord++;
		else break;
	}
	
	//----------------loop disp prm-----------------------------
	readPath[6] = 'l';//lDisp01.csv
	readPath[7] = 'D';
	readPath[8] = 'i';
	readPath[9] = 's';
	readPath[10]= 'p';
	readPath[11]= '0';
	readPath[12]= '1';
	readPath[13]= '.';
	readPath[14]= 'c';
	readPath[15]= 's';
	readPath[16]= 'v';
	readPath[17]= 0;
	i=0;
	ord =0;
	while(i < ctlPrm.cfg.loopCount){
		if((ctlPrm.cfg.loopCfgTab & (((__UINT64_TYPE__)1)<<ord)) && (loopCfgDevDispExistTab[ord])){
			readPath[11] = '0' + ((ord+1)/10);
			readPath[12] = '0' + ((ord+1)%10);
			loopDispPrm.cfg.ord = 0;
			tTmpSta = ReadFile(readPath);
			if(tSta==0)tSta=tTmpSta;
			GetFileLine(&loopDevDispCount[ord+1]);//return each loop dev count
			if(loopDevDispCount[ord+1] > 16)fileAnalysisErr = 1;
			if(fileAnalysisErr)break;
			
			for(a = 1;a <= loopDevDispCount[ord+1];a++){
				GetLoopCfgDispPrm(a);//return loop num of dev prm
				WriteLoopDevDispPrmToSdram(&loopDispPrm);//write prm to sdram
			}
		}
		if(ord < 32)ord++;
		else break;
	}
	
	//----------------bus prm-----------------------------
	readPath[6] = 'b';//bus01.csv
	readPath[7] = 'u';
	readPath[8] = 's';
	readPath[9] = '0';
	readPath[10] = '1';
	readPath[11] = '.';
	readPath[12]= 'c';
	readPath[13]= 's';
	readPath[14]= 'v';
	readPath[15]= 0;
	i=0;
	ord =0;
	while(i < ctlPrm.cfg.busPanelCount){
		if(ctlPrm.cfg.busCfgTab & (1<<ord)){
			i++;
			readPath[9] = '0' + ((ord+1)/10);
			readPath[10] = '0' + ((ord+1)%10);
		
			for(a = 0;a < BusCountMax;a++)busCfgKeyTab[a] = 0;
			tTmpSta = ReadFile(readPath);
			if(tSta==0)tSta=tTmpSta;
			GetFileLine(&busKeyCount[ord+1]);
			if(fileAnalysisErr)break;
		
			for(a = 1;a <= busKeyCount[ord+1];a++){
				GetBusCfgKeyPrm(a);
				WriteBusKeyPrmToSdram(&busPrm);//write prm to sdram
			}
		
			WriteBusCfgKeyTabToSdram((ord+1), busCfgKeyTab);
		}
		if(ord < BusCountMax)ord++;
		else break;
	}
	
	//----------------dir prm-----------------------------
	readPath[6] = 'd';//bus01.csv
	readPath[7] = 'i';
	readPath[8] = 'r';
	readPath[9] = '0';
	readPath[10] = '1';
	readPath[11] = '.';
	readPath[12]= 'c';
	readPath[13]= 's';
	readPath[14]= 'v';
	readPath[15]= 0;
	i=0;
	ord =0;
	while(i < ctlPrm.cfg.dirPanelCount){
		if(ctlPrm.cfg.dirCfgTab & (1<<ord)){
			i++;
			readPath[9] = '0' + ((ord+1)/10);
			readPath[10] = '0' + ((ord+1)%10);
		
			for(a = 0;a < DirCountMax;a++)dirCfgKeyTab[a] = 0;
			tTmpSta = ReadFile(readPath);
			if(tSta==0)tSta=tTmpSta;
			GetFileLine(&dirKeyCount[ord+1]);
			if(fileAnalysisErr)break;
			
			for(a = 1;a <= dirKeyCount[ord+1];a++){
				GetDirCfgKeyPrm(a);
				WriteDirKeyPrmToSdram(&dirPrm);//write prm to sdram
			}
			
			WriteDirCfgKeyTabToSdram((ord+1), dirCfgKeyTab);
		}
		if(ord < DirCountMax)ord++;
		else break;
	}
	
	//----------------area prm-----------------------------
	readPath[6] = 'a';//area.csv
	readPath[7] = 'r';
	readPath[8] = 'e';
	readPath[9] = 'a';
	readPath[10] = '.';
	readPath[11]= 'c';
	readPath[12]= 's';
	readPath[13]= 'v';
	readPath[14]= 0;
	tTmpSta = ReadFile(readPath);
	if(tSta==0)tSta=tTmpSta;
	GetFileLine(&areaCount);
	for(i = 0;i < AreaCountMax;i++)areaCfgTab[i] = 0;
	
	for(a = 1;a <= areaCount;a++){
		GetAreaCfgPrm(a);
		WriteAreaPrmToSdram(&areaPrm);
	}
	
	WriteAreaCfgTabToSdram(areaCfgTab);
	
	//----------------link prm-----------------------------
	readPath[6] = 'l';//link.csv
	readPath[7] = 'i';
	readPath[8] = 'n';
	readPath[9] = 'k';
	readPath[10] = '.';
	readPath[11]= 'c';
	readPath[12]= 's';
	readPath[13]= 'v';
	readPath[14]= 0;
	tTmpSta = ReadFile(readPath);
	if(tSta==0)tSta=tTmpSta;
	GetFileLine(&linkCount);
	for(i = 0;i < LinkCountMax;i++)linkCfgTab[i] = 0;
	
	for(a = 1;a <= linkCount;a++){
		GetLinkCfgPrm(a);
		dataAnalysisErr = WriteLinkCfgToSdram(&linkPrm);
		linkPrm.cfg.inCount = 0;
		linkPrm.cfg.outCount = 0;
	}
	//WriteLinkCfgTabToSdram(linkCfgTab);
	WriteAllLinkCfgToNorFlash();
	
	if(0 != tSta)dataAnalysisErr = 1;
	
	if(!dataAnalysisErr)WriteAllUsbCfg();
	return tSta;
}

void TConfig::WriteProjectName()
{
	unsigned int i,aAddr;
		
	for(i=0;i<32;i++)
		PrjName[i] = ctlPrm.cfg.desc[i];
	
	aAddr = dEepAddrProjectName;
	DelayMs(10);
	eeprom_write_byte_via_page(0, aAddr, PrjName, 32);
}

void TConfig::WriteLoopCfgDevTabTypeDisp()
{
	unsigned int i,m;
	unsigned char aP0,aP1;
	volatile unsigned int aFlashState;
	volatile unsigned short dType;
	TConfigLoopDispData * pLoopDispData;

	for(i = 0; i < LoopCountMax; i++){
		if(ctlPrm.cfg.loopCfgTab & (((__UINT64_TYPE__)1)<<i)){
			aP0 = i;
			ReadLoopCfgDevTabFromSdram((aP0 + 1),loopCfgDevTab);
			for(m = 0; m < DevInLoopCountMax; m++){
				//Port[i].ExistTable[m] = 0;
				//Port[i].dTypeTable[m] = 0;
				if(loopCfgDevTab[m]){
					//Port[i].ExistTable[m] = 0xFF;//0xA5 is register
					dType = *(volatile unsigned short *)(LoopDevCfgBufAddr + (((i * DevInLoopCountMax) + m) * CFG_LOAD_BUF_SIZE) + 2);
					//Port[i].dTypeTable[m] = dType&0xFF;
				}
			}
			
			if(loopCfgDevDispExistTab[aP0]){
				for(m = 0;m<loopDevDispCount[aP0+1];m++){
					pLoopDispData = (TConfigLoopDispData *)(LoopDispCfgBufAddr + (((i * DispInLoopCountMax) + m) * CFG_LOAD_BUF_SIZE));
					Port[aP0].DisplayPadSet[m].Addr = pLoopDispData->cfg.devId;
					Port[aP0].DisplayPadSet[m].CfgBits = pLoopDispData->cfg.dispMode;
					Port[aP0].DisplayPadSet[m].AreaEx[0] = pLoopDispData->cfg.areaId[0];
					Port[aP0].DisplayPadSet[m].AreaEx[1] = pLoopDispData->cfg.areaId[1];
					Port[aP0].DisplayPadSet[m].AreaEx[2] = pLoopDispData->cfg.areaId[2];
					Port[aP0].DisplayPadSet[m].UcList[0].Full = pLoopDispData->cfg.devNum[0];
					Port[aP0].DisplayPadSet[m].UcList[1].Full = pLoopDispData->cfg.devNum[1];
				}
				Port[aP0].SortDisplayPad();
			}

			do{
				DelayMs(2);
				aFlashState = nor_get_busy_state(0);
			}while(aFlashState);
			
			//Erase 
			norFlashAddr = dFlashRegisterDataAddr + (dFlashRegisterPortDataLen * i) + (256 * 0);
			nor_erase_sector_4k(dFlashRegisterDataChip,norFlashAddr);
			do{
				DelayMs(2);
				aFlashState = nor_get_busy_state(0);
			}while(aFlashState);
			
			TRegister::sIList = &this->ItemList;
			TRegister::sWriteOnePort(aP0+1);
			
			//Read Back
			Record.ReadRegisterDataOnePort(aP0+1);
		}
	}

	for(m=0; m<256; m++){
		nFlashPageData.D8[m] = 0;
	}
	//----------------config loop table on APP----------------------------------
	/*
	for(m=0; m<LoopCountMax; m++){
		if(ctlPrm.cfg.loopCfgTab & (((__UINT64_TYPE__)1)<<m)){
			nFlashPageData.D8[m] = 0xA5;
		}
	}
	*/
	//----------------config loop table on practical----------------------------------
	TRegister::sIList = &this->ItemList;
	TRegister::sWritePortList();
	
	Record.ReadRegisterData();
	Record.ReadDisplayPadSet();
}

void TConfig::WriteLoopCfgAllDisp()
{
	unsigned int i,m;
	unsigned char aP0,aP1;
	volatile unsigned int aFlashState;
	volatile unsigned short dType;
	TConfigLoopDispData * pLoopDispData;

	for(i = 0; i < LoopCountMax; i++){
		aP0 = i;
		
		if(loopCfgDevDispExistTab[aP0]){
			for(m = 0;m<loopDevDispCount[aP0+1];m++){
				pLoopDispData = (TConfigLoopDispData *)(LoopDispCfgBufAddr + (((i * DispInLoopCountMax) + m) * CFG_LOAD_BUF_SIZE));
				Port[aP0].DisplayPadSet[m].Addr = pLoopDispData->cfg.devId;
				Port[aP0].DisplayPadSet[m].CfgBits = pLoopDispData->cfg.dispMode;
				Port[aP0].DisplayPadSet[m].AreaEx[0] = pLoopDispData->cfg.areaId[0];
				Port[aP0].DisplayPadSet[m].AreaEx[1] = pLoopDispData->cfg.areaId[1];
				Port[aP0].DisplayPadSet[m].AreaEx[2] = pLoopDispData->cfg.areaId[2];
				Port[aP0].DisplayPadSet[m].UcList[0].Full = pLoopDispData->cfg.devNum[0];
				Port[aP0].DisplayPadSet[m].UcList[1].Full = pLoopDispData->cfg.devNum[1];
			}
		}

		do{
			DelayMs(2);
			aFlashState = nor_get_busy_state(0);
		}while(aFlashState);
		
		//Erase 
		norFlashAddr = dFlashRegisterDataAddr + (dFlashRegisterPortDataLen * i) + (256 * 0);
		nor_erase_sector_4k(dFlashRegisterDataChip,norFlashAddr);
		do{
			DelayMs(2);
			aFlashState = nor_get_busy_state(0);
		}while(aFlashState);
		
		TRegister::sIList = &this->ItemList;
		TRegister::sWriteOnePort(aP0+1);
		
		//Read Back
		Record.ReadRegisterDataOnePort(aP0+1);
	}

	TRegister::sIList = &this->ItemList;
	TRegister::sWritePortList();
	
	Record.ReadRegisterData();
	Record.ReadDisplayPadSet();
}

void TConfig::WriteLoopCfgOneDisp(unsigned int aLoopId, unsigned int aDispId)
{
	unsigned char aP0,aP1;
	volatile unsigned int aFlashState;
	TConfigLoopDispData * pLoopDispData;

	if((aLoopId < 1)||(aLoopId > LoopCountMax))return;
	if((aDispId < 1)||(aDispId > DispInLoopCountMax))return;
	
	aP0 = aLoopId - 1;
	aP1 = aDispId - 1;
		
	pLoopDispData = (TConfigLoopDispData *)(LoopDispCfgBufAddr + (((aP0 * DispInLoopCountMax) + aP1) * CFG_LOAD_BUF_SIZE));
	Port[aP0].DisplayPadSet[aP1].Addr = pLoopDispData->cfg.devId;
	Port[aP0].DisplayPadSet[aP1].CfgBits = pLoopDispData->cfg.dispMode;
	Port[aP0].DisplayPadSet[aP1].AreaEx[0] = pLoopDispData->cfg.areaId[0];
	Port[aP0].DisplayPadSet[aP1].AreaEx[1] = pLoopDispData->cfg.areaId[1];
	Port[aP0].DisplayPadSet[aP1].AreaEx[2] = pLoopDispData->cfg.areaId[2];
	Port[aP0].DisplayPadSet[aP1].UcList[0].Full = pLoopDispData->cfg.devNum[0];
	Port[aP0].DisplayPadSet[aP1].UcList[1].Full = pLoopDispData->cfg.devNum[1];

	do{
		DelayMs(2);
		aFlashState = nor_get_busy_state(0);
	}while(aFlashState);
	
	//Erase 
	norFlashAddr = dFlashRegisterDataAddr + (dFlashRegisterPortDataLen * aP0) + (256 * 0);
	nor_erase_sector_4k(dFlashRegisterDataChip,norFlashAddr);
	do{
		DelayMs(2);
		aFlashState = nor_get_busy_state(0);
	}while(aFlashState);
	
	TRegister::sIList = &this->ItemList;
	TRegister::sWriteOnePort(aP0+1);
	
	//Read Back
	Record.ReadRegisterDataOnePort(aP0+1);

	TRegister::sIList = &this->ItemList;
	TRegister::sWritePortList();
	
	Record.ReadRegisterData();
	Record.ReadDisplayPadSet();
}

void TConfig::WriteLoopDevUserCodeAssignType()
{
	volatile unsigned int i, x;
	unsigned char aP0, aP1;
	
	for(i=0; i<LoopCountMax; i++){
		if(ctlPrm.cfg.loopCfgTab & (((__UINT64_TYPE__)1)<<i)){
			aP0 = i;
			ReadLoopCfgDevTabFromSdram((aP0+1), loopCfgDevTab);
			for(x = 0;x<DevInLoopCountMax; x++){
				aP1 = x;
				if(1 == loopCfgDevTab[x]){
					ReadLoopDevPrmFromSdram((aP0+1),(aP1+1),&loopPrm);
					Port[aP0].UcList[aP1].Full = loopPrm.cfg.devNum;
					Port[aP0].AssignType[aP1] = (loopPrm.cfg.assignType&0xFF);
					Port[aP0].FuncSetByte[aP1] = (loopPrm.cfg.devProperty&0xFF);
				}
			}
			
			TDevProperty::sIList = &this->ItemList;
			TDevProperty::sWriteAuxIdAssignTypeSignalType(aP0 + 1);
		}
	}
	
	Record.ReadDevUserSet();
}

void TConfig::WriteLoopOneDevUserCodeAssignType(unsigned int aLoopId, unsigned int aDevId)
{
	unsigned char aP0, aP1;
	
	if((aLoopId < 1)||(aLoopId > LoopCountMax))return;
	if((aDevId < 1)||(aDevId > DevInLoopCountMax))return;
	
	aP0 = aLoopId - 1;
	aP1 = aDevId - 1;
	
	ReadLoopCfgDevTabFromSdram((aP0+1), loopCfgDevTab);

	if(1 == loopCfgDevTab[aP1]){
		ReadLoopDevPrmFromSdram((aP0+1),(aP1+1),&loopPrm);
		Port[aP0].UcList[aP1].Full = loopPrm.cfg.devNum;
		Port[aP0].AssignType[aP1] = (loopPrm.cfg.assignType&0xFF);
		Port[aP0].FuncSetByte[aP1] = (loopPrm.cfg.devProperty&0xFF);
		
		TDevProperty::sIList = &this->ItemList;
		TDevProperty::sWriteAuxIdAssignTypeSignalType(aP0 + 1);
		
		Record.ReadDevUserSet();
	}
}

void TConfig::WriteOneLoopDevUserCodeAssignType(unsigned int aLoopId)
{
	volatile unsigned int x;
	unsigned char aP0, aP1;
	
	if((aLoopId < 1)||(aLoopId > LoopCountMax))return;
	
	aP0 = aLoopId - 1;
	
	ReadLoopCfgDevTabFromSdram((aP0+1), loopCfgDevTab);
	for(x = 0;x<DevInLoopCountMax; x++){
		aP1 = x;
		if(1 == loopCfgDevTab[x]){
			ReadLoopDevPrmFromSdram((aP0+1),(aP1+1),&loopPrm);
			Port[aP0].UcList[aP1].Full = loopPrm.cfg.devNum;
			Port[aP0].AssignType[aP1] = (loopPrm.cfg.assignType&0xFF);
			Port[aP0].FuncSetByte[aP1] = (loopPrm.cfg.devProperty&0xFF);
		}
	}
	
	TDevProperty::sIList = &this->ItemList;
	TDevProperty::sWriteAuxIdAssignTypeSignalType(aP0 + 1);
	
	Record.ReadDevUserSet();
}

void TConfig::WriteLoopDevArea()
{
	volatile unsigned int i, x;
	unsigned char aP0, aP1;
	
	for(i=0; i<LoopCountMax; i++){
		if(ctlPrm.cfg.loopCfgTab & (((__UINT64_TYPE__)1)<<i)){
			aP0 = i;
			ReadLoopCfgDevTabFromSdram((aP0+1), loopCfgDevTab);
			for(x = 0;x<DevInLoopCountMax; x++){
				aP1 = x;
				if(1 == loopCfgDevTab[x]){
					ReadLoopDevPrmFromSdram((aP0+1),(aP1+1),&loopPrm);
					Port[aP0].Area[aP1] = loopPrm.cfg.areaId;
				}
			}
			
			TDevProperty::sIList = &this->ItemList;
			TDevProperty::sWriteAreaNumOnePort(aP0+1);
		}
	}
}

void TConfig::WriteLoopOneDevArea(unsigned int aLoopId, unsigned int aDevId)
{
	unsigned char aP0, aP1;
	
	if((aLoopId < 1)||(aLoopId > LoopCountMax))return;
	if((aDevId < 1)||(aDevId > DevInLoopCountMax))return;
	
	aP0 = aLoopId - 1;
	aP1 = aDevId - 1;
	
	ReadLoopCfgDevTabFromSdram((aP0+1), loopCfgDevTab);
	if(1 == loopCfgDevTab[aP1]){
		ReadLoopDevPrmFromSdram((aP0+1),(aP1+1),&loopPrm);
		Port[aP0].Area[aP1] = loopPrm.cfg.areaId;
	}
	
	TDevProperty::sIList = &this->ItemList;
	TDevProperty::sWriteAreaNumOnePort(aP0+1);
}

void TConfig::WriteOneLoopDevArea(unsigned int aLoopId)
{
	volatile unsigned int x;
	unsigned char aP0, aP1;
	
	if((aLoopId < 1)||(aLoopId > LoopCountMax))return;
	
	aP0 = aLoopId - 1;

	ReadLoopCfgDevTabFromSdram((aP0+1), loopCfgDevTab);
	for(x = 0;x<DevInLoopCountMax; x++){
		aP1 = x;
		if(1 == loopCfgDevTab[x]){
			ReadLoopDevPrmFromSdram((aP0+1),(aP1+1),&loopPrm);
			Port[aP0].Area[aP1] = loopPrm.cfg.areaId;
		}
	}
	
	TDevProperty::sIList = &this->ItemList;
	TDevProperty::sWriteAreaNumOnePort(aP0+1);
}

void TConfig::WriteLoopDevDescp()
{
	unsigned int a,b,i;
	unsigned int cfgDescpSdramAddr;
	unsigned int cfgLoopSdramAddr;
	
	cfgDescpSdramAddr = dAddrSdRam_Descp;
	cfgLoopSdramAddr = LoopDevCfgBufAddr + 16;//descp start 16th
	
	for(a=1;a<=LoopCountMax;a++){
		if(ctlPrm.cfg.loopCfgTab & (((__UINT64_TYPE__)1)<<(a-1))){
			ReadLoopCfgDevTabFromSdram(a, loopCfgDevTab);
			for(b=0;b<DevInLoopCountMax;b++){
				if(1 == loopCfgDevTab[b]){
					for(i=0;i<(32/4);i++){
						*(volatile unsigned int *)(cfgDescpSdramAddr + (((a-1)*256+b)*32) + (i*4)) = \
							*(volatile unsigned int *)(cfgLoopSdramAddr + (((a-1)*DevInLoopCountMax+b)*CFG_LOAD_BUF_SIZE) + (i*4));
					}
				}else{
					for(i=0;i<(32/4);i++){
						*(volatile unsigned int *)(cfgDescpSdramAddr + (((a-1)*256+b)*32) + (i*4)) = 0;
					}
				}
			}
			TDescpWrite::sWrite2Flash(a);
		}
	}
}

void TConfig::WriteLoopOneDevDescp(unsigned int aLoopId, unsigned int aDevId)
{
	unsigned int a,b,i;
	unsigned int cfgDescpSdramAddr;
	unsigned int cfgLoopSdramAddr;
	
	if((aLoopId < 1)||(aLoopId > LoopCountMax))return;
	if((aDevId < 1)||(aDevId > DevInLoopCountMax))return;
	
	a = aLoopId;
	b = aDevId - 1;
	
	cfgDescpSdramAddr = dAddrSdRam_Descp;
	cfgLoopSdramAddr = LoopDevCfgBufAddr + 16;//descp start 16th
	
	ReadLoopCfgDevTabFromSdram(a, loopCfgDevTab);
	if(1 == loopCfgDevTab[b]){
		for(i=0;i<(32/4);i++){
			*(volatile unsigned int *)(cfgDescpSdramAddr + (((a-1)*256+b)*32) + (i*4)) = \
				*(volatile unsigned int *)(cfgLoopSdramAddr + (((a-1)*DevInLoopCountMax+b)*CFG_LOAD_BUF_SIZE) + (i*4));
		}
	}else{
		for(i=0;i<(32/4);i++){
			*(volatile unsigned int *)(cfgDescpSdramAddr + (((a-1)*256+b)*32) + (i*4)) = 0;
		}
	}
	TDescpWrite::sWrite2Flash(a);
}

void TConfig::WriteOneLoopDevDescp(unsigned int aLoopId)
{
	unsigned int a,b,i;
	unsigned int cfgDescpSdramAddr;
	unsigned int cfgLoopSdramAddr;
	
	if((aLoopId < 1)||(aLoopId > LoopCountMax))return;
	
	a = aLoopId;
	
	cfgDescpSdramAddr = dAddrSdRam_Descp;
	cfgLoopSdramAddr = LoopDevCfgBufAddr + 16;//descp start 16th
	
	ReadLoopCfgDevTabFromSdram(a, loopCfgDevTab);
	for(b=0;b<DevInLoopCountMax;b++){
		if(1 == loopCfgDevTab[b]){
			for(i=0;i<(32/4);i++){
				*(volatile unsigned int *)(cfgDescpSdramAddr + (((a-1)*256+b)*32) + (i*4)) = \
					*(volatile unsigned int *)(cfgLoopSdramAddr + (((a-1)*DevInLoopCountMax+b)*CFG_LOAD_BUF_SIZE) + (i*4));
			}
		}else{
			for(i=0;i<(32/4);i++){
				*(volatile unsigned int *)(cfgDescpSdramAddr + (((a-1)*256+b)*32) + (i*4)) = 0;
			}
		}
	}
	TDescpWrite::sWrite2Flash(a);
}

void TConfig::WriteBusCfgKeyNormal()
{
	unsigned int fAddr;
	volatile unsigned int i, x, a, b, GetSta;
	unsigned char aP0, aP1;
	
	for(i=0; i<BusCountMax; i++){//20max
		if(ctlPrm.cfg.busCfgTab & (1<<i)){
			aP0 = i;
			
			ReadBusCfgKeyTabFromSdram((aP0+1), busCfgKeyTab);
			for(x = 0;x<KeyInBusCountMax; x++){
				aP1 = x;
				if(1 == busCfgKeyTab[x]){
					ReadBusKeyPrmFromSdram((aP0+1),(aP1+1),&busPrm);
					HandCtlBoard[aP0].UcList[aP1].Full = busPrm.cfg.serNum;
					HandCtlBoard[aP0].AssignType[aP1] = busPrm.cfg.devType;
					HandCtlBoard[aP0].IsMatchAreaAssign[aP1] = busPrm.cfg.ctlMode;
					HandCtlBoard[aP0].Area[aP1] = busPrm.cfg.areaId;
				}
			}
			
			TBusPadLnk::sIList = &this->ItemList;
			TBusPadLnk::sWriteOnePad(aP0 + 1);
		}
	}
}

void TConfig::WriteBusOneKeyNormal(unsigned int aBusId, unsigned int aKeyId)
{
	unsigned char aP0, aP1;
		
	if((aBusId < 1)||(aBusId > BusCountMax))return;
	if((aKeyId < 1)||(aKeyId > KeyInBusCountMax))return;

	aP0 = aBusId - 1;
	aP1 = aKeyId - 1;
	
	ReadBusCfgKeyTabFromSdram((aP0+1), busCfgKeyTab);
			
	if(1 == busCfgKeyTab[aP1]){
		ReadBusKeyPrmFromSdram((aP0+1),(aP1+1),&busPrm);
		HandCtlBoard[aP0].UcList[aP1].Full = busPrm.cfg.serNum;
		HandCtlBoard[aP0].AssignType[aP1] = busPrm.cfg.devType;
		HandCtlBoard[aP0].IsMatchAreaAssign[aP1] = busPrm.cfg.ctlMode;
		HandCtlBoard[aP0].Area[aP1] = busPrm.cfg.areaId;
				
		TBusPadLnk::sIList = &this->ItemList;
		TBusPadLnk::sWriteOnePad(aP0 + 1);
	}
}

void TConfig::WriteOneBusKeyNormal(unsigned int aBusId)
{
	unsigned int x;
	unsigned char aP0, aP1;
	
	if((aBusId < 1)||(aBusId > BusCountMax))return;
	
	aP0 = aBusId - 1;
	
	ReadBusCfgKeyTabFromSdram((aP0+1), busCfgKeyTab);
	for(x = 0;x<KeyInBusCountMax; x++){
		aP1 = x;
		if(1 == busCfgKeyTab[x]){
			ReadBusKeyPrmFromSdram((aP0+1),(aP1+1),&busPrm);
			HandCtlBoard[aP0].UcList[aP1].Full = busPrm.cfg.serNum;
			HandCtlBoard[aP0].AssignType[aP1] = busPrm.cfg.devType;
			HandCtlBoard[aP0].IsMatchAreaAssign[aP1] = busPrm.cfg.ctlMode;
			HandCtlBoard[aP0].Area[aP1] = busPrm.cfg.areaId;
		}
	}
	
	TBusPadLnk::sIList = &this->ItemList;
	TBusPadLnk::sWriteOnePad(aP0 + 1);
}

void TConfig::WriteDirCfgKeyNormal()
{
	unsigned int fAddr;
	volatile unsigned int i, x, a, b, GetSta;
	unsigned char aP0, aP1;
	
	for(i=0; i<DirCountMax; i++){//20max
		if(ctlPrm.cfg.dirCfgTab & (1<<i)){
			aP0 = i;
			
			ReadDirCfgKeyTabFromSdram((aP0+1), dirCfgKeyTab);
			for(x = 0;x<KeyInDirCountMax; x++){
				aP1 = x;
				if(1 == dirCfgKeyTab[x]){
					ReadDirKeyPrmFromSdram((aP0+1),(aP1+1),&dirPrm);
					DirectCtlBoard[aP0].IsRegisted[aP1] = dirPrm.cfg.isAble;
					DirectCtlBoard[aP0].IsCheckError[aP1] = dirPrm.cfg.errDetFlag;
					DirectCtlBoard[aP0].IsOutputState[aP1] = 0;
					//DirectCtlBoard[aP0].PadIsProhibit = 1;
				}
			}
			
			TDirectPadCfg::sIList = &this->ItemList;
			TDirectPadCfg::sWrite2Flash(aP0 + 1);
			
			DirectCtlBoard[aP0].SendPadCfg2Pad(aP0);
		}
	}	
}

void TConfig::WriteDirOneKeyNormal(unsigned int aDirId, unsigned int aKeyId)
{
	unsigned char aP0, aP1;
	
	if((aDirId < 1)||(aDirId > DirCountMax))return;
	if((aKeyId < 1)||(aKeyId > KeyInDirCountMax))return;
	
	aP0 = aDirId - 1;
	aP1 = aKeyId - 1;
			
	ReadDirCfgKeyTabFromSdram((aP0+1), dirCfgKeyTab);
	if(1 == dirCfgKeyTab[aP1]){
		ReadDirKeyPrmFromSdram((aP0+1),(aP1+1),&dirPrm);
		DirectCtlBoard[aP0].IsRegisted[aP1] = dirPrm.cfg.isAble;
		DirectCtlBoard[aP0].IsCheckError[aP1] = dirPrm.cfg.errDetFlag;
		DirectCtlBoard[aP0].IsOutputState[aP1] = 0;
		//DirectCtlBoard[aP0].PadIsProhibit = 1;
	}
	
	TDirectPadCfg::sIList = &this->ItemList;
	TDirectPadCfg::sWrite2Flash(aP0 + 1);
	
	DirectCtlBoard[aP0].SendPadCfg2Pad(aP0);
}

void TConfig::WriteOneDirKeyNormal(unsigned int aDirId)
{
	volatile unsigned int x;
	unsigned char aP0, aP1;
	
	if((aDirId < 1)||(aDirId > DirCountMax))return;
	
	aP0 = aDirId - 1;
	
	ReadDirCfgKeyTabFromSdram((aP0+1), dirCfgKeyTab);
	for(x = 0;x<KeyInDirCountMax; x++){
		aP1 = x;
		if(1 == dirCfgKeyTab[x]){
			ReadDirKeyPrmFromSdram((aP0+1),(aP1+1),&dirPrm);
			DirectCtlBoard[aP0].IsRegisted[aP1] = dirPrm.cfg.isAble;
			DirectCtlBoard[aP0].IsCheckError[aP1] = dirPrm.cfg.errDetFlag;
			DirectCtlBoard[aP0].IsOutputState[aP1] = 0;
			//DirectCtlBoard[aP0].PadIsProhibit = 1;
		}
	}
	
	TDirectPadCfg::sIList = &this->ItemList;
	TDirectPadCfg::sWrite2Flash(aP0 + 1);
	
	DirectCtlBoard[aP0].SendPadCfg2Pad(aP0);
}

void TConfig::WriteDirKeyUserCodeAssignType()
{
	unsigned int fAddr;
	volatile unsigned int i, x, a, b, GetSta;
	unsigned char aP0, aP1;
	
	for(i=0; i<DirCountMax; i++){//20max
		if(ctlPrm.cfg.dirCfgTab & (1<<i)){
			aP0 = i;
			
			ReadDirCfgKeyTabFromSdram((aP0+1), dirCfgKeyTab);
			for(x = 0;x<KeyInDirCountMax; x++){
				aP1 = x;
				if(1 == dirCfgKeyTab[x]){
					ReadDirKeyPrmFromSdram((aP0+1),(aP1+1),&dirPrm);
					DirectCtlBoard[aP0].UcList[aP1].Full = dirPrm.cfg.serNum;
					DirectCtlBoard[aP0].AssignType[aP1] = (dirPrm.cfg.ctlDevType&0xFF);
				}
			}
			
			TDirectPadCfg::sIList = &this->ItemList;
			TDirectPadCfg::sWriteUserCodeAssignType(aP0 + 1);
		}
	}	
	
	Record.ReadDirectPadUserSet();
}

void TConfig::WriteDirOneKeyUserCodeAssignType(unsigned int aDirId, unsigned int aKeyId)
{
	unsigned char aP0, aP1;
	
	if((aDirId < 1)||(aDirId > DirCountMax))return;
	if((aKeyId < 1)||(aKeyId > KeyInDirCountMax))return;
	
	aP0 = aDirId - 1;
	aP1 = aKeyId - 1;
			
	ReadDirCfgKeyTabFromSdram((aP0+1), dirCfgKeyTab);
	if(1 == dirCfgKeyTab[aP1]){
		ReadDirKeyPrmFromSdram((aP0+1),(aP1+1),&dirPrm);
		DirectCtlBoard[aP0].UcList[aP1].Full = dirPrm.cfg.serNum;
		DirectCtlBoard[aP0].AssignType[aP1] = (dirPrm.cfg.ctlDevType&0xFF);
	}
			
	TDirectPadCfg::sIList = &this->ItemList;
	TDirectPadCfg::sWriteUserCodeAssignType(aP0 + 1);
	
	Record.ReadDirectPadUserSet(aP0 + 1);
}

void TConfig::WriteOneDirKeyUserCodeAssignType(unsigned int aDirId)
{
	volatile unsigned int x;
	unsigned char aP0, aP1;
	
	if((aDirId < 1)||(aDirId > DirCountMax))return;
	
	aP0 = aDirId - 1;
			
	ReadDirCfgKeyTabFromSdram((aP0+1), dirCfgKeyTab);
	for(x = 0;x<KeyInDirCountMax; x++){
		aP1 = x;
		if(1 == dirCfgKeyTab[x]){
			ReadDirKeyPrmFromSdram((aP0+1),(aP1+1),&dirPrm);
			DirectCtlBoard[aP0].UcList[aP1].Full = dirPrm.cfg.serNum;
			DirectCtlBoard[aP0].AssignType[aP1] = (dirPrm.cfg.ctlDevType&0xFF);
		}
	}
	
	TDirectPadCfg::sIList = &this->ItemList;
	TDirectPadCfg::sWriteUserCodeAssignType(aP0 + 1);
	
	Record.ReadDirectPadUserSet(aP0 + 1);
}

void TConfig::WriteDirKeyArea()
{
	unsigned int fAddr;
	volatile unsigned int i, x, a, b, GetSta;
	unsigned char aP0, aP1;
	
	for(i=0; i<DirCountMax; i++){//20max
		if(ctlPrm.cfg.dirCfgTab & (1<<i)){
			aP0 = i;
			
			ReadDirCfgKeyTabFromSdram((aP0+1), dirCfgKeyTab);
			for(x = 0;x<KeyInDirCountMax; x++){
				aP1 = x;
				if(1 == dirCfgKeyTab[x]){
					ReadDirKeyPrmFromSdram((aP0+1),(aP1+1),&dirPrm);
					DirectCtlBoard[aP0].Area[aP1] = dirPrm.cfg.areaId;
				}
			}
		}
	}
	
	TDirectPadCfg::sIList = &this->ItemList;
	TDirectPadCfg::sWriteAreaNum();
}

void TConfig::WriteDirOneKeyArea(unsigned int aDirId, unsigned int aKeyId)
{
	unsigned char aP0, aP1;
	
	if((aDirId < 1)||(aDirId > DirCountMax))return;
	if((aKeyId < 1)||(aKeyId > KeyInDirCountMax))return;
	
	aP0 = aDirId - 1;
	aP1 = aKeyId - 1;
			
	ReadDirCfgKeyTabFromSdram((aP0+1), dirCfgKeyTab);
	if(1 == dirCfgKeyTab[aP1]){
		ReadDirKeyPrmFromSdram((aP0+1),(aP1+1),&dirPrm);
		DirectCtlBoard[aP0].Area[aP1] = dirPrm.cfg.areaId;
	}
	
	TDirectPadCfg::sIList = &this->ItemList;
	TDirectPadCfg::sWriteAreaNum();
}

void TConfig::WriteOneDirKeyArea(unsigned int aDirId)
{
	volatile unsigned int x;
	unsigned char aP0, aP1;
	
	if((aDirId < 1)||(aDirId > DirCountMax))return;
	
	aP0 = aDirId - 1;
			
	ReadDirCfgKeyTabFromSdram((aP0+1), dirCfgKeyTab);
	for(x = 0;x<KeyInDirCountMax; x++){
		aP1 = x;
		if(1 == dirCfgKeyTab[x]){
			ReadDirKeyPrmFromSdram((aP0+1),(aP1+1),&dirPrm);
			DirectCtlBoard[aP0].Area[aP1] = dirPrm.cfg.areaId;
		}
	}
	
	TDirectPadCfg::sIList = &this->ItemList;
	TDirectPadCfg::sWriteAreaNum();
}

void TConfig::WriteDirCfgDescp()
{
	unsigned int a,b,i;
	unsigned int cfgDescpSdramAddr;
	unsigned int cfgDirSdramAddr;
		
	cfgDescpSdramAddr = dAddrSdRam_Descp + 40 * 8192;
	cfgDirSdramAddr = DirKeyCfgBufAddr + 12;//descp start 12th
	
	for(a=1;a<=DirCountMax;a++){
		if(ctlPrm.cfg.dirCfgTab & (1<<(a-1))){
			ReadDirCfgKeyTabFromSdram(a, dirCfgKeyTab);
			for(b=0;b<KeyInDirCountMax;b++){
				if(1 == dirCfgKeyTab[b]){
					for(i=0;i<(32/4);i++){
						*(volatile unsigned int *)(cfgDescpSdramAddr + (((a-1)*8+b+1)*32) + (i*4)) = \
							*(volatile unsigned int *)(cfgDirSdramAddr + (((a-1)*KeyInDirCountMax+b)*CFG_LOAD_BUF_SIZE) + (i*4));
					}
				}else{
					for(i=0;i<(32/4);i++){
						*(volatile unsigned int *)(cfgDescpSdramAddr + (((a-1)*8+b+1)*32) + (i*4)) = 0;
					}
				}
			}
		}else{
			for(b=0;b<KeyInDirCountMax;b++){
				for(i=0;i<(32/4);i++){
					*(volatile unsigned int *)(cfgDescpSdramAddr + (((a-1)*8+b+1)*32) + (i*4)) = 0;
				}
			}
		}
	}
	
	TDescpWrite::sWrite2Flash(66);
}

void TConfig::WriteDirOneDescp(unsigned int aDirId, unsigned int aKeyId)
{
	unsigned int a,b,i;
	unsigned int cfgDescpSdramAddr;
	unsigned int cfgDirSdramAddr;
	
	if((aDirId < 1)||(aDirId > DirCountMax))return;
	if((aKeyId < 1)||(aKeyId > KeyInDirCountMax))return;
	
	a = aDirId;
	b = aKeyId - 1;
		
	cfgDescpSdramAddr = dAddrSdRam_Descp + 40 * 8192;
	cfgDirSdramAddr = DirKeyCfgBufAddr + 12;//descp start 12th
	
	ReadDirCfgKeyTabFromSdram(a, dirCfgKeyTab);
	if(1 == dirCfgKeyTab[b]){
		for(i=0;i<(32/4);i++){
			*(volatile unsigned int *)(cfgDescpSdramAddr + (((a-1)*8+b+1)*32) + (i*4)) = \
				*(volatile unsigned int *)(cfgDirSdramAddr + (((a-1)*KeyInDirCountMax+b)*CFG_LOAD_BUF_SIZE) + (i*4));
		}
	}else{
		for(i=0;i<(32/4);i++){
			*(volatile unsigned int *)(cfgDescpSdramAddr + (((a-1)*8+b+1)*32) + (i*4)) = 0;
		}
	}
	
	TDescpWrite::sWrite2Flash(66);
}

void TConfig::WriteOneDirDescp(unsigned int aDirId)
{
	unsigned int a,b,i;
	unsigned int cfgDescpSdramAddr;
	unsigned int cfgDirSdramAddr;
	
	if((aDirId < 1)||(aDirId > DirCountMax))return;
	
	a = aDirId;
		
	cfgDescpSdramAddr = dAddrSdRam_Descp + 40 * 8192;
	cfgDirSdramAddr = DirKeyCfgBufAddr + 12;//descp start 12th
	
	ReadDirCfgKeyTabFromSdram(a, dirCfgKeyTab);
	for(b=0;b<KeyInDirCountMax;b++){
		if(1 == dirCfgKeyTab[b]){
			for(i=0;i<(32/4);i++){
				*(volatile unsigned int *)(cfgDescpSdramAddr + (((a-1)*8+b+1)*32) + (i*4)) = \
					*(volatile unsigned int *)(cfgDirSdramAddr + (((a-1)*KeyInDirCountMax+b)*CFG_LOAD_BUF_SIZE) + (i*4));
			}
		}else{
			for(i=0;i<(32/4);i++){
				*(volatile unsigned int *)(cfgDescpSdramAddr + (((a-1)*8+b+1)*32) + (i*4)) = 0;
			}
		}
	}
	
	TDescpWrite::sWrite2Flash(66);
}

void TConfig::WriteAreaCfg()
{
	unsigned int fAddr, GetSta;
	unsigned int sectorX, PageX;
	volatile unsigned int i, x, a, b;
	unsigned int aP0;
	
	for(sectorX = 0; sectorX < (AreaCountMax*64/4096); sectorX++){
		for(i=0;i<64;i++){
			if(areaCfgTab[sectorX * 64 + i]){
				do{
					DelayMs(3);
					GetSta = nor_get_busy_state(dFlash4AreaChip);
				}while(GetSta);
				
				fAddr = dFlash4Area + (sectorX * 4096);
				nor_erase_sector_4k(dFlash4AreaChip,fAddr);
				DelayMs(100);
				do{
					DelayMs(3);
					GetSta = nor_get_busy_state(dFlash4AreaChip);
					LoopCheck1S();
				}while(GetSta);
				
				break;
			}
		}
		
		for(PageX=0;PageX<16;PageX++){
			if((areaCfgTab[(sectorX * 64) + (PageX*4) + 0])||(areaCfgTab[(sectorX * 64) + (PageX*4) + 1])|| \
				(areaCfgTab[(sectorX * 64) + (PageX*4) + 2]) || (areaCfgTab[(sectorX * 64) + (PageX*4) + 3])){
				for(a=0;a<64;a++){
					nFlashPageData.D32[a] = 0;
				}
				
				for(a=0;a<4;a++){
					if(areaCfgTab[(sectorX * 64) + (PageX * 4) + a]){
						aP0 = (sectorX * 64) + (PageX*4) + a;
						ReadAreaPrmFromSdram((aP0+1),&areaPrm);
						nFlashPageData.D32[16*a + 0] = 0xA8A7A6A5;
						nFlashPageData.D8[64*a + 4 + 0] = areaPrm.cfg.serNum/10000000%10 + '0';
						nFlashPageData.D8[64*a + 4 + 1] = areaPrm.cfg.serNum/1000000%10 + '0';
						nFlashPageData.D8[64*a + 4 + 2] = areaPrm.cfg.serNum/100000%10 + '0';
						nFlashPageData.D8[64*a + 4 + 3] = areaPrm.cfg.serNum/10000%10 + '0';
						nFlashPageData.D8[64*a + 4 + 4] = areaPrm.cfg.serNum/1000%10 + '0';
						nFlashPageData.D8[64*a + 4 + 5] = areaPrm.cfg.serNum/100%10 + '0';
						nFlashPageData.D8[64*a + 4 + 6] = areaPrm.cfg.serNum/10%10 + '0';
						nFlashPageData.D8[64*a + 4 + 7] = areaPrm.cfg.serNum%10 + '0';
						for(b=0;b<16;b++){
							nFlashPageData.D8[64*a+16+b] = areaPrm.cfg.profile[b];
						}
						for(b=0;b<32;b++){
							nFlashPageData.D8[64*a+32+b] = areaPrm.cfg.desc[b];
						}
					}
				}
				
				fAddr = dFlash4Area + ((sectorX * 16 + PageX) * 256);
				nor_write_page(dFlash4AreaChip, fAddr, nFlashPageData.D8);

				do{
					DelayMs(5);
					GetSta = nor_get_busy_state(dFlash4AreaChip);
				}while(GetSta);				
			}
		}
	}
}

void TConfig::WriteAllUsbCfg()
{
	WriteProjectName();
	
	TextRender_string24(20,360, clBlue, PanelColor, "配置进度: 30%   ");
	
	WriteLoopCfgDevTabTypeDisp();
	WriteLoopDevUserCodeAssignType();
	WriteLoopDevArea();
	WriteLoopDevDescp();
	
	TextRender_string24(20,360, clBlue, PanelColor, "配置进度: 60%   ");
	
	WriteBusCfgKeyNormal();
	
	TextRender_string24(20,360, clBlue, PanelColor, "配置进度: 80%   ");
	
	WriteDirCfgKeyNormal();
	WriteDirKeyUserCodeAssignType();
	WriteDirKeyArea();
	WriteDirCfgDescp();
	
	TextRender_string24(20,360, clBlue, PanelColor, "配置进度: 90%   ");
	
	WriteAreaCfg();
	
	TextRender_string24(20,360, clBlue, PanelColor, "配置进度:100%   ");
	
	HAL_Delay(1500);
	UserRequestReset = 0x12345678;
}

void TConfig::WriteAllUartCfg()
{
	WriteProjectName();
	
	WriteLoopCfgDevTabTypeDisp();
	WriteLoopDevUserCodeAssignType();
	WriteLoopDevArea();
	WriteLoopDevDescp();
	
	WriteBusCfgKeyNormal();
	
	WriteDirCfgKeyNormal();
	WriteDirKeyUserCodeAssignType();
	WriteDirKeyArea();
	WriteDirCfgDescp();
	
	WriteAreaCfg();
}

void TConfig::rpyNoData()
{
	pCfgTxPkg->prm.head = TEST_UART_HEAD;
	pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
	pCfgTxPkg->prm.count = 0;
	pCfgTxPkg->prm.len = 4;

	pCfgTxPkg->prm.data[0] = pCfgRxPkg->prm.data[0];
	pCfgTxPkg->prm.data[1] = pCfgRxPkg->prm.data[1];
	pCfgTxPkg->prm.data[2] = 0;
			
	oBufProcess();
}

void TConfig::rpyData(unsigned char * pData, unsigned int len)
{
	unsigned int i;
	
	pCfgTxPkg->prm.head = TEST_UART_HEAD;
	pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
	pCfgTxPkg->prm.count = 0;
	pCfgTxPkg->prm.len = len;

	for(i=0;i<len;i++)
		pCfgTxPkg->prm.data[i] = pData[i];
			
	oBufProcess();
}

void TConfig::rpyCmd09()
{
	unsigned int i = 0,a = 0;
	
	pCfgTxPkg->prm.head = TEST_UART_HEAD;
	pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
	pCfgTxPkg->prm.count = 0;
	pCfgTxPkg->prm.len = dCtlCFG_BufCount;

	pConfigCtlDataTX->cfg.ctlAddr = MainCtl.fData.Split.MyNum;
	
	pConfigCtlDataTX->cfg.loopCount = 0;
	pConfigCtlDataTX->cfg.loopCfgTab = 0;
	for(i=0; i<LoopCountMax; i++){
		if(MainCtl.fData.Split.ExistTablePort[i]){
			pConfigCtlDataTX->cfg.loopCfgTab |= (((__UINT64_TYPE__)1)<<i);
			pConfigCtlDataTX->cfg.loopCount++;
		}
	}
	
	pConfigCtlDataTX->cfg.busCfgTab = 0;
	pConfigCtlDataTX->cfg.busPanelCount = 0;
	for(i=0; i<BusCountMax; i++){
		for(a=0;a<KeyInBusCountMax;a++){
			if(((1 == HandCtlBoard[i].IsMatchAreaAssign[a]) && (HandCtlBoard[i].Area[a])) || \ 
				((0 == HandCtlBoard[i].IsMatchAreaAssign[a]) && (HandCtlBoard[i].UcList[a].Full))){
				pConfigCtlDataTX->cfg.busCfgTab |= (1<<i);
				pConfigCtlDataTX->cfg.busPanelCount++;
				break;
			}
		}
	}
	
	pConfigCtlDataTX->cfg.dirCfgTab = 0;
	pConfigCtlDataTX->cfg.dirPanelCount = 0;
	for(i=0; i<DirCountMax; i++){
		for(a=0;a<KeyInDirCountMax;a++){
			if(DirectCtlBoard[i].UcList[a].Full){
				pConfigCtlDataTX->cfg.dirCfgTab |= (1<<i);
				pConfigCtlDataTX->cfg.dirPanelCount++;
				break;
			}
		}
	}
	
	pConfigCtlDataTX->cfg.areaCount = AreaSetting.Count;
	pConfigCtlDataTX->cfg.linkCount = InterLink.LinkCount;
	
	for(i=0;i<32;i++)
		pConfigCtlDataTX->cfg.desc[i] = PrjName[i];
			
	oBufProcess();
}

void TConfig::rpyCmd12()
{
	unsigned int aId,aDevId,i;
	
	aId = pCfgRxPkg->prm.data[0] - 1;
	aDevId = pCfgRxPkg->prm.data[1] - 1;
	
	if((aId >= LoopCountMax) || (aDevId >= DevInLoopCountMax)){
		rpyNoData();
	}else
	if((MainCtl.fData.Split.ExistTablePort[aId])&&((Port[aId].UcList[aDevId].Full))||(Port[aId].dTypeTable[aDevId])||(Port[aId].AssignType[aDevId])){
		pCfgTxPkg->prm.head = TEST_UART_HEAD;
		pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
		pCfgTxPkg->prm.count = 0;
		pCfgTxPkg->prm.len = 48;

		pConfigLoopDataTX->cfg.id = pCfgRxPkg->prm.data[0];
		pConfigLoopDataTX->cfg.devId = pCfgRxPkg->prm.data[1];
		pConfigLoopDataTX->cfg.devType = Port[aId].dTypeTable[aDevId];
		pConfigLoopDataTX->cfg.assignType = Port[aId].AssignType[aDevId];
		pConfigLoopDataTX->cfg.devProperty = Port[aId].FuncSetByte[aDevId];
		pConfigLoopDataTX->cfg.areaId = Port[aId].Area[aDevId];
		pConfigLoopDataTX->cfg.delayTime = 0;
		pConfigLoopDataTX->cfg.devNum = Port[aId].UcList[aDevId].Full;
		
		for(i=0;i<(32/4);i++){
			pConfigLoopDataTX->D32[4+i] = *(volatile unsigned int *)(dAddrSdRam_Descp + ((aId*256+aDevId)*32) + (i*4));
		}
				
		oBufProcess();
	}else{
		rpyNoData();
	}
}

void TConfig::rpyCmd22()
{
	unsigned int aId,aKeyId,i;
	
	aId = pCfgRxPkg->prm.data[0] - 1;
	aKeyId = pCfgRxPkg->prm.data[1] - 1;
	
	if((aId >= BusCountMax) || (aKeyId >= KeyInBusCountMax)){
		pCfgRxPkg->prm.data[2] = 0;
		pCfgRxPkg->prm.data[3] = HandCtlBoard[aId].KeyCount;
		rpyData(pCfgRxPkg->prm.data, 4);
	}else
	if((1 == HandCtlBoard[aId].IsMatchAreaAssign[aKeyId])||(HandCtlBoard[aId].Area[aKeyId]) || \ 
		(HandCtlBoard[aId].UcList[aKeyId].Full)){
		pCfgTxPkg->prm.head = TEST_UART_HEAD;
		pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
		pCfgTxPkg->prm.count = 0;
		pCfgTxPkg->prm.len = dBusKeyCFG_BufCount;

		pConfigBusDataTX->cfg.id = pCfgRxPkg->prm.data[0];
		pConfigBusDataTX->cfg.keyId = pCfgRxPkg->prm.data[1];
		pConfigBusDataTX->cfg.keyCount = HandCtlBoard[aId].KeyCount;
		pConfigBusDataTX->cfg.ctlMode = HandCtlBoard[aId].IsMatchAreaAssign[aKeyId];
		pConfigBusDataTX->cfg.devType = HandCtlBoard[aId].AssignType[aKeyId];
		pConfigBusDataTX->cfg.areaId = HandCtlBoard[aId].Area[aKeyId];
		pConfigBusDataTX->cfg.serNum = HandCtlBoard[aId].UcList[aKeyId].Full;
		
		for(i=0;i<32;i++){
			pConfigBusDataTX->cfg.desc[i] = 0;
		}
		
		oBufProcess();
	}else{
		pCfgRxPkg->prm.data[2] = 0;
		pCfgRxPkg->prm.data[3] = HandCtlBoard[aId].KeyCount;
		rpyData(pCfgRxPkg->prm.data, 4);
	}
}

void TConfig::rpyCmd32()
{
	unsigned int aId,aKeyId,i;
	
	aId = pCfgRxPkg->prm.data[0] - 1;
	aKeyId = pCfgRxPkg->prm.data[1] - 1;
	
	if((aId >= DirCountMax) || (aKeyId >= KeyInDirCountMax)){
		rpyNoData();
	}else
	if(DirectCtlBoard[aId].UcList[aKeyId].Full){
		pCfgTxPkg->prm.head = TEST_UART_HEAD;
		pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
		pCfgTxPkg->prm.count = 0;
		pCfgTxPkg->prm.len = dDirKeyCFG_BufCount;

		pConfigDirDataTX->cfg.id = pCfgRxPkg->prm.data[0];
		pConfigDirDataTX->cfg.keyId = pCfgRxPkg->prm.data[1];
		pConfigDirDataTX->cfg.isAble = DirectCtlBoard[aId].IsRegisted[aKeyId];
		pConfigDirDataTX->cfg.errDetFlag = DirectCtlBoard[aId].IsCheckError[aKeyId];
		pConfigDirDataTX->cfg.outProp = 0;//DirectCtlBoard[aId].IsOutputState[aKeyId];
		pConfigDirDataTX->cfg.ctlDevType = DirectCtlBoard[aId].AssignType[aKeyId];
		pConfigDirDataTX->cfg.areaId = DirectCtlBoard[aId].Area[aKeyId];
		pConfigDirDataTX->cfg.serNum = DirectCtlBoard[aId].UcList[aKeyId].Full;
		
		for(i=0;i<(32/4);i++){
			pConfigDirDataTX->D32[3+i] = \
			*(volatile unsigned int *)((dAddrSdRam_Descp + 40 * 8192) + ((aId*8+aKeyId+1)*32) + (i*4));
		}
				
		oBufProcess();
	}else{
		rpyNoData();
	}
}

void TConfig::rpyCmd42()
{
	unsigned int aId,aIdf,sAddr,i;
	
	aId = pCfgRxPkg->prm.data[1];
	aId<<=8;
	aId |= pCfgRxPkg->prm.data[0];
	
	if((0 == aId)||(aId > AreaCountMax)){
		rpyNoData();
		return;
	}
	
	aId = aId - 1;
	sAddr = dAddrSdRam_4Area + (aId * 64);
	aIdf = *(volatile unsigned int *)(sAddr);
	
	if(aIdf == 0xA8A7A6A5){
		pCfgTxPkg->prm.head = TEST_UART_HEAD;
		pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
		pCfgTxPkg->prm.count = 0;
		pCfgTxPkg->prm.len = 56;

		pConfigAreaDataTX->cfg.id = aId + 1;
		sAddr = dAddrSdRam_4Area + (aId * 64) + 4;
		AsiiToData(&pConfigAreaDataTX->cfg.serNum, ((char *)sAddr), 8);
			
		sAddr = dAddrSdRam_4Area + (aId * 64) + 16;
		for(i=0;i<16;i++){
			pConfigAreaDataTX->cfg.profile[i] = *(volatile unsigned char *)(sAddr + i);
		}
		
		sAddr = dAddrSdRam_4Area + (aId * 64) + 32;
		for(i=0;i<32;i++){
			pConfigAreaDataTX->cfg.desc[i] = *(volatile unsigned char *)(sAddr + i);
		}
				
		oBufProcess();
	}else{
		rpyNoData();
	}
}

void TConfig::rpyCmd52()
{
	unsigned int aId,aInx,sAddr,i,m;
	unsigned char aInCount = 0,aOutCount = 0;
	unsigned int onceFlag = 1;
	
	aId = pCfgRxPkg->prm.data[1];
	aId<<=8;
	aId |= pCfgRxPkg->prm.data[0];
	
	if((0 == aId)||(aId > LinkCountMax)){
		rpyNoData();
		return;
	}
	
	aInx = aId - 1;
	
	sAddr = dSdAddrLinkageExp + (aInx * 1024);
	if(((*(volatile unsigned char *)(sAddr + 0)) == 'T') && ((*(volatile unsigned char *)(sAddr + 1)) == 'R') && \
		((*(volatile unsigned char *)(sAddr + 2)) == 'U') && ((*(volatile unsigned char *)(sAddr + 3)) == 'E')){
		
		pUartCfgLinkDataTX.cfg.head = TEST_UART_HEAD;
		pUartCfgLinkDataTX.cfg.cmd = pCfgRxPkg->prm.cmd;
		pUartCfgLinkDataTX.cfg.count = 0;
		pUartCfgLinkDataTX.cfg.id = aId;
		
		pLinkEquationTX = (TEquation *)(sAddr + 16);
		
		//--------------------input--------------------------------
		pUartCfgLinkDataTX.cfg.len = 20;
		
		for(i=0;i<16;i++){
			if((0 != pLinkEquationTX->prm.inEqn[i][0])&&(0 != pLinkEquationTX->prm.inEqn[i][1])){
				if(onceFlag){
					onceFlag = 0;
					pUartCfgLinkDataTX.formula.isInput = 0x5A;
				}else{
					pUartCfgLinkDataTX.formula.isInput = 0x51;
				}
				
				pUartCfgLinkDataTX.formula.ord = i;
				for(m=0;m<8;m++){
					pUartCfgLinkDataTX.formula.eqn[m] = pLinkEquationTX->prm.inEqn[i][m];
				}
				pUartCfgLinkDataTX.formula.eqn[8] = ' ';
				pUartCfgLinkDataTX.formula.eqn[9] = pLinkEquationTX->prm.inEqn[i][8];
				pUartCfgLinkDataTX.formula.eqn[10] = pLinkEquationTX->prm.inEqn[i][9];
				pUartCfgLinkDataTX.formula.eqn[11] = pLinkEquationTX->prm.inEqn[i][10];
				pUartCfgLinkDataTX.formula.eqn[12] = ' ';
				pUartCfgLinkDataTX.formula.eqn[13] = pLinkEquationTX->prm.inEqn[i][11];
				pUartCfgLinkDataTX.formula.eqn[14] = pLinkEquationTX->prm.inEqn[i][12];
				pUartCfgLinkDataTX.formula.eqn[15] = pLinkEquationTX->prm.inEqn[i][13];
				aInCount++;
				iLinkTXBuf(pUartCfgLinkDataTX.D8);
			}
		}
		if(onceFlag){
			onceFlag = 0;
			pUartCfgLinkDataTX.formula.ord = 16;
			pUartCfgLinkDataTX.formula.isInput = 0x5A;
			for(m=0;m<16;m++){
				pUartCfgLinkDataTX.formula.eqn[m] = 0;
			}
			iLinkTXBuf(pUartCfgLinkDataTX.D8);
		}
		
		//--------------------output--------------------------------
		pUartCfgLinkDataTX.cfg.len = 20;
		pUartCfgLinkDataTX.formula.isInput = 0x15;
		for(i=0;i<16;i++){
			if((0 != pLinkEquationTX->prm.outEqn[i][0])&&(0 != pLinkEquationTX->prm.outEqn[i][1])){
				pUartCfgLinkDataTX.formula.ord = i;
				for(m=0;m<8;m++){
					pUartCfgLinkDataTX.formula.eqn[m] = pLinkEquationTX->prm.outEqn[i][m];
				}
				pUartCfgLinkDataTX.formula.eqn[8] = ' ';
				pUartCfgLinkDataTX.formula.eqn[9] = pLinkEquationTX->prm.outEqn[i][8];
				pUartCfgLinkDataTX.formula.eqn[10] = pLinkEquationTX->prm.outEqn[i][9];
				pUartCfgLinkDataTX.formula.eqn[11] = pLinkEquationTX->prm.outEqn[i][10];
				pUartCfgLinkDataTX.formula.eqn[12] = ' ';
				pUartCfgLinkDataTX.formula.eqn[13] = pLinkEquationTX->prm.outEqn[i][11];
				pUartCfgLinkDataTX.formula.eqn[14] = pLinkEquationTX->prm.outEqn[i][12];
				pUartCfgLinkDataTX.formula.eqn[15] = pLinkEquationTX->prm.outEqn[i][13];
				aOutCount++;
				iLinkTXBuf(pUartCfgLinkDataTX.D8);
			}
		}
		
		//--------------------send end--------------------------------
		pUartCfgLinkDataTX.cfg.len = 8;
		pUartCfgLinkDataTX.cfg.endFlag = 0xA0;
		pUartCfgLinkDataTX.cfg.inCount = aInCount;
		pUartCfgLinkDataTX.cfg.outCount = aOutCount;
		if((*(volatile unsigned char *)(sAddr + 4)) == 'M'){
			pUartCfgLinkDataTX.cfg.isMask = 1;
		}else{
			pUartCfgLinkDataTX.cfg.isMask = 0;
		}
		
		if((*(volatile unsigned char *)(sAddr + 5)) == 'S'){
			pUartCfgLinkDataTX.cfg.isStop = 1;
		}else{
			pUartCfgLinkDataTX.cfg.isStop = 0;
		}
		
		if((*(volatile unsigned char *)(sAddr + 6)) == 'F'){
			pUartCfgLinkDataTX.cfg.isForceAutoExec = 1;
		}else{
			pUartCfgLinkDataTX.cfg.isForceAutoExec = 0;
		}
		iLinkTXBuf(pUartCfgLinkDataTX.D8);
	}else{
		rpyNoData();
	}
}

void TConfig::rpyCmd62()
{
	unsigned int aId,aDevId,i,m;
	
	aId = pCfgRxPkg->prm.data[0] - 1;
	aDevId = pCfgRxPkg->prm.data[1] - 1;
	
	if((aId >= LoopCountMax) || (aDevId >= DevInLoopCountMax)){
		rpyNoData();
	}else
	if((MainCtl.fData.Split.ExistTablePort[aId] && Port[aId].ExistTable[aDevId]) && (DevDispType == Port[aId].dTypeTable[aDevId])){//是层显
		pCfgTxPkg->prm.head = TEST_UART_HEAD;
		pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
		pCfgTxPkg->prm.count = 0;
		pCfgTxPkg->prm.len = 20;
		
		for(m=0;m<16;m++){
			if(Port[aId].DisplayPadSet[m].Addr == pCfgRxPkg->prm.data[1]){
				break;
			}
		}
		if(16 == m){
			rpyNoData();
		}else{
			pConfigLoopDispDataTX->cfg.id = pCfgRxPkg->prm.data[0];
			pConfigLoopDispDataTX->cfg.devId = pCfgRxPkg->prm.data[1];
			pConfigLoopDispDataTX->cfg.devType = DevDispType;
			pConfigLoopDispDataTX->cfg.ord = m+1;
			pConfigLoopDispDataTX->cfg.dispMode = Port[aId].DisplayPadSet[m].CfgBits;
			pConfigLoopDispDataTX->cfg.areaId[0] = Port[aId].DisplayPadSet[m].AreaEx[0];
			pConfigLoopDispDataTX->cfg.areaId[1] = Port[aId].DisplayPadSet[m].AreaEx[1];
			pConfigLoopDispDataTX->cfg.areaId[2] = Port[aId].DisplayPadSet[m].AreaEx[2];
			pConfigLoopDispDataTX->cfg.devNum[0] = Port[aId].DisplayPadSet[m].UcList[0].Full;
			pConfigLoopDispDataTX->cfg.devNum[1] = Port[aId].DisplayPadSet[m].UcList[1].Full;
			
			oBufProcess();
		}
	}else{
		rpyNoData();
	}
}


void TConfig::iBufProcess()
{
	unsigned int i,a,b;
	unsigned int desCount;
	unsigned int aLinkCount;
	unsigned int aCNum,aP0,aP1;
	
	//if(uartRxFlag){
		//uartRxFlag = 0;
	
	if(usbCdcRxFlag){
		usbCdcRxFlag = 0;
		
		switch(pCfgRxPkg->prm.cmd){
			case cfgCMD04:
			pCfgTxPkg->prm.head = TEST_UART_HEAD;
			pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
			pCfgTxPkg->prm.count = 0;
			pCfgTxPkg->prm.len = 0;
			
			oBufProcess();
			break;
			
			case cfgCMD07://save cfg
			WriteAllLinkCfgToNorFlash();
			WriteAllUartCfg();
			
			haveSaveCfgFlag = 1;
			
			pCfgTxPkg->prm.head = TEST_UART_HEAD;
			pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
			pCfgTxPkg->prm.count = 0;
			pCfgTxPkg->prm.len = 4;
			pCfgTxPkg->prm.data[2] = 1;
			
			oBufProcess();
			break;
			
			case cfgCMD08:
			if(pCfgRxPkg->prm.len > dCtlCFG_BufCount){
				pCfgTxPkg->prm.head = TEST_UART_HEAD;
				pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
				pCfgTxPkg->prm.count = 0;
				pCfgTxPkg->prm.len = 4;
				pCfgTxPkg->prm.data[2] = 0;
			}else{
				
				CfgPrmInit();
				
				for(i=0;i<pCfgRxPkg->prm.len;i++){
					ctlPrm.D8[i] = pCfgRxPkg->prm.data[i];
				}
				pCfgTxPkg->prm.head = TEST_UART_HEAD;
				pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
				pCfgTxPkg->prm.count = 0;
				pCfgTxPkg->prm.len = 4;
				pCfgTxPkg->prm.data[2] = 1;
			}
			
			oBufProcess();
			break;
			
			case cfgCMD09:
			rpyCmd09();
			break;
			
			case cfgCMD11:
			if(pCfgRxPkg->prm.len > dLoopDevCFG_BufCount){
				pCfgTxPkg->prm.head = TEST_UART_HEAD;
				pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
				pCfgTxPkg->prm.count = 0;
				pCfgTxPkg->prm.len = 4;
				pCfgTxPkg->prm.data[2] = 0;
			}else{
				for(i=0;i<pCfgRxPkg->prm.len;i++){
					loopPrm.D8[i] = pCfgRxPkg->prm.data[i];
				}
				
				if(WriteLoopDevPrmToSdram(&loopPrm)){
					pCfgTxPkg->prm.data[2] = 0x00;
				}else{
					pCfgTxPkg->prm.data[2] = 0x01;
					WriteLoopCfgOneDevInTabToSdram(loopPrm.cfg.id, loopPrm.cfg.devId);
				}
				
				pCfgTxPkg->prm.head = TEST_UART_HEAD;
				pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
				pCfgTxPkg->prm.count = 0;
				pCfgTxPkg->prm.len = 4;
				pCfgTxPkg->prm.data[0] = pCfgRxPkg->prm.data[0];
				pCfgTxPkg->prm.data[1] = pCfgRxPkg->prm.data[1];
			}
			
			oBufProcess();
			break;
			
			case cfgCMD12:
			rpyCmd12();
			break;
			
			case cfgCMD15://save loop one dev cfg
			if(pCfgRxPkg->prm.len > dLoopDevCFG_BufCount){
				pCfgTxPkg->prm.head = TEST_UART_HEAD;
				pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
				pCfgTxPkg->prm.count = 0;
				pCfgTxPkg->prm.len = 4;
				pCfgTxPkg->prm.data[2] = 0;
			}else{
				for(i=0;i<pCfgRxPkg->prm.len;i++){
					loopPrm.D8[i] = pCfgRxPkg->prm.data[i];
				}
				
				if(WriteLoopDevPrmToSdram(&loopPrm)){
					pCfgTxPkg->prm.data[2] = 0x00;
				}else{
					pCfgTxPkg->prm.data[2] = 0x01;
					WriteLoopCfgOneDevInTabToSdram(loopPrm.cfg.id, loopPrm.cfg.devId);
					
					WriteLoopOneDevUserCodeAssignType(loopPrm.cfg.id, loopPrm.cfg.devId);
					WriteLoopOneDevArea(loopPrm.cfg.id, loopPrm.cfg.devId);
					WriteLoopOneDevDescp(loopPrm.cfg.id, loopPrm.cfg.devId);
				}
				
				pCfgTxPkg->prm.head = TEST_UART_HEAD;
				pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
				pCfgTxPkg->prm.count = 0;
				pCfgTxPkg->prm.len = 4;
				pCfgTxPkg->prm.data[0] = pCfgRxPkg->prm.data[0];
				pCfgTxPkg->prm.data[1] = pCfgRxPkg->prm.data[1];
			}
			
			oBufProcess();
			break;
			
			case cfgCMD17://save loop cfg
			if((pCfgRxPkg->prm.data[0] > 0)&&(pCfgRxPkg->prm.data[0] <= LoopCountMax)){
				WriteOneLoopDevUserCodeAssignType(pCfgRxPkg->prm.data[0]);
				WriteOneLoopDevArea(pCfgRxPkg->prm.data[0]);
				WriteOneLoopDevDescp(pCfgRxPkg->prm.data[0]);
				
				pCfgTxPkg->prm.data[2] = 0x01;
			}else
			if(0 == pCfgRxPkg->prm.data[0]){
				WriteLoopDevUserCodeAssignType();
				WriteLoopDevArea();
				WriteLoopDevDescp();
				
				pCfgTxPkg->prm.data[2] = 0x01;
			}else{
				pCfgTxPkg->prm.data[2] = 0x00;
			}
			
			pCfgTxPkg->prm.head = TEST_UART_HEAD;
			pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
			pCfgTxPkg->prm.count = 0;
			pCfgTxPkg->prm.len = 4;
			pCfgTxPkg->prm.data[0] = pCfgRxPkg->prm.data[0];
			
			oBufProcess();
			break;
			
			case cfgCMD21:
			if(pCfgRxPkg->prm.len > dBusKeyCFG_BufCount){
				pCfgTxPkg->prm.head = TEST_UART_HEAD;
				pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
				pCfgTxPkg->prm.count = 0;
				pCfgTxPkg->prm.len = 4;
				pCfgTxPkg->prm.data[2] = 0;
			}else{
				for(i=0;i<pCfgRxPkg->prm.len;i++){
					busPrm.D8[i] = pCfgRxPkg->prm.data[i];
				}
				
				if(WriteBusKeyPrmToSdram(&busPrm)){
					pCfgTxPkg->prm.data[2] = 0x00;
				}else{
					pCfgTxPkg->prm.data[2] = 0x01;
					WriteBusCfgOneKeyInTabToSdram(busPrm.cfg.id, busPrm.cfg.keyId);
				}	
				
				pCfgTxPkg->prm.head = TEST_UART_HEAD;
				pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
				pCfgTxPkg->prm.count = 0;
				pCfgTxPkg->prm.len = 4;
				pCfgTxPkg->prm.data[0] = pCfgRxPkg->prm.data[0];
				pCfgTxPkg->prm.data[1] = pCfgRxPkg->prm.data[1];
			}
			
			oBufProcess();
			break;
			
			case cfgCMD22:
			rpyCmd22();
			break;
			
			case cfgCMD25://save bus one key cfg
			if(pCfgRxPkg->prm.len > dBusKeyCFG_BufCount){
				pCfgTxPkg->prm.head = TEST_UART_HEAD;
				pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
				pCfgTxPkg->prm.count = 0;
				pCfgTxPkg->prm.len = 4;
				pCfgTxPkg->prm.data[2] = 0;
			}else{
				for(i=0;i<pCfgRxPkg->prm.len;i++){
					busPrm.D8[i] = pCfgRxPkg->prm.data[i];
				}
				
				if(WriteBusKeyPrmToSdram(&busPrm)){
					pCfgTxPkg->prm.data[2] = 0x00;
				}else{
					pCfgTxPkg->prm.data[2] = 0x01;
					WriteBusCfgOneKeyInTabToSdram(busPrm.cfg.id, busPrm.cfg.keyId);
					
					WriteBusOneKeyNormal(busPrm.cfg.id, busPrm.cfg.keyId);
				}
				
				pCfgTxPkg->prm.head = TEST_UART_HEAD;
				pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
				pCfgTxPkg->prm.count = 0;
				pCfgTxPkg->prm.len = 4;
				pCfgTxPkg->prm.data[0] = pCfgRxPkg->prm.data[0];
				pCfgTxPkg->prm.data[1] = pCfgRxPkg->prm.data[1];
			}
			
			oBufProcess();
			break;
			
			case cfgCMD27://save bus cfg
			if((pCfgRxPkg->prm.data[0] > 0)&&(pCfgRxPkg->prm.data[0] <= BusCountMax)){
				WriteOneBusKeyNormal(pCfgRxPkg->prm.data[0]);
				
				pCfgTxPkg->prm.data[2] = 0x01;
			}else
			if(0 == pCfgRxPkg->prm.data[0]){
				WriteBusCfgKeyNormal();
				
				pCfgTxPkg->prm.data[2] = 0x01;
			}else{
				pCfgTxPkg->prm.data[2] = 0x00;
			}
			
			pCfgTxPkg->prm.head = TEST_UART_HEAD;
			pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
			pCfgTxPkg->prm.count = 0;
			pCfgTxPkg->prm.len = 4;
			pCfgTxPkg->prm.data[0] = pCfgRxPkg->prm.data[0];
			
			oBufProcess();
			break;
			
			case cfgCMD31:
			if(pCfgRxPkg->prm.len > dDirKeyCFG_BufCount){
				pCfgTxPkg->prm.head = TEST_UART_HEAD;
				pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
				pCfgTxPkg->prm.count = 0;
				pCfgTxPkg->prm.len = 4;
				pCfgTxPkg->prm.data[2] = 0;
			}else{
				for(i=0;i<pCfgRxPkg->prm.len;i++){
					dirPrm.D8[i] = pCfgRxPkg->prm.data[i];
				}
				
				if(WriteDirKeyPrmToSdram(&dirPrm)){
					pCfgTxPkg->prm.data[2] = 0x00;
				}else{
					pCfgTxPkg->prm.data[2] = 0x01;
					WriteDirCfgOneKeyInTabToSdram(dirPrm.cfg.id, dirPrm.cfg.keyId);
				}	
				
				pCfgTxPkg->prm.head = TEST_UART_HEAD;
				pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
				pCfgTxPkg->prm.count = 0;
				pCfgTxPkg->prm.len = 4;
				pCfgTxPkg->prm.data[0] = pCfgRxPkg->prm.data[0];
				pCfgTxPkg->prm.data[1] = pCfgRxPkg->prm.data[1];
			}
			
			oBufProcess();
			break;
			
			case cfgCMD32:
			rpyCmd32();
			break;
			
			case cfgCMD35://save dir one key cfg
			if(pCfgRxPkg->prm.len > dDirKeyCFG_BufCount){
				pCfgTxPkg->prm.head = TEST_UART_HEAD;
				pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
				pCfgTxPkg->prm.count = 0;
				pCfgTxPkg->prm.len = 4;
				pCfgTxPkg->prm.data[2] = 0;
			}else{
				for(i=0;i<pCfgRxPkg->prm.len;i++){
					dirPrm.D8[i] = pCfgRxPkg->prm.data[i];
				}
				
				if(WriteDirKeyPrmToSdram(&dirPrm)){
					pCfgTxPkg->prm.data[2] = 0x00;
				}else{
					pCfgTxPkg->prm.data[2] = 0x01;
					WriteDirCfgOneKeyInTabToSdram(dirPrm.cfg.id, dirPrm.cfg.keyId);
					
					WriteDirOneKeyNormal(dirPrm.cfg.id, dirPrm.cfg.keyId);
					WriteDirOneKeyUserCodeAssignType(dirPrm.cfg.id, dirPrm.cfg.keyId);
					WriteDirOneKeyArea(dirPrm.cfg.id, dirPrm.cfg.keyId);
					WriteDirOneDescp(dirPrm.cfg.id, dirPrm.cfg.keyId);
				}	
				
				pCfgTxPkg->prm.head = TEST_UART_HEAD;
				pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
				pCfgTxPkg->prm.count = 0;
				pCfgTxPkg->prm.len = 4;
				pCfgTxPkg->prm.data[0] = pCfgRxPkg->prm.data[0];
				pCfgTxPkg->prm.data[1] = pCfgRxPkg->prm.data[1];
			}
			
			oBufProcess();
			break;
			
			case cfgCMD37://save dir cfg
			if((pCfgRxPkg->prm.data[0] > 0)&&(pCfgRxPkg->prm.data[0] <= DirCountMax)){
				WriteOneDirKeyNormal(pCfgRxPkg->prm.data[0]);
				WriteOneDirKeyUserCodeAssignType(pCfgRxPkg->prm.data[0]);
				WriteOneDirKeyArea(pCfgRxPkg->prm.data[0]);
				WriteOneDirDescp(pCfgRxPkg->prm.data[0]);
				
				pCfgTxPkg->prm.data[2] = 0x01;
			}else
			if(0 == pCfgRxPkg->prm.data[0]){
				WriteDirCfgKeyNormal();
				WriteDirKeyUserCodeAssignType();
				WriteDirKeyArea();
				WriteDirCfgDescp();
				
				pCfgTxPkg->prm.data[2] = 0x01;
			}else{
				pCfgTxPkg->prm.data[2] = 0x00;
			}
			
			pCfgTxPkg->prm.head = TEST_UART_HEAD;
			pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
			pCfgTxPkg->prm.count = 0;
			pCfgTxPkg->prm.len = 4;
			pCfgTxPkg->prm.data[0] = pCfgRxPkg->prm.data[0];
			
			oBufProcess();
			break;
			
			case cfgCMD41:
			if(pCfgRxPkg->prm.len > dAreaCFG_BufCount){
				pCfgTxPkg->prm.head = TEST_UART_HEAD;
				pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
				pCfgTxPkg->prm.count = 0;
				pCfgTxPkg->prm.len = 4;
				pCfgTxPkg->prm.data[2] = 0;
			}else{
				for(i=0;i<pCfgRxPkg->prm.len;i++){
					areaPrm.D8[i] = pCfgRxPkg->prm.data[i];
				}
				
				if(WriteAreaPrmToSdram(&areaPrm)){
					pCfgTxPkg->prm.data[2] = 0x00;
				}else{
					pCfgTxPkg->prm.data[2] = 0x01;
					areaCfgTab[areaPrm.cfg.id - 1] = 1;
				}	
				
				pCfgTxPkg->prm.head = TEST_UART_HEAD;
				pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
				pCfgTxPkg->prm.count = 0;
				pCfgTxPkg->prm.len = 4;
				pCfgTxPkg->prm.data[0] = pCfgRxPkg->prm.data[0];
				pCfgTxPkg->prm.data[1] = pCfgRxPkg->prm.data[1];
			}
			
			oBufProcess();
			break;
			
			case cfgCMD42:
			rpyCmd42();
			break;
			
			case cfgCMD51:
			if(pCfgRxPkg->prm.len > dLinkUartCFG_BufCount){
				linkPrm.cfg.uartFlag = 1;
			}
			
			if((0x51 == pUartCfgLinkDataRX->formula.isInput)||(0x5A == pUartCfgLinkDataRX->formula.isInput)){//input
				if(0x5A == pUartCfgLinkDataRX->formula.isInput){
					linkPrm.cfg.inTab = 0;
					linkPrm.cfg.outTab = 0;
				}
				if(16 == pUartCfgLinkDataRX->formula.ord){
					__nop();
				}else
				if(pUartCfgLinkDataRX->formula.ord < 16){
					linkPrm.cfg.inTab |= (1<<pUartCfgLinkDataRX->formula.ord);
					for(b=0;b<16;b++){
						linkPrm.cfg.eqn.prm.inEqn[pUartCfgLinkDataRX->formula.ord][b] = pUartCfgLinkDataRX->formula.eqn[b];
					}
					linkPrm.cfg.eqn.prm.inEqn[pUartCfgLinkDataRX->formula.ord][8] = linkPrm.cfg.eqn.prm.inEqn[pUartCfgLinkDataRX->formula.ord][9];
					linkPrm.cfg.eqn.prm.inEqn[pUartCfgLinkDataRX->formula.ord][9] = linkPrm.cfg.eqn.prm.inEqn[pUartCfgLinkDataRX->formula.ord][10];
					linkPrm.cfg.eqn.prm.inEqn[pUartCfgLinkDataRX->formula.ord][10] = linkPrm.cfg.eqn.prm.inEqn[pUartCfgLinkDataRX->formula.ord][11];
					linkPrm.cfg.eqn.prm.inEqn[pUartCfgLinkDataRX->formula.ord][11] = linkPrm.cfg.eqn.prm.inEqn[pUartCfgLinkDataRX->formula.ord][13];
					linkPrm.cfg.eqn.prm.inEqn[pUartCfgLinkDataRX->formula.ord][12] = linkPrm.cfg.eqn.prm.inEqn[pUartCfgLinkDataRX->formula.ord][14];
					linkPrm.cfg.eqn.prm.inEqn[pUartCfgLinkDataRX->formula.ord][13] = linkPrm.cfg.eqn.prm.inEqn[pUartCfgLinkDataRX->formula.ord][15];
					linkPrm.cfg.eqn.prm.inEqn[pUartCfgLinkDataRX->formula.ord][14] = 0;
					linkPrm.cfg.eqn.prm.inEqn[pUartCfgLinkDataRX->formula.ord][15] = 0;
				}else{
					linkPrm.cfg.uartFlag = 1;
				}
			}else
			if(0x15 == pUartCfgLinkDataRX->formula.isInput){//output
				if(pUartCfgLinkDataRX->formula.ord < 16){
					linkPrm.cfg.outTab |= (1<<pUartCfgLinkDataRX->formula.ord);
					for(b=0;b<16;b++){
						linkPrm.cfg.eqn.prm.outEqn[pUartCfgLinkDataRX->formula.ord][b] = pUartCfgLinkDataRX->formula.eqn[b];
					}
					linkPrm.cfg.eqn.prm.outEqn[pUartCfgLinkDataRX->formula.ord][8] = linkPrm.cfg.eqn.prm.outEqn[pUartCfgLinkDataRX->formula.ord][9];
					linkPrm.cfg.eqn.prm.outEqn[pUartCfgLinkDataRX->formula.ord][9] = linkPrm.cfg.eqn.prm.outEqn[pUartCfgLinkDataRX->formula.ord][10];
					linkPrm.cfg.eqn.prm.outEqn[pUartCfgLinkDataRX->formula.ord][10] = linkPrm.cfg.eqn.prm.outEqn[pUartCfgLinkDataRX->formula.ord][11];
					linkPrm.cfg.eqn.prm.outEqn[pUartCfgLinkDataRX->formula.ord][11] = linkPrm.cfg.eqn.prm.outEqn[pUartCfgLinkDataRX->formula.ord][13];
					linkPrm.cfg.eqn.prm.outEqn[pUartCfgLinkDataRX->formula.ord][12] = linkPrm.cfg.eqn.prm.outEqn[pUartCfgLinkDataRX->formula.ord][14];
					linkPrm.cfg.eqn.prm.outEqn[pUartCfgLinkDataRX->formula.ord][13] = linkPrm.cfg.eqn.prm.outEqn[pUartCfgLinkDataRX->formula.ord][15];
					linkPrm.cfg.eqn.prm.outEqn[pUartCfgLinkDataRX->formula.ord][14] = 0;
					linkPrm.cfg.eqn.prm.outEqn[pUartCfgLinkDataRX->formula.ord][15] = 0;
				}else{
					linkPrm.cfg.uartFlag = 1;
				}
			}else
			if((0xA0 == pUartCfgLinkDataRX->cfg.endFlag)||(0xA5 == pUartCfgLinkDataRX->cfg.endFlag)){//结束或保存
				if((pUartCfgLinkDataRX->cfg.inCount > 16)||(pUartCfgLinkDataRX->cfg.outCount > 16)){
					linkPrm.cfg.uartFlag = 1;
				}else{
					aLinkCount = 0;
					for(i=0;i<16;i++){
						if(linkPrm.cfg.inTab & (1<<i))aLinkCount++;
					}
					if(aLinkCount != pUartCfgLinkDataRX->cfg.inCount)linkPrm.cfg.uartFlag = 1;
					
					aLinkCount = 0;
					for(i=0;i<16;i++){
						if(linkPrm.cfg.outTab & (1<<i))aLinkCount++;
					}
					if(aLinkCount != pUartCfgLinkDataRX->cfg.outCount)linkPrm.cfg.uartFlag = 1;
					
					linkPrm.cfg.inCount = pUartCfgLinkDataRX->cfg.inCount;
					linkPrm.cfg.outCount = pUartCfgLinkDataRX->cfg.outCount;
				
					linkPrm.cfg.id = pUartCfgLinkDataRX->cfg.id;
					linkPrm.cfg.isStop = pUartCfgLinkDataRX->cfg.isStop;
					linkPrm.cfg.isMask = pUartCfgLinkDataRX->cfg.isMask;
					linkPrm.cfg.isForceAutoExec = pUartCfgLinkDataRX->cfg.isForceAutoExec;
				}
				
				if(linkPrm.cfg.uartFlag){//串口配置错误
					pCfgTxPkg->prm.data[2] = 0x00;
				}else{
					if((0 == linkPrm.cfg.id)||(linkPrm.cfg.id > LinkCountMax)){
						pCfgTxPkg->prm.data[2] = 0x00;
					}else{
						if(WriteLinkCfgToSdram(&linkPrm)){
							pCfgTxPkg->prm.data[2] = 0x00;
						}else{
							pCfgTxPkg->prm.data[2] = 0x01;
							
							linkCfgTab[linkPrm.cfg.id - 1] = 1;
							
							if(0xA5 == pUartCfgLinkDataRX->cfg.endFlag){//保存
								WriteOneLinkCfgToNorFlash(linkPrm.cfg.id);
							}
						}
					}
				}
				
				pCfgTxPkg->prm.head = TEST_UART_HEAD;
				pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
				pCfgTxPkg->prm.count = 0;
				pCfgTxPkg->prm.len = 4;
				pCfgTxPkg->prm.data[0] = (linkPrm.cfg.id & 0xFF);
				pCfgTxPkg->prm.data[1] = ((linkPrm.cfg.id>>8) & 0xFF);
				
				oBufProcess();
				
				linkPrm.cfg.uartFlag = 0;
			}
			
			break;
			
			case cfgCMD52:
			rpyCmd52();
			break;
			
			case cfgCMD57://save link cfg
			if(0xCC == pCfgRxPkg->prm.data[0]){//start send
				for(i = 0;i<LinkCountMax;i++){
					linkCfgTab[i] = 0;
				}
				
				pCfgTxPkg->prm.data[2] = 0x01;
			}else
			if(0xA5 == pCfgRxPkg->prm.data[0]){//end send and save
				WriteAllLinkCfgToNorFlash();
				
				pCfgTxPkg->prm.data[2] = 0x01;
			}else{
				pCfgTxPkg->prm.data[2] = 0x00;
			}
			
			pCfgTxPkg->prm.head = TEST_UART_HEAD;
			pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
			pCfgTxPkg->prm.count = 0;
			pCfgTxPkg->prm.len = 4;
			pCfgTxPkg->prm.data[0] = pCfgRxPkg->prm.data[0];
			
			oBufProcess();
			break;
			
			case cfgCMD61:
			if(pCfgRxPkg->prm.len > dLoopDispCFG_BufCount){
				pCfgTxPkg->prm.head = TEST_UART_HEAD;
				pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
				pCfgTxPkg->prm.count = 0;
				pCfgTxPkg->prm.len = 4;
				pCfgTxPkg->prm.data[2] = 0;
			}else{
				for(i=0;i<pCfgRxPkg->prm.len;i++){
					loopDispPrm.D8[i] = pCfgRxPkg->prm.data[i];
				}
				
				if(WriteLoopDevDispPrmToSdram(&loopDispPrm)){
					pCfgTxPkg->prm.data[2] = 0x00;
				}else{
					pCfgTxPkg->prm.data[2] = 0x01;
					loopDevDispCount[loopDispPrm.cfg.id]++;
					loopCfgDevDispExistTab[loopDispPrm.cfg.id - 1] = 1;
				}	
				
				pCfgTxPkg->prm.head = TEST_UART_HEAD;
				pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
				pCfgTxPkg->prm.count = 0;
				pCfgTxPkg->prm.len = 4;
				pCfgTxPkg->prm.data[0] = pCfgRxPkg->prm.data[0];
				pCfgTxPkg->prm.data[1] = pCfgRxPkg->prm.data[1];
			}
			
			oBufProcess();
			break;
			
			case cfgCMD62:
			rpyCmd62();
			break;
			
			case cfgCMD65:
			if(pCfgRxPkg->prm.len > dLoopDispCFG_BufCount){
				pCfgTxPkg->prm.head = TEST_UART_HEAD;
				pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
				pCfgTxPkg->prm.count = 0;
				pCfgTxPkg->prm.len = 4;
				pCfgTxPkg->prm.data[2] = 0;
			}else{
				for(i=0;i<pCfgRxPkg->prm.len;i++){
					loopDispPrm.D8[i] = pCfgRxPkg->prm.data[i];
				}
				
				if(WriteLoopDevDispPrmToSdram(&loopDispPrm)){
					pCfgTxPkg->prm.data[2] = 0x00;
				}else{
					pCfgTxPkg->prm.data[2] = 0x01;
					
					loopCfgDevDispExistTab[loopDispPrm.cfg.id - 1] = 1;
					
					WriteLoopCfgOneDisp(loopDispPrm.cfg.id, loopDispPrm.cfg.ord);
				}	
				
				pCfgTxPkg->prm.head = TEST_UART_HEAD;
				pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
				pCfgTxPkg->prm.count = 0;
				pCfgTxPkg->prm.len = 4;
				pCfgTxPkg->prm.data[0] = pCfgRxPkg->prm.data[0];
				pCfgTxPkg->prm.data[1] = pCfgRxPkg->prm.data[1];
			}
			
			oBufProcess();
			break;
			
			case cfgCMD67:
			if(0xCC == pCfgRxPkg->prm.data[0]){//start send
				for(i = 0;i <= LoopCountMax;i++){
					loopDevDispCount[i] = 0;
					loopCfgDevDispExistTab[i] = 0;
				}
				
				pCfgTxPkg->prm.data[2] = 0x01;
			}else
			if(0xA5 == pCfgRxPkg->prm.data[0]){//end send and save
				WriteLoopCfgAllDisp();
				
				pCfgTxPkg->prm.data[2] = 0x01;
			}else{
				pCfgTxPkg->prm.data[2] = 0x00;
			}
			
			pCfgTxPkg->prm.head = TEST_UART_HEAD;
			pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
			pCfgTxPkg->prm.count = 0;
			pCfgTxPkg->prm.len = 4;
			pCfgTxPkg->prm.data[0] = pCfgRxPkg->prm.data[0];
			
			oBufProcess();
			break;
			
			case cfgCMD71:
			aCNum = pCfgRxPkg->prm.data[0];
			aP0 = pCfgRxPkg->prm.data[1];
			aP1 = pCfgRxPkg->prm.data[2];
			
			if((pCfgRxPkg->prm.len > 36) || (aCNum > dNETWORK_MACHINE_COUNT) || (0 == aCNum) || (0 == aP0) || (0 == aP1)){	
				pCfgTxPkg->prm.head = TEST_UART_HEAD;
				pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
				pCfgTxPkg->prm.count = 0;
				pCfgTxPkg->prm.len = 4;
				pCfgTxPkg->prm.data[0] = pCfgRxPkg->prm.data[1];
				pCfgTxPkg->prm.data[1] = pCfgRxPkg->prm.data[2];
				pCfgTxPkg->prm.data[2] = 0;
			}else{
				//aP1 = aP1 - 1;
				if((aP0 <= LoopCountMax) && (aP1 <= DevInLoopCountMax)){
					if(0xA5 == pCfgRxPkg->prm.data[3]){
						for(i=0;i<256;i++){
							fNetworkReg.PortDevDescp.Body[i].D32[0] = 0;
							fNetworkReg.PortDevDescp.Body[i].D32[1] = 0;
							fNetworkReg.PortDevDescp.Body[i].D32[2] = 0;
							fNetworkReg.PortDevDescp.Body[i].D32[3] = 0;
							fNetworkReg.PortDevDescp.Body[i].D32[4] = 0;
							fNetworkReg.PortDevDescp.Body[i].D32[5] = 0;
							fNetworkReg.PortDevDescp.Body[i].D32[6] = 0;
							fNetworkReg.PortDevDescp.Body[i].D32[7] = 0;
						}
					}
					for(i=0;i<(pCfgRxPkg->prm.len - 4);i++){
						fNetworkReg.PortDevDescp.Body[aP1].D8[i] = pCfgRxPkg->prm.data[i+4];
					}
					for(i = (pCfgRxPkg->prm.len - 4);i < 32; i++){
						fNetworkReg.PortDevDescp.Body[aP1].D8[pCfgRxPkg->prm.len - 4] = 0;
					}
					if(0x5C == pCfgRxPkg->prm.data[3]){
						fNetworkReg.SavePortDescp(aCNum, aP0);
						fNetworkReg.ReadPortDescp(aCNum, aP0);
					}
				
					pCfgTxPkg->prm.head = TEST_UART_HEAD;
					pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
					pCfgTxPkg->prm.count = 0;
					pCfgTxPkg->prm.len = 4;
					pCfgTxPkg->prm.data[0] = pCfgRxPkg->prm.data[1];
					pCfgTxPkg->prm.data[1] = pCfgRxPkg->prm.data[2];
					pCfgTxPkg->prm.data[2] = 1;
				}else
				if((aP0 >= 66) && (aP0 < (66 + DirCountMax)) && (aP1 <= KeyInDirCountMax)){
					aP0 = aP0 - 66;
					if(0xA5 == pCfgRxPkg->prm.data[3]){
						for(i=0;i<256;i++){
							fNetworkReg.PortDevDescp.Body[i].D32[0] = 0;
							fNetworkReg.PortDevDescp.Body[i].D32[1] = 0;
							fNetworkReg.PortDevDescp.Body[i].D32[2] = 0;
							fNetworkReg.PortDevDescp.Body[i].D32[3] = 0;
							fNetworkReg.PortDevDescp.Body[i].D32[4] = 0;
							fNetworkReg.PortDevDescp.Body[i].D32[5] = 0;
							fNetworkReg.PortDevDescp.Body[i].D32[6] = 0;
							fNetworkReg.PortDevDescp.Body[i].D32[7] = 0;
						}
					}
					for(i=0;i<(pCfgRxPkg->prm.len - 4);i++){
						fNetworkReg.PortDevDescp.Body[aP0*KeyInDirCountMax + aP1].D8[i] = pCfgRxPkg->prm.data[i+4];
					}
					if((pCfgRxPkg->prm.len - 4) < 32){
						fNetworkReg.PortDevDescp.Body[aP0*KeyInDirCountMax + aP1].D8[pCfgRxPkg->prm.len - 4] = 0;
					}
					if(0x35 == pCfgRxPkg->prm.data[3]){
						fNetworkReg.SaveDirectPadDescp(aCNum);
						fNetworkReg.ReadDirectPadDescp(aCNum);
					}
					
					pCfgTxPkg->prm.head = TEST_UART_HEAD;
					pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
					pCfgTxPkg->prm.count = 0;
					pCfgTxPkg->prm.len = 4;
					pCfgTxPkg->prm.data[0] = pCfgRxPkg->prm.data[1];
					pCfgTxPkg->prm.data[1] = pCfgRxPkg->prm.data[2];
					pCfgTxPkg->prm.data[2] = 1;
				}else{
					pCfgTxPkg->prm.head = TEST_UART_HEAD;
					pCfgTxPkg->prm.cmd = pCfgRxPkg->prm.cmd;
					pCfgTxPkg->prm.count = 0;
					pCfgTxPkg->prm.len = 4;
					pCfgTxPkg->prm.data[0] = pCfgRxPkg->prm.data[1];
					pCfgTxPkg->prm.data[1] = pCfgRxPkg->prm.data[2];
					pCfgTxPkg->prm.data[2] = 0;
				}
			}
			
			oBufProcess();
			break;
			
			case cfgCMD77:
				//(void)LoadCfgFile();
			break;
			
			case cfgCMD7F://restart ctl
			if(haveSaveCfgFlag){
				haveSaveCfgFlag = 0;
				HAL_Delay(500);
				UserRequestReset = 0x12345678;
			}
			while(1);
			
			default:
			break;
		}
	}
}

void TConfig::Task10Ms()
{
	static unsigned int tick = 0;
	tick++;
	if(tick > 2){
		tick = 0;
		oLinkTXBuf();
	}
}

void TConfig::Task1000Ms()
{
	;
}

void TConfig::OperationTask()
{
	iBufProcess();
}
