#include "includes.h"
#include "math.h"

DetUart_t u_det; 
HLW8110_REG g_CalFactor;		//合力为电参数
DetEng_Info g_Eng;

void BLV_DetEnergy_Init(void)
{
	memset(&u_det,0,sizeof(DetUart_t));
	memset(&g_CalFactor,0,sizeof(HLW8110_REG));
	memset(&g_Eng,0,sizeof(DetEng_Info));
	
	
	
}

//同步flash中保存的参数
void BLV_DetEnergy_Default(void)
{
	if(e_save.ia_k_val != g_Eng.IA_K_Val)
	{
		g_Eng.IA_K_Val = e_save.ia_k_val;
	}

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

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

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

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

	if(e_save.p_OffSetVal != g_Eng.P_OffSetVal)
	{
		g_Eng.P_OffSetVal = e_save.p_OffSetVal;
	}

	if(e_save.collect_tim != g_Eng.Collect_Tim)
	{
		g_Eng.Collect_Tim = e_save.collect_tim;
	}
	
	if(e_save.saveFlash_tim != g_Eng.SaveFlash_Tim)
	{
		g_Eng.SaveFlash_Tim = e_save.saveFlash_tim;
	}

	if(e_save.report_tim != g_Eng.Report_Tim)
	{
		g_Eng.Report_Tim = e_save.report_tim;
	}
	
	//同步FRAM保存的总能耗
	if(g_engfram.total_A_eng != g_Eng.Total_Eng)
	{
		g_Eng.Total_Eng = g_engfram.total_A_eng;
		g_Eng.LastPhase_Eng = g_Eng.Total_Eng;
	}

	if(g_engfram.total_B_eng != g_Eng.Total_Eng_B)
	{
		g_Eng.Total_Eng_B = g_engfram.total_B_eng;
		g_Eng.LastPhase_Eng_B = g_Eng.Total_Eng_B;
	}	

	if(g_engfram.Eng_A_RegData != g_Eng.U32_EnergyA_RegData)
	{
		g_Eng.U32_EnergyA_RegData = g_engfram.Eng_A_RegData;
	}	

	if(g_engfram.Eng_B_RegData != g_Eng.U32_EnergyB_RegData)
	{
		g_Eng.U32_EnergyB_RegData = g_engfram.Eng_B_RegData;
	}	
	
	
#if DBG_LOG_EN	
	Dbg_Println(DBG_BIT_SYS_STATUS,"BLV_DetEnergy_Default Init");
#endif		
	
}

//保存能耗数据到FRAM
void SaveFram_TEng(void)
{
	if( (g_Eng.Total_Eng != g_engfram.total_A_eng)
	||  (g_Eng.Total_Eng_B != g_engfram.total_B_eng) 
	||  (g_Eng.U32_EnergyA_RegData != g_engfram.Eng_A_RegData)
	||  (g_Eng.U32_EnergyB_RegData != g_engfram.Eng_B_RegData) )
	{
		g_engfram.total_A_eng = g_Eng.Total_Eng;
		g_engfram.total_B_eng = g_Eng.Total_Eng_B;
		g_engfram.Eng_A_RegData = g_Eng.U32_EnergyA_RegData;
		g_engfram.Eng_B_RegData = g_Eng.U32_EnergyB_RegData;
		
		
		FRAM_WriteTotalEng(&g_engfram);
	}
	
}





//校验和取反
U8_T HLW8110_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 HLW8110_WriteREG_EN(void)
{
	U8_T SendData[5] = {0};
	U16_T SendLen = 0x00;
	
	SendData[SendLen++] = 0xA5;
	SendData[SendLen++] = 0xEA;
	SendData[SendLen++] = 0xE5;
	
	SendData[SendLen] = HLW8110_CheckSum(SendData,SendLen);
	SendLen +=1;
	
#if DBG_LOG_EN	
//	Dbg_Println(DBG_BIT_Debug_STATUS,"WriteREG_EN len %d:",SendLen);
//  Dbg_Print_Buff(DBG_BIT_Debug_STATUS,"WriteREG_EN:",SendData,SendLen);
#endif	
	UARTTransmit(UART0,SendData,SendLen);
}


//写失能
void HLW8110_WriteREG_DIS(void)
{
	U8_T SendData[5] = {0};
	U16_T SendLen = 0x00;
	
	SendData[SendLen++] = 0xA5;
	SendData[SendLen++] = 0xEA;
	SendData[SendLen++] = 0xDC;
	
	SendData[SendLen] = HLW8110_CheckSum(SendData,SendLen);
	SendLen +=1;
	
#if DBG_LOG_EN	
//	Dbg_Println(DBG_BIT_Debug_STATUS,"WriteREG_DIS len%d:",SendLen);
//  Dbg_Print_Buff(DBG_BIT_Debug_STATUS,"WriteREG_DIS:",SendData,SendLen);
#endif	
	
	UARTTransmit(UART0,SendData,SendLen);
}


//复位HLW8110
void HLW8110_Reset(void)
{
	U8_T SendData[5] = {0};
	U16_T SendLen = 0x00;
	
	SendData[SendLen++] = 0xA5;
	SendData[SendLen++] = 0xEA;
	SendData[SendLen++] = 0x96;
	
	SendData[SendLen] = HLW8110_CheckSum(SendData,SendLen);
	SendLen +=1;
	
	Dbg_Println(DBG_BIT_Debug_STATUS,"HLW8110 Reset");
	
	UARTTransmit(UART0,SendData,SendLen);
}


/*******************************************************
 * 	@brief 	读/写普通寄存器组包
 * 	@para 	cmd   :	要操作的寄存器
 * 	@para 	len   :	写入数据长度,为0时是读寄存器，> 0时是写寄存器
 * 	@para 	value :  要写入的数据，len == 0 时无效
 *  @retval : None
 * */
void BLV_HLW8110_RWCMD_Packaging(U8_T ADDR_Reg,U16_T len,U32_T value)
{
	memset(u_det.SendBuff,0,REV_BUFFER_SIZE);
	u_det.SendLen = 0x00;
	
	u_det.SendBuff[u_det.SendLen++] = 0xA5;
	u_det.SendBuff[u_det.SendLen++] = ADDR_Reg|0x80;
	
	u_det.Resent = 0x00;
	u_det.RevNum = 0x00;

	if( len > 0)
	{
		u_det.WR_flag = SEND_W;
		for (U8_T i = 0; i < len; i++) 
		{
			u_det.SendBuff[i + u_det.SendLen] = (value >> ((len - 1 - i) * 8)) & 0xFF;
		}	
		
		u_det.SendLen += len;
		u_det.SendBuff[u_det.SendLen] = HLW8110_CheckSum(u_det.SendBuff,u_det.SendLen);
		u_det.SendLen += 1;			
		
	}
	else
	{
		u_det.WR_flag = SEND_R;
	}
	
}


/***************************************
 * @brief 	发送命令,
 * @para 	WR_SelFlag:读/写寄存器选择
 * 
 * @retval 	None
 * */
void HLW8110_RWCMD_Send(U8_T WR_SelFlag)
{
	U8_T SendData[30];
	U16_T SendLen = 0x00;
	
	
	if( (WR_SelFlag == SEND_W)&&(u_det.SendLen > 3) )
	{
		memcpy(SendData,u_det.SendBuff,u_det.SendLen);
		SendLen = u_det.SendLen;

	}
	else if(WR_SelFlag == SEND_R)
	{
		memcpy(SendData,u_det.SendBuff,2);
		SendData[1] = (SendData[1] &0x7f );
		SendLen = 2;
	}
	else
	{		
		return;	
	}
	
#if DBG_LOG_EN	
	Dbg_Println(DBG_BIT_Debug_STATUS,"CMD:%02x,len:%d,SdCt:%d",SendData[1],SendLen,u_det.Resent);
//	Dbg_Print_Buff(DBG_BIT_Debug_STATUS,"SendBuff:",SendData,SendLen);
#endif


	UARTTransmit(UART0,SendData,SendLen);
}


//清除发送标志位
U8_T HLW8110_CleanSdFlag(void)
{
	u_det.WR_flag = SEND_NONE;
	u_det.RevNum = REV_NONE;
}


//返回回复状态
U8_T Get_RevState(void)
{
	U8_T ret = 0x00;
	
	if(u_det.RevNum == REV_OK)
	{
		u_det.RevNum = REV_NONE;
		return REV_OK;
	}
	else if(u_det.RevNum == REV_ERR)
	{
		u_det.RevNum = REV_NONE;
		return REV_ERR;
	}
	return REV_NONE;
}


//返回发送状态
U8_T Get_SendState(void)
{
	if(u_det.SendState == SEND_STATE_SUCC)
	{
		u_det.SendState = SEND_STATE_NONE;
		return SEND_STATE_SUCC;
	}
	else if(u_det.SendState == SEND_STATE_FAIL)
	{
		u_det.SendState = SEND_STATE_NONE;
		return SEND_STATE_FAIL;
	}
	
	return SEND_STATE_NONE;	
	
}



//等待时间，以及跳转下一状态
void HLW8110_ToWaitState(U8_T NextState,U32_T WaitTime)
{
	u_det.State = HLW_WAIT;		
	u_det.Pub_NextState = NextState;
	u_det.Pub_WaitTim = WaitTime;	
	
}


//发送 读/写寄存器状态机
void BLV_HLW8110_SendData_Tack(void)
{
	static U32_T HLW8110_Sned_Tisk = 0x00;			//发送读写寄存器的时间戳
	
	switch(u_det.State)
	{
		case HLW_IDLE:
			if(u_det.WR_flag == SEND_R)				//读，需要等待回复校验
			{
				HLW8110_Sned_Tisk = SysTick_1ms;
				u_det.State = HLW_SEND_INIT;
			}
			else if(u_det.WR_flag == SEND_W)		//写,需要先写使能
			{
				HLW8110_Sned_Tisk = SysTick_1ms;
				u_det.State = HLW_SEND_INIT;
			}			
		break;	
		case HLW_SEND_INIT:
			//发送初始化		
			u_det.Resent = 0x00;
			u_det.RevNum = 0x00;
			
			if(u_det.WR_flag == SEND_W){
				HLW8110_WriteREG_EN();//发 写使能,等待20ms+
				HLW8110_ToWaitState(HLW_WTREG,50);
			}else{
				HLW8110_ToWaitState(HLW_SEND,50);
			}
		break;	
		case HLW_WTREG:
			//先写寄存器,写完再读校验
			HLW8110_RWCMD_Send(SEND_W);
			HLW8110_Sned_Tisk = SysTick_1ms;
			HLW8110_ToWaitState(HLW_SEND,50);			
		case HLW_SEND:
			if(SysTick_1ms - HLW8110_Sned_Tisk >= SEND_TIME)
			{
				HLW8110_Sned_Tisk = SysTick_1ms;
				
				if(u_det.Resent < 3)				
				{	
					//发送读取寄存器指令
					HLW8110_RWCMD_Send(SEND_R);		
					u_det.Resent++;
				}else{	//没有重发次数,可能有错误，3S等待时间防止频繁操作
					u_det.SendState = SEND_STATE_FAIL;
					if(u_det.WR_flag == SEND_W){
						HLW8110_ToWaitState(HLW_WRITE_DIS,3000);
						HLW8110_CleanSdFlag();						
					}else{
						HLW8110_ToWaitState(HLW_IDLE,3000);
						HLW8110_CleanSdFlag();						
					}										
				}				
			}else{	//有回复且校验成功	
				if(Get_RevState() == REV_OK)	
				{	//发送成功
					u_det.SendState = SEND_STATE_SUCC;	
					HLW8110_Sned_Tisk = SysTick_1ms;
					
					if(u_det.WR_flag == SEND_W){
						HLW8110_ToWaitState(HLW_WRITE_DIS,50);
						HLW8110_CleanSdFlag();						
					}else{
						HLW8110_ToWaitState(HLW_IDLE,100);
						HLW8110_CleanSdFlag();						
					}					
				}			
			}
		break;
		case HLW_WRITE_DIS:
			//发  写失能,等待10ms+
			HLW8110_WriteREG_DIS();
			HLW8110_ToWaitState(HLW_IDLE,50);
			HLW8110_Sned_Tisk = SysTick_1ms;
		break;		
		case HLW_WAIT:	
			//公用，等待时间区域
			if(SysTick_1ms - HLW8110_Sned_Tisk >= u_det.Pub_WaitTim)
			{	//跳出等待区
				u_det.State = u_det.Pub_NextState;	
				u_det.Pub_NextState = 0x00;
			}
		break;		
	}

}


//转换电压值
F32_T HLW8110_Convert_VoltageReg_Value(U32_T value)
{
	F32_T Conv_Val = 0.0;
	
	if( (g_CalFactor.U24_Voltage_RegData&0x800000) == 0x800000)
	{
		//g_Eng.det_ectA = 0x00;
		Conv_Val = 0;
	}
	else 
	{
        Conv_Val = value / 100.0;
        Conv_Val = Conv_Val * g_CalFactor.U16_RMSUC_RegData;
        Conv_Val = Conv_Val / 0x400000;
        Conv_Val = Conv_Val * g_Eng.V_K_Val;//VK_Val;
	}	
	
	g_Eng.det_V = (Conv_Val * 100);		//单位：10mV 
	
	Dbg_Println(DBG_BIT_Debug_STATUS,"VolReg:%f, %d",Conv_Val,g_CalFactor.U16_RMSUC_RegData);
	
	return Conv_Val;
}


//转换A通道电流值
F32_T HLW8110_Convert_CurrentRegA_Value(U32_T value)
{
	F32_T Conv_Val = 0.0;
	
	if( (g_CalFactor.U24_CurrentA_RegData&0x800000) == 0x800000)
	{
		Conv_Val = 0;
	}
	else 
	{
		//此处需要换算电流值
        Conv_Val = value / 1000.0;
        Conv_Val = Conv_Val * g_CalFactor.U16_RMSIAC_RegData;
        Conv_Val = Conv_Val / 0x800;
        Conv_Val = Conv_Val / 0x1000;
        Conv_Val = Conv_Val * g_Eng.IA_K_Val;
        Conv_Val = Conv_Val + g_Eng.I_OffSetVal;
	}	
	
	g_Eng.det_ectA = (Conv_Val * 100);		//单位10mA
	
	
	Dbg_Println(DBG_BIT_Debug_STATUS,"CurReg_A:%f, %d",Conv_Val,g_CalFactor.U16_RMSIAC_RegData);
	
	return Conv_Val;
}


//转换B通道电流值
F32_T HLW8110_Convert_CurrentRegB_Value(U32_T value)
{
	F32_T Conv_Val = 0.0;
	
	if( (g_CalFactor.U24_CurrentB_RegData&0x800000) == 0x800000)
	{
		Conv_Val = 0;
	}
	else 
	{
		//此处需要换算电流值
        Conv_Val = value / 1000.0;
        Conv_Val = Conv_Val * g_CalFactor.U16_RMSIBC_RegData;
        Conv_Val = Conv_Val / 0x800;
        Conv_Val = Conv_Val / 0x1000;
        Conv_Val = Conv_Val * g_Eng.IB_K_Val;
        Conv_Val = Conv_Val + g_Eng.I_OffSetVal;
	}	
	
	g_Eng.det_ectB = (Conv_Val * 100);		//单位10mA
	
	
	Dbg_Println(DBG_BIT_Debug_STATUS,"CurReg_B:%f, %d",Conv_Val,g_CalFactor.U16_RMSIBC_RegData);
	
	return Conv_Val;
}



//转换A通道有功功率
F32_T HLW8110_Convert_PowerRegA_Value(U32_T value)
{
	F32_T Conv_Val = 0.0;
	
	Conv_Val = abs((int)value);
	
	Conv_Val = Conv_Val / 1.0;			//电流系数
	Conv_Val = Conv_Val / 1.0;			//电压系数
	Conv_Val = Conv_Val * g_CalFactor.U16_PowerPAC_RegData;
	Conv_Val = Conv_Val / 0x8000;	
	Conv_Val = Conv_Val / 0x10000;
	Conv_Val = Conv_Val * g_Eng.IA_K_Val * g_Eng.PA_K_Val * g_Eng.V_K_Val;
	Conv_Val = Conv_Val + g_Eng.P_OffSetVal;
	
	g_Eng.det_powA = (Conv_Val*1000);				//单位：mW


	Dbg_Println(DBG_BIT_Debug_STATUS,"PowReg_A:%f, %d, %d",Conv_Val,value,g_CalFactor.U16_PowerPAC_RegData);
	
	return Conv_Val;
}


//转换B通道有功功率
F32_T HLW8110_Convert_PowerRegB_Value(U32_T value)
{
	F32_T Conv_Val = 0.0;
	
	Conv_Val = abs((int)value);
	
	Conv_Val = Conv_Val / 1.0;			//电流系数
	Conv_Val = Conv_Val / 1.0;			//电压系数
	Conv_Val = Conv_Val * g_CalFactor.U16_PowerPBC_RegData;
	Conv_Val = Conv_Val / 0x8000;	
	Conv_Val = Conv_Val / 0x10000;
	Conv_Val = Conv_Val * g_Eng.IB_K_Val * g_Eng.PB_K_Val * g_Eng.V_K_Val;
	Conv_Val = Conv_Val + g_Eng.P_OffSetVal;
	
	g_Eng.det_powB = (Conv_Val*1000);				//单位：mW
	
	Dbg_Println(DBG_BIT_Debug_STATUS,"PowReg_B:%f, %d, %d",Conv_Val,value,g_CalFactor.U16_PowerPBC_RegData);
	
	return Conv_Val;
}


//转换A通道电能能耗
U8_T HLW8110_Convert_EnergyRegA_Value(U32_T value)
{
	F32_T Conv_Val = 0.0;
	U32_T EnergyA_RegData_Diff = 0x00;		//本次读取电量寄存器值和上次读取到的电量寄存器值的差值
	
	if(value > 0xFFFFFF){
		return 0x02;
	}	
	
//	Dbg_BT_Println(DBG_BIT_BTDebug_STATUS," valA:%d, Last:%d, SumRg1:%d",value,g_Eng.U32_EnergyA_RegData_LastRecord,g_Eng.U32_EnergyA_RegData);
	
	if( g_Eng.U32_EnergyA_RegData_LastRecord <= value )	//记录的上次读取的电能寄存器值小于此次读取到的电能寄存器值
	{
		EnergyA_RegData_Diff = ( value - (g_Eng.U32_EnergyA_RegData_LastRecord & 0x00FFFFFF) );
	}else {
		EnergyA_RegData_Diff = ( ((value & 0x00FFFFFF)+ 0x01000000) - g_Eng.U32_EnergyA_RegData_LastRecord );
	}
	
	
	if(EnergyA_RegData_Diff > REG_DEBUG_NUM){		//差值大，不处理
		g_Eng.EnergyA_reset_cnt++;
		if(g_Eng.EnergyA_reset_cnt >= REG_DEBUG_CNT){		//连续7次差值过大，复位HLW的IC
		Dbg_Println(DBG_BIT_SYS_STATUS,"RESER111");
			g_Eng.EnergyA_reset_cnt = 0x00;
			g_Eng.tack_reset_flag = 0x01;
		}
		
		Dbg_Println(DBG_BIT_Debug_STATUS,"diff:%d, retCntA:%d",EnergyA_RegData_Diff,g_Eng.EnergyA_reset_cnt);
		return 0x01;
	}	
	
	g_Eng.EnergyA_reset_cnt = 0x00;
	
	g_Eng.U32_EnergyA_RegData_LastRecord = (value & 0xFFFFFF);
	g_Eng.U32_EnergyA_RegData += EnergyA_RegData_Diff;

	Conv_Val = g_Eng.U32_EnergyA_RegData / 1.0;
	Conv_Val = Conv_Val * g_Eng.IA_K_Val * g_Eng.V_K_Val;
   
	Conv_Val = Conv_Val * g_CalFactor.U16_EnergyAC_RegData;
	Conv_Val = Conv_Val / 0x20000000;
	
	g_Eng.Total_Eng = (Conv_Val * 1000);				//累计值，单位，W/H
	
#if DBG_LOG_EN	
	//2025-12-01,    打印：与上次的差值、寄存器值、电能转换系数(寄存器值不变)、总电能值（单位：W/H）
	Dbg_Println(DBG_BIT_Debug_STATUS,"diff:%d, value:%d, EngAC:%d, TolEng:%d, SumEReg2:%d",EnergyA_RegData_Diff, value, g_CalFactor.U16_EnergyAC_RegData, g_Eng.Total_Eng,g_Eng.U32_EnergyA_RegData);
	//蓝牙临时调试信息打印通道
//	Dbg_BT_Println(DBG_BIT_BTDebug_STATUS,"dif:%d, val:%d, EngAC:%d, TlE:%d, SumRg2:%d",EnergyA_RegData_Diff, value, g_CalFactor.U16_EnergyAC_RegData, g_Eng.Total_Eng,g_Eng.U32_EnergyA_RegData);
#endif	
	
	//保存总能耗
	SaveFram_TEng();
	
	//超过阈值，复位芯片
	if(g_Eng.U32_EnergyA_RegData_LastRecord >= REG_SUM_MAX){
		Dbg_Println(DBG_BIT_SYS_STATUS,"RESER222");
		g_Eng.tack_reset_flag = 0x01;
	}
	
	return 0x00;
}



//转换B通道电能能耗
U8_T HLW8110_Convert_EnergyRegB_Value(U32_T value)
{
	F32_T Conv_Val = 0.0;
	U32_T EnergyB_RegData_Diff = 0x00;		//本次读取电量寄存器值和上次读取到的电量寄存器值的差值
	
	if(value > 0xFFFFFF){
		return 0x02;
	}
	
//	Dbg_BT_Println(DBG_BIT_BTDebug_STATUS," valB:%d, Last:%d, SumRg1:%d",value,g_Eng.U32_EnergyB_RegData_LastRecord,g_Eng.U32_EnergyB_RegData);
	
	if( g_Eng.U32_EnergyB_RegData_LastRecord <= value )	//记录的上次读取的电能寄存器值小于此次读取到的电能寄存器值
	{
		EnergyB_RegData_Diff = ( value - (g_Eng.U32_EnergyB_RegData_LastRecord & 0x00FFFFFF) );
	}else {
		EnergyB_RegData_Diff = ( ((value & 0x00FFFFFF)+ 0x01000000) - g_Eng.U32_EnergyB_RegData_LastRecord );
	}
	
	if(EnergyB_RegData_Diff > REG_DEBUG_NUM){		//差值大，不处理，要结合硬件修改
		g_Eng.EnergyB_reset_cnt++;
		if(g_Eng.EnergyB_reset_cnt >= REG_DEBUG_CNT){
			Dbg_Println(DBG_BIT_SYS_STATUS,"RESER333");
			g_Eng.EnergyB_reset_cnt = 0x00;
			g_Eng.tack_reset_flag = 0x01;
		}		
		Dbg_Println(DBG_BIT_Debug_STATUS,"diff:%d, retCntB:%d",EnergyB_RegData_Diff,g_Eng.EnergyB_reset_cnt);
		return 0x01;
	}	
	
	g_Eng.EnergyB_reset_cnt = 0x00;
	
	g_Eng.U32_EnergyB_RegData_LastRecord = (value & 0xFFFFFF);
	g_Eng.U32_EnergyB_RegData += EnergyB_RegData_Diff;

	Conv_Val = g_Eng.U32_EnergyB_RegData / 1.0;
	Conv_Val = Conv_Val * g_Eng.IB_K_Val * g_Eng.V_K_Val;
   
	Conv_Val = Conv_Val * g_CalFactor.U16_EnergyBC_RegData;
	Conv_Val = Conv_Val / 0x20000000;
	
	g_Eng.Total_Eng_B = (Conv_Val * 1000);				//累计值，单位，W/H

#if DBG_LOG_EN
	//2025-12-01,    打印：与上次的差值、寄存器值、电能转换系数、换算总电能值（单位：W/H）
	Dbg_Println(DBG_BIT_Debug_STATUS,"diff:%d, value:%d, EngBC:%d, TolEng:%d, SumEReg2:%d",EnergyB_RegData_Diff, value, g_CalFactor.U16_EnergyBC_RegData, g_Eng.Total_Eng_B,g_Eng.U32_EnergyB_RegData);
	//蓝牙临时调试信息打印通道
//	Dbg_BT_Println(DBG_BIT_BTDebug_STATUS,"dif:%d, val:%d, EngBC:%d, TlE:%d, SumRg2:%d",EnergyB_RegData_Diff, value, g_CalFactor.U16_EnergyBC_RegData, g_Eng.Total_Eng_B,g_Eng.U32_EnergyB_RegData);
#endif

	//保存总能耗
	SaveFram_TEng();

	//超过阈值，复位芯片
	if(g_Eng.U32_EnergyB_RegData_LastRecord >= REG_SUM_MAX){
		Dbg_Println(DBG_BIT_SYS_STATUS,"RESER444");
		g_Eng.tack_reset_flag = 0x01;
	}	

	return 0x00;
}


//复位HLW8110前，清除各种数据
void HLW8110_ClearFlag(void)
{
	g_Eng.EnergyA_reset_cnt = 0x00;
	g_Eng.EnergyB_reset_cnt = 0x00;
	g_Eng.U32_EnergyA_RegData_LastRecord = 0x00;
	g_Eng.U32_EnergyB_RegData_LastRecord = 0x00;
	
}


//接收处理
U8_T HLW8110_RecvData_Processing(U8_T *RecvData,U16_T RecvLen)
{
	//必定有1校验+1数据
	if((RecvLen < 0x02)||(RecvLen > 0x05) )	
	{
		Dbg_Println(DBG_BIT_SYS_STATUS,"Len0 Err:%d",RecvLen);
		return 0x01;
	}
	
	if(u_det.WR_flag  == SEND_NONE)	
	{
		Dbg_Println(DBG_BIT_SYS_STATUS,"Not Tack ");
		return 0x02;
	}	

	memset(u_det.RevBuff,0,REV_BUFFER_SIZE);	//清零接收缓冲
	u_det.RevLen = 0;
	
	memcpy(u_det.RevBuff,u_det.SendBuff,2);		//发送的帧头、地址
	u_det.RevLen += 2;
	u_det.RevBuff[1] = (u_det.RevBuff[1]&0x7f );
	
	memcpy(&u_det.RevBuff[2],RecvData,RecvLen);	//接收的数据位校验位
	u_det.RevLen += RecvLen;
	

	//接收数据长度限制
	switch(u_det.RevBuff[1])
	{
		case REG_SYSCON_ADDR:
		case REG_EMUCON1_ADDR:
		case REG_EMUCON2_ADDR:	
		case REG_CHECKSUM_ADDR:
		case REG_RMS_IAC_ADDR:
		case REG_RMS_IBC_ADDR:
		case REG_RMS_UC_ADDR:
		case REG_POWER_PAC_ADDR:
		case REG_POWER_PBC_ADDR:
		case REG_POWER_SC_ADDR:
		case REG_ENERGY_AC_ADDR:
		case REG_ENERGY_BC_ADDR:
		case REG_HFCONST_ADDR:		
			if(RecvLen != 0x03){
				Dbg_Println(DBG_BIT_SYS_STATUS,"Len 3 Err:%d",RecvLen);
				return 0x03;
			}
			break;
		case REG_RMSU_ADDR:
		case REG_RMSIA_ADDR:
		case REG_ENERGY_PA_ADDR:
		case REG_RMSIB_ADDR:
		case REG_ENERGY_PB_ADDR:		
			if(RecvLen != 0x04){
				Dbg_Println(DBG_BIT_SYS_STATUS,"Len 4 Err:%d",RecvLen);
				return 0x03;
			}
			break;
		case REG_POWER_PA_ADDR:	
		case REG_POWER_PB_ADDR:	
			if(RecvLen != 0x05){
				Dbg_Println(DBG_BIT_SYS_STATUS,"Len 5 Err:%d",RecvLen);
				return 0x03;
			}		
			break;
		default:

		break;
	}

	
	if(HLW8110_CheckSum(u_det.RevBuff,u_det.RevLen-1) != RecvData[RecvLen-1])
	{
		Dbg_Println(DBG_BIT_SYS_STATUS,"CRC Err:%x,%x,CMD:%02x",HLW8110_CheckSum(u_det.RevBuff,u_det.RevLen-1),RecvData[RecvLen-1],u_det.RevBuff[1]);
		Dbg_Print_Buff(DBG_BIT_Debug_STATUS,"111",u_det.RevBuff,u_det.RevLen);
//		Dbg_BT_Println(DBG_BIT_BTDebug_STATUS,"CRC Err:%x,%x, CMD:%02x->",HLW8110_CheckSum(u_det.RevBuff,u_det.RevLen-1),RecvData[RecvLen-1],u_det.RevBuff[1]);
		u_det.RevNum = REV_ERR;
		return 0x04;
	}else{
		//读寄存器校验成功。
		u_det.RevNum = REV_OK;	
	}
	
	//解析指令
	Dbg_Println(DBG_BIT_Debug_STATUS,"HLW_Recv CMD:%02X",u_det.RevBuff[1]);
//	Dbg_Print_Buff(DBG_BIT_Debug_STATUS,"111",u_det.RevBuff,u_det.RevLen);
	
	switch(u_det.RevBuff[1])
	{
	/*******************************<-----  系统寄存器  ----->**********************************************/	
		case REG_SYSCON_ADDR:		//系统控制寄存器
			g_CalFactor.U16_Check_SysconReg_Data = ( (RecvData[0]<<8) + RecvData[1] );
			if(g_CalFactor.U16_Check_SysconReg_Data == SYSCON_DEFAULT){
				u_det.RevNum = REV_OK;		//写寄存器后的回读校验
			}else{
				u_det.RevNum = REV_ERR;
			}
		break;			
		case REG_EMUCON1_ADDR:		//写计量控制寄存器1
			g_CalFactor.U16_Check_Emucon1Reg_Data = ( (RecvData[0]<<8) + RecvData[1] );
			if(g_CalFactor.U16_Check_Emucon1Reg_Data == EMUCON1_DEFAULT){
				u_det.RevNum = REV_OK;
			}else{
				u_det.RevNum = REV_ERR;
			}				
		break;
		case REG_EMUCON2_ADDR:		//计量控制寄存器2
			g_CalFactor.U16_Check_Emucon2Reg_Data = ( (RecvData[0]<<8) + RecvData[1] );
			if(g_CalFactor.U16_Check_Emucon2Reg_Data == EMUCON2_DEFAULT){
				u_det.RevNum = REV_OK;
			}else{
				u_det.RevNum = REV_ERR;
			}				
		break;	
	/***********************************<-----  系数寄存器  ----->**********************************************/
		case REG_CHECKSUM_ADDR:		//校表系数校验和
			g_CalFactor.U16_CheckSUM_RegData = ( (RecvData[0]<<8) + RecvData[1] );
		break;
		case REG_RMS_IAC_ADDR:		//电流通道A有效值转换系数
			g_CalFactor.U16_RMSIAC_RegData = ( (RecvData[0]<<8) + RecvData[1] );
		break;
		case REG_RMS_IBC_ADDR:		//电流通道B有效值转换系数
			g_CalFactor.U16_RMSIBC_RegData = ( (RecvData[0]<<8) + RecvData[1] );
		break;
		case REG_RMS_UC_ADDR:		//电压通道U有效值转换系数
			g_CalFactor.U16_RMSUC_RegData = ( (RecvData[0]<<8) + RecvData[1] );
		break;
		case REG_POWER_PAC_ADDR:	//电流通道A有功功率转换系数
			g_CalFactor.U16_PowerPAC_RegData = ( (RecvData[0]<<8) + RecvData[1] );
		break;
		case REG_POWER_PBC_ADDR:	//电流通道B有功功率转换系数
			g_CalFactor.U16_PowerPBC_RegData = ( (RecvData[0]<<8) + RecvData[1] );
		break;
		case REG_POWER_SC_ADDR:		//视在功率转换系数
			g_CalFactor.U16_PowerSC_RegData = ( (RecvData[0]<<8) + RecvData[1] );
		break;
		case REG_ENERGY_AC_ADDR:	//A通道能量转换系数
			g_CalFactor.U16_EnergyAC_RegData = ( (RecvData[0]<<8) + RecvData[1] );
		break;
		case REG_ENERGY_BC_ADDR:	//B通道能量转换系数		
			g_CalFactor.U16_EnergyBC_RegData = ( (RecvData[0]<<8) + RecvData[1] );
		break;
		case REG_HFCONST_ADDR:		//脉冲常量
			g_CalFactor.U16_HFConst_RegData = ( (RecvData[0]<<8) + RecvData[1] );
			g_Eng.hfcount = g_CalFactor.U16_HFConst_RegData;
		break;		
	/***********************************<-----  采集电能相关寄存器  ----->**********************************************/	
		case REG_RMSU_ADDR:			//电压有效值	
			g_CalFactor.U24_Voltage_RegData = ( (RecvData[0]<<16) + (RecvData[1]<<8) + RecvData[2] );
			
			HLW8110_Convert_VoltageReg_Value(g_CalFactor.U24_Voltage_RegData);
		break;		
		case REG_RMSIA_ADDR:		//通道A电流有效值	
			g_CalFactor.U24_CurrentA_RegData = ( (RecvData[0]<<16) + (RecvData[1]<<8) + RecvData[2] );

			HLW8110_Convert_CurrentRegA_Value(g_CalFactor.U24_CurrentA_RegData);
		break;			
		case REG_ENERGY_PA_ADDR:	//通道A有功电能
			g_CalFactor.U24_EnergyA_RegData = ( (RecvData[0]<<16) + (RecvData[1]<<8) + RecvData[2] );

			HLW8110_Convert_EnergyRegA_Value(g_CalFactor.U24_EnergyA_RegData);
		break;	
		case REG_POWER_PA_ADDR:		//通道A有功功率
			g_CalFactor.U32_PowerA_RegData = ( (RecvData[0]<<24) + (RecvData[1]<<16) + (RecvData[2]<<8) +  RecvData[3] );
		
			HLW8110_Convert_PowerRegA_Value(g_CalFactor.U32_PowerA_RegData);
		break;					
		case REG_RMSIB_ADDR:		//通道B电流	
			g_CalFactor.U24_CurrentB_RegData = ( (RecvData[0]<<16) + (RecvData[1]<<8) + RecvData[2] );
			
			HLW8110_Convert_CurrentRegB_Value(g_CalFactor.U24_CurrentB_RegData);
		break;			
		case REG_ENERGY_PB_ADDR:	//通道B有功电能
			g_CalFactor.U24_EnergyB_RegData = ( (RecvData[0]<<16) + (RecvData[1]<<8) + RecvData[2] );
			
			HLW8110_Convert_EnergyRegB_Value(g_CalFactor.U24_EnergyB_RegData);
		break;	
		case REG_POWER_PB_ADDR:		//通道B有功功率
			g_CalFactor.U32_PowerB_RegData = ( (RecvData[0]<<24) + (RecvData[1]<<16) + (RecvData[2]<<8) +  RecvData[3] );
			
			HLW8110_Convert_PowerRegB_Value(g_CalFactor.U32_PowerB_RegData);
		break;
		default:
		break;
	}

	return 0x00;
	
}

//系数寄存器内容校验
U16_T HLW8110_SysPara_Check(HLW8110_REG *g_CC)
{
	U16_T CRC_p = 0x00;
	
	CRC_p =  ~(0xffff+g_CC->U16_RMSIAC_RegData + g_CC->U16_RMSIBC_RegData + g_CC->U16_RMSUC_RegData + 
				g_CC->U16_PowerPAC_RegData + g_CC->U16_PowerPBC_RegData + g_CC->U16_PowerSC_RegData + 
				g_CC->U16_EnergyAC_RegData + g_CC->U16_EnergyBC_RegData  );
	CRC_p = (CRC_p&0xFFFF);
	
	return CRC_p;
}





//系统系数读取
void HLW8110_ReadSysPara(void)
{
	static U32_T HLW8110_Reset_Tack = 0x00;
	
	switch(g_Eng.rWCh_Num)
	{
		case 0x00:
			HLW8110_Reset_Tack = SysTick_1ms;
			g_Eng.rWCh_Num = 0x01;
			HLW8110_Reset();
			break;
		case 0x01:
			if(SysTick_1ms - HLW8110_Reset_Tack >= 1000)		//复位HLW8110时间
			{
				HLW8110_Reset_Tack = SysTick_1ms;
				g_Eng.rWCh_Num = 0x02;
			}
			break;		
		case 0x02:				//读电流通道A有效值转换系数
			if(Get_SendState() == SEND_STATE_SUCC)
			{
				g_Eng.rWCh_Num = 0x03;
				break;
			}
			else if(Get_SendState() == SEND_STATE_FAIL)
			{
				Dbg_Println(DBG_BIT_SYS_STATUS,"REG_RMS_IAC_ADDR ERR!");
				break;
			}
			//当前没有发送任务
			if(u_det.WR_flag == SEND_NONE)		
			{
				BLV_HLW8110_RWCMD_Packaging(REG_RMS_IAC_ADDR,0,0);	
				break;
			}
		break;
		case 0x03:							
			if(Get_SendState() == SEND_STATE_SUCC)
			{
				g_Eng.rWCh_Num = 0x04;
				break;
			}
			else if(Get_SendState() == SEND_STATE_FAIL)
			{
				g_Eng.rWCh_Num = 0x02;
				Dbg_Println(DBG_BIT_SYS_STATUS,"REG_RMS_IBC_ADDR ERR!");
				break;
			}
			//当前没有发送任务
			if(u_det.WR_flag == SEND_NONE)		
			{
				BLV_HLW8110_RWCMD_Packaging(REG_RMS_IBC_ADDR,0,0);	
				break;
			}
		break;
		case 0x04:							
			if(Get_SendState() == SEND_STATE_SUCC)
			{
				g_Eng.rWCh_Num = 0x05;
				break;
			}
			else if(Get_SendState() == SEND_STATE_FAIL)
			{
				g_Eng.rWCh_Num = 0x02;
				Dbg_Println(DBG_BIT_SYS_STATUS,"REG_RMS_UC_ADDR ERR!");
				break;
			}
			//当前没有发送任务
			if(u_det.WR_flag == SEND_NONE)		
			{
				BLV_HLW8110_RWCMD_Packaging(REG_RMS_UC_ADDR,0,0);
				break;	
			}
		break;	
		case 0x05:							
			if(Get_SendState() == SEND_STATE_SUCC)
			{
				g_Eng.rWCh_Num = 0x06;
				break;
			}
			else if(Get_SendState() == SEND_STATE_FAIL)
			{
				g_Eng.rWCh_Num = 0x02;
				Dbg_Println(DBG_BIT_SYS_STATUS,"REG_POWER_PAC_ADDR ERR!");
				break;
			}
			//当前没有发送任务
			if(u_det.WR_flag == SEND_NONE)		
			{
				BLV_HLW8110_RWCMD_Packaging(REG_POWER_PAC_ADDR,0,0);
				break;
			}
		break;
		case 0x06:							
			if(Get_SendState() == SEND_STATE_SUCC)
			{
				g_Eng.rWCh_Num = 0x07;
				break;
			}
			else if(Get_SendState() == SEND_STATE_FAIL)
			{
				g_Eng.rWCh_Num = 0x02;
				Dbg_Println(DBG_BIT_SYS_STATUS,"REG_POWER_PBC_ADDR ERR!");
				break;
			}
			//当前没有发送任务
			if(u_det.WR_flag == SEND_NONE)		
			{
				BLV_HLW8110_RWCMD_Packaging(REG_POWER_PBC_ADDR,0,0);	
				break;
			}
		break;
		case 0x07:							
			if(Get_SendState() == SEND_STATE_SUCC)
			{
				g_Eng.rWCh_Num = 0x08;
				break;
			}
			else if(Get_SendState() == SEND_STATE_FAIL)
			{
				g_Eng.rWCh_Num = 0x02;
				Dbg_Println(DBG_BIT_SYS_STATUS,"REG_POWER_SC_ADDR ERR!");
				break;
			}
			
			if(u_det.WR_flag == SEND_NONE)
			{
				BLV_HLW8110_RWCMD_Packaging(REG_POWER_SC_ADDR,0,0);	
				break;
			}
		break;
		case 0x08:							
			if(Get_SendState() == SEND_STATE_SUCC)
			{
				g_Eng.rWCh_Num = 0x09;
				break;
			}
			else if(Get_SendState() == SEND_STATE_FAIL)
			{
				g_Eng.rWCh_Num = 0x02;
				Dbg_Println(DBG_BIT_SYS_STATUS,"REG_ENERGY_AC_ADDR ERR!");
				break;
			}
			//当前没有发送任务
			if(u_det.WR_flag == SEND_NONE)		
			{
				BLV_HLW8110_RWCMD_Packaging(REG_ENERGY_AC_ADDR,0,0);	
				break;
			}
		break;
		case 0x09:							
			if(Get_SendState() == SEND_STATE_SUCC)
			{
				g_Eng.rWCh_Num = 0x0A;
				break;
			}
			else if(Get_SendState() == SEND_STATE_FAIL)
			{
				g_Eng.rWCh_Num = 0x02;
				Dbg_Println(DBG_BIT_SYS_STATUS,"REG_ENERGY_BC_ADDR ERR!");
				break;
			}
		
			//当前没有发送任务
			if(u_det.WR_flag == SEND_NONE)		
			{
				BLV_HLW8110_RWCMD_Packaging(REG_ENERGY_BC_ADDR,0,0);	
				break;
			}
		break;
		case 0x0A:				//校表系数校验和
			if(Get_SendState() == SEND_STATE_SUCC)
			{
				if(g_CalFactor.U16_CheckSUM_RegData == HLW8110_SysPara_Check(&g_CalFactor))
				{
					//校验系数成功
					//状态机，读完所有系数切换读取系数寄存器读取完到下一状态，记录到flash
					g_Eng.rWCh_Num = 0x00;
					g_Eng.Tack_State = 0x02;
					Dbg_Println(DBG_BIT_SYS_STATUS,"REG_CHECK SUCC!");
					break;
				}
				else
				{
					g_Eng.rWCh_Num = 0x00;
				//	g_Eng.Tack_State = 0x02;
					Dbg_Println(DBG_BIT_SYS_STATUS,"REG_CHECK ERR:%x,%x",g_CalFactor.U16_CheckSUM_RegData,HLW8110_SysPara_Check(&g_CalFactor));
					break;
				}
			}
			else if(Get_SendState() == SEND_STATE_FAIL)		
			{
				Dbg_Println(DBG_BIT_SYS_STATUS,"REG_CHECKSUM Read ERR!");
				break;
			}

			//当前没有发送任务
			if(u_det.WR_flag == SEND_NONE)		
			{
				BLV_HLW8110_RWCMD_Packaging(REG_CHECKSUM_ADDR,0,0);	
				break;
			}
			break;	
		default:
			g_Eng.rWCh_Num = 0x00;
			g_Eng.Tack_State = 0x01;
		break;
	}
	
}

//写系统控制、计量控制寄存器
void HLW8110_ReadSysCtrlPara(void)
{
	switch(g_Eng.rWSysCtrl)
	{
		case 0x00:		//写系统控制寄存器
			if(Get_SendState() == SEND_STATE_SUCC)
			{
				g_Eng.rWSysCtrl = 0x01;
				break;
			}
			else if(Get_SendState() == SEND_STATE_FAIL)
			{
				Dbg_Println(DBG_BIT_SYS_STATUS,"REG_SYSCON_ADDR ERR!");
				break;
			}
			//当前没有发送任务
			if(u_det.WR_flag == SEND_NONE)		
			{
				BLV_HLW8110_RWCMD_Packaging(REG_SYSCON_ADDR,2,SYSCON_DEFAULT);
				break;	
			}
		break;
		case 0x01:		//写计量控制寄存器1
			if(Get_SendState() == SEND_STATE_SUCC)
			{
				g_Eng.rWSysCtrl = 0x02;
				break;
			}
			else if(Get_SendState() == SEND_STATE_FAIL)
			{
				g_Eng.rWSysCtrl = 0x00;
				Dbg_Println(DBG_BIT_SYS_STATUS,"REG_EMUCON1_ADDR ERR!");
				break;
			}
			//当前没有发送任务
			if(u_det.WR_flag == SEND_NONE)		
			{
				BLV_HLW8110_RWCMD_Packaging(REG_EMUCON1_ADDR,2,EMUCON1_DEFAULT);
				break;
			}
		break;
		case 0x02:		//写计量控制寄存器2
			if(Get_SendState() == SEND_STATE_SUCC)
			{
				g_Eng.rWSysCtrl = 0x03;
				break;
			}
			else if(Get_SendState() == SEND_STATE_FAIL)
			{
				g_Eng.rWSysCtrl = 0x00;
				Dbg_Println(DBG_BIT_SYS_STATUS,"REG_EMUCON2_ADDR ERR!");
				break;
			}
			//当前没有发送任务
			if(u_det.WR_flag == SEND_NONE)		
			{
				BLV_HLW8110_RWCMD_Packaging(REG_EMUCON2_ADDR,2,EMUCON2_DEFAULT);	
				break;
			}
		break;
		case 0x03:		//读取HFCONST常数,如果要使用就调整到此处
			if(Get_SendState() == SEND_STATE_SUCC)
			{
				g_Eng.rWSysCtrl = 0x00;
				g_Eng.Tack_State = 0x03;
				break;
			}
			else if(Get_SendState() == SEND_STATE_FAIL)
			{
				g_Eng.rWSysCtrl = 0x00;
				Dbg_Println(DBG_BIT_SYS_STATUS,"REG_HFCONST_ADDR ERR!");
				break;
			}
			//当前没有发送任务
			if(u_det.WR_flag == SEND_NONE)		
			{
				BLV_HLW8110_RWCMD_Packaging(REG_HFCONST_ADDR,0,0);	
				break;		
			}
		break;		
		default:
			g_Eng.rWSysCtrl = 0x00;
			g_Eng.Tack_State = 0x02;
		break;
	}
}

//读取电压电流有效值寄存器
void HLW8110_ReadValue(void)
{
	static U32_T Wait_ReadValue_Tisk = 0x00;
	
	switch(g_Eng.rDValue_State)
	{
		case READ_IDLE:				//等待时间
			if(SysTick_1ms - Wait_ReadValue_Tisk >= g_Eng.Collect_Tim)	//S读取一次
			{
				Wait_ReadValue_Tisk = SysTick_1ms;

				g_Eng.rDValue_State = READ_U;
				Dbg_Println(DBG_BIT_SYS_STATUS,"StartColl:%d",Wait_ReadValue_Tisk);
			}
		break;		
		case READ_U:		//读取电压寄存器
			if(Get_SendState() == SEND_STATE_SUCC)
			{
				g_Eng.rDValue_State = READ_IA;
				break;
			}
			else if(Get_SendState() == SEND_STATE_FAIL)
			{
				g_Eng.rDValue_State = READ_IDLE;
				Dbg_Println(DBG_BIT_SYS_STATUS,"REG_RMSU_ADDR ERR!");
				break;
			}
			//当前没有发送任务
			if(u_det.WR_flag == SEND_NONE)		
			{
				BLV_HLW8110_RWCMD_Packaging(REG_RMSU_ADDR,0,0);	
			}
		break;
		case READ_IA:		//读取A通道电流
			Wait_ReadValue_Tisk = SysTick_1ms;
			if(Get_SendState() == SEND_STATE_SUCC)
			{
				g_Eng.rDValue_State = READ_IB;
				break;
			}
			else if(Get_SendState() == SEND_STATE_FAIL)
			{
				g_Eng.rDValue_State = READ_IDLE;
				Dbg_Println(DBG_BIT_SYS_STATUS,"REG_RMSIA_ADDR ERR!");
				break;
			}
			//当前没有发送任务
			if(u_det.WR_flag == SEND_NONE)		
			{
				BLV_HLW8110_RWCMD_Packaging(REG_RMSIA_ADDR,0,0);	
			}
		break;	
		case READ_IB:		//读取B通道电流
			Wait_ReadValue_Tisk = SysTick_1ms;
			if(Get_SendState() == SEND_STATE_SUCC)
			{
				g_Eng.rDValue_State = READ_EA;
				break;
			}
			else if(Get_SendState() == SEND_STATE_FAIL)
			{
				g_Eng.rDValue_State = READ_IDLE;
				Dbg_Println(DBG_BIT_SYS_STATUS,"REG_RMSIB_ADDR ERR!");
				break;
			}
			//当前没有发送任务
			if(u_det.WR_flag == SEND_NONE)		
			{
				BLV_HLW8110_RWCMD_Packaging(REG_RMSIB_ADDR,0,0);	
			}
		break;
		case READ_EA:		//读取A通道有功电量寄存器
			if(Get_SendState() == SEND_STATE_SUCC)
			{
			
				g_Eng.rDValue_State = READ_EB;
				break;
			}
			else if(Get_SendState() == SEND_STATE_FAIL)
			{
				g_Eng.rDValue_State = READ_IDLE;
				Dbg_Println(DBG_BIT_SYS_STATUS,"REG_ENERGY_PA_ADDR ERR!");
				break;
			}
			//当前没有发送任务
			if(u_det.WR_flag == SEND_NONE)		
			{
				BLV_HLW8110_RWCMD_Packaging(REG_ENERGY_PA_ADDR,0,0);	
			}
		break;	
		case READ_EB:		//读取B通道有功电量寄存器
			if(Get_SendState() == SEND_STATE_SUCC)
			{
			
				g_Eng.rDValue_State = READ_PA;
				break;
			}
			else if(Get_SendState() == SEND_STATE_FAIL)
			{
				g_Eng.rDValue_State = READ_IDLE;
				Dbg_Println(DBG_BIT_SYS_STATUS,"REG_ENERGY_PB_ADDR ERR!");
				break;
			}
			
			
			//当前没有发送任务
			if(u_det.WR_flag == SEND_NONE)		
			{
				BLV_HLW8110_RWCMD_Packaging(REG_ENERGY_PB_ADDR,0,0);	
			}
		break;
		case READ_PA:		//读取A通道有功功率寄存器
			if(Get_SendState() == SEND_STATE_SUCC)
			{
				g_Eng.rDValue_State = READ_PB;
				break;
			}
			else if(Get_SendState() == SEND_STATE_FAIL)
			{
				g_Eng.rDValue_State = READ_IDLE;
				Dbg_Println(DBG_BIT_SYS_STATUS,"REG_POWER_PA_ADDR ERR!");
				break;
			}
			//当前没有发送任务
			if(u_det.WR_flag == SEND_NONE)		
			{
				BLV_HLW8110_RWCMD_Packaging(REG_POWER_PA_ADDR,0,0);	
			}
		break;			
		case READ_PB:		//读取B通道有功功率寄存器
			if(Get_SendState() == SEND_STATE_SUCC)
			{
				GetEnergy_Para();	
				g_Eng.rDValue_State = READ_IDLE;
				break;
			}
			else if(Get_SendState() == SEND_STATE_FAIL)
			{
				g_Eng.rDValue_State = READ_IDLE;
				Dbg_Println(DBG_BIT_SYS_STATUS,"REG_POWER_PB_ADDR ERR!");
				break;
			}
			
			//当前没有发送任务
			if(u_det.WR_flag == SEND_NONE)		
			{
				BLV_HLW8110_RWCMD_Packaging(REG_POWER_PB_ADDR,0,0);	
			}
		break;
		default:
		break;
	
	}
}

//HLW8110能耗执行任务
void BLV_HLW8110_Tack(void)
{
	//初始化1、2、3
	//1、读取系数寄存器的参数，比对，存入flash
	//2、设置系统控制寄存器、计量控制寄存器1、计量控制寄存器2
	
	//3、开始读取电压电流有效值（定时采集）
	static U32_T HLW_State_Tisk = 0x00;
	
	
	switch(g_Eng.Tack_State)
	{
		case 0x00:		//待机或定时,开机1000ms不设置
			if(SysTick_1ms - HLW_State_Tisk > 1000)
			{
				HLW_State_Tisk = SysTick_1ms;
				g_Eng.Tack_State = 0x01;
			}
		break;	
		case 0x01:		//读取系数寄存器的参数
			HLW8110_ReadSysPara();
		break;
		case 0x02:		//设置系统控制寄存器
			HLW8110_ReadSysCtrlPara();
		break;	
		case 0x03:		//开始读取电压电流有效值
			//定时读取电压、电流，时间可设置
			HLW8110_ReadValue();
			
			//标志位成立，复位HLW8112芯片
			if(g_Eng.tack_reset_flag == 0x01)
			{
				g_Eng.tack_reset_flag = 0x00;
				
				HLW8110_ClearFlag();
				HLW_State_Tisk = SysTick_1ms;
				g_Eng.Tack_State = 0x01;
				
			}
			
		break;			
		default:
		
		break;
	}

}

//打印采集数据
void GetEnergy_Para(void)
{
	Dbg_Println(DBG_BIT_SYS_STATUS,"HLW_V :%d /10mV",g_Eng.det_V);
	Dbg_Println(DBG_BIT_SYS_STATUS,"HLW_IA:%d, IB:%d /10mA",g_Eng.det_ectA,g_Eng.det_ectB);
	Dbg_Println(DBG_BIT_SYS_STATUS,"HLW_PA:%d, PB:%d /mW",g_Eng.det_powA,g_Eng.det_powB);
	Dbg_Println(DBG_BIT_SYS_STATUS,"HLW_EA:%d, EB:%d /W/h",(g_Eng.Total_Eng - g_Eng.LastPhase_Eng),(g_Eng.Total_Eng_B - g_Eng.LastPhase_Eng_B) );
	Dbg_Println(DBG_BIT_SYS_STATUS,"HLWTEA:%d, TEB:%d /W/h",g_Eng.Total_Eng,g_Eng.Total_Eng_B);
	Dbg_Println(DBG_BIT_SYS_STATUS,"HLW_HF:%d",g_Eng.hfcount);
}



//电能参数处理，主动上报标志、定时存储总电能
void BLV_ElectricPara_Processing(void)
{
	static U32_T Eng_Save_Tim = 0x00;
	static U8_T WriteEng_rev = 0x00;
	
	
	//2、定时上报
	if( (g_Dip.Dev_Port == Active_Port)&&(SysTick_1ms - g_Eng.Eng_Report_Tim >= g_Eng.Report_Tim) )
	{
		g_Eng.Eng_Report_Tim = SysTick_1ms;
		
		BLV_Energy_PassiveRep_Packing(0x01);
	}
	
}


//组包发送,用于蓝牙设置的回复
U8_T BT_Uart_Packing(U8_T *data,U8_T len,U8_T flag)
{
	if(flag <= 3 ) return 0x01;
	
	U8_T BT_Len = len;
	
	if(BT_Len > 20){
		BT_Len = 20;
	}
	
	memset(g_Eng.BT_SdBuff,0,20);
	memcpy(g_Eng.BT_SdBuff, data, BT_Len);
	g_Eng.BT_SdLen = BT_Len;	
	
	g_Eng.BT_SendFlag |= (0x01 << flag);
	
	return 0x00;	
}



//蓝牙打印调试
void BT_UART_Print(void)
{
	static U32_T BT_State = 0x00;
	static U32_T BT_WaitOutTim = 0x00;
	static U32_T BT_Print_Tim = 0x00;
	
	if( ((Dbg_Switch&0x08) != 0x00)&&(SysTick_1ms - g_Eng.BT_Print_valid >= 180000) )
	{
		g_Eng.BT_Print_valid = SysTick_1ms;
		
		Dbg_Switch &= (~0x08);
	}
	
	
	for(U8_T i = 10;i > 3;i--)
	{
		if( (g_Eng.BT_SendFlag & (0x01 << i )) != 0x00 )
		{
			BT_State = i;
			break;
		}
	}
	
	if(SysTick_1ms - BT_Print_Tim >= 150)
	{
		BT_Print_Tim = SysTick_1ms;
		switch(BT_State)
		{
			case 0x00:
				BT_State = 0x01;
				Dbg_BT_Println(DBG_BIT_BT_STATUS,"V:%dmV",g_Eng.det_V*10);
			break;
			case 0x01:
				BT_State = 0x02;
				Dbg_BT_Println(DBG_BIT_BT_STATUS,"I:%d,%dmA",(g_Eng.det_ectA*10),(g_Eng.det_ectB*10));
			break;			
			case 0x02:
				BT_State = 0x03;
				Dbg_BT_Println(DBG_BIT_BT_STATUS,"P:%d,%dmW",g_Eng.det_powA,g_Eng.det_powB);
			break;
			case 0x03:
				BT_State = 0x04;
				BT_WaitOutTim = SysTick_1ms;
				Dbg_BT_Println(DBG_BIT_BT_STATUS,"E:%d,%d",(g_Eng.Total_Eng - g_Eng.LastPhase_Eng),(g_Eng.Total_Eng_B - g_Eng.LastPhase_Eng_B));
			break;
			case 0x04:
				BT_State = 0x05;
				BT_WaitOutTim = SysTick_1ms;
				Dbg_BT_Println(DBG_BIT_BT_STATUS,"T:%d,%d",g_Eng.Total_Eng,g_Eng.Total_Eng_B);
			break;			
			case 0x05:
				if(SysTick_1ms - BT_WaitOutTim >= 3000)
				{
					BT_WaitOutTim = SysTick_1ms;
					BT_State = 0x00;
				}
			break;
			case REG_BTDEBUG_FLAG:
				g_Eng.BT_SendFlag &= (~(0x01 << REG_BTDEBUG_FLAG));
				
				BT_State = 0x05;
				BT_WaitOutTim = SysTick_1ms;
				DBG_BTPrintf(g_Eng.BT_SdBuff,g_Eng.BT_SdLen );
			break;	
			default:
				g_Eng.BT_SendFlag &= (~0xFFE0);
				BT_State = 0x03;
			break;				
		}
		
	}		
}

//能耗检测总任务函数
void BLV_EngDetect_Tack(void)
{
	//1、HLW初始化设置、读取
	BLV_HLW8110_Tack();
		
	//2、HLW发送命令状态机
	BLV_HLW8110_SendData_Tack();
	
	//3、保存、上报任务
	BLV_ElectricPara_Processing();
	
	//4、蓝牙打印
	BT_UART_Print();

}







//电参数查询回复组包
void BLV_Energy_PassiveRep_Packing(U8_T cmd)
{
	U8_T send_data[50] = {0};
	U16_T send_len = 0x00;
	U32_T Energy_A_Rep = 0,Energy_B_Rep = 0;
	
	Energy_A_Rep = g_Eng.Total_Eng - g_Eng.LastPhase_Eng;
	Energy_B_Rep = g_Eng.Total_Eng_B - g_Eng.LastPhase_Eng_B;

	
	g_Eng.SdSN++;
	if(g_Eng.SdSN > 0x0F)	g_Eng.SdSN = 0x00;

	send_data[send_len++] = ENERGY_HEAD;	//帧
	send_data[send_len++] = g_Eng.SdSN;		//sn;
	send_data[send_len++] = DEV_TYPE;		//type
	send_data[send_len++] = g_Dip.addr;
	send_data[send_len++] = 0x00;			//LEN
	send_data[send_len++] = 0x00;			//sum
	send_data[send_len++] = cmd;			//cmd,0x01:主动上报，0x02：轮询回复
	
	send_data[send_len++] = 0x03;			//采集通道,一个bit置1代表一个通道
	
	send_data[send_len++] =	( (g_Eng.det_V) & 0xff);
	send_data[send_len++] = (((g_Eng.det_V)>>8) & 0xff);
	
	send_data[send_len++] =	( g_Eng.det_ectA & 0xff);
	send_data[send_len++] = ((g_Eng.det_ectA >> 8) & 0xff);

	send_data[send_len++] =	( g_Eng.det_powA & 0xff);
	send_data[send_len++] = ((g_Eng.det_powA >> 8) & 0xff);
	send_data[send_len++] =	((g_Eng.det_powA >> 16) & 0xff);	
	send_data[send_len++] = ((g_Eng.det_powA >> 24) & 0xff);

	send_data[send_len++] =	( Energy_A_Rep & 0xff);
	send_data[send_len++] = ((Energy_A_Rep>> 8) & 0xff);
	send_data[send_len++] =	((Energy_A_Rep >> 16) & 0xff);	
	send_data[send_len++] = ((Energy_A_Rep>> 24) & 0xff);

	send_data[send_len++] =	( g_Eng.Total_Eng & 0xff);	
	send_data[send_len++] = ((g_Eng.Total_Eng >> 8) & 0xff);
	send_data[send_len++] =	((g_Eng.Total_Eng >> 16) & 0xff);	
	send_data[send_len++] = ((g_Eng.Total_Eng >> 24) & 0xff);


	/*** B通道 ***/
	send_data[send_len++] =	( (g_Eng.det_V) & 0xff);
	send_data[send_len++] = (((g_Eng.det_V)>>8) & 0xff);
	
	send_data[send_len++] =	( g_Eng.det_ectB & 0xff);
	send_data[send_len++] = ((g_Eng.det_ectB >> 8) & 0xff);

	send_data[send_len++] =	( g_Eng.det_powB & 0xff);
	send_data[send_len++] = ((g_Eng.det_powB >> 8) & 0xff);
	send_data[send_len++] =	((g_Eng.det_powB >> 16) & 0xff);	
	send_data[send_len++] = ((g_Eng.det_powB >> 24) & 0xff);

	send_data[send_len++] =	( Energy_B_Rep & 0xff);
	send_data[send_len++] = ((Energy_B_Rep>> 8) & 0xff);
	send_data[send_len++] =	((Energy_B_Rep >> 16) & 0xff);	
	send_data[send_len++] = ((Energy_B_Rep>> 24) & 0xff);

	send_data[send_len++] =	( g_Eng.Total_Eng_B & 0xff);	
	send_data[send_len++] = ((g_Eng.Total_Eng_B >> 8) & 0xff);
	send_data[send_len++] =	((g_Eng.Total_Eng_B >> 16) & 0xff);	
	send_data[send_len++] = ((g_Eng.Total_Eng_B >> 24) & 0xff);

	
	send_data[send_len++] = ENERGY_TAIL;	//帧尾
	
	send_data[4] = send_len;
	send_data[5] = HLW8110_CheckSum(send_data,send_len);
	
	if(cmd == 0x02){
		Set_GroupSend(send_data,send_len,1,BUSSend_WaitTime1,50);	//轮询回复组包
	}else if(cmd == 0x01){
		Set_GroupSend(send_data,send_len,BUSSendCnt1,BUSSend_WaitTime2,BUSSend_Tick);	//主动上报组包
	}
	
	
}

//MCU设置周期
U8_T BLV_Energy_SetTime_Packing(U8_T *data,U16_T lens)
{
	if(lens < 18) return 0x01;
	
	U32_T temp_T = 0;
	
	if( ( (data[7])&0x01) == 0x01)
	{
		temp_T = (data[11]<<24) + (data[10]<<16) + (data[9]<<8) + data[8];
		if(temp_T >= 1000){
			g_Eng.Collect_Tim = temp_T;
		}
	}

	if(data[12] == 0x00)
	{
		g_Dip.Dev_Port = Passive_Port;
	}
	else{
		g_Dip.Dev_Port = Active_Port;
	}
	
	if( ((data[7] >> 1)&0x01) == 0x01)
	{
		temp_T = (data[16]<<24) + (data[15]<<16) + (data[14]<<8) + data[13];
		if(temp_T >= 1000){
			g_Eng.Report_Tim = temp_T;
		}
	}
	
	//保存参数
	EEPROM_ENERGY_Validate();
	
	//数据原样返回
	Set_GroupSend(data,lens,1,BUSSend_WaitTime1,20);	//组包
	BT_Uart_Packing(data,lens,REG_BTDEBUG_FLAG);
	
	return 0x00;
}

//版本查询回复组包
void BLV_Energy_QueryVersion_Packing(void)
{
	U8_T send_data[50] = {0};
	U16_T send_len = 0x00;
	
	g_Eng.SdSN++;
	if(g_Eng.SdSN > 0x0F)	g_Eng.SdSN = 0x00;

	send_data[send_len++] = ENERGY_HEAD;
	send_data[send_len++] = g_Eng.SdSN;	//sn;
	send_data[send_len++] = DEV_TYPE;		//type
	send_data[send_len++] = g_Dip.addr;
	send_data[send_len++] = 0x00;		//LEN
	send_data[send_len++] = 0x00;		//sum
	
	send_data[send_len++] = 0x04;		//cmd
	
	send_data[send_len++] = Project_FW_Version;
	send_data[send_len++] =	Project_HW_Version;
	send_data[send_len++] = g_Dip.Dev_Port;
	
	send_data[send_len++] = ENERGY_TAIL;	
	
	send_data[4] = send_len;
	send_data[5] = HLW8110_CheckSum(send_data,send_len);
	
	Set_GroupSend(send_data,send_len,1,BUSSend_WaitTime1,50);	//组包
	BT_Uart_Packing(send_data,send_len,REG_BTDEBUG_FLAG);
}


//串口设置硬件校准系数
U8_T SetCalibFactor_Switch(U8_T ch,F32_T value)
{
	switch(ch)
	{
		case 0x00:
			g_Eng.V_K_Val = value;
		break;
		case 0x01:
			g_Eng.IA_K_Val = value;
		break;
		case 0x02:
			g_Eng.IB_K_Val = value;
		break;
		case 0x03:
			g_Eng.PA_K_Val = value;
		break;
		case 0x04:
			g_Eng.PB_K_Val = value;
		break;
		default:		
		break;
	}
	return 0;
}

//设置硬件校准值
U8_T BLV_Energy_SetCalibFactor(U8_T *data,U16_T lens)
{
	if(lens < 10) return 0x01;	
	
	F32_T temp_T = 0.0;
	
	U8_T ctrl_num = ((data[7])&0xff);
	U8_T temp_num = ((data[8])&0xff);
	
	if( ctrl_num > 0x04) return 0x02;
	if(temp_num > 5) 
	{
		temp_num = 5;
	}
	
	for(U8_T i = 0;i< temp_num;i++)
	{
		temp_T = 0.0;
		temp_T = ( data[(i*3+9)] + (data[(i*3+10)]<<8) + (data[(i*3+11)]<<16) );
		temp_T /= 100000.0;
		SetCalibFactor_Switch(ctrl_num+i,temp_T);
	}
	
	//保存参数
	EEPROM_ENERGY_Validate();

	Set_GroupSend(data,lens,1,BUSSend_WaitTime1,20);	//组包	
	
	return 0x00;
}

//设置硬件校准值
U8_T BLV_Energy_SetCalibFactor_2(U8_T *data,U16_T lens)
{
	if(lens < 10) return 0x01;	
	
	F32_T temp_T = 0.0;
	
	U8_T ctrl_num = ((data[7])&0xff);
	U8_T temp_num = ((data[8])&0xff);
	
	if( ctrl_num > 0x05) return 0x02;
	if(temp_num > 3) 
	{
		temp_num = 3;
	}
	
	for(U8_T i = 0;i< temp_num;i++)
	{
		temp_T = 0.0;
		temp_T = ( data[(i*3+9)] + (data[(i*3+10)]<<8) + (data[(i*3+11)]<<16) );
		temp_T /= 100000.0;
		SetCalibFactor_Switch(ctrl_num+i,temp_T);
	}
	
	//保存参数
	EEPROM_ENERGY_Validate();
	
	//数据原样返回
	BT_Uart_Packing(data,lens,REG_BTDEBUG_FLAG);						//蓝牙发送
	
	return 0x00;
}


//485处理函数
U8_T BLV_485Recv_Processing(U8_T *RecvData,U16_T RecvLen)
{
	
	if(RecvLen < 8){
		return 0x01;
	}
	
	if(RecvData[4] != RecvLen){
		Dbg_Println(DBG_BIT_SYS_STATUS,"%s Data Len Error",__func__);
		return 0x02;
	}
	
	if( (RecvData[0] != ENERGY_HEAD) || (RecvData[RecvLen -1] != ENERGY_TAIL) ){
		Dbg_Println(DBG_BIT_SYS_STATUS,"%s Head Error",__func__);
        return 0x03;
	}
	
	if(RecvData[2] != DEV_TYPE){//
		Dbg_Println(DBG_BIT_SYS_STATUS,"%s TYPE Error",__func__);
		return 0x04;
	}
	
	if(RecvData[3] != g_Dip.addr){//
		Dbg_Println(DBG_BIT_SYS_STATUS,"%s Addr Error",__func__);
		return 0x05;
	}

	if( HLW8110_CheckSum(RecvData,RecvLen) != 0x00 ){
        Dbg_Println(DBG_BIT_SYS_STATUS,"%s Check Error",__func__);
        return 0x06;
    }	
	
	g_Eng.SdSN = RecvData[1];	//序列号
	
	//解析指令
	Dbg_Println(DBG_BIT_SYS_STATUS,"HLW8110_Recv CMD:%02X",RecvData[6]);	
	switch(RecvData[6])
	{
		case 0x01:	//主动上报的回复
			Clear_SendFlag();
		break;
		case 0x02:	//查询
			BLV_Energy_PassiveRep_Packing(0x02);
		break;
		case 0x3:	//设置采集周期
			BLV_Energy_SetTime_Packing(RecvData,RecvLen);
		break;		
		case 0x04:	//查询版本号
			BLV_Energy_QueryVersion_Packing();
		break;
		case 0x13:	//清零上报能耗
			g_Eng.LastPhase_Eng = g_Eng.Total_Eng;
			g_Eng.LastPhase_Eng_B = g_Eng.Total_Eng_B;	
			
			g_Eng.Eng_Report_Tim = SysTick_1ms;

			Set_GroupSend(RecvData,RecvLen,1,BUSSend_WaitTime1,20);	//组包
		break;
		case 0x14:	//清零总能耗
			
			g_Eng.Total_Eng = 0;
			g_Eng.U32_EnergyA_RegData = 0x00;
			g_Eng.LastPhase_Eng = g_Eng.Total_Eng;

			g_Eng.Total_Eng_B = 0;
			g_Eng.U32_EnergyB_RegData = 0x00;
			g_Eng.LastPhase_Eng_B = g_Eng.Total_Eng_B;			
			
			memset(&g_engfram,0,sizeof(FRAM_ENG_t));
			FRAM_WriteTotalEng(&g_engfram);

			Set_GroupSend(RecvData,RecvLen,1,BUSSend_WaitTime1,20);	//组包
		break;	
		case 0x15:	//设置校准系数
			BLV_Energy_SetCalibFactor(RecvData,RecvLen);
		break;	
		case 0x21:	//开关打印信息
			Dbg_Switch = (RecvData[7]&0x1f);
			Set_GroupSend(RecvData,RecvLen,1,BUSSend_WaitTime1,20);	//组包
			g_Eng.BT_Print_valid = SysTick_1ms;
		break;			
		default:
			return 0x01;
		break;
	}
	
	return 0;	
}



//BT处理函数
U8_T BT_Recv_Processing(U8_T *RecvData,U16_T RecvLen)
{
	if(RecvLen < 8){
		Dbg_BT_Println(DBG_BIT_BT_STATUS,"LenMaxErr:%d",RecvLen);
		return 0x01;
	}
	
	if(RecvData[4] != RecvLen){
		Dbg_BT_Println(DBG_BIT_BT_STATUS,"Len1Err:%d",RecvLen);
		return 0x02;
	}
	
	if( (RecvData[0] != ENERGY_HEAD) || (RecvData[RecvLen -1] != ENERGY_TAIL) ){
		Dbg_BT_Println(DBG_BIT_BT_STATUS,"HeadErr");
        return 0x03;
	}
	
	if(RecvData[2] != DEV_TYPE){//
		Dbg_BT_Println(DBG_BIT_BT_STATUS,"TYPEErr");
		return 0x04;
	}
	
	if(RecvData[3] != g_Dip.addr){//
		Dbg_BT_Println(DBG_BIT_BT_STATUS,"AddrErr");
		return 0x05;
	}

	if( HLW8110_CheckSum(RecvData,RecvLen) != 0x00 ){
		Dbg_BT_Println(DBG_BIT_BT_STATUS,"CheckErr");
        return 0x06;
    }	
	
	g_Eng.SdSN = (RecvData[1]&0x0F);	//序列号
	
	//解析指令
	//Dbg_BT_Println(DBG_BIT_BT_STATUS,"Recv CMD:%02X",RecvData[6]);	
	switch(RecvData[6])
	{
		case 0x02:	//查询
			//BLV_Energy_PassiveRep_Packing();
		break;
		case 0x3:	//设置采集周期
			BLV_Energy_SetTime_Packing(RecvData,RecvLen);
		break;		
		case 0x04:	//查询版本号
			BLV_Energy_QueryVersion_Packing();
		break;
		case 0x13:	//清零上报能耗
			g_Eng.LastPhase_Eng = g_Eng.Total_Eng;
			g_Eng.LastPhase_Eng_B = g_Eng.Total_Eng_B;	
			g_Eng.Eng_Report_Tim = SysTick_1ms;
			BT_Uart_Packing(RecvData,RecvLen,REG_BTDEBUG_FLAG);
		break;
		case 0x15:	//设置校准系数
			BLV_Energy_SetCalibFactor_2(RecvData,RecvLen);
		break;			
		case 0x21:		//开启打印
			if( (RecvData[7]&0x08) != 0x00 ){
				Dbg_Switch |= 0x08;
				g_Eng.BT_Print_valid = SysTick_1ms;
			}else{
				Dbg_Switch &= (~0x08);
			}
			BT_Uart_Packing(RecvData,RecvLen,REG_BTDEBUG_FLAG);
		break;		
		default:
		return 0x01;
		break;
	}
	
	return 0;	
}


