1 /*
2  * Copyright (c) 2021 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <zephyr/busy_sim.h>
9 
ZTEST(busy_sim,test_busy_sim)10 ZTEST(busy_sim, test_busy_sim)
11 {
12 	uint32_t ms = 1000;
13 	uint32_t delta = 80;
14 	uint32_t busy_ms;
15 	uint32_t t = k_uptime_get_32();
16 
17 	k_busy_wait(1000 * ms);
18 	t = k_uptime_get_32() - t;
19 
20 	zassert_true((t > (ms - delta)) && (t < (ms + delta)));
21 
22 	/* Start busy simulator and check that k_busy_wait last longer */
23 	t = k_uptime_get_32();
24 	busy_sim_start(500, 200, 1000, 400, NULL);
25 	k_busy_wait(1000 * ms);
26 	t = k_uptime_get_32() - t;
27 	busy_ms = (3 * ms) / 2;
28 
29 	busy_sim_stop();
30 	/* due to clock imprecision, randomness and additional cpu load overhead
31 	 * expected time range is increased.
32 	 */
33 	zassert_true((t > (busy_ms - 2 * delta)) && (t < (busy_ms + 4 * delta)),
34 			"expected in range: %d-%d, k_busy_wait lasted %d",
35 			busy_ms - 2 * delta, busy_ms + 4 * delta,  t);
36 
37 	/* Check that k_busy_wait is not interrupted after busy_sim_stop. */
38 	t = k_uptime_get_32();
39 	k_busy_wait(1000 * ms);
40 	t = k_uptime_get_32() - t;
41 	zassert_true((t > (ms - delta)) && (t < (ms + delta)));
42 }
43 
44 ZTEST_SUITE(busy_sim, NULL, NULL, NULL, NULL, NULL);
45