test_sleep.c 20 KB

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