HAL读取STM32 96位全球唯一ID(UID)

Reference: Device ID in STM32 microcontrollers

HAL

以SMT32F103C8T6为例, stm32f1xx_hal.c中给出了一个读取ID的函数:

/**
  * @brief Return the unique device identifier (UID based on 96 bits)
  * @param UID: pointer to 3 words array.
  * @retval Device identifier
  */
void HAL_GetUID(uint32_t *UID)
{
  UID[0] = (uint32_t)(READ_REG(*((uint32_t *)UID_BASE)));
  UID[1] = (uint32_t)(READ_REG(*((uint32_t *)(UID_BASE + 4U))));
  UID[2] = (uint32_t)(READ_REG(*((uint32_t *)(UID_BASE + 8U))));
}

UID地址

UID_BASE的定义在stm32f103xb.h:

#define UID_BASE              0x1FFFF7E8U    /*!< Unique device ID register base address */

这个基地址不同系列的STM32是不同的:

Device line Starting address
F0, F3 0x1FFFF7AC
F1 0x1FFFF7E8
F2, F4 0x1FFF7A10
F7 0x1FF0F420
L0 0x1FF80050
L0, L1 Cat.1,Cat.2 0x1FF80050
L1 Cat.3,Cat.4,Cat.5,Cat.6 0x1FF800D0

可以直接从地址读:

unsigned long *id = (unsigned long *)0x1FFFF7E8;
id[0]
id[1]
id[2]

HAL和直接取UID地址的值结果比较

结果是一样的:

    uint32_t uid[3];
    HAL_GetUID(uid);
    //printf("0x");
    for(int8_t i = 0; i < 3; i++) {
        printf("%x \r\n", uid[i]);
    }   
    printf("\r\n---------------\r\n");  

    uint32_t *id = (uint32_t *)0x1FFFF7E8;
    for(int8_t i = 0; i < 3; i++) {
        printf("%x \r\n", id[i]);
    }   
    printf("\r\n"); 

output:

66dff57 
57528448 
87231810 

---------------
66dff57 
57528448 
87231810 

猜你喜欢

转载自blog.csdn.net/weifengdq/article/details/79530706