1 /*
2 * Copyright (c) 2018 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8 #include <zephyr/kernel.h>
9 #include <cmsis_os.h>
10
11 #define WAIT_TIME_US 1000000
12
13 /* Specify accepted tolerance. On some Zephyr platforms (e.g. nRF5x) the busy
14 * wait loop and the system timer are based on different mechanisms and may not
15 * align perfectly. 1 percent base intolerance is to cover CPU processing in the
16 * test.
17 */
18 #if CONFIG_NRF_RTC_TIMER
19 /* High frequency clock used for k_busy_wait may have up to 8% tolerance.
20 * Additionally, if RC is used for low frequency clock then it has 5% tolerance.
21 */
22 #define TOLERANCE_PPC \
23 (1 + 8 + (IS_ENABLED(CONFIG_CLOCK_CONTROL_NRF_K32SRC_RC) ? 5 : 0))
24 #else
25 #define TOLERANCE_PPC 1
26 #endif
27
28 /**
29 * @brief Test kernel start
30 *
31 * @see osKernelInitialize(), osKernelStart(),
32 * osKernelRunning()
33 */
ZTEST(kernel_apis,test_kernel_start)34 ZTEST(kernel_apis, test_kernel_start)
35 {
36 if (osFeature_MainThread) {
37 /* When osFeature_MainThread is 1 the kernel offers to start
38 * with 'main'. The kernel is in this case already started.
39 */
40 zassert_true(!osKernelInitialize() && !osKernelStart()
41 && osKernelRunning(), NULL);
42 } else {
43 /* When osFeature_MainThread is 0 the kernel requires
44 * explicit start with osKernelStart.
45 */
46 zassert_false(osKernelRunning());
47 }
48 }
49
50 /**
51 * @brief Test kernel system timer
52 *
53 * @see osKernelSysTick()
54 */
ZTEST(kernel_apis,test_kernel_systick)55 ZTEST(kernel_apis, test_kernel_systick)
56 {
57 uint32_t start_time, stop_time, diff, max, min;
58
59 start_time = osKernelSysTick();
60 k_busy_wait(WAIT_TIME_US);
61 stop_time = osKernelSysTick();
62
63 diff = (uint32_t)k_cyc_to_ns_floor64(stop_time -
64 start_time) / NSEC_PER_USEC;
65
66 max = WAIT_TIME_US + (TOLERANCE_PPC * WAIT_TIME_US / 100);
67 min = WAIT_TIME_US - (TOLERANCE_PPC * WAIT_TIME_US / 100);
68
69 zassert_true(diff <= max && diff >= min,
70 "start %d stop %d (diff %d) wait %d\n",
71 start_time, stop_time, diff, WAIT_TIME_US);
72 }
73 ZTEST_SUITE(kernel_apis, NULL, NULL, NULL, NULL, NULL);
74