一翼闹、簡介
在嵌入式設(shè)備開發(fā)過程中有時會需要為設(shè)備設(shè)置唯一的ID用以標(biāo)識設(shè)備唯一,比如要求同一總線上的所有設(shè)備ID不能重復(fù)蒋纬,要求設(shè)備具體唯一的MAC地址等等猎荠。每個STM32微控制器都自帶一個96位
的唯一ID,這個ID在任何情況下都是唯一且不允許修改
的蜀备,這96位的ID可以以字節(jié)(8位)為單位讀取关摇,也可以以半字(16位)或全字(32位)讀取。不同型號的STM32芯片首地址不同碾阁,UID首地址也不同
输虱。
在ST的相關(guān)資料中,對其功能的描述有3各方面:
- 用作序列號(例如 USB 字符串序列號或其它終端應(yīng)用程序)
- 在對內(nèi)部 Flash 進(jìn)行編程前將唯一 ID 與軟件加密原語和協(xié)議結(jié)合使用時用作安全密鑰以提高 Flash 中代碼的安全性
- 激活安全自舉過程等
由上圖可知瓷蛙,在STM32F1xx的數(shù)據(jù)手冊中關(guān)于UID的描述有(從0x1FFFF7E8
地址開始的12個字節(jié)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
//按字節(jié)(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位,因?yàn)榱慨a(chǎn)有不同批次冠桃,而且采購的很隨機(jī)的話這個ID號也是不唯一的命贴,比較靠譜一點(diǎn)的還是自己在指定FLASH位置定義一個變量,這樣程序就寫死去讀這個地方的值食听,而這個地方的值我們再用別的方式去修改胸蛛,如自己寫個上位機(jī)用串口通信設(shè)置等
。
MAC地址的前12bit固定樱报,后面的便可以直接如此自定義設(shè)置葬项。
/**
@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);
}
使用了正點(diǎn)原子的脫機(jī)下載器,注釋掉該定義變量迹蛤,并在該處設(shè)置滾碼民珍。
? 由 Leung 寫于 2022 年 7 月 29 日
? 參考:STM32 進(jìn)階教程 9 - 芯片維一碼(UID)讀取
如何獲取STM32 MCU的唯一ID
讀取STM32芯片的唯一ID和MAC地址
stm32設(shè)置唯一MAC地址