#include "includes.h"

E_MCU_DEV_INFO g_mcu_dev;

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;	
	
	EnIFCClk; //使能 IFC 时钟
	IFC->MR |= 0x10002; //高速模式，延迟 2 个周期
	
	
	EEPROM_ReadPara();
	
	/*boot*/
	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	
	}	
	
}

/*******************************************************************************
* 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(CheckSum(para_data,read_len) == read_info[3]){
				//校验成功 - 读取参数
				c_rly.wind_STOP_vol = (para_data[0] + (para_data[1]<<8));
				if(c_rly.wind_STOP_vol > 10000){
					c_rly.wind_STOP_vol = EEPROM_WINDSTOP_OUT_Default;
				}
				
				c_rly.wind_LOW_vol = (para_data[2] + (para_data[3]<<8));
				if(c_rly.wind_LOW_vol > 10000){
					c_rly.wind_LOW_vol = EEPROM_WINDLOW_OUT_Default;
				}
				
				c_rly.wind_MID_vol = (para_data[4] + (para_data[5]<<8));
				if(c_rly.wind_MID_vol > 10000){
					c_rly.wind_MID_vol = EEPROM_WINDMID_OUT_Default;
				}
				
				c_rly.wind_HIGH_vol = (para_data[6] + (para_data[7]<<8));
				if(c_rly.wind_HIGH_vol > 10000){
					c_rly.wind_HIGH_vol = EEPROM_WINDHIGH_OUT_Default;
				}				
				
				//设备端口模式
				c_rly.dev_port = para_data[9];
				if((c_rly.dev_port != ACTIVE_PORT)&&(c_rly.dev_port != POLLING_PORT))
				{
					c_rly.dev_port = POLLING_PORT;
				}


				Dbg_Println(DBG_BIT_SYS_STATUS,"EEPROM_ReadPara wind_STOP_vol : %d",c_rly.wind_STOP_vol);
				Dbg_Println(DBG_BIT_SYS_STATUS,"EEPROM_ReadPara wind_LOW_vol  : %d",c_rly.wind_LOW_vol);
				Dbg_Println(DBG_BIT_SYS_STATUS,"EEPROM_ReadPara wind_MID_vol  : %d",c_rly.wind_MID_vol);
				Dbg_Println(DBG_BIT_SYS_STATUS,"EEPROM_ReadPara wind_HIGH_vol : %d",c_rly.wind_HIGH_vol);
				Dbg_Println(DBG_BIT_SYS_STATUS,"EEPROM_ReadPara dev_port      : %d",c_rly.dev_port);				
				SYSCON_IWDCNT_Reload(); 
				return 0x00;
			}
		}
	}
	
	Dbg_Println(DBG_BIT_SYS_STATUS,"EEPROM_ReadPara Default!");
	//数据读取失败，使用默认参数
	c_rly.wind_STOP_vol = EEPROM_WINDSTOP_OUT_Default;
	c_rly.wind_LOW_vol = EEPROM_WINDLOW_OUT_Default;
	c_rly.wind_MID_vol = EEPROM_WINDMID_OUT_Default;
	c_rly.wind_HIGH_vol = EEPROM_WINDHIGH_OUT_Default;
	c_rly.dev_port = POLLING_PORT;
	
	SYSCON_IWDCNT_Reload(); 
	Dbg_Println(DBG_BIT_SYS_STATUS,"wind_STOP_vol : %d",c_rly.wind_STOP_vol);
	Dbg_Println(DBG_BIT_SYS_STATUS,"wind_LOW_vol  : %d",c_rly.wind_LOW_vol);
	Dbg_Println(DBG_BIT_SYS_STATUS,"wind_MID_vol  : %d",c_rly.wind_MID_vol);
	Dbg_Println(DBG_BIT_SYS_STATUS,"wind_HIGH_vol : %d",c_rly.wind_HIGH_vol);
	Dbg_Println(DBG_BIT_SYS_STATUS,"dev_port      : %d",c_rly.dev_port);		
	SYSCON_IWDCNT_Reload(); 
	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 = 0x0A;
	
	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[4] = c_rly.wind_STOP_vol & 0xFF;
	save_para[5] = (c_rly.wind_STOP_vol >> 8) & 0xFF;	
	save_para[6] = c_rly.wind_LOW_vol & 0xFF;
	save_para[7] = (c_rly.wind_LOW_vol >> 8) & 0xFF;	
	save_para[8] = c_rly.wind_MID_vol & 0xFF;
	save_para[9] = (c_rly.wind_MID_vol >> 8) & 0xFF;	
	save_para[10] = c_rly.wind_HIGH_vol & 0xFF;
	save_para[11] = (c_rly.wind_HIGH_vol >> 8) & 0xFF;	
	
	save_para[12] = g_Dip.addr;
	save_para[13] = c_rly.dev_port;		//端口模式
	
	
	
	save_para[3] = 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
				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);
#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
}








