phy_init.c 23 KB

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