wifi_init.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /* mesh event callback handler */
  22. mesh_event_cb_t g_mesh_event_cb = NULL;
  23. #ifdef CONFIG_PM_ENABLE
  24. static esp_pm_lock_handle_t s_wifi_modem_sleep_lock;
  25. #endif
  26. static void __attribute__((constructor)) s_set_default_wifi_log_level()
  27. {
  28. /* WiFi libraries aren't compiled to know CONFIG_LOG_DEFAULT_LEVEL,
  29. so set it at runtime startup. Done here not in esp_wifi_init() to allow
  30. the user to set the level again before esp_wifi_init() is called.
  31. */
  32. esp_log_level_set("wifi", CONFIG_LOG_DEFAULT_LEVEL);
  33. }
  34. static void esp_wifi_set_debug_log()
  35. {
  36. /* set WiFi log level and module */
  37. #if CONFIG_ESP32_WIFI_DEBUG_LOG_ENABLE
  38. uint32_t g_wifi_log_level = WIFI_LOG_INFO;
  39. uint32_t g_wifi_log_module = 0;
  40. uint32_t g_wifi_log_submodule = 0;
  41. #if CONFIG_ESP32_WIFI_DEBUG_LOG_DEBUG
  42. g_wifi_log_level = WIFI_LOG_DEBUG;
  43. #endif
  44. #if CONFIG_ESP32_WIFI_DEBUG_LOG_VERBOSE
  45. g_wifi_log_level = WIFI_LOG_VERBOSE;
  46. #endif
  47. #if CONFIG_ESP32_WIFI_DEBUG_LOG_MODULE_ALL
  48. g_wifi_log_module = WIFI_LOG_MODULE_ALL;
  49. #endif
  50. #if CONFIG_ESP32_WIFI_DEBUG_LOG_MODULE_WIFI
  51. g_wifi_log_module = WIFI_LOG_MODULE_WIFI;
  52. #endif
  53. #if CONFIG_ESP32_WIFI_DEBUG_LOG_MODULE_COEX
  54. g_wifi_log_module = WIFI_LOG_MODULE_COEX;
  55. #endif
  56. #if CONFIG_ESP32_WIFI_DEBUG_LOG_MODULE_MESH
  57. g_wifi_log_module = WIFI_LOG_MODULE_MESH;
  58. #endif
  59. #if CONFIG_ESP32_WIFI_DEBUG_LOG_SUBMODULE_ALL
  60. g_wifi_log_submodule |= WIFI_LOG_SUBMODULE_ALL;
  61. #endif
  62. #if CONFIG_ESP32_WIFI_DEBUG_LOG_SUBMODULE_INIT
  63. g_wifi_log_submodule |= WIFI_LOG_SUBMODULE_INIT;
  64. #endif
  65. #if CONFIG_ESP32_WIFI_DEBUG_LOG_SUBMODULE_IOCTL
  66. g_wifi_log_submodule |= WIFI_LOG_SUBMODULE_IOCTL;
  67. #endif
  68. #if CONFIG_ESP32_WIFI_DEBUG_LOG_SUBMODULE_CONN
  69. g_wifi_log_submodule |= WIFI_LOG_SUBMODULE_CONN;
  70. #endif
  71. #if CONFIG_ESP32_WIFI_DEBUG_LOG_SUBMODULE_SCAN
  72. g_wifi_log_submodule |= WIFI_LOG_SUBMODULE_SCAN;
  73. #endif
  74. esp_wifi_internal_set_log_level(g_wifi_log_level);
  75. esp_wifi_internal_set_log_mod(g_wifi_log_module, g_wifi_log_submodule, true);
  76. #endif /* CONFIG_ESP32_WIFI_DEBUG_LOG_ENABLE*/
  77. }
  78. esp_err_t esp_wifi_init(const wifi_init_config_t *config)
  79. {
  80. #ifdef CONFIG_PM_ENABLE
  81. if (s_wifi_modem_sleep_lock == NULL) {
  82. esp_err_t err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "wifi",
  83. &s_wifi_modem_sleep_lock);
  84. if (err != ESP_OK) {
  85. return err;
  86. }
  87. }
  88. #endif
  89. esp_event_set_default_wifi_handlers();
  90. esp_err_t result = esp_wifi_init_internal(config);
  91. esp_wifi_set_debug_log();
  92. return result;
  93. }
  94. #ifdef CONFIG_PM_ENABLE
  95. void wifi_apb80m_request(void)
  96. {
  97. assert(s_wifi_modem_sleep_lock);
  98. esp_pm_lock_acquire(s_wifi_modem_sleep_lock);
  99. if (rtc_clk_apb_freq_get() != APB_CLK_FREQ) {
  100. ESP_LOGE(__func__, "WiFi needs 80MHz APB frequency to work, but got %dHz", rtc_clk_apb_freq_get());
  101. }
  102. }
  103. void wifi_apb80m_release(void)
  104. {
  105. assert(s_wifi_modem_sleep_lock);
  106. esp_pm_lock_release(s_wifi_modem_sleep_lock);
  107. }
  108. #endif //CONFIG_PM_ENABLE