]> granicus.if.org Git - esp-idf/blob - components/esp32/wifi_init.c
Merge branch 'feature/esp-wrover-kit-v4_1' into 'master'
[esp-idf] / components / esp32 / wifi_init.c
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
15 #include <esp_event.h>
16 #include <esp_wifi.h>
17 #include "esp_log.h"
18 #include "esp_wifi_internal.h"
19 #include "esp_pm.h"
20 #include "soc/rtc.h"
21 #include "esp_mesh.h"
22
23 /* mesh event callback handler */
24 mesh_event_cb_t g_mesh_event_cb = NULL;
25
26 #ifdef CONFIG_PM_ENABLE
27 static esp_pm_lock_handle_t s_wifi_modem_sleep_lock;
28 #endif
29
30 static void __attribute__((constructor)) s_set_default_wifi_log_level()
31 {
32     /* WiFi libraries aren't compiled to know CONFIG_LOG_DEFAULT_LEVEL,
33        so set it at runtime startup. Done here not in esp_wifi_init() to allow
34        the user to set the level again before esp_wifi_init() is called.
35     */
36     esp_log_level_set("wifi", CONFIG_LOG_DEFAULT_LEVEL);
37 }
38
39 esp_err_t esp_wifi_init(const wifi_init_config_t *config)
40 {
41 #ifdef CONFIG_PM_ENABLE
42     if (s_wifi_modem_sleep_lock == NULL) {
43         esp_err_t err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "wifi",
44                 &s_wifi_modem_sleep_lock);
45         if (err != ESP_OK) {
46             return err;
47         }
48     }
49 #endif
50     esp_event_set_default_wifi_handlers();
51     return esp_wifi_init_internal(config);
52 }
53
54 #ifdef CONFIG_PM_ENABLE
55 void wifi_apb80m_request(void)
56 {
57     assert(s_wifi_modem_sleep_lock);
58     esp_pm_lock_acquire(s_wifi_modem_sleep_lock);
59     if (rtc_clk_apb_freq_get() != APB_CLK_FREQ) {
60         ESP_LOGE(__func__, "WiFi needs 80MHz APB frequency to work, but got %dHz", rtc_clk_apb_freq_get());
61     }
62 }
63
64 void wifi_apb80m_release(void)
65 {
66     assert(s_wifi_modem_sleep_lock);
67     esp_pm_lock_release(s_wifi_modem_sleep_lock);
68 }
69 #endif //CONFIG_PM_ENABLE