STM32L0 read the unique serial number UID
Direct addressing
The UID first address of STM32L0 is 0x1FF80050, and the UID of 96 bits in 3 words can be read in the following way.
#define UID_BASE 0x1FF80050
uint32_t UID[3];
UID[0] = (uint32_t)(*((uint32_t *)UID_BASE));
UID[1] = (uint32_t)(*((uint32_t *)(UID_BASE + 4)));
UID[2] = (uint32_t)(*((uint32_t *)(UID_BASE + 8)));
The test found that two different STM32L031 chips, the UID read is the same! !
HAL library read
Use the library function of HAL to read UID directly to read. The HAL library supports the following reading:
uint32_t HAL_GetHalVersion(void);
uint32_t HAL_GetREVID(void);
uint32_t HAL_GetDEVID(void);
uint32_t HAL_GetUIDw0(void);
uint32_t HAL_GetUIDw1(void);
uint32_t HAL_GetUIDw2(void);
Therefore, the reading code is as follows:
uint32_t UID[3];
UID[0] = HAL_GetUIDw0();
UID[1] = HAL_GetUIDw1();
UID[2] = HAL_GetUIDw2();
The test found that two different STM32L031 chips, the UID read is different! !
Find the reason
Analyzing HAL_GetUIDw0(), HAL_GetUIDw1(), HAL_GetUIDw2(), it is found that:
uint32_t HAL_GetUIDw0(void)
{
return(READ_REG(*((uint32_t *)UID_BASE)));
}
uint32_t HAL_GetUIDw1(void)
{
return(READ_REG(*((uint32_t *)(UID_BASE + 0x04U))));
}
uint32_t HAL_GetUIDw2(void)
{
return(READ_REG(*((uint32_t *)(UID_BASE + 0x14U))));
}
The problem is found, the offset address of the third word is 14, not 8. Therefore, the direct reading method is changed to:
UID[0] = (uint32_t)(*((uint32_t *)UID_BASE));
UID[1] = (uint32_t)(*((uint32_t *)(UID_BASE + 4)));
UID[2] = (uint32_t)(*((uint32_t *)(UID_BASE + 14)));
Then the read UID is correct.
-End-