clock_boottime.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. * 2025-01-01 RT-Thread Clock time boottime helpers
  9. */
  10. #include <drivers/clock_time.h>
  11. rt_err_t rt_clock_boottime_get_us(struct timeval *tv)
  12. {
  13. rt_uint64_t cnt;
  14. rt_uint64_t res;
  15. rt_uint64_t ns;
  16. RT_ASSERT(tv != RT_NULL);
  17. cnt = rt_clock_time_get_counter();
  18. res = rt_clock_time_get_res_scaled();
  19. if (res == 0)
  20. {
  21. return -RT_ERROR;
  22. }
  23. ns = (cnt * res) / RT_CLOCK_TIME_RESMUL;
  24. tv->tv_sec = ns / (1000ULL * 1000 * 1000);
  25. tv->tv_usec = (ns % (1000ULL * 1000 * 1000)) / 1000;
  26. return RT_EOK;
  27. }
  28. rt_err_t rt_clock_boottime_get_s(time_t *t)
  29. {
  30. rt_uint64_t cnt;
  31. rt_uint64_t res;
  32. rt_uint64_t ns;
  33. RT_ASSERT(t != RT_NULL);
  34. cnt = rt_clock_time_get_counter();
  35. res = rt_clock_time_get_res_scaled();
  36. if (res == 0)
  37. {
  38. return -RT_ERROR;
  39. }
  40. ns = (cnt * res) / RT_CLOCK_TIME_RESMUL;
  41. *t = ns / (1000ULL * 1000 * 1000);
  42. return RT_EOK;
  43. }
  44. rt_err_t rt_clock_boottime_get_ns(struct timespec *ts)
  45. {
  46. rt_uint64_t cnt;
  47. rt_uint64_t res;
  48. rt_uint64_t ns;
  49. RT_ASSERT(ts != RT_NULL);
  50. cnt = rt_clock_time_get_counter();
  51. res = rt_clock_time_get_res_scaled();
  52. if (res == 0)
  53. {
  54. return -RT_ERROR;
  55. }
  56. ns = (cnt * res) / RT_CLOCK_TIME_RESMUL;
  57. ts->tv_sec = ns / (1000ULL * 1000 * 1000);
  58. ts->tv_nsec = ns % (1000ULL * 1000 * 1000);
  59. return RT_EOK;
  60. }