Merge branch 'feature/support_read_mac_addr_from_customer_efuse' into 'master'

Add customer MAC address that read from efuse



See merge request !673
This commit is contained in:
Jiang Jiang Jian 2017-04-24 11:30:55 +08:00
commit 97142bb8db
3 changed files with 112 additions and 6 deletions

View file

@ -153,16 +153,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.
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"

View file

@ -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.

View file

@ -36,16 +36,48 @@
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;
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);
#ifdef CONFIG_BASE_MAC_STORED_DEFAULT_EFUSE
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 +87,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_BASE_MAC_STORED_DEFAULT_EFUSE
calc_crc = esp_crc8(mac, 6);
if (efuse_crc != calc_crc) {
@ -114,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: