-
-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
provide unique USB serial number #86
Conversation
inspired by erikkaashoek/tinySA#85 create a 12 char string based on the Arduino algorithm https://github.com/limbongofficial/STM32_Core-Arduino/blob/master/cores/arduino/stm32/usb/usbd_desc.c#L326-L370 Signed-off-by: Martin <[email protected]>
Most good solution generate Serial number on ask inside USB driver (not need ~huge buffer in RAM) or use any function for convert digit to different radix (in your solution used radix = 16 for hex) |
usb serial string ID is here:
after host ask this string and deivce process it in:
dindex = 3 is ask serial string, you can use lcd_spi buffer (end) for generate serial and send |
Signed-off-by: Martin <[email protected]>
Good hint, please check my latest commit. |
I add d5377c3 |
Line 207 looks strange, because uint16_t *buf = ((uint16_t *)&spi_buffer[sizeof(spi_buffer)]) - USB_SERIAL_STRING_SIZE - 4; // 16 byte align And more strange - the compiler warns, but only when I set
So IMHO line 207 should look like this: uint16_t *buf = ((uint16_t *)&spi_buffer[SPI_BUFFER_SIZE]) - USB_SERIAL_STRING_SIZE - 4; // 16 byte align or uint16_t *buf = ((uint16_t *)&spi_buffer[ sizeof(spi_buffer) / sizeof(pixel_t) ]) - USB_SERIAL_STRING_SIZE - 4; // 16 byte align |
In the next lines you use two times uint32_t id0 = *(uint32_t *)0x1FFFF7AC; // MCU id0 address
uint32_t id1 = *(uint32_t *)0x1FFFF7B0; // MCU id1 address
uint32_t id2 = *(uint32_t *)0x1FFFF7B4; // MCU id2 address
uint64_t uid = id1;
id0+= id2;
uid = id1 | (uid<<32); // generate unique 64bit ID |
Thanks, fixed 9f35ce5 |
About LTO, yes I see before strange NanoVNA H freeze (but not H4), after long time I enable this and run my H ~2-3 hours and no freeze, so I hope all good Link Time Optimization (LTO) gives GCC the capability of dumping its internal representation (GIMPLE) to disk, so that all the different compilation units that make up a single executable can be optimized as a single module. This expands the scope of inter-procedural optimizations to encompass the whole program (or, rather, everything that is visible at link time). |
Last question, why uint64_t uid = id1;
id0+= id2;
uid|= id0 | (uid<<32); // generate unique 64bit ID better, then id1 and id0+id2 do not interfere. uint64_t uid = id1;
uid = (id0 + id2) | (uid<<32); // generate unique 64bit ID I close this PR here because it's solved now and mostly done. |
With LTO it makes still problems with my -H (HW ver 3.4 w/o SD card). It freezes few minutes to ~1 hour after switching on (with or without any activity). Tested with arm gcc 8.2.1 and 9.2.1. Without LTO a random freeze can happen after one or two day idling at a stupid 5V power supply without any USB activities. This is known, see the issue #26. |
And another small improvement, the radix5 encoding requires 64/5 = 12.8 (i.e. 13) char, your solution cuts the last 4 bits. My change adds the missing bits and aligns correctly for all string sizes: --- a/usbcfg.c
+++ b/usbcfg.c
@@ -201,17 +201,16 @@ static const USBDescriptor vcom_strings[] = {
#ifdef __USB_UID__
// Use unique serial string generated from MCU id
#define UID_RADIX 5 // Radix conversion constant (5 bit, use 0..9 and A..V)
-#define USB_SERIAL_STRING_SIZE (64 / UID_RADIX) // Result string size
+#define USB_SERIAL_STRING_SIZE ((64 + UID_RADIX -1) / UID_RADIX) // Result string size
USBDescriptor *getSerialStringDescriptor(void) {
uint16_t i;
- uint16_t *buf = ((uint16_t *)&spi_buffer[ARRAY_COUNT(spi_buffer)]) - USB_SERIAL_STRING_SIZE - 4; // 16 byte align
+ uint16_t *buf = ((uint16_t *)&spi_buffer[ARRAY_COUNT(spi_buffer)]) - ((USB_SERIAL_STRING_SIZE + 3) & ~3); // 32 bit align
USBDescriptor *d = ((USBDescriptor *)buf) - 1;
uint32_t id0 = *(uint32_t *)0x1FFFF7AC; // MCU id0 address
uint32_t id1 = *(uint32_t *)0x1FFFF7B0; // MCU id1 address
uint32_t id2 = *(uint32_t *)0x1FFFF7B4; // MCU id2 address
uint64_t uid = id1;
- id0+= id2;
- uid|= id0 | (uid<<32); // generate unique 64bit ID
+ uid = (id0 + id2) | (uid<<32); // generate unique 64bit ID
// Prepare serial string descriptor from 64 bit ID
for(i = 1; i < USB_SERIAL_STRING_SIZE + 1; i++) {
uint16_t c = uid & ((1<<UID_RADIX) - 1); |
@DiSlord What is your opinion on my last suggestion? I will then also adjust my PR for the tinySA accordingly. |
inspired by erikkaashoek/tinySA#85 create a 12 char string based on the Arduino algorithm https://github.com/limbongofficial/STM32_Core-Arduino/blob/master/cores/arduino/stm32/usb/usbd_desc.c#L326-L370