forked from bigbigmdm/CH341a_spi_programmer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
845 lines (803 loc) · 29.3 KB
/
mainwindow.cpp
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
/*
* Copyright (C) 2023 Mikhail Medvedev <[email protected]>
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLineEdit>
#include <QLabel>
#include <QMessageBox>
#include <QErrorMessage>
#include <QDragEnterEvent>
#include <QtGui>
#include "qhexedit.h"
#include "dialogsp.h"
#include "dialogrp.h"
#include <alloca.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
int index2;
max_rec = 0;
ui->setupUi(this);
ui->statusBar->addPermanentWidget(ui->lStatus,0);
ui->statusBar->addPermanentWidget(ui->eStatus,0);
ui->statusBar->addPermanentWidget(ui->jLabel,0);
ui->statusBar->addPermanentWidget(ui->jedecEdit,0);
ui->progressBar->setValue(0);
ui->comboBox_name->addItems({""});
ui->comboBox_man->addItems({""});
ui->comboBox_vcc->addItems({ " ", "3.3 V", "1.8 V", "5.0 V"});
//0 = 20kHz; 1 = 100kHz, 2 = 400kHz, 3 = 750kHz
//ui->comboBox_speed->addItem(" ", -1); //uncorrected value
ui->comboBox_speed->addItem("750 khz", 3);
ui->comboBox_speed->addItem("400 khz", 2);
ui->comboBox_speed->addItem("100 khz", 1);
ui->comboBox_speed->addItem("20 khz", 0);
ui->comboBox_page->addItem(" ", 0);
ui->comboBox_page->addItem("1", 1);
ui->comboBox_page->addItem("2", 2);
ui->comboBox_page->addItem("4", 4);
ui->comboBox_page->addItem("8", 8);
ui->comboBox_page->addItem("16", 16);
ui->comboBox_page->addItem("32", 32);
ui->comboBox_page->addItem("64", 64);
ui->comboBox_page->addItem("128", 128);
ui->comboBox_page->addItem("256", 256);
ui->comboBox_page->addItem("512", 512);
ui->comboBox_size->addItem(" ", 0);
ui->comboBox_size->addItem("64 K", 64 * 1024);
ui->comboBox_size->addItem("128 K", 128 * 1024);
ui->comboBox_size->addItem("256 K", 256 * 1024);
ui->comboBox_size->addItem("512 K", 512 * 1024);
ui->comboBox_size->addItem("1 M", 1024 * 1024);
ui->comboBox_size->addItem("2 M", 2048 * 1024);
ui->comboBox_size->addItem("4 M", 4096 * 1024);
ui->comboBox_size->addItem("8 M", 8192 * 1024);
ui->comboBox_size->addItem("16 M", 16384 * 1024);
ui->comboBox_size->addItem("32 M", 32768 * 1024);
ui->comboBox_size->addItem("64 M", 65536 * 1024);
currentChipSize = 0;
currentNumBlocks = 0;
currentBlockSize = 0;
currentSpeed = 0;
blockStartAddr = 0;
blockLen = 0;
// connect and status check
statusCH341 = ch341Configure(0x1a86,0x5512); // VID 1a86 PID 5512 for CH341A
ch341StatusFlashing();
chipData.resize(256);
for (int i=0; i < 256; i++)
{
chipData[i] = char(0xff);
}
//ch341SetStream(3);
ch341Release();
hexEdit = new QHexEdit(ui->frame);
hexEdit->setGeometry(0,0,ui->frame->width(),ui->frame->height());
hexEdit->setData(chipData);
//ui->pushButton->resize(165, ui->pushButton->height()); //change height // widget->setFixedWidth(165)
//opening chip database file
ui->statusBar->showMessage("Opening DAT file");
QFile datfile("CH341a.Dat");
QByteArray dataChips;
if (!datfile.open(QIODevice::ReadOnly))
{
QMessageBox::about(this, "Error", "Error loading chip database file!");
return;
}
dataChips = datfile.readAll();
datfile.close();
//parsing dat file
ui->statusBar->showMessage("Parsing DAT file");
//parsing qbytearray
char txtBuf[0x30];
int i, j, recNo, dataPoz, dataSize, delay;
uint32_t chipSize;
uint16_t blockSize;
unsigned char tmpBuf;
dataPoz = 0;
recNo = 0;
QStringList verticalHeader;
dataSize = dataChips.length();
while (dataPoz < dataSize)
{
for (j=0; j<0x30; j++)
{
txtBuf[j] = 0;
}
j = 0;
while ((j < 0x10) && (dataChips[recNo * 0x44 + j] != ',')) // ASCII data reading
{
txtBuf[j] = dataChips[recNo * 0x44 + j];
j++;
}
if (txtBuf[1] == 0x00) break;
chips[recNo].chipTypeTxt = QByteArray::fromRawData(txtBuf, 0x30);
for (i=0; i<0x30; i++)
{
txtBuf[i] = 0;
}
j++;
i = 0;
while ((i < 0x20) && (dataChips[recNo * 0x44 + j] != ',')) // ASCII data reading
{
txtBuf[i] = dataChips[recNo * 0x44 + j];
j++;
i++;
}
chips[recNo].chipManuf = QByteArray::fromRawData(txtBuf, 0x30);
for (i=0; i<0x30; i++)
{
txtBuf[i] = 0;
}
j++;
i = 0;
while ((i < 0x30) && (dataChips[recNo * 0x44 + j] != '\0')) // ASCII data reading
{
txtBuf[i] = dataChips[recNo * 0x44 + j];
j++;
i++;
}
chips[recNo].chipName = QByteArray::fromRawData(txtBuf, 0x30);
chips[recNo].chipJedecIDMan = static_cast<uint8_t>(dataChips[recNo * 0x44 + 0x32]);
chips[recNo].chipJedecIDDev = static_cast<uint8_t>(dataChips[recNo * 0x44 + 0x31]);
chips[recNo].chipJedecIDCap = static_cast<uint8_t>(dataChips[recNo * 0x44 + 0x30]);
tmpBuf = static_cast<unsigned char>(dataChips[recNo * 0x44 + 0x34]);
chipSize = tmpBuf;
tmpBuf = static_cast<unsigned char>(dataChips[recNo * 0x44 + 0x35]);
chipSize = chipSize + tmpBuf * 256;
tmpBuf = static_cast<unsigned char>(dataChips[recNo * 0x44 + 0x36]);
chipSize = chipSize + tmpBuf * 256 * 256;
tmpBuf = static_cast<unsigned char>(dataChips[recNo * 0x44 + 0x37]);
chipSize = chipSize + tmpBuf * 256 * 256 * 256;
chips[recNo].chipSize = chipSize;
tmpBuf = static_cast<unsigned char>(dataChips[recNo * 0x44 + 0x38]);
blockSize = tmpBuf;
tmpBuf = static_cast<unsigned char>(dataChips[recNo * 0x44 + 0x39]);
blockSize = blockSize + tmpBuf * 256;
chips[recNo].blockSize = blockSize;
tmpBuf = static_cast<unsigned char>(dataChips[recNo * 0x44 + 0x3a]);
chips[recNo].chipTypeHex = tmpBuf;
tmpBuf = static_cast<unsigned char>(dataChips[recNo * 0x44 + 0x3b]);
chips[recNo].algorithmCode = tmpBuf;
tmpBuf = static_cast<unsigned char>(dataChips[recNo * 0x44 + 0x3c]);
delay = tmpBuf;
tmpBuf = static_cast<unsigned char>(dataChips[recNo * 0x44 + 0x3d]);
delay = delay + tmpBuf * 256;
chips[recNo].delay = delay;
tmpBuf = static_cast<unsigned char>(dataChips[recNo * 0x44 + 0x3e]);
chips[recNo].extend = tmpBuf;
tmpBuf = static_cast<unsigned char>(dataChips[recNo * 0x44 + 0x40]);
chips[recNo].eeprom = tmpBuf;
tmpBuf = static_cast<unsigned char>(dataChips[recNo * 0x44 + 0x42]);
chips[recNo].eepromPages = tmpBuf;
tmpBuf = static_cast<unsigned char>(dataChips[recNo * 0x44 + 0x43]);
if (tmpBuf == 0x00) chips[recNo].chipVCC = "3.3 V";
if (tmpBuf == 0x01) chips[recNo].chipVCC = "1.8 V";
if (tmpBuf == 0x02) chips[recNo].chipVCC = "5.0 V";
dataPoz = dataPoz + 0x44; //next record
verticalHeader.append(QString::number(recNo));
recNo++;
}
max_rec = recNo;
for (i = 0; i<max_rec; i++)
{
//replacing items to combobox Manufacture
index2 = ui->comboBox_man->findText(chips[i].chipManuf);
if ( index2 == -1 ) ui->comboBox_man->addItem(chips[i].chipManuf);
}
ui->comboBox_man->setCurrentIndex(0);
ui->statusBar->showMessage("");
}
MainWindow::~MainWindow()
{
delete ui;
}
QString MainWindow::bytePrint(unsigned char z)
{
unsigned char s;
s = z / 16;
if (s > 0x9) s = s + 0x37;
else s = s + 0x30;
z = z % 16;
if (z > 0x9) z = z + 0x37;
else z = z + 0x30;
return QString(s) + QString(z);
}
void MainWindow::on_pushButton_clicked()
{
statusCH341 = ch341Configure(0x1a86,0x5512);
ch341StatusFlashing();
ch341SetStream(currentSpeed);
//Reading data from chip
if ((currentNumBlocks > 0) && (currentBlockSize >0))
{
uint32_t addr = 0;
uint32_t curBlock = 0;
uint32_t j, k;
int res;
//progerssbar settings
ui->progressBar->setRange(0, static_cast<int>(currentNumBlocks));
ui->progressBar->setValue(0);
//uint8_t buf[currentBlockSize];
uint8_t *buf;
buf = (uint8_t *)malloc(currentBlockSize);
ui->pushButton->setStyleSheet("QPushButton{color:#fff;background-color:#f66;border-radius: 20px;border: 2px solid #094065;border-radius:8px;font-weight:600;}");
ui->statusBar->showMessage("Reading data from " + ui->comboBox_name->currentText());
//Select bus speed
//
for (k = 0; k < currentNumBlocks; k++)
{
//int32_t ch341SpiRead(uint8_t *buf, uint32_t add, uint32_t len);
res = ch341SpiRead(buf, curBlock * currentBlockSize, currentBlockSize);
// if res=-1 - error, stop
if (statusCH341 != 0)
{
QMessageBox::about(this, "Error", "Programmer CH341a is not connected!");
break;
}
if (res != 3)
{
QMessageBox::about(this, "Error", "Error reading sector " + QString::number(curBlock));
break;
}
for (j = 0; j < currentBlockSize; j++)
{
chipData[addr + j] = char(buf[addr + j - k * currentBlockSize]);
}
addr = addr + currentBlockSize;
curBlock++;
if (curBlock == 2) hexEdit->setData(chipData); //show buffer in hehedit while chip data is being loaded
ui->progressBar->setValue(static_cast<int>(curBlock));
}
}
else
{
//Not correct Number fnd size of blocks
QMessageBox::about(this, "Error", "Before reading from chip please press 'Detect' button.");
}
hexEdit->setData(chipData);
ui->statusBar->showMessage("");
ui->progressBar->setValue(0);
ui->pushButton->setStyleSheet("QPushButton{color:#fff;background-color:rgb(120, 183, 140);border-radius: 20px;border: 2px solid #094065;border-radius:8px;font-weight:600;}");
ch341Release();
}
QString MainWindow::sizeConvert(int a)
{
QString rez;
rez = "0";
if (a < 1024) rez = QString::number(a) + " B";
else if ((a < 1024 * 1024)) rez = QString::number(a/1024) + " K";
else rez = QString::number(a/1024/1024) + " M";
return rez;
}
void MainWindow::on_pushButton_2_clicked()
{
//searching the connected chip in database
statusCH341 = ch341Configure(0x1a86,0x5512);
ch341StatusFlashing();
ch341SetStream(currentSpeed);
if (statusCH341 != 0)
{
QMessageBox::about(this, "Error", "Programmer CH341a is not connected!");
return;
}
ui->pushButton_2->setStyleSheet("QPushButton {color:#fff;background-color:#f66;border-radius: 20px;border: 2px solid #094065;border-radius:8px;font-weight:600;}");
int i, index;
// print JEDEC info
struct JEDEC listJEDEC;
//ui->sizeEdit->setText(QString::number(ch341SpiCapacity()));
listJEDEC = GetJedecId();
if ((listJEDEC.man == 0xff) && (listJEDEC.type == 0xff) && (listJEDEC.capacity == 0xff))
{
QMessageBox::about(this, "Error", "The chip is not connect or missing!");
ui->pushButton_2->setStyleSheet("QPushButton{color:#fff;background-color:rgb(120, 183, 140);border-radius: 20px;border: 2px solid #094065;border-radius:8px;font-weight:600;}");
ch341Release();
return;
}
ui->jedecEdit->setText(bytePrint(listJEDEC.man) + " " + bytePrint(listJEDEC.type) + " " + bytePrint(listJEDEC.capacity));
for (i = 0; i< max_rec; i++)
{
if ((listJEDEC.man == chips[i].chipJedecIDMan) && (listJEDEC.type == chips[i].chipJedecIDDev) && (listJEDEC.capacity == chips[i].chipJedecIDCap))
{
index = ui->comboBox_man->findText(chips[i].chipManuf);
if ( index != -1 )
{ // -1 for not found
ui->comboBox_man->setCurrentIndex(index);
}
index = ui->comboBox_name->findText(chips[i].chipName);
if ( index != -1 )
{ // -1 for not found
ui->comboBox_name->setCurrentIndex(index);
}
index = ui->comboBox_size->findData(chips[i].chipSize);
if ( index != -1 )
{ // -1 for not found
ui->comboBox_size->setCurrentIndex(index);
}
index = ui->comboBox_page->findData(chips[i].blockSize);
if ( index != -1 )
{ // -1 for not found
ui->comboBox_page->setCurrentIndex(index);
}
index = ui->comboBox_vcc->findText(chips[i].chipVCC);
if ( index != -1 )
{ // -1 for not found
ui->comboBox_vcc->setCurrentIndex(index);
}
ui->pushButton_2->setStyleSheet("QPushButton{color:#fff;background-color:rgb(120, 183, 140);border-radius: 20px;border: 2px solid #094065;border-radius:8px;font-weight:600;}");
break;
}
}
ui->pushButton_2->setStyleSheet("QPushButton{color:#fff;background-color:rgb(120, 183, 140);border-radius: 20px;border: 2px solid #094065;border-radius:8px;font-weight:600;}");
ch341Release();
}
void MainWindow::on_comboBox_size_currentIndexChanged(int index)
{
//qDebug() <<"size="<< ui->comboBox_size->currentData().toInt() << " block_size=" << ui->comboBox_page->currentData().toInt();
currentChipSize = ui->comboBox_size->currentData().toUInt();
currentBlockSize = ui->comboBox_page->currentData().toUInt();
if ((currentChipSize !=0) && (currentBlockSize!=0))
{
currentNumBlocks = currentChipSize / currentBlockSize;
chipData.resize(static_cast<int>(currentChipSize));
for (uint32_t i=0; i < currentChipSize; i++)
{
chipData[i] = char(0xff);
}
hexEdit->setData(chipData);
}
index = index + 0;
}
void MainWindow::on_comboBox_page_currentIndexChanged(int index)
{
currentChipSize = ui->comboBox_size->currentData().toUInt();
currentBlockSize = ui->comboBox_page->currentData().toUInt();
if ((currentChipSize !=0) && (currentBlockSize!=0))
{
currentNumBlocks = currentChipSize / currentBlockSize;
chipData.resize(static_cast<int>(currentChipSize));
for (uint32_t i=0; i < currentChipSize; i++)
{
chipData[i] = char(0xff);
}
hexEdit->setData(chipData);
}
index = index + 0;
}
void MainWindow::on_comboBox_speed_currentIndexChanged(int index)
{
if (ui->comboBox_speed->currentData().toInt() != -1)
{
currentSpeed = static_cast<unsigned char>(ui->comboBox_speed->currentData().toInt());
ch341SetStream(currentSpeed);
}
index = index + 0;
}
void MainWindow::on_actionDetect_triggered()
{
MainWindow::on_pushButton_2_clicked();
}
void MainWindow::on_actionSave_triggered()
{
ui->statusBar->showMessage("Saving file");
fileName = QFileDialog::getSaveFileName(this,
QString::fromUtf8("Save file"),
QDir::currentPath(),
"Data Images (*.bin);;All files (*.*)");
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
{
QMessageBox::about(this, "Error", "Error saving file!");
return;
}
file.write(hexEdit->data());
file.close();
}
void MainWindow::on_actionErase_triggered()
{
statusCH341 = ch341Configure(0x1a86,0x5512);
ch341StatusFlashing();
ch341SetStream(currentSpeed);
int i;
if (statusCH341 != 0)
{
QMessageBox::about(this, "Error", "Programmer CH341a is not connected!");
return;
}
ui->statusBar->showMessage("Erasing the " + ui->comboBox_name->currentText());
ui->checkBox->setStyleSheet("QCheckBox{font-weight:600;}");
ui->centralWidget->repaint();
ui->progressBar->setRange(0, 100);
ui->progressBar->setValue(50);
ch341EraseChip();
for (i=0; i < 120; i++)
{
sleep(1);
qDebug() << "Status=" << ch341ReadStatus() << " " << i;
if (ch341ReadStatus() == 0) break;
}
qDebug() << "Status=" << ch341ReadStatus();
ui->checkBox->setStyleSheet("");
ui->statusBar->showMessage("");
ui->progressBar->setValue(0);
ui->centralWidget->repaint();
ch341Release();
}
void MainWindow::on_actionUndo_triggered()
{
hexEdit->undo();
}
void MainWindow::on_actionRedo_triggered()
{
hexEdit->redo();
}
void MainWindow::on_actionOpen_triggered()
{
ui->statusBar->showMessage("Opening file");
fileName = QFileDialog::getOpenFileName(this,
QString::fromUtf8("Open file"),
QDir::currentPath(),
"Data Images (*.bin);;All files (*.*)");
ui->statusBar->showMessage("Current file: " + fileName);
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
{
return;
}
chipData = file.readAll();
hexEdit->setData(chipData);
file.close();
ui->statusBar->showMessage("");
}
void MainWindow::on_actionWrite_triggered()
{
//Writting data to chip
statusCH341 = ch341Configure(0x1a86,0x5512);
ch341StatusFlashing();
ch341SetStream(currentSpeed);
if (statusCH341 != 0)
{
QMessageBox::about(this, "Error", "Programmer CH341a is not connected!");
return;
}
if ((currentNumBlocks > 0) && (currentBlockSize >0))
{
uint32_t addr = 0;
uint32_t curBlock = 0;
uint32_t j, k;
int res;
ui->statusBar->showMessage("Writing data to " + ui->comboBox_name->currentText());
//progerssbar settings
ui->progressBar->setRange(0, static_cast<int>(currentNumBlocks));
ui->checkBox_2->setStyleSheet("QCheckBox{font-weight:800;}");
chipData = hexEdit->data();
//uint8_t buf[currentBlockSize];
uint8_t *buf;
buf = (uint8_t *)malloc(currentBlockSize);
for (k = 0; k < currentNumBlocks; k++)
{
for (j = 0; j < currentBlockSize; j++)
{
buf[addr + j - k * currentBlockSize] = static_cast<uint8_t>(chipData[addr + j]) ;
}
//int32_t ch341SpiWrite(uint8_t *buf, uint32_t add, uint32_t len)
res = ch341SpiWrite(buf, curBlock * currentBlockSize, currentBlockSize);
// if res=-1 - error, stop
if (statusCH341 != 0)
{
QMessageBox::about(this, "Error", "Programmer CH341a is not connected!");
break;
}
if (res != 0)
{
QMessageBox::about(this, "Error", "Error writing sector " + QString::number(curBlock));
break;
}
addr = addr + currentBlockSize;
curBlock++;
ui->progressBar->setValue(static_cast<int>(curBlock));
}
}
else
{
//Not correct Number fnd size of blocks
QMessageBox::about(this, "Error", "Before reading from chip please press 'Detect' button.");
}
ui->progressBar->setValue(0);
ui->checkBox_2->setStyleSheet("");
ui->statusBar->showMessage("");
ch341Release();
}
void MainWindow::on_actionRead_triggered()
{
MainWindow::on_pushButton_clicked();
}
void MainWindow::on_actionExit_triggered()
{
ch341Release();
MainWindow::close();
}
void MainWindow::on_comboBox_man_currentIndexChanged(int index)
{
int i, index2;
QString txt="";
if (max_rec > 0)
{
txt = ui->comboBox_man->currentText().toUtf8();
ui->comboBox_name->clear();
ui->comboBox_name->addItem("");
for (i = 0; i<max_rec; i++)
{
//replacing items to combobox chip Name
if (txt.compare(chips[i].chipManuf)==0)
{
index2 = ui->comboBox_name->findText(chips[i].chipName);
if ( index2 == -1 ) ui->comboBox_name->addItem(chips[i].chipName);
}
}
ui->comboBox_name->setCurrentIndex(0);
ui->comboBox_vcc->setCurrentIndex(0);
ui->comboBox_page->setCurrentIndex(0);
ui->comboBox_size->setCurrentIndex(0);
ui->statusBar->showMessage("");
}
index = index + 0;
}
void MainWindow::on_comboBox_name_currentIndexChanged(const QString &arg1)
{
int i, index;
QString manName = ui->comboBox_man->currentText();
if (arg1.compare("") !=0)
{
for (i = 0; i < max_rec; i++)
{
if ((manName.compare(chips[i].chipManuf)==0) && (arg1.compare(chips[i].chipName)==0))
{
index = ui->comboBox_size->findData(chips[i].chipSize);
if ( index != -1 )
{ // -1 for not found
ui->comboBox_size->setCurrentIndex(index);
}
index = ui->comboBox_page->findData(chips[i].blockSize);
if ( index != -1 )
{ // -1 for not found
ui->comboBox_page->setCurrentIndex(index);
}
index = ui->comboBox_vcc->findText(chips[i].chipVCC);
if ( index != -1 )
{ // -1 for not found
ui->comboBox_vcc->setCurrentIndex(index);
}
}
}
}
}
void MainWindow::on_actionVerify_triggered()
{
//Reading data from chip
statusCH341 = ch341Configure(0x1a86,0x5512);
ch341StatusFlashing();
ch341SetStream(currentSpeed);
if ((currentNumBlocks > 0) && (currentBlockSize >0))
{
uint32_t addr = 0;
uint32_t curBlock = 0;
uint32_t j, k;
int res;
//progerssbar settings
ui->progressBar->setRange(0, static_cast<int>(currentNumBlocks));
ui->progressBar->setValue(0);
//uint8_t buf[currentBlockSize];
uint8_t *buf;
buf = (uint8_t *)malloc(currentBlockSize);
chipData = hexEdit->data();
ui->checkBox_3->setStyleSheet("QCheckBox{font-weight:800;}");
ui->statusBar->showMessage("Veryfing data from " + ui->comboBox_name->currentText());
//Select bus speed
//
for (k = 0; k < currentNumBlocks; k++)
{
//int32_t ch341SpiRead(uint8_t *buf, uint32_t add, uint32_t len);
res = ch341SpiRead(buf, curBlock * currentBlockSize, currentBlockSize);
// if res=-1 - error, stop
if (statusCH341 != 0)
{
QMessageBox::about(this, "Error", "Programmer CH341a is not connected!");
break;
}
if (res != 3)
{
QMessageBox::about(this, "Error", "Error reading sector " + QString::number(curBlock));
break;
}
for (j = 0; j < currentBlockSize; j++)
{
if (chipData[addr + j] != char(buf[addr + j - k * currentBlockSize]))
{
//error compare
QMessageBox::about(this, "Error", "Error comparing data!\nAddress: " + hexiAddr(addr + j) + "\nBuffer: " + bytePrint(static_cast<unsigned char>(chipData[addr + j])) + " Chip: " + bytePrint(static_cast<unsigned char>(buf[addr + j - k * currentBlockSize])));
ui->statusBar->showMessage("");
ui->checkBox_3->setStyleSheet("");
return;
}
}
addr = addr + currentBlockSize;
curBlock++;
ui->progressBar->setValue(static_cast<int>(curBlock));
}
}
else
{
//Not correct Number fnd size of blocks
QMessageBox::about(this, "Error", "Before reading from chip please press 'Detect' button.");
}
ui->statusBar->showMessage("");
ui->progressBar->setValue(0);
ui->checkBox_3->setStyleSheet("");
ch341Release();
}
QString MainWindow::hexiAddr(uint32_t add)
{
QString rez = "";
uint8_t A,B,C,D;
D = 0xFF & add;
add = add >> 8;
C = 0xFF & add;
add = add >> 8;
B = 0xFF & add;
add = add >> 8;
A = 0xFF & add;
rez = bytePrint(A) + bytePrint(B) + bytePrint(C) + bytePrint(D);
return rez;
}
void MainWindow::on_pushButton_3_clicked()
{
ui->pushButton_3->setStyleSheet("QPushButton{color:#fff;background-color:#f66;border-radius: 20px;border: 2px solid #094065;border-radius:8px;font-weight:600;}");
if (ui->checkBox->isChecked()) MainWindow::on_actionErase_triggered();
if (ui->checkBox_2->isChecked()) MainWindow::on_actionWrite_triggered();
if (ui->checkBox_3->isChecked()) MainWindow::on_actionVerify_triggered();
ui->pushButton_3->setStyleSheet("QPushButton{color:#fff;background-color:rgb(120, 183, 140);border-radius: 20px;border: 2px solid #094065;border-radius:8px;font-weight:600;}");
}
void MainWindow::receiveAddr(QString addressData)
{
uint32_t ee, blockEndAddr = 0;
int e,t;
QString endType;
e = addressData.indexOf("-");
t = addressData.length();
blockStartAddr = 0;
blockLen = 0;
endType = addressData.mid(t - 1, 1);
blockStartAddr = hexToInt(addressData.mid(0, e));
if (endType.compare("*")==0)
{
blockEndAddr = hexToInt(addressData.mid(e + 1, t - e - 2));
if (blockEndAddr < blockStartAddr)
{
QMessageBox::about(this, "Error", "The end address must be greater than the starting addres.");
return;
}
blockLen = blockEndAddr - blockStartAddr;
}
else blockLen = hexToInt(addressData.mid(e + 1, t - e - 2));
//qDebug() << blockStartAddr << " " << blockEndAddr << " " << blockLen;
block.resize(static_cast<int>(blockLen));
chipData = hexEdit->data();
for (ee = 0; ee < blockLen; ee++)
{
block[ee] = chipData[ee + blockStartAddr];
}
ui->statusBar->showMessage("Saving block");
fileName = QFileDialog::getSaveFileName(this,
QString::fromUtf8("Save block"),
QDir::currentPath(),
"Data Images (*.bin);;All files (*.*)");
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
{
QMessageBox::about(this, "Error", "Error saving file!");
return;
}
file.write(block);
file.close();
}
void MainWindow::receiveAddr2(QString addressData)
{
uint32_t ee;
QString endType;
blockStartAddr = 0;
blockLen = 0;
blockStartAddr = hexToInt(addressData);
ui->statusBar->showMessage("Opening block");
fileName = QFileDialog::getOpenFileName(this,
QString::fromUtf8("Open block"),
QDir::currentPath(),
"Data Images (*.bin);;All files (*.*)");
ui->statusBar->showMessage("Current file: " + fileName);
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
{
return;
}
block = file.readAll();
blockLen = static_cast<uint32_t>(block.size());
chipData = hexEdit->data();
if (blockStartAddr + blockLen > static_cast<uint32_t>(chipData.size()))
{
QMessageBox::about(this, "Error", "The end address out of image size!");
return;
}
for (ee=0; ee < blockLen; ee++)
{
chipData[ee + blockStartAddr] = block[ee];
}
hexEdit->setData(chipData);
file.close();
ui->statusBar->showMessage("");
}
void MainWindow::on_actionSave_Part_triggered()
{
DialogSP* savePartDialog = new DialogSP();
savePartDialog->show();
connect(savePartDialog, SIGNAL(sendAddr(QString)), this, SLOT(receiveAddr(QString)));
}
uint32_t MainWindow::hexToInt(QString str)
{
unsigned char c;
uint32_t len = static_cast<uint32_t>(str.length());
QByteArray bstr = str.toLocal8Bit();
if ((len > 0) && (len < 8))
{
uint32_t i, j = 1;
uint32_t addr = 0;
for (i = len; i >0; i--)
{
c = static_cast<unsigned char>(bstr[i-1]);
if ((c >= 0x30) && (c <=0x39)) addr = addr + (c - 0x30) * j;
if ((c >= 0x41) && (c <= 0x46)) addr = addr + (c - 0x37) * j;
if ((c >= 0x61) && (c <= 0x66)) addr = addr + (c - 0x57) * j;
j = j * 16;
}
return addr;
}
else return 0;
}
void MainWindow::on_actionLoad_Part_triggered()
{
DialogRP* loadPartDialog = new DialogRP();
loadPartDialog->show();
connect(loadPartDialog, SIGNAL(sendAddr2(QString)), this, SLOT(receiveAddr2(QString)));
}
void MainWindow::on_actionFind_Replace_triggered()
{
//DialogSP* savePartDialog = new DialogSP();
//savePartDialog->show();
SearchDialog* searchDialog = new SearchDialog(hexEdit);
searchDialog->show();
}
void MainWindow::ch341StatusFlashing()
{
if (statusCH341 == 0)
{
ui->eStatus->setText("Connected");
ui->eStatus -> setStyleSheet("QLineEdit {border: 2px solid gray;border-radius: 5px;color:#000;background:#9f0;font-weight:600;border-style:inset;}");
}
else
{
ui->eStatus->setText("Not connected");
ui->eStatus -> setStyleSheet("QLineEdit {border: 2px solid gray;border-radius: 5px;color:#fff;background:#f00;font-weight:600;border-style:inset;}");
}
}