zephyr/tests/kernel/timer/timer_monotonic/src/main.c
Gerard Marull-Paretas 79e6b0e0f6 includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.

The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.

NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-09-05 16:31:47 +02:00

86 lines
1.9 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2017 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/tc_util.h>
#include <zephyr/ztest.h>
int test_frequency(void)
{
volatile uint32_t start, end;
uint32_t delta, pct;
TC_PRINT("Testing system tick frequency\n");
start = k_cycle_get_32();
k_sleep(K_MSEC(1000));
end = k_cycle_get_32();
delta = end - start;
pct = (uint64_t)delta * 100U / sys_clock_hw_cycles_per_sec();
printk("delta: %u expected: %u %u%%\n", delta,
sys_clock_hw_cycles_per_sec(), pct);
/* Heuristic: if we're more than 10% off, throw an error */
if (pct < 90 || pct > 110) {
TC_PRINT("Clock calibration is way off!\n");
return -1;
}
return 0;
}
/**
* @brief Test monotonic timer
*
* Validates monotonic timer's clock calibration.
*
* It reads the System clocks h/w timer frequency value continuously
* using k_cycle_get_32() to verify its working and correctness.
* It also checks system tick frequency by checking the delta error
* between generated and system clock provided HW cycles per sec values.
*
* @ingroup kernel_timer_tests
*
* @see k_cycle_get_32(), sys_clock_hw_cycles_per_sec()
*/
ZTEST(timer_fn, test_timer)
{
volatile uint32_t t_last, t_now;
uint32_t i, errors;
int32_t diff;
errors = 0U;
TC_PRINT("k_ticks_to_cyc_floor32(1) = %d\n",
k_ticks_to_cyc_floor32(1));
TC_PRINT("sys_clock_hw_cycles_per_sec() = %d\n",
sys_clock_hw_cycles_per_sec());
TC_START("test monotonic timer");
t_last = k_cycle_get_32();
for (i = 0U; i < 1000000; i++) {
t_now = k_cycle_get_32();
if (t_now < t_last) {
diff = t_now - t_last;
TC_PRINT("diff = %d (t_last = %u : t_now = %u);"
"i = %u\n", diff, t_last, t_now, i);
errors++;
}
t_last = t_now;
}
zassert_false(errors, "errors = %d\n", errors);
zassert_false(test_frequency(), "test frequency failed");
}
ZTEST_SUITE(timer_fn, NULL, NULL, NULL, NULL, NULL);