From 5553fbd537e60f1f6a413d434fba0406caee35b8 Mon Sep 17 00:00:00 2001 From: XiaXiaotian Date: Wed, 19 Apr 2017 21:00:00 +0800 Subject: [PATCH] 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 | 32 +++++++++++++++------- components/esp32/include/esp_system.h | 28 +++++++++++++++++++- components/esp32/system_api.c | 38 +++++++++++++++++++++++++-- 3 files changed, 86 insertions(+), 12 deletions(-) diff --git a/components/esp32/Kconfig b/components/esp32/Kconfig index a8470c2c92..fbf51bb856 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 - 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 BASE_MAC_ADDRESS_STORAGE + prompt "Storage of the base MAC address" + default BASE_MAC_STORED_DEFAULT_EFUSE + help + 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 9a88743b80..24eebb8b0e 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 f43e2251a0..ab271063e6 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: -- 2.40.0