Skip to content

Commit e9b429a

Browse files
committed
Chip DB moved to external CSV file
1 parent 8ac7459 commit e9b429a

File tree

8 files changed

+202
-79
lines changed

8 files changed

+202
-79
lines changed

qt/chip_db.cpp

Lines changed: 102 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,125 @@
55

66
#include "chip_db.h"
77
#include <cstring>
8+
#include <QFile>
9+
#include <QDebug>
10+
#include <QStandardPaths>
11+
#include <QDir>
12+
#include <QMessageBox>
813

9-
static ChipInfo chipDB[] =
10-
{
11-
/* id, name, pageSize, blockSize, size, tCS, tCLS, tALS, tCLR, tAR, tWP, tRP, tDS, tCH, tCLH, tALH, tWC, tRC, tREA */
12-
{ CHIP_ID_NONE, "No Chip", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
13-
{ CHIP_ID_K9F2G08U0C, "K9F2G08U0C", 0x800, 0x20000, 0x10000000, 20, 12, 12, 10, 10, 12, 12, 12, 5, 5, 5, 25, 25, 20 },
14-
};
14+
#define CHIP_DB_FILE_NAME "nando_chip_db.csv"
1515

16-
uint32_t chipDbGet(ChipInfo *&db)
16+
QString ChipDb::findFile()
1717
{
18-
db = chipDB;
18+
QString fileName = CHIP_DB_FILE_NAME;
19+
20+
if (!QFileInfo(fileName).exists() &&
21+
(fileName = QStandardPaths::locate(QStandardPaths::ConfigLocation,
22+
CHIP_DB_FILE_NAME)).isNull())
23+
{
24+
QMessageBox::critical(nullptr, tr("Error"), tr("Chip DB file %1 was not"
25+
" found in %2;%3").arg(CHIP_DB_FILE_NAME).arg(QDir::currentPath()).
26+
arg(QStandardPaths::standardLocations(QStandardPaths::
27+
ConfigLocation).join(';')));
28+
return QString();
29+
}
1930

20-
return CHIP_ID_LAST;
31+
return fileName;
2132
}
2233

23-
ChipInfo *chipInfoGetByName(char *name)
34+
int ChipDb::stringToChipInfo(const QString &file, const QString &s,
35+
ChipInfo &ci)
2436
{
25-
for (int id = 0; id < CHIP_ID_LAST; id++)
37+
int paramNum;
38+
QStringList paramsList;
39+
40+
paramsList = s.split(',');
41+
paramNum = paramsList.size();
42+
if (paramNum != CHIP_PARAM_NUM)
2643
{
27-
if (!strcmp(name, chipDB[id].name))
28-
return &chipDB[id];
44+
QMessageBox::critical(nullptr, tr("Error"),
45+
tr("Failed to read chip DB entry from %1. Expected %2 parameters, "
46+
"but read %3").arg(file).arg(CHIP_PARAM_NUM).arg(paramNum));
47+
return -1;
48+
}
49+
50+
ci.name = paramsList[CHIP_PARAM_NAME];
51+
for (int i = CHIP_PARAM_NAME + 1; i < CHIP_PARAM_NUM; i++)
52+
{
53+
bool ok;
54+
55+
ci.params[i] = paramsList[i].toUInt(&ok);
56+
if (!ok)
57+
{
58+
QMessageBox::critical(nullptr, tr("Error"), tr("Failed to parse"
59+
" parameter %1 in %2").arg(paramsList[i]).arg(file));
60+
return -1;
61+
}
2962
}
3063

3164
return 0;
3265
}
3366

34-
ChipInfo *chipInfoGetById(uint32_t id)
67+
void ChipDb::readFromCvs(void)
3568
{
36-
if (id == CHIP_ID_NONE || id >= CHIP_ID_LAST)
37-
return NULL;
69+
ChipInfo chipInfo;
70+
QFile dbFile;
71+
QString fileName = findFile();
72+
73+
if (fileName.isNull())
74+
return;
75+
76+
dbFile.setFileName(fileName);
77+
if (!dbFile.open(QIODevice::ReadOnly | QIODevice::Text))
78+
{
79+
QMessageBox::critical(nullptr, tr("Error"), tr("Failed to open chip DB "
80+
"file: %1, error: %2").arg(fileName).arg(dbFile.errorString()));
81+
return;
82+
}
83+
84+
QTextStream in(&dbFile);
85+
while (!in.atEnd())
86+
{
87+
QString line = in.readLine();
88+
if (line.isEmpty())
89+
continue;
90+
if (*line.data() == '#')
91+
continue;
92+
if (stringToChipInfo(fileName, line, chipInfo))
93+
return;
94+
chipInfoVector.append(chipInfo);
95+
}
96+
}
97+
98+
ChipDb::ChipDb(QObject *parent) : QObject(parent)
99+
{
100+
readFromCvs();
101+
}
102+
103+
QStringList *ChipDb::getNames()
104+
{
105+
QStringList *namesList = new QStringList;
106+
107+
for (int i = 0; i < chipInfoVector.size(); i++)
108+
namesList->append(chipInfoVector[i].name);
109+
110+
return namesList;
111+
}
112+
113+
ChipInfo *ChipDb::chipInfoGetByName(const QString &name)
114+
{
115+
for (int i = 0; i < chipInfoVector.size(); i++)
116+
{
117+
if (!chipInfoVector[i].name.compare(name))
118+
return &chipInfoVector[i];
119+
}
38120

39-
return &chipDB[id];
121+
return nullptr;
40122
}
41123

42-
uint32_t chipPageSizeGet(uint32_t id)
124+
uint32_t ChipDb::pageSizeGetByName(const QString &name)
43125
{
44-
ChipInfo *info = chipInfoGetById(id);
126+
ChipInfo *info = chipInfoGetByName(name);
45127

46-
return info ? info->pageSize : 0;
128+
return info ? info->params[CHIP_PARAM_PAGE_SIZE] : 0;
47129
}

qt/chip_db.h

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
#define CHIP_DB_H
88

99
#include <cstdint>
10-
11-
#define MAX_CHIP_NAME_LEN 16
10+
#include <QString>
11+
#include <QObject>
12+
#include <QVector>
1213

1314
enum
1415
{
@@ -17,33 +18,54 @@ enum
1718
CHIP_ID_LAST = 2,
1819
};
1920

21+
enum
22+
{
23+
CHIP_PARAM_NAME,
24+
CHIP_PARAM_PAGE_SIZE,
25+
CHIP_PARAM_BLOCK_SIZE,
26+
CHIP_PARAM_SIZE,
27+
CHIP_PARAM_T_CS,
28+
CHIP_PARAM_T_CLS,
29+
CHIP_PARAM_T_ALS,
30+
CHIP_PARAM_T_CLR,
31+
CHIP_PARAM_T_AR,
32+
CHIP_PARAM_T_WP,
33+
CHIP_PARAM_T_RP,
34+
CHIP_PARAM_T_DS,
35+
CHIP_PARAM_T_CH,
36+
CHIP_PARAM_T_CLH,
37+
CHIP_PARAM_T_ALH,
38+
CHIP_PARAM_T_WC,
39+
CHIP_PARAM_T_RC,
40+
CHIP_PARAM_T_REA,
41+
CHIP_PARAM_NUM,
42+
};
43+
2044
typedef struct
2145
{
2246
uint32_t id;
23-
char name[MAX_CHIP_NAME_LEN];
24-
uint32_t pageSize;
25-
uint32_t blockSize;
26-
uint32_t size;
27-
uint32_t tCS;
28-
uint32_t tCLS;
29-
uint32_t tALS;
30-
uint32_t tCLR;
31-
uint32_t tAR;
32-
uint32_t tWP;
33-
uint32_t tRP;
34-
uint32_t tDS;
35-
uint32_t tCH;
36-
uint32_t tCLH;
37-
uint32_t tALH;
38-
uint32_t tWC;
39-
uint32_t tRC;
40-
uint32_t tREA;
47+
QString name;
48+
uint32_t params[CHIP_PARAM_NUM];
4149
} ChipInfo;
4250

43-
uint32_t chipDbGet(ChipInfo *&db);
44-
ChipInfo *chipInfoGetByName(char *name);
45-
ChipInfo *chipInfoGetById(uint32_t id);
46-
uint32_t chipPageSizeGet(uint32_t id);
51+
class ChipDb : public QObject
52+
{
53+
Q_OBJECT
54+
55+
QVector<ChipInfo> chipInfoVector;
56+
57+
QString findFile();
58+
int stringToChipInfo(const QString &file, const QString &s, ChipInfo &ci);
59+
void readFromCvs(void);
60+
61+
public:
62+
explicit ChipDb(QObject *parent = 0);
63+
QStringList *getNames();
64+
ChipInfo *chipInfoGetByName(const QString &name);
65+
uint32_t pageSizeGetByName(const QString &name);
66+
};
67+
68+
4769

4870
#endif // CHIP_DB_H
4971

qt/main_window.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,6 @@
2121

2222
#define START_ADDRESS 0x00000000
2323

24-
static void addChipDB(QComboBox *chipSelectComboBox)
25-
{
26-
ChipInfo *db;
27-
uint32_t size = chipDbGet(db);
28-
29-
for (uint32_t i = 0; i < size; i++)
30-
chipSelectComboBox->addItem(db[i].name);
31-
}
32-
3324
void MainWindow::initBufTable()
3425
{
3526
buffer = nullptr;
@@ -64,7 +55,10 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
6455

6556
prog = new Programmer(this);
6657

67-
addChipDB(ui->chipSelectComboBox);
58+
QStringList *chipNames = chipDb.getNames();
59+
ui->chipSelectComboBox->addItems(*chipNames);
60+
free(chipNames);
61+
6862
connect(ui->chipSelectComboBox, SIGNAL(currentIndexChanged(int)),
6963
this, SLOT(slotSelectChip(int)));
7064

@@ -258,19 +252,19 @@ void MainWindow::slotProgEraseCompleted(int status)
258252
void MainWindow::slotProgErase()
259253
{
260254
QByteArray ba = ui->chipSelectComboBox->currentText().toLatin1();
261-
ChipInfo *chipInfo = chipInfoGetByName(ba.data());
255+
ChipInfo *chipInfo = chipDb.chipInfoGetByName(ba.data());
262256

263257
connect(prog, SIGNAL(eraseChipCompleted(int)), this,
264258
SLOT(slotProgEraseCompleted(int)));
265259

266-
prog->eraseChip(START_ADDRESS, chipInfo->size);
260+
prog->eraseChip(START_ADDRESS, chipInfo->params[CHIP_PARAM_BLOCK_SIZE]);
267261
}
268262

269263
void MainWindow::slotProgReadCompleted(int status)
270264
{
271265
QByteArray ba = ui->chipSelectComboBox->currentText().toLatin1();
272-
ChipInfo *chipInfo = chipInfoGetByName(ba.data());
273-
uint32_t readSize = chipInfo->size;
266+
ChipInfo *chipInfo = chipDb.chipInfoGetByName(ba.data());
267+
uint32_t readSize = chipInfo->params[CHIP_PARAM_BLOCK_SIZE];
274268

275269
disconnect(prog, SIGNAL(readChipCompleted(int)), this,
276270
SLOT(slotProgReadCompleted(int)));
@@ -288,8 +282,8 @@ void MainWindow::slotProgReadCompleted(int status)
288282
void MainWindow::slotProgRead()
289283
{
290284
QByteArray ba = ui->chipSelectComboBox->currentText().toLatin1();
291-
ChipInfo *chipInfo = chipInfoGetByName(ba.data());
292-
uint32_t readSize = chipInfo->size;
285+
ChipInfo *chipInfo = chipDb.chipInfoGetByName(ba.data());
286+
uint32_t readSize = chipInfo->params[CHIP_PARAM_BLOCK_SIZE];
293287

294288
connect(prog, SIGNAL(readChipCompleted(int)), this,
295289
SLOT(slotProgReadCompleted(int)));
@@ -316,6 +310,7 @@ void MainWindow::slotProgWriteCompleted(int status)
316310

317311
void MainWindow::slotProgWrite()
318312
{
313+
QString name;
319314
uint32_t pageSize;
320315

321316
if (!bufferSize)
@@ -330,7 +325,8 @@ void MainWindow::slotProgWrite()
330325
return;
331326
}
332327

333-
if (!(pageSize = chipPageSizeGet(selectedChipNum)))
328+
name = ui->chipSelectComboBox->currentText();
329+
if (!(pageSize = chipDb.pageSizeGetByName(name)))
334330
{
335331
qInfo() << "Chip page size is unknown";
336332
return;
@@ -375,6 +371,9 @@ void MainWindow::slotProgSelectCompleted(int status)
375371

376372
void MainWindow::slotSelectChip(int selectedChipNum)
377373
{
374+
QString name;
375+
ChipInfo *chipInfo;
376+
378377
this->selectedChipNum = selectedChipNum;
379378

380379
if (selectedChipNum == CHIP_ID_NONE)
@@ -386,7 +385,9 @@ void MainWindow::slotSelectChip(int selectedChipNum)
386385
connect(prog, SIGNAL(confChipCompleted(int)), this,
387386
SLOT(slotProgSelectCompleted(int)));
388387

389-
prog->confChip(chipInfoGetById(selectedChipNum));
388+
name = ui->chipSelectComboBox->currentText();
389+
chipInfo = chipDb.chipInfoGetByName(name);
390+
prog->confChip(chipInfo);
390391
}
391392

392393
void MainWindow::slotSettingsProgrammer()

qt/main_window.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class MainWindow : public QMainWindow
3030
BufferTableModel bufferTableModel;
3131
uint32_t selectedChipNum;
3232
ChipId chipId;
33+
ChipDb chipDb;
3334

3435
void initBufTable();
3536
void resetBufTable();

qt/nando_chip_db.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# name, pageSize, blockSize, size, tCS, tCLS, tALS, tCLR, tAR, tWP, tRP, tDS, tCH, tCLH, tALH, tWC, tRC, tREA
2+
NONE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
3+
K9F2G08U0C, 2048, 131072, 268435456, 20, 12, 12, 10, 10, 12, 12, 12, 5, 5, 5, 25, 25, 20

qt/programmer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ void Programmer::confChip(ChipInfo *chipInfo)
228228
chipInfoToStmParams(chipInfo, &params);
229229

230230
confCmd.cmd = cmd;
231-
confCmd.pageSize = chipInfo->pageSize;
232-
confCmd.blockSize = chipInfo->blockSize;
233-
confCmd.size = chipInfo->size;
231+
confCmd.pageSize = chipInfo->params[CHIP_PARAM_PAGE_SIZE];
232+
confCmd.blockSize = chipInfo->params[CHIP_PARAM_BLOCK_SIZE];
233+
confCmd.size = chipInfo->params[CHIP_PARAM_SIZE];
234234
confCmd.setupTime = params.setupTime;
235235
confCmd.waitSetupTime = params.waitSetupTime;
236236
confCmd.holdSetupTime = params.holdSetupTime;

qt/qt.pro

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,11 @@ FORMS += main_window.ui \
4949
settings_programmer_dialog.ui
5050

5151
QMAKE_CXXFLAGS += -std=c++11 -Wextra -Werror
52+
53+
DISTFILES += \
54+
nando_chip_db.csv
55+
56+
install_conf.path = $$DESTDIR
57+
install_conf.files += $$PWD/nando_chip_db.csv
58+
59+
INSTALLS += install_conf

0 commit comments

Comments
 (0)