phy_init.c 23 KB

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