2024-08-27 01:59 AM
Hello Everyone,
I am working on stm32h723zg, and I want to obtaind the serial of the micro controller for the unique id:
uint32_t unique_id[3];
void read_serial_number(void) {
// Unique ID registers
unique_id[0] = HAL_GetUIDw0(); // Unique ID Register 1
unique_id[1] = HAL_GetUIDw1(); // Unique ID Register 2
unique_id[2] = HAL_GetUIDw2();
}
How can I combine this id to obtain the proper serial number
2024-08-27 02:10 AM
Welcome @Osability, to the community!
The Unique ID is a globally unique number consisting of 96 bits. As STM32 are based on Cortex-M, which in turn work on 32 bits, the individual 32-bit segments are read and can then be combined to form a 96-bit number. You can use this as a serial number.
Unfortunately, it is not possible to derive a (unique) serial number with a different length from these 96 bits, so in such a case you would have to define your own serial number and store it in the flash.
Hope that helps?
Regards
/Peter
2024-08-27 02:13 AM
Please see the Posting Tips for how to properly post source code:
it should look like this:
uint32_t unique_id[3];
void read_serial_number(void) {
// Unique ID registers
unique_id[0] = HAL_GetUIDw0(); // Unique ID Register 1
unique_id[1] = HAL_GetUIDw1(); // Unique ID Register 2
unique_id[2] = HAL_GetUIDw2();
}
@Osability wrote:How can I combine this id to obtain the proper serial number
What do you mean by, "proper serial number" ?
That code does give you the full 96 bits of the Unique ID in your unique_id array - what more do you want?
2024-08-27 03:00 AM
If you the microcontroller to a computer an open stm32cubeprogrammer , the serial number of the microcontroller shows, and this same number can be obtain using some python code. So I am asking if the serial number is a combination of the unique id of the microcontroller.
2024-08-27 04:23 AM - edited 2024-08-27 07:13 AM
Do the hex digits look to match?
There's a different algorithm used for USB Serial Number, which I think creates a 64-bit 16-digit ASCII hex number. Look at the USB Device examples for that code to generate that descriptor.
2024-08-27 05:40 AM
The Serial Number in STM32CubeProgrammer is the serial number of the ST-Link chip, not the chip you are programming.
2024-08-27 06:12 AM
I have a custom board board ,not a development board, and I am not using a ST-Link. I am programming through dfu.
2024-08-27 07:16 AM
2024-08-27 07:32 AM - edited 2024-08-27 07:46 AM
That code appears to ignore DEVICE_ID1 - it only uses DEVICE_ID0 and DEVICE_ID2.
Correction: all three are used - see later replies.
@Peter BENSCH said, "it is not possible to derive a (unique) serial number with a different length from these 96 bits" - so that code's not going to give a (guaranteed) unique serial number...
:thinking_face:
2024-08-27 07:38 AM - edited 2024-08-27 07:39 AM
>>That code appears to ignore DEVICE_ID1 - it only uses DEVICE_ID0 and DEVICE_ID2
It mixes it up with the addition
So it gets to 64-bit, but all 96-bit factor in, just not "uniquely", but chance of picking up two units randomly and them clashing is exceedingly low. Really just sufficient to determine the PC has enumerated this device before, and which COM port or Drive Letter was associated with it.