phy_init.c 19 KB

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