phy_init.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. ESP_LOGE(TAG, "failed to register wifi os adapter, ret(%d)", status);
  116. _lock_release(&s_phy_rf_init_lock);
  117. return ESP_FAIL;
  118. }
  119. coex_bt_high_prio();
  120. }
  121. }
  122. #if CONFIG_SW_COEXIST_ENABLE
  123. if ((module == PHY_BT_MODULE) || (module == PHY_WIFI_MODULE)){
  124. uint32_t phy_bt_wifi_mask = BIT(PHY_BT_MODULE) | BIT(PHY_WIFI_MODULE);
  125. if ((s_module_phy_rf_init & phy_bt_wifi_mask) == phy_bt_wifi_mask) { //both wifi & bt enabled
  126. coex_init();
  127. coex_preference_set(CONFIG_SW_COEXIST_PREFERENCE_VALUE);
  128. coex_resume();
  129. }
  130. }
  131. #endif
  132. _lock_release(&s_phy_rf_init_lock);
  133. return status;
  134. }
  135. esp_err_t esp_phy_rf_deinit(phy_rf_module_t module)
  136. {
  137. /* 3 modules may call phy_init: Wi-Fi, BT, Modem Sleep */
  138. if (module >= PHY_MODULE_COUNT){
  139. ESP_LOGE(TAG, "%s, invalid module parameter(%d), should be smaller than \
  140. module count(%d)", __func__, module, PHY_MODULE_COUNT);
  141. return ESP_ERR_INVALID_ARG;
  142. }
  143. _lock_acquire(&s_phy_rf_init_lock);
  144. uint32_t s_module_phy_rf_init_old = s_module_phy_rf_init;
  145. uint32_t phy_bt_wifi_mask = BIT(PHY_BT_MODULE) | BIT(PHY_WIFI_MODULE);
  146. bool is_wifi_or_bt_enabled = !!(s_module_phy_rf_init_old & phy_bt_wifi_mask);
  147. bool is_both_wifi_bt_enabled = ((s_module_phy_rf_init_old & phy_bt_wifi_mask) == phy_bt_wifi_mask);
  148. s_module_phy_rf_init &= ~BIT(module);
  149. esp_err_t status = ESP_OK;
  150. #if CONFIG_SW_COEXIST_ENABLE
  151. if ((module == PHY_BT_MODULE) || (module == PHY_WIFI_MODULE)){
  152. if (is_both_wifi_bt_enabled == true) {
  153. coex_deinit();
  154. }
  155. }
  156. #endif
  157. if ((is_wifi_or_bt_enabled == false) && (module == PHY_MODEM_MODULE)){
  158. /* Modem sleep should not take effect in this case */
  159. status = ESP_FAIL;
  160. }
  161. else if (s_is_phy_rf_en == false) {
  162. //do nothing
  163. }
  164. else {
  165. if (is_wifi_or_bt_enabled == false){
  166. if ((module == PHY_BT_MODULE) || (module == PHY_WIFI_MODULE)){
  167. s_is_phy_rf_en = false;
  168. ESP_LOGE(TAG, "%s, RF should not be in enabled state if both Wi-Fi and BT are disabled", __func__);
  169. }
  170. }
  171. else {
  172. if (module == PHY_MODEM_MODULE){
  173. s_is_phy_rf_en = false;
  174. }
  175. else if ((module == PHY_BT_MODULE) || (module == PHY_WIFI_MODULE)){
  176. s_is_phy_rf_en = is_both_wifi_bt_enabled ? true : false;
  177. }
  178. }
  179. if (s_is_phy_rf_en == false) {
  180. // Disable PHY and RF.
  181. phy_close_rf();
  182. // Disable WiFi/BT common peripheral clock. Do not disable clock for hardware RNG
  183. periph_module_disable(PERIPH_WIFI_BT_COMMON_MODULE);
  184. }
  185. }
  186. _lock_release(&s_phy_rf_init_lock);
  187. return status;
  188. }
  189. esp_err_t esp_modem_sleep_enter(modem_sleep_module_t module)
  190. {
  191. #if CONFIG_SW_COEXIST_ENABLE
  192. uint32_t phy_bt_wifi_mask = BIT(PHY_BT_MODULE) | BIT(PHY_WIFI_MODULE);
  193. #endif
  194. if (module >= MODEM_MODULE_COUNT){
  195. ESP_LOGE(TAG, "%s, invalid module parameter(%d), should be smaller than \
  196. module count(%d)", __func__, module, MODEM_MODULE_COUNT);
  197. return ESP_ERR_INVALID_ARG;
  198. }
  199. else if (!(s_modem_sleep_module_register & BIT(module))){
  200. ESP_LOGW(TAG, "%s, module (%d) has not been registered", __func__, module);
  201. return ESP_ERR_INVALID_ARG;
  202. }
  203. else {
  204. _lock_acquire(&s_modem_sleep_lock);
  205. s_modem_sleep_module_enter |= BIT(module);
  206. #if CONFIG_SW_COEXIST_ENABLE
  207. _lock_acquire(&s_phy_rf_init_lock);
  208. if (((s_module_phy_rf_init & phy_bt_wifi_mask) == phy_bt_wifi_mask) //both wifi & bt enabled
  209. && (s_modem_sleep_module_enter & (MODEM_BT_MASK | MODEM_WIFI_MASK)) != 0){
  210. coex_pause();
  211. }
  212. _lock_release(&s_phy_rf_init_lock);
  213. #endif
  214. if (!s_is_modem_sleep_en && (s_modem_sleep_module_enter == s_modem_sleep_module_register)){
  215. esp_err_t status = esp_phy_rf_deinit(PHY_MODEM_MODULE);
  216. if (status == ESP_OK){
  217. s_is_modem_sleep_en = true;
  218. }
  219. }
  220. _lock_release(&s_modem_sleep_lock);
  221. return ESP_OK;
  222. }
  223. }
  224. esp_err_t esp_modem_sleep_exit(modem_sleep_module_t module)
  225. {
  226. #if CONFIG_SW_COEXIST_ENABLE
  227. uint32_t phy_bt_wifi_mask = BIT(PHY_BT_MODULE) | BIT(PHY_WIFI_MODULE);
  228. #endif
  229. if (module >= MODEM_MODULE_COUNT){
  230. ESP_LOGE(TAG, "%s, invalid module parameter(%d), should be smaller than \
  231. module count(%d)", __func__, module, MODEM_MODULE_COUNT);
  232. return ESP_ERR_INVALID_ARG;
  233. }
  234. else if (!(s_modem_sleep_module_register & BIT(module))){
  235. ESP_LOGW(TAG, "%s, module (%d) has not been registered", __func__, module);
  236. return ESP_ERR_INVALID_ARG;
  237. }
  238. else {
  239. _lock_acquire(&s_modem_sleep_lock);
  240. s_modem_sleep_module_enter &= ~BIT(module);
  241. if (s_is_modem_sleep_en){
  242. esp_err_t status = esp_phy_rf_init(NULL,PHY_RF_CAL_NONE,NULL, PHY_MODEM_MODULE);
  243. if (status == ESP_OK){
  244. s_is_modem_sleep_en = false;
  245. }
  246. }
  247. #if CONFIG_SW_COEXIST_ENABLE
  248. _lock_acquire(&s_phy_rf_init_lock);
  249. if (((s_module_phy_rf_init & phy_bt_wifi_mask) == phy_bt_wifi_mask) //both wifi & bt enabled
  250. && (s_modem_sleep_module_enter & (MODEM_BT_MASK | MODEM_WIFI_MASK)) == 0){
  251. coex_resume();
  252. }
  253. _lock_release(&s_phy_rf_init_lock);
  254. #endif
  255. _lock_release(&s_modem_sleep_lock);
  256. return ESP_OK;
  257. }
  258. return ESP_OK;
  259. }
  260. esp_err_t esp_modem_sleep_register(modem_sleep_module_t module)
  261. {
  262. if (module >= MODEM_MODULE_COUNT){
  263. ESP_LOGE(TAG, "%s, invalid module parameter(%d), should be smaller than \
  264. module count(%d)", __func__, module, MODEM_MODULE_COUNT);
  265. return ESP_ERR_INVALID_ARG;
  266. }
  267. else if (s_modem_sleep_module_register & BIT(module)){
  268. ESP_LOGI(TAG, "%s, multiple registration of module (%d)", __func__, module);
  269. return ESP_OK;
  270. }
  271. else{
  272. _lock_acquire(&s_modem_sleep_lock);
  273. s_modem_sleep_module_register |= BIT(module);
  274. /* The module is set to enter modem sleep by default, otherwise will prevent
  275. * other modules from entering sleep mode if this module never call enter sleep function
  276. * in the future */
  277. s_modem_sleep_module_enter |= BIT(module);
  278. _lock_release(&s_modem_sleep_lock);
  279. return ESP_OK;
  280. }
  281. }
  282. esp_err_t esp_modem_sleep_deregister(modem_sleep_module_t module)
  283. {
  284. if (module >= MODEM_MODULE_COUNT){
  285. ESP_LOGE(TAG, "%s, invalid module parameter(%d), should be smaller than \
  286. module count(%d)", __func__, module, MODEM_MODULE_COUNT);
  287. return ESP_ERR_INVALID_ARG;
  288. }
  289. else if (!(s_modem_sleep_module_register & BIT(module))){
  290. ESP_LOGI(TAG, "%s, module (%d) has not been registered", __func__, module);
  291. return ESP_OK;
  292. }
  293. else{
  294. _lock_acquire(&s_modem_sleep_lock);
  295. s_modem_sleep_module_enter &= ~BIT(module);
  296. s_modem_sleep_module_register &= ~BIT(module);
  297. if (s_modem_sleep_module_register == 0){
  298. s_modem_sleep_module_enter = 0;
  299. /* Once all module are de-registered and current state
  300. * is modem sleep mode, we need to turn off modem sleep
  301. */
  302. if (s_is_modem_sleep_en == true){
  303. s_is_modem_sleep_en = false;
  304. esp_phy_rf_init(NULL,PHY_RF_CAL_NONE,NULL, PHY_MODEM_MODULE);
  305. }
  306. }
  307. _lock_release(&s_modem_sleep_lock);
  308. return ESP_OK;
  309. }
  310. }
  311. // PHY init data handling functions
  312. #if CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION
  313. #include "esp_partition.h"
  314. const esp_phy_init_data_t* esp_phy_get_init_data()
  315. {
  316. const esp_partition_t* partition = esp_partition_find_first(
  317. ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_PHY, NULL);
  318. if (partition == NULL) {
  319. ESP_LOGE(TAG, "PHY data partition not found");
  320. return NULL;
  321. }
  322. ESP_LOGD(TAG, "loading PHY init data from partition at offset 0x%x", partition->address);
  323. size_t init_data_store_length = sizeof(phy_init_magic_pre) +
  324. sizeof(esp_phy_init_data_t) + sizeof(phy_init_magic_post);
  325. uint8_t* init_data_store = (uint8_t*) malloc(init_data_store_length);
  326. if (init_data_store == NULL) {
  327. ESP_LOGE(TAG, "failed to allocate memory for PHY init data");
  328. return NULL;
  329. }
  330. esp_err_t err = esp_partition_read(partition, 0, init_data_store, init_data_store_length);
  331. if (err != ESP_OK) {
  332. ESP_LOGE(TAG, "failed to read PHY data partition (0x%x)", err);
  333. return NULL;
  334. }
  335. if (memcmp(init_data_store, PHY_INIT_MAGIC, sizeof(phy_init_magic_pre)) != 0 ||
  336. memcmp(init_data_store + init_data_store_length - sizeof(phy_init_magic_post),
  337. PHY_INIT_MAGIC, sizeof(phy_init_magic_post)) != 0) {
  338. ESP_LOGE(TAG, "failed to validate PHY data partition");
  339. return NULL;
  340. }
  341. ESP_LOGD(TAG, "PHY data partition validated");
  342. return (const esp_phy_init_data_t*) (init_data_store + sizeof(phy_init_magic_pre));
  343. }
  344. void esp_phy_release_init_data(const esp_phy_init_data_t* init_data)
  345. {
  346. free((uint8_t*) init_data - sizeof(phy_init_magic_pre));
  347. }
  348. #else // CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION
  349. // phy_init_data.h will declare static 'phy_init_data' variable initialized with default init data
  350. const esp_phy_init_data_t* esp_phy_get_init_data()
  351. {
  352. ESP_LOGD(TAG, "loading PHY init data from application binary");
  353. return &phy_init_data;
  354. }
  355. void esp_phy_release_init_data(const esp_phy_init_data_t* init_data)
  356. {
  357. // no-op
  358. }
  359. #endif // CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION
  360. // PHY calibration data handling functions
  361. static const char* PHY_NAMESPACE = "phy";
  362. static const char* PHY_CAL_VERSION_KEY = "cal_version";
  363. static const char* PHY_CAL_MAC_KEY = "cal_mac";
  364. static const char* PHY_CAL_DATA_KEY = "cal_data";
  365. static esp_err_t load_cal_data_from_nvs_handle(nvs_handle handle,
  366. esp_phy_calibration_data_t* out_cal_data);
  367. static esp_err_t store_cal_data_to_nvs_handle(nvs_handle handle,
  368. const esp_phy_calibration_data_t* cal_data);
  369. esp_err_t esp_phy_load_cal_data_from_nvs(esp_phy_calibration_data_t* out_cal_data)
  370. {
  371. nvs_handle handle;
  372. esp_err_t err = nvs_open(PHY_NAMESPACE, NVS_READONLY, &handle);
  373. if (err == ESP_ERR_NVS_NOT_INITIALIZED) {
  374. ESP_LOGE(TAG, "%s: NVS has not been initialized. "
  375. "Call nvs_flash_init before starting WiFi/BT.", __func__);
  376. } else if (err != ESP_OK) {
  377. ESP_LOGD(TAG, "%s: failed to open NVS namespace (0x%x)", __func__, err);
  378. return err;
  379. }
  380. err = load_cal_data_from_nvs_handle(handle, out_cal_data);
  381. nvs_close(handle);
  382. return err;
  383. }
  384. esp_err_t esp_phy_store_cal_data_to_nvs(const esp_phy_calibration_data_t* cal_data)
  385. {
  386. nvs_handle handle;
  387. esp_err_t err = nvs_open(PHY_NAMESPACE, NVS_READWRITE, &handle);
  388. if (err != ESP_OK) {
  389. ESP_LOGD(TAG, "%s: failed to open NVS namespace (0x%x)", __func__, err);
  390. return err;
  391. }
  392. else {
  393. err = store_cal_data_to_nvs_handle(handle, cal_data);
  394. nvs_close(handle);
  395. return err;
  396. }
  397. }
  398. static esp_err_t load_cal_data_from_nvs_handle(nvs_handle handle,
  399. esp_phy_calibration_data_t* out_cal_data)
  400. {
  401. esp_err_t err;
  402. uint32_t cal_data_version;
  403. err = nvs_get_u32(handle, PHY_CAL_VERSION_KEY, &cal_data_version);
  404. if (err != ESP_OK) {
  405. ESP_LOGD(TAG, "%s: failed to get cal_version (0x%x)", __func__, err);
  406. return err;
  407. }
  408. uint32_t cal_format_version = phy_get_rf_cal_version() & (~BIT(16));
  409. ESP_LOGV(TAG, "phy_get_rf_cal_version: %d\n", cal_format_version);
  410. if (cal_data_version != cal_format_version) {
  411. ESP_LOGD(TAG, "%s: expected calibration data format %d, found %d",
  412. __func__, cal_format_version, cal_data_version);
  413. return ESP_FAIL;
  414. }
  415. uint8_t cal_data_mac[6];
  416. size_t length = sizeof(cal_data_mac);
  417. err = nvs_get_blob(handle, PHY_CAL_MAC_KEY, cal_data_mac, &length);
  418. if (err != ESP_OK) {
  419. ESP_LOGD(TAG, "%s: failed to get cal_mac (0x%x)", __func__, err);
  420. return err;
  421. }
  422. if (length != sizeof(cal_data_mac)) {
  423. ESP_LOGD(TAG, "%s: invalid length of cal_mac (%d)", __func__, length);
  424. return ESP_ERR_INVALID_SIZE;
  425. }
  426. uint8_t sta_mac[6];
  427. esp_efuse_mac_get_default(sta_mac);
  428. if (memcmp(sta_mac, cal_data_mac, sizeof(sta_mac)) != 0) {
  429. ESP_LOGE(TAG, "%s: calibration data MAC check failed: expected " \
  430. MACSTR ", found " MACSTR,
  431. __func__, MAC2STR(sta_mac), MAC2STR(cal_data_mac));
  432. return ESP_FAIL;
  433. }
  434. length = sizeof(*out_cal_data);
  435. err = nvs_get_blob(handle, PHY_CAL_DATA_KEY, out_cal_data, &length);
  436. if (err != ESP_OK) {
  437. ESP_LOGE(TAG, "%s: failed to get cal_data(0x%x)", __func__, err);
  438. return err;
  439. }
  440. if (length != sizeof(*out_cal_data)) {
  441. ESP_LOGD(TAG, "%s: invalid length of cal_data (%d)", __func__, length);
  442. return ESP_ERR_INVALID_SIZE;
  443. }
  444. memcpy(out_cal_data->mac, sta_mac, 6);
  445. return ESP_OK;
  446. }
  447. static esp_err_t store_cal_data_to_nvs_handle(nvs_handle handle,
  448. const esp_phy_calibration_data_t* cal_data)
  449. {
  450. esp_err_t err;
  451. err = nvs_set_blob(handle, PHY_CAL_DATA_KEY, cal_data, sizeof(*cal_data));
  452. if (err != ESP_OK) {
  453. ESP_LOGE(TAG, "%s: store calibration data failed(0x%x)\n", __func__, err);
  454. return err;
  455. }
  456. uint8_t sta_mac[6];
  457. esp_efuse_mac_get_default(sta_mac);
  458. err = nvs_set_blob(handle, PHY_CAL_MAC_KEY, sta_mac, sizeof(sta_mac));
  459. if (err != ESP_OK) {
  460. ESP_LOGE(TAG, "%s: store calibration mac failed(0x%x)\n", __func__, err);
  461. return err;
  462. }
  463. uint32_t cal_format_version = phy_get_rf_cal_version() & (~BIT(16));
  464. ESP_LOGV(TAG, "phy_get_rf_cal_version: %d\n", cal_format_version);
  465. err = nvs_set_u32(handle, PHY_CAL_VERSION_KEY, cal_format_version);
  466. if (err != ESP_OK) {
  467. ESP_LOGE(TAG, "%s: store calibration version failed(0x%x)\n", __func__, err);
  468. return err;
  469. }
  470. err = nvs_commit(handle);
  471. if (err != ESP_OK) {
  472. ESP_LOGE(TAG, "%s: store calibration nvs commit failed(0x%x)\n", __func__, err);
  473. }
  474. return err;
  475. }
  476. void esp_phy_load_cal_and_init(phy_rf_module_t module)
  477. {
  478. esp_phy_calibration_data_t* cal_data =
  479. (esp_phy_calibration_data_t*) calloc(sizeof(esp_phy_calibration_data_t), 1);
  480. if (cal_data == NULL) {
  481. ESP_LOGE(TAG, "failed to allocate memory for RF calibration data");
  482. abort();
  483. }
  484. const esp_phy_init_data_t* init_data = esp_phy_get_init_data();
  485. if (init_data == NULL) {
  486. ESP_LOGE(TAG, "failed to obtain PHY init data");
  487. abort();
  488. }
  489. #ifdef CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE
  490. esp_phy_calibration_mode_t calibration_mode = PHY_RF_CAL_PARTIAL;
  491. if (rtc_get_reset_reason(0) == DEEPSLEEP_RESET) {
  492. calibration_mode = PHY_RF_CAL_NONE;
  493. }
  494. esp_err_t err = esp_phy_load_cal_data_from_nvs(cal_data);
  495. if (err != ESP_OK) {
  496. ESP_LOGW(TAG, "failed to load RF calibration data (0x%x), falling back to full calibration", err);
  497. calibration_mode = PHY_RF_CAL_FULL;
  498. }
  499. esp_phy_rf_init(init_data, calibration_mode, cal_data, module);
  500. if (calibration_mode != PHY_RF_CAL_NONE && err != ESP_OK) {
  501. err = esp_phy_store_cal_data_to_nvs(cal_data);
  502. } else {
  503. err = ESP_OK;
  504. }
  505. #else
  506. esp_phy_rf_init(init_data, PHY_RF_CAL_FULL, cal_data, module);
  507. #endif
  508. esp_phy_release_init_data(init_data);
  509. free(cal_data); // PHY maintains a copy of calibration data, so we can free this
  510. }