test_sleep.c 19 KB

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