1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
#include "firmwareuploaderwindow.h"
#include "ui_firmwareuploaderwindow.h"
#include "mainwindow.h"
#include <QFile>
//You might wonder: Collin, what in the hell is this for? Firmware uploader? For what? I'm interested! Well, it's a custom
//firmware uploader for a motor controller I built. Why would that be in this project. Cuz. It's not really relevant
//to anyone else but might serve as a decent reference for a few things: How to make an uploader interface that runs over CAN,
//how to lay out a screen like this, how to make a comm protocol for firmware updating over CAN. But, most things use UDS
//for firmware updates and wouldn't need this specific code. But, it might be able to be turned into a UDS firmware uploader or downloader.
//Note that this screen is specifically hidden by default because of it's oddball status. You have to re-enable it in mainwindow.cpp to see it.
FirmwareUploaderWindow::FirmwareUploaderWindow(const QVector<CANFrame> *frames, QWidget *parent) :
QDialog(parent),
ui(new Ui::FirmwareUploaderWindow)
{
ui->setupUi(this);
setWindowFlags(Qt::Window);
transferInProgress = false;
startedProcess = false;
firmwareSize = 0;
currentSendingPosition = 0;
baseAddress = 0;
bus = 0;
modelFrames = frames;
ui->lblFilename->setText("");
ui->spinBus->setValue(0);
ui->spinBus->setMaximum(1);
ui->txtBaseAddr->setText("0x100");
updateProgress();
timer = new QTimer();
timer->setInterval(100); //100ms without a reply will cause us to attempt a resend
connect(MainWindow::getReference(), SIGNAL(framesUpdated(int)), this, SLOT(updatedFrames(int)));
connect(ui->btnLoadFile, SIGNAL(clicked(bool)), this, SLOT(handleLoadFile()));
connect(ui->btnStartStop, SIGNAL(clicked(bool)), this, SLOT(handleStartStopTransfer()));
connect(timer, SIGNAL(timeout()), this, SLOT(timerElapsed()));
}
FirmwareUploaderWindow::~FirmwareUploaderWindow()
{
timer->stop();
CANConManager::getInstance()->removeAllTargettedFrames(this);
delete timer;
delete ui;
}
void FirmwareUploaderWindow::updateProgress()
{
ui->lblProgress->setText(QString::number(currentSendingPosition * 4) + " of " + QString::number(firmwareSize) + " transferred");
}
void FirmwareUploaderWindow::updatedFrames(int numFrames)
{
//CANFrame thisFrame;
if (numFrames == -1) //all frames deleted.
{
}
else if (numFrames == -2) //all new set of frames.
{
}
else //just got some new frames. See if they are relevant.
{
/*
//run through the supposedly new frames in order
for (int i = modelFrames->count() - numFrames; i < modelFrames->count(); i++)
{
thisFrame = modelFrames->at(i);
}
*/
}
}
void FirmwareUploaderWindow::gotTargettedFrame(CANFrame frame)
{
const unsigned char *data = reinterpret_cast<const unsigned char *>(frame.payload().constData());
int dataLen = frame.payload().length();
qDebug() << "FUW: Got targetted frame with id " << frame.frameId();
if (frame.frameId() == (uint32_t)(baseAddress + 0x10) && (dataLen == 8) ) {
qDebug() << "Start firmware reply";
if ((data[0] == 0xAD) && (data[1] == 0xDE))
{
if ((data[2] == 0xAF) && (data[3] == 0xDE))
{
qDebug() << "There's dead beef here";
if ( (data[4] == (token & 0xFF)) && (data[5] == ((token >> 8) & 0xFF) ) )
{
if ((data[6] == ((token >> 16) & 0xFF)) && (data[7] == ((token >> 24) & 0xFF)))
{
qDebug() << "starting firmware process";
//MainWindow::getReference()->setTargettedID(baseAddress + 0x20);
transferInProgress = true;
sendFirmwareChunk();
}
}
}
}
}
if (frame.frameId() == (uint32_t)(baseAddress + 0x20)) {
qDebug() << "Firmware reception success reply";
int seq = data[0] + (256 * data[1]);
if (seq == currentSendingPosition)
{
currentSendingPosition++;
if (currentSendingPosition * 4 > firmwareSize || currentSendingPosition > 65535)
{
transferInProgress = false;
timer->stop();
handleStartStopTransfer();
ui->progressBar->setValue(100);
sendFirmwareEnding();
}
else
{
ui->progressBar->setValue((400 * currentSendingPosition) / firmwareSize);
updateProgress();
sendFirmwareChunk();
}
}
}
}
void FirmwareUploaderWindow::timerElapsed()
{
sendFirmwareChunk(); //resend
}
void FirmwareUploaderWindow::sendFirmwareChunk()
{
CANFrame output;
int firmwareLocation = currentSendingPosition * 4;
int xorByte = 0;
output.setExtendedFrameFormat(false);
QByteArray bytes(7,0);
output.bus = bus;
output.setFrameId(baseAddress + 0x16);
bytes[0] = currentSendingPosition & 0xFF;
bytes[1] = (currentSendingPosition >> 8) & 0xFF;
bytes[2] = firmwareData[firmwareLocation++];
bytes[3] = firmwareData[firmwareLocation++];
bytes[4] = firmwareData[firmwareLocation++];
bytes[5] = firmwareData[firmwareLocation++];
for (int i = 0; i < 6; i++) xorByte ^= static_cast<unsigned char>(bytes[i]);
bytes[6] = xorByte;
output.setPayload(bytes);
CANConManager::getInstance()->sendFrame(output);
timer->start();
}
void FirmwareUploaderWindow::sendFirmwareEnding()
{
CANFrame output;
output.setExtendedFrameFormat(false);
output.bus = bus;
QByteArray bytes(4,0);
output.setFrameId(baseAddress + 0x30);
bytes[3] = (char)0xC0;
bytes[2] = (char)0xDE;
bytes[1] = (char)0xFA;
bytes[0] = (char)0xDE;
output.setPayload(bytes);
//sendCANFrame(output, bus);
}
void FirmwareUploaderWindow::handleStartStopTransfer()
{
startedProcess = !startedProcess;
if (startedProcess) //start the process
{
ui->progressBar->setValue(0);
ui->btnStartStop->setText("Stop Upload");
token = Utility::ParseStringToNum(ui->txtToken->text());
bus = ui->spinBus->value();
baseAddress = Utility::ParseStringToNum(ui->txtBaseAddr->text());
qDebug() << "Base address: " + QString::number(baseAddress);
CANConManager::getInstance()->addTargettedFrame(bus, baseAddress + 0x10, 0x7FF, this);
CANConManager::getInstance()->addTargettedFrame(bus, baseAddress + 0x20, 0x7FF, this);
CANFrame output;
output.setExtendedFrameFormat(false);
QByteArray bytes(8,0);
output.bus = bus;
output.setFrameId(baseAddress);
output.setFrameType(QCanBusFrame::DataFrame);
bytes[0] = (char)0xEF;
bytes[1] = (char)0xBE;
bytes[2] = (char)0xAD;
bytes[3] = (char)0xDE;
bytes[4] = token & 0xFF;
bytes[5] = (token >> 8) & 0xFF;
bytes[6] = (token >> 16) & 0xFF;
bytes[7] = (token >> 24) & 0xFF;
output.setPayload(bytes);
CANConManager::getInstance()->sendFrame(output);
}
else //stop anything in process
{
ui->btnStartStop->setText("Start Upload");
CANConManager::getInstance()->removeAllTargettedFrames(this);
}
}
void FirmwareUploaderWindow::handleLoadFile()
{
QFileDialog dialog;
QStringList filters;
filters.append(QString(tr("Raw firmware binary (*.bin)")));
dialog.setFileMode(QFileDialog::ExistingFile);
dialog.setNameFilters(filters);
dialog.setViewMode(QFileDialog::Detail);
if (dialog.exec() == QDialog::Accepted)
{
QString filename = dialog.selectedFiles().constFirst();
loadBinaryFile(filename);
}
}
void FirmwareUploaderWindow::loadBinaryFile(QString filename)
{
if (transferInProgress) handleStartStopTransfer();
QFile inFile(filename);
if (!inFile.open(QIODevice::ReadOnly))
{
return;
}
firmwareData = inFile.readAll();
currentSendingPosition = 0;
firmwareSize = firmwareData.length();
updateProgress();
inFile.close();
}
|