1 /*
2  * Copyright (c) 2019 Intel Corp.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <zephyr/kernel.h>
9 
10 /*
11  * precision timing tests in an emulation environment are not reliable.
12  * if the test passes at least once, we know it works properly, so we
13  * attempt to repeat the test RETRIES times before reporting failure.
14  */
15 
16 #define RETRIES		10
17 
18 /*
19  * We need to know how many ticks will elapse when we ask for the
20  * shortest possible tick timeout.  That's generally 1, but in some
21  * cases it may be more.  On Nordic paths that take 5 or 6 ticks may
22  * be observed depending on clock stability and alignment. The base
23  * rate assumes 3 ticks for non-timeout effects so increase the
24  * maximum effect of timeout to 3 ticks on this platform.
25  */
26 
27 #if defined(CONFIG_NRF_RTC_TIMER) && (CONFIG_SYS_CLOCK_TICKS_PER_SEC > 16384)
28 /* The overhead of k_usleep() adds three ticks per loop iteration on
29  * nRF51, which has a slow CPU clock.
30  */
31 #define MAXIMUM_SHORTEST_TICKS (IS_ENABLED(CONFIG_SOC_SERIES_NRF51X) ? 6 : 3)
32 /* Similar situation for TI CC13XX/CC26XX RTC kernel timer due to the
33  * limitation that a value too close to the current time cannot be
34  * loaded to its comparator.
35  */
36 #elif defined(CONFIG_CC13XX_CC26XX_RTC_TIMER) && \
37 	(CONFIG_SYS_CLOCK_TICKS_PER_SEC > 16384)
38 #define MAXIMUM_SHORTEST_TICKS 3
39 #else
40 #define MAXIMUM_SHORTEST_TICKS 1
41 #endif
42 
43 /*
44  * Theory of operation: we can't use absolute units (e.g., "sleep for
45  * 10us") in testing k_usleep() because the granularity of sleeps is
46  * highly dependent on the hardware's capabilities and kernel
47  * configuration. Instead, we test that k_usleep() actually sleeps for
48  * the minimum possible duration, which is nominally two ticks.  So,
49  * we loop k_usleep()ing for as many iterations as should comprise a
50  * second, and check to see that a total of one second has elapsed.
51  */
52 
53 #define LOOPS (CONFIG_SYS_CLOCK_TICKS_PER_SEC / 2)
54 
55 /* It should never iterate faster than the tick rate.  However the
56  * app, sleep, and timeout layers may each add a tick alignment with
57  * fast tick rates, and cycle layer may inject another to guarantee
58  * the timeout deadline is met.
59  */
60 #define LOWER_BOUND_MS	((1000 * LOOPS) / CONFIG_SYS_CLOCK_TICKS_PER_SEC)
61 #define UPPER_BOUND_MS	(((3 + MAXIMUM_SHORTEST_TICKS) * 1000 * LOOPS)	\
62 			 / CONFIG_SYS_CLOCK_TICKS_PER_SEC)
63 
ZTEST_USER(sleep,test_usleep)64 ZTEST_USER(sleep, test_usleep)
65 {
66 	int retries = 0;
67 	int64_t elapsed_ms = 0;
68 
69 	while (retries < RETRIES) {
70 		int64_t start_ms;
71 		int64_t end_ms;
72 		int i;
73 
74 		++retries;
75 		start_ms = k_uptime_get();
76 
77 		for (i = 0; i < LOOPS; ++i) {
78 			k_usleep(1);
79 		}
80 
81 		end_ms = k_uptime_get();
82 		elapsed_ms = end_ms - start_ms;
83 
84 		/* if at first you don't succeed, keep sucking. */
85 
86 		if ((elapsed_ms >= LOWER_BOUND_MS) &&
87 		    (elapsed_ms <= UPPER_BOUND_MS)) {
88 			break;
89 		}
90 	}
91 
92 	zassert_true(elapsed_ms >= LOWER_BOUND_MS, "short sleep");
93 	zassert_true(elapsed_ms <= UPPER_BOUND_MS, "overslept");
94 }
95