ktime.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (c) 2006-2025, 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 Compatibility layer for legacy ktime API
  9. *
  10. * COMPATIBILITY HEADER:
  11. * This header provides backward compatibility for code using the old ktime API.
  12. * All rt_ktime_* APIs are now redirected to rt_clock_* APIs.
  13. *
  14. * The old ktime subsystem has been removed and replaced with the unified
  15. * clock_time subsystem. Include <drivers/clock_time.h> for new code.
  16. */
  17. #ifndef __KTIME_H__
  18. #define __KTIME_H__
  19. #include <rtthread.h>
  20. #include <sys/time.h>
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #ifdef RT_USING_CLOCK_TIME
  25. /* Include the unified clock_time header which provides all APIs */
  26. #include <drivers/clock_time.h>
  27. /* All rt_ktime_* APIs are already defined as macros in clock_time.h */
  28. #else
  29. /* When clock_time is not enabled, provide stub implementations for backward compatibility */
  30. /* These are minimal stub implementations to maintain compilation compatibility */
  31. rt_inline rt_err_t rt_ktime_boottime_get_ns(struct timespec *ts)
  32. {
  33. rt_uint64_t tick = rt_tick_get();
  34. rt_uint64_t ns = tick * (1000000000ULL / RT_TICK_PER_SECOND);
  35. ts->tv_sec = ns / 1000000000ULL;
  36. ts->tv_nsec = ns % 1000000000ULL;
  37. return RT_EOK;
  38. }
  39. rt_inline rt_err_t rt_ktime_boottime_get_us(struct timeval *tv)
  40. {
  41. rt_uint64_t tick = rt_tick_get();
  42. rt_uint64_t us = tick * (1000000ULL / RT_TICK_PER_SECOND);
  43. tv->tv_sec = us / 1000000ULL;
  44. tv->tv_usec = us % 1000000ULL;
  45. return RT_EOK;
  46. }
  47. rt_inline void rt_ktime_cputimer_init(void)
  48. {
  49. /* Stub implementation */
  50. }
  51. #endif /* RT_USING_CLOCK_TIME */
  52. #ifdef __cplusplus
  53. }
  54. #endif
  55. #endif /* __KTIME_H__ */