2016-11-15 05:36:18 -05:00
|
|
|
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
2017-02-16 09:06:02 -05:00
|
|
|
#include <sys/lock.h>
|
2017-02-16 06:05:07 -05:00
|
|
|
|
2017-04-11 03:44:43 -04:00
|
|
|
#include "soc/rtc.h"
|
2016-11-15 05:36:18 -05:00
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_phy_init.h"
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "esp_log.h"
|
2016-11-17 12:18:39 -05:00
|
|
|
#include "nvs.h"
|
2017-02-16 06:05:07 -05:00
|
|
|
#include "nvs_flash.h"
|
2022-01-09 22:56:59 -05:00
|
|
|
#include "esp_efuse.h"
|
2016-11-15 05:36:18 -05:00
|
|
|
#include "sdkconfig.h"
|
2018-01-23 23:19:57 -05:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/portmacro.h"
|
2017-01-15 13:33:16 -05:00
|
|
|
#include "phy.h"
|
2016-11-15 05:36:18 -05:00
|
|
|
#include "phy_init_data.h"
|
2019-02-20 08:01:27 -05:00
|
|
|
#include "esp_coexist_internal.h"
|
2017-11-01 05:05:38 -04:00
|
|
|
#include "driver/periph_ctrl.h"
|
2019-03-22 02:21:15 -04:00
|
|
|
#include "esp_private/wifi.h"
|
2020-06-19 03:08:38 -04:00
|
|
|
#include "esp_rom_crc.h"
|
2016-11-15 05:36:18 -05:00
|
|
|
|
2019-05-09 23:34:06 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
#include "esp32/rom/rtc.h"
|
2020-01-16 22:47:08 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
|
|
|
#include "esp32s2/rom/rtc.h"
|
2021-01-10 03:16:28 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
2021-05-20 09:55:59 -04:00
|
|
|
#include "esp32c3/rom/rtc.h"
|
2021-01-10 03:16:28 -05:00
|
|
|
#include "soc/rtc_cntl_reg.h"
|
|
|
|
#include "soc/syscon_reg.h"
|
2021-01-24 11:18:42 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
2021-05-20 09:55:59 -04:00
|
|
|
#include "esp32s3/rom/rtc.h"
|
2021-01-24 11:18:42 -05:00
|
|
|
#include "soc/rtc_cntl_reg.h"
|
|
|
|
#include "soc/syscon_reg.h"
|
2019-05-09 23:34:06 -04:00
|
|
|
#endif
|
|
|
|
|
2019-06-16 23:50:37 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2019-02-20 08:01:27 -05:00
|
|
|
extern wifi_mac_time_update_cb_t s_wifi_mac_time_update_cb;
|
2019-06-16 23:50:37 -04:00
|
|
|
#endif
|
2016-11-15 05:36:18 -05:00
|
|
|
|
2017-11-09 21:54:50 -05:00
|
|
|
static const char* TAG = "phy_init";
|
2017-02-16 06:05:07 -05:00
|
|
|
|
2020-05-22 05:15:28 -04:00
|
|
|
static _lock_t s_phy_access_lock;
|
2017-02-16 06:05:07 -05:00
|
|
|
|
2020-05-22 05:15:28 -04:00
|
|
|
/* Indicate PHY is calibrated or not */
|
|
|
|
static bool s_is_phy_calibrated = false;
|
2017-11-09 21:54:50 -05:00
|
|
|
|
2020-05-22 05:15:28 -04:00
|
|
|
/* Reference count of enabling PHY */
|
|
|
|
static uint8_t s_phy_access_ref = 0;
|
2017-11-09 21:54:50 -05:00
|
|
|
|
2021-01-10 03:16:28 -05:00
|
|
|
#if CONFIG_MAC_BB_PD
|
|
|
|
/* Reference of powering down MAC and BB */
|
2021-02-04 09:24:37 -05:00
|
|
|
static bool s_mac_bb_pu = true;
|
2021-01-10 03:16:28 -05:00
|
|
|
#endif
|
|
|
|
|
2019-06-16 23:50:37 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2018-12-11 03:49:01 -05:00
|
|
|
/* time stamp updated when the PHY/RF is turned on */
|
|
|
|
static int64_t s_phy_rf_en_ts = 0;
|
2019-06-16 23:50:37 -04:00
|
|
|
#endif
|
2018-12-11 03:49:01 -05:00
|
|
|
|
2020-05-22 05:15:28 -04:00
|
|
|
/* PHY spinlock for libphy.a */
|
2019-12-02 01:56:18 -05:00
|
|
|
static DRAM_ATTR portMUX_TYPE s_phy_int_mux = portMUX_INITIALIZER_UNLOCKED;
|
|
|
|
|
2021-01-19 06:36:06 -05:00
|
|
|
/* Memory to store PHY digital registers */
|
|
|
|
static uint32_t* s_phy_digital_regs_mem = NULL;
|
2022-06-16 09:52:12 -04:00
|
|
|
static uint8_t s_phy_backup_mem_ref = 0;
|
2021-01-19 06:36:06 -05:00
|
|
|
|
2021-01-10 03:16:28 -05:00
|
|
|
#if CONFIG_MAC_BB_PD
|
|
|
|
uint32_t* s_mac_bb_pd_mem = NULL;
|
2022-06-16 09:52:12 -04:00
|
|
|
/* Reference count of MAC BB backup memory */
|
|
|
|
static uint8_t s_macbb_backup_mem_ref = 0;
|
2021-01-10 03:16:28 -05:00
|
|
|
#endif
|
|
|
|
|
2019-10-15 23:22:50 -04:00
|
|
|
#if CONFIG_ESP32_SUPPORT_MULTIPLE_PHY_INIT_DATA_BIN
|
2021-05-30 04:32:06 -04:00
|
|
|
#if CONFIG_ESP32_MULTIPLE_PHY_DATA_BIN_EMBEDDED
|
|
|
|
extern uint8_t multi_phy_init_data_bin_start[] asm("_binary_phy_multiple_init_data_bin_start");
|
|
|
|
extern uint8_t multi_phy_init_data_bin_end[] asm("_binary_phy_multiple_init_data_bin_end");
|
|
|
|
#endif
|
2019-10-15 23:22:50 -04:00
|
|
|
/* The following static variables are only used by Wi-Fi tasks, so they can be handled without lock */
|
|
|
|
static phy_init_data_type_t s_phy_init_data_type = 0;
|
|
|
|
|
|
|
|
static phy_init_data_type_t s_current_apply_phy_init_data = 0;
|
|
|
|
|
|
|
|
static char s_phy_current_country[PHY_COUNTRY_CODE_LEN] = {0};
|
|
|
|
|
|
|
|
/* Whether it is a new bin */
|
|
|
|
static bool s_multiple_phy_init_data_bin = false;
|
|
|
|
|
|
|
|
/* PHY init data type array */
|
2020-11-10 02:40:01 -05:00
|
|
|
static char* s_phy_type[ESP_PHY_INIT_DATA_TYPE_NUMBER] = {"DEFAULT", "SRRC", "FCC", "CE", "NCC", "KCC", "MIC", "IC",
|
2019-10-15 23:22:50 -04:00
|
|
|
"ACMA", "ANATEL", "ISED", "WPC", "OFCA", "IFETEL", "RCM"};
|
|
|
|
|
|
|
|
/* Country and PHY init data type map */
|
|
|
|
static phy_country_to_bin_type_t s_country_code_map_type_table[] = {
|
|
|
|
{"AT", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"AU", ESP_PHY_INIT_DATA_TYPE_ACMA},
|
|
|
|
{"BE", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"BG", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"BR", ESP_PHY_INIT_DATA_TYPE_ANATEL},
|
|
|
|
{"CA", ESP_PHY_INIT_DATA_TYPE_ISED},
|
|
|
|
{"CH", ESP_PHY_INIT_DATA_TYPE_CE},
|
2020-11-10 02:40:01 -05:00
|
|
|
{"CN", ESP_PHY_INIT_DATA_TYPE_SRRC},
|
2019-10-15 23:22:50 -04:00
|
|
|
{"CY", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"CZ", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"DE", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"DK", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"EE", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"ES", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"FI", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"FR", ESP_PHY_INIT_DATA_TYPE_CE},
|
2020-11-10 02:40:01 -05:00
|
|
|
{"GB", ESP_PHY_INIT_DATA_TYPE_CE},
|
2019-10-15 23:22:50 -04:00
|
|
|
{"GR", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"HK", ESP_PHY_INIT_DATA_TYPE_OFCA},
|
|
|
|
{"HR", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"HU", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"IE", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"IN", ESP_PHY_INIT_DATA_TYPE_WPC},
|
|
|
|
{"IS", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"IT", ESP_PHY_INIT_DATA_TYPE_CE},
|
2020-11-10 02:40:01 -05:00
|
|
|
{"JP", ESP_PHY_INIT_DATA_TYPE_MIC},
|
2019-10-15 23:22:50 -04:00
|
|
|
{"KR", ESP_PHY_INIT_DATA_TYPE_KCC},
|
|
|
|
{"LI", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"LT", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"LU", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"LV", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"MT", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"MX", ESP_PHY_INIT_DATA_TYPE_IFETEL},
|
|
|
|
{"NL", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"NO", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"NZ", ESP_PHY_INIT_DATA_TYPE_RCM},
|
|
|
|
{"PL", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"PT", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"RO", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"SE", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"SI", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"SK", ESP_PHY_INIT_DATA_TYPE_CE},
|
|
|
|
{"TW", ESP_PHY_INIT_DATA_TYPE_NCC},
|
|
|
|
{"US", ESP_PHY_INIT_DATA_TYPE_FCC},
|
|
|
|
};
|
|
|
|
#endif
|
2018-01-26 04:12:59 -05:00
|
|
|
uint32_t IRAM_ATTR phy_enter_critical(void)
|
2018-01-23 23:19:57 -05:00
|
|
|
{
|
2019-12-02 01:56:18 -05:00
|
|
|
if (xPortInIsrContext()) {
|
|
|
|
portENTER_CRITICAL_ISR(&s_phy_int_mux);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
portENTER_CRITICAL(&s_phy_int_mux);
|
|
|
|
}
|
|
|
|
// Interrupt level will be stored in current tcb, so always return zero.
|
|
|
|
return 0;
|
2018-01-23 23:19:57 -05:00
|
|
|
}
|
|
|
|
|
2018-01-26 04:12:59 -05:00
|
|
|
void IRAM_ATTR phy_exit_critical(uint32_t level)
|
2018-01-23 23:19:57 -05:00
|
|
|
{
|
2019-12-02 01:56:18 -05:00
|
|
|
// Param level don't need any more, ignore it.
|
|
|
|
if (xPortInIsrContext()) {
|
|
|
|
portEXIT_CRITICAL_ISR(&s_phy_int_mux);
|
|
|
|
} else {
|
|
|
|
portEXIT_CRITICAL(&s_phy_int_mux);
|
|
|
|
}
|
2018-01-23 23:19:57 -05:00
|
|
|
}
|
|
|
|
|
2019-06-16 23:50:37 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2018-12-11 03:49:01 -05:00
|
|
|
int64_t esp_phy_rf_get_on_ts(void)
|
|
|
|
{
|
|
|
|
return s_phy_rf_en_ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void phy_update_wifi_mac_time(bool en_clock_stopped, int64_t now)
|
2018-10-11 08:57:15 -04:00
|
|
|
{
|
|
|
|
static uint32_t s_common_clock_disable_time = 0;
|
|
|
|
|
|
|
|
if (en_clock_stopped) {
|
2018-12-11 03:49:01 -05:00
|
|
|
s_common_clock_disable_time = (uint32_t)now;
|
2018-10-11 08:57:15 -04:00
|
|
|
} else {
|
|
|
|
if (s_common_clock_disable_time) {
|
2018-12-11 03:49:01 -05:00
|
|
|
uint32_t diff = (uint64_t)now - s_common_clock_disable_time;
|
2018-10-11 08:57:15 -04:00
|
|
|
|
2019-02-20 08:01:27 -05:00
|
|
|
if (s_wifi_mac_time_update_cb) {
|
|
|
|
s_wifi_mac_time_update_cb(diff);
|
|
|
|
}
|
2018-10-11 08:57:15 -04:00
|
|
|
s_common_clock_disable_time = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-30 03:51:12 -05:00
|
|
|
#endif
|
2019-08-20 03:47:42 -04:00
|
|
|
|
|
|
|
IRAM_ATTR void esp_phy_common_clock_enable(void)
|
|
|
|
{
|
2020-07-02 07:53:15 -04:00
|
|
|
wifi_bt_common_module_enable();
|
2019-08-20 03:47:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
IRAM_ATTR void esp_phy_common_clock_disable(void)
|
|
|
|
{
|
2020-07-02 07:53:15 -04:00
|
|
|
wifi_bt_common_module_disable();
|
2019-08-20 03:47:42 -04:00
|
|
|
}
|
|
|
|
|
2021-01-19 06:36:06 -05:00
|
|
|
static inline void phy_digital_regs_store(void)
|
|
|
|
{
|
|
|
|
if (s_phy_digital_regs_mem != NULL) {
|
|
|
|
phy_dig_reg_backup(true, s_phy_digital_regs_mem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void phy_digital_regs_load(void)
|
|
|
|
{
|
|
|
|
if (s_phy_digital_regs_mem != NULL) {
|
|
|
|
phy_dig_reg_backup(false, s_phy_digital_regs_mem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-21 05:56:09 -05:00
|
|
|
void esp_phy_enable(void)
|
2017-02-16 06:05:07 -05:00
|
|
|
{
|
2020-05-22 05:15:28 -04:00
|
|
|
_lock_acquire(&s_phy_access_lock);
|
2017-11-09 21:54:50 -05:00
|
|
|
|
2020-05-22 05:15:28 -04:00
|
|
|
if (s_phy_access_ref == 0) {
|
2019-06-16 23:50:37 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2020-05-22 05:15:28 -04:00
|
|
|
// Update time stamp
|
|
|
|
s_phy_rf_en_ts = esp_timer_get_time();
|
|
|
|
// Update WiFi MAC time before WiFi/BT common clock is enabled
|
|
|
|
phy_update_wifi_mac_time(false, s_phy_rf_en_ts);
|
2019-06-16 23:50:37 -04:00
|
|
|
#endif
|
2020-05-22 05:15:28 -04:00
|
|
|
esp_phy_common_clock_enable();
|
2018-05-19 00:13:34 -04:00
|
|
|
|
2020-05-22 05:15:28 -04:00
|
|
|
if (s_is_phy_calibrated == false) {
|
|
|
|
esp_phy_load_cal_and_init();
|
|
|
|
s_is_phy_calibrated = true;
|
2017-11-09 21:54:50 -05:00
|
|
|
}
|
|
|
|
else {
|
2020-05-22 05:15:28 -04:00
|
|
|
phy_wakeup_init();
|
2021-01-19 06:36:06 -05:00
|
|
|
phy_digital_regs_load();
|
2017-11-09 21:54:50 -05:00
|
|
|
}
|
|
|
|
|
2019-06-16 23:50:37 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2020-05-22 05:15:28 -04:00
|
|
|
coex_bt_high_prio();
|
2019-06-16 23:50:37 -04:00
|
|
|
#endif
|
2020-07-09 08:58:13 -04:00
|
|
|
|
2021-01-24 11:18:42 -05:00
|
|
|
#if CONFIG_BT_ENABLED && (CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3)
|
2020-07-09 08:58:13 -04:00
|
|
|
extern void coex_pti_v2(void);
|
|
|
|
coex_pti_v2();
|
|
|
|
#endif
|
|
|
|
|
2017-02-16 06:05:07 -05:00
|
|
|
}
|
2020-05-22 05:15:28 -04:00
|
|
|
s_phy_access_ref++;
|
2017-11-09 21:54:50 -05:00
|
|
|
|
2020-05-22 05:15:28 -04:00
|
|
|
_lock_release(&s_phy_access_lock);
|
2017-11-09 21:54:50 -05:00
|
|
|
}
|
|
|
|
|
2021-01-21 05:56:09 -05:00
|
|
|
void esp_phy_disable(void)
|
2017-11-09 21:54:50 -05:00
|
|
|
{
|
2020-05-22 05:15:28 -04:00
|
|
|
_lock_acquire(&s_phy_access_lock);
|
2017-11-09 21:54:50 -05:00
|
|
|
|
2020-05-22 05:15:28 -04:00
|
|
|
s_phy_access_ref--;
|
|
|
|
if (s_phy_access_ref == 0) {
|
2021-01-19 06:36:06 -05:00
|
|
|
phy_digital_regs_store();
|
2020-05-22 05:15:28 -04:00
|
|
|
// Disable PHY and RF.
|
|
|
|
phy_close_rf();
|
2021-03-29 02:08:24 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S2
|
2021-01-19 01:57:52 -05:00
|
|
|
// Disable PHY temperature sensor
|
|
|
|
phy_xpd_tsens();
|
|
|
|
#endif
|
2020-05-22 05:15:28 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
// Update WiFi MAC time before disalbe WiFi/BT common peripheral clock
|
|
|
|
phy_update_wifi_mac_time(true, esp_timer_get_time());
|
2017-11-09 21:54:50 -05:00
|
|
|
#endif
|
2020-05-22 05:15:28 -04:00
|
|
|
// Disable WiFi/BT common peripheral clock. Do not disable clock for hardware RNG
|
|
|
|
esp_phy_common_clock_disable();
|
2017-11-09 21:54:50 -05:00
|
|
|
}
|
|
|
|
|
2020-05-22 05:15:28 -04:00
|
|
|
_lock_release(&s_phy_access_lock);
|
2017-11-09 21:54:50 -05:00
|
|
|
}
|
|
|
|
|
2022-06-16 09:52:12 -04:00
|
|
|
void esp_phy_pd_mem_init(void)
|
|
|
|
{
|
|
|
|
_lock_acquire(&s_phy_access_lock);
|
|
|
|
|
|
|
|
s_phy_backup_mem_ref++;
|
|
|
|
if (s_phy_digital_regs_mem == NULL) {
|
|
|
|
s_phy_digital_regs_mem = (uint32_t *)heap_caps_malloc(SOC_PHY_DIG_REGS_MEM_SIZE, MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
_lock_release(&s_phy_access_lock);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_phy_pd_mem_deinit(void)
|
|
|
|
{
|
|
|
|
_lock_acquire(&s_phy_access_lock);
|
|
|
|
|
|
|
|
s_phy_backup_mem_ref--;
|
|
|
|
if (s_phy_backup_mem_ref == 0) {
|
2022-07-06 00:19:22 -04:00
|
|
|
s_is_phy_calibrated = false;
|
2022-06-16 09:52:12 -04:00
|
|
|
free(s_phy_digital_regs_mem);
|
|
|
|
s_phy_digital_regs_mem = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
_lock_release(&s_phy_access_lock);
|
|
|
|
}
|
|
|
|
|
2021-01-10 03:16:28 -05:00
|
|
|
#if CONFIG_MAC_BB_PD
|
|
|
|
void esp_mac_bb_pd_mem_init(void)
|
|
|
|
{
|
|
|
|
_lock_acquire(&s_phy_access_lock);
|
|
|
|
|
2022-06-16 09:52:12 -04:00
|
|
|
s_macbb_backup_mem_ref++;
|
2021-01-10 03:16:28 -05:00
|
|
|
if (s_mac_bb_pd_mem == NULL) {
|
2021-01-19 06:36:06 -05:00
|
|
|
s_mac_bb_pd_mem = (uint32_t *)heap_caps_malloc(SOC_MAC_BB_PD_MEM_SIZE, MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
|
2021-01-10 03:16:28 -05:00
|
|
|
}
|
2022-06-16 09:52:12 -04:00
|
|
|
_lock_release(&s_phy_access_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void esp_mac_bb_pd_mem_deinit(void)
|
|
|
|
{
|
|
|
|
_lock_acquire(&s_phy_access_lock);
|
|
|
|
|
|
|
|
s_macbb_backup_mem_ref--;
|
|
|
|
if (s_macbb_backup_mem_ref == 0) {
|
|
|
|
free(s_mac_bb_pd_mem);
|
|
|
|
s_mac_bb_pd_mem = NULL;
|
|
|
|
}
|
2021-01-10 03:16:28 -05:00
|
|
|
|
|
|
|
_lock_release(&s_phy_access_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
IRAM_ATTR void esp_mac_bb_power_up(void)
|
|
|
|
{
|
2021-02-04 09:24:37 -05:00
|
|
|
if (s_mac_bb_pd_mem != NULL && (!s_mac_bb_pu)) {
|
2021-01-10 03:16:28 -05:00
|
|
|
esp_phy_common_clock_enable();
|
|
|
|
CLEAR_PERI_REG_MASK(RTC_CNTL_DIG_PWC_REG, RTC_CNTL_WIFI_FORCE_PD);
|
|
|
|
SET_PERI_REG_MASK(SYSCON_WIFI_RST_EN_REG, SYSTEM_BB_RST | SYSTEM_FE_RST);
|
|
|
|
CLEAR_PERI_REG_MASK(SYSCON_WIFI_RST_EN_REG, SYSTEM_BB_RST | SYSTEM_FE_RST);
|
|
|
|
CLEAR_PERI_REG_MASK(RTC_CNTL_DIG_ISO_REG, RTC_CNTL_WIFI_FORCE_ISO);
|
|
|
|
phy_freq_mem_backup(false, s_mac_bb_pd_mem);
|
|
|
|
esp_phy_common_clock_disable();
|
2021-02-04 09:24:37 -05:00
|
|
|
s_mac_bb_pu = true;
|
2021-01-10 03:16:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IRAM_ATTR void esp_mac_bb_power_down(void)
|
|
|
|
{
|
2021-02-04 09:24:37 -05:00
|
|
|
if (s_mac_bb_pd_mem != NULL && s_mac_bb_pu) {
|
2021-01-10 03:16:28 -05:00
|
|
|
esp_phy_common_clock_enable();
|
|
|
|
phy_freq_mem_backup(true, s_mac_bb_pd_mem);
|
|
|
|
SET_PERI_REG_MASK(RTC_CNTL_DIG_ISO_REG, RTC_CNTL_WIFI_FORCE_ISO);
|
|
|
|
SET_PERI_REG_MASK(RTC_CNTL_DIG_PWC_REG, RTC_CNTL_WIFI_FORCE_PD);
|
|
|
|
esp_phy_common_clock_disable();
|
2021-02-04 09:24:37 -05:00
|
|
|
s_mac_bb_pu = false;
|
2021-01-10 03:16:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-11-15 05:36:18 -05:00
|
|
|
// PHY init data handling functions
|
|
|
|
#if CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION
|
|
|
|
#include "esp_partition.h"
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
const esp_phy_init_data_t* esp_phy_get_init_data(void)
|
2016-11-15 05:36:18 -05:00
|
|
|
{
|
2021-05-30 04:32:06 -04:00
|
|
|
#if CONFIG_ESP32_MULTIPLE_PHY_DATA_BIN_EMBEDDED
|
|
|
|
size_t init_data_store_length = sizeof(phy_init_magic_pre) +
|
|
|
|
sizeof(esp_phy_init_data_t) + sizeof(phy_init_magic_post);
|
|
|
|
uint8_t* init_data_store = (uint8_t*) malloc(init_data_store_length);
|
|
|
|
if (init_data_store == NULL) {
|
|
|
|
ESP_LOGE(TAG, "failed to allocate memory for updated country code PHY init data");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy(init_data_store, multi_phy_init_data_bin_start, init_data_store_length);
|
|
|
|
ESP_LOGI(TAG, "loading embedded multiple PHY init data");
|
|
|
|
#else
|
2022-04-20 05:25:29 -04:00
|
|
|
const esp_partition_t* partition = esp_partition_find_first(
|
2016-11-15 05:36:18 -05:00
|
|
|
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_PHY, NULL);
|
|
|
|
if (partition == NULL) {
|
|
|
|
ESP_LOGE(TAG, "PHY data partition not found");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ESP_LOGD(TAG, "loading PHY init data from partition at offset 0x%x", partition->address);
|
|
|
|
size_t init_data_store_length = sizeof(phy_init_magic_pre) +
|
|
|
|
sizeof(esp_phy_init_data_t) + sizeof(phy_init_magic_post);
|
|
|
|
uint8_t* init_data_store = (uint8_t*) malloc(init_data_store_length);
|
|
|
|
if (init_data_store == NULL) {
|
|
|
|
ESP_LOGE(TAG, "failed to allocate memory for PHY init data");
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-04-20 05:25:29 -04:00
|
|
|
esp_err_t err = esp_partition_read(partition, 0, init_data_store, init_data_store_length);
|
2016-11-15 05:36:18 -05:00
|
|
|
if (err != ESP_OK) {
|
2017-03-14 09:27:27 -04:00
|
|
|
ESP_LOGE(TAG, "failed to read PHY data partition (0x%x)", err);
|
2021-05-30 04:32:06 -04:00
|
|
|
free(init_data_store);
|
2016-11-15 05:36:18 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
2021-05-30 04:32:06 -04:00
|
|
|
#endif
|
2016-11-15 05:36:18 -05:00
|
|
|
if (memcmp(init_data_store, PHY_INIT_MAGIC, sizeof(phy_init_magic_pre)) != 0 ||
|
|
|
|
memcmp(init_data_store + init_data_store_length - sizeof(phy_init_magic_post),
|
|
|
|
PHY_INIT_MAGIC, sizeof(phy_init_magic_post)) != 0) {
|
|
|
|
ESP_LOGE(TAG, "failed to validate PHY data partition");
|
2021-05-30 04:32:06 -04:00
|
|
|
free(init_data_store);
|
2016-11-15 05:36:18 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-11-10 02:40:01 -05:00
|
|
|
#if CONFIG_ESP32_SUPPORT_MULTIPLE_PHY_INIT_DATA_BIN
|
2019-10-15 23:22:50 -04:00
|
|
|
if ((*(init_data_store + (sizeof(phy_init_magic_pre) + PHY_SUPPORT_MULTIPLE_BIN_OFFSET)))) {
|
|
|
|
s_multiple_phy_init_data_bin = true;
|
|
|
|
ESP_LOGI(TAG, "Support multiple PHY init data bins");
|
|
|
|
} else {
|
|
|
|
ESP_LOGW(TAG, "Does not support multiple PHY init data bins");
|
|
|
|
}
|
|
|
|
#endif
|
2017-07-04 23:29:53 -04:00
|
|
|
ESP_LOGD(TAG, "PHY data partition validated");
|
2016-11-15 05:36:18 -05:00
|
|
|
return (const esp_phy_init_data_t*) (init_data_store + sizeof(phy_init_magic_pre));
|
|
|
|
}
|
|
|
|
|
2016-11-17 12:18:39 -05:00
|
|
|
void esp_phy_release_init_data(const esp_phy_init_data_t* init_data)
|
2016-11-15 05:36:18 -05:00
|
|
|
{
|
|
|
|
free((uint8_t*) init_data - sizeof(phy_init_magic_pre));
|
|
|
|
}
|
|
|
|
|
|
|
|
#else // CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION
|
|
|
|
|
|
|
|
// phy_init_data.h will declare static 'phy_init_data' variable initialized with default init data
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
const esp_phy_init_data_t* esp_phy_get_init_data(void)
|
2016-11-15 05:36:18 -05:00
|
|
|
{
|
|
|
|
ESP_LOGD(TAG, "loading PHY init data from application binary");
|
|
|
|
return &phy_init_data;
|
|
|
|
}
|
|
|
|
|
2016-11-17 12:18:39 -05:00
|
|
|
void esp_phy_release_init_data(const esp_phy_init_data_t* init_data)
|
2016-11-15 05:36:18 -05:00
|
|
|
{
|
|
|
|
// no-op
|
|
|
|
}
|
|
|
|
#endif // CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION
|
|
|
|
|
|
|
|
|
|
|
|
// PHY calibration data handling functions
|
|
|
|
static const char* PHY_NAMESPACE = "phy";
|
|
|
|
static const char* PHY_CAL_VERSION_KEY = "cal_version";
|
|
|
|
static const char* PHY_CAL_MAC_KEY = "cal_mac";
|
|
|
|
static const char* PHY_CAL_DATA_KEY = "cal_data";
|
|
|
|
|
2019-04-01 10:13:55 -04:00
|
|
|
static esp_err_t load_cal_data_from_nvs_handle(nvs_handle_t handle,
|
2016-11-15 05:36:18 -05:00
|
|
|
esp_phy_calibration_data_t* out_cal_data);
|
|
|
|
|
2019-04-01 10:13:55 -04:00
|
|
|
static esp_err_t store_cal_data_to_nvs_handle(nvs_handle_t handle,
|
2016-11-15 05:36:18 -05:00
|
|
|
const esp_phy_calibration_data_t* cal_data);
|
|
|
|
|
2016-11-17 12:18:39 -05:00
|
|
|
esp_err_t esp_phy_load_cal_data_from_nvs(esp_phy_calibration_data_t* out_cal_data)
|
2016-11-15 05:36:18 -05:00
|
|
|
{
|
2019-04-01 10:13:55 -04:00
|
|
|
nvs_handle_t handle;
|
2017-07-13 11:46:19 -04:00
|
|
|
esp_err_t err = nvs_open(PHY_NAMESPACE, NVS_READONLY, &handle);
|
|
|
|
if (err == ESP_ERR_NVS_NOT_INITIALIZED) {
|
|
|
|
ESP_LOGE(TAG, "%s: NVS has not been initialized. "
|
|
|
|
"Call nvs_flash_init before starting WiFi/BT.", __func__);
|
2018-10-22 08:46:14 -04:00
|
|
|
return err;
|
2017-07-13 11:46:19 -04:00
|
|
|
} else if (err != ESP_OK) {
|
2017-03-14 09:27:27 -04:00
|
|
|
ESP_LOGD(TAG, "%s: failed to open NVS namespace (0x%x)", __func__, err);
|
2016-11-15 05:36:18 -05:00
|
|
|
return err;
|
|
|
|
}
|
2017-07-13 11:46:19 -04:00
|
|
|
err = load_cal_data_from_nvs_handle(handle, out_cal_data);
|
|
|
|
nvs_close(handle);
|
|
|
|
return err;
|
2016-11-15 05:36:18 -05:00
|
|
|
}
|
|
|
|
|
2016-11-17 12:18:39 -05:00
|
|
|
esp_err_t esp_phy_store_cal_data_to_nvs(const esp_phy_calibration_data_t* cal_data)
|
2016-11-15 05:36:18 -05:00
|
|
|
{
|
2019-04-01 10:13:55 -04:00
|
|
|
nvs_handle_t handle;
|
2016-11-15 05:36:18 -05:00
|
|
|
esp_err_t err = nvs_open(PHY_NAMESPACE, NVS_READWRITE, &handle);
|
|
|
|
if (err != ESP_OK) {
|
2017-03-14 09:27:27 -04:00
|
|
|
ESP_LOGD(TAG, "%s: failed to open NVS namespace (0x%x)", __func__, err);
|
2016-11-15 05:36:18 -05:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
else {
|
2016-11-17 12:18:39 -05:00
|
|
|
err = store_cal_data_to_nvs_handle(handle, cal_data);
|
2016-11-15 05:36:18 -05:00
|
|
|
nvs_close(handle);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-29 03:07:28 -05:00
|
|
|
esp_err_t esp_phy_erase_cal_data_in_nvs(void)
|
|
|
|
{
|
2019-04-01 10:13:55 -04:00
|
|
|
nvs_handle_t handle;
|
2018-11-29 03:07:28 -05:00
|
|
|
esp_err_t err = nvs_open(PHY_NAMESPACE, NVS_READWRITE, &handle);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "%s: failed to open NVS phy namespace (0x%x)", __func__, err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
err = nvs_erase_all(handle);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "%s: failed to erase NVS phy namespace (0x%x)", __func__, err);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
err = nvs_commit(handle);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "%s: failed to commit NVS phy namespace (0x%x)", __func__, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nvs_close(handle);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2019-04-01 10:13:55 -04:00
|
|
|
static esp_err_t load_cal_data_from_nvs_handle(nvs_handle_t handle,
|
2016-11-17 12:18:39 -05:00
|
|
|
esp_phy_calibration_data_t* out_cal_data)
|
2016-11-15 05:36:18 -05:00
|
|
|
{
|
|
|
|
esp_err_t err;
|
|
|
|
uint32_t cal_data_version;
|
2019-10-15 23:22:50 -04:00
|
|
|
|
2016-11-15 05:36:18 -05:00
|
|
|
err = nvs_get_u32(handle, PHY_CAL_VERSION_KEY, &cal_data_version);
|
|
|
|
if (err != ESP_OK) {
|
2017-03-14 09:27:27 -04:00
|
|
|
ESP_LOGD(TAG, "%s: failed to get cal_version (0x%x)", __func__, err);
|
2016-11-15 05:36:18 -05:00
|
|
|
return err;
|
|
|
|
}
|
2016-11-17 12:18:39 -05:00
|
|
|
uint32_t cal_format_version = phy_get_rf_cal_version() & (~BIT(16));
|
|
|
|
ESP_LOGV(TAG, "phy_get_rf_cal_version: %d\n", cal_format_version);
|
2016-11-15 05:36:18 -05:00
|
|
|
if (cal_data_version != cal_format_version) {
|
|
|
|
ESP_LOGD(TAG, "%s: expected calibration data format %d, found %d",
|
|
|
|
__func__, cal_format_version, cal_data_version);
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
uint8_t cal_data_mac[6];
|
|
|
|
size_t length = sizeof(cal_data_mac);
|
|
|
|
err = nvs_get_blob(handle, PHY_CAL_MAC_KEY, cal_data_mac, &length);
|
|
|
|
if (err != ESP_OK) {
|
2017-03-14 09:27:27 -04:00
|
|
|
ESP_LOGD(TAG, "%s: failed to get cal_mac (0x%x)", __func__, err);
|
2016-11-15 05:36:18 -05:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
if (length != sizeof(cal_data_mac)) {
|
|
|
|
ESP_LOGD(TAG, "%s: invalid length of cal_mac (%d)", __func__, length);
|
|
|
|
return ESP_ERR_INVALID_SIZE;
|
|
|
|
}
|
|
|
|
uint8_t sta_mac[6];
|
2017-05-05 10:24:56 -04:00
|
|
|
esp_efuse_mac_get_default(sta_mac);
|
2016-11-15 05:36:18 -05:00
|
|
|
if (memcmp(sta_mac, cal_data_mac, sizeof(sta_mac)) != 0) {
|
|
|
|
ESP_LOGE(TAG, "%s: calibration data MAC check failed: expected " \
|
|
|
|
MACSTR ", found " MACSTR,
|
|
|
|
__func__, MAC2STR(sta_mac), MAC2STR(cal_data_mac));
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
length = sizeof(*out_cal_data);
|
|
|
|
err = nvs_get_blob(handle, PHY_CAL_DATA_KEY, out_cal_data, &length);
|
|
|
|
if (err != ESP_OK) {
|
2017-03-14 09:27:27 -04:00
|
|
|
ESP_LOGE(TAG, "%s: failed to get cal_data(0x%x)", __func__, err);
|
2016-11-15 05:36:18 -05:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
if (length != sizeof(*out_cal_data)) {
|
|
|
|
ESP_LOGD(TAG, "%s: invalid length of cal_data (%d)", __func__, length);
|
|
|
|
return ESP_ERR_INVALID_SIZE;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2019-04-01 10:13:55 -04:00
|
|
|
static esp_err_t store_cal_data_to_nvs_handle(nvs_handle_t handle,
|
2016-11-15 05:36:18 -05:00
|
|
|
const esp_phy_calibration_data_t* cal_data)
|
|
|
|
{
|
|
|
|
esp_err_t err;
|
2018-05-03 03:48:14 -04:00
|
|
|
|
|
|
|
err = nvs_set_blob(handle, PHY_CAL_DATA_KEY, cal_data, sizeof(*cal_data));
|
2016-11-15 05:36:18 -05:00
|
|
|
if (err != ESP_OK) {
|
2018-05-03 03:48:14 -04:00
|
|
|
ESP_LOGE(TAG, "%s: store calibration data failed(0x%x)\n", __func__, err);
|
2016-11-15 05:36:18 -05:00
|
|
|
return err;
|
|
|
|
}
|
2018-05-03 03:48:14 -04:00
|
|
|
|
2016-11-15 05:36:18 -05:00
|
|
|
uint8_t sta_mac[6];
|
2017-05-05 10:24:56 -04:00
|
|
|
esp_efuse_mac_get_default(sta_mac);
|
2016-11-15 05:36:18 -05:00
|
|
|
err = nvs_set_blob(handle, PHY_CAL_MAC_KEY, sta_mac, sizeof(sta_mac));
|
|
|
|
if (err != ESP_OK) {
|
2018-05-03 03:48:14 -04:00
|
|
|
ESP_LOGE(TAG, "%s: store calibration mac failed(0x%x)\n", __func__, err);
|
2016-11-15 05:36:18 -05:00
|
|
|
return err;
|
|
|
|
}
|
2018-05-03 03:48:14 -04:00
|
|
|
|
|
|
|
uint32_t cal_format_version = phy_get_rf_cal_version() & (~BIT(16));
|
|
|
|
ESP_LOGV(TAG, "phy_get_rf_cal_version: %d\n", cal_format_version);
|
|
|
|
err = nvs_set_u32(handle, PHY_CAL_VERSION_KEY, cal_format_version);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "%s: store calibration version failed(0x%x)\n", __func__, err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = nvs_commit(handle);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "%s: store calibration nvs commit failed(0x%x)\n", __func__, err);
|
|
|
|
}
|
2019-05-09 23:34:06 -04:00
|
|
|
|
2016-11-15 05:36:18 -05:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2019-04-30 06:51:55 -04:00
|
|
|
#if CONFIG_ESP32_REDUCE_PHY_TX_POWER
|
2020-01-16 22:47:08 -05:00
|
|
|
// TODO: fix the esp_phy_reduce_tx_power unused warning for esp32s2 - IDF-759
|
2019-07-07 21:16:06 -04:00
|
|
|
static void __attribute((unused)) esp_phy_reduce_tx_power(esp_phy_init_data_t* init_data)
|
2018-07-25 05:28:56 -04:00
|
|
|
{
|
|
|
|
uint8_t i;
|
2019-05-09 23:34:06 -04:00
|
|
|
|
2018-07-25 05:28:56 -04:00
|
|
|
for(i = 0; i < PHY_TX_POWER_NUM; i++) {
|
|
|
|
// LOWEST_PHY_TX_POWER is the lowest tx power
|
2019-05-09 23:34:06 -04:00
|
|
|
init_data->params[PHY_TX_POWER_OFFSET+i] = PHY_TX_POWER_LOWEST;
|
2018-07-25 05:28:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-05-22 05:15:28 -04:00
|
|
|
void esp_phy_load_cal_and_init(void)
|
2017-02-16 06:05:07 -05:00
|
|
|
{
|
2020-11-13 03:11:11 -05:00
|
|
|
char * phy_version = get_phy_version_str();
|
|
|
|
ESP_LOGI(TAG, "phy_version %s", phy_version);
|
|
|
|
|
2022-01-09 22:56:59 -05:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32S2
|
|
|
|
phy_eco_version_sel(esp_efuse_get_chip_ver());
|
|
|
|
#endif
|
|
|
|
|
2017-06-15 06:37:53 -04:00
|
|
|
esp_phy_calibration_data_t* cal_data =
|
|
|
|
(esp_phy_calibration_data_t*) calloc(sizeof(esp_phy_calibration_data_t), 1);
|
|
|
|
if (cal_data == NULL) {
|
|
|
|
ESP_LOGE(TAG, "failed to allocate memory for RF calibration data");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2019-04-30 06:51:55 -04:00
|
|
|
#if CONFIG_ESP32_REDUCE_PHY_TX_POWER
|
2018-07-25 05:28:56 -04:00
|
|
|
const esp_phy_init_data_t* phy_init_data = esp_phy_get_init_data();
|
|
|
|
if (phy_init_data == NULL) {
|
|
|
|
ESP_LOGE(TAG, "failed to obtain PHY init data");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_phy_init_data_t* init_data = (esp_phy_init_data_t*) malloc(sizeof(esp_phy_init_data_t));
|
|
|
|
if (init_data == NULL) {
|
|
|
|
ESP_LOGE(TAG, "failed to allocate memory for phy init data");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(init_data, phy_init_data, sizeof(esp_phy_init_data_t));
|
2019-06-16 23:50:37 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
// ToDo: remove once esp_reset_reason is supported on esp32s2
|
2018-07-25 05:28:56 -04:00
|
|
|
if (esp_reset_reason() == ESP_RST_BROWNOUT) {
|
|
|
|
esp_phy_reduce_tx_power(init_data);
|
|
|
|
}
|
2019-06-16 23:50:37 -04:00
|
|
|
#endif
|
2018-07-25 05:28:56 -04:00
|
|
|
#else
|
2017-02-16 06:05:07 -05:00
|
|
|
const esp_phy_init_data_t* init_data = esp_phy_get_init_data();
|
|
|
|
if (init_data == NULL) {
|
|
|
|
ESP_LOGE(TAG, "failed to obtain PHY init data");
|
|
|
|
abort();
|
|
|
|
}
|
2018-07-25 05:28:56 -04:00
|
|
|
#endif
|
2017-06-15 06:37:53 -04:00
|
|
|
|
2021-10-26 04:09:26 -04:00
|
|
|
#if CONFIG_ESP_PHY_ENABLE_USB
|
|
|
|
phy_bbpll_en_usb(true);
|
|
|
|
#endif
|
|
|
|
|
2018-01-03 22:48:24 -05:00
|
|
|
#ifdef CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE
|
|
|
|
esp_phy_calibration_mode_t calibration_mode = PHY_RF_CAL_PARTIAL;
|
2018-06-12 03:51:51 -04:00
|
|
|
uint8_t sta_mac[6];
|
2018-01-03 22:48:24 -05:00
|
|
|
if (rtc_get_reset_reason(0) == DEEPSLEEP_RESET) {
|
|
|
|
calibration_mode = PHY_RF_CAL_NONE;
|
|
|
|
}
|
2017-02-16 06:05:07 -05:00
|
|
|
esp_err_t err = esp_phy_load_cal_data_from_nvs(cal_data);
|
|
|
|
if (err != ESP_OK) {
|
2017-03-14 09:27:27 -04:00
|
|
|
ESP_LOGW(TAG, "failed to load RF calibration data (0x%x), falling back to full calibration", err);
|
2017-02-16 06:05:07 -05:00
|
|
|
calibration_mode = PHY_RF_CAL_FULL;
|
|
|
|
}
|
|
|
|
|
2018-06-12 03:51:51 -04:00
|
|
|
esp_efuse_mac_get_default(sta_mac);
|
|
|
|
memcpy(cal_data->mac, sta_mac, 6);
|
2020-05-22 05:15:28 -04:00
|
|
|
esp_err_t ret = register_chipv7_phy(init_data, cal_data, calibration_mode);
|
|
|
|
if (ret == ESP_CAL_DATA_CHECK_FAIL) {
|
|
|
|
ESP_LOGW(TAG, "saving new calibration data because of checksum failure, mode(%d)", calibration_mode);
|
|
|
|
}
|
2017-02-16 06:05:07 -05:00
|
|
|
|
2020-05-22 05:15:28 -04:00
|
|
|
if ((calibration_mode != PHY_RF_CAL_NONE && err != ESP_OK) ||
|
|
|
|
(calibration_mode != PHY_RF_CAL_FULL && ret == ESP_CAL_DATA_CHECK_FAIL)) {
|
2017-02-16 06:05:07 -05:00
|
|
|
err = esp_phy_store_cal_data_to_nvs(cal_data);
|
|
|
|
} else {
|
|
|
|
err = ESP_OK;
|
|
|
|
}
|
2017-02-16 09:06:02 -05:00
|
|
|
#else
|
2020-05-22 05:15:28 -04:00
|
|
|
register_chipv7_phy(init_data, cal_data, PHY_RF_CAL_FULL);
|
2017-02-16 09:06:02 -05:00
|
|
|
#endif
|
2017-06-15 06:37:53 -04:00
|
|
|
|
2019-04-30 06:51:55 -04:00
|
|
|
#if CONFIG_ESP32_REDUCE_PHY_TX_POWER
|
2018-07-25 05:28:56 -04:00
|
|
|
esp_phy_release_init_data(phy_init_data);
|
|
|
|
free(init_data);
|
|
|
|
#else
|
2018-01-03 22:48:24 -05:00
|
|
|
esp_phy_release_init_data(init_data);
|
2018-07-25 05:28:56 -04:00
|
|
|
#endif
|
2018-01-03 22:48:24 -05:00
|
|
|
|
2017-06-15 06:37:53 -04:00
|
|
|
free(cal_data); // PHY maintains a copy of calibration data, so we can free this
|
2017-02-16 06:05:07 -05:00
|
|
|
}
|
|
|
|
|
2019-10-15 23:22:50 -04:00
|
|
|
#if CONFIG_ESP32_SUPPORT_MULTIPLE_PHY_INIT_DATA_BIN
|
|
|
|
static esp_err_t phy_crc_check_init_data(uint8_t* init_data, const uint8_t* checksum, size_t init_data_length)
|
2020-11-10 02:40:01 -05:00
|
|
|
{
|
2019-10-15 23:22:50 -04:00
|
|
|
uint32_t crc_data = 0;
|
2020-06-19 03:08:38 -04:00
|
|
|
crc_data = esp_rom_crc32_le(crc_data, init_data, init_data_length);
|
2019-10-15 23:22:50 -04:00
|
|
|
uint32_t crc_size_conversion = htonl(crc_data);
|
|
|
|
|
|
|
|
if (crc_size_conversion != *(uint32_t*)(checksum)) {
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t phy_find_bin_type_according_country(const char* country)
|
|
|
|
{
|
|
|
|
uint32_t i = 0;
|
|
|
|
uint8_t phy_init_data_type = 0;
|
|
|
|
|
2020-11-10 02:40:01 -05:00
|
|
|
for (i = 0; i < sizeof(s_country_code_map_type_table)/sizeof(phy_country_to_bin_type_t); i++)
|
2019-10-15 23:22:50 -04:00
|
|
|
{
|
|
|
|
if (!memcmp(country, s_country_code_map_type_table[i].cc, sizeof(s_phy_current_country))) {
|
|
|
|
phy_init_data_type = s_country_code_map_type_table[i].type;
|
|
|
|
ESP_LOGD(TAG, "Current country is %c%c, PHY init data type is %s\n", s_country_code_map_type_table[i].cc[0],
|
|
|
|
s_country_code_map_type_table[i].cc[1], s_phy_type[s_country_code_map_type_table[i].type]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == sizeof(s_country_code_map_type_table)/sizeof(phy_country_to_bin_type_t)) {
|
|
|
|
phy_init_data_type = ESP_PHY_INIT_DATA_TYPE_DEFAULT;
|
|
|
|
ESP_LOGW(TAG, "Use the default certification code beacuse %c%c doesn't have a certificate", country[0], country[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return phy_init_data_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t phy_find_bin_data_according_type(uint8_t* out_init_data_store,
|
|
|
|
const phy_control_info_data_t* init_data_control_info,
|
|
|
|
const uint8_t* init_data_multiple,
|
|
|
|
phy_init_data_type_t init_data_type)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < init_data_control_info->number; i++) {
|
|
|
|
if (init_data_type == *(init_data_multiple + (i * sizeof(esp_phy_init_data_t)) + PHY_INIT_DATA_TYPE_OFFSET)) {
|
|
|
|
memcpy(out_init_data_store + sizeof(phy_init_magic_pre),
|
|
|
|
init_data_multiple + (i * sizeof(esp_phy_init_data_t)), sizeof(esp_phy_init_data_t));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == init_data_control_info->number) {
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t phy_get_multiple_init_data(const esp_partition_t* partition,
|
|
|
|
uint8_t* init_data_store,
|
|
|
|
size_t init_data_store_length,
|
|
|
|
phy_init_data_type_t init_data_type)
|
|
|
|
{
|
|
|
|
phy_control_info_data_t* init_data_control_info = (phy_control_info_data_t*) malloc(sizeof(phy_control_info_data_t));
|
|
|
|
if (init_data_control_info == NULL) {
|
|
|
|
ESP_LOGE(TAG, "failed to allocate memory for PHY init data control info");
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2021-05-30 04:32:06 -04:00
|
|
|
esp_err_t err = ESP_OK;
|
|
|
|
#if CONFIG_ESP32_MULTIPLE_PHY_DATA_BIN_EMBEDDED
|
|
|
|
memcpy(init_data_control_info, multi_phy_init_data_bin_start + init_data_store_length, sizeof(phy_control_info_data_t));
|
|
|
|
#else
|
|
|
|
err = esp_partition_read(partition, init_data_store_length, init_data_control_info, sizeof(phy_control_info_data_t));
|
2019-10-15 23:22:50 -04:00
|
|
|
if (err != ESP_OK) {
|
|
|
|
free(init_data_control_info);
|
|
|
|
ESP_LOGE(TAG, "failed to read PHY control info data partition (0x%x)", err);
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2021-05-30 04:32:06 -04:00
|
|
|
#endif
|
2019-10-15 23:22:50 -04:00
|
|
|
if ((init_data_control_info->check_algorithm) == PHY_CRC_ALGORITHM) {
|
|
|
|
err = phy_crc_check_init_data(init_data_control_info->multiple_bin_checksum, init_data_control_info->control_info_checksum,
|
|
|
|
sizeof(phy_control_info_data_t) - sizeof(init_data_control_info->control_info_checksum));
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
free(init_data_control_info);
|
|
|
|
ESP_LOGE(TAG, "PHY init data control info check error");
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
free(init_data_control_info);
|
|
|
|
ESP_LOGE(TAG, "Check algorithm not CRC, PHY init data update failed");
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* init_data_multiple = (uint8_t*) malloc(sizeof(esp_phy_init_data_t) * init_data_control_info->number);
|
|
|
|
if (init_data_multiple == NULL) {
|
|
|
|
free(init_data_control_info);
|
|
|
|
ESP_LOGE(TAG, "failed to allocate memory for PHY init data multiple bin");
|
|
|
|
return ESP_FAIL;
|
2020-11-10 02:40:01 -05:00
|
|
|
}
|
|
|
|
|
2021-05-30 04:32:06 -04:00
|
|
|
#if CONFIG_ESP32_MULTIPLE_PHY_DATA_BIN_EMBEDDED
|
|
|
|
memcpy(init_data_multiple, multi_phy_init_data_bin_start + init_data_store_length + sizeof(phy_control_info_data_t), sizeof(esp_phy_init_data_t) * init_data_control_info->number);
|
|
|
|
#else
|
2019-10-15 23:22:50 -04:00
|
|
|
err = esp_partition_read(partition, init_data_store_length + sizeof(phy_control_info_data_t),
|
|
|
|
init_data_multiple, sizeof(esp_phy_init_data_t) * init_data_control_info->number);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
free(init_data_multiple);
|
|
|
|
free(init_data_control_info);
|
|
|
|
ESP_LOGE(TAG, "failed to read PHY init data multiple bin partition (0x%x)", err);
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2021-05-30 04:32:06 -04:00
|
|
|
#endif
|
2019-10-15 23:22:50 -04:00
|
|
|
if ((init_data_control_info->check_algorithm) == PHY_CRC_ALGORITHM) {
|
2020-11-10 02:40:01 -05:00
|
|
|
err = phy_crc_check_init_data(init_data_multiple, init_data_control_info->multiple_bin_checksum,
|
2019-10-15 23:22:50 -04:00
|
|
|
sizeof(esp_phy_init_data_t) * init_data_control_info->number);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
free(init_data_multiple);
|
|
|
|
free(init_data_control_info);
|
|
|
|
ESP_LOGE(TAG, "PHY init data multiple bin check error");
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
free(init_data_multiple);
|
|
|
|
free(init_data_control_info);
|
|
|
|
ESP_LOGE(TAG, "Check algorithm not CRC, PHY init data update failed");
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = phy_find_bin_data_according_type(init_data_store, init_data_control_info, init_data_multiple, init_data_type);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGW(TAG, "%s has not been certified, use DEFAULT PHY init data", s_phy_type[init_data_type]);
|
2020-11-10 02:40:01 -05:00
|
|
|
s_phy_init_data_type = ESP_PHY_INIT_DATA_TYPE_DEFAULT;
|
2019-10-15 23:22:50 -04:00
|
|
|
} else {
|
|
|
|
s_phy_init_data_type = init_data_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(init_data_multiple);
|
|
|
|
free(init_data_control_info);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t esp_phy_update_init_data(phy_init_data_type_t init_data_type)
|
|
|
|
{
|
2021-05-30 04:32:06 -04:00
|
|
|
#if CONFIG_ESP32_MULTIPLE_PHY_DATA_BIN_EMBEDDED
|
|
|
|
esp_err_t err = ESP_OK;
|
|
|
|
const esp_partition_t* partition = NULL;
|
|
|
|
size_t init_data_store_length = sizeof(phy_init_magic_pre) +
|
|
|
|
sizeof(esp_phy_init_data_t) + sizeof(phy_init_magic_post);
|
|
|
|
uint8_t* init_data_store = (uint8_t*) malloc(init_data_store_length);
|
|
|
|
if (init_data_store == NULL) {
|
|
|
|
ESP_LOGE(TAG, "failed to allocate memory for updated country code PHY init data");
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
memcpy(init_data_store, multi_phy_init_data_bin_start, init_data_store_length);
|
|
|
|
ESP_LOGI(TAG, "load embedded multi phy init data");
|
|
|
|
#else
|
2019-10-15 23:22:50 -04:00
|
|
|
const esp_partition_t* partition = esp_partition_find_first(
|
|
|
|
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_PHY, NULL);
|
|
|
|
if (partition == NULL) {
|
|
|
|
ESP_LOGE(TAG, "Updated country code PHY data partition not found");
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
size_t init_data_store_length = sizeof(phy_init_magic_pre) +
|
|
|
|
sizeof(esp_phy_init_data_t) + sizeof(phy_init_magic_post);
|
|
|
|
uint8_t* init_data_store = (uint8_t*) malloc(init_data_store_length);
|
|
|
|
if (init_data_store == NULL) {
|
|
|
|
ESP_LOGE(TAG, "failed to allocate memory for updated country code PHY init data");
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t err = esp_partition_read(partition, 0, init_data_store, init_data_store_length);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
free(init_data_store);
|
|
|
|
ESP_LOGE(TAG, "failed to read updated country code PHY data partition (0x%x)", err);
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2021-05-30 04:32:06 -04:00
|
|
|
#endif
|
2019-10-15 23:22:50 -04:00
|
|
|
if (memcmp(init_data_store, PHY_INIT_MAGIC, sizeof(phy_init_magic_pre)) != 0 ||
|
|
|
|
memcmp(init_data_store + init_data_store_length - sizeof(phy_init_magic_post),
|
|
|
|
PHY_INIT_MAGIC, sizeof(phy_init_magic_post)) != 0) {
|
|
|
|
free(init_data_store);
|
|
|
|
ESP_LOGE(TAG, "failed to validate updated country code PHY data partition");
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//find init data bin according init data type
|
|
|
|
if (init_data_type != ESP_PHY_INIT_DATA_TYPE_DEFAULT) {
|
|
|
|
err = phy_get_multiple_init_data(partition, init_data_store, init_data_store_length, init_data_type);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
free(init_data_store);
|
|
|
|
#if CONFIG_ESP32_PHY_INIT_DATA_ERROR
|
|
|
|
abort();
|
|
|
|
#else
|
|
|
|
return ESP_FAIL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s_phy_init_data_type = ESP_PHY_INIT_DATA_TYPE_DEFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s_current_apply_phy_init_data != s_phy_init_data_type) {
|
|
|
|
err = esp_phy_apply_phy_init_data(init_data_store + sizeof(phy_init_magic_pre));
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "PHY init data failed to load");
|
|
|
|
free(init_data_store);
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "PHY init data type updated from %s to %s",
|
|
|
|
s_phy_type[s_current_apply_phy_init_data], s_phy_type[s_phy_init_data_type]);
|
|
|
|
s_current_apply_phy_init_data = s_phy_init_data_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(init_data_store);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
esp_err_t esp_phy_update_country_info(const char *country)
|
|
|
|
{
|
|
|
|
#if CONFIG_ESP32_SUPPORT_MULTIPLE_PHY_INIT_DATA_BIN
|
|
|
|
uint8_t phy_init_data_type_map = 0;
|
2021-05-30 04:32:06 -04:00
|
|
|
|
|
|
|
if (!s_multiple_phy_init_data_bin) {
|
|
|
|
ESP_LOGD(TAG, "Does not support multiple PHY init data bins");
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
2019-10-15 23:22:50 -04:00
|
|
|
//if country equal s_phy_current_country, return;
|
|
|
|
if (!memcmp(country, s_phy_current_country, sizeof(s_phy_current_country))) {
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(s_phy_current_country, country, sizeof(s_phy_current_country));
|
2020-11-10 02:40:01 -05:00
|
|
|
|
2019-10-15 23:22:50 -04:00
|
|
|
phy_init_data_type_map = phy_find_bin_type_according_country(country);
|
|
|
|
if (phy_init_data_type_map == s_phy_init_data_type) {
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
2020-11-10 02:40:01 -05:00
|
|
|
|
2019-10-15 23:22:50 -04:00
|
|
|
esp_err_t err = esp_phy_update_init_data(phy_init_data_type_map);
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
2020-11-10 02:40:01 -05:00
|
|
|
#endif
|
2019-10-15 23:22:50 -04:00
|
|
|
return ESP_OK;
|
|
|
|
}
|