test_rtc_clk.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include "unity.h"
  8. #include "esp_attr.h"
  9. #include "soc/soc_caps.h"
  10. #include "soc/rtc.h"
  11. #include "soc/rtc_periph.h"
  12. #if SOC_ADC_RTC_CTRL_SUPPORTED
  13. #include "soc/sens_periph.h"
  14. #endif
  15. #include "soc/gpio_periph.h"
  16. #include "hal/gpio_ll.h"
  17. #include "driver/rtc_io.h"
  18. #include "test_utils.h"
  19. #include "freertos/FreeRTOS.h"
  20. #include "freertos/task.h"
  21. #include "freertos/semphr.h"
  22. #include "esp_rom_gpio.h"
  23. #include "esp_rom_sys.h"
  24. #include "esp_rom_uart.h"
  25. #include "esp_sleep.h"
  26. #include "esp_system.h"
  27. #if CONFIG_IDF_TARGET_ESP32
  28. #include "esp32/rtc.h"
  29. #include "esp32/clk.h"
  30. #include "esp32/rom/rtc.h"
  31. #elif CONFIG_IDF_TARGET_ESP32S2
  32. #include "esp32s2/rtc.h"
  33. #include "esp32s2/clk.h"
  34. #include "esp32s2/rom/rtc.h"
  35. #elif CONFIG_IDF_TARGET_ESP32S3
  36. #include "esp32s3/rtc.h"
  37. #include "esp32s3/clk.h"
  38. #include "esp32s3/rom/rtc.h"
  39. #elif CONFIG_IDF_TARGET_ESP32C3
  40. #include "esp32c3/rtc.h"
  41. #include "esp32c3/clk.h"
  42. #include "esp32c3/rom/rtc.h"
  43. #elif CONFIG_IDF_TARGET_ESP32H2
  44. #include "esp32h2/rtc.h"
  45. #include "esp32h2/clk.h"
  46. #include "esp32h2/rom/rtc.h"
  47. #endif
  48. extern void rtc_clk_select_rtc_slow_clk(void);
  49. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2, ESP32S3, ESP32C3)
  50. #define CALIBRATE_ONE(cali_clk) calibrate_one(cali_clk, #cali_clk)
  51. static uint32_t calibrate_one(rtc_cal_sel_t cal_clk, const char* name)
  52. {
  53. const uint32_t cal_count = 1000;
  54. const float factor = (1 << 19) * 1000.0f;
  55. uint32_t cali_val;
  56. printf("%s:\n", name);
  57. for (int i = 0; i < 5; ++i) {
  58. printf("calibrate (%d): ", i);
  59. cali_val = rtc_clk_cal(cal_clk, cal_count);
  60. printf("%.3f kHz\n", factor / (float) cali_val);
  61. }
  62. return cali_val;
  63. }
  64. TEST_CASE("RTC_SLOW_CLK sources calibration", "[rtc_clk]")
  65. {
  66. rtc_clk_32k_enable(true);
  67. rtc_clk_8m_enable(true, true);
  68. CALIBRATE_ONE(RTC_CAL_RTC_MUX);
  69. CALIBRATE_ONE(RTC_CAL_8MD256);
  70. uint32_t cal_32k = CALIBRATE_ONE(RTC_CAL_32K_XTAL);
  71. if (cal_32k == 0) {
  72. printf("32K XTAL OSC has not started up");
  73. } else {
  74. printf("switching to RTC_SLOW_FREQ_32K_XTAL: ");
  75. rtc_clk_slow_freq_set(RTC_SLOW_FREQ_32K_XTAL);
  76. printf("done\n");
  77. CALIBRATE_ONE(RTC_CAL_RTC_MUX);
  78. CALIBRATE_ONE(RTC_CAL_8MD256);
  79. CALIBRATE_ONE(RTC_CAL_32K_XTAL);
  80. }
  81. printf("switching to RTC_SLOW_FREQ_8MD256: ");
  82. rtc_clk_slow_freq_set(RTC_SLOW_FREQ_8MD256);
  83. printf("done\n");
  84. CALIBRATE_ONE(RTC_CAL_RTC_MUX);
  85. CALIBRATE_ONE(RTC_CAL_8MD256);
  86. CALIBRATE_ONE(RTC_CAL_32K_XTAL);
  87. }
  88. /* The following two are not unit tests, but are added here to make it easy to
  89. * check the frequency of 150k/32k oscillators. The following two "tests" will
  90. * output either 32k or 150k clock to GPIO25.
  91. */
  92. static void pull_out_clk(int sel)
  93. {
  94. REG_SET_BIT(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_MUX_SEL_M);
  95. REG_CLR_BIT(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_RDE_M | RTC_IO_PDAC1_RUE_M);
  96. REG_SET_FIELD(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_FUN_SEL, 1);
  97. REG_SET_FIELD(SENS_SAR_DAC_CTRL1_REG, SENS_DEBUG_BIT_SEL, 0);
  98. REG_SET_FIELD(RTC_IO_RTC_DEBUG_SEL_REG, RTC_IO_DEBUG_SEL0, sel);
  99. }
  100. TEST_CASE("Output 150k clock to GPIO25", "[rtc_clk][ignore]")
  101. {
  102. pull_out_clk(RTC_IO_DEBUG_SEL0_150K_OSC);
  103. }
  104. TEST_CASE("Output 32k XTAL clock to GPIO25", "[rtc_clk][ignore]")
  105. {
  106. rtc_clk_32k_enable(true);
  107. pull_out_clk(RTC_IO_DEBUG_SEL0_32K_XTAL);
  108. }
  109. TEST_CASE("Output 8M XTAL clock to GPIO25", "[rtc_clk][ignore]")
  110. {
  111. rtc_clk_8m_enable(true, true);
  112. SET_PERI_REG_MASK(RTC_IO_RTC_DEBUG_SEL_REG, RTC_IO_DEBUG_12M_NO_GATING);
  113. pull_out_clk(RTC_IO_DEBUG_SEL0_8M);
  114. }
  115. static void test_clock_switching(void (*switch_func)(const rtc_cpu_freq_config_t* config))
  116. {
  117. esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
  118. const int test_duration_sec = 10;
  119. ref_clock_init();
  120. uint64_t t_start = ref_clock_get();
  121. rtc_cpu_freq_config_t cur_config;
  122. rtc_clk_cpu_freq_get_config(&cur_config);
  123. rtc_cpu_freq_config_t xtal_config;
  124. rtc_clk_cpu_freq_mhz_to_config((uint32_t) rtc_clk_xtal_freq_get(), &xtal_config);
  125. int count = 0;
  126. while (ref_clock_get() - t_start < test_duration_sec * 1000000) {
  127. switch_func(&xtal_config);
  128. switch_func(&cur_config);
  129. ++count;
  130. }
  131. uint64_t t_end = ref_clock_get();
  132. printf("Switch count: %d. Average time to switch PLL -> XTAL -> PLL: %d us\n", count, (int) ((t_end - t_start) / count));
  133. ref_clock_deinit();
  134. }
  135. TEST_CASE("Calculate 8M clock frequency", "[rtc_clk]")
  136. {
  137. // calibrate 8M/256 clock against XTAL, get 8M/256 clock period
  138. uint32_t rtc_8md256_period = rtc_clk_cal(RTC_CAL_8MD256, 100);
  139. uint32_t rtc_fast_freq_hz = 1000000ULL * (1 << RTC_CLK_CAL_FRACT) * 256 / rtc_8md256_period;
  140. printf("RTC_FAST_CLK=%d Hz\n", rtc_fast_freq_hz);
  141. TEST_ASSERT_INT32_WITHIN(650000, RTC_FAST_CLK_FREQ_APPROX, rtc_fast_freq_hz);
  142. }
  143. TEST_CASE("Test switching between PLL and XTAL", "[rtc_clk]")
  144. {
  145. test_clock_switching(rtc_clk_cpu_freq_set_config);
  146. }
  147. TEST_CASE("Test fast switching between PLL and XTAL", "[rtc_clk]")
  148. {
  149. test_clock_switching(rtc_clk_cpu_freq_set_config_fast);
  150. }
  151. /* In CI environments, the 32kXTAL runners don't have 8MB psram for bank switching.
  152. So can only test one config or the other. */
  153. #if !IDF_CI_BUILD || !CONFIG_SPIRAM_BANKSWITCH_ENABLE
  154. #define COUNT_TEST 3
  155. #define TIMEOUT_TEST_MS (5 + CONFIG_ESP32_RTC_CLK_CAL_CYCLES / 16)
  156. void stop_rtc_external_quartz(void){
  157. const uint8_t pin_32 = 32;
  158. const uint8_t pin_33 = 33;
  159. rtc_clk_32k_enable(false);
  160. esp_rom_gpio_pad_select_gpio(pin_32);
  161. esp_rom_gpio_pad_select_gpio(pin_33);
  162. gpio_ll_output_enable(&GPIO, pin_32);
  163. gpio_ll_output_enable(&GPIO, pin_33);
  164. gpio_ll_set_level(&GPIO, pin_32, 0);
  165. gpio_ll_set_level(&GPIO, pin_33, 0);
  166. esp_rom_delay_us(500000);
  167. gpio_ll_output_disable(&GPIO, pin_32);
  168. gpio_ll_output_disable(&GPIO, pin_33);
  169. }
  170. static void start_freq(rtc_slow_freq_t required_src_freq, uint32_t start_delay_ms)
  171. {
  172. int i = 0, fail = 0;
  173. uint32_t start_time;
  174. uint32_t end_time;
  175. rtc_slow_freq_t selected_src_freq;
  176. stop_rtc_external_quartz();
  177. #ifdef CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS
  178. uint32_t bootstrap_cycles = CONFIG_ESP_SYSTEM_RTC_EXT_XTAL_BOOTSTRAP_CYCLES;
  179. printf("Test is started. Kconfig settings:\n External 32K crystal is selected,\n Oscillation cycles = %d,\n Calibration cycles = %d.\n",
  180. bootstrap_cycles,
  181. CONFIG_ESP32_RTC_CLK_CAL_CYCLES);
  182. #else
  183. uint32_t bootstrap_cycles = 5;
  184. printf("Test is started. Kconfig settings:\n Internal RC is selected,\n Oscillation cycles = %d,\n Calibration cycles = %d.\n",
  185. bootstrap_cycles,
  186. CONFIG_ESP32_RTC_CLK_CAL_CYCLES);
  187. #endif // CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS
  188. if (start_delay_ms == 0 && CONFIG_ESP32_RTC_CLK_CAL_CYCLES < 1500){
  189. start_delay_ms = 50;
  190. printf("Recommended increase Number of cycles for RTC_SLOW_CLK calibration to 3000!\n");
  191. }
  192. while(i < COUNT_TEST){
  193. start_time = xTaskGetTickCount() * (1000 / configTICK_RATE_HZ);
  194. i++;
  195. printf("attempt #%d/%d...", i, COUNT_TEST);
  196. rtc_clk_32k_bootstrap(bootstrap_cycles);
  197. esp_rom_delay_us(start_delay_ms * 1000);
  198. rtc_clk_select_rtc_slow_clk();
  199. selected_src_freq = rtc_clk_slow_freq_get();
  200. end_time = xTaskGetTickCount() * (1000 / configTICK_RATE_HZ);
  201. printf(" [time=%d] ", (end_time - start_time) - start_delay_ms);
  202. if(selected_src_freq != required_src_freq){
  203. printf("FAIL. Time measurement...");
  204. fail = 1;
  205. } else {
  206. printf("PASS. Time measurement...");
  207. }
  208. uint64_t clk_rtc_time;
  209. uint32_t fail_measure = 0;
  210. for (int j = 0; j < 3; ++j) {
  211. clk_rtc_time = esp_clk_rtc_time();
  212. esp_rom_delay_us(1000000);
  213. uint64_t delta = esp_clk_rtc_time() - clk_rtc_time;
  214. if (delta < 900000LL || delta > 1100000){
  215. printf("FAIL");
  216. fail = 1;
  217. fail_measure = 1;
  218. break;
  219. }
  220. }
  221. if(fail_measure == 0) {
  222. printf("PASS");
  223. }
  224. printf(" [calibration val = %d] \n", esp_clk_slowclk_cal_get());
  225. stop_rtc_external_quartz();
  226. esp_rom_delay_us(500000);
  227. }
  228. TEST_ASSERT_MESSAGE(fail == 0, "Test failed");
  229. printf("Test passed successfully\n");
  230. }
  231. TEST_CASE("Test starting external RTC quartz", "[rtc_clk][test_env=UT_T1_32kXTAL]")
  232. {
  233. int i = 0, fail = 0;
  234. uint32_t start_time;
  235. uint32_t end_time;
  236. stop_rtc_external_quartz();
  237. #ifdef CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS
  238. uint32_t bootstrap_cycles = CONFIG_ESP_SYSTEM_RTC_EXT_XTAL_BOOTSTRAP_CYCLES;
  239. printf("Test is started. Kconfig settings:\n External 32K crystal is selected,\n Oscillation cycles = %d,\n Calibration cycles = %d.\n",
  240. bootstrap_cycles,
  241. CONFIG_ESP32_RTC_CLK_CAL_CYCLES);
  242. #else
  243. uint32_t bootstrap_cycles = 5;
  244. printf("Test is started. Kconfig settings:\n Internal RC is selected,\n Oscillation cycles = %d,\n Calibration cycles = %d.\n",
  245. bootstrap_cycles,
  246. CONFIG_ESP32_RTC_CLK_CAL_CYCLES);
  247. #endif // CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS
  248. if (CONFIG_ESP32_RTC_CLK_CAL_CYCLES < 1500){
  249. printf("Recommended increase Number of cycles for RTC_SLOW_CLK calibration to 3000!\n");
  250. }
  251. while(i < COUNT_TEST){
  252. start_time = xTaskGetTickCount() * (1000 / configTICK_RATE_HZ);
  253. i++;
  254. printf("attempt #%d/%d...", i, COUNT_TEST);
  255. rtc_clk_32k_bootstrap(bootstrap_cycles);
  256. rtc_clk_select_rtc_slow_clk();
  257. end_time = xTaskGetTickCount() * (1000 / configTICK_RATE_HZ);
  258. printf(" [time=%d] ", end_time - start_time);
  259. if((end_time - start_time) > TIMEOUT_TEST_MS){
  260. printf("FAIL\n");
  261. fail = 1;
  262. } else {
  263. printf("PASS\n");
  264. }
  265. stop_rtc_external_quartz();
  266. esp_rom_delay_us(100000);
  267. }
  268. TEST_ASSERT_MESSAGE(fail == 0, "Test failed");
  269. printf("Test passed successfully\n");
  270. }
  271. TEST_CASE("Test starting 'External 32kHz XTAL' on the board with it.", "[rtc_clk][test_env=UT_T1_32kXTAL]")
  272. {
  273. start_freq(RTC_SLOW_FREQ_32K_XTAL, 200);
  274. start_freq(RTC_SLOW_FREQ_32K_XTAL, 0);
  275. }
  276. TEST_CASE("Test starting 'External 32kHz XTAL' on the board without it.", "[rtc_clk][test_env=UT_T1_no32kXTAL]")
  277. {
  278. printf("Tries to start the 'External 32kHz XTAL' on the board without it. "
  279. "Clock switching to 'Internal 150 kHz RC oscillator'.\n");
  280. printf("This test will be successful for boards without an external crystal or non-working crystal. "
  281. "First, there will be an attempt to start from the external crystal after a failure "
  282. "will switch to the internal RC circuit. If the switch to the internal RC circuit "
  283. "was successful then the test succeeded.\n");
  284. start_freq(RTC_SLOW_FREQ_RTC, 200);
  285. start_freq(RTC_SLOW_FREQ_RTC, 0);
  286. }
  287. #endif // !IDF_CI_BUILD || !CONFIG_SPIRAM_BANKSWITCH_ENABLE
  288. #endif // !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2, ESP32S3, ESP32C3)
  289. TEST_CASE("Test rtc clk calibration compensation", "[rtc_clk]")
  290. {
  291. int64_t t1 = esp_rtc_get_time_us();
  292. // Modify calibration value
  293. esp_clk_slowclk_cal_set(esp_clk_slowclk_cal_get() / 2);
  294. // Delay for error accumulation.
  295. vTaskDelay(pdMS_TO_TICKS(1000));
  296. // Internally, the origin point of rtc clk has been adjusted
  297. // so that t2 > t1 remains true
  298. int64_t t2 = esp_rtc_get_time_us();
  299. TEST_ASSERT_GREATER_THAN(t1, t2);
  300. // Restore calibration value
  301. esp_clk_slowclk_cal_set(esp_clk_slowclk_cal_get() * 2);
  302. // Delay for error accumulation.
  303. vTaskDelay(pdMS_TO_TICKS(1000));
  304. t2 = esp_rtc_get_time_us();
  305. TEST_ASSERT_GREATER_THAN(t1, t2);
  306. }
  307. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3)
  308. /* Disabled until deep sleep is brought up TODO ESP32-S3 IDF-2691 */
  309. static RTC_NOINIT_ATTR int64_t start = 0;
  310. static void trigger_deepsleep(void)
  311. {
  312. printf("Trigger deep sleep. Waiting for 10 sec ...\n");
  313. // Simulate the dispersion of the calibration coefficients at start-up.
  314. // Corrupt the calibration factor.
  315. esp_clk_slowclk_cal_set(esp_clk_slowclk_cal_get() / 2);
  316. // Delay for error accumulation.
  317. vTaskDelay(pdMS_TO_TICKS(1000));
  318. // Save start time. Deep sleep.
  319. start = esp_rtc_get_time_us();
  320. esp_sleep_enable_timer_wakeup(1000);
  321. // In function esp_deep_sleep_start() uses function esp_sync_counters_rtc_and_frc()
  322. // to prevent a negative time after wake up.
  323. esp_deep_sleep_start();
  324. }
  325. static void check_time_deepsleep_1(void)
  326. {
  327. soc_reset_reason_t reason = esp_rom_get_reset_reason(0);
  328. TEST_ASSERT(reason == RESET_REASON_CORE_DEEP_SLEEP);
  329. int64_t end = esp_rtc_get_time_us();
  330. TEST_ASSERT_GREATER_THAN(start, end);
  331. esp_clk_slowclk_cal_set(esp_clk_slowclk_cal_get() * 2);
  332. // Delay for error accumulation.
  333. vTaskDelay(pdMS_TO_TICKS(1000));
  334. start = esp_rtc_get_time_us();
  335. esp_sleep_enable_timer_wakeup(1000);
  336. // In function esp_deep_sleep_start() uses function esp_sync_counters_rtc_and_frc()
  337. // to prevent a negative time after wake up.
  338. esp_deep_sleep_start();
  339. }
  340. static void check_time_deepsleep_2(void)
  341. {
  342. soc_reset_reason_t reason = esp_rom_get_reset_reason(0);
  343. TEST_ASSERT(reason == RESET_REASON_CORE_DEEP_SLEEP);
  344. int64_t end = esp_rtc_get_time_us();
  345. TEST_ASSERT_GREATER_THAN(start, end);
  346. }
  347. TEST_CASE_MULTIPLE_STAGES("Test rtc clk calibration compensation across deep sleep", "[rtc_clk][reset=DEEPSLEEP_RESET, DEEPSLEEP_RESET]", trigger_deepsleep, check_time_deepsleep_1, check_time_deepsleep_2);
  348. #endif // !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3)