STM32F103学习笔记(8)——读取芯片UID和MAC地址

一、简介

在嵌入式设备开发过程中有时会需要为设备设置唯一的ID用以标识设备唯一,比如要求同一总线上的所有设备ID不能重复,要求设备具体唯一的MAC地址等等。每个STM32微控制器都自带一个96位的唯一ID,这个ID在任何情况下都是唯一且不允许修改的,这96位的ID可以以字节(8位)为单位读取,也可以以半字(16位)或全字(32位)读取。不同型号的STM32芯片首地址不同,UID首地址也不同

在ST的相关资料中,对其功能的描述有3各方面:

  • 用作序列号(例如 USB 字符串序列号或其它终端应用程序)
  • 在对内部 Flash 进行编程前将唯一 ID 与软件加密原语和协议结合使用时用作安全密钥以提高 Flash 中代码的安全性
  • 激活安全自举过程等

由上图可知,在STM32F1xx的数据手册中关于UID的描述有(从0x1FFFF7E8地址开始的12个字节96bit)

在不同系列的MCU中地址是有差别的,如下图:

二、获取芯片UID

uint32_t GetUid(uint8_t* pUid)
{
    
    
	uint32_t chipId[3] = {
    
    0};
		
	//获取CPU唯一ID
	#if 0//STM32F1系列
	chipId[0] =*(volatile unsigned long *)(0x1ffff7e8); //按全字(32位)读取
	chipId[1] =*(volatile unsigned long *)(0x1ffff7ec);
	chipId[2] =*(volatile unsigned long *)(0x1ffff7f0);
	#endif
	
	#if 1//STM32F4系列
	chipId[0]=*(volatile unsigned long *)(0x1fff7a10);
	chipId[1]=*(volatile unsigned long *)(0x1fff7a14);
	chipId[2]=*(volatile unsigned long *)(0x1fff7a18);
//	/* printf the chipid */
//	printf("\r\n芯片的唯一ID为: %X-%X-%X\r\n",
//	            chipId[0],chipId[1],chipId[2]);
//	printf("\r\n芯片flash的容量为: %dK \r\n", *(uint16_t *)(0X1FFF7a22));
	#endif
	
	//按字节(8位)读取
	pUid[0] = (uint8_t)(chipId[0] & 0x000000FF);
	pUid[1] = (uint8_t)((chipId[0] & 0xFF00) >>8);
	pUid[2] = (uint8_t)((chipId[0] & 0xFF0000) >>16);
	pUid[3] = (uint8_t)((chipId[0] & 0xFF000000) >>24);
	
	pUid[4] = (uint8_t)(chipId[1] & 0xFF);
	pUid[5] = (uint8_t)((chipId[1] & 0xFF00) >>8);
	pUid[6] = (uint8_t)((chipId[1] & 0xFF0000) >>16);
	pUid[7] = (uint8_t)((chipId[1] & 0xFF000000) >>24);
	
	pUid[8] = (uint8_t)(chipId[2] & 0xFF);
	pUid[9] = (uint8_t)((chipId[2] & 0xFF00) >>8);
	pUid[10] = (uint8_t)((chipId[2] & 0xFF0000) >>16);
	pUid[11] = (uint8_t)((chipId[2] & 0xFF000000) >>24);

	return (chipId[0]>>1)+(chipId[1]>>2)+(chipId[2]>>3);
}
uint8_t uid[12] = {
    
    0};
GetUid(uid);
for(uint8_t i = 0; i < 12; i++)
{
    
    
    printf("%02x", uid[i]);
}

三、获取MAC地址

/**
 @brief 获取MAC地址
 @param pMac - [out] MAC地址
 @return 无
*/
void GetMacAddress(uint8_t *pMac)
{
    
    
    uint32_t uid = 0;
    uint8_t chipId[15] = {
    
    0};
    int i = 0;

    mcuId = GetChipId(chipId);

    for(i = 0; i < 12; i++)         // 获取ID[12]
    {
    
    
        chipId[12] += chipId[i];	
    }
    for(i=0; i<12; i++)	            // 获取ID[13]
    {
    
    
        chipId[13] ^= chipId[i];	
    }

    pMac[0] = (uint8_t)(uid & 0xF0);
    pMac[1] = (uint8_t)((uid & 0xFF00) >> 8);
    pMac[2] = (uint8_t)((uid & 0xFF0000) >> 16);
    pMac[3] = (uint8_t)((uid & 0xFF000000) >> 24);
    pMac[4] = chipId[12];
    pMac[5] = chipId[13];  
}
uint8_t mac[6] = {
    
    0};
GetMacAddress(mac);
for(uint8_t i = 0; i < 6; i++)
{
    
    
    printf("%02x", mac[i]);
}

虽然这个96位的ID是唯一的,但是MAC地址却只有48位,因为量产有不同批次,而且采购的很随机的话这个ID号也是不唯一的,比较靠谱一点的还是自己在指定FLASH位置定义一个变量,这样程序就写死去读这个地方的值,而这个地方的值我们再用别的方式去修改,如自己写个上位机用串口通信设置等

MAC地址的前12bit固定,后面的便可以直接如此自定义设置。

/**
 @brief 获取MAC地址
 @param pMac - [out] MAC地址
 @return 无
*/
void GetMacAddress(uint8_t *pMac)
{
    
    
    pMac[0] = 0x11;    
    pMac[1] = 0x22; 
    pMac[2] = *(volatile uint8_t *)(0X800F000); 
    pMac[3] = *(volatile uint8_t *)(0X800F001);
    pMac[4] = *(volatile uint8_t *)(0X800F002);
    pMac[5] = *(volatile uint8_t *)(0X800F003);
}

使用了正点原子的脱机下载器,注释掉该定义变量,并在该处设置滚码。


• 由 Leung 写于 2022 年 7 月 29 日

• 参考:STM32 进阶教程 9 - 芯片维一码(UID)读取
    如何获取STM32 MCU的唯一ID
    读取STM32芯片的唯一ID和MAC地址
    stm32设置唯一MAC地址

猜你喜欢

转载自blog.csdn.net/qq_36347513/article/details/126061779