Skip to main content
Graduate II
December 18, 2025
Solved

Using UID_BASE to obtain the STM32 96-bit UID

  • December 18, 2025
  • 3 replies
  • 124 views

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);

 

    This topic has been closed for replies.
    Best answer by ######

    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

    Super User
    December 18, 2025

    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.

    Technical Moderator
    December 18, 2025

    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

    ######AuthorAnswer
    Graduate II
    December 18, 2025

    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.