test_pm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "freertos/semphr.h"
  12. #include "esp_log.h"
  13. #include "driver/timer.h"
  14. #include "driver/rtc_io.h"
  15. #include "soc/rtc_periph.h"
  16. #include "esp_rom_sys.h"
  17. #include "sdkconfig.h"
  18. #if CONFIG_IDF_TARGET_ESP32
  19. #include "esp32/clk.h"
  20. #include "esp32/ulp.h"
  21. #elif CONFIG_IDF_TARGET_ESP32S2
  22. #include "esp32s2/clk.h"
  23. #include "esp32s2/ulp.h"
  24. #elif CONFIG_IDF_TARGET_ESP32S3
  25. #include "esp32s3/clk.h"
  26. #include "esp32s3/ulp.h"
  27. #elif CONFIG_IDF_TARGET_ESP32C3
  28. #include "esp32c3/clk.h"
  29. #endif
  30. TEST_CASE("Can dump power management lock stats", "[pm]")
  31. {
  32. esp_pm_dump_locks(stdout);
  33. }
  34. #ifdef CONFIG_PM_ENABLE
  35. static void switch_freq(int mhz)
  36. {
  37. int xtal_freq = rtc_clk_xtal_freq_get();
  38. #if CONFIG_IDF_TARGET_ESP32
  39. esp_pm_config_esp32_t pm_config = {
  40. #elif CONFIG_IDF_TARGET_ESP32S2
  41. esp_pm_config_esp32s2_t pm_config = {
  42. #elif CONFIG_IDF_TARGET_ESP32S3
  43. esp_pm_config_esp32s3_t pm_config = {
  44. #elif CONFIG_IDF_TARGET_ESP32C3
  45. esp_pm_config_esp32c3_t pm_config = {
  46. #endif
  47. .max_freq_mhz = mhz,
  48. .min_freq_mhz = MIN(mhz, xtal_freq),
  49. };
  50. ESP_ERROR_CHECK( esp_pm_configure(&pm_config) );
  51. printf("Waiting for frequency to be set to %d MHz...\n", mhz);
  52. while (esp_clk_cpu_freq() / MHZ != mhz) {
  53. vTaskDelay(pdMS_TO_TICKS(200));
  54. printf("Frequency is %d MHz\n", esp_clk_cpu_freq() / MHZ);
  55. }
  56. }
  57. #if CONFIG_IDF_TARGET_ESP32C3
  58. static const int test_freqs[] = {40, 160, 80, 40, 80, 10, 80, 20, 40};
  59. #else
  60. static const int test_freqs[] = {240, 40, 160, 240, 80, 40, 240, 40, 80, 10, 80, 20, 40};
  61. #endif
  62. TEST_CASE("Can switch frequency using esp_pm_configure", "[pm]")
  63. {
  64. int orig_freq_mhz = esp_clk_cpu_freq() / MHZ;
  65. for (int i = 0; i < sizeof(test_freqs)/sizeof(int); i++) {
  66. switch_freq(test_freqs[i]);
  67. }
  68. switch_freq(orig_freq_mhz);
  69. }
  70. #if CONFIG_FREERTOS_USE_TICKLESS_IDLE
  71. static void light_sleep_enable(void)
  72. {
  73. int cur_freq_mhz = esp_clk_cpu_freq() / MHZ;
  74. int xtal_freq = (int) rtc_clk_xtal_freq_get();
  75. #if CONFIG_IDF_TARGET_ESP32
  76. esp_pm_config_esp32_t pm_config = {
  77. #elif CONFIG_IDF_TARGET_ESP32S2
  78. esp_pm_config_esp32s2_t pm_config = {
  79. #elif CONFIG_IDF_TARGET_ESP32S3
  80. esp_pm_config_esp32s3_t pm_config = {
  81. #elif CONFIG_IDF_TARGET_ESP32C3
  82. esp_pm_config_esp32c3_t pm_config = {
  83. #endif
  84. .max_freq_mhz = cur_freq_mhz,
  85. .min_freq_mhz = xtal_freq,
  86. .light_sleep_enable = true
  87. };
  88. ESP_ERROR_CHECK( esp_pm_configure(&pm_config) );
  89. }
  90. static void light_sleep_disable(void)
  91. {
  92. int cur_freq_mhz = esp_clk_cpu_freq() / MHZ;
  93. #if CONFIG_IDF_TARGET_ESP32
  94. esp_pm_config_esp32_t pm_config = {
  95. #elif CONFIG_IDF_TARGET_ESP32S2
  96. esp_pm_config_esp32s2_t pm_config = {
  97. #elif CONFIG_IDF_TARGET_ESP32S3
  98. esp_pm_config_esp32s3_t pm_config = {
  99. #elif CONFIG_IDF_TARGET_ESP32C3
  100. esp_pm_config_esp32c3_t pm_config = {
  101. #endif
  102. .max_freq_mhz = cur_freq_mhz,
  103. .min_freq_mhz = cur_freq_mhz,
  104. };
  105. ESP_ERROR_CHECK( esp_pm_configure(&pm_config) );
  106. }
  107. TEST_CASE("Automatic light occurs when tasks are suspended", "[pm]")
  108. {
  109. /* To figure out if light sleep takes place, use Timer Group timer.
  110. * It will stop working while in light sleep.
  111. */
  112. timer_config_t config = {
  113. .counter_dir = TIMER_COUNT_UP,
  114. .divider = 80 /* 1 us per tick */
  115. };
  116. timer_init(TIMER_GROUP_0, TIMER_0, &config);
  117. timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0);
  118. timer_start(TIMER_GROUP_0, TIMER_0);
  119. light_sleep_enable();
  120. for (int ticks_to_delay = CONFIG_FREERTOS_IDLE_TIME_BEFORE_SLEEP;
  121. ticks_to_delay < CONFIG_FREERTOS_IDLE_TIME_BEFORE_SLEEP * 10;
  122. ++ticks_to_delay) {
  123. /* Wait until next tick */
  124. vTaskDelay(1);
  125. /* The following delay should cause light sleep to start */
  126. uint64_t count_start;
  127. timer_get_counter_value(TIMER_GROUP_0, TIMER_0, &count_start);
  128. vTaskDelay(ticks_to_delay);
  129. uint64_t count_end;
  130. timer_get_counter_value(TIMER_GROUP_0, TIMER_0, &count_end);
  131. int timer_diff_us = (int) (count_end - count_start);
  132. const int us_per_tick = 1 * portTICK_PERIOD_MS * 1000;
  133. printf("%d %d\n", ticks_to_delay * us_per_tick, timer_diff_us);
  134. TEST_ASSERT(timer_diff_us < ticks_to_delay * us_per_tick);
  135. }
  136. light_sleep_disable();
  137. }
  138. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2, ESP32S3)
  139. #if !DISABLED_FOR_TARGETS(ESP32C3)
  140. // No ULP on C3
  141. // Fix failure on ESP32 when running alone; passes when the previous test is run before this one
  142. TEST_CASE("Can wake up from automatic light sleep by GPIO", "[pm][ignore]")
  143. {
  144. #if CONFIG_IDF_TARGET_ESP32
  145. assert(CONFIG_ESP32_ULP_COPROC_RESERVE_MEM >= 16 && "this test needs ESP32_ULP_COPROC_RESERVE_MEM option set in menuconfig");
  146. #elif CONFIG_IDF_TARGET_ESP32S2
  147. assert(CONFIG_ESP32S2_ULP_COPROC_RESERVE_MEM >= 16 && "this test needs ESP32_ULP_COPROC_RESERVE_MEM option set in menuconfig");
  148. #elif CONFIG_IDF_TARGET_ESP32S3
  149. assert(CONFIG_ESP32S3_ULP_COPROC_RESERVE_MEM >= 16 && "this test needs ESP32_ULP_COPROC_RESERVE_MEM option set in menuconfig");
  150. #endif
  151. /* Set up GPIO used to wake up RTC */
  152. const int ext1_wakeup_gpio = 25;
  153. const int ext_rtc_io = RTCIO_GPIO25_CHANNEL;
  154. TEST_ESP_OK(rtc_gpio_init(ext1_wakeup_gpio));
  155. rtc_gpio_set_direction(ext1_wakeup_gpio, RTC_GPIO_MODE_INPUT_OUTPUT);
  156. rtc_gpio_set_level(ext1_wakeup_gpio, 0);
  157. /* Enable wakeup */
  158. TEST_ESP_OK(esp_sleep_enable_ext1_wakeup(1ULL << ext1_wakeup_gpio, ESP_EXT1_WAKEUP_ANY_HIGH));
  159. /* To simplify test environment, we'll use a ULP program to set GPIO high */
  160. ulp_insn_t ulp_code[] = {
  161. I_DELAY(65535), /* about 8ms, given 8MHz ULP clock */
  162. I_WR_REG_BIT(RTC_CNTL_HOLD_FORCE_REG, RTC_CNTL_PDAC1_HOLD_FORCE_S, 0),
  163. I_WR_REG_BIT(RTC_GPIO_OUT_REG, ext_rtc_io + RTC_GPIO_OUT_DATA_S, 1),
  164. I_DELAY(1000),
  165. I_WR_REG_BIT(RTC_GPIO_OUT_REG, ext_rtc_io + RTC_GPIO_OUT_DATA_S, 0),
  166. I_WR_REG_BIT(RTC_CNTL_HOLD_FORCE_REG, RTC_CNTL_PDAC1_HOLD_FORCE_S, 1),
  167. I_END(),
  168. I_HALT()
  169. };
  170. TEST_ESP_OK(ulp_set_wakeup_period(0, 1000 /* us */));
  171. size_t size = sizeof(ulp_code)/sizeof(ulp_insn_t);
  172. TEST_ESP_OK(ulp_process_macros_and_load(0, ulp_code, &size));
  173. light_sleep_enable();
  174. int rtcio_num = rtc_io_number_get(ext1_wakeup_gpio);
  175. for (int i = 0; i < 10; ++i) {
  176. /* Set GPIO low */
  177. REG_CLR_BIT(rtc_io_desc[rtcio_num].reg, rtc_io_desc[rtcio_num].hold_force);
  178. rtc_gpio_set_level(ext1_wakeup_gpio, 0);
  179. REG_SET_BIT(rtc_io_desc[rtcio_num].reg, rtc_io_desc[rtcio_num].hold_force);
  180. /* Wait for the next tick */
  181. vTaskDelay(1);
  182. /* Start ULP program */
  183. ulp_run(0);
  184. const int delay_ms = 200;
  185. const int delay_ticks = delay_ms / portTICK_PERIOD_MS;
  186. int64_t start_rtc = esp_clk_rtc_time();
  187. int64_t start_hs = esp_timer_get_time();
  188. uint32_t start_tick = xTaskGetTickCount();
  189. /* Will enter sleep here */
  190. vTaskDelay(delay_ticks);
  191. int64_t end_rtc = esp_clk_rtc_time();
  192. int64_t end_hs = esp_timer_get_time();
  193. uint32_t end_tick = xTaskGetTickCount();
  194. printf("%lld %lld %u\n", end_rtc - start_rtc, end_hs - start_hs, end_tick - start_tick);
  195. TEST_ASSERT_INT32_WITHIN(3, delay_ticks, end_tick - start_tick);
  196. TEST_ASSERT_INT32_WITHIN(2 * portTICK_PERIOD_MS * 1000, delay_ms * 1000, end_hs - start_hs);
  197. TEST_ASSERT_INT32_WITHIN(2 * portTICK_PERIOD_MS * 1000, delay_ms * 1000, end_rtc - start_rtc);
  198. }
  199. REG_CLR_BIT(rtc_io_desc[rtcio_num].reg, rtc_io_desc[rtcio_num].hold_force);
  200. rtc_gpio_deinit(ext1_wakeup_gpio);
  201. light_sleep_disable();
  202. }
  203. #endif //!DISABLED_FOR_TARGETS(ESP32C3)
  204. #endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2, ESP32S3)
  205. typedef struct {
  206. int delay_us;
  207. int result;
  208. SemaphoreHandle_t done;
  209. } delay_test_arg_t;
  210. static void test_delay_task(void* p)
  211. {
  212. delay_test_arg_t* arg = (delay_test_arg_t*) p;
  213. vTaskDelay(1);
  214. uint64_t start = esp_clk_rtc_time();
  215. vTaskDelay(arg->delay_us / portTICK_PERIOD_MS / 1000);
  216. uint64_t stop = esp_clk_rtc_time();
  217. arg->result = (int) (stop - start);
  218. xSemaphoreGive(arg->done);
  219. vTaskDelete(NULL);
  220. }
  221. TEST_CASE("vTaskDelay duration is correct with light sleep enabled", "[pm]")
  222. {
  223. light_sleep_enable();
  224. delay_test_arg_t args = {
  225. .done = xSemaphoreCreateBinary()
  226. };
  227. const int delays[] = { 10, 20, 50, 100, 150, 200, 250 };
  228. const int delays_count = sizeof(delays) / sizeof(delays[0]);
  229. for (int i = 0; i < delays_count; ++i) {
  230. int delay_ms = delays[i];
  231. args.delay_us = delay_ms * 1000;
  232. xTaskCreatePinnedToCore(test_delay_task, "", 2048, (void*) &args, 3, NULL, 0);
  233. TEST_ASSERT( xSemaphoreTake(args.done, delay_ms * 10 / portTICK_PERIOD_MS) );
  234. printf("CPU0: %d %d\n", args.delay_us, args.result);
  235. TEST_ASSERT_INT32_WITHIN(1000 * portTICK_PERIOD_MS * 2, args.delay_us, args.result);
  236. #if portNUM_PROCESSORS == 2
  237. xTaskCreatePinnedToCore(test_delay_task, "", 2048, (void*) &args, 3, NULL, 1);
  238. TEST_ASSERT( xSemaphoreTake(args.done, delay_ms * 10 / portTICK_PERIOD_MS) );
  239. printf("CPU1: %d %d\n", args.delay_us, args.result);
  240. TEST_ASSERT_INT32_WITHIN(1000 * portTICK_PERIOD_MS * 2, args.delay_us, args.result);
  241. #endif
  242. }
  243. vSemaphoreDelete(args.done);
  244. light_sleep_disable();
  245. }
  246. /* This test is similar to the one in test_esp_timer.c, but since we can't use
  247. * ref_clock, this test uses RTC clock for timing. Also enables automatic
  248. * light sleep.
  249. */
  250. TEST_CASE("esp_timer produces correct delays with light sleep", "[pm]")
  251. {
  252. // no, we can't make this a const size_t (§6.7.5.2)
  253. #define NUM_INTERVALS 16
  254. typedef struct {
  255. esp_timer_handle_t timer;
  256. size_t cur_interval;
  257. int intervals[NUM_INTERVALS];
  258. int64_t t_start;
  259. SemaphoreHandle_t done;
  260. } test_args_t;
  261. void timer_func(void* arg)
  262. {
  263. test_args_t* p_args = (test_args_t*) arg;
  264. int64_t t_end = esp_clk_rtc_time();
  265. int32_t ms_diff = (t_end - p_args->t_start) / 1000;
  266. printf("timer #%d %dms\n", p_args->cur_interval, ms_diff);
  267. p_args->intervals[p_args->cur_interval++] = ms_diff;
  268. // Deliberately make timer handler run longer.
  269. // We check that this doesn't affect the result.
  270. esp_rom_delay_us(10*1000);
  271. if (p_args->cur_interval == NUM_INTERVALS) {
  272. printf("done\n");
  273. TEST_ESP_OK(esp_timer_stop(p_args->timer));
  274. xSemaphoreGive(p_args->done);
  275. }
  276. }
  277. light_sleep_enable();
  278. const int delay_ms = 100;
  279. test_args_t args = {0};
  280. esp_timer_handle_t timer1;
  281. esp_timer_create_args_t create_args = {
  282. .callback = &timer_func,
  283. .arg = &args,
  284. .name = "timer1",
  285. };
  286. TEST_ESP_OK(esp_timer_create(&create_args, &timer1));
  287. args.timer = timer1;
  288. args.t_start = esp_clk_rtc_time();
  289. args.done = xSemaphoreCreateBinary();
  290. TEST_ESP_OK(esp_timer_start_periodic(timer1, delay_ms * 1000));
  291. TEST_ASSERT(xSemaphoreTake(args.done, delay_ms * NUM_INTERVALS * 2));
  292. TEST_ASSERT_EQUAL_UINT32(NUM_INTERVALS, args.cur_interval);
  293. for (size_t i = 0; i < NUM_INTERVALS; ++i) {
  294. TEST_ASSERT_INT32_WITHIN(portTICK_PERIOD_MS, (i + 1) * delay_ms, args.intervals[i]);
  295. }
  296. TEST_ESP_OK( esp_timer_dump(stdout) );
  297. TEST_ESP_OK( esp_timer_delete(timer1) );
  298. vSemaphoreDelete(args.done);
  299. light_sleep_disable();
  300. #undef NUM_INTERVALS
  301. }
  302. #endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE
  303. #endif // CONFIG_PM_ENABLE