esp32: Fix not the right time spent in a deep sleep

Before entering the deep sleep, the RTC and FRC counters are synchronized. Updating the boot_time.
Added a unit test for this case.
Fixed warnings for MULTIPLE_STAGES

Closes https://github.com/espressif/esp-idf/issues/1840
This commit is contained in:
Konstantin Kondrashov 2018-06-04 12:39:18 +05:00
parent d441dd32ea
commit 7e2a3da643
6 changed files with 75 additions and 3 deletions

View file

@ -809,6 +809,24 @@ UT_001_24:
- ESP32_IDF
- UT_T1_1
UT_001_25:
<<: *unit_test_template
tags:
- ESP32_IDF
- UT_T1_1
UT_001_26:
<<: *unit_test_template
tags:
- ESP32_IDF
- UT_T1_1
UT_001_27:
<<: *unit_test_template
tags:
- ESP32_IDF
- UT_T1_1
UT_002_01:
<<: *unit_test_template
tags:
@ -901,6 +919,13 @@ UT_004_08:
- UT_T1_1
- psram
UT_004_09:
<<: *unit_test_template
tags:
- ESP32_IDF
- UT_T1_1
- psram
UT_005_01:
<<: *unit_test_template
tags:

View file

@ -203,7 +203,7 @@ void IRAM_ATTR esp_deep_sleep_start()
{
// record current RTC time
s_config.rtc_ticks_at_sleep_start = rtc_time_get();
esp_sync_counters_rtc_and_frc();
// Configure wake stub
if (esp_get_deep_sleep_wake_stub() == NULL) {
esp_set_deep_sleep_wake_stub(esp_wake_deep_sleep);

View file

@ -14,6 +14,8 @@
#include "soc/rtc.h" // for wakeup trigger defines
#include "soc/rtc_cntl_reg.h" // for read rtc registers directly (cause)
#include "soc/soc.h" // for direct register read macros
#include "rom/rtc.h"
#include "esp_newlib.h"
#define ESP_EXT0_WAKEUP_LEVEL_LOW 0
#define ESP_EXT0_WAKEUP_LEVEL_HIGH 1
@ -338,3 +340,35 @@ TEST_CASE("disable source trigger behavior", "[deepsleep]")
TEST_ASSERT(err_code == ESP_ERR_INVALID_STATE);
}
static RTC_DATA_ATTR struct timeval start;
static void trigger_deepsleep(void)
{
printf("Trigger deep sleep. Waiting 30 sec ...\n");
// Simulate the dispersion of the calibration coefficients at start-up.
// Corrupt the calibration factor.
esp_clk_slowclk_cal_set(esp_clk_slowclk_cal_get() - 1000000);
esp_set_time_from_rtc();
// Delay for time error accumulation.
vTaskDelay(30000/portTICK_RATE_MS);
// Save start time. Deep sleep.
gettimeofday(&start, NULL);
esp_sleep_enable_timer_wakeup(1000);
esp_deep_sleep_start();
}
static void check_time_deepsleep(void)
{
struct timeval stop;
RESET_REASON reason = rtc_get_reset_reason(0);
TEST_ASSERT(reason == DEEPSLEEP_RESET);
gettimeofday(&stop, NULL);
// Time dt_ms must in any case be positive.
int dt_ms = (stop.tv_sec - start.tv_sec) * 1000 + (stop.tv_usec - start.tv_usec) / 1000;
printf("delta time = %d \n", dt_ms);
TEST_ASSERT_MESSAGE(dt_ms > 0, "Time in deep sleep is negative");
}
TEST_CASE_MULTIPLE_STAGES("check a time after wakeup from deep sleep", "[deepsleep][reset=DEEPSLEEP_RESET]", trigger_deepsleep, check_time_deepsleep);

View file

@ -38,4 +38,9 @@ void esp_setup_syscall_table();
*/
void esp_set_time_from_rtc();
/*
* Sync counters RTC and FRC. Update boot_time.
*/
void esp_sync_counters_rtc_and_frc();
#endif //__ESP_NEWLIB_H__

View file

@ -371,3 +371,11 @@ uint64_t system_get_rtc_time(void)
#endif
}
void esp_sync_counters_rtc_and_frc()
{
#if defined( WITH_FRC ) && defined( WITH_RTC )
adjtime_corr_stop();
int64_t s_microseconds_offset_cur = get_rtc_time_us() - esp_timer_get_time();
set_boot_time(get_adjusted_boot_time() + ((int64_t)s_microseconds_offset - s_microseconds_offset_cur));
#endif
}

View file

@ -49,7 +49,7 @@
#define UNITY_TEST_FN_SET(...) \
static test_func UNITY_TEST_UID(test_functions)[] = {__VA_ARGS__}; \
static char* UNITY_TEST_UID(test_fn_name)[] = FN_NAME_SET(PP_NARG(__VA_ARGS__), __VA_ARGS__)
static const char* UNITY_TEST_UID(test_fn_name)[] = FN_NAME_SET(PP_NARG(__VA_ARGS__), __VA_ARGS__)
typedef void (* test_func)(void);
@ -62,7 +62,7 @@ struct test_desc_t
const char* file;
int line;
uint8_t test_fn_count;
char ** test_fn_name;
const char ** test_fn_name;
struct test_desc_t* next;
};