Skip to content

Commit c1c1eed

Browse files
committed
Implemented firmware version command
1 parent 0c7ae75 commit c1c1eed

File tree

8 files changed

+116
-12
lines changed

8 files changed

+116
-12
lines changed

firmware/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
int main()
1616
{
1717
uart_init();
18-
printf("\r\nNAND programmer ver: "SW_VERSION"\r\n");
18+
printf("\r\nNAND programmer ver: %d.%d.%d\r\n", SW_VERSION_MAJOR,
19+
SW_VERSION_MINOR, SW_VERSION_BUILD);
1920

2021
printf("JTAG init...");
2122
jtag_init();

firmware/nand_programmer.c

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "chip_info.h"
1010
#include "led.h"
1111
#include "log.h"
12+
#include "version.h"
1213
#include <stdio.h>
1314
#include <string.h>
1415
#include <stddef.h>
@@ -31,7 +32,8 @@ typedef enum
3132
NP_CMD_NAND_WRITE_E = 0x05,
3233
NP_CMD_NAND_CONF = 0x06,
3334
NP_CMD_NAND_READ_BB = 0x07,
34-
NP_CMD_NAND_LAST = 0x08,
35+
NP_CMD_VERSION_GET = 0x08,
36+
NP_CMD_NAND_LAST = 0x09,
3537
} np_cmd_code_t;
3638

3739
enum
@@ -160,6 +162,19 @@ typedef struct __attribute__((__packed__))
160162
uint8_t err_code;
161163
} np_resp_err_t;
162164

165+
typedef struct __attribute__((__packed__))
166+
{
167+
uint8_t major;
168+
uint8_t minor;
169+
uint16_t build;
170+
} version_t;
171+
172+
typedef struct __attribute__((__packed__))
173+
{
174+
np_resp_t header;
175+
version_t version;
176+
} np_resp_version_t;
177+
163178
typedef struct
164179
{
165180
uint32_t addr;
@@ -908,6 +923,25 @@ int np_cmd_read_bad_blocks(np_prog_t *prog)
908923
return np_send_ok_status();
909924
}
910925

926+
int np_cmd_version_get(np_prog_t *prog)
927+
{
928+
np_resp_version_t resp;
929+
size_t resp_len = sizeof(resp);
930+
931+
DEBUG_PRINT("Read version command\r\n");
932+
933+
resp.header.code = NP_RESP_DATA;
934+
resp.header.info = resp_len - sizeof(resp.header);
935+
resp.version.major = SW_VERSION_MAJOR;
936+
resp.version.minor = SW_VERSION_MINOR;
937+
resp.version.build = SW_VERSION_BUILD;
938+
939+
if (np_comm_cb)
940+
np_comm_cb->send((uint8_t *)&resp, resp_len);
941+
942+
return 0;
943+
}
944+
911945
static np_cmd_handler_t cmd_handler[] =
912946
{
913947
{ NP_CMD_NAND_READ_ID, np_cmd_nand_read_id },
@@ -918,6 +952,7 @@ static np_cmd_handler_t cmd_handler[] =
918952
{ NP_CMD_NAND_WRITE_E, np_cmd_nand_write },
919953
{ NP_CMD_NAND_CONF, np_cmd_nand_conf },
920954
{ NP_CMD_NAND_READ_BB, np_cmd_read_bad_blocks },
955+
{ NP_CMD_VERSION_GET, np_cmd_version_get },
921956
};
922957

923958
static bool np_cmd_is_valid(np_cmd_code_t code)
@@ -929,7 +964,8 @@ static int np_cmd_handler(np_prog_t *prog)
929964
{
930965
np_cmd_t *cmd = (np_cmd_t *)prog->rx_buf;
931966

932-
if (!prog->chip_is_conf && cmd->code != NP_CMD_NAND_CONF)
967+
if (!prog->chip_is_conf && cmd->code != NP_CMD_NAND_CONF &&
968+
cmd->code != NP_CMD_VERSION_GET)
933969
{
934970
ERROR_PRINT("Chip is not configured\r\n");
935971
return NP_ERR_CHIP_NOT_CONF;

firmware/version.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef _VERSION_H_
22
#define _VERSION_H_
33

4-
#define SW_VERSION "2.2.0"
4+
#define SW_VERSION_MAJOR 2
5+
#define SW_VERSION_MINOR 2
6+
#define SW_VERSION_BUILD 0
57

68
#endif

qt/cmd.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ enum
1313
CMD_NAND_WRITE_E = 0x05,
1414
CMD_NAND_CONF = 0x06,
1515
CMD_NAND_READ_BB = 0x07,
16+
CMD_VERSION_GET = 0x08,
1617
};
1718

1819
typedef struct __attribute__((__packed__))
@@ -90,7 +91,12 @@ typedef enum
9091
STATUS_BB_SKIP = 0x04,
9192
} StatusData;
9293

93-
94+
typedef struct __attribute__((__packed__))
95+
{
96+
uint8_t major;
97+
uint8_t minor;
98+
uint16_t build;
99+
} FwVersion;
94100

95101
typedef struct __attribute__((__packed__))
96102
{
@@ -107,6 +113,12 @@ typedef struct __attribute__((__packed__))
107113
uint8_t data[];
108114
} RespHeader;
109115

116+
typedef struct __attribute__((__packed__))
117+
{
118+
RespHeader header;
119+
FwVersion version;
120+
} RespVersion;
121+
110122
typedef struct __attribute__((__packed__))
111123
{
112124
RespHeader header;

qt/main_window.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,28 @@ void MainWindow::setUiStateSelected(bool isSelected)
194194
ui->actionReadBadBlocks->setEnabled(isSelected);
195195
}
196196

197+
void MainWindow::slotProgConnectCompleted(int status)
198+
{
199+
disconnect(prog, SIGNAL(connectCompleted(int)), this,
200+
SLOT(slotProgConnectCompleted(int)));
201+
202+
if (status)
203+
return;
204+
205+
qInfo() << "Connected to programmer";
206+
setUiStateConnected(true);
207+
ui->actionConnect->setText(tr("Disconnect"));
208+
}
209+
197210
void MainWindow::slotProgConnect()
198211
{
199212
if (!prog->isConnected())
200213
{
201214
if (!prog->connect())
202-
qInfo() << "Connected to programmer";
203-
else
204-
return;
205-
206-
setUiStateConnected(true);
207-
ui->actionConnect->setText(tr("Disconnect"));
215+
{
216+
connect(prog, SIGNAL(connectCompleted(int)), this,
217+
SLOT(slotProgConnectCompleted(int)));
218+
}
208219
}
209220
else
210221
{

qt/main_window.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class MainWindow : public QMainWindow
3838
void updateChipList();
3939

4040
private slots:
41+
void slotProgConnectCompleted(int status);
4142
void slotProgReadDeviceIdCompleted(int status);
4243
void slotProgReadCompleted(int status);
4344
void slotProgWriteCompleted(int status);

qt/programmer.cpp

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,43 @@ void Programmer::serialPortDisconnect()
5656
serialPort.close();
5757
}
5858

59+
void Programmer::connectCb(int ret)
60+
{
61+
QObject::disconnect(&reader, SIGNAL(result(int)), this,
62+
SLOT(connectCb(int)));
63+
64+
if (ret < 0)
65+
{
66+
qCritical() << "Failed to read firmware version";
67+
return;
68+
}
69+
70+
emit connectCompleted(ret);
71+
72+
isConn = true;
73+
qInfo() << "Firmware version: " <<
74+
fwVersionToString(fwVersion).toLatin1().data();
75+
}
76+
5977
int Programmer::connect()
6078
{
79+
Cmd cmd = { .code = CMD_VERSION_GET };
80+
6181
if (serialPortConnect())
6282
return -1;
6383

64-
isConn = true;
84+
QObject::connect(&reader, SIGNAL(result(int)), this,
85+
SLOT(connectCb(int)));
86+
87+
/* Serial port object cannot be used in other thread */
88+
serialPortDisconnect();
89+
writeData.clear();
90+
writeData.append(reinterpret_cast<const char *>(&cmd), sizeof(cmd));
91+
reader.init(usbDevName, SERIAL_PORT_SPEED,
92+
reinterpret_cast<uint8_t *>(&fwVersion), sizeof(fwVersion),
93+
reinterpret_cast<const uint8_t *>(writeData.constData()),
94+
static_cast<uint32_t>(writeData.size()), false, false);
95+
reader.start();
6596

6697
return 0;
6798
}
@@ -283,4 +314,10 @@ void Programmer::logCb(QtMsgType msgType, QString msg)
283314
}
284315
}
285316

317+
QString Programmer::fwVersionToString(FwVersion fwVersion)
318+
{
319+
return QString("%1.%2.%3").arg(fwVersion.major).
320+
arg(fwVersion.minor).arg(fwVersion.build);
321+
}
322+
286323

qt/programmer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Programmer : public QObject
2727
Reader reader;
2828
bool isConn;
2929
bool skipBB;
30+
FwVersion fwVersion;
3031

3132
int serialPortConnect();
3233
void serialPortDisconnect();
@@ -50,8 +51,10 @@ class Programmer : public QObject
5051
uint32_t pageSize);
5152
void readChipBadBlocks();
5253
void confChip(ChipInfo *chipInfo);
54+
QString fwVersionToString(FwVersion fwVersion);
5355

5456
signals:
57+
void connectCompleted(int ret);
5558
void readChipIdCompleted(int ret);
5659
void writeChipCompleted(int ret);
5760
void readChipCompleted(int ret);
@@ -67,6 +70,7 @@ private slots:
6770
void readChipBadBlocksCb(int ret);
6871
void confChipCb(int ret);
6972
void logCb(QtMsgType msgType, QString msg);
73+
void connectCb(int ret);
7074
};
7175

7276
#endif // PROGRAMMER_H

0 commit comments

Comments
 (0)