cancel
Showing results for 
Search instead for 
Did you mean: 

Using UID_BASE to obtain the STM32 96-bit UID

######
Senior II

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
######
Senior II

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.

View solution in original post

3 REPLIES 3
Andrew Neil
Super User

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 Employee

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.
######
Senior II

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.