Skip to content

Commit 7cea59a

Browse files
authored
Merge pull request #51 from r2axz/r2axz-add-device-version
Add Device Version Reporting
2 parents 4e8df8f + 6e63211 commit 7cea59a

File tree

5 files changed

+68
-4
lines changed

5 files changed

+68
-4
lines changed

Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ ifneq ($(FIRMWARE_ORIGIN),)
4444
LDFLAGS += -Wl,-section-start=.isr_vector=$(FIRMWARE_ORIGIN)
4545
endif
4646

47+
GIT_VERSION := $(subst ., ,$(subst v,,$(shell git describe --abbrev=0 --tags 2>/dev/null || true)))
48+
49+
ifneq ($(GIT_VERSION),)
50+
GIT_VERSION_MAJOR := $(word 1, $(GIT_VERSION))
51+
ifneq ($(GIT_VERSION_MAJOR),)
52+
CFLAGS += -DDEVICE_VERSION_MAJOR=$(GIT_VERSION_MAJOR)
53+
endif
54+
GIT_VERSION_MINOR := $(word 2, $(GIT_VERSION))
55+
ifneq ($(GIT_VERSION_MINOR),)
56+
CFLAGS += -DDEVICE_VERSION_MINOR=$(GIT_VERSION_MINOR)
57+
endif
58+
GIT_VERSION_REVISION := $(word 3, $(GIT_VERSION))
59+
ifneq ($(GIT_VERSION_REVISION),)
60+
CFLAGS += -DDEVICE_VERSION_REVISION=$(GIT_VERSION_REVISION)
61+
endif
62+
endif
63+
4764
.PHONY: all
4865
all: $(TARGET).hex $(TARGET).bin size
4966

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,6 @@ a sequence of configuration shell commands. When _screen_ or another terminal
192192
emulation software is started, its output may contain garbage characters left
193193
due to the above exchange.
194194

195-
196-
197195
## Advanced Configuration
198196

199197
_bluepill-serial-monster_ provides a configuration shell that allows
@@ -339,6 +337,14 @@ To reset the device to the default settings, type:
339337
config reset
340338
```
341339

340+
### Printing the Firmware Version
341+
342+
To print the firmware version, type:
343+
344+
```text
345+
version
346+
```
347+
342348
The default configuration is automatically stored in the flash memory after reset.
343349

344350
## Flashing Firmware

cdc_shell.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "gpio.h"
1212
#include "cdc_config.h"
1313
#include "device_config.h"
14+
#include "version.h"
1415
#include "cdc_shell.h"
1516

1617

@@ -349,6 +350,13 @@ static void cdc_shell_cmd_config(int argc, char *argv[]) {
349350
cdc_shell_write_string(cdc_shell_err_config_missing_arguments);
350351
}
351352

353+
static const char cdc_shell_device_version[] = DEVICE_VERSION_STRING;
354+
355+
static void cdc_shell_cmd_version(int argc, char *argv[]) {
356+
cdc_shell_write_string(cdc_shell_device_version);
357+
cdc_shell_write_string(cdc_shell_new_line);
358+
}
359+
352360
static void cdc_shell_cmd_help(int argc, char *argv[]);
353361

354362
static const cdc_shell_cmd_t cdc_shell_commands[] = {
@@ -379,7 +387,13 @@ static const cdc_shell_cmd_t cdc_shell_commands[] = {
379387
" active\t[low|high]\r\n"
380388
" pull\t\t[floating|up|down]\r\n"
381389
"Example: \"uart 1 tx output od\" sets UART1 TX output type to open-drain\r\n"
382-
"Example: \"uart 3 rts active high dcd active high pull down\" allows to set multiple parameters at once."
390+
"Example: \"uart 3 rts active high dcd active high pull down\" allows to set multiple parameters at once.",
391+
},
392+
{
393+
.cmd = "version",
394+
.handler = cdc_shell_cmd_version,
395+
.description = "print firmware version",
396+
.usage = "Usage: version",
383397
},
384398
{ 0 }
385399
};

usb_descriptors.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "usb_core.h"
88
#include "usb_cdc.h"
9+
#include "version.h"
910
#include "usb_descriptors.h"
1011

1112
#define USB_CONTROL_ENDPOINT_SIZE 16
@@ -95,7 +96,7 @@ const usb_device_descriptor_t usb_device_descriptor = {
9596
.bMaxPacketSize = usb_endpoints[usb_endpoint_address_control].rx_size,
9697
.idVendor = USB_ID_VENDOR,
9798
.idProduct = USB_ID_PRODUCT,
98-
.bcdDevice = USB_BCD_VERSION(1, 0, 0),
99+
.bcdDevice = USB_BCD_VERSION(DEVICE_VERSION_MAJOR, DEVICE_VERSION_MINOR, DEVICE_VERSION_REVISION),
99100
.iManufacturer = usb_string_index_manufacturer,
100101
.iProduct = usb_string_index_product,
101102
.iSerialNumber = usb_string_index_serial,

version.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2022 Kirill Kotyagin
5+
*/
6+
7+
#ifndef VERSION_H
8+
#define VERSION_H
9+
10+
#ifndef DEVICE_VERSION_MAJOR
11+
#define DEVICE_VERSION_MAJOR 0
12+
#endif
13+
14+
#ifndef DEVICE_VERSION_MINOR
15+
#define DEVICE_VERSION_MINOR 0
16+
#endif
17+
18+
#ifndef DEVICE_VERSION_REVISION
19+
#define DEVICE_VERSION_REVISION 0
20+
#endif
21+
22+
#define __DEVICE_VERSION_STRING(major,minor,revision) "v" #major "." #minor "." #revision
23+
#define _DEVICE_VERSION_STRING(major,minor,revision) __DEVICE_VERSION_STRING(major,minor,revision)
24+
#define DEVICE_VERSION_STRING _DEVICE_VERSION_STRING(DEVICE_VERSION_MAJOR,DEVICE_VERSION_MINOR,DEVICE_VERSION_REVISION)
25+
26+
#endif // VERSION_H

0 commit comments

Comments
 (0)