phy_init.c 23 KB

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