1 /*
2 * Copyright (c) 2019 Intel Corp.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6 #include <zephyr/drivers/counter.h>
7 #include <zephyr/ztest.h>
8 #include <zephyr/kernel.h>
9
10 /*
11 * Basic test to ensure that the clock is ticking at roughly 1Hz.
12 */
13
14 #ifdef CONFIG_COUNTER_CMOS
15 #define CTR_DEV DT_COMPAT_GET_ANY_STATUS_OKAY(motorola_mc146818)
16 #else
17 #define CTR_DEV DT_ALIAS(rtc_0)
18 #endif
19
20 #define DELAY_MS 1200 /* pause 1.2 seconds should always pass */
21 #define MIN_BOUND 1 /* counter must report at least MIN_BOUND .. */
22 #define MAX_BOUND 2 /* .. but at most MAX_BOUND seconds elapsed */
23
ZTEST(seconds_counter,test_seconds_rate)24 ZTEST(seconds_counter, test_seconds_rate)
25 {
26 const struct device *const dev = DEVICE_DT_GET(CTR_DEV);
27 uint32_t start, elapsed;
28 int err;
29
30 zassert_true(device_is_ready(dev), "counter device is not ready");
31
32 err = counter_get_value(dev, &start);
33 zassert_true(err == 0, "failed to read counter device");
34
35 k_msleep(DELAY_MS);
36
37 err = counter_get_value(dev, &elapsed);
38 zassert_true(err == 0, "failed to read counter device");
39 elapsed -= start;
40
41 zassert_true(elapsed >= MIN_BOUND, "busted minimum bound");
42 zassert_true(elapsed <= MAX_BOUND, "busted maximum bound");
43 }
44
45 ZTEST_SUITE(seconds_counter, NULL, NULL, NULL, NULL, NULL);
46