test_sleep.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. #include "unity.h"
  2. #include <sys/time.h>
  3. #include <sys/param.h>
  4. #include "esp_sleep.h"
  5. #include "driver/rtc_io.h"
  6. #include "esp_rom_uart.h"
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "freertos/semphr.h"
  10. #include "soc/gpio_periph.h"
  11. #include "hal/uart_types.h"
  12. #include "hal/uart_ll.h"
  13. #include "soc/rtc.h" // for wakeup trigger defines
  14. #include "soc/rtc_periph.h" // for read rtc registers directly (cause)
  15. #include "soc/soc.h" // for direct register read macros
  16. #include "hal/rtc_cntl_ll.h"
  17. #include "esp_newlib.h"
  18. #include "test_utils.h"
  19. #include "sdkconfig.h"
  20. #include "esp_rom_sys.h"
  21. #if CONFIG_IDF_TARGET_ESP32
  22. #include "esp32/clk.h"
  23. #include "esp32/rom/rtc.h"
  24. #elif CONFIG_IDF_TARGET_ESP32S2
  25. #include "esp32s2/clk.h"
  26. #include "esp32s2/rom/rtc.h"
  27. #elif CONFIG_IDF_TARGET_ESP32S3
  28. #include "esp32s3/clk.h"
  29. #include "esp32s3/rom/rtc.h"
  30. #endif
  31. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3)
  32. #define ESP_EXT0_WAKEUP_LEVEL_LOW 0
  33. #define ESP_EXT0_WAKEUP_LEVEL_HIGH 1
  34. __attribute__((unused)) static struct timeval tv_start, tv_stop;
  35. #ifndef CONFIG_FREERTOS_UNICORE
  36. static void deep_sleep_task(void *arg)
  37. {
  38. esp_deep_sleep_start();
  39. }
  40. static void do_deep_sleep_from_app_cpu(void)
  41. {
  42. xTaskCreatePinnedToCore(&deep_sleep_task, "ds", 2048, NULL, 5, NULL, 1);
  43. // keep running some non-IRAM code
  44. vTaskSuspendAll();
  45. while (true) {
  46. ;
  47. }
  48. }
  49. #endif
  50. TEST_CASE("wake up from deep sleep using timer", "[deepsleep][reset=DEEPSLEEP_RESET]")
  51. {
  52. esp_sleep_enable_timer_wakeup(2000000);
  53. esp_deep_sleep_start();
  54. }
  55. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2)
  56. TEST_CASE("light sleep followed by deep sleep", "[deepsleep][reset=DEEPSLEEP_RESET]")
  57. {
  58. esp_sleep_enable_timer_wakeup(1000000);
  59. esp_light_sleep_start();
  60. esp_deep_sleep_start();
  61. }
  62. TEST_CASE("wake up from light sleep using timer", "[deepsleep]")
  63. {
  64. esp_sleep_enable_timer_wakeup(2000000);
  65. struct timeval tv_start, tv_stop;
  66. gettimeofday(&tv_start, NULL);
  67. esp_light_sleep_start();
  68. gettimeofday(&tv_stop, NULL);
  69. float dt = (tv_stop.tv_sec - tv_start.tv_sec) * 1e3f +
  70. (tv_stop.tv_usec - tv_start.tv_usec) * 1e-3f;
  71. TEST_ASSERT_INT32_WITHIN(500, 2000, (int) dt);
  72. }
  73. #endif // !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2)
  74. static void test_light_sleep(void* arg)
  75. {
  76. vTaskDelay(2);
  77. for (int i = 0; i < 1000; ++i) {
  78. printf("%d %d\n", xPortGetCoreID(), i);
  79. fflush(stdout);
  80. esp_light_sleep_start();
  81. }
  82. SemaphoreHandle_t done = (SemaphoreHandle_t) arg;
  83. xSemaphoreGive(done);
  84. vTaskDelete(NULL);
  85. }
  86. TEST_CASE("light sleep stress test", "[deepsleep]")
  87. {
  88. SemaphoreHandle_t done = xSemaphoreCreateCounting(2, 0);
  89. esp_sleep_enable_timer_wakeup(1000);
  90. xTaskCreatePinnedToCore(&test_light_sleep, "ls0", 4096, done, UNITY_FREERTOS_PRIORITY + 1, NULL, 0);
  91. #if portNUM_PROCESSORS == 2
  92. xTaskCreatePinnedToCore(&test_light_sleep, "ls1", 4096, done, UNITY_FREERTOS_PRIORITY + 1, NULL, 1);
  93. #endif
  94. xSemaphoreTake(done, portMAX_DELAY);
  95. #if portNUM_PROCESSORS == 2
  96. xSemaphoreTake(done, portMAX_DELAY);
  97. #endif
  98. vSemaphoreDelete(done);
  99. }
  100. TEST_CASE("light sleep stress test with periodic esp_timer", "[deepsleep]")
  101. {
  102. void timer_func(void* arg)
  103. {
  104. esp_rom_delay_us(50);
  105. }
  106. SemaphoreHandle_t done = xSemaphoreCreateCounting(2, 0);
  107. esp_sleep_enable_timer_wakeup(1000);
  108. esp_timer_handle_t timer;
  109. esp_timer_create_args_t config = {
  110. .callback = &timer_func,
  111. };
  112. TEST_ESP_OK(esp_timer_create(&config, &timer));
  113. esp_timer_start_periodic(timer, 500);
  114. xTaskCreatePinnedToCore(&test_light_sleep, "ls1", 4096, done, UNITY_FREERTOS_PRIORITY + 1, NULL, 0);
  115. #if portNUM_PROCESSORS == 2
  116. xTaskCreatePinnedToCore(&test_light_sleep, "ls1", 4096, done, UNITY_FREERTOS_PRIORITY + 1, NULL, 1);
  117. #endif
  118. xSemaphoreTake(done, portMAX_DELAY);
  119. #if portNUM_PROCESSORS == 2
  120. xSemaphoreTake(done, portMAX_DELAY);
  121. #endif
  122. vSemaphoreDelete(done);
  123. esp_timer_stop(timer);
  124. esp_timer_delete(timer);
  125. }
  126. #if defined(CONFIG_ESP_SYSTEM_RTC_EXT_XTAL)
  127. #define MAX_SLEEP_TIME_ERROR_US 200
  128. #else
  129. #define MAX_SLEEP_TIME_ERROR_US 100
  130. #endif
  131. TEST_CASE("light sleep duration is correct", "[deepsleep][ignore]")
  132. {
  133. // don't power down XTAL — powering it up takes different time on
  134. // different boards
  135. esp_sleep_pd_config(ESP_PD_DOMAIN_XTAL, ESP_PD_OPTION_ON);
  136. // run one light sleep without checking timing, to warm up the cache
  137. esp_sleep_enable_timer_wakeup(1000);
  138. esp_light_sleep_start();
  139. const int sleep_intervals_ms[] = {
  140. 1, 1, 2, 3, 4, 5, 6, 7, 8, 10, 15,
  141. 20, 25, 50, 100, 200, 500,
  142. };
  143. const int sleep_intervals_count = sizeof(sleep_intervals_ms)/sizeof(sleep_intervals_ms[0]);
  144. for (int i = 0; i < sleep_intervals_count; ++i) {
  145. uint64_t sleep_time = sleep_intervals_ms[i] * 1000;
  146. esp_sleep_enable_timer_wakeup(sleep_time);
  147. for (int repeat = 0; repeat < 5; ++repeat) {
  148. uint64_t start = esp_clk_rtc_time();
  149. int64_t start_hs = esp_timer_get_time();
  150. esp_light_sleep_start();
  151. int64_t stop_hs = esp_timer_get_time();
  152. uint64_t stop = esp_clk_rtc_time();
  153. int diff_us = (int) (stop - start);
  154. int diff_hs_us = (int) (stop_hs - start_hs);
  155. printf("%lld %d\n", sleep_time, (int) (diff_us - sleep_time));
  156. int32_t threshold = MAX(sleep_time / 100, MAX_SLEEP_TIME_ERROR_US);
  157. TEST_ASSERT_INT32_WITHIN(threshold, sleep_time, diff_us);
  158. TEST_ASSERT_INT32_WITHIN(threshold, sleep_time, diff_hs_us);
  159. fflush(stdout);
  160. }
  161. vTaskDelay(10/portTICK_PERIOD_MS);
  162. }
  163. }
  164. TEST_CASE("light sleep and frequency switching", "[deepsleep]")
  165. {
  166. #ifndef CONFIG_PM_ENABLE
  167. uart_ll_set_sclk(UART_LL_GET_HW(CONFIG_ESP_CONSOLE_UART_NUM), UART_SCLK_REF_TICK);
  168. uart_ll_set_baudrate(UART_LL_GET_HW(CONFIG_ESP_CONSOLE_UART_NUM), CONFIG_ESP_CONSOLE_UART_BAUDRATE);
  169. #endif
  170. rtc_cpu_freq_config_t config_xtal, config_default;
  171. rtc_clk_cpu_freq_get_config(&config_default);
  172. rtc_clk_cpu_freq_mhz_to_config((int) rtc_clk_xtal_freq_get(), &config_xtal);
  173. esp_sleep_enable_timer_wakeup(1000);
  174. for (int i = 0; i < 1000; ++i) {
  175. if (i % 2 == 0) {
  176. rtc_clk_cpu_freq_set_config_fast(&config_xtal);
  177. } else {
  178. rtc_clk_cpu_freq_set_config_fast(&config_default);
  179. }
  180. printf("%d\n", i);
  181. fflush(stdout);
  182. esp_light_sleep_start();
  183. }
  184. }
  185. #ifndef CONFIG_FREERTOS_UNICORE
  186. TEST_CASE("enter deep sleep on APP CPU and wake up using timer", "[deepsleep][reset=DEEPSLEEP_RESET]")
  187. {
  188. esp_sleep_enable_timer_wakeup(2000000);
  189. do_deep_sleep_from_app_cpu();
  190. }
  191. #endif
  192. static void do_deep_sleep(void)
  193. {
  194. esp_sleep_enable_timer_wakeup(100000);
  195. esp_deep_sleep_start();
  196. }
  197. static void check_sleep_reset_and_sleep(void)
  198. {
  199. TEST_ASSERT_EQUAL(ESP_RST_DEEPSLEEP, esp_reset_reason());
  200. esp_sleep_enable_timer_wakeup(100000);
  201. esp_deep_sleep_start();
  202. }
  203. static void check_sleep_reset(void)
  204. {
  205. TEST_ASSERT_EQUAL(ESP_RST_DEEPSLEEP, esp_reset_reason());
  206. }
  207. TEST_CASE_MULTIPLE_STAGES("enter deep sleep more than once", "[deepsleep][reset=DEEPSLEEP_RESET,DEEPSLEEP_RESET,DEEPSLEEP_RESET]",
  208. do_deep_sleep,
  209. check_sleep_reset_and_sleep,
  210. check_sleep_reset_and_sleep,
  211. check_sleep_reset);
  212. static void do_abort(void)
  213. {
  214. abort();
  215. }
  216. static void check_abort_reset_and_sleep(void)
  217. {
  218. TEST_ASSERT_EQUAL(ESP_RST_PANIC, esp_reset_reason());
  219. esp_sleep_enable_timer_wakeup(100000);
  220. esp_deep_sleep_start();
  221. }
  222. TEST_CASE_MULTIPLE_STAGES("enter deep sleep after abort", "[deepsleep][reset=abort,SW_CPU_RESET,DEEPSLEEP_RESET]",
  223. do_abort,
  224. check_abort_reset_and_sleep,
  225. check_sleep_reset);
  226. static RTC_DATA_ATTR uint32_t s_wake_stub_var;
  227. static RTC_IRAM_ATTR void wake_stub(void)
  228. {
  229. esp_default_wake_deep_sleep();
  230. s_wake_stub_var = (uint32_t) &wake_stub;
  231. }
  232. static void prepare_wake_stub(void)
  233. {
  234. esp_set_deep_sleep_wake_stub(&wake_stub);
  235. esp_sleep_enable_timer_wakeup(100000);
  236. esp_deep_sleep_start();
  237. }
  238. static void check_wake_stub(void)
  239. {
  240. TEST_ASSERT_EQUAL(ESP_RST_DEEPSLEEP, esp_reset_reason());
  241. TEST_ASSERT_EQUAL_HEX32((uint32_t) &wake_stub, s_wake_stub_var);
  242. /* ROM code clears wake stub entry address */
  243. TEST_ASSERT_NULL(esp_get_deep_sleep_wake_stub());
  244. }
  245. TEST_CASE_MULTIPLE_STAGES("can set sleep wake stub", "[deepsleep][reset=DEEPSLEEP_RESET]",
  246. prepare_wake_stub,
  247. check_wake_stub);
  248. #if CONFIG_ESP32_ALLOW_RTC_FAST_MEM_AS_HEAP || CONFIG_ESP32S2_ALLOW_RTC_FAST_MEM_AS_HEAP \
  249. || CONFIG_ESP32S3_ALLOW_RTC_FAST_MEM_AS_HEAP
  250. #if CONFIG_FREERTOS_SUPPORT_STATIC_ALLOCATION
  251. /* Version of prepare_wake_stub() that sets up the deep sleep call while running
  252. from RTC memory as stack, with a high frequency timer also writing RTC FAST
  253. memory.
  254. This is important because the ROM code (ESP32 & ESP32-S2) requires software
  255. trigger a CRC calculation (done in hardware) for the entire RTC FAST memory
  256. before going to deep sleep and if it's invalid then the stub is not
  257. run. Also, while the CRC is being calculated the RTC FAST memory is not
  258. accesible by the CPU (reads all zeros).
  259. */
  260. static void increment_rtc_memory_cb(void *arg)
  261. {
  262. static volatile RTC_FAST_ATTR unsigned counter;
  263. counter++;
  264. }
  265. static void prepare_wake_stub_from_rtc(void)
  266. {
  267. /* RTC memory can be used as heap, however there is no API call that returns this as
  268. a memory capability (as it's an implementation detail). So to test this we need to allocate
  269. the stack statically.
  270. */
  271. static RTC_FAST_ATTR uint8_t sleep_stack[1024];
  272. static RTC_FAST_ATTR StaticTask_t sleep_task;
  273. /* normally BSS like sleep_stack will be cleared on reset, but RTC memory is not cleared on
  274. * wake from deep sleep. So to ensure unused stack is different if test is re-run without a full reset,
  275. * fill with some random bytes
  276. */
  277. esp_fill_random(sleep_stack, sizeof(sleep_stack));
  278. /* to make things extra sure, start a periodic timer to write to RTC FAST RAM at high frequency */
  279. const esp_timer_create_args_t timer_args = {
  280. .callback = increment_rtc_memory_cb,
  281. .arg = NULL,
  282. .dispatch_method = ESP_TIMER_TASK,
  283. .name = "Write RTC MEM"
  284. };
  285. esp_timer_handle_t timer;
  286. ESP_ERROR_CHECK( esp_timer_create(&timer_args, &timer) );
  287. ESP_ERROR_CHECK( esp_timer_start_periodic(timer, 200) );
  288. printf("Creating test task with stack %p\n", sleep_stack);
  289. TEST_ASSERT_NOT_NULL(xTaskCreateStatic( (void *)prepare_wake_stub, "sleep", sizeof(sleep_stack), NULL,
  290. UNITY_FREERTOS_PRIORITY, sleep_stack, &sleep_task));
  291. vTaskDelay(1000 / portTICK_PERIOD_MS);
  292. TEST_FAIL_MESSAGE("Should be asleep by now");
  293. }
  294. TEST_CASE_MULTIPLE_STAGES("can set sleep wake stub from stack in RTC RAM", "[deepsleep][reset=DEEPSLEEP_RESET]",
  295. prepare_wake_stub_from_rtc,
  296. check_wake_stub);
  297. #endif // CONFIG_FREERTOS_SUPPORT_STATIC_ALLOCATION
  298. #endif // CONFIG_xyz_ALLOW_RTC_FAST_MEM_AS_HEAP
  299. TEST_CASE("wake up using ext0 (13 high)", "[deepsleep][ignore]")
  300. {
  301. ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
  302. ESP_ERROR_CHECK(gpio_pullup_dis(GPIO_NUM_13));
  303. ESP_ERROR_CHECK(gpio_pulldown_en(GPIO_NUM_13));
  304. ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, ESP_EXT0_WAKEUP_LEVEL_HIGH));
  305. esp_deep_sleep_start();
  306. }
  307. TEST_CASE("wake up using ext0 (13 low)", "[deepsleep][ignore]")
  308. {
  309. ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
  310. ESP_ERROR_CHECK(gpio_pullup_en(GPIO_NUM_13));
  311. ESP_ERROR_CHECK(gpio_pulldown_dis(GPIO_NUM_13));
  312. ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, ESP_EXT0_WAKEUP_LEVEL_LOW));
  313. esp_deep_sleep_start();
  314. }
  315. TEST_CASE("wake up using ext1 when RTC_PERIPH is off (13 high)", "[deepsleep][ignore]")
  316. {
  317. // This test needs external pulldown
  318. ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
  319. ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(BIT(GPIO_NUM_13), ESP_EXT1_WAKEUP_ANY_HIGH));
  320. esp_deep_sleep_start();
  321. }
  322. TEST_CASE("wake up using ext1 when RTC_PERIPH is off (13 low)", "[deepsleep][ignore]")
  323. {
  324. // This test needs external pullup
  325. ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
  326. ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(BIT(GPIO_NUM_13), ESP_EXT1_WAKEUP_ALL_LOW));
  327. esp_deep_sleep_start();
  328. }
  329. TEST_CASE("wake up using ext1 when RTC_PERIPH is on (13 high)", "[deepsleep][ignore]")
  330. {
  331. ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
  332. ESP_ERROR_CHECK(gpio_pullup_dis(GPIO_NUM_13));
  333. ESP_ERROR_CHECK(gpio_pulldown_en(GPIO_NUM_13));
  334. ESP_ERROR_CHECK(esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON));
  335. ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(BIT(GPIO_NUM_13), ESP_EXT1_WAKEUP_ANY_HIGH));
  336. esp_deep_sleep_start();
  337. }
  338. TEST_CASE("wake up using ext1 when RTC_PERIPH is on (13 low)", "[deepsleep][ignore]")
  339. {
  340. ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
  341. ESP_ERROR_CHECK(gpio_pullup_en(GPIO_NUM_13));
  342. ESP_ERROR_CHECK(gpio_pulldown_dis(GPIO_NUM_13));
  343. ESP_ERROR_CHECK(esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON));
  344. ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(BIT(GPIO_NUM_13), ESP_EXT1_WAKEUP_ALL_LOW));
  345. esp_deep_sleep_start();
  346. }
  347. __attribute__((unused)) static float get_time_ms(void)
  348. {
  349. gettimeofday(&tv_stop, NULL);
  350. float dt = (tv_stop.tv_sec - tv_start.tv_sec) * 1e3f +
  351. (tv_stop.tv_usec - tv_start.tv_usec) * 1e-3f;
  352. return fabs(dt);
  353. }
  354. __attribute__((unused)) static uint32_t get_cause(void)
  355. {
  356. uint32_t wakeup_cause = REG_GET_FIELD(RTC_CNTL_WAKEUP_STATE_REG, \
  357. RTC_CNTL_WAKEUP_CAUSE);
  358. return wakeup_cause;
  359. }
  360. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2)
  361. // This test case verifies deactivation of trigger for wake up sources
  362. TEST_CASE("disable source trigger behavior", "[deepsleep]")
  363. {
  364. float dt = 0;
  365. printf("Setup timer and ext0 to wake up immediately from GPIO_13 \n");
  366. // Setup ext0 configuration to wake up almost immediately
  367. // The wakeup time is proportional to input capacitance * pullup resistance
  368. ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
  369. ESP_ERROR_CHECK(gpio_pullup_en(GPIO_NUM_13));
  370. ESP_ERROR_CHECK(gpio_pulldown_dis(GPIO_NUM_13));
  371. ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, ESP_EXT0_WAKEUP_LEVEL_HIGH));
  372. // Setup timer to wakeup with timeout
  373. esp_sleep_enable_timer_wakeup(2000000);
  374. // Save start time
  375. gettimeofday(&tv_start, NULL);
  376. esp_light_sleep_start();
  377. dt = get_time_ms();
  378. printf("Ext0 sleep time = %d \n", (int) dt);
  379. // Check wakeup from Ext0 using time measurement because wakeup cause is
  380. // not available in light sleep mode
  381. TEST_ASSERT_INT32_WITHIN(100, 100, (int) dt);
  382. TEST_ASSERT((get_cause() & RTC_EXT0_TRIG_EN) != 0);
  383. // Disable Ext0 source. Timer source should be triggered
  384. ESP_ERROR_CHECK(esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_EXT0));
  385. printf("Disable ext0 trigger and leave timer active.\n");
  386. gettimeofday(&tv_start, NULL);
  387. esp_light_sleep_start();
  388. dt = get_time_ms();
  389. printf("Timer sleep time = %d \n", (int) dt);
  390. TEST_ASSERT_INT32_WITHIN(500, 2000, (int) dt);
  391. // Additionally check wakeup cause
  392. TEST_ASSERT((get_cause() & RTC_TIMER_TRIG_EN) != 0);
  393. // Disable timer source.
  394. ESP_ERROR_CHECK(esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER));
  395. // Setup ext0 configuration to wake up immediately
  396. ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
  397. ESP_ERROR_CHECK(gpio_pullup_en(GPIO_NUM_13));
  398. ESP_ERROR_CHECK(gpio_pulldown_dis(GPIO_NUM_13));
  399. ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, ESP_EXT0_WAKEUP_LEVEL_HIGH));
  400. printf("Disable timer trigger to wake up from ext0 source.\n");
  401. gettimeofday(&tv_start, NULL);
  402. esp_light_sleep_start();
  403. dt = get_time_ms();
  404. printf("Ext0 sleep time = %d \n", (int) dt);
  405. TEST_ASSERT_INT32_WITHIN(100, 100, (int) dt);
  406. TEST_ASSERT((get_cause() & RTC_EXT0_TRIG_EN) != 0);
  407. // Check error message when source is already disabled
  408. esp_err_t err_code = esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER);
  409. TEST_ASSERT(err_code == ESP_ERR_INVALID_STATE);
  410. // Disable ext0 wakeup source, as this might interfere with other tests
  411. ESP_ERROR_CHECK(esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_EXT0));
  412. }
  413. #endif // !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2)
  414. static RTC_DATA_ATTR struct timeval start;
  415. static void trigger_deepsleep(void)
  416. {
  417. printf("Trigger deep sleep. Waiting for 10 sec ...\n");
  418. // Simulate the dispersion of the calibration coefficients at start-up.
  419. // Corrupt the calibration factor.
  420. esp_clk_slowclk_cal_set(esp_clk_slowclk_cal_get() / 2);
  421. esp_set_time_from_rtc();
  422. // Delay for time error accumulation.
  423. vTaskDelay(10000/portTICK_RATE_MS);
  424. // Save start time. Deep sleep.
  425. gettimeofday(&start, NULL);
  426. esp_sleep_enable_timer_wakeup(1000);
  427. // In function esp_deep_sleep_start() uses function esp_sync_counters_rtc_and_frc()
  428. // to prevent a negative time after wake up.
  429. esp_deep_sleep_start();
  430. }
  431. static void check_time_deepsleep(void)
  432. {
  433. struct timeval stop;
  434. RESET_REASON reason = rtc_get_reset_reason(0);
  435. TEST_ASSERT(reason == DEEPSLEEP_RESET);
  436. gettimeofday(&stop, NULL);
  437. // Time dt_ms must in any case be positive.
  438. int dt_ms = (stop.tv_sec - start.tv_sec) * 1000 + (stop.tv_usec - start.tv_usec) / 1000;
  439. printf("delta time = %d \n", dt_ms);
  440. TEST_ASSERT_MESSAGE(dt_ms > 0, "Time in deep sleep is negative");
  441. }
  442. TEST_CASE_MULTIPLE_STAGES("check a time after wakeup from deep sleep", "[deepsleep][reset=DEEPSLEEP_RESET]", trigger_deepsleep, check_time_deepsleep);
  443. #endif // #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3)