用jsp怎么做的购物网站,wordpress站点设置使用期限,微信h5,佛山市住房和城乡建设部网站一、简介
在嵌入式设备开发过程中有时会需要为设备设置唯一的ID用以标识设备唯一#xff0c;比如要求同一总线上的所有设备ID不能重复#xff0c;要求设备具体唯一的MAC地址等等。每个STM32微控制器都自带一个96位的唯一ID#xff0c;这个ID在任何情况下都是唯一且不允许修…一、简介
在嵌入式设备开发过程中有时会需要为设备设置唯一的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(i0; i12; 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