kernel.c 2.4 KB

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