1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-11-30 08:24:11 +01:00

OP-21/Flight Bootloader - More functions added, file hash now automagicaly created and uploaded. Device discovery working. As with the bootloader embedded SW this is highly experimental.

git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@1509 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
zedamota 2010-09-02 22:46:06 +00:00 committed by zedamota
parent 749267aee5
commit f4464c01a7
3 changed files with 116 additions and 17 deletions

View File

@ -13,6 +13,7 @@ int main(int argc, char *argv[])
filename = QString(argv[1]);
OP_DFU dfu;
dfu.enterDFU(0);
// dfu.enterDFU(1);
// dfu.StartUpload(4,OP_DFU::Descript);
// QByteArray array;
@ -25,10 +26,15 @@ int main(int argc, char *argv[])
// array[6]=7;
// array[7]=8;
// dfu.UploadData(8,array);
//dfu.UploadDescription(1,"jose manuel");
//QString str=dfu.DownloadDescription(1,12);
// dfu.UploadDescription(1,"jose manuel");
// QString str=dfu.DownloadDescription(1,12);
// dfu.JumpToApp();
dfu.UploadFirmware(filename.toAscii());
//qDebug()<<"Description="<<str;
dfu.findDevices();
dfu.UploadFirmware(filename.toAscii());
dfu.UploadDescription("jose manuel");
// QString str=dfu.DownloadDescription(1,12);
// dfu.JumpToApp();
// qDebug()<<"Description="<<str;
return a.exec();
}

View File

@ -53,7 +53,7 @@ void OP_DFU::StartUpload(qint32 numberOfBytes, TransferTypes type)
buf[7] = lastPacketCount;//DFU Data1
buf[8] = 1;//DFU Data2
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";
@ -104,9 +104,8 @@ void OP_DFU::UploadData(qint32 numberOfBytes, QByteArray data)
// qDebug() << "UPLOAD:"<<"Data="<<(int)buf[6]<<(int)buf[7]<<(int)buf[8]<<(int)buf[9]<<";"<<result << " bytes sent";
}
}
void OP_DFU::UploadDescription(int devNumber, QString description)
void OP_DFU::UploadDescription(QString description)
{
enterDFU(devNumber);
if(description.length()%2!=0)
{
@ -117,17 +116,17 @@ void OP_DFU::UploadDescription(int devNumber, QString description)
padding.fill(' ',pad);
description.append(padding);
}
StartUpload(description.length()/4,OP_DFU::Descript);
StartUpload(description.length(),OP_DFU::Descript);
QByteArray array=description.toAscii();
UploadData(description.length()/4,array);
UploadData(description.length(),array);
EndOperation();
int ret=StatusRequest();
qDebug()<<"Status="<<ret;
qDebug()<<"Upload description Status="<<ret;
}
QString OP_DFU::DownloadDescription(int devNumber, int numberOfChars)
{
// enterDFU(devNumber);
QByteArray arr=StartDownload(numberOfChars/4,Descript);
QByteArray arr=StartDownload(numberOfChars,Descript);
QString str(arr);
return str;
@ -204,10 +203,9 @@ int OP_DFU::StatusRequest()
buf[8] = 0;
buf[9] = 0;
int result = hidHandle.send(0,buf, BUF_LEN, 500);
hidHandle.receive(0,buf,BUF_LEN,500);
int result = hidHandle.send(0,buf, BUF_LEN, 5000);
qDebug() << result << " bytes sent";
result = hidHandle.receive(0,buf,BUF_LEN,500);
result = hidHandle.receive(0,buf,BUF_LEN,5000);
qDebug() << result << " bytes received";
if(buf[1]==OP_DFU::Status_Rep)
{
@ -217,6 +215,72 @@ int OP_DFU::StatusRequest()
return -1;
}
void OP_DFU::findDevices()
{
devices.clear();
char buf[BUF_LEN];
buf[0] =0x02;//reportID
buf[1] = OP_DFU::Req_Capabilities;//DFU Command
buf[2] = 0;
buf[3] = 0;
buf[4] = 0;
buf[5] = 0;
buf[6] = 0;
buf[7] = 0;
buf[8] = 0;
buf[9] = 0;
int result = hidHandle.send(0,buf, BUF_LEN, 5000);
result = hidHandle.receive(0,buf,BUF_LEN,5000);
numberOfDevices=buf[7];
RWFlags=buf[8];
RWFlags=RWFlags<<8 | buf[9];
if(buf[1]==OP_DFU::Rep_Capabilities)
{
for(int x=0;x<numberOfDevices;++x)
{
device dev;
dev.Readable=(bool)(RWFlags>>(x*2) & 1);
dev.Writable=(bool)(RWFlags>>(x*2+1) & 1);
devices.append(dev);
buf[0] =0x02;//reportID
buf[1] = OP_DFU::Req_Capabilities;//DFU Command
buf[2] = 0;
buf[3] = 0;
buf[4] = 0;
buf[5] = 0;
buf[6] = x+1;
buf[7] = 0;
buf[8] = 0;
buf[9] = 0;
int result = hidHandle.send(0,buf, BUF_LEN, 5000);
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];
aux=aux<<8 |(quint8)buf[3];
aux=aux<<8 |(quint8)buf[4];
aux=aux<<8 |(quint8)buf[5];
devices[x].SizeOfCode=aux;
}
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;
}
}
}
void OP_DFU::EndOperation()
{
@ -233,12 +297,11 @@ void OP_DFU::EndOperation()
buf[9] = 0;
int result = hidHandle.send(0,buf, BUF_LEN, 5000);
hidHandle.receive(0,buf,BUF_LEN,5000);
// hidHandle.receive(0,buf,BUF_LEN,5000);
qDebug() << result << " bytes sent";
}
void OP_DFU::UploadFirmware(const QString &sfile)
{
enterDFU(1);
QFile file(sfile);
//QFile file("in.txt");
if (!file.open(QIODevice::ReadOnly))
@ -247,6 +310,8 @@ void OP_DFU::UploadFirmware(const QString &sfile)
return;
}
QByteArray arr=file.readAll();
QByteArray hash=QCryptographicHash::hash(arr,QCryptographicHash::Sha1);
qDebug()<<"hash size="<<hash.length()<<" -"<<hash;
qDebug()<<"Bytes Loaded="<<arr.length();
if(arr.length()%4!=0)
{
@ -256,10 +321,22 @@ void OP_DFU::UploadFirmware(const QString &sfile)
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();
ret=StatusRequest();
qDebug()<<"Status="<<ret;
}

View File

@ -5,6 +5,8 @@
#include <../../plugins/rawhid/pjrc_rawhid.h>
#include <QDebug>
#include <QFile>
#include <QCryptographicHash>
#include <QList>
#define BUF_LEN 64
class OP_DFU
{
@ -45,12 +47,22 @@ public:
Status_Rep,
};
struct device
{
int ID;
int SizeOfHash;
int SizeOfDesc;
quint32 SizeOfCode;
bool Readable;
bool Writable;
};
void JumpToApp();
void ResetDevice(void);
void enterDFU(int devNumber);
void StartUpload(qint32 numberOfBytes, TransferTypes type);
void UploadData(qint32 numberOfPackets,QByteArray data);
void UploadDescription(int devNumber, QString description);
void UploadDescription(QString description);
void UploadFirmware(const QString &sfile);
int StatusRequest();
void EndOperation();
@ -59,7 +71,11 @@ public:
void CopyWords(char * source, char* destination, int count);
// QByteArray DownloadData(int devNumber,int numberOfPackets);
OP_DFU();
void findDevices();
private:
int numberOfDevices;
int RWFlags;
QList<device> devices;
pjrc_rawhid hidHandle;
int setStartBit(int command){return command|0x20;}
};