1 /*
2 * Copyright (c) 2024 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "iut.h"
8 #include "sedi_driver_hpet.h"
9 #include "sedi_driver_rtc.h"
10 #include <zephyr/sys/time_units.h>
11 #include <zephyr/kernel.h>
12
test_sys_clock_freq(int argc,char ** argv)13 static int test_sys_clock_freq(int argc, char **argv)
14 {
15 uint32_t sec_run = 60;
16
17 uint64_t us_rtc_start, cyc_rtc_start;
18 uint64_t us_hpet_start, cyc_hpet_start;
19 uint64_t cyc_sys_start;
20
21 uint64_t us_rtc_end, cyc_rtc_end;
22 uint64_t us_hpet_end, cyc_hpet_end;
23 uint64_t cyc_sys_end;
24
25 uint64_t us_rtc, cyc_rtc;
26 uint64_t us_hpet, cyc_hpet;
27 uint64_t us_sys, cyc_sys;
28 uint32_t hz_rtc, hz_hpet, hz_sys;
29 uint32_t psec_hpet;
30
31 if (argc) {
32 sec_run = (uint32_t)strtoul(argv[0], NULL, 0);
33 }
34
35 iut_case_print("to run %u seconds\n", sec_run);
36
37 us_rtc_start = sedi_rtc_get_us();
38 cyc_rtc_start = sedi_rtc_get();
39
40 us_hpet_start = sedi_hpet_get_us();
41 cyc_hpet_start = sedi_hpet_get_main_counter();
42
43 cyc_sys_start = k_cycle_get_64();
44
45 k_msleep(sec_run * 1000);
46
47 us_rtc_end = sedi_rtc_get_us();
48 cyc_rtc_end = sedi_rtc_get();
49
50 us_hpet_end = sedi_hpet_get_us();
51 cyc_hpet_end = sedi_hpet_get_main_counter();
52
53 cyc_sys_end = k_cycle_get_64();
54
55 us_rtc = (us_rtc_end - us_rtc_start);
56 cyc_rtc = (cyc_rtc_end - cyc_rtc_start);
57
58 us_hpet = (us_hpet_end - us_hpet_start);
59 cyc_hpet = (cyc_hpet_end - cyc_hpet_start);
60
61 cyc_sys = (cyc_sys_end - cyc_sys_start);
62 us_sys = cyc_sys * Z_HZ_us / Z_HZ_cyc;
63
64 hz_rtc = (uint32_t)((cyc_rtc * USEC_PER_SEC) / us_rtc);
65 hz_hpet = (uint32_t)((cyc_hpet * USEC_PER_SEC) / us_hpet);
66 hz_sys = (uint32_t)((cyc_sys * USEC_PER_SEC) / us_sys);
67 #define PSEC_PER_USEC (1000000)
68 psec_hpet = (uint32_t)((us_hpet * PSEC_PER_USEC) / cyc_hpet);
69
70 iut_case_print("RTC : %llu cycles, %llu us, %u HZ\n", cyc_rtc, us_rtc, hz_rtc);
71 iut_case_print("HPET: %llu cycles, %llu us, %u HZ\n", cyc_hpet, us_hpet, hz_hpet);
72 iut_case_print("SYS CLOCK: %llu cycles, %llu us, %u HZ\n", cyc_sys, us_sys, hz_sys);
73
74 /* checking diff < 1/100 */
75 TEST_ASSERT_TRUE(abs((int)(hz_rtc - SEDI_RTC_TICKS_PER_SECOND)) * 100
76 < SEDI_RTC_TICKS_PER_SECOND);
77 TEST_ASSERT_TRUE(abs((int)(hz_sys - CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC)) * 100
78 < CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC);
79 TEST_ASSERT_TRUE(abs((int)(psec_hpet - sedi_hpet_get_period())) * 100
80 < sedi_hpet_get_period());
81 TEST_ASSERT_TRUE(abs((int)(us_rtc - sec_run * USEC_PER_SEC - us_rtc)) * 100
82 < (sec_run * USEC_PER_SEC - us_rtc));
83
84 return IUT_ERR_OK;
85 }
86
87 DEFINE_IUT_CASE(sys_clock_freq, timer, IUT_ATTRI_NONE);
88