From df3c857a3015a81db900047f5e62a4cfa19427ea Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 22 Sep 2017 23:34:52 +0800 Subject: [PATCH] esp32: add power management hooks for WiFi library --- components/esp32/wifi_init.c | 41 ++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/components/esp32/wifi_init.c b/components/esp32/wifi_init.c index e0f21b615..84bb4fc55 100644 --- a/components/esp32/wifi_init.c +++ b/components/esp32/wifi_init.c @@ -1,9 +1,9 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at - +// // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software @@ -14,10 +14,43 @@ #include #include +#include "esp_log.h" #include "esp_wifi_internal.h" +#include "esp_pm.h" +#include "soc/rtc.h" + +#ifdef CONFIG_PM_ENABLE +static esp_pm_lock_handle_t s_wifi_modem_sleep_lock; +#endif esp_err_t esp_wifi_init(const wifi_init_config_t *config) { - esp_event_set_default_wifi_handlers(); - return esp_wifi_init_internal(config); +#ifdef CONFIG_PM_ENABLE + if (s_wifi_modem_sleep_lock == NULL) { + esp_err_t err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "wifi", + &s_wifi_modem_sleep_lock); + if (err != ESP_OK) { + return err; + } + } +#endif + esp_event_set_default_wifi_handlers(); + return esp_wifi_init_internal(config); } + +#ifdef CONFIG_PM_ENABLE +void wifi_apb80m_request(void) +{ + assert(s_wifi_modem_sleep_lock); + esp_pm_lock_acquire(s_wifi_modem_sleep_lock); + if (rtc_clk_apb_freq_get() != APB_CLK_FREQ) { + ESP_LOGE(__func__, "WiFi needs 80MHz APB frequency to work, but got %dHz", rtc_clk_apb_freq_get()); + } +} + +void wifi_apb80m_release(void) +{ + assert(s_wifi_modem_sleep_lock); + esp_pm_lock_release(s_wifi_modem_sleep_lock); +} +#endif //CONFIG_PM_ENABLE