phy_init.c 23 KB

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