phy_init.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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_resume();
  205. }
  206. }
  207. #endif
  208. _lock_release(&s_phy_rf_init_lock);
  209. return status;
  210. }
  211. esp_err_t esp_phy_rf_deinit(phy_rf_module_t module)
  212. {
  213. /* 3 modules may call phy_init: Wi-Fi, BT, Modem Sleep */
  214. if (module >= PHY_MODULE_COUNT){
  215. ESP_LOGE(TAG, "%s, invalid module parameter(%d), should be smaller than \
  216. module count(%d)", __func__, module, PHY_MODULE_COUNT);
  217. return ESP_ERR_INVALID_ARG;
  218. }
  219. _lock_acquire(&s_phy_rf_init_lock);
  220. uint32_t s_module_phy_rf_init_old = s_module_phy_rf_init;
  221. uint32_t phy_bt_wifi_mask = BIT(PHY_BT_MODULE) | BIT(PHY_WIFI_MODULE);
  222. bool is_wifi_or_bt_enabled = !!(s_module_phy_rf_init_old & phy_bt_wifi_mask);
  223. bool is_both_wifi_bt_enabled = ((s_module_phy_rf_init_old & phy_bt_wifi_mask) == phy_bt_wifi_mask);
  224. s_module_phy_rf_init &= ~BIT(module);
  225. esp_err_t status = ESP_OK;
  226. #if CONFIG_SW_COEXIST_ENABLE
  227. if ((module == PHY_BT_MODULE) || (module == PHY_WIFI_MODULE)){
  228. if (is_both_wifi_bt_enabled == true) {
  229. coex_deinit();
  230. }
  231. }
  232. #endif
  233. if ((is_wifi_or_bt_enabled == false) && (module == PHY_MODEM_MODULE)){
  234. /* Modem sleep should not take effect in this case */
  235. status = ESP_FAIL;
  236. }
  237. else if (s_is_phy_rf_en == false) {
  238. //do nothing
  239. }
  240. else {
  241. if (is_wifi_or_bt_enabled == false){
  242. if ((module == PHY_BT_MODULE) || (module == PHY_WIFI_MODULE)){
  243. s_is_phy_rf_en = false;
  244. ESP_LOGE(TAG, "%s, RF should not be in enabled state if both Wi-Fi and BT are disabled", __func__);
  245. }
  246. }
  247. else {
  248. if (module == PHY_MODEM_MODULE){
  249. s_is_phy_rf_en = false;
  250. }
  251. else if ((module == PHY_BT_MODULE) || (module == PHY_WIFI_MODULE)){
  252. s_is_phy_rf_en = is_both_wifi_bt_enabled ? true : false;
  253. }
  254. }
  255. if (s_is_phy_rf_en == false) {
  256. // Disable PHY and RF.
  257. phy_close_rf();
  258. // Update WiFi MAC time before disalbe WiFi/BT common peripheral clock
  259. phy_update_wifi_mac_time(true, esp_timer_get_time());
  260. // Disable WiFi/BT common peripheral clock. Do not disable clock for hardware RNG
  261. //periph_module_disable(PERIPH_WIFI_BT_COMMON_MODULE);
  262. esp_phy_common_clock_disable();
  263. }
  264. }
  265. _lock_release(&s_phy_rf_init_lock);
  266. return status;
  267. }
  268. esp_err_t esp_modem_sleep_enter(modem_sleep_module_t module)
  269. {
  270. #if CONFIG_SW_COEXIST_ENABLE
  271. uint32_t phy_bt_wifi_mask = BIT(PHY_BT_MODULE) | BIT(PHY_WIFI_MODULE);
  272. #endif
  273. if (module >= MODEM_MODULE_COUNT){
  274. ESP_LOGE(TAG, "%s, invalid module parameter(%d), should be smaller than \
  275. module count(%d)", __func__, module, MODEM_MODULE_COUNT);
  276. return ESP_ERR_INVALID_ARG;
  277. }
  278. else if (!(s_modem_sleep_module_register & BIT(module))){
  279. ESP_LOGW(TAG, "%s, module (%d) has not been registered", __func__, module);
  280. return ESP_ERR_INVALID_ARG;
  281. }
  282. else {
  283. _lock_acquire(&s_modem_sleep_lock);
  284. s_modem_sleep_module_enter |= BIT(module);
  285. #if CONFIG_SW_COEXIST_ENABLE
  286. _lock_acquire(&s_phy_rf_init_lock);
  287. if (((s_module_phy_rf_init & phy_bt_wifi_mask) == phy_bt_wifi_mask) //both wifi & bt enabled
  288. && (s_modem_sleep_module_enter & (MODEM_BT_MASK | MODEM_WIFI_MASK)) != 0){
  289. coex_pause();
  290. }
  291. _lock_release(&s_phy_rf_init_lock);
  292. #endif
  293. if (!s_is_modem_sleep_en && (s_modem_sleep_module_enter == s_modem_sleep_module_register)){
  294. esp_err_t status = esp_phy_rf_deinit(PHY_MODEM_MODULE);
  295. if (status == ESP_OK){
  296. s_is_modem_sleep_en = true;
  297. }
  298. }
  299. _lock_release(&s_modem_sleep_lock);
  300. return ESP_OK;
  301. }
  302. }
  303. esp_err_t esp_modem_sleep_exit(modem_sleep_module_t module)
  304. {
  305. #if CONFIG_SW_COEXIST_ENABLE
  306. uint32_t phy_bt_wifi_mask = BIT(PHY_BT_MODULE) | BIT(PHY_WIFI_MODULE);
  307. #endif
  308. if (module >= MODEM_MODULE_COUNT){
  309. ESP_LOGE(TAG, "%s, invalid module parameter(%d), should be smaller than \
  310. module count(%d)", __func__, module, MODEM_MODULE_COUNT);
  311. return ESP_ERR_INVALID_ARG;
  312. }
  313. else if (!(s_modem_sleep_module_register & BIT(module))){
  314. ESP_LOGW(TAG, "%s, module (%d) has not been registered", __func__, module);
  315. return ESP_ERR_INVALID_ARG;
  316. }
  317. else {
  318. _lock_acquire(&s_modem_sleep_lock);
  319. s_modem_sleep_module_enter &= ~BIT(module);
  320. if (s_is_modem_sleep_en){
  321. esp_err_t status = esp_phy_rf_init(NULL,PHY_RF_CAL_NONE,NULL, PHY_MODEM_MODULE);
  322. if (status == ESP_OK){
  323. s_is_modem_sleep_en = false;
  324. }
  325. }
  326. #if CONFIG_SW_COEXIST_ENABLE
  327. _lock_acquire(&s_phy_rf_init_lock);
  328. if (((s_module_phy_rf_init & phy_bt_wifi_mask) == phy_bt_wifi_mask) //both wifi & bt enabled
  329. && (s_modem_sleep_module_enter & (MODEM_BT_MASK | MODEM_WIFI_MASK)) == 0){
  330. coex_resume();
  331. }
  332. _lock_release(&s_phy_rf_init_lock);
  333. #endif
  334. _lock_release(&s_modem_sleep_lock);
  335. return ESP_OK;
  336. }
  337. return ESP_OK;
  338. }
  339. esp_err_t esp_modem_sleep_register(modem_sleep_module_t module)
  340. {
  341. if (module >= MODEM_MODULE_COUNT){
  342. ESP_LOGE(TAG, "%s, invalid module parameter(%d), should be smaller than \
  343. module count(%d)", __func__, module, MODEM_MODULE_COUNT);
  344. return ESP_ERR_INVALID_ARG;
  345. }
  346. else if (s_modem_sleep_module_register & BIT(module)){
  347. ESP_LOGI(TAG, "%s, multiple registration of module (%d)", __func__, module);
  348. return ESP_OK;
  349. }
  350. else{
  351. _lock_acquire(&s_modem_sleep_lock);
  352. s_modem_sleep_module_register |= BIT(module);
  353. /* The module is set to enter modem sleep by default, otherwise will prevent
  354. * other modules from entering sleep mode if this module never call enter sleep function
  355. * in the future */
  356. s_modem_sleep_module_enter |= BIT(module);
  357. _lock_release(&s_modem_sleep_lock);
  358. return ESP_OK;
  359. }
  360. }
  361. esp_err_t esp_modem_sleep_deregister(modem_sleep_module_t module)
  362. {
  363. if (module >= MODEM_MODULE_COUNT){
  364. ESP_LOGE(TAG, "%s, invalid module parameter(%d), should be smaller than \
  365. module count(%d)", __func__, module, MODEM_MODULE_COUNT);
  366. return ESP_ERR_INVALID_ARG;
  367. }
  368. else if (!(s_modem_sleep_module_register & BIT(module))){
  369. ESP_LOGI(TAG, "%s, module (%d) has not been registered", __func__, module);
  370. return ESP_OK;
  371. }
  372. else{
  373. _lock_acquire(&s_modem_sleep_lock);
  374. s_modem_sleep_module_enter &= ~BIT(module);
  375. s_modem_sleep_module_register &= ~BIT(module);
  376. if (s_modem_sleep_module_register == 0){
  377. s_modem_sleep_module_enter = 0;
  378. /* Once all module are de-registered and current state
  379. * is modem sleep mode, we need to turn off modem sleep
  380. */
  381. if (s_is_modem_sleep_en == true){
  382. s_is_modem_sleep_en = false;
  383. esp_phy_rf_init(NULL,PHY_RF_CAL_NONE,NULL, PHY_MODEM_MODULE);
  384. }
  385. }
  386. _lock_release(&s_modem_sleep_lock);
  387. return ESP_OK;
  388. }
  389. }
  390. // PHY init data handling functions
  391. #if CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION
  392. #include "esp_partition.h"
  393. const esp_phy_init_data_t* esp_phy_get_init_data()
  394. {
  395. const esp_partition_t* partition = esp_partition_find_first(
  396. ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_PHY, NULL);
  397. if (partition == NULL) {
  398. ESP_LOGE(TAG, "PHY data partition not found");
  399. return NULL;
  400. }
  401. ESP_LOGD(TAG, "loading PHY init data from partition at offset 0x%x", partition->address);
  402. size_t init_data_store_length = sizeof(phy_init_magic_pre) +
  403. sizeof(esp_phy_init_data_t) + sizeof(phy_init_magic_post);
  404. uint8_t* init_data_store = (uint8_t*) malloc(init_data_store_length);
  405. if (init_data_store == NULL) {
  406. ESP_LOGE(TAG, "failed to allocate memory for PHY init data");
  407. return NULL;
  408. }
  409. esp_err_t err = esp_partition_read(partition, 0, init_data_store, init_data_store_length);
  410. if (err != ESP_OK) {
  411. ESP_LOGE(TAG, "failed to read PHY data partition (0x%x)", err);
  412. return NULL;
  413. }
  414. if (memcmp(init_data_store, PHY_INIT_MAGIC, sizeof(phy_init_magic_pre)) != 0 ||
  415. memcmp(init_data_store + init_data_store_length - sizeof(phy_init_magic_post),
  416. PHY_INIT_MAGIC, sizeof(phy_init_magic_post)) != 0) {
  417. ESP_LOGE(TAG, "failed to validate PHY data partition");
  418. return NULL;
  419. }
  420. ESP_LOGD(TAG, "PHY data partition validated");
  421. return (const esp_phy_init_data_t*) (init_data_store + sizeof(phy_init_magic_pre));
  422. }
  423. void esp_phy_release_init_data(const esp_phy_init_data_t* init_data)
  424. {
  425. free((uint8_t*) init_data - sizeof(phy_init_magic_pre));
  426. }
  427. #else // CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION
  428. // phy_init_data.h will declare static 'phy_init_data' variable initialized with default init data
  429. const esp_phy_init_data_t* esp_phy_get_init_data()
  430. {
  431. ESP_LOGD(TAG, "loading PHY init data from application binary");
  432. return &phy_init_data;
  433. }
  434. void esp_phy_release_init_data(const esp_phy_init_data_t* init_data)
  435. {
  436. // no-op
  437. }
  438. #endif // CONFIG_ESP32_PHY_INIT_DATA_IN_PARTITION
  439. // PHY calibration data handling functions
  440. static const char* PHY_NAMESPACE = "phy";
  441. static const char* PHY_CAL_VERSION_KEY = "cal_version";
  442. static const char* PHY_CAL_MAC_KEY = "cal_mac";
  443. static const char* PHY_CAL_DATA_KEY = "cal_data";
  444. static esp_err_t load_cal_data_from_nvs_handle(nvs_handle handle,
  445. esp_phy_calibration_data_t* out_cal_data);
  446. static esp_err_t store_cal_data_to_nvs_handle(nvs_handle handle,
  447. const esp_phy_calibration_data_t* cal_data);
  448. esp_err_t esp_phy_load_cal_data_from_nvs(esp_phy_calibration_data_t* out_cal_data)
  449. {
  450. nvs_handle handle;
  451. esp_err_t err = nvs_open(PHY_NAMESPACE, NVS_READONLY, &handle);
  452. if (err == ESP_ERR_NVS_NOT_INITIALIZED) {
  453. ESP_LOGE(TAG, "%s: NVS has not been initialized. "
  454. "Call nvs_flash_init before starting WiFi/BT.", __func__);
  455. return err;
  456. } else if (err != ESP_OK) {
  457. ESP_LOGD(TAG, "%s: failed to open NVS namespace (0x%x)", __func__, err);
  458. return err;
  459. }
  460. err = load_cal_data_from_nvs_handle(handle, out_cal_data);
  461. nvs_close(handle);
  462. return err;
  463. }
  464. esp_err_t esp_phy_store_cal_data_to_nvs(const esp_phy_calibration_data_t* cal_data)
  465. {
  466. nvs_handle handle;
  467. esp_err_t err = nvs_open(PHY_NAMESPACE, NVS_READWRITE, &handle);
  468. if (err != ESP_OK) {
  469. ESP_LOGD(TAG, "%s: failed to open NVS namespace (0x%x)", __func__, err);
  470. return err;
  471. }
  472. else {
  473. err = store_cal_data_to_nvs_handle(handle, cal_data);
  474. nvs_close(handle);
  475. return err;
  476. }
  477. }
  478. esp_err_t esp_phy_erase_cal_data_in_nvs(void)
  479. {
  480. nvs_handle handle;
  481. esp_err_t err = nvs_open(PHY_NAMESPACE, NVS_READWRITE, &handle);
  482. if (err != ESP_OK) {
  483. ESP_LOGE(TAG, "%s: failed to open NVS phy namespace (0x%x)", __func__, err);
  484. return err;
  485. }
  486. else {
  487. err = nvs_erase_all(handle);
  488. if (err != ESP_OK) {
  489. ESP_LOGE(TAG, "%s: failed to erase NVS phy namespace (0x%x)", __func__, err);
  490. }
  491. else {
  492. err = nvs_commit(handle);
  493. if (err != ESP_OK) {
  494. ESP_LOGE(TAG, "%s: failed to commit NVS phy namespace (0x%x)", __func__, err);
  495. }
  496. }
  497. }
  498. nvs_close(handle);
  499. return err;
  500. }
  501. static esp_err_t load_cal_data_from_nvs_handle(nvs_handle handle,
  502. esp_phy_calibration_data_t* out_cal_data)
  503. {
  504. esp_err_t err;
  505. uint32_t cal_data_version;
  506. err = nvs_get_u32(handle, PHY_CAL_VERSION_KEY, &cal_data_version);
  507. if (err != ESP_OK) {
  508. ESP_LOGD(TAG, "%s: failed to get cal_version (0x%x)", __func__, err);
  509. return err;
  510. }
  511. uint32_t cal_format_version = phy_get_rf_cal_version() & (~BIT(16));
  512. ESP_LOGV(TAG, "phy_get_rf_cal_version: %d\n", cal_format_version);
  513. if (cal_data_version != cal_format_version) {
  514. ESP_LOGD(TAG, "%s: expected calibration data format %d, found %d",
  515. __func__, cal_format_version, cal_data_version);
  516. return ESP_FAIL;
  517. }
  518. uint8_t cal_data_mac[6];
  519. size_t length = sizeof(cal_data_mac);
  520. err = nvs_get_blob(handle, PHY_CAL_MAC_KEY, cal_data_mac, &length);
  521. if (err != ESP_OK) {
  522. ESP_LOGD(TAG, "%s: failed to get cal_mac (0x%x)", __func__, err);
  523. return err;
  524. }
  525. if (length != sizeof(cal_data_mac)) {
  526. ESP_LOGD(TAG, "%s: invalid length of cal_mac (%d)", __func__, length);
  527. return ESP_ERR_INVALID_SIZE;
  528. }
  529. uint8_t sta_mac[6];
  530. esp_efuse_mac_get_default(sta_mac);
  531. if (memcmp(sta_mac, cal_data_mac, sizeof(sta_mac)) != 0) {
  532. ESP_LOGE(TAG, "%s: calibration data MAC check failed: expected " \
  533. MACSTR ", found " MACSTR,
  534. __func__, MAC2STR(sta_mac), MAC2STR(cal_data_mac));
  535. return ESP_FAIL;
  536. }
  537. length = sizeof(*out_cal_data);
  538. err = nvs_get_blob(handle, PHY_CAL_DATA_KEY, out_cal_data, &length);
  539. if (err != ESP_OK) {
  540. ESP_LOGE(TAG, "%s: failed to get cal_data(0x%x)", __func__, err);
  541. return err;
  542. }
  543. if (length != sizeof(*out_cal_data)) {
  544. ESP_LOGD(TAG, "%s: invalid length of cal_data (%d)", __func__, length);
  545. return ESP_ERR_INVALID_SIZE;
  546. }
  547. return ESP_OK;
  548. }
  549. static esp_err_t store_cal_data_to_nvs_handle(nvs_handle handle,
  550. const esp_phy_calibration_data_t* cal_data)
  551. {
  552. esp_err_t err;
  553. err = nvs_set_blob(handle, PHY_CAL_DATA_KEY, cal_data, sizeof(*cal_data));
  554. if (err != ESP_OK) {
  555. ESP_LOGE(TAG, "%s: store calibration data failed(0x%x)\n", __func__, err);
  556. return err;
  557. }
  558. uint8_t sta_mac[6];
  559. esp_efuse_mac_get_default(sta_mac);
  560. err = nvs_set_blob(handle, PHY_CAL_MAC_KEY, sta_mac, sizeof(sta_mac));
  561. if (err != ESP_OK) {
  562. ESP_LOGE(TAG, "%s: store calibration mac failed(0x%x)\n", __func__, err);
  563. return err;
  564. }
  565. uint32_t cal_format_version = phy_get_rf_cal_version() & (~BIT(16));
  566. ESP_LOGV(TAG, "phy_get_rf_cal_version: %d\n", cal_format_version);
  567. err = nvs_set_u32(handle, PHY_CAL_VERSION_KEY, cal_format_version);
  568. if (err != ESP_OK) {
  569. ESP_LOGE(TAG, "%s: store calibration version failed(0x%x)\n", __func__, err);
  570. return err;
  571. }
  572. err = nvs_commit(handle);
  573. if (err != ESP_OK) {
  574. ESP_LOGE(TAG, "%s: store calibration nvs commit failed(0x%x)\n", __func__, err);
  575. }
  576. return err;
  577. }
  578. #if CONFIG_REDUCE_PHY_TX_POWER
  579. static void esp_phy_reduce_tx_power(esp_phy_init_data_t* init_data)
  580. {
  581. uint8_t i;
  582. for(i = 0; i < PHY_TX_POWER_NUM; i++) {
  583. // LOWEST_PHY_TX_POWER is the lowest tx power
  584. init_data->params[PHY_TX_POWER_OFFSET+i] = PHY_TX_POWER_LOWEST;
  585. }
  586. }
  587. #endif
  588. void esp_phy_load_cal_and_init(phy_rf_module_t module)
  589. {
  590. esp_phy_calibration_data_t* cal_data =
  591. (esp_phy_calibration_data_t*) calloc(sizeof(esp_phy_calibration_data_t), 1);
  592. if (cal_data == NULL) {
  593. ESP_LOGE(TAG, "failed to allocate memory for RF calibration data");
  594. abort();
  595. }
  596. #if CONFIG_REDUCE_PHY_TX_POWER
  597. const esp_phy_init_data_t* phy_init_data = esp_phy_get_init_data();
  598. if (phy_init_data == NULL) {
  599. ESP_LOGE(TAG, "failed to obtain PHY init data");
  600. abort();
  601. }
  602. esp_phy_init_data_t* init_data = (esp_phy_init_data_t*) malloc(sizeof(esp_phy_init_data_t));
  603. if (init_data == NULL) {
  604. ESP_LOGE(TAG, "failed to allocate memory for phy init data");
  605. abort();
  606. }
  607. memcpy(init_data, phy_init_data, sizeof(esp_phy_init_data_t));
  608. if (esp_reset_reason() == ESP_RST_BROWNOUT) {
  609. esp_phy_reduce_tx_power(init_data);
  610. }
  611. #else
  612. const esp_phy_init_data_t* init_data = esp_phy_get_init_data();
  613. if (init_data == NULL) {
  614. ESP_LOGE(TAG, "failed to obtain PHY init data");
  615. abort();
  616. }
  617. #endif
  618. #ifdef CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE
  619. esp_phy_calibration_mode_t calibration_mode = PHY_RF_CAL_PARTIAL;
  620. uint8_t sta_mac[6];
  621. if (rtc_get_reset_reason(0) == DEEPSLEEP_RESET) {
  622. calibration_mode = PHY_RF_CAL_NONE;
  623. }
  624. esp_err_t err = esp_phy_load_cal_data_from_nvs(cal_data);
  625. if (err != ESP_OK) {
  626. ESP_LOGW(TAG, "failed to load RF calibration data (0x%x), falling back to full calibration", err);
  627. calibration_mode = PHY_RF_CAL_FULL;
  628. }
  629. esp_efuse_mac_get_default(sta_mac);
  630. memcpy(cal_data->mac, sta_mac, 6);
  631. esp_phy_rf_init(init_data, calibration_mode, cal_data, module);
  632. if (calibration_mode != PHY_RF_CAL_NONE && err != ESP_OK) {
  633. err = esp_phy_store_cal_data_to_nvs(cal_data);
  634. } else {
  635. err = ESP_OK;
  636. }
  637. #else
  638. esp_phy_rf_init(init_data, PHY_RF_CAL_FULL, cal_data, module);
  639. #endif
  640. #if CONFIG_REDUCE_PHY_TX_POWER
  641. esp_phy_release_init_data(phy_init_data);
  642. free(init_data);
  643. #else
  644. esp_phy_release_init_data(init_data);
  645. #endif
  646. free(cal_data); // PHY maintains a copy of calibration data, so we can free this
  647. }