1 /*
2  * Copyright (c) 2018 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <cmsis_os2.h>
9 #include <zephyr/irq_offload.h>
10 #include <zephyr/kernel.h>
11 
12 uint32_t tick;
13 osStatus_t status_val;
14 
15 typedef struct {
16 	osVersion_t os_info;
17 	char info[100];
18 } versionInfo;
19 
get_version_check(const void * param)20 void get_version_check(const void *param)
21 {
22 	char infobuf[100];
23 	osVersion_t osv;
24 	osStatus_t status;
25 	versionInfo *version_i = (versionInfo *)param;
26 
27 	status = osKernelGetInfo(&osv, infobuf, sizeof(infobuf));
28 	if (status == osOK) {
29 		version_i->os_info.api = osv.api;
30 		version_i->os_info.kernel = osv.kernel;
31 		strcpy(version_i->info, infobuf);
32 	}
33 }
34 
lock_unlock_check(const void * arg)35 void lock_unlock_check(const void *arg)
36 {
37 	int32_t state_before_lock, state_after_lock, current_state;
38 
39 	ARG_UNUSED(arg);
40 
41 	state_before_lock = osKernelLock();
42 	if (k_is_in_isr()) {
43 		zassert_true(state_before_lock == osErrorISR);
44 	}
45 
46 	state_after_lock = osKernelUnlock();
47 	if (k_is_in_isr()) {
48 		zassert_true(state_after_lock == osErrorISR);
49 	} else {
50 		zassert_true(state_before_lock == !state_after_lock);
51 	}
52 	current_state = osKernelRestoreLock(state_before_lock);
53 	if (k_is_in_isr()) {
54 		zassert_true(current_state == osErrorISR);
55 	} else {
56 		zassert_equal(current_state, state_before_lock);
57 	}
58 }
59 
ZTEST(cmsis_kernel,test_kernel_apis)60 ZTEST(cmsis_kernel, test_kernel_apis)
61 {
62 	versionInfo version = {
63 		.os_info = {
64 			.api = 0xfefefefe,
65 			.kernel = 0xfdfdfdfd,
66 		},
67 		.info = "local function call info is uninitialized"
68 	};
69 	versionInfo version_irq = {
70 		.os_info = {
71 			.api = 0xfcfcfcfc,
72 			.kernel = 0xfbfbfbfb,
73 		},
74 		.info = "irq_offload function call info is uninitialized"
75 	};
76 
77 	get_version_check(&version);
78 	irq_offload(get_version_check, (const void *)&version_irq);
79 
80 	/* Check if the version value retrieved in ISR and thread is same */
81 	zassert_str_equal(version.info, version_irq.info);
82 	zassert_equal(version.os_info.api, version_irq.os_info.api);
83 	zassert_equal(version.os_info.kernel, version_irq.os_info.kernel);
84 
85 	lock_unlock_check(NULL);
86 
87 	irq_offload(lock_unlock_check, NULL);
88 }
89 
delay_until(const void * param)90 void delay_until(const void *param)
91 {
92 	ARG_UNUSED(param);
93 
94 	tick = osKernelGetTickCount();
95 	tick += 50U;
96 
97 	status_val = osDelayUntil(tick);
98 }
99 
ZTEST(cmsis_kernel,test_delay)100 ZTEST(cmsis_kernel, test_delay)
101 {
102 	delay_until(NULL);
103 	zassert_true(tick <= osKernelGetTickCount());
104 	zassert_equal(status_val, osOK);
105 
106 	irq_offload(delay_until, NULL);
107 	zassert_equal(status_val, osErrorISR);
108 }
109 ZTEST_SUITE(cmsis_kernel, NULL, NULL, NULL, NULL, NULL);
110