From 23f933a78d66b92507e53d9dca4b6424c5d1a849 Mon Sep 17 00:00:00 2001 From: XiaXiaotian Date: Wed, 1 Mar 2017 20:42:46 +0800 Subject: [PATCH] mac address: add user set mac address add menuconfig for user to set mac address of wifi, bt and ethernet. --- components/bt/Kconfig | 9 ++++++ components/esp32/Kconfig | 18 +++++++++++ components/esp32/include/esp_system.h | 20 +++++++++++++ components/esp32/lib | 2 +- components/esp32/system_api.c | 43 +++++++++++++++++++++++++++ components/ethernet/Kconfig | 9 ++++++ components/ethernet/emac_main.c | 3 +- 7 files changed, 101 insertions(+), 3 deletions(-) diff --git a/components/bt/Kconfig b/components/bt/Kconfig index 63dce8d1f9..e4a72654fa 100644 --- a/components/bt/Kconfig +++ b/components/bt/Kconfig @@ -3,6 +3,15 @@ menuconfig BT_ENABLED help Select this option to enable Bluetooth stack and show the submenu with Bluetooth configuration choices. +config ESP_MAC_OFFSET_BT_ + int "MAC address offset value of WiFi station" + depends on BT_ENABLED + range 0 255 + default 2 + help + The offset value of bluetooth MAC address from the MAC address which is read from efuse. + This offset value must be different from that of WiFi softap, WiFi station and ethernet. + config BTC_TASK_STACK_SIZE int "Bluetooth event (callback to application) task stack size" depends on BT_ENABLED diff --git a/components/esp32/Kconfig b/components/esp32/Kconfig index 8aa0ed3b5f..361ddccd52 100644 --- a/components/esp32/Kconfig +++ b/components/esp32/Kconfig @@ -481,6 +481,24 @@ menuconfig WIFI_ENABLED help Select this option to enable WiFi stack and show the submenu with WiFi configuration choices. +config ESP_MAC_OFFSET_WIFI_STA + int "MAC address offset value of WiFi station" + depends on WIFI_ENABLED + range 0 255 + default 0 + help + The offset value of WiFi station MAC address from the MAC address which is read from efuse. + This offset value must be different from that of WiFi softap, bluetooth and ethernet. + +config ESP_MAC_OFFSET_WIFI_SOFTAP + int "MAC address offset value of WiFi softap" + depends on WIFI_ENABLED + range 0 255 + default 1 + help + The offset value of WiFi softap MAC address from the MAC address which is read from efuse. + This offset value must be different from that of WiFi station, bluetooth and ethernet. + config SW_COEXIST_ENABLE bool "Software controls WiFi/Bluetooth coexistence" depends on WIFI_ENABLED && BT_ENABLED diff --git a/components/esp32/include/esp_system.h b/components/esp32/include/esp_system.h index 30701761ad..b127d56fea 100644 --- a/components/esp32/include/esp_system.h +++ b/components/esp32/include/esp_system.h @@ -24,6 +24,13 @@ extern "C" { #endif +enum { + ESP_MAC_WIFI_STA, + ESP_MAC_WIFI_SOFTAP, + ESP_MAC_BT, + ESP_MAC_ETH, +}; + /** * @attention application don't need to call this function anymore. It do nothing and will * be removed in future version. @@ -115,6 +122,19 @@ esp_err_t esp_efuse_read_mac(uint8_t* mac); */ esp_err_t system_efuse_read_mac(uint8_t mac[6]) __attribute__ ((deprecated)); +/** + * @brief Read hardware MAC address and set MAC address of the interface. + * + * This function first reads hardware MAC address from efuse. Then set the MAC address of the interface + * including wifi station, wifi softap, bluetooth and ethernet according to the offset value in menuconfig. + * + * @param mac MAC address of the interface, length: 6 bytes. + * @param interface interface, 0:wifi station, 1:wifi softap, 2:bluetooth, 3:ethernet. + * + * @return ESP_OK on success + */ +esp_err_t esp_read_mac(uint8_t* mac, int interface); + /** * Get SDK version * diff --git a/components/esp32/lib b/components/esp32/lib index 7b06303c0f..e22ec1a514 160000 --- a/components/esp32/lib +++ b/components/esp32/lib @@ -1 +1 @@ -Subproject commit 7b06303c0fa416aea7f86b7596e84db367189066 +Subproject commit e22ec1a5140dca2bb44f9bfb3147911fa4ebb8fe diff --git a/components/esp32/system_api.c b/components/esp32/system_api.c index d984af78a7..36b8b6b588 100644 --- a/components/esp32/system_api.c +++ b/components/esp32/system_api.c @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include + #include "esp_system.h" #include "esp_attr.h" #include "esp_wifi.h" @@ -72,6 +74,47 @@ esp_err_t esp_efuse_read_mac(uint8_t* mac) esp_err_t system_efuse_read_mac(uint8_t mac[6]) __attribute__((alias("esp_efuse_read_mac"))); +esp_err_t esp_read_mac(uint8_t* mac, int interface) +{ + uint8_t efuse_mac[6]; + + if (mac == NULL) { + ESP_LOGE(TAG, "mac address param is NULL"); + abort(); + } + + esp_efuse_read_mac(efuse_mac); + + switch (interface) { +#if CONFIG_WIFI_ENABLED + case ESP_MAC_WIFI_STA: + memcpy(mac, efuse_mac, 6); + mac[5] += CONFIG_ESP_MAC_OFFSET_WIFI_STA; + break; + case ESP_MAC_WIFI_SOFTAP: + memcpy(mac, efuse_mac, 6); + mac[5] += CONFIG_ESP_MAC_OFFSET_WIFI_SOFTAP; + break; +#endif +#if CONFIG_BT_ENABLED + case ESP_MAC_WIFI_BT: + memcpy(mac, efuse_mac, 6); + mac[5] += CONFIG_ESP_MAC_OFFSET_BT; + break; +#endif +#if CONFIG_ETHERNET + case ESP_MAC_WIFI_ETH: + memcpy(mac, efuse_mac, 6); + mac[5] += CONFIG_ESP_MAC_OFFSET_ETH; + break; +#endif + default: + ESP_LOGW(TAG, "wrong mac type"); + break; + } + + return ESP_OK; +} void esp_restart_noos() __attribute__ ((noreturn)); diff --git a/components/ethernet/Kconfig b/components/ethernet/Kconfig index 46e86cc60e..230e53cdc7 100644 --- a/components/ethernet/Kconfig +++ b/components/ethernet/Kconfig @@ -4,6 +4,15 @@ menuconfig ETHERNET help Select this option to enable ethernet driver and show the submenu with ethernet features. +config ESP_MAC_OFFSET_ETH + int "MAC address offset value of ethernet" + depends on ETHERNET + range 0 255 + default 3 + help + The offset value of ethernet MAC address from the MAC address which is read from efuse. + This offset value must be different from that of WiFi softap, bluetooth and WiFi station. + config DMA_RX_BUF_NUM int "Number of DMA RX buffers" range 3 20 diff --git a/components/ethernet/emac_main.c b/components/ethernet/emac_main.c index 5479ec399e..8c93a1830e 100644 --- a/components/ethernet/emac_main.c +++ b/components/ethernet/emac_main.c @@ -75,8 +75,7 @@ esp_err_t emac_post(emac_sig_t sig, emac_par_t par); static void emac_macaddr_init(void) { - esp_efuse_read_mac(&(emac_config.macaddr[0])); - emac_config.macaddr[5] = emac_config.macaddr[5] + 3; + esp_read_mac(&(emac_config.macaddr[0]), ESP_MAC_ETH); } void esp_eth_get_mac(uint8_t mac[6]) -- 2.40.0