diff --git a/arch/risc-v/src/esp32c3/esp32c3_ble_adapter.c b/arch/risc-v/src/esp32c3/esp32c3_ble_adapter.c index 366b3a4a87..8e62065001 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_ble_adapter.c +++ b/arch/risc-v/src/esp32c3/esp32c3_ble_adapter.c @@ -1328,7 +1328,7 @@ static void *malloc_internal_wrapper(size_t size) static int read_mac_wrapper(uint8_t mac[6]) { - return 0; + return esp_read_mac(mac, ESP_MAC_BT); } /**************************************************************************** diff --git a/arch/risc-v/src/esp32c3/esp32c3_wifi_adapter.c b/arch/risc-v/src/esp32c3/esp32c3_wifi_adapter.c index 5a456d0bab..343c8aaef2 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_wifi_adapter.c +++ b/arch/risc-v/src/esp32c3/esp32c3_wifi_adapter.c @@ -84,9 +84,6 @@ # error "on_exit() API must be enabled for deallocating Wi-Fi resources" #endif -#define MAC_ADDR0_REG (DR_REG_EFUSE_BASE + 0x044) -#define MAC_ADDR1_REG (DR_REG_EFUSE_BASE + 0x048) - #define PHY_RF_MASK ((1 << PHY_BT_MODULE) | (1 << PHY_WIFI_MODULE)) #ifdef CONFIG_ESP32C3_WIFI_SAVE_PARAM @@ -2652,66 +2649,6 @@ static int wifi_phy_update_country_info(const char *country) return -1; } -/**************************************************************************** - * Name: esp_read_mac - * - * Description: - * Read MAC address from efuse - * - * Input Parameters: - * mac - MAC address buffer pointer - * type - MAC address type - * - * Returned Value: - * 0 if success or -1 if fail - * - ****************************************************************************/ - -esp_err_t esp_read_mac(uint8_t *mac, esp_mac_type_t type) -{ - uint32_t regval[2]; - uint8_t tmp; - uint8_t *data = (uint8_t *)regval; - int i; - - if (type > ESP_MAC_WIFI_SOFTAP) - { - wlerr("ERROR: Input type is error=%d\n", type); - return -1; - } - - regval[0] = getreg32(MAC_ADDR0_REG); - regval[1] = getreg32(MAC_ADDR1_REG); - - for (i = 0; i < MAC_LEN; i++) - { - mac[i] = data[5 - i]; - } - - if (type == ESP_MAC_WIFI_SOFTAP) - { - tmp = mac[0]; - for (i = 0; i < 64; i++) - { - mac[0] = tmp | 0x02; - mac[0] ^= i << 2; - - if (mac[0] != tmp) - { - break; - } - } - - if (i >= 64) - { - wlerr("ERROR: Failed to generate softAP MAC\n"); - return -1; - } - } - - return 0; -} - /**************************************************************************** * Name: esp_wifi_read_mac * diff --git a/arch/risc-v/src/esp32c3/esp32c3_wifi_adapter.h b/arch/risc-v/src/esp32c3/esp32c3_wifi_adapter.h index 964319e7b5..1d2cfc1e03 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_wifi_adapter.h +++ b/arch/risc-v/src/esp32c3/esp32c3_wifi_adapter.h @@ -62,7 +62,6 @@ extern "C" #define SSID_MAX_LEN (32) #define PWD_MAX_LEN (64) -#define MAC_LEN (6) /* Wi-Fi event ID */ diff --git a/arch/risc-v/src/esp32c3/esp32c3_wifi_utils.c b/arch/risc-v/src/esp32c3/esp32c3_wifi_utils.c index a3c9b4029e..07ef6bb84f 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_wifi_utils.c +++ b/arch/risc-v/src/esp32c3/esp32c3_wifi_utils.c @@ -33,6 +33,7 @@ #include "esp32c3_wifi_adapter.h" #include "esp32c3_wifi_utils.h" +#include "esp32c3_wireless.h" #include "espidf_wifi.h" /**************************************************************************** diff --git a/arch/risc-v/src/esp32c3/esp32c3_wireless.c b/arch/risc-v/src/esp32c3/esp32c3_wireless.c index 1e085041dd..6e9060f235 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_wireless.c +++ b/arch/risc-v/src/esp32c3/esp32c3_wireless.c @@ -33,17 +33,21 @@ #include "hardware/esp32c3_system.h" #include "hardware/esp32c3_soc.h" #include "hardware/esp32c3_syscon.h" +#include "hardware/esp32c3_efuse.h" +#include "esp32c3_wireless.h" #include "esp32c3.h" #include "esp32c3_irq.h" #include "esp32c3_attr.h" #include "esp32c3_wireless.h" - #include "espidf_wifi.h" /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ +#define MAC_ADDR0_REG EFUSE_RD_MAC_SPI_SYS_0_REG +#define MAC_ADDR1_REG EFUSE_RD_MAC_SPI_SYS_1_REG + /* Software Interrupt */ #define SWI_IRQ ESP32C3_IRQ_FROM_CPU_INT0 @@ -504,3 +508,80 @@ int esp32c3_wl_deinit(void) return OK; } + +/**************************************************************************** + * Name: esp_read_mac + * + * Description: + * Read MAC address from efuse + * + * Input Parameters: + * mac - MAC address buffer pointer + * type - MAC address type + * + * Returned Value: + * 0 if success or -1 if fail + * + ****************************************************************************/ + +int esp_read_mac(uint8_t *mac, esp_mac_type_t type) +{ + uint32_t regval[2]; + uint8_t tmp; + uint8_t *data = (uint8_t *)regval; + int i; + + if (type > ESP_MAC_BT) + { + wlerr("ERROR: Input type is error=%d\n", type); + return -1; + } + + regval[0] = getreg32(MAC_ADDR0_REG); + regval[1] = getreg32(MAC_ADDR1_REG); + + for (i = 0; i < MAC_LEN; i++) + { + mac[i] = data[5 - i]; + } + + if (type == ESP_MAC_WIFI_SOFTAP) + { + tmp = mac[0]; + for (i = 0; i < 64; i++) + { + mac[0] = tmp | 0x02; + mac[0] ^= i << 2; + + if (mac[0] != tmp) + { + break; + } + } + + if (i >= 64) + { + wlerr("ERROR: Failed to generate softAP MAC\n"); + return -1; + } + } + + if (type == ESP_MAC_BT) + { + tmp = mac[0]; + for (i = 0; i < 64; i++) + { + mac[0] = tmp | 0x02; + mac[0] ^= i << 2; + + if (mac[0] != tmp) + { + break; + } + } + + mac[5] += 1; + } + + return 0; +} diff --git a/arch/risc-v/src/esp32c3/esp32c3_wireless.h b/arch/risc-v/src/esp32c3/esp32c3_wireless.h index 8fe156498c..14da611eb1 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_wireless.h +++ b/arch/risc-v/src/esp32c3/esp32c3_wireless.h @@ -31,6 +31,8 @@ #include #include +#include "espidf_wifi.h" + #ifndef __ASSEMBLY__ #undef EXTERN @@ -56,6 +58,12 @@ struct esp32c3_wl_semcache_s uint32_t count; }; +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define MAC_LEN (6) + /**************************************************************************** * Public Function Prototypes ****************************************************************************/ @@ -160,6 +168,23 @@ int esp32c3_wl_init(void); int esp32c3_wl_deinit(void); +/**************************************************************************** + * Name: esp_read_mac + * + * Description: + * Read MAC address from efuse + * + * Input Parameters: + * mac - MAC address buffer pointer + * type - MAC address type + * + * Returned Value: + * 0 if success or -1 if fail + * + ****************************************************************************/ + +int esp_read_mac(uint8_t *mac, esp_mac_type_t type); + #ifdef __cplusplus } #endif diff --git a/arch/risc-v/src/esp32c3/esp32c3_wlan.c b/arch/risc-v/src/esp32c3/esp32c3_wlan.c index 989bf79bbc..6644e7317a 100644 --- a/arch/risc-v/src/esp32c3/esp32c3_wlan.c +++ b/arch/risc-v/src/esp32c3/esp32c3_wlan.c @@ -47,6 +47,7 @@ #include "esp32c3_wlan.h" #include "esp32c3_wifi_utils.h" #include "esp32c3_wifi_adapter.h" +#include "esp32c3_wireless.h" #include "esp32c3_systemreset.h" /****************************************************************************