1
0

cputime.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-12-23 Bernard first version
  9. */
  10. #include <rtdevice.h>
  11. #include <rtthread.h>
  12. #include <sys/errno.h>
  13. static const struct rt_clock_cputime_ops *_cputime_ops = RT_NULL;
  14. /**
  15. * The clock_cpu_getres() function shall return the resolution of CPU time, the
  16. * number of nanosecond per tick.
  17. *
  18. * @return the number of nanosecond per tick
  19. */
  20. double clock_cpu_getres(void)
  21. {
  22. if (_cputime_ops)
  23. return _cputime_ops->cputime_getres();
  24. rt_set_errno(ENOSYS);
  25. return 0;
  26. }
  27. /**
  28. * The clock_cpu_gettime() function shall return the current value of cpu time tick.
  29. *
  30. * @return the cpu tick
  31. */
  32. uint64_t clock_cpu_gettime(void)
  33. {
  34. if (_cputime_ops)
  35. return _cputime_ops->cputime_gettime();
  36. rt_set_errno(ENOSYS);
  37. return 0;
  38. }
  39. /**
  40. * The clock_cpu_settimeout() fucntion set timeout time and timeout callback function
  41. * The timeout callback function will be called when the timeout time is reached
  42. *
  43. * @param tick the Timeout tick
  44. * @param timeout the Timeout function
  45. * @param parameter the Parameters of timeout function
  46. *
  47. */
  48. int clock_cpu_settimeout(uint64_t tick, void (*timeout)(void *param), void *param)
  49. {
  50. if (_cputime_ops)
  51. return _cputime_ops->cputime_settimeout(tick, timeout, param);
  52. rt_set_errno(ENOSYS);
  53. return 0;
  54. }
  55. int clock_cpu_issettimeout(void)
  56. {
  57. if (_cputime_ops)
  58. return _cputime_ops->cputime_settimeout != RT_NULL;
  59. return RT_FALSE;
  60. }
  61. /**
  62. * The clock_cpu_microsecond() fucntion shall return the microsecond according to
  63. * cpu_tick parameter.
  64. *
  65. * @param cpu_tick the cpu tick
  66. *
  67. * @return the microsecond
  68. */
  69. uint64_t clock_cpu_microsecond(uint64_t cpu_tick)
  70. {
  71. double unit = clock_cpu_getres();
  72. return (uint64_t)((cpu_tick * unit) / 1000);
  73. }
  74. /**
  75. * The clock_cpu_microsecond() fucntion shall return the millisecond according to
  76. * cpu_tick parameter.
  77. *
  78. * @param cpu_tick the cpu tick
  79. *
  80. * @return the millisecond
  81. */
  82. uint64_t clock_cpu_millisecond(uint64_t cpu_tick)
  83. {
  84. double unit = clock_cpu_getres();
  85. return (uint64_t)((cpu_tick * unit) / (1000 * 1000));
  86. }
  87. /**
  88. * The clock_cpu_seops() function shall set the ops of cpu time.
  89. *
  90. * @return always return 0.
  91. */
  92. int clock_cpu_setops(const struct rt_clock_cputime_ops *ops)
  93. {
  94. _cputime_ops = ops;
  95. if (ops)
  96. {
  97. RT_ASSERT(ops->cputime_getres != RT_NULL);
  98. RT_ASSERT(ops->cputime_gettime != RT_NULL);
  99. }
  100. return 0;
  101. }