1 /*
2  * Copyright (c) 2018 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdio.h>
8 #include <string.h>
9 #include <zephyr/kernel.h>
10 #include <cmsis_os2.h>
11 
12 extern uint32_t sys_clock_tick_get_32(void);
13 
14 /**
15  * @brief Get RTOS Kernel Information.
16  */
osKernelGetInfo(osVersion_t * version,char * id_buf,uint32_t id_size)17 osStatus_t osKernelGetInfo(osVersion_t *version, char *id_buf, uint32_t id_size)
18 {
19 	uint32_t ver = sys_kernel_version_get();
20 
21 	if (version != NULL) {
22 		version->api = ver;
23 		version->kernel = ver;
24 	}
25 
26 	if ((id_buf != NULL) && (version != NULL)) {
27 		snprintf(id_buf, id_size,
28 			 "Zephyr V%2"PRIu32".%2"PRIu32".%2"PRIu32,
29 			 SYS_KERNEL_VER_MAJOR(version->kernel),
30 			 SYS_KERNEL_VER_MINOR(version->kernel),
31 			 SYS_KERNEL_VER_PATCHLEVEL(version->kernel));
32 	}
33 
34 	return osOK;
35 }
36 
37 /**
38  * @brief Lock the RTOS Kernel scheduler.
39  */
osKernelLock(void)40 int32_t osKernelLock(void)
41 {
42 	int temp = _current->base.sched_locked;
43 
44 	if (k_is_in_isr()) {
45 		return osErrorISR;
46 	}
47 
48 	k_sched_lock();
49 
50 	return temp;
51 }
52 
53 /**
54  * @brief Unlock the RTOS Kernel scheduler.
55  */
osKernelUnlock(void)56 int32_t osKernelUnlock(void)
57 {
58 	int temp = _current->base.sched_locked;
59 
60 	if (k_is_in_isr()) {
61 		return osErrorISR;
62 	}
63 
64 	k_sched_unlock();
65 
66 	return temp;
67 }
68 
69 /**
70  * @brief Restore the RTOS Kernel scheduler lock state.
71  */
osKernelRestoreLock(int32_t lock)72 int32_t osKernelRestoreLock(int32_t lock)
73 {
74 	_current->base.sched_locked = lock;
75 
76 	if (k_is_in_isr()) {
77 		return osErrorISR;
78 	}
79 
80 	if (lock < 0) {
81 		return 1;       /* locked */
82 	} else {
83 		return 0;       /* not locked */
84 	}
85 }
86 
87 /**
88  * @brief Get the RTOS kernel tick count.
89  */
osKernelGetTickCount(void)90 uint32_t osKernelGetTickCount(void)
91 {
92 	return sys_clock_tick_get_32();
93 }
94 
95 /**
96  * @brief Get the RTOS kernel tick frequency.
97  */
osKernelGetTickFreq(void)98 uint32_t osKernelGetTickFreq(void)
99 {
100 	return CONFIG_SYS_CLOCK_TICKS_PER_SEC;
101 }
102 
103 /**
104  * @brief Get the RTOS kernel system timer count.
105  */
osKernelGetSysTimerCount(void)106 uint32_t osKernelGetSysTimerCount(void)
107 {
108 	return k_cycle_get_32();
109 }
110 
111 /**
112  * @brief Get the RTOS kernel system timer frequency.
113  */
osKernelGetSysTimerFreq(void)114 uint32_t osKernelGetSysTimerFreq(void)
115 {
116 	return sys_clock_hw_cycles_per_sec();
117 }
118 
119 /**
120  * @brief Wait for Timeout (Time Delay).
121  */
osDelay(uint32_t ticks)122 osStatus_t osDelay(uint32_t ticks)
123 {
124 	if (k_is_in_isr()) {
125 		return osErrorISR;
126 	}
127 
128 	k_sleep(K_TICKS(ticks));
129 
130 	return osOK;
131 }
132 
133 /**
134  * @brief Wait until specified time.
135  */
osDelayUntil(uint32_t ticks)136 osStatus_t osDelayUntil(uint32_t ticks)
137 {
138 	uint32_t ticks_elapsed;
139 
140 	if (k_is_in_isr()) {
141 		return osErrorISR;
142 	}
143 
144 	ticks_elapsed = osKernelGetTickCount();
145 	k_sleep(K_TICKS(ticks - ticks_elapsed));
146 
147 	return osOK;
148 }
149