test_rtc_clk.c 14 KB

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