test_pm.c 9.8 KB

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