1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-18 03:52:11 +01:00

OP-21/Flight QT test app for the USB HID bootloader

git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@1377 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
zedamota 2010-08-22 19:13:18 +00:00 committed by zedamota
parent 2e705e6836
commit d899bc3c81
4 changed files with 349 additions and 0 deletions

View File

@ -0,0 +1,28 @@
#include <QtCore/QCoreApplication>
#include <QThread>
#include <../../plugins/rawhid/pjrc_rawhid.h>
#include "op_dfu.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
OP_DFU dfu;
// 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.UploadFirmware("C:/OpenPilot.bin");
//qDebug()<<"Description="<<str;
return a.exec();
}

View File

@ -0,0 +1,229 @@
#include "op_dfu.h"
#include <cmath>
OP_DFU::OP_DFU()
{
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";
}
void OP_DFU::enterDFU(int devNumber)
{
char buf[BUF_LEN];
buf[0] =0x02;//reportID
buf[1] = OP_DFU::EnterDFU;//DFU Command
buf[2] = 0;//DFU Count
buf[3] = 0;//DFU Count
buf[4] = 0;//DFU Count
buf[5] = 0;//DFU Count
buf[6] = devNumber;//DFU Data0
buf[7] = 1;//DFU Data1
buf[8] = 1;//DFU Data2
buf[9] = 1;//DFU Data3
int result = hidHandle.send(0,buf, BUF_LEN, 500);
qDebug() << result << " bytes sent";
}
void OP_DFU::StartUpload(qint32 numberOfPackets, TransferTypes type)
{
char buf[BUF_LEN];
buf[0] =0x02;//reportID
buf[1] = setStartBit(OP_DFU::Upload);//DFU Command
buf[2] = numberOfPackets>>24;//DFU Count
buf[3] = numberOfPackets>>16;//DFU Count
buf[4] = numberOfPackets>>8;//DFU Count
buf[5] = numberOfPackets;//DFU Count
buf[6] = (int)type;//DFU Data0
buf[7] = 1;//DFU Data1
buf[8] = 1;//DFU Data2
buf[9] = 1;//DFU Data3
int result = hidHandle.send(0,buf, BUF_LEN, 5000);
qDebug() << result << " bytes sent";
}
void OP_DFU::UploadData(qint32 numberOfPackets, QByteArray data)
{
qDebug()<<"Start Uploading:"<<numberOfPackets<<"4Bytes";
char buf[BUF_LEN];
buf[0] =0x02;//reportID
buf[1] = OP_DFU::Upload;//DFU Command
for(qint32 packetcount=0;packetcount<numberOfPackets;++packetcount)
{
buf[2] = packetcount>>24;//DFU Count
buf[3] = packetcount>>16;//DFU Count
buf[4] = packetcount>>8;//DFU Count
buf[5] = packetcount;//DFU Count
buf[6] = data[packetcount*4+3];//DFU Data0
buf[7] = data[packetcount*4+2];//DFU Data1
buf[8] = data[packetcount*4+1];//DFU Data2
buf[9] = data[packetcount*4];//DFU Data3
int result = hidHandle.send(0,buf, BUF_LEN, 5000);
qDebug()<<packetcount;
// 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)
{
enterDFU(devNumber);
if(description.length()%2!=0)
{
int pad=description.length()/4;
pad=(pad+1)*4;
pad=pad-description.length();
QString padding;
padding.fill(' ',pad);
description.append(padding);
}
StartUpload(description.length()/4,OP_DFU::Descript);
QByteArray array=description.toAscii();
UploadData(description.length()/4,array);
EndOperation();
int ret=StatusRequest();
qDebug()<<"Status="<<ret;
}
QString OP_DFU::DownloadDescription(int devNumber, int numberOfChars)
{
// enterDFU(devNumber);
QByteArray arr=StartDownload(numberOfChars/4,Descript);
QString str(arr);
return str;
}
QByteArray OP_DFU::StartDownload(qint32 numberOfPackets, TransferTypes type)
{
QByteArray ret;
char buf[BUF_LEN];
buf[0] =0x02;//reportID
buf[1] = OP_DFU::Download_Req;//DFU Command
buf[2] = (char)numberOfPackets>>24;//DFU Count
buf[3] = (char)numberOfPackets>>16;//DFU Count
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[8] = 1;//DFU Data2
buf[9] = 1;//DFU Data3
int result = hidHandle.send(0,buf, BUF_LEN, 500);
qDebug() << "StartDownload:"<<result << " bytes sent";
for(qint32 x=0;x<numberOfPackets;++x)
{
result = hidHandle.receive(0,buf,10,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);
}
return ret;
}
void OP_DFU::ResetDevice(void)
{
char buf[BUF_LEN];
buf[0] =0x02;//reportID
buf[1] = OP_DFU::Reset;//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, 500);
}
void OP_DFU::JumpToApp()
{
char buf[BUF_LEN];
buf[0] =0x02;//reportID
buf[1] = OP_DFU::JumpFW;//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, 500);
}
int OP_DFU::StatusRequest()
{
char buf[BUF_LEN];
buf[0] =0x02;//reportID
buf[1] = OP_DFU::Status_Request;//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, 500);
hidHandle.receive(0,buf,10,500);
qDebug() << result << " bytes sent";
result = hidHandle.receive(0,buf,10,500);
qDebug() << result << " bytes received";
if(buf[1]==OP_DFU::Status_Rep)
{
return buf[6];
}
else
return -1;
}
void OP_DFU::EndOperation()
{
char buf[BUF_LEN];
buf[0] =0x02;//reportID
buf[1] = OP_DFU::Op_END;//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, 500);
hidHandle.receive(0,buf,10,500);
qDebug() << result << " bytes sent";
}
void OP_DFU::UploadFirmware(QString sfile)
{
enterDFU(1);
QFile file(sfile);
//QFile file("in.txt");
if (!file.open(QIODevice::ReadOnly))
{
qDebug()<<"Cant open file";
return;
}
QByteArray arr=file.readAll();
if(arr.length()%4!=0)
{
int pad=arr.length()/4;
++pad;
pad=pad*4;
pad=pad-arr.length();
arr.append(QByteArray(pad,255));
}
StartUpload(arr.length()/4,FW);
UploadData(arr.length()/4,arr);
EndOperation();
int ret=StatusRequest();
qDebug()<<"Status="<<ret;
}

View File

@ -0,0 +1,66 @@
#ifndef OP_DFU_H
#define OP_DFU_H
#include <QByteArray>
#include <../../plugins/rawhid/pjrc_rawhid.h>
#include <QDebug>
#include <QFile>
#define BUF_LEN 10
class OP_DFU
{
public:
enum TransferTypes
{
FW,
Hash,
Descript
};
enum Status
{
DFUidle,
uploading,
wrong_packet_received,
too_many_packets,
too_few_packets,
Last_operation_Success,
downloading,
idle,
Last_operation_failed
};
enum Commands
{
Reserved,
Req_Capabilities,
Rep_Capabilities,
EnterDFU,
JumpFW,
Reset,
Abort_Operation,
Upload,
Op_END,
Download_Req,
Download,
Status_Request,
Status_Rep,
};
void JumpToApp();
void ResetDevice(void);
void enterDFU(int devNumber);
void StartUpload(qint32 numberOfPackets, TransferTypes type);
void UploadData(qint32 numberOfPackets,QByteArray data);
void UploadDescription(int devNumber, QString description);
void UploadFirmware(QString sfile);
int StatusRequest();
void EndOperation();
QString DownloadDescription(int devNumber,int numberOfChars);
QByteArray StartDownload(qint32 numberOfPackets, TransferTypes type);
// QByteArray DownloadData(int devNumber,int numberOfPackets);
OP_DFU();
private:
pjrc_rawhid hidHandle;
int setStartBit(int command){return command|0b00100000;}
};
#endif // OP_DFU_H

View File

@ -0,0 +1,26 @@
#-------------------------------------------------
#
# Project created by QtCreator 2010-07-24T11:26:38
#
#-------------------------------------------------
QT += core
QT -= gui
TARGET = HIDTest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp \
op_dfu.cpp
HEADERS += ../../plugins/rawhid/pjrc_rawhid.h \
op_dfu.h
win32 {
SOURCES += ../../plugins/rawhid/pjrc_rawhid_win.cpp
LIBS += -lhid \
-lsetupapi
}