2018-10-08 12:20:49 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 Gunar Schorcht
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup cpu_esp32
|
|
|
|
* @ingroup drivers_periph_pm
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Implementation of power management functions
|
|
|
|
*
|
|
|
|
* @author Gunar Schorcht <gunar@schorcht.net>
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2022-02-01 22:09:50 +01:00
|
|
|
/* RIOT headers have to be included before ESP-IDF headers! */
|
2018-10-08 12:20:49 +02:00
|
|
|
#include "esp_attr.h"
|
2020-02-20 08:30:06 +01:00
|
|
|
#include "gpio_arch.h"
|
2022-02-01 22:09:50 +01:00
|
|
|
#include "periph/rtc.h"
|
2020-03-29 13:27:53 +02:00
|
|
|
#include "rtt_arch.h"
|
2018-10-08 12:20:49 +02:00
|
|
|
#include "syscalls.h"
|
|
|
|
|
2022-02-01 22:09:50 +01:00
|
|
|
/* ESP-IDF headers */
|
|
|
|
#include "esp_sleep.h"
|
2018-10-08 12:20:49 +02:00
|
|
|
#include "rom/rtc.h"
|
|
|
|
#include "rom/uart.h"
|
|
|
|
#include "soc/rtc.h"
|
|
|
|
#include "soc/rtc_cntl_reg.h"
|
|
|
|
|
2022-02-01 22:09:50 +01:00
|
|
|
#define ENABLE_DEBUG 0
|
|
|
|
#include "debug.h"
|
|
|
|
|
2020-03-29 13:27:53 +02:00
|
|
|
extern void rtt_save_counter(void);
|
|
|
|
extern void rtt_restore_counter(void);
|
|
|
|
|
2020-03-23 12:33:45 +01:00
|
|
|
static inline esp_sleep_wakeup_cause_t pm_get_wakeup_cause(void)
|
|
|
|
{
|
|
|
|
return esp_sleep_get_wakeup_cause();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* function that is required by pm_set if esp_now and esp_wifi are not used */
|
|
|
|
esp_err_t __attribute__((weak)) esp_wifi_start(void)
|
|
|
|
{
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* function that is required by pm_set if esp_now and esp_wifi are not used */
|
|
|
|
esp_err_t __attribute__((weak)) esp_wifi_stop(void)
|
|
|
|
{
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2020-02-20 08:30:06 +01:00
|
|
|
static inline void pm_set_lowest_normal(void)
|
2018-10-08 12:20:49 +02:00
|
|
|
{
|
2020-02-20 08:30:06 +01:00
|
|
|
/* reset system watchdog timer */
|
|
|
|
system_wdt_feed();
|
2018-10-08 12:20:49 +02:00
|
|
|
|
2020-02-20 08:30:06 +01:00
|
|
|
#ifndef MODULE_ESP_QEMU
|
2018-10-08 12:20:49 +02:00
|
|
|
/* passive wait for interrupt to leave lowest power mode */
|
|
|
|
__asm__ volatile ("waiti 0");
|
|
|
|
|
|
|
|
/* reset system watchdog timer */
|
|
|
|
system_wdt_feed();
|
2020-02-20 08:30:06 +01:00
|
|
|
#endif
|
2018-10-08 12:20:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void IRAM_ATTR pm_off(void)
|
|
|
|
{
|
2020-02-20 08:30:06 +01:00
|
|
|
/* disable remaining power domains */
|
|
|
|
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_OFF);
|
|
|
|
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_OFF);
|
|
|
|
|
2020-02-19 10:31:52 +01:00
|
|
|
/* enter hibernate mode without any enabled wake-up sources */
|
2020-02-20 08:30:06 +01:00
|
|
|
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
|
2020-02-19 10:31:52 +01:00
|
|
|
esp_deep_sleep_start();
|
2018-10-08 12:20:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extern void esp_restart_noos(void) __attribute__ ((noreturn));
|
|
|
|
|
|
|
|
void pm_reboot(void)
|
|
|
|
{
|
|
|
|
DEBUG ("%s\n", __func__);
|
|
|
|
|
2020-03-23 12:33:45 +01:00
|
|
|
if (IS_USED(MODULE_ESP_WIFI_ANY)) {
|
|
|
|
/* stop WiFi if necessary */
|
|
|
|
esp_wifi_stop();
|
|
|
|
}
|
|
|
|
|
2018-10-08 12:20:49 +02:00
|
|
|
/* suspend and flush UARTs */
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
REG_SET_BIT(UART_FLOW_CONF_REG(i), UART_FORCE_XOFF);
|
|
|
|
uart_tx_wait_idle(i);
|
|
|
|
}
|
|
|
|
|
2020-03-29 13:27:53 +02:00
|
|
|
/* save RTT counters */
|
|
|
|
rtt_save_counter();
|
|
|
|
|
2018-10-08 12:20:49 +02:00
|
|
|
software_reset();
|
|
|
|
}
|
2020-02-20 08:30:06 +01:00
|
|
|
|
2022-02-01 22:09:50 +01:00
|
|
|
#if !IS_USED(MODULE_PM_LAYERED)
|
2020-02-20 08:30:06 +01:00
|
|
|
|
|
|
|
void pm_set_lowest(void)
|
|
|
|
{
|
|
|
|
pm_set_lowest_normal();
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* MODULE_PM_LAYERED */
|
|
|
|
|
|
|
|
void pm_set(unsigned mode)
|
|
|
|
{
|
|
|
|
if (mode == ESP_PM_MODEM_SLEEP) {
|
|
|
|
pm_set_lowest_normal();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG ("%s enter to power mode %d @%u\n", __func__, mode, system_get_time());
|
|
|
|
|
|
|
|
/* flush stdout */
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
/* Labels for RTC slow memory that are defined in the linker script */
|
|
|
|
extern int _rtc_bss_rtc_start;
|
|
|
|
extern int _rtc_bss_rtc_end;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Activate the Power Domain for slow RTC memory when the .rtc.bss
|
|
|
|
* section is used to retain uninitialized data. The Power Domain for
|
|
|
|
* slow RTC memory is automatically activated when the .rtc.data section
|
|
|
|
* is used to retain initialized data.
|
|
|
|
*/
|
2021-11-16 16:50:28 +01:00
|
|
|
/* cppcheck-suppress comparePointers */
|
2020-02-20 08:30:06 +01:00
|
|
|
if (&_rtc_bss_rtc_end > &_rtc_bss_rtc_start) {
|
|
|
|
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON);
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:44:32 +02:00
|
|
|
/* first disable all wake-up sources */
|
|
|
|
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
|
|
|
|
|
2020-02-20 08:30:06 +01:00
|
|
|
/* Prepare the RTC timer if an RTC alarm is set to wake up. */
|
2020-03-29 13:27:53 +02:00
|
|
|
rtt_pm_sleep_enter(mode);
|
2020-02-20 08:30:06 +01:00
|
|
|
|
|
|
|
/* Prepare GPIOs as wakeup source */
|
|
|
|
gpio_pm_sleep_enter(mode);
|
|
|
|
|
|
|
|
if (mode == ESP_PM_DEEP_SLEEP) {
|
|
|
|
esp_deep_sleep_start();
|
|
|
|
/* waking up from deep-sleep leads to a DEEPSLEEP_RESET */
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
else if (mode == ESP_PM_LIGHT_SLEEP) {
|
2020-03-23 12:33:45 +01:00
|
|
|
if (IS_USED(MODULE_ESP_WIFI_ANY)) {
|
|
|
|
/* stop WiFi if necessary */
|
|
|
|
esp_wifi_stop();
|
|
|
|
}
|
2020-02-20 08:30:06 +01:00
|
|
|
|
|
|
|
esp_light_sleep_start();
|
|
|
|
|
|
|
|
esp_sleep_wakeup_cause_t wakeup_reason = pm_get_wakeup_cause();
|
|
|
|
gpio_pm_sleep_exit(wakeup_reason);
|
2020-03-29 13:27:53 +02:00
|
|
|
rtt_pm_sleep_exit(wakeup_reason);
|
2020-02-20 08:30:06 +01:00
|
|
|
|
|
|
|
DEBUG ("%s exit from power mode %d @%u with reason %d\n", __func__,
|
|
|
|
mode, system_get_time(), wakeup_reason);
|
2020-03-23 12:33:45 +01:00
|
|
|
|
|
|
|
/* restart WiFi if necessary */
|
|
|
|
if (IS_USED(MODULE_ESP_WIFI_ANY) && (esp_wifi_start() != ESP_OK)) {
|
|
|
|
LOG_ERROR("esp_wifi_start failed\n");
|
2020-02-20 08:30:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MODULE_PM_LAYERED */
|