ESP8266 SDK 获取CHIP_ID
在实际的项目当中,我们有的时候需要一个唯一的芯片ID,使得同一份固件可以得到不同的ID,之前用过nodemcu的固件,里面有一个函数node.chipId(),这个函数可以用来获取某一个ESP8266的ID,但是RTOS_3.2版本中我并未找到相关的函数可以调用,就打算自己编写一个加入SDK当中去。接下是代码和方法。
一、添加定义把这个获取ID放到esp_system.h中最为合适:先找到\components\esp8266\include目录下的esp_system.h,添加一下下面的函数定义,传入的参数是一个32位整形指针,用于存放芯片ID
/**
* @brief Get the chip id .
*
* @param chip_id the chiip id .length 32 bit.
*
* @return ESP_OK on success
*/
esp_err_t get_chip_id(uint32_t* chip_id);
二、添加实现
在\components\esp8266\source目录下找到system_api.c 然后添加以下代码:
esp_err_t get_chip_id(uint32_t* chip_id){
esp_err_t status = ESP_OK;
*chip_id = (REG_READ(0x3FF00050) & 0xFF000000) |
(REG_READ(0x3ff0005C) & 0xFFFFFF);
return status;
}
三、测试
#include
#include "esp_system.h"
/******************************************************************************
* FunctionName : app_main
* Description : entry of user application, init user function here
* Parameters : none
* Returns : none
*******************************************************************************/
void app_main(void)
{
uint32_t id ;
get_chip_id(&id);
printf("SDK version:%s,chip id:%u\n", esp_get_idf_version(),id);
}
三、总结
测试了两块,一块是2865516771,一块是3045330318,是不一样的,不过这个是否唯一还不好说,只能说重复率很低,上述的这种获取方法是和mac地址有关的。 搞出来的过程有点点复杂,先是找了nodemcu的源码,看了源码后又去看了ESP8266_RTOS的源码,最后终于搞出来了。