clock_time_cputime.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-12-04 RT-Thread CPU time and legacy cputime API implementation
  9. */
  10. #include <rtdevice.h>
  11. #include <rthw.h>
  12. #include <sys/errno.h>
  13. /**
  14. * @brief Get CPU time resolution
  15. *
  16. * @return Resolution in nanoseconds * 1000000
  17. */
  18. uint64_t clock_cpu_getres(void)
  19. {
  20. return rt_clock_time_getres();
  21. }
  22. /**
  23. * @brief Get current CPU time counter value
  24. *
  25. * @return Current counter value
  26. */
  27. uint64_t clock_cpu_gettime(void)
  28. {
  29. return rt_clock_time_getcnt();
  30. }
  31. /**
  32. * @brief Convert CPU ticks to microseconds
  33. *
  34. * @param cpu_tick CPU tick count
  35. * @return Microseconds
  36. */
  37. uint64_t clock_cpu_microsecond(uint64_t cpu_tick)
  38. {
  39. return rt_clock_time_cnt_to_us(cpu_tick);
  40. }
  41. /**
  42. * @brief Convert CPU ticks to milliseconds
  43. *
  44. * @param cpu_tick CPU tick count
  45. * @return Milliseconds
  46. */
  47. uint64_t clock_cpu_millisecond(uint64_t cpu_tick)
  48. {
  49. return rt_clock_time_cnt_to_ms(cpu_tick);
  50. }
  51. /**
  52. * @brief High-precision nanosecond delay
  53. *
  54. * @param ns Nanoseconds to delay
  55. * @return RT_EOK on success
  56. */
  57. rt_err_t rt_cputime_ndelay(rt_uint64_t ns)
  58. {
  59. return rt_clock_ndelay((unsigned long)ns);
  60. }
  61. /**
  62. * @brief High-precision microsecond delay
  63. *
  64. * @param us Microseconds to delay
  65. * @return RT_EOK on success
  66. */
  67. rt_err_t rt_cputime_udelay(rt_uint64_t us)
  68. {
  69. return rt_clock_udelay((unsigned long)us);
  70. }
  71. /**
  72. * @brief High-precision millisecond delay
  73. *
  74. * @param ms Milliseconds to delay
  75. * @return RT_EOK on success
  76. */
  77. rt_err_t rt_cputime_mdelay(rt_uint64_t ms)
  78. {
  79. return rt_clock_mdelay((unsigned long)ms);
  80. }