24
24
#define HEADER_ADDRESS_WIDTH 80
25
25
#define HEADER_HEX_WIDTH 340
26
26
27
+ #define START_ADDRESS 0x00000000
28
+
27
29
static void initBufferTable (QTableWidget *bufTable)
28
30
{
29
31
QTableWidgetItem *addressHeaderItem, *hexHeaderItem, *anciiHeaderItem;
@@ -90,12 +92,31 @@ void MainWindow::log(QString logMsg)
90
92
ui->logTextEdit ->insertPlainText (logMsg);
91
93
}
92
94
95
+ void MainWindow::insertBufferRow (quint8 *readBuf, quint32 size, quint32 rowNum,
96
+ quint32 address)
97
+ {
98
+ QString addressString, hexString;
99
+
100
+ ui->bufferTableWidget ->insertRow (rowNum);
101
+
102
+ for (uint32_t i = 0 ; i < size; i++)
103
+ hexString.append (QString ().sprintf (" %02X " , readBuf[i]));
104
+
105
+ addressString.sprintf (" 0x%08X" , address);
106
+
107
+ ui->bufferTableWidget ->setItem (rowNum, HEADER_ADDRESS_COL,
108
+ new QTableWidgetItem (addressString));
109
+ ui->bufferTableWidget ->setItem (rowNum, HEADER_HEX_COL,
110
+ new QTableWidgetItem (hexString));
111
+ ui->bufferTableWidget ->setItem (rowNum, HEADER_ANCII_COL,
112
+ new QTableWidgetItem (" ................" ));
113
+ }
114
+
93
115
void MainWindow::slotFileOpen ()
94
116
{
95
117
qint64 ret;
96
- QString addressString, hexString;
97
118
quint8 readBuf[ROW_DATA_SIZE] = {};
98
- quint32 rowNum = 1 , address = 0 ;
119
+ quint32 rowNum = HEADER_ROW_NUM , address = START_ADDRESS ;
99
120
QString fileName = QFileDialog::getOpenFileName (this , tr (" Open File" ), " ." ,
100
121
tr (" Binary Files (*)" ));
101
122
@@ -110,23 +131,13 @@ void MainWindow::slotFileOpen()
110
131
return ;
111
132
}
112
133
134
+ /* Reset buffer table */
135
+ ui->bufferTableWidget ->setRowCount (HEADER_ROW_NUM);
136
+
113
137
while ((ret = file.read ((char *)readBuf, ROW_DATA_SIZE)) > 0 )
114
138
{
115
- ui->bufferTableWidget ->insertRow (rowNum);
116
-
117
- hexString.clear ();
118
- for (int i = 0 ; i < ret; i++)
119
- hexString.append (QString ().sprintf (" %02X " , readBuf[i]));
120
-
121
- addressString.sprintf (" 0x%08X" , address);
139
+ insertBufferRow (readBuf, ret, rowNum, address);
122
140
address += ret;
123
-
124
- ui->bufferTableWidget ->setItem (rowNum, HEADER_ADDRESS_COL,
125
- new QTableWidgetItem (addressString));
126
- ui->bufferTableWidget ->setItem (rowNum, HEADER_HEX_COL,
127
- new QTableWidgetItem (hexString));
128
- ui->bufferTableWidget ->setItem (rowNum, HEADER_ANCII_COL,
129
- new QTableWidgetItem (" ................" ));
130
141
rowNum++;
131
142
}
132
143
@@ -180,20 +191,43 @@ void MainWindow::slotProgErase()
180
191
QByteArray ba = ui->chipSelectComboBox ->currentText ().toLatin1 ();
181
192
ChipInfo *chipInfo = getChipInfoByName (ba.data ());
182
193
183
- if (prog->eraseChip (0x00000000 , chipInfo->size ))
194
+ if (prog->eraseChip (START_ADDRESS , chipInfo->size ))
184
195
log (tr (" Failed to erase chip\n " ));
185
196
else
186
197
log (tr (" Chip has been erased successfully\n " ));
187
198
}
188
199
189
200
void MainWindow::slotProgRead ()
190
201
{
191
- uint8_t buf[2048 ];
202
+ uint32_t rowNum = HEADER_ROW_NUM, address = START_ADDRESS;
203
+ QByteArray ba = ui->chipSelectComboBox ->currentText ().toLatin1 ();
204
+ ChipInfo *chipInfo = getChipInfoByName (ba.data ());
205
+ std::unique_ptr< uint8_t [] > buf = std::unique_ptr< uint8_t [] >
206
+ (new (std::nothrow) uint8_t [chipInfo->size ]);
207
+
208
+ if (!buf.get ())
209
+ {
210
+ qCritical () << " Failed to allocate momory for read buffer" ;
211
+ return ;
212
+ }
213
+
214
+ /* Reset buffer table */
215
+ ui->bufferTableWidget ->setRowCount (HEADER_ROW_NUM);
192
216
193
- if (prog->readChip (buf, 0x00000000 , sizeof (buf)))
217
+ if (prog->readChip (buf.get (), START_ADDRESS, chipInfo->size ))
218
+ {
194
219
log (tr (" Failed to read chip\n " ));
220
+ return ;
221
+ }
195
222
else
196
223
log (tr (" Data has been successfully read\n " ));
224
+
225
+ for (uint32_t i = 0 ; i < chipInfo->size ; i += ROW_DATA_SIZE)
226
+ {
227
+ insertBufferRow (buf.get () + i, ROW_DATA_SIZE, rowNum, address);
228
+ rowNum++;
229
+ address += ROW_DATA_SIZE;
230
+ }
197
231
}
198
232
199
233
void MainWindow::slotProgWrite ()
@@ -234,7 +268,7 @@ void MainWindow::slotProgWrite()
234
268
}
235
269
}
236
270
237
- if (prog->writeChip (buf.get (), 0x00000000 , bufIter))
271
+ if (prog->writeChip (buf.get (), START_ADDRESS , bufIter))
238
272
log (tr (" Failed to write chip\n " ));
239
273
else
240
274
log (tr (" Data has been successfully written\n " ));
0 commit comments