]> granicus.if.org Git - esp-idf/commitdiff
esp32: fix potential PHY calibration bug
authorLiu Zhi Fu <liuzhifu@espressif.com>
Thu, 3 May 2018 07:48:14 +0000 (15:48 +0800)
committerLiu Zhi Fu <liuzhifu@espressif.com>
Mon, 7 May 2018 02:03:51 +0000 (10:03 +0800)
1. Add error log if failed to store calibration version/mac/data
2. Change the NVS calibration version/mac/data store sequence
3. Pass the init_data instead of NULL to esp_phy_rf_init() in esp_phy_load_cal_and_init()

components/esp32/phy_init.c

index 5a007ce5ddd68a148808674101ec06b12475a640..29c8f06a6484b43e9282226fd52537b7b1bdbf63 100644 (file)
@@ -241,19 +241,34 @@ static esp_err_t store_cal_data_to_nvs_handle(nvs_handle handle,
         const esp_phy_calibration_data_t* cal_data)
 {
     esp_err_t err;
-    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);
+
+    err = nvs_set_blob(handle, PHY_CAL_DATA_KEY, cal_data, sizeof(*cal_data));
     if (err != ESP_OK) {
+        ESP_LOGE(TAG, "%s: store calibration data failed(0x%x)\n", __func__, err);
         return err;
     }
+
     uint8_t sta_mac[6];
     esp_efuse_mac_get_default(sta_mac);
     err = nvs_set_blob(handle, PHY_CAL_MAC_KEY, sta_mac, sizeof(sta_mac));
     if (err != ESP_OK) {
+        ESP_LOGE(TAG, "%s: store calibration mac failed(0x%x)\n", __func__, err);
         return err;
     }
-    err = nvs_set_blob(handle, PHY_CAL_DATA_KEY, cal_data, sizeof(*cal_data));
+
+    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);
+    }
+    
     return err;
 }
 
@@ -266,17 +281,18 @@ void esp_phy_load_cal_and_init(void)
         abort();
     }
 
-#ifdef CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE
-    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) {
         ESP_LOGE(TAG, "failed to obtain PHY init data");
         abort();
     }
 
+#ifdef CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE
+    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;
+    }
+
     esp_err_t err = esp_phy_load_cal_data_from_nvs(cal_data);
     if (err != ESP_OK) {
         ESP_LOGW(TAG, "failed to load RF calibration data (0x%x), falling back to full calibration", err);
@@ -292,7 +308,7 @@ void esp_phy_load_cal_and_init(void)
     }
     esp_phy_release_init_data(init_data);
 #else
-    esp_phy_rf_init(NULL, PHY_RF_CAL_FULL, cal_data);
+    esp_phy_rf_init(init_data, PHY_RF_CAL_FULL, cal_data);
 #endif
 
     free(cal_data); // PHY maintains a copy of calibration data, so we can free this