phy_init.c 24 KB

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