phy_init.c 19 KB

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