2025-12-18 4:20 AM - last edited on 2025-12-18 4:43 AM by Andrew Neil
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);
Solved! Go to Solution.
2025-12-18 4:46 AM
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.
2025-12-18 4:42 AM - edited 2025-12-18 4:42 AM
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.
2025-12-18 4:44 AM
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
2025-12-18 4:46 AM
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.