cancel
Showing results for 
Search instead for 
Did you mean: 

Unique Id stm32H7

Osability
Associate II

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

11 REPLIES 11
Peter BENSCH
ST Employee

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

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Please see the Posting Tips for how to properly post source code:

https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

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?

 

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.

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

The Serial Number in STM32CubeProgrammer is the serial number of the ST-Link chip, not the chip you are programming.

 

If you feel a post has answered your question, please click "Accept as Solution".

I have a custom  board board ,not a development board, and I am not using a ST-Link. I am programming through dfu.

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:

>>That code appears to ignore DEVICE_ID1 - it only uses DEVICE_ID0 and DEVICE_ID2

It mixes it up with the addition

https://github.com/STMicroelectronics/STM32CubeH7/blob/master/Projects/STM32H743I-EVAL/Applications/USB_Device/DFU_Standalone/Src/usbd_desc.c#L248

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..