基于canoe的bootload刷写程序
/*@!Encoding:936*/
includes
{
}
variables
{
long mNewPage;
message 0x722 ecu_rx_phy;
message 0x711 ecu_rx_fun;
message 0x745 ecu_rx;
message 0x722 ecu_rx_3E00;
msTimer TesterPresent_3E;
msTimer boot_order;
msTimer countinue_36; //为防止打印跟不上速度
int file_check_num; //判断有几个文件需要刷写
int boot_check=1;
int TesterPresent_3E_start=0;
byte make_key[4],send_key[4];
int step_control; //用来判断进行到哪一步 ,因为有多个返回值相同,用于区分
long fileHandle = 0; //获取文件句柄
long file_line=0; //行计数
const int bufferSize=100; //s19文件行数据长度
char buffer[bufferSize]; //行字节数
byte file_byte_all[4]; //文件字节总数
byte start_address[4]; //起始地址
int choice; //选择文件
byte file_send_all[1025]; //将s19行里面的数据进行读取
long file_send_all_byte=0; //进行行字节计数
long file_send_all_byte_line=0; //进行读取行的读取字节数计数
long file_send_max; //每次允许发送字节数
int end_flag=0; //结束标志位,1代表结束,用来判断文件是否读取完成
byte file_send_step=0x01; //发送文件,一共进行了几次
int Suppress_negative_response=0; //36服务执行过程中可能会有否定响应,
long msg_time_control=0; //报文发送时间太短,设置相当于for函数i++使用
msTimer msg_time_1ms; //设置报文间隔1ms发送一条
byte mcg_byt0=0x20; //控制21-2f循环发送报文
int DE_1001=0; //1001判断标志
dword crc =0xFFFFFFFF; //crc校验初始值
dword Crc32Table[256] = //crc表
{
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
};
}
on timer TesterPresent_3E
{
if(@sysvar::env_set::startBtn == 1)
{
ecu_rx_3E00.dlc=8;
ecu_rx_3E00.byte(0)=0x02;
ecu_rx_3E00.byte(1)=0x3E;
ecu_rx_3E00.byte(2)=0x00;
output(ecu_rx_3E00);
// setTimer(TesterPresent_3E,4000);
}
else
{
write("TesterPresent Stop");
}
}
on timer countinue_36
{
file_send_message();TransferData_36();
}
on timer boot_order
{
switch (boot_check)
{
case 1: //进入1003
Init_ecu_rx_phy();SwitchToExtendedSession_1003();break;
case 2://检查预编程条件
Init_ecu_rx_phy();RoutineControl_31010203();break;
case 3://控制DTC关闭
Init_ecu_rx_phy();ControlDTCSetting_8502();break;
case 4://通信控制,禁止收发非诊断报文
Init_ecu_rx_phy();CommunicationControl_2803();break;
case 5://读写数据 待定
break;
case 6: //进入编程会话
Init_ecu_rx_phy();ProgrammingSession_1002();break;
case 7://过安全访问
Init_ecu_rx_phy();SecurityAccess_27();break;
case 14://擦除内存
Init_ecu_rx_phy();Get_Address_and_byteNum();eraseMemory_3101FF00();break;
case 8://请求下载
Init_ecu_rx_phy();RequestDownload_34();break;
case 9://传输数据
Suppress_negative_response=1;file_send_message();TransferData_36();break;
case 10://请求退出传输
RequestTransferExit_37();break;
case 15://检查编程完整性
CheckProgrammingIntegrity_31010202();break;
case 11://检查编程依赖性
Init_ecu_rx_phy();voidRoutineControl_3101FF01();break;
case 12://硬件复位
Init_ecu_rx_phy();EcuReset_1101();break;
case 13://切换默认会话
Init_ecu_rx_phy();DefaultSession_1001();break;
}
if(@sysvar::env_set::startBtn==1)
{
setTimer(boot_order,100);
}
}
//流程控制,根据返回的响应进行下一步,此步为响应报文
on message 0x745
{
if(!(this.byte(1)==0x7E && this.byte(2)== 0x00 && this.byte(0)==0x02))//判断是不是3E00维持帧的响应
{
putValueToControl("bootpanel","OutputTrace",this,0,1); //将响应打印
putValueToControl("bootpanel","OutputTrace","\r");
writeLineEx(mNewPage,1,"%x %2x %2x %2x %2x %2x %2x %2x %2x",this.id,this.byte(0),this.byte(1),this.byte(2),this.byte(3),this.byte(4),this.byte(5),this.byte(6),this.byte(7));
writeLineEx(mNewPage,1," ");
if(this.byte(1)==0x50 && ecu_rx_phy.byte(2)==0x03 )//1003返回值
{
boot_check=2; //此项为判断像一部如何进行标志,具体见on timer boot_order
}
else if(this.byte(1)==0x71 && ecu_rx_phy.byte(3)==0x02 && ecu_rx_phy.byte(4)==0x03 )//31 01 02 03返回值
{
boot_check=3;
}
else if(this.byte(1)==0xC5 && this.byte(2)==0x02) //8502返回值
{
boot_check=4;
}
else if(this.byte(1)==0x68 && this.byte(2)==0x03) //28 03 返回值
{
boot_check=6;
}
else if(this.byte(1)==0x50 && this.byte(2)==0x02) //10 02 返回值
{
boot_check=7;
}
else if(this.byte(1)==0x67 && this.byte(2)==0x09) // 27 09 返回值
{
make_key[0]=this.byte(3);make_key[1]=this.byte(4);make_key[2]=this.byte(5);make_key[3]=this.byte(6);//获取2709的返回值,生成密钥
SecurityAccess_make_key_And_sendkey();
}
else if(this.byte(1)==0x67 && this.byte(2)==0x0A) //27 0A 返回值
{
boot_check=14;
}
else if(this.byte(1)==0x71 && this.byte(2)==0x01 && this.byte(3)==0xFF && this.byte(4)==0x00) //3101FF00返回值
{
boot_check=8;
}
else if(this.byte(1)==0x74 ) //34返回值
{
boot_check=9;file_send_max = this.byte(3)*0x100 + this.byte(4);//单次连续帧允许发送的byte位数
}
else if(this.byte(1)==0x76 && end_flag==1) //36 返回值 传输完成 标志位置1
{
end_flag=0;boot_check=10;
file_send_step=0x01;
file_line=0;
}
else if(this.byte(1)==0x76 && end_flag!=1) //36 返回值 继续传输 标志位 置零
{
setTimer(countinue_36,3);
}
else if(this.byte(1)==0x77) //37 返回值 退出传输
{
Suppress_negative_response=0; //已修改,此处可省略,原来有否定响应,懒得删
if(choice==0) //如果有多个文件,再次进行传输
{
choice++;
boot_check=8;
}
else
{
boot_check=15;
}
}
else if(this.byte(1)==0x71 && this.byte(2)==0x01 && this.byte(3)==0x02 && this.byte(4)==0x02) //31 01 02 02返回值
{
boot_check=11;
}
else if(this.byte(0)==0x30 && this.byte(1)==0x00) //流控帧
{
switch(step_control)
{
case 8:Get_Address_and_byteNum_continue();break; //将为传输完成的数据进行传输,下同
case 9:TransferData_36_continue();break;
case 14:eraseMemory_3101FF00_continue();break;
case 15:CheckProgrammingIntegrity_31010202_continue();break;
}
}
else if(this.byte(1)==0x71 && ecu_rx_phy.byte(3)==0xFF && ecu_rx_phy.byte(4)==0x01 )//31 01 FF 01返回值
{
boot_check=12;
}
if(this.byte(1)==0x51) //11返回值
{
boot_check=13;
}
if(this.byte(1)==0x50 && DE_1001==1) //11 01 返回值,因为一开始进入默认会话,此处为了区分
{
putValueToControl("bootpanel","OutputTrace","刷写成功");
writeLineEx(mNewPage,1,"刷写成功");
DE_1001=0;
}
else if(this.byte(1)==0x7F) //否定响应
{
if( this.byte(3)== 0x78) //这个在连续帧的结尾出现,所以屏蔽掉
{
putValueToControl("bootpanel","OutputTrace","等待继续\r");
}
else
{
putValueToControl("bootpanel","OutputTrace","刷写失败\r");
}
}
}
}
//控制开启
on sysvar sysvar::env_set::startBtn
{
char file_path_app[100]; //判断文件是不是放入
char file_path_driver[100];
sysGetVariableString(sysvar::files_space::app_path,file_path_app, elCount(file_path_app)); //获取文件路径,将string类型转换为char类型
sysGetVariableString(sysvar::files_space::driverPath,file_path_driver, elCount(file_path_driver)); //获取文件路径
if(@sysvar::env_set::startBtn==1) //按钮按下
{
writeConfigure(mNewPage, 20, 1, "H:\\bootloader_log.txt"); //打印日志,因为时间间隔短,数据量大,面板无法全部保存,所以创建文件进行打印
writeLineEx(mNewPage,1,"开始测试"); //将所需内容打印到上面设定的文件里,下同,不在解释
if(file_path_app[1]!=':') //判断是否选定文件,可以自己定义
{
file_check_num=0; //作为判断标志,判断哪些文件已经选定并加载
putValueToControl("bootpanel","OutputTrace","请选择文件再开始\r"); //在面板输出设置内容,下同,不再解释
}
else if(file_path_driver[1]!=':')
{
choice=1; //刷写文件判断标志,有Driver先刷Driver
file_check_num=1;
}
else
{
choice=0;
file_check_num=2;
}
if(file_check_num!=0)
{
file_send_step=0x01; //重置发送文件,一共进行了几次数据,
boot_check=1; //刷写流程控制,具体控制流程见on timer boot_order
setTimer(boot_order,1000); //启动刷写流程控制
DeleteControlContent("bootpanel","OutputTrace"); //重置面板输出的信息
setTimer(TesterPresent_3E,1000); //3E 00 保持帧,因为和我的刷写文件冲突,我屏蔽掉了,可以on timer TesterPresent_3E启用
putValueToControl("bootpanel","OutputTrace","进入默认会话\r");
ecu_rx_phy.dlc=8;
ecu_rx_phy.byte(0)=0x02;
ecu_rx_phy.byte(1)=0x10;
ecu_rx_phy.byte(2)=0x01;
output(ecu_rx_phy); //将报文发送出去,进入10 01,下同,根据报文进行分析,不再解释
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r"); //将面板输出的信息进行换行,下同,不再解释
}
}
else
{
putValueToControl("bootpanel","OutputTrace","刷写停止\r");
}
}
void Init_ecu_rx_phy() //初始化ecu_rx_phy,防止出现问题,每次运用前将数据置零
{
int i=0;
boot_check=0;
for(i=0;i<8;i++)
{
ecu_rx_phy.byte(i)=0;
}
}
void Init_file_buffer() //初始化buffer,防止出现问题
{
int i;
for(i=0;i< elCount(buffer);i++ )
{
buffer[i]=0;
}
}
//将char类型转换为byte类型
byte Type_conversion_char_byte(char buffer_one)
{
byte buffer_return;
if(buffer_one<71 && buffer_one>64)
{
buffer_return=buffer_one-55;
}
else if(buffer_one<58 && buffer_one>47)
{
buffer_return=buffer_one-48;
}
else if(buffer_one=='S')//为S的时候返回FF,区分s3文件
{
buffer_return=0xFF;
}
return buffer_return;
}
//1003
void SwitchToExtendedSession_1003()
{
putValueToControl("bootpanel","OutputTrace","进入预编程阶段\r");
writeLineEx(mNewPage,1,"进入预编程阶段");
putValueToControl("bootpanel","OutputTrace","进入扩展会话\r");
writeLineEx(mNewPage,1,"进入扩展会话");
ecu_rx_phy.dlc=8;
Init_ecu_rx_phy();
ecu_rx_phy.byte(0)=0x02;
ecu_rx_phy.byte(1)=0x10;
ecu_rx_phy.byte(2)=0x03;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
//3101 0203
void RoutineControl_31010203()
{
putValueToControl("bootpanel","OutputTrace","检查预编程条件\r");
writeLineEx(mNewPage,1,"检查预编程条件");
ecu_rx_phy.dlc=8;
Init_ecu_rx_phy();
ecu_rx_phy.byte(0)=0x04;
ecu_rx_phy.byte(1)=0x31;
ecu_rx_phy.byte(2)=0x01;
ecu_rx_phy.byte(3)=0x02;
ecu_rx_phy.byte(4)=0x03;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
//85 02
void ControlDTCSetting_8502()
{
putValueToControl("bootpanel","OutputTrace","DTC关闭\r");
writeLineEx(mNewPage,1,"DTC关闭");
ecu_rx_phy.dlc=8;
Init_ecu_rx_phy();
ecu_rx_phy.byte(0)=0x02;
ecu_rx_phy.byte(1)=0x85;
ecu_rx_phy.byte(2)=0x02;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
//28
void CommunicationControl_2803()
{
putValueToControl("bootpanel","OutputTrace","通信控制,禁止收发非诊断报文\r");
writeLineEx(mNewPage,1,"通信控制");
ecu_rx_phy.dlc=8;
Init_ecu_rx_phy();
ecu_rx_phy.byte(0)=0x03;
ecu_rx_phy.byte(1)=0x28;
ecu_rx_phy.byte(2)=0x03;
ecu_rx_phy.byte(3)=0x03;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
//22 可选,本次刷写不需要,所以屏蔽掉了
void ReadDataByIdentifier_22()
{
}
//10 02
void ProgrammingSession_1002()
{
putValueToControl("bootpanel","OutputTrace","进入主编程阶段\r");
putValueToControl("bootpanel","OutputTrace","进入编程会话\r");
writeLineEx(mNewPage,1,"进入主编程阶段");
writeLineEx(mNewPage,1,"进入编程会话");
ecu_rx_phy.dlc=8;
Init_ecu_rx_phy();
ecu_rx_phy.byte(0)=0x02;
ecu_rx_phy.byte(1)=0x10;
ecu_rx_phy.byte(2)=0x02;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
//27 09 过安全访问
void SecurityAccess_27()
{
putValueToControl("bootpanel","OutputTrace","过安全访问\r");
writeLineEx(mNewPage,1,"过安全访问");
ecu_rx_phy.dlc=8;
Init_ecu_rx_phy();
ecu_rx_phy.byte(0)=0x02;
ecu_rx_phy.byte(1)=0x27;
ecu_rx_phy.byte(2)=0x09;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
//过安全访问 27 0A
void SecurityAccess_make_key_And_sendkey()
{
int64 seed=0x00; //密钥判定,我的要求是64位数据,所以重新定义
long seed_cheek=0x00;
long MASK=0x71004000;
byte sendkey_check[4];
int i;
seed=make_key[0]*16*16*16*16*16*16+make_key[1]*16*16*16*16+make_key[2]*16*16+make_key[3]; //将27 09 返回的4BYTE数据赋到seed
seed = seed <<1; //判定逻辑。因为安全问题,密钥判断方式已做处理,请按需要设定
send_key[0]=seed>>24; //将密钥输出到全局变量
send_key[1]=(seed>>16)%0x100;
send_key[2]=(seed>>8)%0x100;
send_key[3]=seed%0x100;
Init_ecu_rx_phy();//初始化ecu_rx_phy
ecu_rx_phy.byte(0)=0x06;
ecu_rx_phy.byte(1)=0x27;
ecu_rx_phy.byte(2)=0x0A;
ecu_rx_phy.byte(3)=send_key[0];
ecu_rx_phy.byte(4)=send_key[1];
ecu_rx_phy.byte(5)=send_key[2];
ecu_rx_phy.byte(6)=send_key[3];
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
//获取字节长度和起始地址 //34服务和31擦除内存用 如果不懂搜34服务和31服务擦除内存
void Get_Address_and_byteNum()
{
char file_path[100];//获取文件路径
long file_byte; //获取文件的数据流长度
file_byte=0; //可能不止一次调用此函数,重置数据
if(choice==0)
{
sysGetVariableString(sysvar::files_space::driverPath,file_path, elCount(file_path)); //获取文件路径
fileHandle = OpenFileRead(file_path,0); //获取文件句柄
}
else
{
sysGetVariableString(sysvar::files_space::app_path,file_path, elCount(file_path)); //获取文件路径
fileHandle = OpenFileRead(file_path,0); //获取文件句柄
}
//获取总字符数
while(1) //依次每行获取数据
{
Init_file_buffer(); //防止出现问题,每次调用前初始化数据
Filegetstringsz(buffer,elcount(buffer), fileHandle);//打开一行数据,每次调用,打开下一行
if(buffer[0]==0) //判断读取数据到了文档结尾,是的话终止,
{
break;
}
//此次设计只针对于S3文件使用,
if(Type_conversion_char_byte(buffer[0])==0xFF && Type_conversion_char_byte(buffer[1])==0x03) //读取数据长度,累加,做为总数据长度,0XFF是因为当第一位为S时,返回FF,判断是不是S3
{
file_byte=file_byte+Type_conversion_char_byte(buffer[2])*16+Type_conversion_char_byte(buffer[3])-5;//数据长度累加,因为S3类型原因,每次需要减去5位非数据流数据,详情请搜素S19文件格式
}
}
fileClose (fileHandle); //读取文件完成后将句柄关掉
file_byte_all[3]=file_byte%0x100; //将数据长度进行赋值给全局变量,为34服务用,下同
file_byte_all[2]=file_byte>>8%0x100;
file_byte_all[1]=file_byte>>16%0x100;
file_byte_all[0]=file_byte>>24;
//获取起始地址
if(choice==0)
{
sysGetVariableString(sysvar::files_space::driverPath,file_path, elCount(file_path)); //获取文件路径
fileHandle = OpenFileRead(file_path,0); //获取文件句柄
}
else
{
sysGetVariableString(sysvar::files_space::app_path,file_path, elCount(file_path)); //获取文件路径
fileHandle = OpenFileRead(file_path,0); //获取文件句柄
}
while(1)
{
Init_file_buffer();
Filegetstringsz(buffer,elcount(buffer), fileHandle);
if(buffer[0]==0)
{
break;
}
if(Type_conversion_char_byte(buffer[0])==0xFF && Type_conversion_char_byte(buffer[1])==0x03)
{
start_address[0]=Type_conversion_char_byte(buffer[4])*0x10+Type_conversion_char_byte(buffer[5]);//将第一行上S3获取到的地址位进行赋值到全局变量
start_address[1]=Type_conversion_char_byte(buffer[6])*0x10+Type_conversion_char_byte(buffer[7]);
start_address[2]=Type_conversion_char_byte(buffer[8])*0x10+Type_conversion_char_byte(buffer[9]);
start_address[3]=Type_conversion_char_byte(buffer[10])*0x10+Type_conversion_char_byte(buffer[11]);
break;
}
}
fileClose (fileHandle);
}
//擦除内存 31 01 FF 00
void eraseMemory_3101FF00()
{
step_control=14;
putValueToControl("bootpanel","OutputTrace","请求下载\r");
writeLineEx(mNewPage,1,"请求下载");
Init_ecu_rx_phy();
ecu_rx_phy.dlc=8;
ecu_rx_phy.byte(0)=0x10;
ecu_rx_phy.byte(1)=0x0D;
ecu_rx_phy.byte(2)=0x31;
ecu_rx_phy.byte(3)=0x01;
ecu_rx_phy.byte(4)=0xFF;
ecu_rx_phy.byte(5)=0x00;
ecu_rx_phy.byte(6)=0x44;
ecu_rx_phy.byte(7)=start_address[0];
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
//因为31 01 FF 00 长度大于8位,需要连续帧和流控帧控制,此为第二帧报文,具体请搜索连续帧定义
void eraseMemory_3101FF00_continue()
{
Init_ecu_rx_phy();
ecu_rx_phy.dlc=8;
ecu_rx_phy.byte(0)=0x21;
ecu_rx_phy.byte(1)=start_address[1];
ecu_rx_phy.byte(2)=start_address[2];
ecu_rx_phy.byte(3)=start_address[3];
ecu_rx_phy.byte(4)=file_byte_all[0];
ecu_rx_phy.byte(5)=file_byte_all[1];
ecu_rx_phy.byte(6)=file_byte_all[2];
ecu_rx_phy.byte(7)=file_byte_all[3];
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
//34 ,具体报文格式请搜索34服务
void RequestDownload_34()
{
step_control=8;
putValueToControl("bootpanel","OutputTrace","请求下载\r");
writeLineEx(mNewPage,1,"请求下载");
Init_ecu_rx_phy();
ecu_rx_phy.dlc=8;
ecu_rx_phy.byte(0)=0x10;
ecu_rx_phy.byte(1)=0x0B;
ecu_rx_phy.byte(2)=0x34;
ecu_rx_phy.byte(3)=0x00;
ecu_rx_phy.byte(4)=0x44;
ecu_rx_phy.byte(5)=start_address[0];
ecu_rx_phy.byte(6)=start_address[1];
ecu_rx_phy.byte(7)=start_address[2];
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
//同31 大于8,连续帧
void Get_Address_and_byteNum_continue()
{
Init_ecu_rx_phy();
ecu_rx_phy.byte(0)=0x21;
ecu_rx_phy.byte(1)=start_address[3];
ecu_rx_phy.byte(2)=file_byte_all[0];
ecu_rx_phy.byte(3)=file_byte_all[1];
ecu_rx_phy.byte(4)=file_byte_all[2];
ecu_rx_phy.byte(5)=file_byte_all[3];
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
step_control=0;
}
//读取文件数据位,并赋值 36服务用
void file_send_message()
{
int i=0;
char file_path[100];
int file_start_line; //因为文件太长,每次发送的数据量有限,所以用此变量让文件读取到指定行数
int file_line_byte_num=0;
file_start_line=file_line;//此次起始行数为上次读取的位置
//初始化file_send_all,防止出现问题,将之前记录的数据都清除
for(file_send_all_byte=0;file_send_all_byte< elCount(file_send_all);file_send_all_byte++)
{
file_send_all[file_send_all_byte]=0x00;
}
file_send_all_byte=0; //每次起始记录数据从byte0开始
if(choice==0) //读取文件标志,判断需要读取的文件
{
sysGetVariableString(sysvar::files_space::driverPath,file_path, elCount(file_path)); //获取文件路径
fileHandle = OpenFileRead(file_path,0); //获取文件句柄
}
else
{
sysGetVariableString(sysvar::files_space::app_path,file_path, elCount(file_path)); //获取文件路径
fileHandle = OpenFileRead(file_path,0); //获取文件句柄
}
//读取文件行到上次使用的位置
for(file_line=1;file_line<file_start_line;file_line++)
{
Filegetstringsz(buffer,elcount(buffer), fileHandle);
}
//将数据赋值到file_send_all数组
for(file_line;;file_line++)
{
Init_file_buffer();
Filegetstringsz(buffer,elcount(buffer), fileHandle);
if(file_line>=file_start_line) //判断现在的行数是不是不小于上次读取的行数,此个判断可省略,因为前面已经设定,因为懒,没删掉
{
if(Type_conversion_char_byte(buffer[0])==0xFF && Type_conversion_char_byte(buffer[1])==0x03)//判断是不是S3格式
{
file_line_byte_num=Type_conversion_char_byte(buffer[2])*16+Type_conversion_char_byte(buffer[3]);//算本行的数据量
}
else
{
file_line_byte_num=0;
}
}
//读取到空行 结束
if(buffer[0]==0)//读取到文件结尾,结束读取
{
end_flag=1; //结束标志,防止再次调用此函数
break;
}
//判断数据是不是超过设定的长度
if(file_send_all_byte+file_line_byte_num-5 > file_send_max)//判断要求发送的字节数是不是在行中,没到行结尾,因为我的文件都是到行尾,此步没用到,不确定有没有问题
{
file_send_all_byte_line=0; //本行发送的数据
for(i=12;file_send_max-2-file_send_all_byte>0;i=i+2) //判断数据是不是超过限定的数据-2,此2位用来发送连续帧
{
file_send_all[file_send_all_byte]=Type_conversion_char_byte(buffer[i])*16+Type_conversion_char_byte(buffer[i+1]);//赋给数组
file_send_all_byte++;
file_send_all_byte_line++;
}
break;
}
//将每一行数据输入到file_send_all字符数组里
if(file_line_byte_num != 0)//判断是不是S3格式
{
for(i=12+file_send_all_byte_line*2;i<12+(file_line_byte_num-5)*2;i=i+2)//从上次读取行的读取位开始往file_send_all数组写入数据
{
file_send_all[file_send_all_byte]=Type_conversion_char_byte(buffer[i])*16+Type_conversion_char_byte(buffer[i+1]);
file_send_all_byte++;
}
file_send_all_byte_line=0;
}
}
}
//36服务
void TransferData_36()
{
int i=0;
crc32(file_send_all,file_send_all_byte); //将获取到的所有数据进行CRC_32校验
step_control=9;//流程控制,方便进行下一次连续帧
putValueToControl("bootpanel","OutputTrace","数据传输\r");
writeLineEx(mNewPage,1,"数据传输");
Init_ecu_rx_phy();
ecu_rx_phy.dlc=8;
ecu_rx_phy.byte(0)=0x10+(file_send_all_byte>>8);
ecu_rx_phy.byte(1)=(file_send_all_byte%0x100)+0x02;
ecu_rx_phy.byte(2)=0x36;
ecu_rx_phy.byte(3)=file_send_step;
ecu_rx_phy.byte(4)=file_send_all[0];//将文件里的数据进行赋值
ecu_rx_phy.byte(5)=file_send_all[1];
ecu_rx_phy.byte(6)=file_send_all[2];
ecu_rx_phy.byte(7)=file_send_all[3];
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
file_send_step=file_send_step+0x01; //进行计数,为了进行连续帧的顺序
}
void TransferData_36_continue()
{
int i;
step_control=0;
mcg_byt0=0x20;//连续帧起始0x20-0x2f
msg_time_control=0;
setTimer(msg_time_1ms,1);//控制数据发送的时间,防止出现负载率过大
}
on timer msg_time_1ms //本来想设置1ms一条,但是感觉出问题,设置的15ms一条,名称就懒得改了
{
Init_ecu_rx_phy();
if(mcg_byt0>=0x2F)//连续帧0x30-0x2f循环
{
mcg_byt0=0X20;
}
else
{
mcg_byt0=mcg_byt0+0x01;
}
ecu_rx_phy.byte(0)=mcg_byt0;
ecu_rx_phy.byte(1)=file_send_all[4+msg_time_control*7];//数据赋值
ecu_rx_phy.byte(2)=file_send_all[5+msg_time_control*7];
ecu_rx_phy.byte(3)=file_send_all[6+msg_time_control*7];
ecu_rx_phy.byte(4)=file_send_all[7+msg_time_control*7];
ecu_rx_phy.byte(5)=file_send_all[8+msg_time_control*7];
ecu_rx_phy.byte(6)=file_send_all[9+msg_time_control*7];
ecu_rx_phy.byte(7)=file_send_all[10+msg_time_control*7];
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
if(10+msg_time_control*7>=file_send_all_byte)
{
//数据发送完成,停发报文,因为之前设置,懒得改,相当于for函数的break;
writeLineEx(mNewPage,1,"%d %d",10+msg_time_control*7,file_send_all_byte );
}
else
{
msg_time_control++;
setTimer(msg_time_1ms,15);
}
}
//37 服务
void RequestTransferExit_37()
{
putValueToControl("bootpanel","OutputTrace","请求退出传输\r");
writeLineEx(mNewPage,1,"请求退出传输");
Init_ecu_rx_phy();
ecu_rx_phy.dlc=8;
ecu_rx_phy.byte(0)=0x01;
ecu_rx_phy.byte(1)=0x37;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
//获取crc 在网上搜的,具体不太了解,感觉crc-32和其他的CRC-*不太相同,理解出来再说
dword crc32(byte data[], dword len)
{
dword i =0;
for (i = 0; i < len; i++)
{
crc = Crc32Table[(crc ^ data[i]) & 0xff] ^ (crc >> 8);
}
writeLineEx(mNewPage,1,"CRC %08x",crc);
return crc;
}
//31010202 + 4位crc校验码 ,此项目要求,可以自己改
void CheckProgrammingIntegrity_31010202()
{
step_control=15;
putValueToControl("bootpanel","OutputTrace","检查编程完整性\r");
writeLineEx(mNewPage,1,"检查编程完整性");
Init_ecu_rx_phy();
crc = crc ^ 0xFFFFFFFF;
writeLineEx(mNewPage,1,"CRC %08x",crc);
ecu_rx_phy.dlc=8;
ecu_rx_phy.byte(0)=0x10;
ecu_rx_phy.byte(1)=0x08;
ecu_rx_phy.byte(2)=0x31;
ecu_rx_phy.byte(3)=0x01;
ecu_rx_phy.byte(4)=0x02;
ecu_rx_phy.byte(5)=0x02;
ecu_rx_phy.byte(6)=crc>>24;
ecu_rx_phy.byte(7)=(crc>>16)%0x100;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
//连续帧
void CheckProgrammingIntegrity_31010202_continue()
{
Init_ecu_rx_phy();
ecu_rx_phy.dlc=8;
ecu_rx_phy.byte(0)=0x21;
ecu_rx_phy.byte(1)=(crc>>8)%0x100;
ecu_rx_phy.byte(2)=crc%0x100;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
// 31 01 FF 01
void voidRoutineControl_3101FF01()
{
putValueToControl("bootpanel","OutputTrace","检查编程依赖性\r");
writeLineEx(mNewPage,1,"检查编程依赖性");
ecu_rx_phy.dlc=8;
Init_ecu_rx_phy();
ecu_rx_phy.byte(0)=0x04;
ecu_rx_phy.byte(1)=0x31;
ecu_rx_phy.byte(2)=0x01;
ecu_rx_phy.byte(3)=0xFF;
ecu_rx_phy.byte(4)=0x01;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
//11 01
void EcuReset_1101()
{
putValueToControl("bootpanel","OutputTrace","电控单元复位\r");
writeLineEx(mNewPage,1,"电控单元复位");
ecu_rx_phy.dlc=8;
Init_ecu_rx_phy();
ecu_rx_phy.byte(0)=0x02;
ecu_rx_phy.byte(1)=0x11;
ecu_rx_phy.byte(2)=0x01;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}
//10 01
void DefaultSession_1001()
{
putValueToControl("bootpanel","OutputTrace","切换默认会话\r");
writeLineEx(mNewPage,1,"切换默认会话");
ecu_rx_phy.dlc=8;
DE_1001=1;
Init_ecu_rx_phy();
ecu_rx_phy.byte(0)=0x02;
ecu_rx_phy.byte(1)=0x10;
ecu_rx_phy.byte(2)=0x01;
output(ecu_rx_phy);
writeLineEx(mNewPage,1,"%x %02x %02x %2x %02x %02x %02x %02x %02x", ecu_rx_phy.id ,ecu_rx_phy.byte(0),ecu_rx_phy.byte(1),ecu_rx_phy.byte(2),ecu_rx_phy.byte(3),ecu_rx_phy.byte(4),ecu_rx_phy.byte(5),ecu_rx_phy.byte(6),ecu_rx_phy.byte(7));
putValueToControl("bootpanel","OutputTrace",ecu_rx_phy,0,1);
putValueToControl("bootpanel","OutputTrace","\r");
}