wifi_init.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2015-2017 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 <esp_event.h>
  15. #include <esp_wifi.h>
  16. #include "esp_log.h"
  17. #include "esp_wifi_internal.h"
  18. #include "esp_pm.h"
  19. #include "soc/rtc.h"
  20. #include "esp_mesh.h"
  21. #include "driver/adc.h"
  22. #if (CONFIG_ESP32_WIFI_RX_BA_WIN > CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM)
  23. #error "WiFi configuration check: WARNING, WIFI_RX_BA_WIN should not be larger than WIFI_DYNAMIC_RX_BUFFER_NUM!"
  24. #endif
  25. #if (CONFIG_ESP32_WIFI_RX_BA_WIN > (CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM << 1))
  26. #error "WiFi configuration check: WARNING, WIFI_RX_BA_WIN should not be larger than double of the WIFI_STATIC_RX_BUFFER_NUM!"
  27. #endif
  28. /* mesh event callback handler */
  29. mesh_event_cb_t g_mesh_event_cb = NULL;
  30. /* Set additional WiFi features and capabilities */
  31. uint64_t g_wifi_feature_caps =
  32. #if CONFIG_ESP32_WIFI_ENABLE_WPA3_SAE
  33. CONFIG_FEATURE_WPA3_SAE_BIT |
  34. #endif
  35. #if (CONFIG_ESP32_SPIRAM_SUPPORT | CONFIG_ESP32S2_SPIRAM_SUPPORT)
  36. CONFIG_FEATURE_CACHE_TX_BUF_BIT |
  37. #endif
  38. 0;
  39. #ifdef CONFIG_PM_ENABLE
  40. static esp_pm_lock_handle_t s_wifi_modem_sleep_lock;
  41. #endif
  42. /* Callback function to update WiFi MAC time */
  43. wifi_mac_time_update_cb_t s_wifi_mac_time_update_cb = NULL;
  44. static bool s_wifi_adc_xpd_flag;
  45. static const char* TAG = "wifi_init";
  46. static void __attribute__((constructor)) s_set_default_wifi_log_level()
  47. {
  48. /* WiFi libraries aren't compiled to know CONFIG_LOG_DEFAULT_LEVEL,
  49. so set it at runtime startup. Done here not in esp_wifi_init() to allow
  50. the user to set the level again before esp_wifi_init() is called.
  51. */
  52. esp_log_level_set("wifi", CONFIG_LOG_DEFAULT_LEVEL);
  53. esp_log_level_set("mesh", CONFIG_LOG_DEFAULT_LEVEL);
  54. esp_log_level_set("smartconfig", CONFIG_LOG_DEFAULT_LEVEL);
  55. esp_log_level_set("ESPNOW", CONFIG_LOG_DEFAULT_LEVEL);
  56. }
  57. static void esp_wifi_set_debug_log()
  58. {
  59. /* set WiFi log level and module */
  60. #if CONFIG_ESP32_WIFI_DEBUG_LOG_ENABLE
  61. uint32_t g_wifi_log_level = WIFI_LOG_INFO;
  62. uint32_t g_wifi_log_module = 0;
  63. uint32_t g_wifi_log_submodule = 0;
  64. #if CONFIG_ESP32_WIFI_DEBUG_LOG_DEBUG
  65. g_wifi_log_level = WIFI_LOG_DEBUG;
  66. #endif
  67. #if CONFIG_ESP32_WIFI_DEBUG_LOG_VERBOSE
  68. g_wifi_log_level = WIFI_LOG_VERBOSE;
  69. #endif
  70. #if CONFIG_ESP32_WIFI_DEBUG_LOG_MODULE_ALL
  71. g_wifi_log_module = WIFI_LOG_MODULE_ALL;
  72. #endif
  73. #if CONFIG_ESP32_WIFI_DEBUG_LOG_MODULE_WIFI
  74. g_wifi_log_module = WIFI_LOG_MODULE_WIFI;
  75. #endif
  76. #if CONFIG_ESP32_WIFI_DEBUG_LOG_MODULE_COEX
  77. g_wifi_log_module = WIFI_LOG_MODULE_COEX;
  78. #endif
  79. #if CONFIG_ESP32_WIFI_DEBUG_LOG_MODULE_MESH
  80. g_wifi_log_module = WIFI_LOG_MODULE_MESH;
  81. #endif
  82. #if CONFIG_ESP32_WIFI_DEBUG_LOG_SUBMODULE_ALL
  83. g_wifi_log_submodule |= WIFI_LOG_SUBMODULE_ALL;
  84. #endif
  85. #if CONFIG_ESP32_WIFI_DEBUG_LOG_SUBMODULE_INIT
  86. g_wifi_log_submodule |= WIFI_LOG_SUBMODULE_INIT;
  87. #endif
  88. #if CONFIG_ESP32_WIFI_DEBUG_LOG_SUBMODULE_IOCTL
  89. g_wifi_log_submodule |= WIFI_LOG_SUBMODULE_IOCTL;
  90. #endif
  91. #if CONFIG_ESP32_WIFI_DEBUG_LOG_SUBMODULE_CONN
  92. g_wifi_log_submodule |= WIFI_LOG_SUBMODULE_CONN;
  93. #endif
  94. #if CONFIG_ESP32_WIFI_DEBUG_LOG_SUBMODULE_SCAN
  95. g_wifi_log_submodule |= WIFI_LOG_SUBMODULE_SCAN;
  96. #endif
  97. esp_wifi_internal_set_log_level(g_wifi_log_level);
  98. esp_wifi_internal_set_log_mod(g_wifi_log_module, g_wifi_log_submodule, true);
  99. #endif /* CONFIG_ESP32_WIFI_DEBUG_LOG_ENABLE*/
  100. }
  101. static void esp_wifi_config_info(void)
  102. {
  103. #ifdef CONFIG_ESP32_WIFI_RX_BA_WIN
  104. ESP_LOGI(TAG, "rx ba win: %d", CONFIG_ESP32_WIFI_RX_BA_WIN);
  105. #endif
  106. ESP_LOGI(TAG, "tcpip mbox: %d", CONFIG_TCPIP_RECVMBOX_SIZE);
  107. ESP_LOGI(TAG, "udp mbox: %d", CONFIG_UDP_RECVMBOX_SIZE);
  108. ESP_LOGI(TAG, "tcp mbox: %d", CONFIG_TCP_RECVMBOX_SIZE);
  109. ESP_LOGI(TAG, "tcp tx win: %d", CONFIG_TCP_SND_BUF_DEFAULT);
  110. ESP_LOGI(TAG, "tcp rx win: %d", CONFIG_TCP_WND_DEFAULT);
  111. ESP_LOGI(TAG, "tcp mss: %d", CONFIG_TCP_MSS);
  112. #ifdef CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP
  113. ESP_LOGI(TAG, "WiFi/LWIP prefer SPIRAM");
  114. #endif
  115. #ifdef CONFIG_ESP32_WIFI_IRAM_OPT
  116. ESP_LOGI(TAG, "WiFi IRAM OP enabled");
  117. #endif
  118. #ifdef CONFIG_ESP32_WIFI_RX_IRAM_OPT
  119. ESP_LOGI(TAG, "WiFi RX IRAM OP enabled");
  120. #endif
  121. #ifdef CONFIG_LWIP_IRAM_OPTIMIZATION
  122. ESP_LOGI(TAG, "LWIP IRAM OP enabled");
  123. #endif
  124. }
  125. esp_err_t esp_wifi_init(const wifi_init_config_t *config)
  126. {
  127. #ifdef CONFIG_PM_ENABLE
  128. if (s_wifi_modem_sleep_lock == NULL) {
  129. esp_err_t err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "wifi",
  130. &s_wifi_modem_sleep_lock);
  131. if (err != ESP_OK) {
  132. return err;
  133. }
  134. }
  135. #endif
  136. esp_event_set_default_wifi_handlers();
  137. esp_err_t result = esp_wifi_init_internal(config);
  138. if (result == ESP_OK) {
  139. esp_wifi_set_debug_log();
  140. s_wifi_mac_time_update_cb = esp_wifi_internal_update_mac_time;
  141. }
  142. esp_wifi_config_info();
  143. return result;
  144. }
  145. #ifdef CONFIG_PM_ENABLE
  146. void wifi_apb80m_request(void)
  147. {
  148. assert(s_wifi_modem_sleep_lock);
  149. esp_pm_lock_acquire(s_wifi_modem_sleep_lock);
  150. if (rtc_clk_apb_freq_get() != APB_CLK_FREQ) {
  151. ESP_LOGE(__func__, "WiFi needs 80MHz APB frequency to work, but got %dHz", rtc_clk_apb_freq_get());
  152. }
  153. }
  154. void wifi_apb80m_release(void)
  155. {
  156. assert(s_wifi_modem_sleep_lock);
  157. esp_pm_lock_release(s_wifi_modem_sleep_lock);
  158. }
  159. #endif //CONFIG_PM_ENABLE
  160. /* Coordinate ADC power with other modules. This overrides the function from PHY lib. */
  161. void set_xpd_sar(bool en)
  162. {
  163. if (s_wifi_adc_xpd_flag == en) {
  164. /* ignore repeated calls to set_xpd_sar when the state is already correct */
  165. return;
  166. }
  167. s_wifi_adc_xpd_flag = en;
  168. if (en) {
  169. adc_power_acquire();
  170. } else {
  171. adc_power_release();
  172. }
  173. }