phy_init.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stddef.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <stdbool.h>
  18. #include <sys/lock.h>
  19. #include "rom/ets_sys.h"
  20. #include "rom/rtc.h"
  21. #include "soc/rtc.h"
  22. #include "soc/dport_reg.h"
  23. #include "esp_err.h"
  24. #include "esp_phy_init.h"
  25. #include "esp_system.h"
  26. #include "esp_log.h"
  27. #include "nvs.h"
  28. #include "nvs_flash.h"
  29. #include "sdkconfig.h"
  30. #include "freertos/FreeRTOS.h"
  31. #include "freertos/portmacro.h"
  32. #include "phy.h"
  33. #include "phy_init_data.h"
  34. #include "coexist_internal.h"
  35. #include "driver/periph_ctrl.h"
  36. #include "esp_wifi_internal.h"
  37. static const char* TAG = "phy_init";
  38. static _lock_t s_phy_rf_init_lock;
  39. /* Bit mask of modules needing to call phy_rf_init */
  40. static uint32_t s_module_phy_rf_init = 0;
  41. /* Whether modern sleep in turned on */
  42. static volatile bool s_is_phy_rf_en = false;
  43. /* Bit mask of modules needing to enter modem sleep mode */
  44. static uint32_t s_modem_sleep_module_enter = 0;
  45. /* Bit mask of modules which might use RF, system can enter modem
  46. * sleep mode only when all modules registered require to enter
  47. * modem sleep*/
  48. static uint32_t s_modem_sleep_module_register = 0;
  49. /* Whether modern sleep is turned on */
  50. static volatile bool s_is_modem_sleep_en = false;
  51. static _lock_t s_modem_sleep_lock;
  52. uint32_t IRAM_ATTR phy_enter_critical(void)
  53. {
  54. return portENTER_CRITICAL_NESTED();
  55. }
  56. void IRAM_ATTR phy_exit_critical(uint32_t level)
  57. {
  58. portEXIT_CRITICAL_NESTED(level);
  59. }
  60. esp_err_t esp_phy_rf_init(const esp_phy_init_data_t* init_data, esp_phy_calibration_mode_t mode,
  61. esp_phy_calibration_data_t* calibration_data, phy_rf_module_t module)
  62. {
  63. /* 3 modules may call phy_init: Wi-Fi, BT, Modem Sleep */
  64. if (module >= PHY_MODULE_COUNT){
  65. ESP_LOGE(TAG, "%s, invalid module parameter(%d), should be smaller than \
  66. module count(%d)", __func__, module, PHY_MODULE_COUNT);
  67. return ESP_ERR_INVALID_ARG;
  68. }
  69. _lock_acquire(&s_phy_rf_init_lock);
  70. uint32_t s_module_phy_rf_init_old = s_module_phy_rf_init;
  71. bool is_wifi_or_bt_enabled = !!(s_module_phy_rf_init_old & (BIT(PHY_BT_MODULE) | BIT(PHY_WIFI_MODULE)));
  72. esp_err_t status = ESP_OK;
  73. s_module_phy_rf_init |= BIT(module);
  74. if ((is_wifi_or_bt_enabled == false) && (module == PHY_MODEM_MODULE)){
  75. status = ESP_FAIL;
  76. }
  77. else if (s_is_phy_rf_en == true) {
  78. }
  79. else {
  80. /* If Wi-Fi, BT all disabled, modem sleep should not take effect;
  81. * If either Wi-Fi or BT is enabled, should allow modem sleep requires
  82. * to enter sleep;
  83. * If Wi-Fi, BT co-exist, it is disallowed that only one module
  84. * support modem sleep, E,g. BT support modem sleep but Wi-Fi not
  85. * support modem sleep;
  86. */
  87. if (is_wifi_or_bt_enabled == false){
  88. if ((module == PHY_BT_MODULE) || (module == PHY_WIFI_MODULE)){
  89. s_is_phy_rf_en = true;
  90. }
  91. }
  92. else {
  93. if (module == PHY_MODEM_MODULE){
  94. s_is_phy_rf_en = true;
  95. }
  96. else if ((module == PHY_BT_MODULE) || (module == PHY_WIFI_MODULE)){
  97. /* New module (BT or Wi-Fi) can init RF according to modem_sleep_exit */
  98. }
  99. }
  100. if (s_is_phy_rf_en == true){
  101. // Enable WiFi/BT common peripheral clock
  102. periph_module_enable(PERIPH_WIFI_BT_COMMON_MODULE);
  103. phy_set_wifi_mode_only(0);
  104. if (ESP_CAL_DATA_CHECK_FAIL == register_chipv7_phy(init_data, calibration_data, mode)) {
  105. ESP_LOGW(TAG, "saving new calibration data because of checksum failure, mode(%d)", mode);
  106. #ifdef CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE
  107. if (mode != PHY_RF_CAL_FULL) {
  108. esp_phy_store_cal_data_to_nvs(calibration_data);
  109. }
  110. #endif
  111. }
  112. extern esp_err_t wifi_osi_funcs_register(wifi_osi_funcs_t *osi_funcs);
  113. status = wifi_osi_funcs_register(&g_wifi_osi_funcs);
  114. if(status != ESP_OK) {
  115. return ESP_FAIL;
  116. }
  117. coex_bt_high_prio();
  118. }
  119. }
  120. #if CONFIG_SW_COEXIST_ENABLE
  121. if ((module == PHY_BT_MODULE) || (module == PHY_WIFI_MODULE)){
  122. uint32_t phy_bt_wifi_mask = BIT(PHY_BT_MODULE) | BIT(PHY_WIFI_MODULE);
  123. if ((s_module_phy_rf_init & phy_bt_wifi_mask) == phy_bt_wifi_mask) { //both wifi & bt enabled
  124. coex_init();
  125. coex_preference_set(CONFIG_SW_COEXIST_PREFERENCE_VALUE);
  126. coex_resume();
  127. }
  128. }
  129. #endif
  130. _lock_release(&s_phy_rf_init_lock);
  131. return status;
  132. }
  133. esp_err_t esp_phy_rf_deinit(phy_rf_module_t module)
  134. {
  135. /* 3 modules may call phy_init: Wi-Fi, BT, Modem Sleep */
  136. if (module >= PHY_MODULE_COUNT){
  137. ESP_LOGE(TAG, "%s, invalid module parameter(%d), should be smaller than \
  138. module count(%d)", __func__, module, PHY_MODULE_COUNT);
  139. return ESP_ERR_INVALID_ARG;
  140. }
  141. _lock_acquire(&s_phy_rf_init_lock);
  142. uint32_t s_module_phy_rf_init_old = s_module_phy_rf_init;
  143. uint32_t phy_bt_wifi_mask = BIT(PHY_BT_MODULE) | BIT(PHY_WIFI_MODULE);
  144. bool is_wifi_or_bt_enabled = !!(s_module_phy_rf_init_old & phy_bt_wifi_mask);
  145. bool is_both_wifi_bt_enabled = ((s_module_phy_rf_init_old & phy_bt_wifi_mask) == phy_bt_wifi_mask);
  146. s_module_phy_rf_init &= ~BIT(module);
  147. esp_err_t status = ESP_OK;
  148. #if CONFIG_SW_COEXIST_ENABLE
  149. if ((module == PHY_BT_MODULE) || (module == PHY_WIFI_MODULE)){
  150. if (is_both_wifi_bt_enabled == true) {
  151. coex_deinit();
  152. }
  153. }
  154. #endif
  155. if ((is_wifi_or_bt_enabled == false) && (module == PHY_MODEM_MODULE)){
  156. /* Modem sleep should not take effect in this case */
  157. status = ESP_FAIL;
  158. }
  159. else if (s_is_phy_rf_en == false) {
  160. //do nothing
  161. }
  162. else {
  163. if (is_wifi_or_bt_enabled == false){
  164. if ((module == PHY_BT_MODULE) || (module == PHY_WIFI_MODULE)){
  165. s_is_phy_rf_en = false;
  166. ESP_LOGE(TAG, "%s, RF should not be in enabled state if both Wi-Fi and BT are disabled", __func__);
  167. }
  168. }
  169. else {
  170. if (module == PHY_MODEM_MODULE){
  171. s_is_phy_rf_en = false;
  172. }
  173. else if ((module == PHY_BT_MODULE) || (module == PHY_WIFI_MODULE)){
  174. s_is_phy_rf_en = is_both_wifi_bt_enabled ? true : false;
  175. }
  176. }
  177. if (s_is_phy_rf_en == false) {
  178. // Disable PHY and RF.
  179. phy_close_rf();
  180. // Disable WiFi/BT common peripheral clock. Do not disable clock for hardware RNG
  181. periph_module_disable(PERIPH_WIFI_BT_COMMON_MODULE);
  182. }
  183. }
  184. _lock_release(&s_phy_rf_init_lock);
  185. return status;
  186. }
  187. esp_err_t esp_modem_sleep_enter(modem_sleep_module_t module)
  188. {
  189. #if CONFIG_SW_COEXIST_ENABLE
  190. uint32_t phy_bt_wifi_mask = BIT(PHY_BT_MODULE) | BIT(PHY_WIFI_MODULE);
  191. #endif
  192. if (module >= MODEM_MODULE_COUNT){
  193. ESP_LOGE(TAG, "%s, invalid module parameter(%d), should be smaller than \
  194. module count(%d)", __func__, module, MODEM_MODULE_COUNT);
  195. return ESP_ERR_INVALID_ARG;
  196. }
  197. else if (!(s_modem_sleep_module_register & BIT(module))){
  198. ESP_LOGW(TAG, "%s, module (%d) has not been registered", __func__, module);
  199. return ESP_ERR_INVALID_ARG;
  200. }
  201. else {
  202. _lock_acquire(&s_modem_sleep_lock);
  203. s_modem_sleep_module_enter |= BIT(module);
  204. #if CONFIG_SW_COEXIST_ENABLE
  205. _lock_acquire(&s_phy_rf_init_lock);
  206. if (((s_module_phy_rf_init & phy_bt_wifi_mask) == phy_bt_wifi_mask) //both wifi & bt enabled
  207. && (s_modem_sleep_module_enter & (MODEM_BT_MASK | MODEM_WIFI_MASK)) != 0){
  208. coex_pause();
  209. }
  210. _lock_release(&s_phy_rf_init_lock);
  211. #endif
  212. if (!s_is_modem_sleep_en && (s_modem_sleep_module_enter == s_modem_sleep_module_register)){
  213. esp_err_t status = esp_phy_rf_deinit(PHY_MODEM_MODULE);
  214. if (status == ESP_OK){
  215. s_is_modem_sleep_en = true;
  216. }
  217. }
  218. _lock_release(&s_modem_sleep_lock);
  219. return ESP_OK;
  220. }
  221. }
  222. esp_err_t esp_modem_sleep_exit(modem_sleep_module_t module)
  223. {
  224. #if CONFIG_SW_COEXIST_ENABLE
  225. uint32_t phy_bt_wifi_mask = BIT(PHY_BT_MODULE) | BIT(PHY_WIFI_MODULE);
  226. #endif
  227. if (module >= MODEM_MODULE_COUNT){
  228. ESP_LOGE(TAG, "%s, invalid module parameter(%d), should be smaller than \
  229. module count(%d)", __func__, module, MODEM_MODULE_COUNT);
  230. return ESP_ERR_INVALID_ARG;
  231. }
  232. else if (!(s_modem_sleep_module_register & BIT(module))){
  233. ESP_LOGW(TAG, "%s, module (%d) has not been registered", __func__, module);
  234. return ESP_ERR_INVALID_ARG;
  235. }
  236. else {
  237. _lock_acquire(&s_modem_sleep_lock);
  238. s_modem_sleep_module_enter &= ~BIT(module);
  239. if (s_is_modem_sleep_en){
  240. esp_err_t status = esp_phy_rf_init(NULL,PHY_RF_CAL_NONE,NULL, PHY_MODEM_MODULE);
  241. if (status == ESP_OK){
  242. s_is_modem_sleep_en = false;
  243. }
  244. }
  245. #if CONFIG_SW_COEXIST_ENABLE
  246. _lock_acquire(&s_phy_rf_init_lock);
  247. if (((s_module_phy_rf_init & phy_bt_wifi_mask) == phy_bt_wifi_mask) //both wifi & bt enabled
  248. && (s_modem_sleep_module_enter & (MODEM_BT_MASK | MODEM_WIFI_MASK)) == 0){
  249. coex_resume();
  250. }
  251. _lock_release(&s_phy_rf_init_lock);
  252. #endif
  253. _lock_release(&s_modem_sleep_lock);
  254. return ESP_OK;
  255. }
  256. return ESP_OK;
  257. }
  258. esp_err_t esp_modem_sleep_register(modem_sleep_module_t module)
  259. {
  260. if (module >= MODEM_MODULE_COUNT){
  261. ESP_LOGE(TAG, "%s, invalid module parameter(%d), should be smaller than \
  262. module count(%d)", __func__, module, MODEM_MODULE_COUNT);
  263. return ESP_ERR_INVALID_ARG;
  264. }
  265. else if (s_modem_sleep_module_register & BIT(module)){
  266. ESP_LOGI(TAG, "%s, multiple registration of module (%d)", __func__, module);
  267. return ESP_OK;
  268. }
  269. else{
  270. _lock_acquire(&s_modem_sleep_lock);
  271. s_modem_sleep_module_register |= BIT(module);
  272. /* The module is set to enter modem sleep by default, otherwise will prevent
  273. * other modules from entering sleep mode if this module never call enter sleep function
  274. * in the future */
  275. s_modem_sleep_module_enter |= BIT(module);
  276. _lock_release(&s_modem_sleep_lock);
  277. return ESP_OK;
  278. }
  279. }
  280. esp_err_t esp_modem_sleep_deregister(modem_sleep_module_t module)
  281. {
  282. if (module >= MODEM_MODULE_COUNT){
  283. ESP_LOGE(TAG, "%s, invalid module parameter(%d), should be smaller than \
  284. module count(%d)", __func__, module, MODEM_MODULE_COUNT);
  285. return ESP_ERR_INVALID_ARG;
  286. }
  287. else if (!(s_modem_sleep_module_register & BIT(module))){
  288. ESP_LOGI(TAG, "%s, module (%d) has not been registered", __func__, module);
  289. return ESP_OK;
  290. }
  291. else{
  292. _lock_acquire(&s_modem_sleep_lock);
  293. s_modem_sleep_module_enter &= ~BIT(module);
  294. s_modem_sleep_module_register &= ~BIT(module);
  295. if (s_modem_sleep_module_register == 0){
  296. s_modem_sleep_module_enter = 0;
  297. /* Once all module are de-registered and current state
  298. * is modem sleep mode, we need to turn off modem sleep
  299. */
  300. if (s_is_modem_sleep_en == true){
  301. s_is_modem_sleep_en = false;
  302. esp_phy_rf_init(NULL,PHY_RF_CAL_NONE,NULL, PHY_MODEM_MODULE);
  303. }
  304. }
  305. _lock_release(&s_modem_sleep_lock);
  306. return ESP_OK;
  307. }
  308. }
  309. // PHY init data handling functions
  310. #if CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION
  311. #include "esp_partition.h"
  312. const esp_phy_init_data_t* esp_phy_get_init_data()
  313. {
  314. const esp_partition_t* partition = esp_partition_find_first(
  315. ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_PHY, NULL);
  316. if (partition == NULL) {
  317. ESP_LOGE(TAG, "PHY data partition not found");
  318. return NULL;
  319. }
  320. ESP_LOGD(TAG, "loading PHY init data from partition at offset 0x%x", partition->address);
  321. size_t init_data_store_length = sizeof(phy_init_magic_pre) +
  322. sizeof(esp_phy_init_data_t) + sizeof(phy_init_magic_post);
  323. uint8_t* init_data_store = (uint8_t*) malloc(init_data_store_length);
  324. if (init_data_store == NULL) {
  325. ESP_LOGE(TAG, "failed to allocate memory for PHY init data");
  326. return NULL;
  327. }
  328. esp_err_t err = esp_partition_read(partition, 0, init_data_store, init_data_store_length);
  329. if (err != ESP_OK) {
  330. ESP_LOGE(TAG, "failed to read PHY data partition (0x%x)", err);
  331. return NULL;
  332. }
  333. if (memcmp(init_data_store, PHY_INIT_MAGIC, sizeof(phy_init_magic_pre)) != 0 ||
  334. memcmp(init_data_store + init_data_store_length - sizeof(phy_init_magic_post),
  335. PHY_INIT_MAGIC, sizeof(phy_init_magic_post)) != 0) {
  336. ESP_LOGE(TAG, "failed to validate PHY data partition");
  337. return NULL;
  338. }
  339. ESP_LOGD(TAG, "PHY data partition validated");
  340. return (const esp_phy_init_data_t*) (init_data_store + sizeof(phy_init_magic_pre));
  341. }
  342. void esp_phy_release_init_data(const esp_phy_init_data_t* init_data)
  343. {
  344. free((uint8_t*) init_data - sizeof(phy_init_magic_pre));
  345. }
  346. #else // CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION
  347. // phy_init_data.h will declare static 'phy_init_data' variable initialized with default init data
  348. const esp_phy_init_data_t* esp_phy_get_init_data()
  349. {
  350. ESP_LOGD(TAG, "loading PHY init data from application binary");
  351. return &phy_init_data;
  352. }
  353. void esp_phy_release_init_data(const esp_phy_init_data_t* init_data)
  354. {
  355. // no-op
  356. }
  357. #endif // CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION
  358. // PHY calibration data handling functions
  359. static const char* PHY_NAMESPACE = "phy";
  360. static const char* PHY_CAL_VERSION_KEY = "cal_version";
  361. static const char* PHY_CAL_MAC_KEY = "cal_mac";
  362. static const char* PHY_CAL_DATA_KEY = "cal_data";
  363. static esp_err_t load_cal_data_from_nvs_handle(nvs_handle handle,
  364. esp_phy_calibration_data_t* out_cal_data);
  365. static esp_err_t store_cal_data_to_nvs_handle(nvs_handle handle,
  366. const esp_phy_calibration_data_t* cal_data);
  367. esp_err_t esp_phy_load_cal_data_from_nvs(esp_phy_calibration_data_t* out_cal_data)
  368. {
  369. nvs_handle handle;
  370. esp_err_t err = nvs_open(PHY_NAMESPACE, NVS_READONLY, &handle);
  371. if (err == ESP_ERR_NVS_NOT_INITIALIZED) {
  372. ESP_LOGE(TAG, "%s: NVS has not been initialized. "
  373. "Call nvs_flash_init before starting WiFi/BT.", __func__);
  374. } else if (err != ESP_OK) {
  375. ESP_LOGD(TAG, "%s: failed to open NVS namespace (0x%x)", __func__, err);
  376. return err;
  377. }
  378. err = load_cal_data_from_nvs_handle(handle, out_cal_data);
  379. nvs_close(handle);
  380. return err;
  381. }
  382. esp_err_t esp_phy_store_cal_data_to_nvs(const esp_phy_calibration_data_t* cal_data)
  383. {
  384. nvs_handle handle;
  385. esp_err_t err = nvs_open(PHY_NAMESPACE, NVS_READWRITE, &handle);
  386. if (err != ESP_OK) {
  387. ESP_LOGD(TAG, "%s: failed to open NVS namespace (0x%x)", __func__, err);
  388. return err;
  389. }
  390. else {
  391. err = store_cal_data_to_nvs_handle(handle, cal_data);
  392. nvs_close(handle);
  393. return err;
  394. }
  395. }
  396. static esp_err_t load_cal_data_from_nvs_handle(nvs_handle handle,
  397. esp_phy_calibration_data_t* out_cal_data)
  398. {
  399. esp_err_t err;
  400. uint32_t cal_data_version;
  401. err = nvs_get_u32(handle, PHY_CAL_VERSION_KEY, &cal_data_version);
  402. if (err != ESP_OK) {
  403. ESP_LOGD(TAG, "%s: failed to get cal_version (0x%x)", __func__, err);
  404. return err;
  405. }
  406. uint32_t cal_format_version = phy_get_rf_cal_version() & (~BIT(16));
  407. ESP_LOGV(TAG, "phy_get_rf_cal_version: %d\n", cal_format_version);
  408. if (cal_data_version != cal_format_version) {
  409. ESP_LOGD(TAG, "%s: expected calibration data format %d, found %d",
  410. __func__, cal_format_version, cal_data_version);
  411. return ESP_FAIL;
  412. }
  413. uint8_t cal_data_mac[6];
  414. size_t length = sizeof(cal_data_mac);
  415. err = nvs_get_blob(handle, PHY_CAL_MAC_KEY, cal_data_mac, &length);
  416. if (err != ESP_OK) {
  417. ESP_LOGD(TAG, "%s: failed to get cal_mac (0x%x)", __func__, err);
  418. return err;
  419. }
  420. if (length != sizeof(cal_data_mac)) {
  421. ESP_LOGD(TAG, "%s: invalid length of cal_mac (%d)", __func__, length);
  422. return ESP_ERR_INVALID_SIZE;
  423. }
  424. uint8_t sta_mac[6];
  425. esp_efuse_mac_get_default(sta_mac);
  426. if (memcmp(sta_mac, cal_data_mac, sizeof(sta_mac)) != 0) {
  427. ESP_LOGE(TAG, "%s: calibration data MAC check failed: expected " \
  428. MACSTR ", found " MACSTR,
  429. __func__, MAC2STR(sta_mac), MAC2STR(cal_data_mac));
  430. return ESP_FAIL;
  431. }
  432. length = sizeof(*out_cal_data);
  433. err = nvs_get_blob(handle, PHY_CAL_DATA_KEY, out_cal_data, &length);
  434. if (err != ESP_OK) {
  435. ESP_LOGE(TAG, "%s: failed to get cal_data(0x%x)", __func__, err);
  436. return err;
  437. }
  438. if (length != sizeof(*out_cal_data)) {
  439. ESP_LOGD(TAG, "%s: invalid length of cal_data (%d)", __func__, length);
  440. return ESP_ERR_INVALID_SIZE;
  441. }
  442. memcpy(out_cal_data->mac, sta_mac, 6);
  443. return ESP_OK;
  444. }
  445. static esp_err_t store_cal_data_to_nvs_handle(nvs_handle handle,
  446. const esp_phy_calibration_data_t* cal_data)
  447. {
  448. esp_err_t err;
  449. err = nvs_set_blob(handle, PHY_CAL_DATA_KEY, cal_data, sizeof(*cal_data));
  450. if (err != ESP_OK) {
  451. ESP_LOGE(TAG, "%s: store calibration data failed(0x%x)\n", __func__, err);
  452. return err;
  453. }
  454. uint8_t sta_mac[6];
  455. esp_efuse_mac_get_default(sta_mac);
  456. err = nvs_set_blob(handle, PHY_CAL_MAC_KEY, sta_mac, sizeof(sta_mac));
  457. if (err != ESP_OK) {
  458. ESP_LOGE(TAG, "%s: store calibration mac failed(0x%x)\n", __func__, err);
  459. return err;
  460. }
  461. uint32_t cal_format_version = phy_get_rf_cal_version() & (~BIT(16));
  462. ESP_LOGV(TAG, "phy_get_rf_cal_version: %d\n", cal_format_version);
  463. err = nvs_set_u32(handle, PHY_CAL_VERSION_KEY, cal_format_version);
  464. if (err != ESP_OK) {
  465. ESP_LOGE(TAG, "%s: store calibration version failed(0x%x)\n", __func__, err);
  466. return err;
  467. }
  468. err = nvs_commit(handle);
  469. if (err != ESP_OK) {
  470. ESP_LOGE(TAG, "%s: store calibration nvs commit failed(0x%x)\n", __func__, err);
  471. }
  472. return err;
  473. }
  474. void esp_phy_load_cal_and_init(phy_rf_module_t module)
  475. {
  476. esp_phy_calibration_data_t* cal_data =
  477. (esp_phy_calibration_data_t*) calloc(sizeof(esp_phy_calibration_data_t), 1);
  478. if (cal_data == NULL) {
  479. ESP_LOGE(TAG, "failed to allocate memory for RF calibration data");
  480. abort();
  481. }
  482. const esp_phy_init_data_t* init_data = esp_phy_get_init_data();
  483. if (init_data == NULL) {
  484. ESP_LOGE(TAG, "failed to obtain PHY init data");
  485. abort();
  486. }
  487. #ifdef CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE
  488. esp_phy_calibration_mode_t calibration_mode = PHY_RF_CAL_PARTIAL;
  489. if (rtc_get_reset_reason(0) == DEEPSLEEP_RESET) {
  490. calibration_mode = PHY_RF_CAL_NONE;
  491. }
  492. esp_err_t err = esp_phy_load_cal_data_from_nvs(cal_data);
  493. if (err != ESP_OK) {
  494. ESP_LOGW(TAG, "failed to load RF calibration data (0x%x), falling back to full calibration", err);
  495. calibration_mode = PHY_RF_CAL_FULL;
  496. }
  497. esp_phy_rf_init(init_data, calibration_mode, cal_data, module);
  498. if (calibration_mode != PHY_RF_CAL_NONE && err != ESP_OK) {
  499. err = esp_phy_store_cal_data_to_nvs(cal_data);
  500. } else {
  501. err = ESP_OK;
  502. }
  503. #else
  504. esp_phy_rf_init(init_data, PHY_RF_CAL_FULL, cal_data, module);
  505. #endif
  506. esp_phy_release_init_data(init_data);
  507. free(cal_data); // PHY maintains a copy of calibration data, so we can free this
  508. }