#include "includes.h"

E_PARA_INFO g_eeprom;

void EEPROM_Init(void){
	U8_T rev = 0;
	EnIFCClk; //使能 IFC 时钟
	IFC->MR |= 0x10002; //高速模式，延迟 2 个周期
	
	delay_nms(10);
	
	rev = EEPROM_ReadParaInfo(&g_eeprom);
	if(rev != 0x00){
		//读取失败，恢复默认参数
		EEPROM_Default_ParaInfo(&g_eeprom);
		Dbg_Println(DBG_BIT_SYS_STATUS,"Para Info Use Default");
	}
	
	EEPROM_Validate_ParaInfo(&g_eeprom);
}


U8_T EEPROM_ReadParaInfo(E_PARA_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_ParaInfo_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_ParaInfo_Address+4,read_len,para_data);
			if(PB_CheckSum(para_data,sizeof(E_PARA_INFO)) == read_info[3]){
				//校验成功
				memcpy((uint8_t *)info,para_data,sizeof(E_PARA_INFO));
				
				return 0x00;
			}
		}
	}
	
	return 0x01;
}

U8_T EEPROM_WriteParaInfo(E_PARA_INFO *info){
	U8_T save_data[EEPROM_DATA_Size_Max + 6];
	U16_T save_len = sizeof(E_PARA_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] = PB_CheckSum(&save_data[4],save_len);
	
	save_len+=4;
	
	Page_ProgramData(EEPROM_ParaInfo_Address,save_len,save_data);
	
	return 0;
}

U8_T EEPROM_ClearParaInfo(void){
	U8_T save_data[EEPROM_DATA_Size_Max+10];
	UINT16 save_len = sizeof(E_PARA_INFO);
	
	if(save_len >= EEPROM_DATA_Size_Max)	save_len = EEPROM_DATA_Size_Max;
	
	save_len += 4;
	memset(save_data,0xFF,save_len);

	Page_ProgramData(EEPROM_ParaInfo_Address,save_len,save_data);
	
	return 0;
}

U8_T EEPROM_Validate_ParaInfo(E_PARA_INFO *info){
	U8_T i=0;
	
	//以下为本地参数
	
	//PowerBus 总线开关 0x01:开启，0x02:关闭
	if((info->powerbus_enable < 0x01) || (info->powerbus_enable > 0x01) ){
		info->powerbus_enable = EEPROM_ParaDefault_PowerBusEnable;		//默认开启
	}
	Dbg_Println(DBG_BIT_SYS_STATUS,"Para Info powerbus_enable %d",info->powerbus_enable);
	
	//PowerBus 总线保护电流范围：1~165	单位：0.1A				1V = 5A
	//因为 BLV_C8_V05 硬件版本目前芯片供电为3.3V,提供给保护阈值电压最高为3.3V,所以保护电流最高为16.5A
	if((info->save_curr < PB_SaveCurrent_Min) || (info->save_curr > PB_SaveCurrent_Max)){
		info->save_curr = EEPROM_ParaDefault_SaveCurr;				//默认保护电流为10A
	}
	Dbg_Println(DBG_BIT_SYS_STATUS,"Para Info save_curr %d",info->save_curr);
	
	
	//以下为灯光参数校验
	
	//全局亮度有效范围：0~100
	if(info->allBrightness > BRIGHTNESS_MAX){
		info->allBrightness = BRIGHTNESS_MAX;		//全局亮度默认：100
	}
	
	//全局亮度可调上限有效范围：0~100
	if(info->allBrightnessUpLimit > BRIGHTNESS_MAX){
		info->allBrightnessUpLimit = BRIGHTNESS_MAX;		//全局亮度可调上限默认：100
	}
	
	//全局亮度可调下限有效范围：0~100
	if(info->allBrightnessDownLimit > BRIGHTNESS_MAX){
		info->allBrightnessDownLimit = BRIGHTNESS_MIN;		//全局亮度可调下限默认：0
	}
	
	Dbg_Println(DBG_BIT_SYS_STATUS,"Para Info allBrightness %d",info->allBrightness);
	Dbg_Println(DBG_BIT_SYS_STATUS,"Para Info allBrightnessUpLimit %d",info->allBrightnessUpLimit);
	Dbg_Println(DBG_BIT_SYS_STATUS,"Para Info allBrightnessDownLimit %d",info->allBrightnessDownLimit);
	
	for(i=0;i<PWM_OUT_CH_MAX;i++){
		//回路开关状态范围：0~1
		if(info->swithcState[i] > 0x01){
			info->swithcState[i] = 0x01;				//回路开关状态默认：0x01
		}
		
		//回路亮度：0~100
		if(info->brightness[i] > BRIGHTNESS_MAX){
			info->brightness[i] = BRIGHTNESS_MIN;		//回路亮度默认：100
		}
		
		Dbg_Println(DBG_BIT_SYS_STATUS,"Para Light Loop%d %d-%d",i,info->swithcState[i],info->brightness[i]);
	}
	
	return 0;
}

/*恢复默认值*/
void EEPROM_Default_ParaInfo(E_PARA_INFO *info){
	info->powerbus_enable = EEPROM_ParaDefault_PowerBusEnable;
	info->save_curr = EEPROM_ParaDefault_SaveCurr;
	info->allBrightness = BRIGHTNESS_MAX;				//全局亮度默认：100
	info->allBrightnessUpLimit = BRIGHTNESS_MAX;		//全局亮度可调上限默认：100
	info->allBrightnessDownLimit = BRIGHTNESS_MIN;		//全局亮度可调下限默认：0
	
	for(U8_T i=0;i<PWM_OUT_CH_MAX;i++){
		info->swithcState[i] = 0x01;				//回路开关状态默认：0x01
		info->brightness[i] = BRIGHTNESS_MIN;		//回路亮度默认：100
		info->gradialTime[i] = 1000;				//回路渐变时间默认：1000
	}
}

