#include "op_dfu.h" #include #include #include OP_DFU::OP_DFU(bool _debug): debug(_debug) { send_delay=10; use_delay=true; int numDevices=0; int count=0; while(numDevices==0) { if (debug) qDebug() << "."; delay::msleep(500); numDevices = hidHandle.open(1,0x20a0,0x4117,0,0); //0xff9c,0x0001); if(++count==10) { count=0; } } if(debug) qDebug() << numDevices << " device(s) opened"; } bool OP_DFU::SaveByteArrayToFile(QString const & sfile, const QByteArray &array) { QFile file(sfile); if (!file.open(QIODevice::WriteOnly)) { if(debug) qDebug()<<"Can't open file"; return false; } file.write(array); file.close(); return true; } bool OP_DFU::enterDFU(int const &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); if(result<1) return false; if(debug) qDebug() << "EnterDFU: " << result << " bytes sent"; return true; } bool OP_DFU::StartUpload(qint32 const & numberOfBytes, TransferTypes const & type,quint32 crc) { int lastPacketCount; qint32 numberOfPackets=numberOfBytes/4/14; int pad=(numberOfBytes-numberOfPackets*4*14)/4; if(pad==0) { lastPacketCount=14; } else { ++numberOfPackets; lastPacketCount=pad; } 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] = lastPacketCount;//DFU Data1 buf[8] = crc>>24; buf[9] = crc>>16; buf[10] = crc>>8; buf[11] = crc; if(debug) qDebug()<<"Number of packets:"<0) { return true; } return false; } bool OP_DFU::UploadData(qint32 const & numberOfBytes, QByteArray & data) { int lastPacketCount; qint32 numberOfPackets=numberOfBytes/4/14; int pad=(numberOfBytes-numberOfPackets*4*14)/4; if(pad==0) { lastPacketCount=14; } else { ++numberOfPackets; lastPacketCount=pad; } if(debug) qDebug()<<"Start Uploading:"<>24;//DFU Count buf[3] = packetcount>>16;//DFU Count buf[4] = packetcount>>8;//DFU Count buf[5] = packetcount;//DFU Count char *pointer=data.data(); pointer=pointer+4*14*packetcount; // qDebug()<<"Packet Number="<>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] = lastPacketCount;//DFU Data1 buf[8] = 1;//DFU Data2 buf[9] = 1;//DFU Data3 int result = hidHandle.send(0,buf, BUF_LEN, 500); if(debug) qDebug() << "StartDownload:"<>(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].BL_Version=buf[7]; devices[x].SizeOfDesc=buf[8]; quint32 aux; aux=(quint8)buf[10]; aux=aux<<8 |(quint8)buf[11]; aux=aux<<8 |(quint8)buf[12]; aux=aux<<8 |(quint8)buf[13]; devices[x].FW_CRC=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; } if(debug) { qDebug()<<"Found "<0) return true; return false; } OP_DFU::Status OP_DFU::UploadFirmware(const QString &sfile, const bool &verify,int device) { OP_DFU::Status ret; cout<<"Starting Firmware Uploading...\n"; QFile file(sfile); //QFile file("in.txt"); if (!file.open(QIODevice::ReadOnly)) { if(debug) qDebug()<<"Cant open file"; return OP_DFU::abort; } QByteArray arr=file.readAll(); if(debug) qDebug()<<"Bytes Loaded="<"); }else{ bar.replace(i,1," "); } } std::cout<< "\r"<> 2; // /4 Size passed in as a byte count, assumed to be a multiple of 4 while(Size--) { static const quint32 CrcTable[16] = { // Nibble lookup table for 0x04C11DB7 polynomial 0x00000000,0x04C11DB7,0x09823B6E,0x0D4326D9,0x130476DC,0x17C56B6B,0x1A864DB2,0x1E475005, 0x2608EDB8,0x22C9F00F,0x2F8AD6D6,0x2B4BCB61,0x350C9B64,0x31CD86D3,0x3C8EA00A,0x384FBDBD }; Crc = Crc ^ *((quint32 *)Buffer); // Apply all 32-bits Buffer += 1; // Process 32-bits, 4 at a time, or 8 rounds Crc = (Crc << 4) ^ CrcTable[Crc >> 28]; // Assumes 32-bit reg, masking index to 4-bits Crc = (Crc << 4) ^ CrcTable[Crc >> 28]; // 0x04C11DB7 Polynomial used in STM32 Crc = (Crc << 4) ^ CrcTable[Crc >> 28]; Crc = (Crc << 4) ^ CrcTable[Crc >> 28]; Crc = (Crc << 4) ^ CrcTable[Crc >> 28]; Crc = (Crc << 4) ^ CrcTable[Crc >> 28]; Crc = (Crc << 4) ^ CrcTable[Crc >> 28]; Crc = (Crc << 4) ^ CrcTable[Crc >> 28]; } return(Crc); } quint32 OP_DFU::CRCFromQBArray(QByteArray array, quint32 Size) { int pad=Size-array.length(); array.append(QByteArray(pad,255)); quint32 t[Size/4]; for(int x=0;x