Skip to main content
######
Senior II
Dec 18, 2025 12:20 PM UTC
Solved

Using UID_BASE to obtain the STM32 96-bit UID

Originally a comment on the How to obtain and use the STM32 96-bit UID Knowledge Base article;

Moved for better discussion


Hello,

@MCU Support TD  Can I query the following please (correct me if i'm wrong): 

"If you are not using HAL, then the CMSIS device header provides a definition for the address of the UID. Even without HAL, you have a uniform way to obtain the UID."

uint32_t uid[3];

uid[0] = *(uint32_t *)UID_BASE;
uid[1] = *(uint32_t *)(UID_BASE + 4);
uid[2] = *(uint32_t *)(UID_BASE + 8);

 I believe this references the 0x04 and 0x08 offset from a reference manual, however this is an offset in bytes, and uid in this example is a 32bit variable.

In this example are you not then offsetting by 4 times as much each time and actually missing most of the unique ID? 

I believe the actual code should offset by 1 32 bit object, i.e:

uint32_t uid[3];

uid[0] = *(uint32_t *)UID_BASE;
uid[1] = *(uint32_t *)(UID_BASE + 1);
uid[2] = *(uint32_t *)(UID_BASE + 2);

 

######
######Best answer
Senior II
Dec 18, 2025 12:46 PM UTC

Apologies, the original article is correct.

I thought I'd found an error with this during testing, but realised that the implementation suggested in the knowledge article hadn't been followed in my code. i.e. with brackets not replicated as per the knowledge example.

3 replies

Andrew Neil
Super User
Dec 18, 2025 12:42 PM UTC

The calculation (UID_BASE + x) is done as normal integer arithmetic; it is a byte address.

the result of the calculation is them cast to a pointer to a 32-bit value.

So it is correct, as shown.

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Peter BENSCH
ST Technical Moderator
Dec 18, 2025 12:44 PM UTC

The notation +4 and +8 for the second and third values is actually correct, as addresses in the address space are always counted in bytes. The qualifier (uint32_t*) only specifies that the address of this byte (or rather the group of bytes) is interpreted as a pointer with 32 bits.

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.
######
######AuthorBest answer
Senior II
Dec 18, 2025 12:46 PM UTC

Apologies, the original article is correct.

I thought I'd found an error with this during testing, but realised that the implementation suggested in the knowledge article hadn't been followed in my code. i.e. with brackets not replicated as per the knowledge example.