esp32: Add using efuse component
This commit is contained in:
parent
7626145e6d
commit
ed179ade8d
|
@ -53,7 +53,7 @@ else()
|
||||||
"hwcrypto/sha.c")
|
"hwcrypto/sha.c")
|
||||||
set(COMPONENT_ADD_INCLUDEDIRS "include")
|
set(COMPONENT_ADD_INCLUDEDIRS "include")
|
||||||
|
|
||||||
set(COMPONENT_REQUIRES driver tcpip_adapter esp_event)
|
set(COMPONENT_REQUIRES driver tcpip_adapter esp_event efuse)
|
||||||
# driver is a public requirement because esp_sleep.h uses gpio_num_t & touch_pad_t
|
# driver is a public requirement because esp_sleep.h uses gpio_num_t & touch_pad_t
|
||||||
# tcpip_adapter is a public requirement because esp_event.h uses tcpip_adapter types
|
# tcpip_adapter is a public requirement because esp_event.h uses tcpip_adapter types
|
||||||
# app_update is added here because cpu_start.c uses esp_ota_get_app_description() function.
|
# app_update is added here because cpu_start.c uses esp_ota_get_app_description() function.
|
||||||
|
|
|
@ -7,6 +7,9 @@
|
||||||
#if __has_include("esp32/ulp.h")
|
#if __has_include("esp32/ulp.h")
|
||||||
#include "esp32/ulp.h"
|
#include "esp32/ulp.h"
|
||||||
#endif
|
#endif
|
||||||
|
#if __has_include("esp_efuse.h")
|
||||||
|
#include "esp_efuse.h"
|
||||||
|
#endif
|
||||||
#if __has_include("esp_err.h")
|
#if __has_include("esp_err.h")
|
||||||
#include "esp_err.h"
|
#include "esp_err.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -37,6 +37,8 @@
|
||||||
#include "freertos/xtensa_api.h"
|
#include "freertos/xtensa_api.h"
|
||||||
#include "esp_heap_caps.h"
|
#include "esp_heap_caps.h"
|
||||||
#include "esp_system_internal.h"
|
#include "esp_system_internal.h"
|
||||||
|
#include "esp_efuse.h"
|
||||||
|
#include "esp_efuse_table.h"
|
||||||
|
|
||||||
static const char* TAG = "system_api";
|
static const char* TAG = "system_api";
|
||||||
|
|
||||||
|
@ -77,31 +79,17 @@ esp_err_t esp_base_mac_addr_get(uint8_t *mac)
|
||||||
|
|
||||||
esp_err_t esp_efuse_mac_get_custom(uint8_t *mac)
|
esp_err_t esp_efuse_mac_get_custom(uint8_t *mac)
|
||||||
{
|
{
|
||||||
uint32_t mac_low;
|
uint8_t version;
|
||||||
uint32_t mac_high;
|
esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM_VER, &version, 8);
|
||||||
uint8_t efuse_crc;
|
|
||||||
uint8_t calc_crc;
|
|
||||||
|
|
||||||
uint8_t version = REG_READ(EFUSE_BLK3_RDATA5_REG) >> 24;
|
|
||||||
|
|
||||||
if (version != 1) {
|
if (version != 1) {
|
||||||
ESP_LOGE(TAG, "Base MAC address from BLK3 of EFUSE version error, version = %d", version);
|
ESP_LOGE(TAG, "Base MAC address from BLK3 of EFUSE version error, version = %d", version);
|
||||||
return ESP_ERR_INVALID_VERSION;
|
return ESP_ERR_INVALID_VERSION;
|
||||||
}
|
}
|
||||||
|
|
||||||
mac_low = REG_READ(EFUSE_BLK3_RDATA1_REG);
|
uint8_t efuse_crc;
|
||||||
mac_high = REG_READ(EFUSE_BLK3_RDATA0_REG);
|
esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM, mac, 48);
|
||||||
|
esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM_CRC, &efuse_crc, 8);
|
||||||
mac[0] = mac_high >> 8;
|
uint8_t calc_crc = esp_crc8(mac, 6);
|
||||||
mac[1] = mac_high >> 16;
|
|
||||||
mac[2] = mac_high >> 24;
|
|
||||||
mac[3] = mac_low;
|
|
||||||
mac[4] = mac_low >> 8;
|
|
||||||
mac[5] = mac_low >> 16;
|
|
||||||
|
|
||||||
efuse_crc = mac_high;
|
|
||||||
|
|
||||||
calc_crc = esp_crc8(mac, 6);
|
|
||||||
|
|
||||||
if (efuse_crc != calc_crc) {
|
if (efuse_crc != calc_crc) {
|
||||||
ESP_LOGE(TAG, "Base MAC address from BLK3 of EFUSE CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
|
ESP_LOGE(TAG, "Base MAC address from BLK3 of EFUSE CRC error, efuse_crc = 0x%02x; calc_crc = 0x%02x", efuse_crc, calc_crc);
|
||||||
|
@ -112,29 +100,17 @@ esp_err_t esp_efuse_mac_get_custom(uint8_t *mac)
|
||||||
|
|
||||||
esp_err_t esp_efuse_mac_get_default(uint8_t* mac)
|
esp_err_t esp_efuse_mac_get_default(uint8_t* mac)
|
||||||
{
|
{
|
||||||
uint32_t mac_low;
|
|
||||||
uint32_t mac_high;
|
|
||||||
uint8_t efuse_crc;
|
uint8_t efuse_crc;
|
||||||
uint8_t calc_crc;
|
esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY, mac, 48);
|
||||||
|
esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY_CRC, &efuse_crc, 8);
|
||||||
mac_low = REG_READ(EFUSE_BLK0_RDATA1_REG);
|
uint8_t calc_crc = esp_crc8(mac, 6);
|
||||||
mac_high = REG_READ(EFUSE_BLK0_RDATA2_REG);
|
|
||||||
|
|
||||||
mac[0] = mac_high >> 8;
|
|
||||||
mac[1] = mac_high;
|
|
||||||
mac[2] = mac_low >> 24;
|
|
||||||
mac[3] = mac_low >> 16;
|
|
||||||
mac[4] = mac_low >> 8;
|
|
||||||
mac[5] = mac_low;
|
|
||||||
|
|
||||||
efuse_crc = mac_high >> 16;
|
|
||||||
|
|
||||||
calc_crc = esp_crc8(mac, 6);
|
|
||||||
|
|
||||||
if (efuse_crc != calc_crc) {
|
if (efuse_crc != calc_crc) {
|
||||||
// Small range of MAC addresses are accepted even if CRC is invalid.
|
// Small range of MAC addresses are accepted even if CRC is invalid.
|
||||||
// These addresses are reserved for Espressif internal use.
|
// These addresses are reserved for Espressif internal use.
|
||||||
|
uint32_t mac_high = ((uint32_t)mac[0] << 8) | mac[1];
|
||||||
if ((mac_high & 0xFFFF) == 0x18fe) {
|
if ((mac_high & 0xFFFF) == 0x18fe) {
|
||||||
|
uint32_t mac_low = ((uint32_t)mac[2] << 24) | ((uint32_t)mac[3] << 16) | ((uint32_t)mac[4] << 8) | mac[5];
|
||||||
if ((mac_low >= 0x346a85c7) && (mac_low <= 0x346a85f8)) {
|
if ((mac_low >= 0x346a85c7) && (mac_low <= 0x346a85f8)) {
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue