var ew_code_update_init_flag = 0;
var EWB_CMD_CTR = 6;
function EwCodeUpdate()
{
	if(ew_code_update_init_flag == 0)
	{
		ShowCommMes("程序更新脚本加载成功",MesgType.MESG_TYPE_INFO);
		ew_code_update_init_flag =1;
		var content_str = '<div class="layui-row" style="line-height: 1.8rem; padding-top: 2px;">';
		
		content_str +=  '<div class="layui-col-xs2">'
		content_str += '<button type="button" class="layui-btn layui-btn-normal plugin_bind_test" style="height: 1.8rem; line-height: 1.8rem;width: 90%;" onclick="EwCodeUpdateExcue()">升级程序</button>';
		content_str += '</div> ';
		
		content_str +=  '<div class="layui-col-xs2">'
		content_str += '<button type="button" class="layui-btn layui-btn-normal plugin_bind_test" style="height: 1.8rem; line-height: 1.8rem;width: 90%;" onclick="EWB_Chg(1)">充电</button>';
		content_str += '</div> ';
		
		content_str +=  '<div class="layui-col-xs2">'
		content_str += '<button type="button" class="layui-btn layui-btn-normal plugin_bind_test" style="height: 1.8rem; line-height: 1.8rem;width: 90%;" onclick="EWB_EraseCode()">擦除代码</button>';
		content_str += '</div> ';
		
		content_str +=  '<div class="layui-col-xs2">'
		content_str += '<button type="button" class="layui-btn layui-btn-normal plugin_bind_test" style="height: 1.8rem; line-height: 1.8rem;width: 90%;" onclick="EWB_UpdateCode()">升级代码</button>';
		content_str += '</div> ';
		
		content_str +=  '<div class="layui-col-xs2">'
		content_str += '<button type="button" class="layui-btn layui-btn-normal plugin_bind_test" style="height: 1.8rem; line-height: 1.8rem;width: 90%;" onclick="EWB_RunApp()">运行App</button>';
		content_str += '</div> ';
		
		content_str +=  '<div class="layui-col-xs2">'
		content_str += '<button type="button" class="layui-btn layui-btn-normal plugin_bind_test" style="height: 1.8rem; line-height: 1.8rem;width: 90%;" onclick="EWB_AppSleep()">休眠App</button>';
		content_str += '</div> ';
		
		content_str +='</div>';
		 $("#Oper_View").append(content_str);
		return;
	}
}

async function EWB_AppSleep()
{
	var param = [];
	var cmd_buf = Ep2_CmdData(1,60,param,0,0,EP2_CMM_RATE);
	ep2_excuedcmd = 0;
	epro2_oper.cmd = 0xFF;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0){
		ShowCommMes("执行休眠成功",MesgType.MESG_TYPE_INFO);
	}
	
}

async function Ep2_ChipRunBoot()
{
	var param = [];
	param[0] =0x99;
	param[1] = 0x66;
	param[2] = 0xAA;
	param[3] = 0x55;
	var cmd_buf = Ep2_CmdData(0,61,param,0,0,EP2_CMM_RATE);
	ep2_excuedcmd = 0;
	epro2_oper.cmd = 0xFF;
	Ep2_SendCmd(cmd_buf);
	var rtv = await Ep2_WaitCmdExcu(epro2_oper.cmd,1000);
	if(rtv == 0){
		ShowCommMes("芯片复位执行成功",MesgType.MESG_TYPE_INFO);
	}
}

async function EWB_Chg(sw)
{
	var cmd_buf = [];
	cmd_buf[0] = sw;
	epro2_oper.ctr_cmd = EWB_CMD_CTR;
	epro2_oper.cmd = 0x01;
	Ep2_SendCmd(cmd_buf);
	await  TransSleep(200);
}
async function EWB_EraseCode()
{
	var cmd_buf = [];
	epro2_oper.ctr_cmd = EWB_CMD_CTR;
	epro2_oper.cmd = 0x02;
	Ep2_SendCmd(cmd_buf);
	await  TransSleep(200);
}

async function EWB_UpdateCode()
{
	var cmd_buf = [];
	var code_addr = 0x1000;
	var code_size = 11*1024;
	var err_close = 0;//1 写入出现错误终止 0 错误继续执行
	var w_size = 0;
	cmd_buf[0] = code_addr & 0x0FF;
	cmd_buf[1] = (code_addr >> 8) & 0x0FF;
	cmd_buf[2] = code_size & 0x0FF;
	cmd_buf[3] = (code_size >> 8) & 0x0FF;
	cmd_buf[4] = err_close;
	//05334577249
	epro2_oper.ctr_cmd = EWB_CMD_CTR;
	epro2_oper.cmd = 0x06;
	Ep2_SendCmd(cmd_buf);
	var time_out = 0;
	ep2_excued_rtv = 0;
	ep2_excuedcmd = 0;
	while(time_out < 100)
	{
	    if(ep2_excuedcmd == 0x06)
	    {
			time_out = 0;
			
		  if(ep2_excued_rtv == 0)
		  {
			  w_size =  ep2_recive_buf[11]*256+ep2_recive_buf[10];
			  ShowCommMes("已经下载程序 "+w_size+" 字节",MesgType.MESG_TYPE_INFO);
		  }else{
			  
			  ShowCommMes("已经下载程序 "+w_size+" 字节 下载异常",MesgType.MESG_TYPE_ERROR);
			  break;
		  }
	     
		  ep2_excuedcmd = 0;
		  ep2_excued_rtv = 0;
		  
	    }
		if(w_size >= code_size)
		{
			break;
		}
	    await  TransSleep(10);
	    time_out++;
	}
}
async function EWB_RunApp()
{
	var cmd_buf = [];
	epro2_oper.ctr_cmd = EWB_CMD_CTR;
	epro2_oper.cmd = 0x05;
	Ep2_SendCmd(cmd_buf);
	await  TransSleep(200);
}

async function EwCodeUpdateExcue()
{
	//使能MTP
	epro2_oper.commaddr = 1;
	epro2_oper.cmd = 29;
	await Ep2_EnMTPWrite(1);
	await  TransSleep(300);
	//用户程序跳转到bootloader
	await Ep2_ChipRunBoot();
	//关闭总线
	await Ep2_BusSwitch(0);
	await  TransSleep(5000);
	//开总线
	epro2_oper.ep2_heartbeat_sw = 0;
	await Ep2_HeartBeat_SW();
	await  TransSleep(100);
	await Ep2_BusSwitch(1);
	await  TransSleep(300);
	//执行充电命令
	EWB_Chg(1);
	//等待充电
	await  TransSleep(5000);
	
	EWB_Chg(1);
	await  TransSleep(5000);
	//擦除用户Code区
	EWB_EraseCode();
	await  TransSleep(1000);
	//升级程序
	await EWB_UpdateCode();
	//跳转用户命令
	await  TransSleep(1000);
	await EWB_RunApp();
	
}