From 03e2618d3597516fc3c9287814567e78fee6824c Mon Sep 17 00:00:00 2001 From: XiaXiaotian Date: Mon, 17 Apr 2017 21:16:16 +0800 Subject: [PATCH 1/2] Add customer MAC address that read from efuse --- components/esp32/Kconfig | 7 +++++++ components/esp32/system_api.c | 29 +++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/components/esp32/Kconfig b/components/esp32/Kconfig index 0db76f3d5..a8470c2c9 100644 --- a/components/esp32/Kconfig +++ b/components/esp32/Kconfig @@ -105,6 +105,13 @@ config MEMMAP_SPISRAM main memory map. Enable this if you have this hardware and want to use it in the same way as on-chip RAM. +config CUSTOMER_MAC_ADDRESS + bool "Customer MAC address" + default n + help + Customers can define their own mac address in efuse. + Set to 'y' if you decide to use the mac address that you defined in efuse. + choice NUMBER_OF_MAC_ADDRESS_GENERATED_FROM_EFUSE bool "Number of MAC address generated from the hardware MAC address in efuse" default FOUR_MAC_ADDRESS_FROM_EFUSE diff --git a/components/esp32/system_api.c b/components/esp32/system_api.c index 028513718..f43e2251a 100644 --- a/components/esp32/system_api.c +++ b/components/esp32/system_api.c @@ -42,10 +42,14 @@ void system_init() esp_err_t esp_efuse_read_mac(uint8_t* mac) { + uint32_t mac_low; + uint32_t mac_high; uint8_t efuse_crc; uint8_t calc_crc; - uint32_t mac_low = REG_READ(EFUSE_BLK0_RDATA1_REG); - uint32_t mac_high = REG_READ(EFUSE_BLK0_RDATA2_REG); + +#ifndef CONFIG_CUSTOMER_MAC_ADDRESS + mac_low = REG_READ(EFUSE_BLK0_RDATA1_REG); + mac_high = REG_READ(EFUSE_BLK0_RDATA2_REG); mac[0] = mac_high >> 8; mac[1] = mac_high; @@ -55,6 +59,27 @@ esp_err_t esp_efuse_read_mac(uint8_t* mac) mac[5] = mac_low; efuse_crc = mac_high >> 16; +#else + uint8_t version = REG_READ(EFUSE_BLK3_RDATA5_REG) >> 24; + + if (version != 1) { + ESP_LOGE(TAG, "Customer efuse MAC address version error, version = %d", version); + abort(); + } + + mac_low = REG_READ(EFUSE_BLK3_RDATA1_REG); + mac_high = REG_READ(EFUSE_BLK3_RDATA0_REG); + + mac[0] = mac_high >> 8; + 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; +#endif //CONFIG_CUSTOMER_DEFINED_MAC_ADDR + calc_crc = esp_crc8(mac, 6); if (efuse_crc != calc_crc) { From 5553fbd537e60f1f6a413d434fba0406caee35b8 Mon Sep 17 00:00:00 2001 From: XiaXiaotian Date: Wed, 19 Apr 2017 21:00:00 +0800 Subject: [PATCH 2/2] add base MAC address storage choice. Base MAC address can be stored in default manufacture-defined or customer pre-defined place in EFUSE and other place e.g. flash or EEPROM. If choose to use base MAC address which is stored in other place, please call esp_base_mac_addr_set_external() before initializing WiFi/BT/Ehternet. --- components/esp32/Kconfig | 30 +++++++++++++++------ components/esp32/include/esp_system.h | 28 +++++++++++++++++++- components/esp32/system_api.c | 38 +++++++++++++++++++++++++-- 3 files changed, 85 insertions(+), 11 deletions(-) diff --git a/components/esp32/Kconfig b/components/esp32/Kconfig index a8470c2c9..fbf51bb85 100644 --- a/components/esp32/Kconfig +++ b/components/esp32/Kconfig @@ -105,23 +105,37 @@ config MEMMAP_SPISRAM main memory map. Enable this if you have this hardware and want to use it in the same way as on-chip RAM. -config CUSTOMER_MAC_ADDRESS - bool "Customer MAC address" - default n +choice BASE_MAC_ADDRESS_STORAGE + prompt "Storage of the base MAC address" + default BASE_MAC_STORED_DEFAULT_EFUSE help - Customers can define their own mac address in efuse. - Set to 'y' if you decide to use the mac address that you defined in efuse. + Select storage of the base MAC address which is used for all network interfaces when networking is initialized. + If "Default place in EFUSE" is selected, esp32 will use the base MAC address which is written into default + place in EFUSE when the chip is manufactured. + If "Customer-defined place in EFUSE" is selected, ESP32 will use customer-defined base MAC address which + is written into EFUSE Block 3 words 0, 1. + If "Other customer-defined place" is selected, esp32 will use customer-defined base MAC address from other + place(flash, EEPROM, etc). User code must call esp_base_mac_addr_set_external to set the base MAC address + before network features are initialised. + +config BASE_MAC_STORED_DEFAULT_EFUSE + bool "Default place in EFUSE" +config BASE_MAC_STORED_CUSTOMER_DEFINED_EFUSE + bool "Customer-defined place in EFUSE" +config BASE_MAC_STORED_OTHER_CUSTOMER_DEFINED_PLACE + bool "Other customer-defined place" +endchoice choice NUMBER_OF_MAC_ADDRESS_GENERATED_FROM_EFUSE bool "Number of MAC address generated from the hardware MAC address in efuse" default FOUR_MAC_ADDRESS_FROM_EFUSE help - Config the number of MAC address which is generated from the hardware MAC address in efuse. + Config the number of MAC address which is generated from the base MAC address in efuse. If the number is two, the MAC addresses of WiFi station and bluetooth are generated from - the hardware MAC address in efuse. The MAC addresses of WiFi softap and ethernet are derived + the base MAC address in efuse. The MAC addresses of WiFi softap and ethernet are derived from that of WiFi station and bluetooth respectively. If the number is four, the MAC addresses of WiFi station, WiFi softap, bluetooth and ethernet - are all generated from the hardware MAC address in efuse. + are all generated from the base MAC address in efuse. config TWO_MAC_ADDRESS_FROM_EFUSE bool "Two" diff --git a/components/esp32/include/esp_system.h b/components/esp32/include/esp_system.h index 9a88743b8..24eebb8b0 100644 --- a/components/esp32/include/esp_system.h +++ b/components/esp32/include/esp_system.h @@ -103,7 +103,33 @@ uint32_t system_get_free_heap_size(void) __attribute__ ((deprecated)); uint32_t esp_random(void); /** - * @brief Read hardware MAC address. + * @brief Set base MAC address from external storage e.g. flash and EEPROM. + * + * Base MAC address is used to generate the MAC addresses used by the networking interfaces. + * If using base MAC address stored in external storage, call this API to set base MAC + * address from external storage before initializing WiFi/BT/Ethernet. + * + * @param mac base MAC address, length: 6 bytes. + * + * @return ESP_OK on success + */ +esp_err_t esp_base_mac_addr_set_external(uint8_t *mac); + +/** + * @brief Return base MAC address set using esp_mac_addr_set_external. + * + * @param mac base MAC address, length: 6 bytes. + * + * Base MAC address is used to generate the MAC addresses used by the networking interfaces. + * If using base MAC address stored in external storage, call this API to set base MAC + * address from external storage before initializing WiFi/BT/Ethernet. + * + * @return ESP_OK on success + */ +esp_err_t esp_base_mac_addr_get_external(uint8_t *mac); + +/** + * @brief Read hardware MAC address from efuse. * * In WiFi MAC, only ESP32 station MAC is the hardware MAC, ESP32 softAP MAC is a software MAC * calculated from ESP32 station MAC. diff --git a/components/esp32/system_api.c b/components/esp32/system_api.c index f43e2251a..ab271063e 100644 --- a/components/esp32/system_api.c +++ b/components/esp32/system_api.c @@ -36,10 +36,38 @@ static const char* TAG = "system_api"; +static uint8_t ext_base_mac_addr[6] = {0}; + void system_init() { } +esp_err_t esp_base_mac_addr_set_external(uint8_t *mac) +{ + if (mac == NULL) { + ESP_LOGE(TAG, "External base MAC address is NULL"); + abort(); + } + + memcpy(ext_base_mac_addr, mac, 6); + + return ESP_OK; +} + +esp_err_t esp_base_mac_addr_get_external(uint8_t *mac) +{ + uint8_t null_mac[6] = {0}; + + if (memcmp(ext_base_mac_addr, null_mac, 6) == 0) { + ESP_LOGE(TAG, "External MAC address is not set"); + abort(); + } + + memcpy(mac, ext_base_mac_addr, 6); + + return ESP_OK; +} + esp_err_t esp_efuse_read_mac(uint8_t* mac) { uint32_t mac_low; @@ -47,7 +75,7 @@ esp_err_t esp_efuse_read_mac(uint8_t* mac) uint8_t efuse_crc; uint8_t calc_crc; -#ifndef CONFIG_CUSTOMER_MAC_ADDRESS +#ifdef CONFIG_BASE_MAC_STORED_DEFAULT_EFUSE mac_low = REG_READ(EFUSE_BLK0_RDATA1_REG); mac_high = REG_READ(EFUSE_BLK0_RDATA2_REG); @@ -78,7 +106,7 @@ esp_err_t esp_efuse_read_mac(uint8_t* mac) mac[5] = mac_low >> 16; efuse_crc = mac_high; -#endif //CONFIG_CUSTOMER_DEFINED_MAC_ADDR +#endif //CONFIG_BASE_MAC_STORED_DEFAULT_EFUSE calc_crc = esp_crc8(mac, 6); @@ -139,7 +167,13 @@ esp_err_t esp_read_mac(uint8_t* mac, esp_mac_type_t type) || NUM_MAC_ADDRESS_FROM_EFUSE == TWO_MAC_ADDRESS_FROM_EFUSE, \ "incorrect NUM_MAC_ADDRESS_FROM_EFUSE value"); +#if defined(CONFIG_BASE_MAC_STORED_DEFAULT_EFUSE) || defined(CONFIG_BASE_MAC_STORED_CUSTOMER_DEFINED_EFUSE) esp_efuse_read_mac(efuse_mac); +#endif + +#if defined(CONFIG_BASE_MAC_STORED_OTHER_CUSTOMER_DEFINED_PLACE) + esp_base_mac_addr_get_external(efuse_mac); +#endif switch (type) { case ESP_MAC_WIFI_STA: