Skip to content

Commit f4e799a

Browse files
committed
qt: changed read command to use async read/write
1 parent c2c07ea commit f4e799a

File tree

4 files changed

+95
-80
lines changed

4 files changed

+95
-80
lines changed

qt/main_window.cpp

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -198,34 +198,43 @@ void MainWindow::slotProgErase()
198198
chipInfo->size);
199199
}
200200

201-
void MainWindow::slotProgRead()
201+
void MainWindow::readChipCb(int status)
202202
{
203203
uint32_t rowNum = HEADER_ROW_NUM, address = START_ADDRESS;
204-
QByteArray ba = ui->chipSelectComboBox->currentText().toLatin1();
205-
ChipInfo *chipInfo = getChipInfoByName(ba.data());
206-
std::unique_ptr< uint8_t[] > buf = std::unique_ptr< uint8_t[] >
207-
(new (std::nothrow) uint8_t[chipInfo->size]);
208204

209-
if (!buf.get())
205+
if (!status)
210206
{
211-
qCritical() << "Failed to allocate momory for read buffer";
212-
return;
207+
qInfo() << "Data has been successfully read";
208+
209+
/* Reset buffer table */
210+
ui->bufferTableWidget->setRowCount(HEADER_ROW_NUM);
211+
212+
for (uint32_t i = 0; i < readBufSize; i += ROW_DATA_SIZE)
213+
{
214+
insertBufferRow(readBuf + i, ROW_DATA_SIZE, rowNum, address);
215+
rowNum++;
216+
address += ROW_DATA_SIZE;
217+
}
213218
}
214219

215-
/* Reset buffer table */
216-
ui->bufferTableWidget->setRowCount(HEADER_ROW_NUM);
220+
delete readBuf;
221+
}
217222

218-
if (prog->readChip(buf.get(), START_ADDRESS, chipInfo->size))
219-
return;
220-
else
221-
qInfo() << "Data has been successfully read";
223+
void MainWindow::slotProgRead()
224+
{
225+
QByteArray ba = ui->chipSelectComboBox->currentText().toLatin1();
226+
ChipInfo *chipInfo = getChipInfoByName(ba.data());
222227

223-
for (uint32_t i = 0; i < chipInfo->size; i += ROW_DATA_SIZE)
228+
readBufSize = chipInfo->size;
229+
readBuf = new (std::nothrow) uint8_t[readBufSize];
230+
if (!readBuf)
224231
{
225-
insertBufferRow(buf.get() + i, ROW_DATA_SIZE, rowNum, address);
226-
rowNum++;
227-
address += ROW_DATA_SIZE;
232+
qCritical() << "Failed to allocate memory for read buffer";
233+
return;
228234
}
235+
236+
prog->readChip(std::bind(&MainWindow::readChipCb, this,
237+
std::placeholders::_1), readBuf, START_ADDRESS, readBufSize);
229238
}
230239

231240
void MainWindow::slotProgWrite()

qt/main_window.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ class MainWindow : public QMainWindow
2424

2525
private:
2626
Ui::MainWindow *ui;
27+
uint8_t *readBuf;
28+
uint32_t readBufSize;
2729

2830
void insertBufferRow(quint8 *readBuf, quint32 size, quint32 rowNum,
2931
quint32 address);
3032
void readChipIdCb(ChipId id);
3133
void selectChipCb();
3234
void eraseChipCb();
35+
void readChipCb(int status);
3336

3437
public slots:
3538
void slotFileOpen();

qt/programmer.cpp

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -296,84 +296,82 @@ void Programmer::eraseChip(std::function<void(void)> callback, uint32_t addr,
296296
this, std::placeholders::_1), &writeData);
297297
}
298298

299-
int Programmer::readChip(uint8_t *buf, uint32_t addr, uint32_t len)
299+
void Programmer::readRespReadChipCb(int status)
300300
{
301-
int ret;
302-
uint8_t rx_buf[CDC_BUF_SIZE];
303-
RespHeader *dataResp;
304-
RespBadBlock badBlock;
301+
uint size;
302+
RespHeader *header;
305303
uint32_t offset = 0;
306-
Cmd cmd = { .code = CMD_NAND_READ };
307-
ReadCmd readCmd = { .cmd = cmd, .addr = addr, .len = len };
308304

309-
if (sendCmd(&readCmd.cmd, sizeof(readCmd)))
310-
return -1;
305+
if (status == SerialPortReader::READ_ERROR)
306+
goto Error;
311307

312-
while (len)
308+
while ((size = readData.size()))
313309
{
314-
serialPort.waitForReadyRead(READ_WRITE_TIMEOUT_MS);
315-
ret = serialPort.read((char *)rx_buf, sizeof(RespHeader));
316-
if (ret < 0)
317-
{
318-
qCritical() << "Failed to read data " << serialPort.error()
319-
<< serialPort.errorString();
320-
return -1;
321-
}
310+
if (readRespHeader(&readData, header))
311+
goto Error;
322312

323-
if (ret < (int)sizeof(RespHeader))
324-
{
325-
qCritical() << "Failed to read response header: data was partially"
326-
" received, length: " << ret;
327-
return -1;
328-
}
329-
330-
dataResp = (RespHeader *)rx_buf;
331-
if (dataResp->code == RESP_STATUS)
313+
switch (header->code)
332314
{
333-
if (dataResp->info == STATUS_BAD_BLOCK)
315+
case RESP_STATUS:
316+
if (header->info == STATUS_OK && header->info == STATUS_BAD_BLOCK)
334317
{
335-
if (readRespBadBlockAddress(&badBlock))
336-
return -1;
337-
qInfo() << "Bad block at" << QString("0x%1").
338-
arg(badBlock.addr, 8, 16, QLatin1Char('0'));
318+
if (handleBadBlock(&readData))
319+
goto Error;
320+
readData.remove(0, sizeof(RespBadBlock));
339321
}
340322
else
341-
return handleStatus(dataResp);
342-
}
343-
344-
if (dataResp->code == RESP_DATA)
345-
{
346-
if (dataResp->info > sizeof(rx_buf) - sizeof(RespHeader))
347-
{
348-
qCritical() << "Programmer returns wrong data length";
349-
return -1;
350-
}
351-
352-
serialPort.waitForReadyRead(READ_WRITE_TIMEOUT_MS);
353-
ret = serialPort.read((char *)dataResp->data, dataResp->info);
354-
if (ret < 0)
355323
{
356-
qCritical() << "Failed to read data " << serialPort.error()
357-
<< serialPort.errorString();
358-
return -1;
324+
qCritical() << "Programmer error: failed to read chip";
325+
goto Error;
359326
}
360-
361-
if (dataResp->info != ret)
327+
break;
328+
case RESP_DATA:
329+
if (header->info > CDC_BUF_SIZE - sizeof(RespHeader) || header->info > size)
362330
{
363-
qCritical() << "Programmer error: expected to receive " <<
364-
dataResp->info << "but received" << ret << "Bytes";
365-
for (int i = 0; i < ret; i++)
366-
qInfo() << dataResp->data[i];
367-
return -1;
331+
qCritical() << "Wrong data length in response header:" << header->info;
332+
goto Error;
368333
}
369-
370-
memcpy(buf + offset, dataResp->data, ret);
371-
offset += ret;
372-
len -= ret;
334+
memcpy(readChipBuf + offset, header->data, header->info);
335+
offset += header->info;
336+
readData.remove(0, sizeof(RespHeader) + header->info);
337+
break;
338+
default:
339+
handleWrongResp(header->code);
340+
goto Error;
373341
}
374342
}
375343

376-
return 0;
344+
if (readChipLen == offset)
345+
readChipCb(0);
346+
else
347+
{
348+
qCritical() << "Data was partialy received, size:" << offset;
349+
goto Error;
350+
}
351+
352+
return;
353+
354+
Error:
355+
readChipCb(-1);
356+
}
357+
358+
void Programmer::readChip(std::function<void(int)> callback, uint8_t *buf,
359+
uint32_t addr, uint32_t len)
360+
{
361+
Cmd cmd = { .code = CMD_NAND_READ };
362+
ReadCmd readCmd = { .cmd = cmd, .addr = addr, .len = len };
363+
364+
readData.clear();
365+
serialPortReader->read(std::bind(&Programmer::readRespReadChipCb, this,
366+
std::placeholders::_1), &readData, READ_TIMEOUT_MS);
367+
368+
readChipCb = callback;
369+
readChipBuf = buf;
370+
readChipLen = len;
371+
writeData.clear();
372+
writeData.append((const char *)&readCmd, sizeof(readCmd));
373+
serialPortWriter->write(std::bind(&Programmer::sendCmdCb,
374+
this, std::placeholders::_1), &writeData);
377375
}
378376

379377
int Programmer::writeChip(uint8_t *buf, uint32_t addr, uint32_t len)

qt/programmer.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ class Programmer : public QObject
119119
std::function<void(ChipId)> readChipIdCb;
120120
std::function<void(void)> selectChipCb;
121121
std::function<void(void)> eraseChipCb;
122+
std::function<void(int)> readChipCb;
123+
uint8_t *readChipBuf;
124+
uint32_t readChipLen;
122125

123126
int sendCmd(Cmd *cmd, size_t size);
124127
void sendCmdCb(int status);
@@ -128,6 +131,7 @@ class Programmer : public QObject
128131
void readRespChipIdCb(int status);
129132
void readRespSelectChipCb(int status);
130133
void readRespEraseChipCb(int status);
134+
void readRespReadChipCb(int status);
131135
int handleStatus(RespHeader *respHead);
132136
int handleWrongResp(uint8_t code);
133137
int handleBadBlock(QByteArray *data);
@@ -145,7 +149,8 @@ class Programmer : public QObject
145149
void readChipId(std::function<void(ChipId)> callback);
146150
void eraseChip(std::function<void(void)> callback, uint32_t addr,
147151
uint32_t len);
148-
int readChip(uint8_t *buf, uint32_t addr, uint32_t len);
152+
void readChip(std::function<void(int)> callback, uint8_t *buf,
153+
uint32_t addr, uint32_t len);
149154
int writeChip(uint8_t *buf, uint32_t addr, uint32_t len);
150155
void selectChip(std::function<void(void)> callback, uint32_t chipNum);
151156
};

0 commit comments

Comments
 (0)