can_bootloader/user/DiagBootcode.aardio
2022-05-17 16:34:44 +08:00

308 lines
5.3 KiB
Plaintext

var bootstate = 0;
var bootcount = 0;
var sendstate = 0;
FuncWait = function(sid){
if(RespState == (sid + 0x40)){//正响应
//bootcount = 0;
return 0;
}
errsid,errnrc = FuncGetNrc();
FuncClrNrc();
if(errsid == sid){
if(errnrc == 0x78){//等待
bootcount = 0;
return 0xff;
}
else {
FuncDisplay("刷写错误");
boottimer.disable();
return 1; //负响应
}
}
if(stopflag == 1){
FuncDisplay("停止");
boottimer.disable();
return 2;//停止命令
}
bootcount += 1;
if(bootcount >= 400){
FuncDisplay("超时 " + tostring(sid,16));
boottimer.disable();
return 3;//超时
}
}
FuncStopBoot = function(){
stopflag = 1;
}
FuncClearState = function(){
RespState = 0;
bootcount = 0;
sendstate = 1;
}
FuncInitBootState = function(){
bootstate = 0;
RespState = 0;
bootcount = 0;
sendstate = 0;
}
nextstate = function(val){
if(val == 0){
bootstate += 1;
sendstate = 0;
}
}
BCD_Conv = function(num){
if(num > 99){
return 0;
}
return (num/10)*16 + (num%10);
}
var sendblockstate = 0;
var alldata = {};
var blocknum = 1;
FuncSendBlockInit = function(){
alldata = FuncGetAllData();
blocknum = 1;
if(alldata != null){
blocknum = 1;
FuncStartSendBlock(alldata[blocknum].["data"])
}
}
var FuncSendBlockApp_sendflag = 0
FuncSendBlockApp = function(){
select(sendblockstate) {
case 0{
if(FuncSendBlockApp_sendflag == 0){
FuncReq34(alldata[blocknum].["address"],#alldata[blocknum].["data"]);
FuncSendBlockApp_sendflag = 1;
RespState = 0;
bootcount = 0;
}
else {
if(FuncWait(0x34) == 0){
FuncSendBlockApp_sendflag = 0;
sendblockstate += 1;
}
}
}
case 1 {
FuncStartSendBlock(alldata[blocknum].["data"]);//初始化
FuncSendBlockApp_sendflag = 0;
sendblockstate += 1;
}
case 2 {
if(FuncSendBlockApp_sendflag == 0){
FuncSendNextBlock();
FuncSendBlockApp_sendflag = 1;
RespState = 0;
bootcount = 0;
}
else {
if(FuncWait(0x36) == 0){
FuncSendBlockApp_sendflag = 0;
if(FuncGetSendStatus() == true){//所有块发送完成
sendblockstate += 1;
}
}
}
}
case 3 {
if(FuncSendBlockApp_sendflag == 0){
FuncReq37();
FuncSendBlockApp_sendflag = 1;
RespState = 0;
bootcount = 0;
}
else {
if(FuncWait(0x37) == 0){
FuncSendBlockApp_sendflag = 0;
sendblockstate += 1;
}
}
}
else {
if(blocknum < #alldata){
blocknum += 1;
sendblockstate = 0;
}
else {
//所有数据发送完成
return 0;
}
}
}
return 1;
}
//boot总流程
FuncBootSeq = function(){
select(bootstate) {
case 0 {
//读取当前会话
if(sendstate == 0){
FuncReadDID(0xF186);
FuncClearState();
}
else {
var ret = FuncWait(0x22);
nextstate(ret);
}
}
case 1 {
if(sendstate == 0){
FuncReadDID(0xF195);//读取软件版本
FuncClearState();
}
else {
var ret = FuncWait(0x22);
nextstate(ret);
}
}
case 2 {
if(sendstate == 0){
FuncReadDID(0xF192);//读取硬件版本
FuncClearState();
}
else {
var ret = FuncWait(0x22);
nextstate(ret);
}
}
case 3 {
if(sendstate == 0){
FuncReq10(true,0x03);//进入扩展会话
FuncClearState();
}
else {
var ret = FuncWait(0x10);
nextstate(ret);
}
}
case 4 {
if(sendstate == 0){
FuncReq85(0x02);//关闭DTC
FuncClearState();
}
else {
var ret = FuncWait(0x85);
nextstate(ret);
}
}
case 5 {
if(sendstate == 0){
FuncReq28(0x01,0x01);//禁止发送
FuncClearState();
}
else {
var ret = FuncWait(0x28);
nextstate(ret);
}
}
case 6 {
if(sendstate == 0){
FuncReq10(false,0x02);//进入编程会话
FuncClearState();
}
else {
var ret = FuncWait(0x10);
nextstate(ret);
}
}
case 7 {
if(sendstate == 0){
FuncReq27(01);//解密
FuncClearState();
}
else {
var ret = FuncWait(0x27);
nextstate(ret);
}
}
case 8 {
if(sendstate == 0){
//FuncReq27(02);//发送key
FuncClearState();
}
else {
var ret = FuncWait(0x27);
nextstate(ret);
}
}
case 9 {
if(sendstate == 0){
FuncDIDWriteStr(0xf198,"0123456789abcdef1234");//写入repair_shopcode
FuncClearState();
}
else {
var ret = FuncWait(0x2E);
nextstate(ret);
}
}
case 10 {
if(sendstate == 0){//写入installation_date
var year1 = BCD_Conv(time.now().year/100);
var year2 = BCD_Conv(time.now().year%100);
var month = BCD_Conv(time.now().month);
var day = BCD_Conv(time.now().day);
FuncReq2E(0XF199,{year1,year2,month,day})
FuncClearState();
}
else {
var ret = FuncWait(0x2E);
nextstate(ret);
}
}
case 11 {
if(sendstate == 0){//擦除flash
FuncReq31EraseFlash(0x00FE0000,0x00019FE0);//
FuncClearState();
}
else {
var ret = FuncWait(0x31);
nextstate(ret);
}
}
case 12 {
if(sendstate == 0){//发送数据
FuncSendBlockInit();
FuncClearState();
}
else {
var ret = FuncSendBlockApp();
nextstate(ret);
}
}
case 13 {
if(sendstate == 0){//发送数据
FuncSendBlockInit();
FuncClearState();
}
else {
var ret = FuncSendBlockApp();
nextstate(ret);
}
}
else {
FuncDisplay("刷写完成")
boottimer.disable();
}
}
}