clock_time.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 Unified clock_time subsystem
  9. */
  10. #ifndef __CLOCK_TIME_H__
  11. #define __CLOCK_TIME_H__
  12. #include <rtthread.h>
  13. #include <sys/time.h>
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #define RT_CLOCK_TIME_RESMUL (1000000ULL)
  18. /* Capabilities flags */
  19. #define RT_CLOCK_TIME_CAP_CLOCKSOURCE (1 << 0) /* Provides free-running counter */
  20. #define RT_CLOCK_TIME_CAP_CLOCKEVENT (1 << 1) /* Supports oneshot timeout events */
  21. /**
  22. * @brief Clock time device operations
  23. *
  24. * This structure defines the operations that a clock time device must support.
  25. * At minimum, get_freq and get_counter must be implemented for clocksource capability.
  26. * For clockevent capability, set_timeout must also be implemented.
  27. */
  28. struct rt_clock_time_ops
  29. {
  30. rt_uint64_t (*get_freq)(void); /* Get counting frequency in Hz */
  31. rt_uint64_t (*get_counter)(void); /* Get current free-running counter value */
  32. rt_err_t (*set_timeout)(rt_uint64_t delta); /* Set relative timeout in counter units, 0 to cancel */
  33. };
  34. /**
  35. * @brief Clock time device structure
  36. *
  37. * This represents a unified time device that can provide both clocksource
  38. * (continuous time counter) and clockevent (programmable timeout) capabilities.
  39. */
  40. struct rt_clock_time_device
  41. {
  42. struct rt_device parent; /* Inherit from rt_device */
  43. const struct rt_clock_time_ops *ops; /* Device operations */
  44. rt_uint64_t res_scale; /* Resolution scale factor (ns * scale / freq -> cnt) */
  45. rt_uint8_t caps; /* Capability flags */
  46. };
  47. typedef struct rt_clock_time_device *rt_clock_time_t;
  48. /**
  49. * @brief Register a clock time device
  50. *
  51. * @param dev Clock time device to register
  52. * @param name Device name
  53. * @param caps Capability flags (RT_CLOCK_TIME_CAP_CLOCKSOURCE | RT_CLOCK_TIME_CAP_CLOCKEVENT)
  54. * @return RT_EOK on success, error code otherwise
  55. */
  56. rt_err_t rt_clock_time_device_register(struct rt_clock_time_device *dev,
  57. const char *name,
  58. rt_uint8_t caps);
  59. /**
  60. * @brief Set the system default clock time device
  61. *
  62. * @param dev Clock time device to set as default
  63. * @return RT_EOK on success, error code otherwise
  64. */
  65. rt_err_t rt_clock_time_set_default(struct rt_clock_time_device *dev);
  66. /**
  67. * @brief Get the system default clock time device
  68. *
  69. * @return Pointer to default clock time device, or RT_NULL if none set
  70. */
  71. struct rt_clock_time_device *rt_clock_time_get_default(void);
  72. /* Clocksource APIs - Get time information */
  73. /**
  74. * @brief Get clock resolution in nanoseconds
  75. *
  76. * @return Resolution value multiplied by RT_CLOCK_TIME_RESMUL
  77. */
  78. rt_uint64_t rt_clock_time_getres(void);
  79. /**
  80. * @brief Get clock frequency in Hz
  81. *
  82. * @return Frequency in Hz
  83. */
  84. rt_uint64_t rt_clock_time_getfreq(void);
  85. /**
  86. * @brief Get current counter value
  87. *
  88. * @return Current counter value
  89. */
  90. rt_uint64_t rt_clock_time_getcnt(void);
  91. /**
  92. * @brief Get boottime (time since boot) in various formats
  93. */
  94. rt_err_t rt_clock_time_boottime_ns(struct timespec *ts);
  95. rt_err_t rt_clock_time_boottime_us(struct timeval *tv);
  96. rt_err_t rt_clock_time_boottime_s(time_t *t);
  97. /**
  98. * @brief Convert counter ticks to time units
  99. */
  100. rt_uint64_t rt_clock_time_cnt_to_ns(rt_uint64_t cnt);
  101. rt_uint64_t rt_clock_time_cnt_to_us(rt_uint64_t cnt);
  102. rt_uint64_t rt_clock_time_cnt_to_ms(rt_uint64_t cnt);
  103. /**
  104. * @brief Convert time units to counter ticks
  105. */
  106. rt_uint64_t rt_clock_time_ns_to_cnt(rt_uint64_t ns);
  107. rt_uint64_t rt_clock_time_us_to_cnt(rt_uint64_t us);
  108. rt_uint64_t rt_clock_time_ms_to_cnt(rt_uint64_t ms);
  109. /* High-resolution timer (hrtimer) APIs */
  110. struct rt_clock_hrtimer
  111. {
  112. rt_uint8_t flag; /* Timer flags (compatible with rt_timer) */
  113. char name[RT_NAME_MAX]; /* Timer name */
  114. rt_list_t node; /* List node */
  115. void *parameter; /* User parameter */
  116. unsigned long delay_cnt; /* Delay in counter ticks */
  117. unsigned long timeout_cnt; /* Absolute timeout counter value */
  118. rt_err_t error; /* Error code */
  119. struct rt_completion completion; /* Completion for blocking waits */
  120. void (*timeout_func)(void *parameter); /* Timeout callback */
  121. };
  122. typedef struct rt_clock_hrtimer *rt_clock_hrtimer_t;
  123. /**
  124. * @brief Initialize a high-resolution timer
  125. *
  126. * @param timer Timer structure to initialize
  127. * @param name Timer name
  128. * @param flag Timer flags (RT_TIMER_FLAG_ONE_SHOT or RT_TIMER_FLAG_PERIODIC)
  129. * @param timeout Timeout callback function
  130. * @param parameter Parameter passed to timeout callback
  131. */
  132. void rt_clock_hrtimer_init(rt_clock_hrtimer_t timer,
  133. const char *name,
  134. rt_uint8_t flag,
  135. void (*timeout)(void *parameter),
  136. void *parameter);
  137. /**
  138. * @brief Start a high-resolution timer
  139. *
  140. * @param timer Timer to start
  141. * @param delay_cnt Delay in counter ticks
  142. * @return RT_EOK on success, error code otherwise
  143. */
  144. rt_err_t rt_clock_hrtimer_start(rt_clock_hrtimer_t timer, unsigned long delay_cnt);
  145. /**
  146. * @brief Stop a high-resolution timer
  147. *
  148. * @param timer Timer to stop
  149. * @return RT_EOK on success, error code otherwise
  150. */
  151. rt_err_t rt_clock_hrtimer_stop(rt_clock_hrtimer_t timer);
  152. /**
  153. * @brief Control a high-resolution timer
  154. *
  155. * @param timer Timer to control
  156. * @param cmd Control command
  157. * @param arg Command argument
  158. * @return RT_EOK on success, error code otherwise
  159. */
  160. rt_err_t rt_clock_hrtimer_control(rt_clock_hrtimer_t timer, int cmd, void *arg);
  161. /**
  162. * @brief Detach a high-resolution timer
  163. *
  164. * @param timer Timer to detach
  165. * @return RT_EOK on success, error code otherwise
  166. */
  167. rt_err_t rt_clock_hrtimer_detach(rt_clock_hrtimer_t timer);
  168. /**
  169. * @brief High-precision delay functions
  170. */
  171. rt_err_t rt_clock_hrtimer_sleep(rt_clock_hrtimer_t timer, unsigned long cnt);
  172. rt_err_t rt_clock_hrtimer_ndelay(rt_clock_hrtimer_t timer, unsigned long ns);
  173. rt_err_t rt_clock_hrtimer_udelay(rt_clock_hrtimer_t timer, unsigned long us);
  174. rt_err_t rt_clock_hrtimer_mdelay(rt_clock_hrtimer_t timer, unsigned long ms);
  175. /**
  176. * @brief Simple delay functions (use internal timer)
  177. */
  178. rt_err_t rt_clock_ndelay(unsigned long ns);
  179. rt_err_t rt_clock_udelay(unsigned long us);
  180. rt_err_t rt_clock_mdelay(unsigned long ms);
  181. /**
  182. * @brief Process hrtimer timeouts (called from device driver ISR)
  183. */
  184. void rt_clock_hrtimer_process(void);
  185. /* POSIX clock support */
  186. #define CLOCK_REALTIME_ALARM 8
  187. #define CLOCK_BOOTTIME_ALARM 9
  188. /* CPU time and boottime APIs */
  189. uint64_t clock_cpu_getres(void);
  190. uint64_t clock_cpu_gettime(void);
  191. uint64_t clock_cpu_microsecond(uint64_t cpu_tick);
  192. uint64_t clock_cpu_millisecond(uint64_t cpu_tick);
  193. rt_err_t rt_cputime_ndelay(rt_uint64_t ns);
  194. rt_err_t rt_cputime_udelay(rt_uint64_t us);
  195. rt_err_t rt_cputime_mdelay(rt_uint64_t ms);
  196. rt_err_t rt_boottime_get_us(struct timeval *tv);
  197. rt_err_t rt_boottime_get_s(time_t *t);
  198. rt_err_t rt_boottime_get_ns(struct timespec *ts);
  199. #ifdef __cplusplus
  200. }
  201. #endif
  202. #endif /* __CLOCK_TIME_H__ */