#include "includes.h"

E_MCU_DEV_INFO g_mcu_dev;
ENERGY_SAVE_PARA e_save;

U8_T EEPROM_CheckSum(U8_T *data,U16_T len)
{
	U8_T data_sum = 0;
	
	for(U16_T i = 0;i<len;i++)
	{
		data_sum += data[i];
	}
	return data_sum;
}


void EEPROM_Init(void)
{
	U8_T rev = 0;	
	U8_T eng_rev = 0;
	
	EnIFCClk; //使能 IFC 时钟
	IFC->MR |= 0x10002; //高速模式，延迟 2 个周期

	delay_nms(10);
	
	
	//// 0、读取boot设备相关信息
	Dbg_Println(DBG_BIT_SYS_STATUS,"/************> Init Read MCU_DEV <*****************/");
	memset(&g_mcu_dev,0,sizeof(E_MCU_DEV_INFO));
	rev = EEPROM_ReadMCUDevInfo(&g_mcu_dev);
	if(g_Dip.addr != g_mcu_dev.dev_addr){
		g_mcu_dev.dev_addr = g_Dip.addr;
		EEPROM_WriteMCUDevInfo(&g_mcu_dev);
	}
	
	if(rev == 0x00){
		//读取成功，开始校验以下参数
		EEPROM_Validate_MCUDevInfo(&g_mcu_dev);
	}else{
		//读取失败，恢复默认参数
		EEPROM_Default_MCUDevInfo(&g_mcu_dev);
#if DBG_LOG_EN
		SYSCON_IWDCNT_Reload(); 
		Dbg_Println(DBG_BIT_SYS_STATUS,"EE Use Defalut Para");

		Dbg_Println(DBG_BIT_SYS_STATUS,"EE DevBootVer:%d",g_mcu_dev.dev_boot_ver);
		Dbg_Println(DBG_BIT_SYS_STATUS,"EE DevNameLen:%d",g_mcu_dev.dev_name_len);
		Dbg_Println(DBG_BIT_SYS_STATUS,"EE DevName:%s",g_mcu_dev.dev_name);
#endif	
	}		
	
	
	////// 1、读取电能参数
	Dbg_Println(DBG_BIT_SYS_STATUS,"/************> Init Read EngPara <*****************/");
	memset(&e_save,0,sizeof(ENERGY_SAVE_PARA));
	eng_rev = EEOROM_ENERGY_ReadSet(&e_save);		//电能检测参数,读取
#if DBG_LOG_EN
		Dbg_Println(DBG_BIT_SYS_STATUS,"Init1 ReadEngPara Rev:%d",eng_rev);
#endif		
	if(eng_rev == 0)
	{
		//读取成功，第一次上电读取必定失败
	}else{
		//读取失败恢复默认值
		EEPROM_Default_ENERGY(&e_save);
	}

	
	//// 3、能耗检测参数初始化
	BLV_DetEnergy_Default();	

}

/*****************************************
* Function Name  : EEOROM_ENERGY_ReadSet
* Description    : 读取电能检测参数
* Parameter		 :
* 		info	：读取参数指针
*******************************************************************/
U8_T EEOROM_ENERGY_ReadSet(ENERGY_SAVE_PARA *info)
{
	U8_T read_info[6];
	U8_T para_data[EEPROM_ENERGY_SIZE];
	U16_T read_len = 0;
	
	memset(read_info,0,sizeof(read_info));
	memset(para_data,0,sizeof(para_data));
	
	ReadDataArry_U8(EEPROM_ENERGE_PARA_SaveAddr,4,read_info);
	
	if(read_info[0] == EEPROM_ENERGY_SAVE_FLAG){
		read_len = read_info[2];
		read_len <<= 8;
		read_len |= read_info[1];
		
		if(read_len <= EEPROM_ENERGY_SIZE){
			
			ReadDataArry_U8(EEPROM_ENERGE_PARA_SaveAddr+EEPROM_Offset_Data,read_len,para_data);
			if(EEPROM_CheckSum(para_data,sizeof(ENERGY_SAVE_PARA)) == read_info[3]){
				//校验成功
				memcpy((uint8_t *)info,para_data,sizeof(ENERGY_SAVE_PARA));
				
#if DBG_LOG_EN
				SYSCON_IWDCNT_Reload();
				Dbg_Println(DBG_BIT_SYS_STATUS,"Eng V_K:%f",e_save.v_k_val);
				Dbg_Println(DBG_BIT_SYS_STATUS,"Eng IA_K:%f",e_save.ia_k_val);
				Dbg_Println(DBG_BIT_SYS_STATUS,"Eng IB_K:%f",e_save.ib_k_val);
				Dbg_Println(DBG_BIT_SYS_STATUS,"Eng PA_K:%f",e_save.pa_k_val);
				Dbg_Println(DBG_BIT_SYS_STATUS,"Eng PB_K:%f",e_save.pb_k_val);
				Dbg_Println(DBG_BIT_SYS_STATUS,"Eng I_OffSet:%f",e_save.i_OffSetVal);
				Dbg_Println(DBG_BIT_SYS_STATUS,"Eng P_OffSet:%f",e_save.p_OffSetVal);
				SYSCON_IWDCNT_Reload();
				Dbg_Println(DBG_BIT_SYS_STATUS,"Eng save_max:%d",e_save.eng_save_max);
				Dbg_Println(DBG_BIT_SYS_STATUS,"Eng save_offset:%d",e_save.eng_save_offset);
				Dbg_Println(DBG_BIT_SYS_STATUS,"Eng collect_tim:%d",e_save.collect_tim);
				Dbg_Println(DBG_BIT_SYS_STATUS,"Eng report_tim:%d",e_save.report_tim);
				Dbg_Println(DBG_BIT_SYS_STATUS,"Eng SaveFlash_Tim:%d",e_save.saveFlash_tim);				
				
				SYSCON_IWDCNT_Reload();
#endif
				return 0x00;
			}
		}
	}
	
	return 0x01;	
	
}
/*******************************************************************************
* Function Name  : EEOROM_ENERGY_WriteSet 
* Description    : 将电能设置参数写入到EEPROM中
*******************************************************************************/
U8_T EEOROM_ENERGY_WriteSet(ENERGY_SAVE_PARA *info)
{
	U8_T save_data[EEPROM_ENERGY_SIZE + 6];
	U16_T save_len = sizeof(ENERGY_SAVE_PARA);
	
	if(save_len >= EEPROM_ENERGY_SIZE) save_len = EEPROM_ENERGY_SIZE;
	
	save_data[0] = EEPROM_ENERGY_SAVE_FLAG;
	save_data[1] = save_len & 0xFF;
	save_data[2] = (save_len >> 8) & 0xFF;
	
	memcpy(&save_data[4],(uint8_t *)info,save_len);
	
	save_data[3] = EEPROM_CheckSum(&save_data[4],save_len);
	
	save_len+=4;
	
	Page_ProgramData(EEPROM_ENERGE_PARA_SaveAddr,save_len,save_data);
	
	return 0;
}
/*******************************************************************************
* Function Name  : EEPROM_ENERGY_Validate
* Description    : 比对当前参数，确认是否有改变需要写入
*******************************************************************************/
void EEPROM_ENERGY_Validate(void)
{
	U8_T save_flag = 0;
	
	if(e_save.ia_k_val != g_Eng.IA_K_Val)
	{
		e_save.ia_k_val = g_Eng.IA_K_Val;
		save_flag = 1;
	}

	if(e_save.ib_k_val != g_Eng.IB_K_Val)
	{
		e_save.ib_k_val = g_Eng.IB_K_Val;
		save_flag = 1;
	}
	
	if(e_save.v_k_val != g_Eng.V_K_Val)
	{
		e_save.v_k_val = g_Eng.V_K_Val;
		save_flag = 1;
	}

	if(e_save.pa_k_val != g_Eng.PA_K_Val)
	{
		e_save.pa_k_val = g_Eng.PA_K_Val;
		save_flag = 1;
	}

	if(e_save.pb_k_val != g_Eng.PB_K_Val)
	{
		e_save.pb_k_val = g_Eng.PB_K_Val;
		save_flag = 1;
	}

	if(e_save.i_OffSetVal != g_Eng.I_OffSetVal)
	{
		e_save.i_OffSetVal = g_Eng.I_OffSetVal;
		save_flag = 1;
	}

	if(e_save.p_OffSetVal != g_Eng.P_OffSetVal)
	{
		e_save.p_OffSetVal = g_Eng.P_OffSetVal;
		save_flag = 1;
	}
	
	if(e_save.collect_tim != g_Eng.Collect_Tim)
	{
		e_save.collect_tim = g_Eng.Collect_Tim;
		save_flag = 1;
	}	
	
	if(e_save.saveFlash_tim != g_Eng.SaveFlash_Tim)
	{
		e_save.saveFlash_tim = g_Eng.SaveFlash_Tim;
		save_flag = 1;
	}

	if(e_save.report_tim != g_Eng.Report_Tim)
	{
		e_save.report_tim = g_Eng.Report_Tim;
		save_flag = 1;
	}

	if(save_flag == 0x01)
	{
		EEOROM_ENERGY_WriteSet(&e_save);
		
		#if DBG_LOG_EN
		SYSCON_IWDCNT_Reload();
		Dbg_Println(DBG_BIT_SYS_STATUS,"Need Save EngParam");
		Dbg_Println(DBG_BIT_SYS_STATUS,"V_K:%f",e_save.v_k_val);
		Dbg_Println(DBG_BIT_SYS_STATUS,"IA_K:%f",e_save.ia_k_val);
		Dbg_Println(DBG_BIT_SYS_STATUS,"IB_K:%f",e_save.ib_k_val);
		Dbg_Println(DBG_BIT_SYS_STATUS,"PA_K:%f",e_save.pa_k_val);
		Dbg_Println(DBG_BIT_SYS_STATUS,"PB_K:%f",e_save.pb_k_val);
		Dbg_Println(DBG_BIT_SYS_STATUS,"I_OS:%f",e_save.i_OffSetVal);
		Dbg_Println(DBG_BIT_SYS_STATUS,"P_OS:%f",e_save.p_OffSetVal);
		SYSCON_IWDCNT_Reload();
		#endif		
	}
	else 
	{
		#if DBG_LOG_EN
		Dbg_Println(DBG_BIT_SYS_STATUS,"NO Need Save EngParam");
		#endif		
	}
}
/*******************************************************************************
* Function Name  : EEPROM_Default_ENERGY 
* Description    : EEPROM中参数恢复默认值，且将默认参数保存至EEPROM中
*******************************************************************************/
void EEPROM_Default_ENERGY(ENERGY_SAVE_PARA *info)
{
	/*APP 区域*/
	info->ia_k_val = 1;
	info->ib_k_val = 1;
	info->v_k_val = 2;
	info->pa_k_val = 1;
	info->pb_k_val = 1;
	info->i_OffSetVal = 0;
	info->p_OffSetVal = 0;
	info->eng_save_max = EEPROM_SAVEADDR_CNT;
	info->eng_save_offset = 1;	//存储偏移编号从1开始
	info->saveFlash_tim = EEPROM_SAVETIME;
	info->collect_tim = EEPROM_COLLTIME;
	info->report_tim = EEPROM_REPTIME;
	
	
//	EEOROM_ENERGY_WriteSet(info);
	
#if DBG_LOG_EN
	Dbg_Println(DBG_BIT_SYS_STATUS,"Energy Use Default Para");
	SYSCON_IWDCNT_Reload();
	Dbg_Println(DBG_BIT_SYS_STATUS,"Default V_K:%f",e_save.v_k_val);
	Dbg_Println(DBG_BIT_SYS_STATUS,"Default IA_K:%f",e_save.ia_k_val);
	Dbg_Println(DBG_BIT_SYS_STATUS,"Default IB_K:%f",e_save.ib_k_val);
	Dbg_Println(DBG_BIT_SYS_STATUS,"Default PA_K:%f",e_save.pa_k_val);
	Dbg_Println(DBG_BIT_SYS_STATUS,"Default PB_K:%f",e_save.pb_k_val);
	Dbg_Println(DBG_BIT_SYS_STATUS,"Default I_OffSet:%f",e_save.i_OffSetVal);
	Dbg_Println(DBG_BIT_SYS_STATUS,"Default P_OffSet:%f",e_save.p_OffSetVal);
	SYSCON_IWDCNT_Reload();
	Dbg_Println(DBG_BIT_SYS_STATUS,"Default save_max:%d",e_save.eng_save_max);
	Dbg_Println(DBG_BIT_SYS_STATUS,"Default save_offset:%d",e_save.eng_save_offset);
	Dbg_Println(DBG_BIT_SYS_STATUS,"Default collect_tim:%d",e_save.collect_tim);
	Dbg_Println(DBG_BIT_SYS_STATUS,"Default report_tim:%d",e_save.report_tim);
	Dbg_Println(DBG_BIT_SYS_STATUS,"Default saveFlash_tim:%d",e_save.saveFlash_tim);				
	
	SYSCON_IWDCNT_Reload();
	
#endif		
	
}
/*********************************************************************
 * Function Name  : EEPROM_ReadTotalEng
 * Description    : 读取总电量、存储总次数
 *********************************************************************/
U8_T EEPROM_ReadTotalEng(ENERGY_SAVE *info)
{
	U32_T temp_addr = EEPROM_EngSave_StartAddr;		//存储地址
	U8_T read_info[10];								//帧头
	U8_T para_data[EEPROM_ENERGY_SIZE];				//存储数据
	UINT16 read_len = 0;							//存储数据长度
	uint32_t save_block = 0;						//存储扇区编号
	
	memset(read_info,0,sizeof(read_info));
	memset(para_data,0,sizeof(para_data));
	
	if(e_save.eng_save_offset == 0) return 0x01;	//起始扇区编号从1开始，0为未存储过
	
	save_block = e_save.eng_save_offset;			//从其他扇区读取的总电量扇区读取编号
	
	temp_addr = EEPROM_EngSave_StartAddr + (save_block - 1) * EEPROM_OFFSET_ADDRSIZE;		//通过扇区编号计算当前总能耗存储扇区地址
	
	if( ( temp_addr < EEPROM_EngSave_StartAddr ) || (temp_addr > EEPROM_EngSave_EndAddr) )	return 0x02;
	
	ReadDataArry_U8(temp_addr,4,read_info);			//读取目标地址的标志位和存储数据长度
	
	if(read_info[0] != EEPROM_ENERGY_SAVE_FLAG)	return 0x03;	
	
	read_len = read_info[2];
	read_len <<= 8;
	read_len |= read_info[1];
	
	if(read_len <= EEPROM_ENERGY_SIZE){
		
		ReadDataArry_U8(temp_addr+EEPROM_Offset_Data,read_len,para_data);
		
		if(EEPROM_CheckSum(para_data,sizeof(ENERGY_SAVE)) == read_info[3]){
			//校验成功
			memcpy((uint8_t *)info,para_data,sizeof(ENERGY_SAVE));
			
		#if DBG_LOG_EN
			Dbg_Println(DBG_BIT_SYS_STATUS,"TotalEnergy ReadSucc");
		#endif				
			return 0x00;
		}else{
		#if DBG_LOG_EN
			Dbg_Println(DBG_BIT_SYS_STATUS,"TotalEnergy ReadFail");
		#endif				
			return 0x04;
		}
		
	}

	return 0x00;
}
/*********************************************************************
 * Function Name  : EEPROM_WriteTotalEng
 * Description    : 写入总电量
 *********************************************************************/
U8_T EEPROM_WriteTotalEng(ENERGY_SAVE *info)
{
	U8_T save_data[EEPROM_ENERGY_SIZE + 6];
	U8_T savepara_falg = 0x00;
	U32_T temp_addr = EEPROM_EngSave_StartAddr;	
	U16_T save_len = sizeof(ENERGY_SAVE);
	uint32_t save_block = 0,save_cnt = 0;
	
	save_block = e_save.eng_save_offset;			//从其他扇区读取的总电量扇区读取编号
	save_cnt = info->eng_save_cnt;					//单扇区最大存储次数
	
	if(e_save.eng_save_offset == 0) return 0x01;				//起始扇区编号从1开始，0为未存储过
	if(e_save.eng_save_max == 0) return 0x02;
	
	if(info->eng_save_cnt >= 0xFFFFFFFF){
		return 0x03;
	}
	info->eng_save_cnt++;
	
	if(info->eng_save_cnt > e_save.eng_save_max)
	{
		e_save.eng_save_offset = (info->eng_save_cnt/e_save.eng_save_max)+1;	//更新扇区编号
		savepara_falg = 0x01;
		
	}
	
	//通过扇区编号计算当前总能耗存储扇区地址
	temp_addr = EEPROM_EngSave_StartAddr + (e_save.eng_save_offset - 1) * EEPROM_OFFSET_ADDRSIZE;		
	
	if(temp_addr > EEPROM_EngSave_EndAddr )	//超限，不再改变存储扇区
	{
		e_save.eng_save_offset = save_block; 
		savepara_falg = 0x00;
		//info->eng_save_cnt = save_cnt;
		temp_addr = EEPROM_EngSave_EndAddr;
	}
	
	if(save_len >= EEPROM_ENERGY_SIZE) save_len = EEPROM_ENERGY_SIZE;
	
	save_data[0] = EEPROM_ENERGY_SAVE_FLAG;
	save_data[1] = save_len & 0xFF;
	save_data[2] = (save_len >> 8) & 0xFF;
	
	memcpy(&save_data[4],(uint8_t *)info,save_len);
	
	save_data[3] = EEPROM_CheckSum(&save_data[4],save_len);
	
	save_len+=4;
	
	Page_ProgramData(temp_addr,save_len,save_data);
	
	
	if(savepara_falg == 0x01)
	{
		EEOROM_ENERGY_WriteSet(&e_save);
	}
	
	return 0;	
}




/***********************************************************************************************************************************/


/*******************************************************************************
* Function Name  : EEPROM_ReadPara 
* Description    : 读取参数
* Parameter		 :
* 		info	：读取参数指针
*******************************************************************************/
U8_T EEPROM_ReadPara(void)
{
	U32_T temp_addr = EEPROM_PARA_SaveAddr;
	U8_T read_info[10];
	U8_T para_data[EEPROM_PARA_Size];
	UINT16 read_len = 0;
	
	memset(read_info,0,sizeof(read_info));
	memset(para_data,0,sizeof(para_data));
	
	ReadDataArry_U8(temp_addr,4,read_info);
	
	if(read_info[0] == EEPROM_SAVE_Flag){
		read_len = read_info[2];
		read_len <<= 8;
		read_len |= read_info[1];
		
		if((read_len <= EEPROM_PARA_Size) && (read_len == 0x0A)){	//校验要保存数据的长度
			temp_addr += EEPROM_Data_Offset;
			ReadDataArry_U8(temp_addr,read_len,para_data);
			if(EEPROM_CheckSum(para_data,read_len) == read_info[3]){
				//校验成功 - 读取参数

				return 0x00;
			}
		}
	}
	
	Dbg_Println(DBG_BIT_SYS_STATUS,"EEPROM_ReadPara Default!");
	//数据读取失败，使用默认参数

	return 0x01;
}

/*******************************************************************************
* Function Name  : EEPROM_ValidateWrite 
* Description    : 校验写入参数
*******************************************************************************/
U8_T EEPROM_ValidateWrite(U32_T Eeprom_Write_SaveAddr,U8_T* Write_Data,U16_T Write_Len){
	U8_T Read_para[Write_Len];
	U16_T i = 0;
	
	memset(Read_para,0,sizeof(Read_para));
	
	ReadDataArry_U8(Eeprom_Write_SaveAddr,Write_Len,Read_para);
	for(i=0;i<Write_Len;i++){
		if (Read_para[i]!=Write_Data[i]) {
			return 0x01;
		}
	}
	return 0x00;
}

/*******************************************************************************
* Function Name  : EEPROM_WritePara 
* Description    : 保存参数
*******************************************************************************/
U8_T EEPROM_WritePara(void)
{
	U32_T temp_addr = EEPROM_PARA_SaveAddr;
	U8_T save_para[EEPROM_PARA_Size+10];
	UINT16 save_len = 25;	//要保存数据的长度
	
	memset(save_para,0,sizeof(save_para));
	
	if(save_len >= EEPROM_PARA_Size)	save_len = EEPROM_PARA_Size;
	
	save_para[0] = EEPROM_SAVE_Flag;
	save_para[1] = save_len & 0xFF;
	save_para[2] = (save_len >> 8) & 0xFF;
	save_para[3] = 0x00;
	
	
	save_para[3] = EEPROM_CheckSum(&save_para[4],save_len);
	
	save_len += 4;
	Page_ProgramData(temp_addr,save_len,save_para);	
	
	if(EEPROM_ValidateWrite(temp_addr,save_para,save_len)){
		Dbg_Println(DBG_BIT_SYS_STATUS,"EEPROM_WritePara Save Para Err");
		return 0x01;
	}	
	Dbg_Println(DBG_BIT_SYS_STATUS,"EEPROM_WritePara Save Para");
	return 0;
}









/****************************<-------   boot   ------ -->***************************************************/

/*******************************************************************************
* Function Name  : EEPROM_ReadMCUDevInfo 
* Description    : 从EEPROM中读取设备信息
*******************************************************************************/
U8_T EEPROM_ReadMCUDevInfo(E_MCU_DEV_INFO *info)
{
	U8_T read_info[6];
	U8_T para_data[EEPROM_DATA_Size_Max];
	U16_T read_len = 0;
	
	memset(read_info,0,sizeof(read_info));
	memset(para_data,0,sizeof(para_data));
	
	ReadDataArry_U8(EEPROM_MCUDevInfo_Address,4,read_info);
	
	if(read_info[0] == EEPROM_SVAE_FLAG){
		read_len = read_info[2];
		read_len <<= 8;
		read_len |= read_info[1];
		
		if(read_len <= EEPROM_DATA_Size_Max){
			
			ReadDataArry_U8(EEPROM_MCUDevInfo_Address+EEPROM_Offset_Data,read_len,para_data);
			if(EEPROM_CheckSum(para_data,sizeof(E_MCU_DEV_INFO)) == read_info[3]){
				//校验成功
				memcpy((uint8_t *)info,para_data,sizeof(E_MCU_DEV_INFO));
				
#if DBG_LOG_EN
				SYSCON_IWDCNT_Reload();
				Dbg_Println(DBG_BIT_SYS_STATUS,"EE DevAddr:%d",g_mcu_dev.dev_addr);
				Dbg_Println(DBG_BIT_SYS_STATUS,"EE DevType:%d",g_mcu_dev.dev_type);
				Dbg_Println(DBG_BIT_SYS_STATUS,"EE DevBootVer:%d",g_mcu_dev.dev_boot_ver);
				Dbg_Println(DBG_BIT_SYS_STATUS,"EE DevAppVer:%d",g_mcu_dev.dev_app_ver);
				Dbg_Println(DBG_BIT_SYS_STATUS,"EE DevNameLen:%d",g_mcu_dev.dev_name_len);
				Dbg_Println(DBG_BIT_SYS_STATUS,"EE DevName:%s",g_mcu_dev.dev_name);
				SYSCON_IWDCNT_Reload();
#endif
				return 0x00;
			}
		}
	}
	
	return 0x01;
}

/*******************************************************************************
* Function Name  : EEPROM_WriteMCUDevInfo 
* Description    : 将设备信息写入到EEPROM中
*******************************************************************************/
U8_T EEPROM_WriteMCUDevInfo(E_MCU_DEV_INFO *info)
{
	U8_T save_data[EEPROM_DATA_Size_Max + 6];
	U16_T save_len = sizeof(E_MCU_DEV_INFO);
	
	if(save_len >= EEPROM_DATA_Size_Max) save_len = EEPROM_DATA_Size_Max;
	
	save_data[0] = EEPROM_SVAE_FLAG;
	save_data[1] = save_len & 0xFF;
	save_data[2] = (save_len >> 8) & 0xFF;
	
	memcpy(&save_data[4],(uint8_t *)info,save_len);
	
	save_data[3] = EEPROM_CheckSum(&save_data[4],save_len);
	
	save_len+=4;
	
	Page_ProgramData(EEPROM_MCUDevInfo_Address,save_len,save_data);
	
	return 0;
}

/*******************************************************************************
* Function Name  : EEPROM_Default_MCUDevInfo 
* Description    : EEPROM中参数恢复默认值，且将默认参数保存至EEPROM中
*******************************************************************************/
void EEPROM_Default_MCUDevInfo(E_MCU_DEV_INFO *info)
{
#if (Project_Area == 0x01)
	/*Boot 区域*/
	info->dev_addr = 0x00;
	info->dev_type = 0x00;
	info->dev_app_ver = 0x00;
	info->dev_boot_ver = Project_FW_Version;
	info->dev_name_len = sizeof(Peoject_Name);

	memset((char *)info->dev_name,0,EEPROM_DEV_NAME_Size);
	memcpy((char *)info->dev_name,(char *)Peoject_Name,info->dev_name_len);

	EEPROM_WriteMCUDevInfo(info);
#elif (Project_Area == 0x02)
	/*APP 区域*/
	info->dev_addr = 0x00;
	info->dev_type = Project_Type;
	info->dev_app_ver = Project_FW_Version;
	info->dev_name_len = sizeof(Peoject_Name);

	memset((char *)info->dev_name,0,EEPROM_DEV_NAME_Size);
	memcpy((char *)info->dev_name,(char *)Peoject_Name,info->dev_name_len);

	EEPROM_WriteMCUDevInfo(info);
#endif
}

/*******************************************************************************
* Function Name  : EEPROM_Validate_MCUDevInfo 
* Description    : 校验从EEPROM 中读取的参数是否正确，如果不正确的话，便将当前正确的参数写入
		APP区域中，判断APP参数与EEPROM中记录的是否一致
		Boot区域中，判断Boot参数与EEPROM中记录的是否一致
*******************************************************************************/
void EEPROM_Validate_MCUDevInfo(E_MCU_DEV_INFO *info)
{
#if (Project_Area == 0x01)
	/*Boot 区域*/
	U8_T save_flag = 0;

	if(info->dev_boot_ver != Project_FW_Version)
	{
		info->dev_boot_ver = Project_FW_Version;
		save_flag = 0x01;
	}

	if(save_flag == 0x01)
	{
		EEPROM_WriteMCUDevInfo(info);
	}
#elif (Project_Area == 0x02)
	/*APP 区域*/
	U8_T save_flag = 0;

	if(info->dev_app_ver != Project_FW_Version)
	{
		info->dev_app_ver = Project_FW_Version;
		save_flag = 0x01;
	}

	if(info->dev_type != Project_Type)
	{
		info->dev_type = Project_Type;
		save_flag = 0x01;
	}

	if(info->dev_name_len != sizeof(Peoject_Name))
	{
		info->dev_name_len = sizeof(Peoject_Name);
		save_flag = 0x01;
	}

	if(strncmp((char *)info->dev_name,(char *)Peoject_Name,sizeof(Peoject_Name)))
	{
		memcpy((char *)info->dev_name,(char *)Peoject_Name,info->dev_name_len);
		save_flag = 0x01;
	}

	if(save_flag == 0x01)
	{
		EEPROM_WriteMCUDevInfo(info);
	}
#endif
}








