mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
examples/system/deep_sleep:
Use nvs instead of RTC_DATA_ATTR to record deep sleep enter time when the target chip does not have rtc mem.
This commit is contained in:
parent
001c6f5e86
commit
baaef3bd48
@ -21,6 +21,14 @@
|
|||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "driver/rtc_io.h"
|
#include "driver/rtc_io.h"
|
||||||
#include "soc/rtc.h"
|
#include "soc/rtc.h"
|
||||||
|
#include "nvs_flash.h"
|
||||||
|
#include "nvs.h"
|
||||||
|
|
||||||
|
#if SOC_RTC_FAST_MEM_SUPPORTED
|
||||||
|
static RTC_DATA_ATTR struct timeval sleep_enter_time;
|
||||||
|
#else
|
||||||
|
static struct timeval sleep_enter_time;
|
||||||
|
#endif
|
||||||
|
|
||||||
#if SOC_TOUCH_SENSOR_SUPPORTED
|
#if SOC_TOUCH_SENSOR_SUPPORTED
|
||||||
#include "soc/sens_periph.h"
|
#include "soc/sens_periph.h"
|
||||||
@ -36,8 +44,6 @@
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static RTC_DATA_ATTR struct timeval sleep_enter_time;
|
|
||||||
|
|
||||||
#ifdef CONFIG_EXAMPLE_TOUCH_WAKEUP
|
#ifdef CONFIG_EXAMPLE_TOUCH_WAKEUP
|
||||||
#if CONFIG_IDF_TARGET_ESP32
|
#if CONFIG_IDF_TARGET_ESP32
|
||||||
#define TOUCH_THRESH_NO_USE 0
|
#define TOUCH_THRESH_NO_USE 0
|
||||||
@ -47,6 +53,35 @@ static void calibrate_touch_pad(touch_pad_t pad);
|
|||||||
|
|
||||||
void app_main(void)
|
void app_main(void)
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Prefer to use RTC mem instead of NVS to save the deep sleep enter time, unless the chip
|
||||||
|
* does not support RTC mem(such as esp32c2). Because the time overhead of NVS will cause
|
||||||
|
* the recorded deep sleep enter time to be not very accurate.
|
||||||
|
*/
|
||||||
|
#if !SOC_RTC_FAST_MEM_SUPPORTED
|
||||||
|
// Initialize NVS
|
||||||
|
esp_err_t err = nvs_flash_init();
|
||||||
|
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||||
|
// NVS partition was truncated and needs to be erased
|
||||||
|
// Retry nvs_flash_init
|
||||||
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||||
|
err = nvs_flash_init();
|
||||||
|
}
|
||||||
|
ESP_ERROR_CHECK(err);
|
||||||
|
|
||||||
|
nvs_handle_t nvs_handle;
|
||||||
|
err = nvs_open("storage", NVS_READWRITE, &nvs_handle);
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err));
|
||||||
|
} else {
|
||||||
|
printf("Open NVS done\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get deep sleep enter time
|
||||||
|
nvs_get_i32(nvs_handle, "slp_enter_sec", (int32_t *)&sleep_enter_time.tv_sec);
|
||||||
|
nvs_get_i32(nvs_handle, "slp_enter_usec", (int32_t *)&sleep_enter_time.tv_usec);
|
||||||
|
#endif
|
||||||
|
|
||||||
struct timeval now;
|
struct timeval now;
|
||||||
gettimeofday(&now, NULL);
|
gettimeofday(&now, NULL);
|
||||||
int sleep_time_ms = (now.tv_sec - sleep_enter_time.tv_sec) * 1000 + (now.tv_usec - sleep_enter_time.tv_usec) / 1000;
|
int sleep_time_ms = (now.tv_sec - sleep_enter_time.tv_sec) * 1000 + (now.tv_usec - sleep_enter_time.tv_usec) / 1000;
|
||||||
@ -226,8 +261,19 @@ void app_main(void)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
printf("Entering deep sleep\n");
|
printf("Entering deep sleep\n");
|
||||||
|
|
||||||
|
// get deep sleep enter time
|
||||||
gettimeofday(&sleep_enter_time, NULL);
|
gettimeofday(&sleep_enter_time, NULL);
|
||||||
|
|
||||||
|
#if !SOC_RTC_FAST_MEM_SUPPORTED
|
||||||
|
// record deep sleep enter time via nvs
|
||||||
|
ESP_ERROR_CHECK(nvs_set_i32(nvs_handle, "slp_enter_sec", sleep_enter_time.tv_sec));
|
||||||
|
ESP_ERROR_CHECK(nvs_set_i32(nvs_handle, "slp_enter_usec", sleep_enter_time.tv_usec));
|
||||||
|
ESP_ERROR_CHECK(nvs_commit(nvs_handle));
|
||||||
|
nvs_close(nvs_handle);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// enter deep sleep
|
||||||
esp_deep_sleep_start();
|
esp_deep_sleep_start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user