diff --git a/cores/esp32/esp32-hal-misc.c b/cores/esp32/esp32-hal-misc.c index 9cf2b800..ce9eb552 100644 --- a/cores/esp32/esp32-hal-misc.c +++ b/cores/esp32/esp32-hal-misc.c @@ -18,6 +18,51 @@ #include "freertos/task.h" #include "esp_attr.h" +#if !CONFIG_ESP32_PHY_AUTO_INIT +#include "nvs_flash.h" +#include "esp_phy_init.h" +#include "rom/rtc.h" +void arduino_phy_init() +{ + static bool initialized = false; + if(initialized){ + return; + } + nvs_flash_init(); + esp_phy_calibration_mode_t calibration_mode = PHY_RF_CAL_PARTIAL; + if (rtc_get_reset_reason(0) == DEEPSLEEP_RESET) { + calibration_mode = PHY_RF_CAL_NONE; + } + const esp_phy_init_data_t* init_data = esp_phy_get_init_data(); + if (init_data == NULL) { + printf("failed to obtain PHY init data\n"); + abort(); + } + esp_phy_calibration_data_t* cal_data = + (esp_phy_calibration_data_t*) calloc(sizeof(esp_phy_calibration_data_t), 1); + if (cal_data == NULL) { + printf("failed to allocate memory for RF calibration data\n"); + abort(); + } + esp_err_t err = esp_phy_load_cal_data_from_nvs(cal_data); + if (err != ESP_OK) { + printf("failed to load RF calibration data, falling back to full calibration\n"); + calibration_mode = PHY_RF_CAL_FULL; + } + + esp_phy_init(init_data, calibration_mode, cal_data); + + if (calibration_mode != PHY_RF_CAL_NONE) { + err = esp_phy_store_cal_data_to_nvs(cal_data); + } else { + err = ESP_OK; + } + esp_phy_release_init_data(init_data); + free(cal_data); // PHY maintains a copy of calibration data, so we can free this + initialized = true; +} +#endif + uint32_t IRAM_ATTR micros() { uint32_t ccount; diff --git a/cores/esp32/esp32-hal.h b/cores/esp32/esp32-hal.h index 3f042500..3304f6c2 100644 --- a/cores/esp32/esp32-hal.h +++ b/cores/esp32/esp32-hal.h @@ -65,6 +65,10 @@ uint32_t millis(); void delay(uint32_t); void delayMicroseconds(uint32_t us); +#if !CONFIG_ESP32_PHY_AUTO_INIT +void arduino_phy_init(); +#endif + #if !CONFIG_AUTOSTART_ARDUINO void initArduino(); #endif diff --git a/libraries/WiFi/src/WiFiGeneric.cpp b/libraries/WiFi/src/WiFiGeneric.cpp index 2fde8e4f..a8a1a07f 100644 --- a/libraries/WiFi/src/WiFiGeneric.cpp +++ b/libraries/WiFi/src/WiFiGeneric.cpp @@ -52,6 +52,9 @@ extern "C" { static bool _esp_wifi_initalized = false; extern void initWiFi() { +#if !CONFIG_ESP32_PHY_AUTO_INIT + arduino_phy_init(); +#endif wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); tcpip_adapter_init(); esp_event_loop_init(&WiFiGenericClass::_eventCallback, NULL); diff --git a/tools/sdk/include/config/sdkconfig.h b/tools/sdk/include/config/sdkconfig.h index 26d934a5..17e2421e 100644 --- a/tools/sdk/include/config/sdkconfig.h +++ b/tools/sdk/include/config/sdkconfig.h @@ -58,7 +58,7 @@ #define CONFIG_PARTITION_TABLE_CUSTOM_FILENAME "partitions.csv" #define CONFIG_FREERTOS_ISR_STACKSIZE 1536 #define CONFIG_OPTIMIZATION_LEVEL_DEBUG 1 -#define CONFIG_ESP32_PHY_AUTO_INIT 1 +#define CONFIG_ESP32_PHY_AUTO_INIT 0 #define CONFIG_SYSTEM_EVENT_QUEUE_SIZE 32 #define CONFIG_ESPTOOLPY_BAUD_921600B 1 #define CONFIG_APP_OFFSET 0x10000 diff --git a/tools/sdk/lib/libesp32.a b/tools/sdk/lib/libesp32.a index ae56232e..398af669 100644 Binary files a/tools/sdk/lib/libesp32.a and b/tools/sdk/lib/libesp32.a differ