test_pm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <sys/time.h>
  5. #include <sys/param.h>
  6. #include "unity.h"
  7. #include "esp_pm.h"
  8. #include "esp_sleep.h"
  9. #include "esp_timer.h"
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "freertos/semphr.h"
  13. #include "esp_log.h"
  14. #include "driver/gptimer.h"
  15. #include "driver/rtc_io.h"
  16. #include "soc/rtc.h"
  17. #include "esp_private/gptimer.h"
  18. #include "soc/rtc_periph.h"
  19. #include "esp_rom_sys.h"
  20. #include "esp_private/esp_clk.h"
  21. #include "test_utils.h"
  22. #include "sdkconfig.h"
  23. #if CONFIG_ULP_COPROC_TYPE_FSM
  24. #if CONFIG_IDF_TARGET_ESP32
  25. #include "esp32/ulp.h"
  26. #elif CONFIG_IDF_TARGET_ESP32S2
  27. #include "esp32s2/ulp.h"
  28. #elif CONFIG_IDF_TARGET_ESP32S3
  29. #include "esp32s3/ulp.h"
  30. #endif
  31. #endif //CONFIG_ULP_COPROC_TYPE_FSM
  32. TEST_CASE("Can dump power management lock stats", "[pm]")
  33. {
  34. esp_pm_dump_locks(stdout);
  35. }
  36. #ifdef CONFIG_PM_ENABLE
  37. static void switch_freq(int mhz)
  38. {
  39. int xtal_freq_mhz = esp_clk_xtal_freq() / MHZ;
  40. #if CONFIG_IDF_TARGET_ESP32
  41. esp_pm_config_esp32_t pm_config = {
  42. #elif CONFIG_IDF_TARGET_ESP32S2
  43. esp_pm_config_esp32s2_t pm_config = {
  44. #elif CONFIG_IDF_TARGET_ESP32S3
  45. esp_pm_config_esp32s3_t pm_config = {
  46. #elif CONFIG_IDF_TARGET_ESP32C2
  47. esp_pm_config_esp32c2_t pm_config = {
  48. #elif CONFIG_IDF_TARGET_ESP32C3
  49. esp_pm_config_esp32c3_t pm_config = {
  50. #elif CONFIG_IDF_TARGET_ESP32H4
  51. esp_pm_config_esp32h4_t pm_config = {
  52. #elif CONFIG_IDF_TARGET_ESP32C6
  53. esp_pm_config_esp32c6_t pm_config = {
  54. #endif
  55. .max_freq_mhz = mhz,
  56. .min_freq_mhz = MIN(mhz, xtal_freq_mhz),
  57. };
  58. ESP_ERROR_CHECK( esp_pm_configure(&pm_config) );
  59. printf("Waiting for frequency to be set to %d MHz...\n", mhz);
  60. while (esp_clk_cpu_freq() / MHZ != mhz)
  61. {
  62. vTaskDelay(pdMS_TO_TICKS(200));
  63. printf("Frequency is %d MHz\n", esp_clk_cpu_freq() / MHZ);
  64. }
  65. }
  66. #if CONFIG_IDF_TARGET_ESP32C3
  67. static const int test_freqs[] = {40, CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ, 80, 40, 80, 10, 80, 20, 40};
  68. #elif CONFIG_IDF_TARGET_ESP32C2
  69. static const int test_freqs[] = {CONFIG_XTAL_FREQ, CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ, 80, CONFIG_XTAL_FREQ, 80,
  70. CONFIG_XTAL_FREQ / 2, CONFIG_XTAL_FREQ}; // C2 xtal has 40/26MHz option
  71. #elif CONFIG_IDF_TARGET_ESP32H4
  72. static const int test_freqs[] = {32, CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ, 32} // TODO: IDF-3786
  73. #else
  74. static const int test_freqs[] = {240, 40, 160, 240, 80, 40, 240, 40, 80, 10, 80, 20, 40};
  75. #endif
  76. TEST_CASE("Can switch frequency using esp_pm_configure", "[pm]")
  77. {
  78. int orig_freq_mhz = esp_clk_cpu_freq() / MHZ;
  79. for (int i = 0; i < sizeof(test_freqs) / sizeof(int); i++) {
  80. switch_freq(test_freqs[i]);
  81. }
  82. switch_freq(orig_freq_mhz);
  83. }
  84. #if CONFIG_FREERTOS_USE_TICKLESS_IDLE
  85. static void light_sleep_enable(void)
  86. {
  87. int cur_freq_mhz = esp_clk_cpu_freq() / MHZ;
  88. int xtal_freq = esp_clk_xtal_freq() / MHZ;
  89. #if CONFIG_IDF_TARGET_ESP32
  90. esp_pm_config_esp32_t pm_config = {
  91. #elif CONFIG_IDF_TARGET_ESP32S2
  92. esp_pm_config_esp32s2_t pm_config = {
  93. #elif CONFIG_IDF_TARGET_ESP32S3
  94. esp_pm_config_esp32s3_t pm_config = {
  95. #elif CONFIG_IDF_TARGET_ESP32C2
  96. esp_pm_config_esp32c2_t pm_config = {
  97. #elif CONFIG_IDF_TARGET_ESP32C3
  98. esp_pm_config_esp32c3_t pm_config = {
  99. #elif CONFIG_IDF_TARGET_ESP32H4
  100. esp_pm_config_esp32h4_t pm_config = {
  101. #elif CONFIG_IDF_TARGET_ESP32C6
  102. esp_pm_config_esp32c6_t pm_config = {
  103. #endif
  104. .max_freq_mhz = cur_freq_mhz,
  105. .min_freq_mhz = xtal_freq,
  106. .light_sleep_enable = true
  107. };
  108. ESP_ERROR_CHECK( esp_pm_configure(&pm_config) );
  109. }
  110. static void light_sleep_disable(void)
  111. {
  112. int cur_freq_mhz = esp_clk_cpu_freq() / MHZ;
  113. #if CONFIG_IDF_TARGET_ESP32
  114. esp_pm_config_esp32_t pm_config = {
  115. #elif CONFIG_IDF_TARGET_ESP32S2
  116. esp_pm_config_esp32s2_t pm_config = {
  117. #elif CONFIG_IDF_TARGET_ESP32S3
  118. esp_pm_config_esp32s3_t pm_config = {
  119. #elif CONFIG_IDF_TARGET_ESP32C2
  120. esp_pm_config_esp32c2_t pm_config = {
  121. #elif CONFIG_IDF_TARGET_ESP32C3
  122. esp_pm_config_esp32c3_t pm_config = {
  123. #elif CONFIG_IDF_TARGET_ESP32H4
  124. esp_pm_config_esp32h4_t pm_config = {
  125. #elif CONFIG_IDF_TARGET_ESP32C6
  126. esp_pm_config_esp32c6_t pm_config = {
  127. #endif
  128. .max_freq_mhz = cur_freq_mhz,
  129. .min_freq_mhz = cur_freq_mhz,
  130. };
  131. ESP_ERROR_CHECK( esp_pm_configure(&pm_config) );
  132. }
  133. TEST_CASE("Automatic light occurs when tasks are suspended", "[pm]")
  134. {
  135. gptimer_handle_t gptimer = NULL;
  136. /* To figure out if light sleep takes place, use GPTimer
  137. * It will stop working while in light sleep.
  138. */
  139. gptimer_config_t config = {
  140. .clk_src = GPTIMER_CLK_SRC_DEFAULT,
  141. .direction = GPTIMER_COUNT_UP,
  142. .resolution_hz = 1000000, /* 1 us per tick */
  143. };
  144. TEST_ESP_OK(gptimer_new_timer(&config, &gptimer));
  145. TEST_ESP_OK(gptimer_enable(gptimer));
  146. TEST_ESP_OK(gptimer_start(gptimer));
  147. // if GPTimer is clocked from APB, when PM is enabled, the driver will acquire the PM lock
  148. // causing the auto light sleep doesn't take effect
  149. // so we manually release the lock here
  150. esp_pm_lock_handle_t gptimer_pm_lock;
  151. TEST_ESP_OK(gptimer_get_pm_lock(gptimer, &gptimer_pm_lock));
  152. if (gptimer_pm_lock) {
  153. TEST_ESP_OK(esp_pm_lock_release(gptimer_pm_lock));
  154. }
  155. light_sleep_enable();
  156. for (int ticks_to_delay = CONFIG_FREERTOS_IDLE_TIME_BEFORE_SLEEP;
  157. ticks_to_delay < CONFIG_FREERTOS_IDLE_TIME_BEFORE_SLEEP * 10;
  158. ++ticks_to_delay) {
  159. /* Wait until next tick */
  160. vTaskDelay(1);
  161. /* The following delay should cause light sleep to start */
  162. uint64_t count_start;
  163. uint64_t count_end;
  164. TEST_ESP_OK(gptimer_get_raw_count(gptimer, &count_start));
  165. vTaskDelay(ticks_to_delay);
  166. TEST_ESP_OK(gptimer_get_raw_count(gptimer, &count_end));
  167. int timer_diff_us = (int) (count_end - count_start);
  168. const int us_per_tick = 1 * portTICK_PERIOD_MS * 1000;
  169. printf("%d %d\n", ticks_to_delay * us_per_tick, timer_diff_us);
  170. TEST_ASSERT(timer_diff_us < ticks_to_delay * us_per_tick);
  171. }
  172. light_sleep_disable();
  173. if (gptimer_pm_lock) {
  174. TEST_ESP_OK(esp_pm_lock_acquire(gptimer_pm_lock));
  175. }
  176. TEST_ESP_OK(gptimer_stop(gptimer));
  177. TEST_ESP_OK(gptimer_disable(gptimer));
  178. TEST_ESP_OK(gptimer_del_timer(gptimer));
  179. }
  180. #if CONFIG_ULP_COPROC_TYPE_FSM
  181. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2, ESP32S3)
  182. // Fix failure on ESP32 when running alone; passes when the previous test is run before this one
  183. TEST_CASE("Can wake up from automatic light sleep by GPIO", "[pm][ignore]")
  184. {
  185. assert(CONFIG_ULP_COPROC_RESERVE_MEM >= 16 && "this test needs ULP_COPROC_RESERVE_MEM option set in menuconfig");
  186. /* Set up GPIO used to wake up RTC */
  187. const int ext1_wakeup_gpio = 25;
  188. const int ext_rtc_io = RTCIO_GPIO25_CHANNEL;
  189. TEST_ESP_OK(rtc_gpio_init(ext1_wakeup_gpio));
  190. rtc_gpio_set_direction(ext1_wakeup_gpio, RTC_GPIO_MODE_INPUT_OUTPUT);
  191. rtc_gpio_set_level(ext1_wakeup_gpio, 0);
  192. /* Enable wakeup */
  193. TEST_ESP_OK(esp_sleep_enable_ext1_wakeup(1ULL << ext1_wakeup_gpio, ESP_EXT1_WAKEUP_ANY_HIGH));
  194. /* To simplify test environment, we'll use a ULP program to set GPIO high */
  195. ulp_insn_t ulp_code[] = {
  196. I_DELAY(65535), /* about 8ms, given 8MHz ULP clock */
  197. I_WR_REG_BIT(RTC_CNTL_HOLD_FORCE_REG, RTC_CNTL_PDAC1_HOLD_FORCE_S, 0),
  198. I_WR_REG_BIT(RTC_GPIO_OUT_REG, ext_rtc_io + RTC_GPIO_OUT_DATA_S, 1),
  199. I_DELAY(1000),
  200. I_WR_REG_BIT(RTC_GPIO_OUT_REG, ext_rtc_io + RTC_GPIO_OUT_DATA_S, 0),
  201. I_WR_REG_BIT(RTC_CNTL_HOLD_FORCE_REG, RTC_CNTL_PDAC1_HOLD_FORCE_S, 1),
  202. I_END(),
  203. I_HALT()
  204. };
  205. TEST_ESP_OK(ulp_set_wakeup_period(0, 1000 /* us */));
  206. size_t size = sizeof(ulp_code) / sizeof(ulp_insn_t);
  207. TEST_ESP_OK(ulp_process_macros_and_load(0, ulp_code, &size));
  208. light_sleep_enable();
  209. int rtcio_num = rtc_io_number_get(ext1_wakeup_gpio);
  210. for (int i = 0; i < 10; ++i) {
  211. /* Set GPIO low */
  212. REG_CLR_BIT(rtc_io_desc[rtcio_num].reg, rtc_io_desc[rtcio_num].hold_force);
  213. rtc_gpio_set_level(ext1_wakeup_gpio, 0);
  214. REG_SET_BIT(rtc_io_desc[rtcio_num].reg, rtc_io_desc[rtcio_num].hold_force);
  215. /* Wait for the next tick */
  216. vTaskDelay(1);
  217. /* Start ULP program */
  218. ulp_run(0);
  219. const int delay_ms = 200;
  220. const int delay_ticks = delay_ms / portTICK_PERIOD_MS;
  221. int64_t start_rtc = esp_clk_rtc_time();
  222. int64_t start_hs = esp_timer_get_time();
  223. uint32_t start_tick = xTaskGetTickCount();
  224. /* Will enter sleep here */
  225. vTaskDelay(delay_ticks);
  226. int64_t end_rtc = esp_clk_rtc_time();
  227. int64_t end_hs = esp_timer_get_time();
  228. uint32_t end_tick = xTaskGetTickCount();
  229. printf("%lld %lld %u\n", end_rtc - start_rtc, end_hs - start_hs, end_tick - start_tick);
  230. TEST_ASSERT_INT32_WITHIN(3, delay_ticks, end_tick - start_tick);
  231. TEST_ASSERT_INT32_WITHIN(2 * portTICK_PERIOD_MS * 1000, delay_ms * 1000, end_hs - start_hs);
  232. TEST_ASSERT_INT32_WITHIN(2 * portTICK_PERIOD_MS * 1000, delay_ms * 1000, end_rtc - start_rtc);
  233. }
  234. REG_CLR_BIT(rtc_io_desc[rtcio_num].reg, rtc_io_desc[rtcio_num].hold_force);
  235. rtc_gpio_deinit(ext1_wakeup_gpio);
  236. light_sleep_disable();
  237. }
  238. #endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2, ESP32S3)
  239. #endif //CONFIG_ULP_COPROC_TYPE_FSM
  240. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
  241. //IDF-5053
  242. typedef struct {
  243. int delay_us;
  244. int result;
  245. SemaphoreHandle_t done;
  246. } delay_test_arg_t;
  247. static void test_delay_task(void *p)
  248. {
  249. delay_test_arg_t *arg = (delay_test_arg_t *) p;
  250. vTaskDelay(1);
  251. uint64_t start = esp_clk_rtc_time();
  252. vTaskDelay(arg->delay_us / portTICK_PERIOD_MS / 1000);
  253. uint64_t stop = esp_clk_rtc_time();
  254. arg->result = (int) (stop - start);
  255. xSemaphoreGive(arg->done);
  256. vTaskDelete(NULL);
  257. }
  258. TEST_CASE("vTaskDelay duration is correct with light sleep enabled", "[pm]")
  259. {
  260. light_sleep_enable();
  261. SemaphoreHandle_t done_sem = xSemaphoreCreateBinary();
  262. TEST_ASSERT_NOT_NULL(done_sem);
  263. delay_test_arg_t args = {
  264. .done = done_sem,
  265. };
  266. const int delays[] = { 10, 20, 50, 100, 150, 200, 250 };
  267. const int delays_count = sizeof(delays) / sizeof(delays[0]);
  268. for (int i = 0; i < delays_count; ++i) {
  269. int delay_ms = delays[i];
  270. args.delay_us = delay_ms * 1000;
  271. xTaskCreatePinnedToCore(test_delay_task, "", 2048, (void *) &args, 3, NULL, 0);
  272. TEST_ASSERT( xSemaphoreTake(done_sem, delay_ms * 10 / portTICK_PERIOD_MS) );
  273. printf("CPU0: %d %d\n", args.delay_us, args.result);
  274. TEST_ASSERT_INT32_WITHIN(1000 * portTICK_PERIOD_MS * 2, args.delay_us, args.result);
  275. #if portNUM_PROCESSORS == 2
  276. xTaskCreatePinnedToCore(test_delay_task, "", 2048, (void *) &args, 3, NULL, 1);
  277. TEST_ASSERT( xSemaphoreTake(done_sem, delay_ms * 10 / portTICK_PERIOD_MS) );
  278. printf("CPU1: %d %d\n", args.delay_us, args.result);
  279. TEST_ASSERT_INT32_WITHIN(1000 * portTICK_PERIOD_MS * 2, args.delay_us, args.result);
  280. #endif
  281. }
  282. vSemaphoreDelete(done_sem);
  283. light_sleep_disable();
  284. }
  285. /* This test is similar to the one in test_esp_timer.c, but since we can't use
  286. * ref_clock, this test uses RTC clock for timing. Also enables automatic
  287. * light sleep.
  288. */
  289. TEST_CASE("esp_timer produces correct delays with light sleep", "[pm]")
  290. {
  291. // no, we can't make this a const size_t (§6.7.5.2)
  292. #define NUM_INTERVALS 16
  293. typedef struct {
  294. esp_timer_handle_t timer;
  295. size_t cur_interval;
  296. int intervals[NUM_INTERVALS];
  297. int64_t t_start;
  298. SemaphoreHandle_t done;
  299. } test_args_t;
  300. void timer_func(void *arg) {
  301. test_args_t *p_args = (test_args_t *) arg;
  302. int64_t t_end = esp_clk_rtc_time();
  303. int32_t ms_diff = (t_end - p_args->t_start) / 1000;
  304. printf("timer #%d %dms\n", p_args->cur_interval, ms_diff);
  305. p_args->intervals[p_args->cur_interval++] = ms_diff;
  306. // Deliberately make timer handler run longer.
  307. // We check that this doesn't affect the result.
  308. esp_rom_delay_us(10 * 1000);
  309. if (p_args->cur_interval == NUM_INTERVALS) {
  310. printf("done\n");
  311. TEST_ESP_OK(esp_timer_stop(p_args->timer));
  312. xSemaphoreGive(p_args->done);
  313. }
  314. }
  315. light_sleep_enable();
  316. const int delay_ms = 100;
  317. test_args_t args = {0};
  318. esp_timer_handle_t timer1;
  319. esp_timer_create_args_t create_args = {
  320. .callback = timer_func,
  321. .arg = &args,
  322. .name = "timer1",
  323. };
  324. TEST_ESP_OK(esp_timer_create(&create_args, &timer1));
  325. args.timer = timer1;
  326. args.t_start = esp_clk_rtc_time();
  327. args.done = xSemaphoreCreateBinary();
  328. TEST_ESP_OK(esp_timer_start_periodic(timer1, delay_ms * 1000));
  329. TEST_ASSERT(xSemaphoreTake(args.done, delay_ms * NUM_INTERVALS * 2));
  330. TEST_ASSERT_EQUAL_UINT32(NUM_INTERVALS, args.cur_interval);
  331. for (size_t i = 0; i < NUM_INTERVALS; ++i) {
  332. TEST_ASSERT_INT32_WITHIN(portTICK_PERIOD_MS, (i + 1) * delay_ms, args.intervals[i]);
  333. }
  334. TEST_ESP_OK( esp_timer_dump(stdout) );
  335. TEST_ESP_OK( esp_timer_delete(timer1) );
  336. vSemaphoreDelete(args.done);
  337. light_sleep_disable();
  338. #undef NUM_INTERVALS
  339. }
  340. #endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32C2)
  341. static void timer_cb1(void *arg)
  342. {
  343. ++*((int *) arg);
  344. }
  345. TEST_CASE("esp_timer with SKIP_UNHANDLED_EVENTS does not wake up CPU from sleep", "[pm]")
  346. {
  347. int count_calls = 0;
  348. int timer_interval_ms = 50;
  349. const esp_timer_create_args_t timer_args = {
  350. .name = "timer_cb1",
  351. .arg = &count_calls,
  352. .callback = &timer_cb1,
  353. .skip_unhandled_events = true,
  354. };
  355. esp_timer_handle_t periodic_timer;
  356. esp_timer_create(&timer_args, &periodic_timer);
  357. TEST_ESP_OK(esp_timer_start_periodic(periodic_timer, timer_interval_ms * 1000));
  358. light_sleep_enable();
  359. const unsigned count_delays = 5;
  360. unsigned i = count_delays;
  361. while (i-- > 0) {
  362. vTaskDelay(pdMS_TO_TICKS(500));
  363. }
  364. TEST_ASSERT_INT_WITHIN(1, count_delays, count_calls);
  365. light_sleep_disable();
  366. TEST_ESP_OK(esp_timer_stop(periodic_timer));
  367. TEST_ESP_OK(esp_timer_dump(stdout));
  368. TEST_ESP_OK(esp_timer_delete(periodic_timer));
  369. }
  370. #endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE
  371. #endif // CONFIG_PM_ENABLE