STM32F4学习笔记读取芯片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);
}

原创链接:https://www.jianshu.com/p/79a1bbe6786f

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谢谢~谢先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值