test_sleep.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. #include "unity.h"
  2. #include <sys/time.h>
  3. #include <sys/param.h>
  4. #include "esp_sleep.h"
  5. #include "esp32/clk.h"
  6. #include "driver/rtc_io.h"
  7. #include "esp32/rom/uart.h"
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "freertos/semphr.h"
  11. #include "soc/gpio_periph.h"
  12. #include "soc/uart_periph.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 "esp32/rom/rtc.h"
  17. #include "esp_newlib.h"
  18. #include "test_utils.h"
  19. #include "sdkconfig.h"
  20. #define ESP_EXT0_WAKEUP_LEVEL_LOW 0
  21. #define ESP_EXT0_WAKEUP_LEVEL_HIGH 1
  22. static struct timeval tv_start, tv_stop;
  23. #ifndef CONFIG_FREERTOS_UNICORE
  24. static void deep_sleep_task(void *arg)
  25. {
  26. esp_deep_sleep_start();
  27. }
  28. static void do_deep_sleep_from_app_cpu()
  29. {
  30. xTaskCreatePinnedToCore(&deep_sleep_task, "ds", 2048, NULL, 5, NULL, 1);
  31. // keep running some non-IRAM code
  32. vTaskSuspendAll();
  33. while (true) {
  34. ;
  35. }
  36. }
  37. #endif
  38. TEST_CASE("wake up from deep sleep using timer", "[deepsleep][reset=DEEPSLEEP_RESET]")
  39. {
  40. esp_sleep_enable_timer_wakeup(2000000);
  41. esp_deep_sleep_start();
  42. }
  43. TEST_CASE("light sleep followed by deep sleep", "[deepsleep][reset=DEEPSLEEP_RESET]")
  44. {
  45. esp_sleep_enable_timer_wakeup(1000000);
  46. esp_light_sleep_start();
  47. esp_deep_sleep_start();
  48. }
  49. TEST_CASE("wake up from light sleep using timer", "[deepsleep]")
  50. {
  51. esp_sleep_enable_timer_wakeup(2000000);
  52. struct timeval tv_start, tv_stop;
  53. gettimeofday(&tv_start, NULL);
  54. esp_light_sleep_start();
  55. gettimeofday(&tv_stop, NULL);
  56. float dt = (tv_stop.tv_sec - tv_start.tv_sec) * 1e3f +
  57. (tv_stop.tv_usec - tv_start.tv_usec) * 1e-3f;
  58. TEST_ASSERT_INT32_WITHIN(500, 2000, (int) dt);
  59. }
  60. static void test_light_sleep(void* arg)
  61. {
  62. vTaskDelay(2);
  63. for (int i = 0; i < 1000; ++i) {
  64. printf("%d %d\n", xPortGetCoreID(), i);
  65. fflush(stdout);
  66. esp_light_sleep_start();
  67. }
  68. SemaphoreHandle_t done = (SemaphoreHandle_t) arg;
  69. xSemaphoreGive(done);
  70. vTaskDelete(NULL);
  71. }
  72. TEST_CASE("light sleep stress test", "[deepsleep]")
  73. {
  74. SemaphoreHandle_t done = xSemaphoreCreateCounting(2, 0);
  75. esp_sleep_enable_timer_wakeup(1000);
  76. xTaskCreatePinnedToCore(&test_light_sleep, "ls0", 4096, done, UNITY_FREERTOS_PRIORITY + 1, NULL, 0);
  77. #if portNUM_PROCESSORS == 2
  78. xTaskCreatePinnedToCore(&test_light_sleep, "ls1", 4096, done, UNITY_FREERTOS_PRIORITY + 1, NULL, 1);
  79. #endif
  80. xSemaphoreTake(done, portMAX_DELAY);
  81. #if portNUM_PROCESSORS == 2
  82. xSemaphoreTake(done, portMAX_DELAY);
  83. #endif
  84. vSemaphoreDelete(done);
  85. }
  86. TEST_CASE("light sleep stress test with periodic esp_timer", "[deepsleep]")
  87. {
  88. void timer_func(void* arg)
  89. {
  90. ets_delay_us(50);
  91. }
  92. SemaphoreHandle_t done = xSemaphoreCreateCounting(2, 0);
  93. esp_sleep_enable_timer_wakeup(1000);
  94. esp_timer_handle_t timer;
  95. esp_timer_create_args_t config = {
  96. .callback = &timer_func,
  97. };
  98. TEST_ESP_OK(esp_timer_create(&config, &timer));
  99. esp_timer_start_periodic(timer, 500);
  100. xTaskCreatePinnedToCore(&test_light_sleep, "ls1", 4096, done, UNITY_FREERTOS_PRIORITY + 1, NULL, 0);
  101. #if portNUM_PROCESSORS == 2
  102. xTaskCreatePinnedToCore(&test_light_sleep, "ls1", 4096, done, UNITY_FREERTOS_PRIORITY + 1, NULL, 1);
  103. #endif
  104. xSemaphoreTake(done, portMAX_DELAY);
  105. #if portNUM_PROCESSORS == 2
  106. xSemaphoreTake(done, portMAX_DELAY);
  107. #endif
  108. vSemaphoreDelete(done);
  109. esp_timer_stop(timer);
  110. esp_timer_delete(timer);
  111. }
  112. #ifdef CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS
  113. #define MAX_SLEEP_TIME_ERROR_US 200
  114. #else
  115. #define MAX_SLEEP_TIME_ERROR_US 100
  116. #endif
  117. TEST_CASE("light sleep duration is correct", "[deepsleep][ignore]")
  118. {
  119. // don't power down XTAL — powering it up takes different time on
  120. // different boards
  121. esp_sleep_pd_config(ESP_PD_DOMAIN_XTAL, ESP_PD_OPTION_ON);
  122. // run one light sleep without checking timing, to warm up the cache
  123. esp_sleep_enable_timer_wakeup(1000);
  124. esp_light_sleep_start();
  125. const int sleep_intervals_ms[] = {
  126. 1, 1, 2, 3, 4, 5, 6, 7, 8, 10, 15,
  127. 20, 25, 50, 100, 200, 500,
  128. };
  129. const int sleep_intervals_count = sizeof(sleep_intervals_ms)/sizeof(sleep_intervals_ms[0]);
  130. for (int i = 0; i < sleep_intervals_count; ++i) {
  131. uint64_t sleep_time = sleep_intervals_ms[i] * 1000;
  132. esp_sleep_enable_timer_wakeup(sleep_time);
  133. for (int repeat = 0; repeat < 5; ++repeat) {
  134. uint64_t start = esp_clk_rtc_time();
  135. int64_t start_hs = esp_timer_get_time();
  136. esp_light_sleep_start();
  137. int64_t stop_hs = esp_timer_get_time();
  138. uint64_t stop = esp_clk_rtc_time();
  139. int diff_us = (int) (stop - start);
  140. int diff_hs_us = (int) (stop_hs - start_hs);
  141. printf("%lld %d\n", sleep_time, (int) (diff_us - sleep_time));
  142. int32_t threshold = MAX(sleep_time / 100, MAX_SLEEP_TIME_ERROR_US);
  143. TEST_ASSERT_INT32_WITHIN(threshold, sleep_time, diff_us);
  144. TEST_ASSERT_INT32_WITHIN(threshold, sleep_time, diff_hs_us);
  145. fflush(stdout);
  146. }
  147. vTaskDelay(10/portTICK_PERIOD_MS);
  148. }
  149. }
  150. TEST_CASE("light sleep and frequency switching", "[deepsleep]")
  151. {
  152. #ifndef CONFIG_PM_ENABLE
  153. const int uart_clk_freq = REF_CLK_FREQ;
  154. CLEAR_PERI_REG_MASK(UART_CONF0_REG(CONFIG_ESP_CONSOLE_UART_NUM), UART_TICK_REF_ALWAYS_ON);
  155. uart_div_modify(CONFIG_ESP_CONSOLE_UART_NUM, (uart_clk_freq << 4) / CONFIG_ESP_CONSOLE_UART_BAUDRATE);
  156. #endif
  157. rtc_cpu_freq_config_t config_xtal, config_default;
  158. rtc_clk_cpu_freq_get_config(&config_default);
  159. rtc_clk_cpu_freq_mhz_to_config((int) rtc_clk_xtal_freq_get(), &config_xtal);
  160. esp_sleep_enable_timer_wakeup(1000);
  161. for (int i = 0; i < 1000; ++i) {
  162. if (i % 2 == 0) {
  163. rtc_clk_cpu_freq_set_config_fast(&config_xtal);
  164. } else {
  165. rtc_clk_cpu_freq_set_config_fast(&config_default);
  166. }
  167. printf("%d\n", i);
  168. fflush(stdout);
  169. esp_light_sleep_start();
  170. }
  171. }
  172. #ifndef CONFIG_FREERTOS_UNICORE
  173. TEST_CASE("enter deep sleep on APP CPU and wake up using timer", "[deepsleep][reset=DEEPSLEEP_RESET]")
  174. {
  175. esp_sleep_enable_timer_wakeup(2000000);
  176. do_deep_sleep_from_app_cpu();
  177. }
  178. #endif
  179. static void do_deep_sleep()
  180. {
  181. esp_sleep_enable_timer_wakeup(100000);
  182. esp_deep_sleep_start();
  183. }
  184. static void check_sleep_reset_and_sleep()
  185. {
  186. TEST_ASSERT_EQUAL(ESP_RST_DEEPSLEEP, esp_reset_reason());
  187. esp_sleep_enable_timer_wakeup(100000);
  188. esp_deep_sleep_start();
  189. }
  190. static void check_sleep_reset()
  191. {
  192. TEST_ASSERT_EQUAL(ESP_RST_DEEPSLEEP, esp_reset_reason());
  193. }
  194. TEST_CASE_MULTIPLE_STAGES("enter deep sleep more than once", "[deepsleep][reset=DEEPSLEEP_RESET,DEEPSLEEP_RESET,DEEPSLEEP_RESET]",
  195. do_deep_sleep,
  196. check_sleep_reset_and_sleep,
  197. check_sleep_reset_and_sleep,
  198. check_sleep_reset);
  199. static void do_abort()
  200. {
  201. abort();
  202. }
  203. static void check_abort_reset_and_sleep()
  204. {
  205. TEST_ASSERT_EQUAL(ESP_RST_PANIC, esp_reset_reason());
  206. esp_sleep_enable_timer_wakeup(100000);
  207. esp_deep_sleep_start();
  208. }
  209. TEST_CASE_MULTIPLE_STAGES("enter deep sleep after abort", "[deepsleep][reset=abort,SW_CPU_RESET,DEEPSLEEP_RESET]",
  210. do_abort,
  211. check_abort_reset_and_sleep,
  212. check_sleep_reset);
  213. static RTC_DATA_ATTR uint32_t s_wake_stub_var;
  214. static RTC_IRAM_ATTR void wake_stub()
  215. {
  216. esp_default_wake_deep_sleep();
  217. s_wake_stub_var = (uint32_t) &wake_stub;
  218. }
  219. static void prepare_wake_stub()
  220. {
  221. esp_set_deep_sleep_wake_stub(&wake_stub);
  222. esp_sleep_enable_timer_wakeup(100000);
  223. esp_deep_sleep_start();
  224. }
  225. static void check_wake_stub()
  226. {
  227. TEST_ASSERT_EQUAL(ESP_RST_DEEPSLEEP, esp_reset_reason());
  228. TEST_ASSERT_EQUAL_HEX32((uint32_t) &wake_stub, s_wake_stub_var);
  229. /* ROM code clears wake stub entry address */
  230. TEST_ASSERT_NULL(esp_get_deep_sleep_wake_stub());
  231. }
  232. TEST_CASE_MULTIPLE_STAGES("can set sleep wake stub", "[deepsleep][reset=DEEPSLEEP_RESET]",
  233. prepare_wake_stub,
  234. check_wake_stub);
  235. TEST_CASE("wake up using ext0 (13 high)", "[deepsleep][ignore]")
  236. {
  237. ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
  238. ESP_ERROR_CHECK(gpio_pullup_dis(GPIO_NUM_13));
  239. ESP_ERROR_CHECK(gpio_pulldown_en(GPIO_NUM_13));
  240. ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, ESP_EXT0_WAKEUP_LEVEL_HIGH));
  241. esp_deep_sleep_start();
  242. }
  243. TEST_CASE("wake up using ext0 (13 low)", "[deepsleep][ignore]")
  244. {
  245. ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
  246. ESP_ERROR_CHECK(gpio_pullup_en(GPIO_NUM_13));
  247. ESP_ERROR_CHECK(gpio_pulldown_dis(GPIO_NUM_13));
  248. ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, ESP_EXT0_WAKEUP_LEVEL_LOW));
  249. esp_deep_sleep_start();
  250. }
  251. TEST_CASE("wake up using ext1 when RTC_PERIPH is off (13 high)", "[deepsleep][ignore]")
  252. {
  253. // This test needs external pulldown
  254. ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
  255. ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(BIT(GPIO_NUM_13), ESP_EXT1_WAKEUP_ANY_HIGH));
  256. esp_deep_sleep_start();
  257. }
  258. TEST_CASE("wake up using ext1 when RTC_PERIPH is off (13 low)", "[deepsleep][ignore]")
  259. {
  260. // This test needs external pullup
  261. ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
  262. ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(BIT(GPIO_NUM_13), ESP_EXT1_WAKEUP_ALL_LOW));
  263. esp_deep_sleep_start();
  264. }
  265. TEST_CASE("wake up using ext1 when RTC_PERIPH is on (13 high)", "[deepsleep][ignore]")
  266. {
  267. ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
  268. ESP_ERROR_CHECK(gpio_pullup_dis(GPIO_NUM_13));
  269. ESP_ERROR_CHECK(gpio_pulldown_en(GPIO_NUM_13));
  270. ESP_ERROR_CHECK(esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON));
  271. ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(BIT(GPIO_NUM_13), ESP_EXT1_WAKEUP_ANY_HIGH));
  272. esp_deep_sleep_start();
  273. }
  274. TEST_CASE("wake up using ext1 when RTC_PERIPH is on (13 low)", "[deepsleep][ignore]")
  275. {
  276. ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
  277. ESP_ERROR_CHECK(gpio_pullup_en(GPIO_NUM_13));
  278. ESP_ERROR_CHECK(gpio_pulldown_dis(GPIO_NUM_13));
  279. ESP_ERROR_CHECK(esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON));
  280. ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(BIT(GPIO_NUM_13), ESP_EXT1_WAKEUP_ALL_LOW));
  281. esp_deep_sleep_start();
  282. }
  283. static float get_time_ms(void)
  284. {
  285. gettimeofday(&tv_stop, NULL);
  286. float dt = (tv_stop.tv_sec - tv_start.tv_sec) * 1e3f +
  287. (tv_stop.tv_usec - tv_start.tv_usec) * 1e-3f;
  288. return fabs(dt);
  289. }
  290. static uint32_t get_cause()
  291. {
  292. uint32_t wakeup_cause = REG_GET_FIELD(RTC_CNTL_WAKEUP_STATE_REG, \
  293. RTC_CNTL_WAKEUP_CAUSE);
  294. return wakeup_cause;
  295. }
  296. // This test case verifies deactivation of trigger for wake up sources
  297. TEST_CASE("disable source trigger behavior", "[deepsleep]")
  298. {
  299. float dt = 0;
  300. printf("Setup timer and ext0 to wake up immediately from GPIO_13 \n");
  301. // Setup ext0 configuration to wake up almost immediately
  302. // The wakeup time is proportional to input capacitance * pullup resistance
  303. ESP_ERROR_CHECK(rtc_gpio_init(GPIO_NUM_13));
  304. ESP_ERROR_CHECK(gpio_pullup_en(GPIO_NUM_13));
  305. ESP_ERROR_CHECK(gpio_pulldown_dis(GPIO_NUM_13));
  306. ESP_ERROR_CHECK(esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, ESP_EXT0_WAKEUP_LEVEL_HIGH));
  307. // Setup timer to wakeup with timeout
  308. esp_sleep_enable_timer_wakeup(2000000);
  309. // Save start time
  310. gettimeofday(&tv_start, NULL);
  311. esp_light_sleep_start();
  312. dt = get_time_ms();
  313. printf("Ext0 sleep time = %d \n", (int) dt);
  314. // Check wakeup from Ext0 using time measurement because wakeup cause is
  315. // not available in light sleep mode
  316. TEST_ASSERT_INT32_WITHIN(100, 100, (int) dt);
  317. TEST_ASSERT((get_cause() & RTC_EXT0_TRIG_EN) != 0);
  318. // Disable Ext0 source. Timer source should be triggered
  319. ESP_ERROR_CHECK(esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_EXT0));
  320. printf("Disable ext0 trigger and leave timer active.\n");
  321. gettimeofday(&tv_start, NULL);
  322. esp_light_sleep_start();
  323. dt = get_time_ms();
  324. printf("Timer sleep time = %d \n", (int) dt);
  325. TEST_ASSERT_INT32_WITHIN(500, 2000, (int) dt);
  326. // Additionally check wakeup cause
  327. TEST_ASSERT((get_cause() & RTC_TIMER_TRIG_EN) != 0);
  328. // Disable timer source.
  329. ESP_ERROR_CHECK(esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER));
  330. // Setup ext0 configuration to wake up immediately
  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_HIGH));
  335. printf("Disable timer trigger to wake up from ext0 source.\n");
  336. gettimeofday(&tv_start, NULL);
  337. esp_light_sleep_start();
  338. dt = get_time_ms();
  339. printf("Ext0 sleep time = %d \n", (int) dt);
  340. TEST_ASSERT_INT32_WITHIN(100, 100, (int) dt);
  341. TEST_ASSERT((get_cause() & RTC_EXT0_TRIG_EN) != 0);
  342. // Check error message when source is already disabled
  343. esp_err_t err_code = esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TIMER);
  344. TEST_ASSERT(err_code == ESP_ERR_INVALID_STATE);
  345. }
  346. static RTC_DATA_ATTR struct timeval start;
  347. static void trigger_deepsleep(void)
  348. {
  349. printf("Trigger deep sleep. Waiting for 10 sec ...\n");
  350. // Simulate the dispersion of the calibration coefficients at start-up.
  351. // Corrupt the calibration factor.
  352. esp_clk_slowclk_cal_set(esp_clk_slowclk_cal_get() / 2);
  353. esp_set_time_from_rtc();
  354. // Delay for time error accumulation.
  355. vTaskDelay(10000/portTICK_RATE_MS);
  356. // Save start time. Deep sleep.
  357. gettimeofday(&start, NULL);
  358. esp_sleep_enable_timer_wakeup(1000);
  359. // In function esp_deep_sleep_start() uses function esp_sync_counters_rtc_and_frc()
  360. // to prevent a negative time after wake up.
  361. esp_deep_sleep_start();
  362. }
  363. static void check_time_deepsleep(void)
  364. {
  365. struct timeval stop;
  366. RESET_REASON reason = rtc_get_reset_reason(0);
  367. TEST_ASSERT(reason == DEEPSLEEP_RESET);
  368. gettimeofday(&stop, NULL);
  369. // Time dt_ms must in any case be positive.
  370. int dt_ms = (stop.tv_sec - start.tv_sec) * 1000 + (stop.tv_usec - start.tv_usec) / 1000;
  371. printf("delta time = %d \n", dt_ms);
  372. TEST_ASSERT_MESSAGE(dt_ms > 0, "Time in deep sleep is negative");
  373. }
  374. TEST_CASE_MULTIPLE_STAGES("check a time after wakeup from deep sleep", "[deepsleep][reset=DEEPSLEEP_RESET]", trigger_deepsleep, check_time_deepsleep);