phy_init.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. } else if (err != ESP_OK) {
  396. ESP_LOGD(TAG, "%s: failed to open NVS namespace (0x%x)", __func__, err);
  397. return err;
  398. }
  399. err = load_cal_data_from_nvs_handle(handle, out_cal_data);
  400. nvs_close(handle);
  401. return err;
  402. }
  403. esp_err_t esp_phy_store_cal_data_to_nvs(const esp_phy_calibration_data_t* cal_data)
  404. {
  405. nvs_handle handle;
  406. esp_err_t err = nvs_open(PHY_NAMESPACE, NVS_READWRITE, &handle);
  407. if (err != ESP_OK) {
  408. ESP_LOGD(TAG, "%s: failed to open NVS namespace (0x%x)", __func__, err);
  409. return err;
  410. }
  411. else {
  412. err = store_cal_data_to_nvs_handle(handle, cal_data);
  413. nvs_close(handle);
  414. return err;
  415. }
  416. }
  417. static esp_err_t load_cal_data_from_nvs_handle(nvs_handle handle,
  418. esp_phy_calibration_data_t* out_cal_data)
  419. {
  420. esp_err_t err;
  421. uint32_t cal_data_version;
  422. err = nvs_get_u32(handle, PHY_CAL_VERSION_KEY, &cal_data_version);
  423. if (err != ESP_OK) {
  424. ESP_LOGD(TAG, "%s: failed to get cal_version (0x%x)", __func__, err);
  425. return err;
  426. }
  427. uint32_t cal_format_version = phy_get_rf_cal_version() & (~BIT(16));
  428. ESP_LOGV(TAG, "phy_get_rf_cal_version: %d\n", cal_format_version);
  429. if (cal_data_version != cal_format_version) {
  430. ESP_LOGD(TAG, "%s: expected calibration data format %d, found %d",
  431. __func__, cal_format_version, cal_data_version);
  432. return ESP_FAIL;
  433. }
  434. uint8_t cal_data_mac[6];
  435. size_t length = sizeof(cal_data_mac);
  436. err = nvs_get_blob(handle, PHY_CAL_MAC_KEY, cal_data_mac, &length);
  437. if (err != ESP_OK) {
  438. ESP_LOGD(TAG, "%s: failed to get cal_mac (0x%x)", __func__, err);
  439. return err;
  440. }
  441. if (length != sizeof(cal_data_mac)) {
  442. ESP_LOGD(TAG, "%s: invalid length of cal_mac (%d)", __func__, length);
  443. return ESP_ERR_INVALID_SIZE;
  444. }
  445. uint8_t sta_mac[6];
  446. esp_efuse_mac_get_default(sta_mac);
  447. if (memcmp(sta_mac, cal_data_mac, sizeof(sta_mac)) != 0) {
  448. ESP_LOGE(TAG, "%s: calibration data MAC check failed: expected " \
  449. MACSTR ", found " MACSTR,
  450. __func__, MAC2STR(sta_mac), MAC2STR(cal_data_mac));
  451. return ESP_FAIL;
  452. }
  453. length = sizeof(*out_cal_data);
  454. err = nvs_get_blob(handle, PHY_CAL_DATA_KEY, out_cal_data, &length);
  455. if (err != ESP_OK) {
  456. ESP_LOGE(TAG, "%s: failed to get cal_data(0x%x)", __func__, err);
  457. return err;
  458. }
  459. if (length != sizeof(*out_cal_data)) {
  460. ESP_LOGD(TAG, "%s: invalid length of cal_data (%d)", __func__, length);
  461. return ESP_ERR_INVALID_SIZE;
  462. }
  463. return ESP_OK;
  464. }
  465. static esp_err_t store_cal_data_to_nvs_handle(nvs_handle handle,
  466. const esp_phy_calibration_data_t* cal_data)
  467. {
  468. esp_err_t err;
  469. err = nvs_set_blob(handle, PHY_CAL_DATA_KEY, cal_data, sizeof(*cal_data));
  470. if (err != ESP_OK) {
  471. ESP_LOGE(TAG, "%s: store calibration data failed(0x%x)\n", __func__, err);
  472. return err;
  473. }
  474. uint8_t sta_mac[6];
  475. esp_efuse_mac_get_default(sta_mac);
  476. err = nvs_set_blob(handle, PHY_CAL_MAC_KEY, sta_mac, sizeof(sta_mac));
  477. if (err != ESP_OK) {
  478. ESP_LOGE(TAG, "%s: store calibration mac failed(0x%x)\n", __func__, err);
  479. return err;
  480. }
  481. uint32_t cal_format_version = phy_get_rf_cal_version() & (~BIT(16));
  482. ESP_LOGV(TAG, "phy_get_rf_cal_version: %d\n", cal_format_version);
  483. err = nvs_set_u32(handle, PHY_CAL_VERSION_KEY, cal_format_version);
  484. if (err != ESP_OK) {
  485. ESP_LOGE(TAG, "%s: store calibration version failed(0x%x)\n", __func__, err);
  486. return err;
  487. }
  488. err = nvs_commit(handle);
  489. if (err != ESP_OK) {
  490. ESP_LOGE(TAG, "%s: store calibration nvs commit failed(0x%x)\n", __func__, err);
  491. }
  492. return err;
  493. }
  494. #if CONFIG_REDUCE_PHY_TX_POWER
  495. static void esp_phy_reduce_tx_power(esp_phy_init_data_t* init_data)
  496. {
  497. uint8_t i;
  498. for(i = 0; i < PHY_TX_POWER_NUM; i++) {
  499. // LOWEST_PHY_TX_POWER is the lowest tx power
  500. init_data->params[PHY_TX_POWER_OFFSET+i] = PHY_TX_POWER_LOWEST;
  501. }
  502. }
  503. #endif
  504. void esp_phy_load_cal_and_init(phy_rf_module_t module)
  505. {
  506. esp_phy_calibration_data_t* cal_data =
  507. (esp_phy_calibration_data_t*) calloc(sizeof(esp_phy_calibration_data_t), 1);
  508. if (cal_data == NULL) {
  509. ESP_LOGE(TAG, "failed to allocate memory for RF calibration data");
  510. abort();
  511. }
  512. #if CONFIG_REDUCE_PHY_TX_POWER
  513. const esp_phy_init_data_t* phy_init_data = esp_phy_get_init_data();
  514. if (phy_init_data == NULL) {
  515. ESP_LOGE(TAG, "failed to obtain PHY init data");
  516. abort();
  517. }
  518. esp_phy_init_data_t* init_data = (esp_phy_init_data_t*) malloc(sizeof(esp_phy_init_data_t));
  519. if (init_data == NULL) {
  520. ESP_LOGE(TAG, "failed to allocate memory for phy init data");
  521. abort();
  522. }
  523. memcpy(init_data, phy_init_data, sizeof(esp_phy_init_data_t));
  524. if (esp_reset_reason() == ESP_RST_BROWNOUT) {
  525. esp_phy_reduce_tx_power(init_data);
  526. }
  527. #else
  528. const esp_phy_init_data_t* init_data = esp_phy_get_init_data();
  529. if (init_data == NULL) {
  530. ESP_LOGE(TAG, "failed to obtain PHY init data");
  531. abort();
  532. }
  533. #endif
  534. #ifdef CONFIG_ESP32_PHY_CALIBRATION_AND_DATA_STORAGE
  535. esp_phy_calibration_mode_t calibration_mode = PHY_RF_CAL_PARTIAL;
  536. uint8_t sta_mac[6];
  537. if (rtc_get_reset_reason(0) == DEEPSLEEP_RESET) {
  538. calibration_mode = PHY_RF_CAL_NONE;
  539. }
  540. esp_err_t err = esp_phy_load_cal_data_from_nvs(cal_data);
  541. if (err != ESP_OK) {
  542. ESP_LOGW(TAG, "failed to load RF calibration data (0x%x), falling back to full calibration", err);
  543. calibration_mode = PHY_RF_CAL_FULL;
  544. }
  545. esp_efuse_mac_get_default(sta_mac);
  546. memcpy(cal_data->mac, sta_mac, 6);
  547. esp_phy_rf_init(init_data, calibration_mode, cal_data, module);
  548. if (calibration_mode != PHY_RF_CAL_NONE && err != ESP_OK) {
  549. err = esp_phy_store_cal_data_to_nvs(cal_data);
  550. } else {
  551. err = ESP_OK;
  552. }
  553. #else
  554. esp_phy_rf_init(init_data, PHY_RF_CAL_FULL, cal_data, module);
  555. #endif
  556. #if CONFIG_REDUCE_PHY_TX_POWER
  557. esp_phy_release_init_data(phy_init_data);
  558. free(init_data);
  559. #else
  560. esp_phy_release_init_data(init_data);
  561. #endif
  562. free(cal_data); // PHY maintains a copy of calibration data, so we can free this
  563. }