// 讀取芯片內(nèi)部flash數(shù)據(jù)
// 地址范圍:0x0800 0000 ~ 0x0801 ffff 128K Byte
// 循環(huán)讀取,每次讀取16個(gè)字節(jié),讀取128K Byte /16Byte = 8000 次
int base = 0x08000000;
for(int i =0;i<8000;i++)
{
//1.發(fā)送讀內(nèi)存命令
UartSendHex('11ee');
Sleep(5);
//2.發(fā)送地址和校驗(yàn)和
int offer = base + i*16;
QString offer_str = QString::number(offer,16);
if(offer_str.size()<8)
offer_str.insert(0,'0');
QString check_str;
check_str = getXORresult(offer_str.mid(0,2), offer_str.mid(2,2));
check_str = getXORresult(check_str, offer_str.mid(4,2));
check_str = getXORresult(check_str, offer_str.mid(6,2));
UartSendHex(offer_str+check_str);
Sleep(5);
//3.發(fā)送要讀取的字節(jié)數(shù)和效驗(yàn)和
QByteArray ByteNum = '0ff0';
UartSendHex(ByteNum);
Sleep(5);
ui->progressBar->setFormat(QString::fromLocal8Bit('%1%').arg(QString::number((i+1)/10, 'f', 1)));
ui->progressBar->setValue((i+1)/10);
}
// Qt flash數(shù)據(jù) 轉(zhuǎn) hex文件算法
int t = 0,g = 0;
QString text = ui->textEdit_Recv->toPlainText();
QStringList number_list = text.split('\n');
QStringList result_list;
// 遍歷字符串列表,濾除全ffff.........結(jié)尾,記錄行數(shù)
for(int i = 0; i < number_list.size(); ++i)
{
if(number_list.at(i) == 'ffffffffffffffffffffffffffffffff')
{
t = i;
break;
}
}
// 遍歷字符串列表斬?cái)嘈校瑸V除改行的 ffff.........
text.clear();
text = number_list.at(t-1);
for(int i =0;i<text.size();i++)
{
if((text.at(i) == 'f')&&(text.at(i+1) == 'f')&&(i%2 == 0))
{
g = i;
break;
}
}
// 拼接新的全數(shù)據(jù)的字符串列表
for(int i =0;i<t-1;i++)
{
result_list<<number_list.at(i);
}
if(g!=0)
{
text = text.remove(g,32-g);
result_list<<text;
}
//生成HEX文件
//1.添加 10 字節(jié)數(shù)
text.clear();
QStringList result_list1;
for(int i = 0; i < t; ++i)
{
text = result_list.at(i);
if(i!=t-1)
text = text.insert(0,'10');
else
{
QString str = QString::number(text.size()/2,16);
if(str.size()<2)
str.insert(0,'0');
text = text.insert(0,str);
}
result_list1 << text;
}
//2.添加地址偏移
QStringList result_list2;
int offer = 0x0000;
QString offer_str;
text.clear();
for(int i = 0; i < t; ++i)
{
offer = i*16;
offer_str = QString::number(offer,16);
if(offer_str.size()<4)
{
if(offer_str.size() == 1)
offer_str.insert(0,'000');
if(offer_str.size() == 2)
offer_str.insert(0,'00');
if(offer_str.size() == 3)
offer_str.insert(0,'0');
}
text = result_list1.at(i);
text = text.insert(2,offer_str);
result_list2 << text;
}
//3.添加效驗(yàn)和
// 以020000040003F7為例子
//具體算法為 0x100-((0x02+0x00+0x00+0x04+0x00+0x03)%256)= 0xF7
text.clear();
bool ok;
int check;
QString check_str;
QStringList result_list3;
for(int i = 0; i < t; ++i)
{
text = result_list2.at(i);
check = 0;
for(int j=0;j<text.size();j++)
{
check += text.mid(j*2,2).toInt(&ok,16);
}
check = (0x100 - check%256);
check_str = QString::number(check,16);
if(check_str.size()<2)
check_str = check_str.insert(0,'0');
text = text.insert(text.size(),check_str);
result_list3<<text;
}
//4.添加冒號 :
QStringList result_list4;
text.clear();
for(int i = 0; i < t; ++i)
{
text = result_list3.at(i);
text = text.insert(0,':');
result_list4<<text;
}
//5.添加HEX頭部
QString hear=':020000040800f2';
//QStringList result_list5;
result_list4.insert(0,hear);
//6.添加HEX尾部
QString tail1=':04000005080001cd21';
QString tail2=':00000001ff';
result_list4.insert(t+2,tail1);
result_list4.insert(t+3,tail2);
//7.保存數(shù)據(jù)為.hex文件
...............