test_pm.c 9.9 KB

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