From 0a4e5c99f9576a346aae7e73bde577a79349420f Mon Sep 17 00:00:00 2001 From: Marius Vikhammer Date: Tue, 9 Feb 2021 19:30:43 +0800 Subject: [PATCH] deep_sleep: on S2 disable the brown out detector before deep sleeping On S2 the brown out detector would occasionally trigger erroneously during deep sleep. Disable it before sleeping to circumvent this issue. Closes https://github.com/espressif/esp-idf/issues/6179 --- components/esp32/include/esp32/brownout.h | 10 ++++++++++ components/esp32c3/include/esp32c3/brownout.h | 12 +++++++++++- components/esp32s2/include/esp32s2/brownout.h | 4 +++- components/esp32s3/include/esp32s3/brownout.h | 10 ++++++++++ components/esp_common/src/brownout.c | 15 +++++++++++++++ components/esp_system/sleep_modes.c | 7 +++++++ 6 files changed, 56 insertions(+), 2 deletions(-) diff --git a/components/esp32/include/esp32/brownout.h b/components/esp32/include/esp32/brownout.h index c17567ffb8..7fbe98c11a 100644 --- a/components/esp32/include/esp32/brownout.h +++ b/components/esp32/include/esp32/brownout.h @@ -16,6 +16,16 @@ #ifndef __ESP_BROWNOUT_H #define __ESP_BROWNOUT_H +#ifdef __cplusplus +extern "C" { +#endif + void esp_brownout_init(void); +void esp_brownout_disable(void); + +#ifdef __cplusplus +} +#endif + #endif diff --git a/components/esp32c3/include/esp32c3/brownout.h b/components/esp32c3/include/esp32c3/brownout.h index 80ddf1c948..da58a09309 100644 --- a/components/esp32c3/include/esp32c3/brownout.h +++ b/components/esp32c3/include/esp32c3/brownout.h @@ -1,4 +1,4 @@ -// Copyright 2015-2020 Espressif Systems (Shanghai) PTE LTD +// Copyright 2015-2021 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. @@ -16,6 +16,16 @@ #ifndef __ESP_BROWNOUT_H #define __ESP_BROWNOUT_H +#ifdef __cplusplus +extern "C" { +#endif + void esp_brownout_init(void); +void esp_brownout_disable(void); + +#ifdef __cplusplus +} +#endif + #endif diff --git a/components/esp32s2/include/esp32s2/brownout.h b/components/esp32s2/include/esp32s2/brownout.h index e55063cf16..397092cd44 100644 --- a/components/esp32s2/include/esp32s2/brownout.h +++ b/components/esp32s2/include/esp32s2/brownout.h @@ -1,4 +1,4 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// Copyright 2015-2021 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. @@ -22,6 +22,8 @@ extern "C" { void esp_brownout_init(void); +void esp_brownout_disable(void); + #ifdef __cplusplus } #endif diff --git a/components/esp32s3/include/esp32s3/brownout.h b/components/esp32s3/include/esp32s3/brownout.h index 80ddf1c948..f8b657a9fb 100644 --- a/components/esp32s3/include/esp32s3/brownout.h +++ b/components/esp32s3/include/esp32s3/brownout.h @@ -16,6 +16,16 @@ #ifndef __ESP_BROWNOUT_H #define __ESP_BROWNOUT_H +#ifdef __cplusplus +extern "C" { +#endif + void esp_brownout_init(void); +void esp_brownout_disable(void); + +#ifdef __cplusplus +} +#endif + #endif diff --git a/components/esp_common/src/brownout.c b/components/esp_common/src/brownout.c index f64b3d0b29..91eea5eecc 100644 --- a/components/esp_common/src/brownout.c +++ b/components/esp_common/src/brownout.c @@ -85,3 +85,18 @@ void esp_brownout_init(void) brownout_hal_intr_enable(true); #endif // not SOC_BROWNOUT_RESET_SUPPORTED } + +void esp_brownout_disable(void) +{ + brownout_hal_config_t cfg = { + .enabled = false, + }; + + brownout_hal_config(&cfg); + +#ifndef SOC_BROWNOUT_RESET_SUPPORTED + brownout_hal_intr_enable(false); + + rtc_isr_deregister(rtc_brownout_isr_handler, NULL); +#endif // not SOC_BROWNOUT_RESET_SUPPORTED +} diff --git a/components/esp_system/sleep_modes.c b/components/esp_system/sleep_modes.c index cbcd82b0ad..0f1964f97c 100644 --- a/components/esp_system/sleep_modes.c +++ b/components/esp_system/sleep_modes.c @@ -58,6 +58,7 @@ #include "esp32s2/clk.h" #include "esp32s2/rom/cache.h" #include "esp32s2/rom/rtc.h" +#include "esp32s2/brownout.h" #include "soc/extmem_reg.h" #include "driver/gpio.h" #elif CONFIG_IDF_TARGET_ESP32S3 @@ -588,6 +589,12 @@ inline static uint32_t IRAM_ATTR call_rtc_sleep_start(uint32_t reject_triggers) void IRAM_ATTR esp_deep_sleep_start(void) { +#if CONFIG_IDF_TARGET_ESP32S2 + /* Due to hardware limitations, on S2 the brownout detector sometimes trigger during deep sleep + to circumvent this we disable the brownout detector before sleeping */ + esp_brownout_disable(); +#endif //CONFIG_IDF_TARGET_ESP32S2 + // record current RTC time s_config.rtc_ticks_at_sleep_start = rtc_time_get();