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 <zephyr/portability/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)17osStatus_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, "Zephyr V%2" PRIu32 ".%2" PRIu32 ".%2" PRIu32, 28 SYS_KERNEL_VER_MAJOR(version->kernel), 29 SYS_KERNEL_VER_MINOR(version->kernel), 30 SYS_KERNEL_VER_PATCHLEVEL(version->kernel)); 31 } 32 33 return osOK; 34 } 35 36 /** 37 * @brief Lock the RTOS Kernel scheduler. 38 */ osKernelLock(void)39int32_t osKernelLock(void) 40 { 41 int temp = _current->base.sched_locked; 42 43 if (k_is_in_isr()) { 44 return osErrorISR; 45 } 46 47 k_sched_lock(); 48 49 return temp; 50 } 51 52 /** 53 * @brief Unlock the RTOS Kernel scheduler. 54 */ osKernelUnlock(void)55int32_t osKernelUnlock(void) 56 { 57 int temp = _current->base.sched_locked; 58 59 if (k_is_in_isr()) { 60 return osErrorISR; 61 } 62 63 k_sched_unlock(); 64 65 return temp; 66 } 67 68 /** 69 * @brief Restore the RTOS Kernel scheduler lock state. 70 */ osKernelRestoreLock(int32_t lock)71int32_t osKernelRestoreLock(int32_t lock) 72 { 73 _current->base.sched_locked = lock; 74 75 if (k_is_in_isr()) { 76 return osErrorISR; 77 } 78 79 if (lock < 0) { 80 return 1; /* locked */ 81 } else { 82 return 0; /* not locked */ 83 } 84 } 85 86 /** 87 * @brief Get the RTOS kernel tick count. 88 */ osKernelGetTickCount(void)89uint32_t osKernelGetTickCount(void) 90 { 91 return sys_clock_tick_get_32(); 92 } 93 94 /** 95 * @brief Get the RTOS kernel tick frequency. 96 */ osKernelGetTickFreq(void)97uint32_t osKernelGetTickFreq(void) 98 { 99 return CONFIG_SYS_CLOCK_TICKS_PER_SEC; 100 } 101 102 /** 103 * @brief Get the RTOS kernel system timer count. 104 */ osKernelGetSysTimerCount(void)105uint32_t osKernelGetSysTimerCount(void) 106 { 107 return k_cycle_get_32(); 108 } 109 110 /** 111 * @brief Get the RTOS kernel system timer frequency. 112 */ osKernelGetSysTimerFreq(void)113uint32_t osKernelGetSysTimerFreq(void) 114 { 115 return sys_clock_hw_cycles_per_sec(); 116 } 117 118 /** 119 * @brief Wait for Timeout (Time Delay). 120 */ osDelay(uint32_t ticks)121osStatus_t osDelay(uint32_t ticks) 122 { 123 if (k_is_in_isr()) { 124 return osErrorISR; 125 } 126 127 k_sleep(K_TICKS(ticks)); 128 129 return osOK; 130 } 131 132 /** 133 * @brief Wait until specified time. 134 */ osDelayUntil(uint32_t ticks)135osStatus_t osDelayUntil(uint32_t ticks) 136 { 137 uint32_t ticks_elapsed; 138 139 if (k_is_in_isr()) { 140 return osErrorISR; 141 } 142 143 ticks_elapsed = osKernelGetTickCount(); 144 k_sleep(K_TICKS(ticks - ticks_elapsed)); 145 146 return osOK; 147 } 148