mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2025-02-20 10:54:14 +01:00
OP-21/Flight Bootloader - Command line parameters added, still some missing.
git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@1514 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
parent
8233f87b0a
commit
72e64e1009
@ -2,39 +2,244 @@
|
||||
#include <QThread>
|
||||
#include <../../plugins/rawhid/pjrc_rawhid.h>
|
||||
#include "op_dfu.h"
|
||||
#include <QStringList>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QCoreApplication a(argc, argv);
|
||||
QString filename;
|
||||
if(argc < 2)
|
||||
filename = QString("C:/OpenPilot.bin");
|
||||
else
|
||||
filename = QString(argv[1]);
|
||||
if(argc>1)
|
||||
{
|
||||
bool debug=false;
|
||||
OP_DFU::Actions action;
|
||||
QString file;
|
||||
QString description;
|
||||
int device=-1;
|
||||
QStringList args;
|
||||
for(int i=0;i<argc;++i)
|
||||
{
|
||||
args<<argv[i];
|
||||
}
|
||||
|
||||
OP_DFU dfu;
|
||||
if(args.contains("-?"))
|
||||
{
|
||||
cout<<"| Commands\n";
|
||||
cout<<"| \n";
|
||||
cout<<"| -ls : lists available devices\n";
|
||||
cout<<"| -p <file> : program hw (requires:-d - optionals:-v,-w)\n";
|
||||
cout<<"| -dn <file> : download firmware to file (requires:-d)\n";
|
||||
cout<<"| -dd <file> : download discription (requires:-d)\n";
|
||||
cout<<"| -d <number Of Device : (requires: -p or -dn)\n";
|
||||
cout<<"| -w <description> : (requires: -p)\n";
|
||||
cout<<"| -ca <file> : compares byte by byte current firmware with file\n";
|
||||
cout<<"| -ch <file> : compares hash of current firmware with file\n";
|
||||
cout<<"| -s : requests status of device\n";
|
||||
return 0;
|
||||
|
||||
|
||||
}
|
||||
else if(args.contains("-dd"))
|
||||
{
|
||||
action=OP_DFU::downdesc;
|
||||
if(args.contains(DEVICE))
|
||||
{
|
||||
if(args.indexOf(DEVICE)+1<args.length())
|
||||
{
|
||||
device=(args[args.indexOf(DEVICE)+1]).toInt();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(args.contains("-p"))
|
||||
{
|
||||
if(args.contains("-w"))
|
||||
{
|
||||
if(args.indexOf("-w")+1<args.length())
|
||||
{
|
||||
description=(args[args.indexOf("-w")+1]);
|
||||
}
|
||||
|
||||
}
|
||||
action=OP_DFU::program;
|
||||
if(args.contains(DEVICE))
|
||||
{
|
||||
if(args.indexOf(DEVICE)+1<args.length())
|
||||
{
|
||||
device=(args[args.indexOf(DEVICE)+1]).toInt();
|
||||
}
|
||||
if(args.indexOf("-p")+1<args.length())
|
||||
{
|
||||
file=args[args.indexOf("-p")+1];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cout<<("Device not specified\n");
|
||||
return -1;
|
||||
}
|
||||
if(args.contains("-v"))
|
||||
action=OP_DFU::programandverify;
|
||||
}
|
||||
else if(args.contains("-ch") || args.contains("-ca"))
|
||||
{
|
||||
int index;
|
||||
if(args.contains("-ch"))
|
||||
{
|
||||
index=args.indexOf("-ch");
|
||||
action=OP_DFU::comparehash;
|
||||
}
|
||||
else
|
||||
{
|
||||
index=args.indexOf("-ca");
|
||||
action=OP_DFU::compareall;
|
||||
}
|
||||
if(args.contains(DEVICE))
|
||||
{
|
||||
if(args.indexOf(DEVICE)+1<args.length())
|
||||
{
|
||||
device=(args[args.indexOf(DEVICE)+1]).toInt();
|
||||
}
|
||||
if(index+1<args.length())
|
||||
{
|
||||
file=args[index+1];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cout<<"Device not specified";
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else if(args.contains(DOWNLOAD))
|
||||
{
|
||||
int index;
|
||||
index=args.indexOf(DOWNLOAD);
|
||||
action=OP_DFU::download;
|
||||
|
||||
if(args.contains(DEVICE))
|
||||
{
|
||||
if(args.indexOf(DEVICE)+1<args.length())
|
||||
{
|
||||
device=(args[args.indexOf(DEVICE)+1]).toInt();
|
||||
}
|
||||
if(index+1<args.length())
|
||||
{
|
||||
file=args[index+1];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cout<<"Device not specified";
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else if(args.contains("-s"))
|
||||
{
|
||||
action=OP_DFU::statusreq;
|
||||
}
|
||||
else if(args.contains("-ls"))
|
||||
{
|
||||
action=OP_DFU::listdevs;
|
||||
}
|
||||
if((file.isEmpty()|| device==-1) && action!=OP_DFU::downdesc && action!=OP_DFU::statusreq && action!=OP_DFU::listdevs)
|
||||
{
|
||||
cout<<"wrong parameters";
|
||||
return -1;
|
||||
}
|
||||
|
||||
// qDebug()<<"Action="<<(int)action;
|
||||
// qDebug()<<"File="<<file;
|
||||
// qDebug()<<"Device="<<device;
|
||||
// qDebug()<<"Action="<<action;
|
||||
OP_DFU dfu(debug);
|
||||
if(!dfu.enterDFU(0))
|
||||
{
|
||||
cout<<"Could not enter DFU mode\n";
|
||||
return -1;
|
||||
}
|
||||
if(action!=OP_DFU::statusreq)
|
||||
{
|
||||
dfu.findDevices();
|
||||
if(action==OP_DFU::listdevs)
|
||||
{
|
||||
cout<<"Found "<<dfu.numberOfDevices<<"\n";
|
||||
for(int x=0;x<dfu.numberOfDevices;++x)
|
||||
{
|
||||
cout<<"Device #"<<x+1<<"\n";
|
||||
cout<<"Device ID="<<dfu.devices[x].ID<<"\n";
|
||||
cout<<"Device Readable="<<dfu.devices[x].Readable<<"\n";
|
||||
cout<<"Device Writable="<<dfu.devices[x].Writable<<"\n";
|
||||
cout<<"Device SizeOfCode="<<dfu.devices[x].SizeOfCode<<"\n";
|
||||
cout<<"Device SizeOfHash="<<dfu.devices[x].SizeOfHash<<"\n";
|
||||
cout<<"Device SizeOfDesc="<<dfu.devices[x].SizeOfDesc<<"\n";
|
||||
cout<<"\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
else if (action==OP_DFU::program)
|
||||
{
|
||||
if(device>dfu.numberOfDevices)
|
||||
{
|
||||
cout<<"Error:Invalid Device";
|
||||
return -1;
|
||||
}
|
||||
if(!dfu.enterDFU(device))
|
||||
{
|
||||
cout<<"Error:Could not enter DFU mode\n";
|
||||
return -1;
|
||||
}
|
||||
OP_DFU::Status retstatus=dfu.UploadFirmware(file.toAscii());
|
||||
if(retstatus!=OP_DFU::Last_operation_Success)
|
||||
{
|
||||
cout<<"Upload failed with code:"<<dfu.StatusToString(retstatus).data();
|
||||
return -1;
|
||||
}
|
||||
if(!description.isEmpty())
|
||||
{
|
||||
retstatus=dfu.UploadDescription(description);
|
||||
if(retstatus!=OP_DFU::Last_operation_Success)
|
||||
{
|
||||
cout<<"Upload failed with code:"<<dfu.StatusToString(retstatus).toLatin1().data();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
cout<<"Uploading Succeded!\n";
|
||||
}
|
||||
else if (action==OP_DFU::download)
|
||||
{
|
||||
qint32 size=((OP_DFU::device)dfu.devices[device]).SizeOfCode;
|
||||
|
||||
}
|
||||
else if(action==OP_DFU::downdesc)
|
||||
{
|
||||
int size=((OP_DFU::device)dfu.devices[device]).SizeOfDesc;
|
||||
cout<<"Description:"<<dfu.DownloadDescription(size).toLatin1().data();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
OP_DFU dfu(true);
|
||||
dfu.enterDFU(0);
|
||||
// dfu.enterDFU(1);
|
||||
// dfu.StartUpload(4,OP_DFU::Descript);
|
||||
// QByteArray array;
|
||||
// array[0]=11;
|
||||
// array[1]=2;
|
||||
// array[2]=3;
|
||||
// array[3]=4;
|
||||
// array[4]=5;
|
||||
// array[5]=6;
|
||||
// array[6]=7;
|
||||
// array[7]=8;
|
||||
// dfu.UploadData(8,array);
|
||||
// dfu.UploadDescription(1,"jose manuel");
|
||||
// QString str=dfu.DownloadDescription(1,12);
|
||||
// dfu.JumpToApp();
|
||||
dfu.findDevices();
|
||||
|
||||
dfu.UploadFirmware(filename.toAscii());
|
||||
dfu.UploadDescription("jose manuel");
|
||||
// QString str=dfu.DownloadDescription(1,12);
|
||||
// dfu.enterDFU(1);
|
||||
// dfu.StartUpload(4,OP_DFU::Descript);
|
||||
// QByteArray array;
|
||||
// array[0]=11;
|
||||
// array[1]=2;
|
||||
// array[2]=3;
|
||||
// array[3]=4;
|
||||
// array[4]=5;
|
||||
// array[5]=6;
|
||||
// array[6]=7;
|
||||
// array[7]=8;
|
||||
// dfu.UploadData(8,array);
|
||||
// dfu.UploadDescription(1,"jose manuel");
|
||||
// QString str=dfu.DownloadDescription(1,12);
|
||||
// dfu.JumpToApp();
|
||||
// qDebug()<<"Description="<<str;
|
||||
// dfu.findDevices();
|
||||
|
||||
// dfu.UploadFirmware(filename.toAscii());
|
||||
// dfu.UploadDescription("josemanuel");
|
||||
QString str=dfu.DownloadDescription(12);
|
||||
// dfu.JumpToApp();
|
||||
qDebug()<<"Description="<<str;
|
||||
return a.exec();
|
||||
}
|
||||
|
@ -1,16 +1,34 @@
|
||||
#include "op_dfu.h"
|
||||
#include <cmath>
|
||||
OP_DFU::OP_DFU()
|
||||
#include <qwaitcondition.h>
|
||||
#include <QMutex>
|
||||
|
||||
OP_DFU::OP_DFU(bool _debug): debug(_debug)
|
||||
{
|
||||
qDebug() << "Hello";
|
||||
|
||||
int numDevices = hidHandle.open(1,0x20a0,0x4117,0,0); //0xff9c,0x0001);
|
||||
if( numDevices == 0 )
|
||||
numDevices = hidHandle.open(1,0x0483,0,0,0);
|
||||
|
||||
qDebug() << numDevices << " device(s) opened";
|
||||
QWaitCondition sleep;
|
||||
QMutex mutex;
|
||||
int numDevices=0;
|
||||
cout<<"Please connect device now\n";
|
||||
int count=0;
|
||||
while(numDevices==0)
|
||||
{
|
||||
cout<<".";
|
||||
mutex.lock();
|
||||
sleep.wait(&mutex,500);
|
||||
mutex.unlock();
|
||||
numDevices = hidHandle.open(1,0x20a0,0x4117,0,0); //0xff9c,0x0001);
|
||||
if(++count==10)
|
||||
{
|
||||
cout<<"\r";
|
||||
cout<<" ";
|
||||
cout<<"\r";
|
||||
count=0;
|
||||
}
|
||||
}
|
||||
if(debug)
|
||||
qDebug() << numDevices << " device(s) opened";
|
||||
}
|
||||
void OP_DFU::enterDFU(int devNumber)
|
||||
bool OP_DFU::enterDFU(int devNumber)
|
||||
{
|
||||
char buf[BUF_LEN];
|
||||
buf[0] =0x02;//reportID
|
||||
@ -25,10 +43,13 @@ void OP_DFU::enterDFU(int devNumber)
|
||||
buf[9] = 1;//DFU Data3
|
||||
|
||||
int result = hidHandle.send(0,buf, BUF_LEN, 500);
|
||||
|
||||
qDebug() << result << " bytes sent";
|
||||
if(result<1)
|
||||
return false;
|
||||
if(debug)
|
||||
qDebug() << result << " bytes sent";
|
||||
return true;
|
||||
}
|
||||
void OP_DFU::StartUpload(qint32 numberOfBytes, TransferTypes type)
|
||||
bool OP_DFU::StartUpload(qint32 numberOfBytes, TransferTypes type)
|
||||
{
|
||||
int lastPacketCount;
|
||||
qint32 numberOfPackets=numberOfBytes/4/14;
|
||||
@ -55,10 +76,15 @@ void OP_DFU::StartUpload(qint32 numberOfBytes, TransferTypes type)
|
||||
buf[9] = 1;//DFU Data3
|
||||
qDebug()<<"Number of packets:"<<numberOfPackets<<" Size of last packet:"<<lastPacketCount;
|
||||
int result = hidHandle.send(0,buf, BUF_LEN, 5000);
|
||||
|
||||
qDebug() << result << " bytes sent";
|
||||
if(debug)
|
||||
qDebug() << result << " bytes sent";
|
||||
if(result>0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
void OP_DFU::UploadData(qint32 numberOfBytes, QByteArray data)
|
||||
bool OP_DFU::UploadData(qint32 numberOfBytes, QByteArray data)
|
||||
{
|
||||
int lastPacketCount;
|
||||
qint32 numberOfPackets=numberOfBytes/4/14;
|
||||
@ -72,7 +98,8 @@ void OP_DFU::UploadData(qint32 numberOfBytes, QByteArray data)
|
||||
++numberOfPackets;
|
||||
lastPacketCount=pad;
|
||||
}
|
||||
qDebug()<<"Start Uploading:"<<numberOfPackets<<"4Bytes";
|
||||
if(debug)
|
||||
qDebug()<<"Start Uploading:"<<numberOfPackets<<"4Bytes";
|
||||
char buf[BUF_LEN];
|
||||
buf[0] =0x02;//reportID
|
||||
buf[1] = OP_DFU::Upload;//DFU Command
|
||||
@ -89,26 +116,30 @@ void OP_DFU::UploadData(qint32 numberOfBytes, QByteArray data)
|
||||
buf[5] = packetcount;//DFU Count
|
||||
char *pointer=data.data();
|
||||
pointer=pointer+4*14*packetcount;
|
||||
// qDebug()<<"Packet Number="<<packetcount<<"Data0="<<(int)data[0]<<" Data1="<<(int)data[1]<<" Data0="<<(int)data[2]<<" Data0="<<(int)data[3]<<" buf6="<<(int)buf[6]<<" buf7="<<(int)buf[7]<<" buf8="<<(int)buf[8]<<" buf9="<<(int)buf[9];
|
||||
// qDebug()<<"Packet Number="<<packetcount<<"Data0="<<(int)data[0]<<" Data1="<<(int)data[1]<<" Data0="<<(int)data[2]<<" Data0="<<(int)data[3]<<" buf6="<<(int)buf[6]<<" buf7="<<(int)buf[7]<<" buf8="<<(int)buf[8]<<" buf9="<<(int)buf[9];
|
||||
CopyWords(pointer,buf+6,packetsize*4);
|
||||
// for (int y=0;y<packetsize*4;++y)
|
||||
// {
|
||||
// for (int y=0;y<packetsize*4;++y)
|
||||
// {
|
||||
|
||||
// qDebug()<<y<<":"<<(int)data[packetcount*14*4+y]<<"---"<<(int)buf[6+y];
|
||||
// qDebug()<<y<<":"<<(int)data[packetcount*14*4+y]<<"---"<<(int)buf[6+y];
|
||||
|
||||
|
||||
// }
|
||||
// qDebug()<<" Data0="<<(int)data[0]<<" Data0="<<(int)data[1]<<" Data0="<<(int)data[2]<<" Data0="<<(int)data[3]<<" buf6="<<(int)buf[6]<<" buf7="<<(int)buf[7]<<" buf8="<<(int)buf[8]<<" buf9="<<(int)buf[9];
|
||||
// }
|
||||
// qDebug()<<" Data0="<<(int)data[0]<<" Data0="<<(int)data[1]<<" Data0="<<(int)data[2]<<" Data0="<<(int)data[3]<<" buf6="<<(int)buf[6]<<" buf7="<<(int)buf[7]<<" buf8="<<(int)buf[8]<<" buf9="<<(int)buf[9];
|
||||
int result = hidHandle.send(0,buf, BUF_LEN, 5000);
|
||||
if(result<1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// qDebug() << "UPLOAD:"<<"Data="<<(int)buf[6]<<(int)buf[7]<<(int)buf[8]<<(int)buf[9]<<";"<<result << " bytes sent";
|
||||
// qDebug() << "UPLOAD:"<<"Data="<<(int)buf[6]<<(int)buf[7]<<(int)buf[8]<<(int)buf[9]<<";"<<result << " bytes sent";
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void OP_DFU::UploadDescription(QString description)
|
||||
OP_DFU::Status OP_DFU::UploadDescription(QString description)
|
||||
{
|
||||
if(description.length()%2!=0)
|
||||
{
|
||||
|
||||
if(description.length()%4!=0)
|
||||
{
|
||||
int pad=description.length()/4;
|
||||
pad=(pad+1)*4;
|
||||
pad=pad-description.length();
|
||||
@ -116,23 +147,44 @@ void OP_DFU::UploadDescription(QString description)
|
||||
padding.fill(' ',pad);
|
||||
description.append(padding);
|
||||
}
|
||||
StartUpload(description.length(),OP_DFU::Descript);
|
||||
if(!StartUpload(description.length(),OP_DFU::Descript))
|
||||
return OP_DFU::abort;
|
||||
QByteArray array=description.toAscii();
|
||||
UploadData(description.length(),array);
|
||||
EndOperation();
|
||||
int ret=StatusRequest();
|
||||
qDebug()<<"Upload description Status="<<ret;
|
||||
if(!UploadData(description.length(),array))
|
||||
{
|
||||
return OP_DFU::abort;
|
||||
}
|
||||
if(!EndOperation())
|
||||
{
|
||||
return OP_DFU::abort;
|
||||
}
|
||||
OP_DFU::Status ret=StatusRequest();
|
||||
if(debug)
|
||||
qDebug()<<"Upload description Status="<<ret;
|
||||
return ret;
|
||||
}
|
||||
QString OP_DFU::DownloadDescription(int devNumber, int numberOfChars)
|
||||
QString OP_DFU::DownloadDescription(int numberOfChars)
|
||||
{
|
||||
// enterDFU(devNumber);
|
||||
// enterDFU(devNumber);
|
||||
QByteArray arr=StartDownload(numberOfChars,Descript);
|
||||
QString str(arr);
|
||||
return str;
|
||||
|
||||
}
|
||||
QByteArray OP_DFU::StartDownload(qint32 numberOfPackets, TransferTypes type)
|
||||
QByteArray OP_DFU::StartDownload(qint32 numberOfBytes, TransferTypes type)
|
||||
{
|
||||
int lastPacketCount;
|
||||
qint32 numberOfPackets=numberOfBytes/4/14;
|
||||
int pad=(numberOfBytes-numberOfPackets*4*14)/4;
|
||||
if(pad==0)
|
||||
{
|
||||
lastPacketCount=14;
|
||||
}
|
||||
else
|
||||
{
|
||||
++numberOfPackets;
|
||||
lastPacketCount=pad;
|
||||
}
|
||||
QByteArray ret;
|
||||
char buf[BUF_LEN];
|
||||
buf[0] =0x02;//reportID
|
||||
@ -142,19 +194,27 @@ QByteArray OP_DFU::StartDownload(qint32 numberOfPackets, TransferTypes type)
|
||||
buf[4] = (char)numberOfPackets>>8;//DFU Count
|
||||
buf[5] = (char)numberOfPackets;//DFU Count
|
||||
buf[6] = (int)type;//DFU Data0
|
||||
buf[7] = 1;//DFU Data1
|
||||
buf[7] = lastPacketCount;//DFU Data1
|
||||
buf[8] = 1;//DFU Data2
|
||||
buf[9] = 1;//DFU Data3
|
||||
|
||||
int result = hidHandle.send(0,buf, BUF_LEN, 500);
|
||||
|
||||
qDebug() << "StartDownload:"<<result << " bytes sent";
|
||||
if(debug)
|
||||
qDebug() << "StartDownload:"<<result << " bytes sent";
|
||||
for(qint32 x=0;x<numberOfPackets;++x)
|
||||
{
|
||||
// qDebug()<<"Status="<<StatusToString(StatusRequest());
|
||||
result = hidHandle.receive(0,buf,BUF_LEN,5000);
|
||||
qDebug() << result << " bytes received"<<" Count="<<(int)buf[2]<<";"<<(int)buf[3]<<";"<<(int)buf[4]<<";"<<(int)buf[5]<<" Data="<<(int)buf[6]<<";"<<(int)buf[7]<<";"<<(int)buf[8]<<";"<<(int)buf[9];
|
||||
ret.append(buf+6,4);
|
||||
if(debug)
|
||||
qDebug() << result << " bytes received"<<" Count="<<(int)buf[2]<<";"<<(int)buf[3]<<";"<<(int)buf[4]<<";"<<(int)buf[5]<<" Data="<<(int)buf[6]<<";"<<(int)buf[7]<<";"<<(int)buf[8]<<";"<<(int)buf[9];
|
||||
int size;
|
||||
if(x==numberOfPackets-1)
|
||||
size=lastPacketCount*4;
|
||||
else
|
||||
size=14*4;
|
||||
ret.append(buf+6,size);
|
||||
}
|
||||
StatusRequest();
|
||||
return ret;
|
||||
}
|
||||
void OP_DFU::ResetDevice(void)
|
||||
@ -170,7 +230,6 @@ void OP_DFU::ResetDevice(void)
|
||||
buf[7] = 0;
|
||||
buf[8] = 0;
|
||||
buf[9] = 0;
|
||||
|
||||
int result = hidHandle.send(0,buf, BUF_LEN, 500);
|
||||
}
|
||||
void OP_DFU::JumpToApp()
|
||||
@ -189,7 +248,7 @@ void OP_DFU::JumpToApp()
|
||||
|
||||
int result = hidHandle.send(0,buf, BUF_LEN, 500);
|
||||
}
|
||||
int OP_DFU::StatusRequest()
|
||||
OP_DFU::Status OP_DFU::StatusRequest()
|
||||
{
|
||||
char buf[BUF_LEN];
|
||||
buf[0] =0x02;//reportID
|
||||
@ -204,19 +263,21 @@ int OP_DFU::StatusRequest()
|
||||
buf[9] = 0;
|
||||
|
||||
int result = hidHandle.send(0,buf, BUF_LEN, 5000);
|
||||
qDebug() << result << " bytes sent";
|
||||
if(debug)
|
||||
qDebug() << result << " bytes sent";
|
||||
result = hidHandle.receive(0,buf,BUF_LEN,5000);
|
||||
qDebug() << result << " bytes received";
|
||||
if(debug)
|
||||
qDebug() << result << " bytes received";
|
||||
if(buf[1]==OP_DFU::Status_Rep)
|
||||
{
|
||||
return buf[6];
|
||||
return (OP_DFU::Status)buf[6];
|
||||
}
|
||||
else
|
||||
return -1;
|
||||
return OP_DFU::abort;
|
||||
|
||||
|
||||
}
|
||||
void OP_DFU::findDevices()
|
||||
bool OP_DFU::findDevices()
|
||||
{
|
||||
devices.clear();
|
||||
char buf[BUF_LEN];
|
||||
@ -231,7 +292,15 @@ void OP_DFU::findDevices()
|
||||
buf[8] = 0;
|
||||
buf[9] = 0;
|
||||
int result = hidHandle.send(0,buf, BUF_LEN, 5000);
|
||||
if(result<1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
result = hidHandle.receive(0,buf,BUF_LEN,5000);
|
||||
if(result<1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
numberOfDevices=buf[7];
|
||||
RWFlags=buf[8];
|
||||
RWFlags=RWFlags<<8 | buf[9];
|
||||
@ -259,7 +328,6 @@ void OP_DFU::findDevices()
|
||||
result = hidHandle.receive(0,buf,BUF_LEN,5000);
|
||||
devices[x].ID=buf[9];
|
||||
devices[x].SizeOfHash=buf[7];
|
||||
qDebug()<<"---------------"<<(int)buf[8];
|
||||
devices[x].SizeOfDesc=buf[8];
|
||||
quint32 aux;
|
||||
aux=(quint8)buf[2];
|
||||
@ -268,21 +336,24 @@ void OP_DFU::findDevices()
|
||||
aux=aux<<8 |(quint8)buf[5];
|
||||
devices[x].SizeOfCode=aux;
|
||||
}
|
||||
qDebug()<<"Found "<<numberOfDevices;
|
||||
for(int x=0;x<numberOfDevices;++x)
|
||||
if(debug)
|
||||
{
|
||||
qDebug()<<"Device #"<<x+1;
|
||||
qDebug()<<"Device ID="<<devices[x].ID;
|
||||
qDebug()<<"Device Readable="<<devices[x].Readable;
|
||||
qDebug()<<"Device Writable="<<devices[x].Writable;
|
||||
qDebug()<<"Device SizeOfCode="<<devices[x].SizeOfCode;
|
||||
qDebug()<<"Device SizeOfHash="<<devices[x].SizeOfHash;
|
||||
qDebug()<<"Device SizeOfDesc="<<devices[x].SizeOfDesc;
|
||||
}
|
||||
qDebug()<<"Found "<<numberOfDevices;
|
||||
for(int x=0;x<numberOfDevices;++x)
|
||||
{
|
||||
qDebug()<<"Device #"<<x+1;
|
||||
qDebug()<<"Device ID="<<devices[x].ID;
|
||||
qDebug()<<"Device Readable="<<devices[x].Readable;
|
||||
qDebug()<<"Device Writable="<<devices[x].Writable;
|
||||
qDebug()<<"Device SizeOfCode="<<devices[x].SizeOfCode;
|
||||
qDebug()<<"Device SizeOfHash="<<devices[x].SizeOfHash;
|
||||
qDebug()<<"Device SizeOfDesc="<<devices[x].SizeOfDesc;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
void OP_DFU::EndOperation()
|
||||
bool OP_DFU::EndOperation()
|
||||
{
|
||||
char buf[BUF_LEN];
|
||||
buf[0] =0x02;//reportID
|
||||
@ -297,57 +368,132 @@ void OP_DFU::EndOperation()
|
||||
buf[9] = 0;
|
||||
|
||||
int result = hidHandle.send(0,buf, BUF_LEN, 5000);
|
||||
// hidHandle.receive(0,buf,BUF_LEN,5000);
|
||||
qDebug() << result << " bytes sent";
|
||||
// hidHandle.receive(0,buf,BUF_LEN,5000);
|
||||
if(debug)
|
||||
qDebug() << result << " bytes sent";
|
||||
if(result>0)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
void OP_DFU::UploadFirmware(const QString &sfile)
|
||||
OP_DFU::Status OP_DFU::UploadFirmware(const QString &sfile)
|
||||
{
|
||||
cout<<"Starting Firmware Uploading...\n";
|
||||
QFile file(sfile);
|
||||
//QFile file("in.txt");
|
||||
if (!file.open(QIODevice::ReadOnly))
|
||||
{
|
||||
qDebug()<<"Cant open file";
|
||||
return;
|
||||
}
|
||||
if(debug)
|
||||
qDebug()<<"Cant open file";
|
||||
return OP_DFU::abort;
|
||||
}
|
||||
QByteArray arr=file.readAll();
|
||||
QByteArray hash=QCryptographicHash::hash(arr,QCryptographicHash::Sha1);
|
||||
qDebug()<<"hash size="<<hash.length()<<" -"<<hash;
|
||||
qDebug()<<"Bytes Loaded="<<arr.length();
|
||||
if(debug)
|
||||
qDebug()<<"hash size="<<hash.length()<<" -"<<hash;
|
||||
if(debug)
|
||||
qDebug()<<"Bytes Loaded="<<arr.length();
|
||||
if(arr.length()%4!=0)
|
||||
{
|
||||
int pad=arr.length()/4;
|
||||
++pad;
|
||||
pad=pad*4;
|
||||
pad=pad-arr.length();
|
||||
arr.append(QByteArray(pad,255));
|
||||
}
|
||||
qDebug()<<"1";
|
||||
StartUpload(arr.length(),FW);
|
||||
qDebug()<<"2";
|
||||
UploadData(arr.length(),arr);
|
||||
qDebug()<<"3";
|
||||
EndOperation();
|
||||
qDebug()<<"4";
|
||||
int ret=StatusRequest();
|
||||
qDebug()<<"5";
|
||||
qDebug()<<"Status="<<ret;
|
||||
StartUpload(hash.length(),Hash);
|
||||
qDebug()<<"6";
|
||||
UploadData(hash.length(),hash);
|
||||
qDebug()<<"7";
|
||||
EndOperation();
|
||||
int pad=arr.length()/4;
|
||||
++pad;
|
||||
pad=pad*4;
|
||||
pad=pad-arr.length();
|
||||
arr.append(QByteArray(pad,255));
|
||||
}
|
||||
if(!StartUpload(arr.length(),FW))
|
||||
{
|
||||
if(debug)
|
||||
qDebug()<<"StartUpload failed";
|
||||
return OP_DFU::abort;
|
||||
}
|
||||
if(!UploadData(arr.length(),arr))
|
||||
{
|
||||
if(debug)
|
||||
qDebug()<<"Upload failed";
|
||||
return OP_DFU::abort;
|
||||
}
|
||||
if(!EndOperation())
|
||||
{
|
||||
if(debug)
|
||||
qDebug()<<"Upload failed";
|
||||
return OP_DFU::abort;
|
||||
}
|
||||
OP_DFU::Status ret=StatusRequest();
|
||||
if(ret==OP_DFU::Last_operation_Success)
|
||||
{
|
||||
cout<<"Firmware Uploading succeeded...going to upload hash\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
if(debug)
|
||||
qDebug()<<"Status="<<ret;
|
||||
if(!StartUpload(hash.length(),Hash))
|
||||
{
|
||||
if(debug)
|
||||
qDebug()<<"StartUpload failed";
|
||||
return OP_DFU::abort;
|
||||
}
|
||||
if(!UploadData(hash.length(),hash))
|
||||
{
|
||||
if(debug)
|
||||
qDebug()<<"Upload failed";
|
||||
return OP_DFU::abort;
|
||||
}
|
||||
if(!EndOperation())
|
||||
{
|
||||
{
|
||||
if(debug)
|
||||
qDebug()<<"Upload failed";
|
||||
return OP_DFU::abort;
|
||||
}
|
||||
}
|
||||
ret=StatusRequest();
|
||||
qDebug()<<"Status="<<ret;
|
||||
|
||||
if(debug)
|
||||
qDebug()<<"Status="<<ret;
|
||||
if(ret==OP_DFU::Last_operation_Success)
|
||||
{
|
||||
cout<<"Hash Uploading succeeded...\n";
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
void OP_DFU::CopyWords(char *source, char *destination, int count)
|
||||
{
|
||||
for (int x=0;x<count;x=x+4)
|
||||
{
|
||||
//qDebug()<<(int)source[x*4+3]<<"xxx="<<4*x;
|
||||
*(destination+x)=source[x+3];
|
||||
*(destination+x+1)=source[x+2];
|
||||
*(destination+x+2)=source[x+1];
|
||||
*(destination+x+3)=source[x+0];
|
||||
}
|
||||
}
|
||||
QString OP_DFU::StatusToString(OP_DFU::Status status)
|
||||
{
|
||||
switch(status)
|
||||
{
|
||||
case DFUidle:
|
||||
return "DFUidle";
|
||||
case uploading:
|
||||
return "";
|
||||
case wrong_packet_received:
|
||||
return "wrong_packet_received";
|
||||
case too_many_packets:
|
||||
return "too_many_packets";
|
||||
case too_few_packets:
|
||||
return "too_few_packets";
|
||||
case Last_operation_Success:
|
||||
return "Last_operation_Success";
|
||||
case downloading:
|
||||
return "downloading";
|
||||
case idle:
|
||||
return "idle";
|
||||
case Last_operation_failed:
|
||||
return "Last_operation_failed";
|
||||
case outsideDevCapabilities:
|
||||
return "outsideDevCapabilities";
|
||||
case abort:
|
||||
return "abort";
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,11 @@
|
||||
#include <QFile>
|
||||
#include <QCryptographicHash>
|
||||
#include <QList>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
#define BUF_LEN 64
|
||||
#define DOWNLOAD "-dn"
|
||||
#define DEVICE "-d"
|
||||
class OP_DFU
|
||||
{
|
||||
public:
|
||||
@ -27,9 +31,23 @@ public:
|
||||
Last_operation_Success,
|
||||
downloading,
|
||||
idle,
|
||||
Last_operation_failed
|
||||
Last_operation_failed,
|
||||
outsideDevCapabilities,
|
||||
abort
|
||||
|
||||
};
|
||||
enum Actions
|
||||
{
|
||||
program,
|
||||
programandverify,
|
||||
download,
|
||||
compareall,
|
||||
comparehash,
|
||||
listdevs,
|
||||
statusreq,
|
||||
downdesc
|
||||
};
|
||||
|
||||
enum Commands
|
||||
{
|
||||
Reserved,
|
||||
@ -59,23 +77,26 @@ public:
|
||||
|
||||
void JumpToApp();
|
||||
void ResetDevice(void);
|
||||
void enterDFU(int devNumber);
|
||||
void StartUpload(qint32 numberOfBytes, TransferTypes type);
|
||||
void UploadData(qint32 numberOfPackets,QByteArray data);
|
||||
void UploadDescription(QString description);
|
||||
void UploadFirmware(const QString &sfile);
|
||||
int StatusRequest();
|
||||
void EndOperation();
|
||||
QString DownloadDescription(int devNumber,int numberOfChars);
|
||||
QByteArray StartDownload(qint32 numberOfPackets, TransferTypes type);
|
||||
bool enterDFU(int devNumber);
|
||||
bool StartUpload(qint32 numberOfBytes, TransferTypes type);
|
||||
bool UploadData(qint32 numberOfPackets,QByteArray data);
|
||||
Status UploadDescription(QString description);
|
||||
Status UploadFirmware(const QString &sfile);
|
||||
Status StatusRequest();
|
||||
bool EndOperation();
|
||||
QString DownloadDescription(int numberOfChars);
|
||||
QByteArray StartDownload(qint32 numberOfBytes, TransferTypes type);
|
||||
void CopyWords(char * source, char* destination, int count);
|
||||
// QByteArray DownloadData(int devNumber,int numberOfPackets);
|
||||
OP_DFU();
|
||||
void findDevices();
|
||||
private:
|
||||
int numberOfDevices;
|
||||
int RWFlags;
|
||||
OP_DFU(bool debug);
|
||||
bool findDevices();
|
||||
QList<device> devices;
|
||||
int numberOfDevices;
|
||||
QString StatusToString(OP_DFU::Status);
|
||||
private:
|
||||
bool debug;
|
||||
int RWFlags;
|
||||
|
||||
pjrc_rawhid hidHandle;
|
||||
int setStartBit(int command){return command|0x20;}
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user