phy_init.c 22 KB

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