From 02d7120d029e03ed61bcfdce8e15ad6710a3ba52 Mon Sep 17 00:00:00 2001 From: Konstantin Kondrashov Date: Wed, 24 Jan 2024 12:59:43 +0200 Subject: [PATCH] feat(esp_hw_support): Adds MAC support for esp32p4 --- components/esp_hw_support/Kconfig | 6 ++ components/esp_hw_support/include/esp_mac.h | 15 +--- components/esp_hw_support/mac_addr.c | 89 ++++++++++--------- .../esp_hw_support/port/esp32p4/Kconfig.mac | 22 +++-- .../pytest_base_mac_address.py | 8 +- 5 files changed, 77 insertions(+), 63 deletions(-) diff --git a/components/esp_hw_support/Kconfig b/components/esp_hw_support/Kconfig index 1ebb71c107..e1924af8c9 100644 --- a/components/esp_hw_support/Kconfig +++ b/components/esp_hw_support/Kconfig @@ -39,6 +39,12 @@ menu "Hardware Settings" config ESP_MAC_UNIVERSAL_MAC_ADDRESSES_FOUR bool + config ESP_MAC_UNIVERSAL_MAC_ADDRESSES + int + default 1 if ESP_MAC_UNIVERSAL_MAC_ADDRESSES_ONE + default 2 if ESP_MAC_UNIVERSAL_MAC_ADDRESSES_TWO + default 4 if ESP_MAC_UNIVERSAL_MAC_ADDRESSES_FOUR + # Insert chip-specific MAC config orsource "./port/$IDF_TARGET/Kconfig.mac" diff --git a/components/esp_hw_support/include/esp_mac.h b/components/esp_hw_support/include/esp_mac.h index 8ead3a393f..365e11337d 100644 --- a/components/esp_hw_support/include/esp_mac.h +++ b/components/esp_hw_support/include/esp_mac.h @@ -31,21 +31,10 @@ typedef enum { } esp_mac_type_t; /** @cond */ +#define ONE_UNIVERSAL_MAC_ADDR 1 #define TWO_UNIVERSAL_MAC_ADDR 2 #define FOUR_UNIVERSAL_MAC_ADDR 4 -#if CONFIG_IDF_TARGET_ESP32 -#define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP32_UNIVERSAL_MAC_ADDRESSES -#elif CONFIG_IDF_TARGET_ESP32S2 -#define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP32S2_UNIVERSAL_MAC_ADDRESSES -#elif CONFIG_IDF_TARGET_ESP32S3 -#define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP32S3_UNIVERSAL_MAC_ADDRESSES -#elif CONFIG_IDF_TARGET_ESP32C3 -#define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP32C3_UNIVERSAL_MAC_ADDRESSES -#elif CONFIG_IDF_TARGET_ESP32C2 -#define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP32C2_UNIVERSAL_MAC_ADDRESSES -#elif CONFIG_IDF_TARGET_ESP32C6 -#define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP32C6_UNIVERSAL_MAC_ADDRESSES -#endif +#define UNIVERSAL_MAC_ADDR_NUM CONFIG_ESP_MAC_UNIVERSAL_MAC_ADDRESSES /** @endcond */ diff --git a/components/esp_hw_support/mac_addr.c b/components/esp_hw_support/mac_addr.c index a73f40e8e6..6059a92aaf 100644 --- a/components/esp_hw_support/mac_addr.c +++ b/components/esp_hw_support/mac_addr.c @@ -1,10 +1,11 @@ /* - * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ #include #include "sdkconfig.h" +#include "soc/soc_caps.h" #include "esp_rom_efuse.h" #include "esp_mac.h" #include "esp_efuse.h" @@ -18,7 +19,7 @@ #define MAC_ADDR_UNIVERSE_BT_OFFSET 1 #endif -#if CONFIG_SOC_IEEE802154_SUPPORTED +#if SOC_IEEE802154_SUPPORTED #define ESP_MAC_ADDRESS_LEN 8 #else #define ESP_MAC_ADDRESS_LEN 6 @@ -39,7 +40,7 @@ typedef struct { } mac_t; static mac_t s_mac_table[] = { -#if CONFIG_SOC_WIFI_SUPPORTED +#if SOC_WIFI_SUPPORTED {ESP_MAC_WIFI_STA, STATE_INIT, 6, {0}}, {ESP_MAC_WIFI_SOFTAP, STATE_INIT, 6, {0}}, #endif @@ -50,7 +51,7 @@ static mac_t s_mac_table[] = { {ESP_MAC_ETH, STATE_INIT, 6, {0}}, -#ifdef CONFIG_SOC_IEEE802154_SUPPORTED +#ifdef SOC_IEEE802154_SUPPORTED {ESP_MAC_IEEE802154, STATE_INIT, ESP_MAC_ADDRESS_LEN, {0}}, {ESP_MAC_EFUSE_EXT, STATE_INIT, 2, {0}}, #endif @@ -65,7 +66,7 @@ static mac_t s_mac_table[] = { static esp_err_t generate_mac(uint8_t *mac, uint8_t *base_mac_addr, esp_mac_type_t type); static esp_err_t get_efuse_factory_mac(uint8_t *mac); static esp_err_t get_efuse_mac_custom(uint8_t *mac); -#if CONFIG_SOC_IEEE802154_SUPPORTED +#if SOC_IEEE802154_SUPPORTED static esp_err_t get_efuse_mac_ext(uint8_t *mac); #endif @@ -102,7 +103,7 @@ static esp_err_t get_mac_addr_from_mac_table(uint8_t *mac, int idx, bool silent) ) { err = get_efuse_mac_custom(s_mac_table[idx].mac); } -#if CONFIG_SOC_IEEE802154_SUPPORTED +#if SOC_IEEE802154_SUPPORTED else if (type == ESP_MAC_EFUSE_EXT) { err = get_efuse_mac_ext(s_mac_table[idx].mac); } @@ -170,7 +171,7 @@ esp_err_t esp_base_mac_addr_get(uint8_t *mac) return esp_read_mac(mac, ESP_MAC_BASE); } -#if CONFIG_SOC_IEEE802154_SUPPORTED +#if SOC_IEEE802154_SUPPORTED static esp_err_t get_efuse_mac_ext(uint8_t *mac) { // ff:fe @@ -201,7 +202,7 @@ esp_err_t esp_efuse_mac_get_custom(uint8_t *mac) if (err != ESP_OK) { return err; } -#if CONFIG_SOC_IEEE802154_SUPPORTED +#if SOC_IEEE802154_SUPPORTED return insert_mac_ext_into_mac(mac); #else return ESP_OK; @@ -258,7 +259,7 @@ esp_err_t esp_efuse_mac_get_default(uint8_t *mac) if (err != ESP_OK) { return err; } -#if CONFIG_SOC_IEEE802154_SUPPORTED +#if SOC_IEEE802154_SUPPORTED return insert_mac_ext_into_mac(mac); #else return ESP_OK; @@ -358,53 +359,58 @@ esp_err_t esp_read_mac(uint8_t *mac, esp_mac_type_t type) static esp_err_t generate_mac(uint8_t *mac, uint8_t *base_mac_addr, esp_mac_type_t type) { switch (type) { +#if SOC_WIFI_SUPPORTED case ESP_MAC_WIFI_STA: memcpy(mac, base_mac_addr, 6); break; case ESP_MAC_WIFI_SOFTAP: -#if CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP - memcpy(mac, base_mac_addr, 6); - // as a result of some esp32s2 chips burned with one MAC address by mistake, - // there are some MAC address are reserved for this bug fix. - // related mistake MAC address is 0x7cdfa1003000~0x7cdfa1005fff, - // reserved MAC address is 0x7cdfa1020000~0x7cdfa1022fff (MAC address + 0x1d000). -#ifdef CONFIG_IDF_TARGET_ESP32S2 - uint8_t mac_begin[6] = { 0x7c, 0xdf, 0xa1, 0x00, 0x30, 0x00 }; - uint8_t mac_end[6] = { 0x7c, 0xdf, 0xa1, 0x00, 0x5f, 0xff }; - if (memcmp(mac, mac_begin, 6) >= 0 && memcmp(mac_end, mac, 6) >= 0 ) { - mac[3] += 0x02; // contain carry bit - mac[4] += 0xd0; - } else { - mac[5] += 1; - } -#else - mac[5] += 1; -#endif // IDF_TARGET_ESP32S2 -#else - esp_derive_local_mac(mac, base_mac_addr); -#endif // CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP + #if CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP + memcpy(mac, base_mac_addr, 6); + // as a result of some esp32s2 chips burned with one MAC address by mistake, + // there are some MAC address are reserved for this bug fix. + // related mistake MAC address is 0x7cdfa1003000~0x7cdfa1005fff, + // reserved MAC address is 0x7cdfa1020000~0x7cdfa1022fff (MAC address + 0x1d000). + #ifdef CONFIG_IDF_TARGET_ESP32S2 + uint8_t mac_begin[6] = { 0x7c, 0xdf, 0xa1, 0x00, 0x30, 0x00 }; + uint8_t mac_end[6] = { 0x7c, 0xdf, 0xa1, 0x00, 0x5f, 0xff }; + if (memcmp(mac, mac_begin, 6) >= 0 && memcmp(mac_end, mac, 6) >= 0 ) { + mac[3] += 0x02; // contain carry bit + mac[4] += 0xd0; + } else { + mac[5] += 1; + } + #else + mac[5] += 1; + #endif // IDF_TARGET_ESP32S2 + #else + esp_derive_local_mac(mac, base_mac_addr); + #endif // CONFIG_ESP_MAC_ADDR_UNIVERSE_WIFI_AP break; - case ESP_MAC_BT: +#endif // SOC_WIFI_SUPPORTED + #if CONFIG_ESP_MAC_ADDR_UNIVERSE_BT + case ESP_MAC_BT: memcpy(mac, base_mac_addr, 6); -#if SOC_WIFI_SUPPORTED - // If the chips do not have wifi module, the mac address do not need to add the BT offset - mac[5] += MAC_ADDR_UNIVERSE_BT_OFFSET; -#endif //SOC_WIFI_SUPPORTED -#else - return ESP_ERR_NOT_SUPPORTED; -#endif // CONFIG_ESP_MAC_ADDR_UNIVERSE_BT + #if SOC_WIFI_SUPPORTED + // If the chips do not have wifi module, the mac address do not need to add the BT offset + mac[5] += MAC_ADDR_UNIVERSE_BT_OFFSET; + #endif // SOC_WIFI_SUPPORTED break; +#endif // CONFIG_ESP_MAC_ADDR_UNIVERSE_BT + case ESP_MAC_ETH: #if CONFIG_ESP_MAC_ADDR_UNIVERSE_ETH memcpy(mac, base_mac_addr, 6); - mac[5] += 3; + #if SOC_WIFI_SUPPORTED || CONFIG_ESP_MAC_ADDR_UNIVERSE_BT + mac[5] += 3; + #endif // SOC_WIFI_SUPPORTED || CONFIG_ESP_MAC_ADDR_UNIVERSE_BT #else base_mac_addr[5] += 1; esp_derive_local_mac(mac, base_mac_addr); #endif // CONFIG_ESP_MAC_ADDR_UNIVERSE_ETH break; -#if CONFIG_SOC_IEEE802154_SUPPORTED + +#if SOC_IEEE802154_SUPPORTED case ESP_MAC_IEEE802154: // 60:55:f9 memcpy(mac, base_mac_addr, 3); @@ -414,7 +420,8 @@ static esp_err_t generate_mac(uint8_t *mac, uint8_t *base_mac_addr, esp_mac_type memcpy(&mac[5], &base_mac_addr[3], 3); // 60:55:f9:ff:fe:f7:2c:a2 break; -#endif +#endif // SOC_IEEE802154_SUPPORTED + default: ESP_LOGE(TAG, "unsupported mac type"); return ESP_ERR_NOT_SUPPORTED; diff --git a/components/esp_hw_support/port/esp32p4/Kconfig.mac b/components/esp_hw_support/port/esp32p4/Kconfig.mac index 54e238685f..f998e2a5f6 100644 --- a/components/esp_hw_support/port/esp32p4/Kconfig.mac +++ b/components/esp_hw_support/port/esp32p4/Kconfig.mac @@ -1,15 +1,25 @@ choice ESP32P4_UNIVERSAL_MAC_ADDRESSES bool "Number of universally administered (by IEEE) MAC address" - default ESP32P4_UNIVERSAL_MAC_ADDRESSES_TWO + default ESP32P4_UNIVERSAL_MAC_ADDRESSES_ONE help - TODO IDF-8949 + Configure the number of universally administered (by IEEE) MAC addresses. - config ESP32P4_UNIVERSAL_MAC_ADDRESSES_TWO - bool "Two" - select ESP_MAC_UNIVERSAL_MAC_ADDRESSES_TWO + During initialization, MAC addresses for each network interface are generated or derived from a + single base MAC address. + + If the number of universal MAC addresses is one, only Ethernet interface receives a universally + administered MAC address. It's generated by adding 0 to the base MAC address. + + When using the default (Espressif-assigned) base MAC address, either setting can be used. When using + a custom universal MAC address range, the correct setting will depend on the allocation of MAC + addresses in this range. + + config ESP32P4_UNIVERSAL_MAC_ADDRESSES_ONE + bool "One" + select ESP_MAC_UNIVERSAL_MAC_ADDRESSES_ONE select ESP_MAC_ADDR_UNIVERSE_ETH endchoice config ESP32P4_UNIVERSAL_MAC_ADDRESSES int - default 2 if ESP32P4_UNIVERSAL_MAC_ADDRESSES_TWO + default 1 if ESP32P4_UNIVERSAL_MAC_ADDRESSES_ONE diff --git a/examples/system/base_mac_address/pytest_base_mac_address.py b/examples/system/base_mac_address/pytest_base_mac_address.py index 8fa8b7f543..3d7fedb30b 100644 --- a/examples/system/base_mac_address/pytest_base_mac_address.py +++ b/examples/system/base_mac_address/pytest_base_mac_address.py @@ -1,6 +1,5 @@ -# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: CC0-1.0 - import pytest from pytest_embedded import Dut @@ -44,7 +43,10 @@ def test_base_mac_address(dut: Dut) -> None: if dut.target != 'esp32s2' and dut.target != 'esp32h2': if sdkconfig.get('ESP_MAC_ADDR_UNIVERSE_BT'): dut.expect_exact('BT MAC: ' + get_expected_mac_string(2, dut.target)) - dut.expect_exact('Ethernet MAC: ' + get_expected_mac_string(3, dut.target)) + if sdkconfig.get('SOC_WIFI_SUPPORTED') or sdkconfig.get('ESP_MAC_ADDR_UNIVERSE_BT'): + dut.expect_exact('Ethernet MAC: ' + get_expected_mac_string(3, dut.target)) + else: + dut.expect_exact('Ethernet MAC: ' + get_expected_mac_string(0, dut.target)) # for esp32p4 dut.expect_exact('New Ethernet MAC: ' + get_expected_mac_string(6, dut.target)) elif dut.target == 'esp32h2': dut.expect_exact('BT MAC: ' + get_expected_mac_string(0, dut.target))