clock_time.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 Unified clock_time subsystem (replaces hwtimer/ktime/cputime)
  9. */
  10. #ifndef __CLOCK_TIME_H__
  11. #define __CLOCK_TIME_H__
  12. #include <rtthread.h>
  13. #include <sys/time.h>
  14. #include <ipc/completion.h>
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. /* Resolution multiplier for time calculations */
  19. #define RT_CLOCK_TIME_RESMUL (1000000ULL)
  20. /* Clock time device capabilities */
  21. #define RT_CLOCK_TIME_CAP_CLOCKSOURCE (1 << 0) /* Device provides time source */
  22. #define RT_CLOCK_TIME_CAP_CLOCKEVENT (1 << 1) /* Device provides event generation */
  23. /**
  24. * @brief Clock time device operations structure
  25. *
  26. * This structure defines the hardware interface for clock/timer devices.
  27. * BSPs should implement these operations for their specific hardware.
  28. */
  29. struct rt_clock_time_ops
  30. {
  31. /**
  32. * Get the counting frequency in Hz
  33. * @return Frequency in Hz
  34. */
  35. rt_uint64_t (*get_freq)(void);
  36. /**
  37. * Get the current free-running counter value
  38. * @return Current counter value
  39. */
  40. rt_uint64_t (*get_counter)(void);
  41. /**
  42. * Set a timeout relative to current counter value
  43. * @param delta Timeout in counter ticks (0 to cancel)
  44. * @return RT_EOK on success, error code otherwise
  45. */
  46. rt_err_t (*set_timeout)(rt_uint64_t delta);
  47. };
  48. /**
  49. * @brief Clock time device structure
  50. *
  51. * Unified device abstraction for time sources and event generators.
  52. * This replaces the separate hwtimer, ktime, and cputime devices.
  53. */
  54. struct rt_clock_time_device
  55. {
  56. struct rt_device parent; /* Standard device interface */
  57. const struct rt_clock_time_ops *ops; /* Hardware operations */
  58. rt_uint64_t res_scale; /* Resolution scale factor */
  59. rt_uint8_t caps; /* Device capabilities (RT_CLOCK_TIME_CAP_*) */
  60. };
  61. typedef struct rt_clock_time_device *rt_clock_time_t;
  62. /**
  63. * @brief High-resolution timer structure
  64. *
  65. * Software timer built on top of clock_time device.
  66. * Compatible with rt_ktime_hrtimer interface.
  67. */
  68. struct rt_clock_hrtimer
  69. {
  70. rt_uint8_t flag; /* Timer flags (compatible with rt_timer) */
  71. char name[RT_NAME_MAX]; /* Timer name */
  72. rt_list_t node; /* List node for timer management */
  73. void *parameter; /* User parameter */
  74. unsigned long delay_cnt; /* Delay count */
  75. unsigned long timeout_cnt; /* Absolute timeout count */
  76. rt_err_t error; /* Last error code */
  77. struct rt_completion completion; /* For synchronous waiting */
  78. void (*timeout_func)(void *parameter); /* Timeout callback */
  79. };
  80. typedef struct rt_clock_hrtimer *rt_clock_hrtimer_t;
  81. /*
  82. * ============================================================================
  83. * Device Management APIs
  84. * ============================================================================
  85. */
  86. /**
  87. * @brief Register a clock time device
  88. *
  89. * @param dev Clock time device to register
  90. * @param name Device name (e.g., "cputimer", "hwtimer0")
  91. * @param caps Device capabilities (RT_CLOCK_TIME_CAP_*)
  92. * @return RT_EOK on success, error code otherwise
  93. */
  94. rt_err_t rt_clock_time_device_register(struct rt_clock_time_device *dev,
  95. const char *name,
  96. rt_uint8_t caps);
  97. /**
  98. * @brief Get the default system clock time device
  99. * @return Pointer to default device, or RT_NULL if none
  100. */
  101. rt_clock_time_t rt_clock_time_default(void);
  102. /**
  103. * @brief Set the default system clock time device
  104. * @param dev Device to set as default
  105. * @return RT_EOK on success, error code otherwise
  106. */
  107. rt_err_t rt_clock_time_set_default(rt_clock_time_t dev);
  108. /*
  109. * ============================================================================
  110. * Clock Source APIs (Boottime)
  111. * ============================================================================
  112. */
  113. /**
  114. * @brief Get boottime with microsecond precision
  115. * @param tv Output timeval structure
  116. * @return RT_EOK on success, error code otherwise
  117. */
  118. rt_err_t rt_clock_boottime_get_us(struct timeval *tv);
  119. /**
  120. * @brief Get boottime with second precision
  121. * @param t Output time_t value
  122. * @return RT_EOK on success, error code otherwise
  123. */
  124. rt_err_t rt_clock_boottime_get_s(time_t *t);
  125. /**
  126. * @brief Get boottime with nanosecond precision
  127. * @param ts Output timespec structure
  128. * @return RT_EOK on success, error code otherwise
  129. */
  130. rt_err_t rt_clock_boottime_get_ns(struct timespec *ts);
  131. /*
  132. * ============================================================================
  133. * CPU Timer APIs (Clock Source)
  134. * ============================================================================
  135. */
  136. /**
  137. * @brief Get CPU timer resolution (resolution * RT_CLOCK_TIME_RESMUL)
  138. * @return Resolution value
  139. */
  140. rt_uint64_t rt_clock_cputimer_getres(void);
  141. /**
  142. * @brief Get CPU timer frequency in Hz
  143. * @return Frequency in Hz
  144. */
  145. unsigned long rt_clock_cputimer_getfrq(void);
  146. /**
  147. * @brief Get current CPU timer counter value
  148. * @return Counter value
  149. */
  150. unsigned long rt_clock_cputimer_getcnt(void);
  151. /**
  152. * @brief Initialize CPU timer subsystem
  153. */
  154. void rt_clock_cputimer_init(void);
  155. /*
  156. * ============================================================================
  157. * High-Resolution Timer APIs
  158. * ============================================================================
  159. */
  160. /**
  161. * @brief Get hrtimer resolution (resolution * RT_CLOCK_TIME_RESMUL)
  162. * @return Resolution value
  163. */
  164. rt_uint64_t rt_clock_hrtimer_getres(void);
  165. /**
  166. * @brief Get hrtimer frequency in Hz
  167. * @return Frequency in Hz
  168. */
  169. unsigned long rt_clock_hrtimer_getfrq(void);
  170. /**
  171. * @brief Set hrtimer interrupt timeout (BSP should implement)
  172. * @param cnt Timeout in counter ticks
  173. * @return RT_EOK on success, error code otherwise
  174. */
  175. rt_err_t rt_clock_hrtimer_settimeout(unsigned long cnt);
  176. /**
  177. * @brief Process expired hrtimers (call from ISR)
  178. */
  179. void rt_clock_hrtimer_process(void);
  180. /**
  181. * @brief Initialize a high-resolution timer
  182. *
  183. * @param timer Timer structure to initialize
  184. * @param name Timer name
  185. * @param flag Timer flags (RT_TIMER_FLAG_*)
  186. * @param timeout Timeout callback function
  187. * @param parameter User parameter for callback
  188. */
  189. void rt_clock_hrtimer_init(rt_clock_hrtimer_t timer,
  190. const char *name,
  191. rt_uint8_t flag,
  192. void (*timeout)(void *parameter),
  193. void *parameter);
  194. /**
  195. * @brief Start a high-resolution timer
  196. * @param timer Timer to start
  197. * @param cnt Timeout in counter ticks
  198. * @return RT_EOK on success, error code otherwise
  199. */
  200. rt_err_t rt_clock_hrtimer_start(rt_clock_hrtimer_t timer, unsigned long cnt);
  201. /**
  202. * @brief Stop a high-resolution timer
  203. * @param timer Timer to stop
  204. * @return RT_EOK on success, error code otherwise
  205. */
  206. rt_err_t rt_clock_hrtimer_stop(rt_clock_hrtimer_t timer);
  207. /**
  208. * @brief Control a high-resolution timer
  209. * @param timer Timer to control
  210. * @param cmd Control command
  211. * @param arg Command argument
  212. * @return RT_EOK on success, error code otherwise
  213. */
  214. rt_err_t rt_clock_hrtimer_control(rt_clock_hrtimer_t timer, int cmd, void *arg);
  215. /**
  216. * @brief Detach a high-resolution timer
  217. * @param timer Timer to detach
  218. * @return RT_EOK on success, error code otherwise
  219. */
  220. rt_err_t rt_clock_hrtimer_detach(rt_clock_hrtimer_t timer);
  221. /**
  222. * @brief Keep errno in timer structure
  223. * @param timer Timer structure
  224. * @param err Error code to keep
  225. *
  226. * Note: This function negates err when setting errno to convert RT-Thread
  227. * error codes to POSIX-style errno values. This maintains compatibility
  228. * with the original ktime implementation.
  229. */
  230. rt_inline void rt_clock_hrtimer_keep_errno(rt_clock_hrtimer_t timer, rt_err_t err)
  231. {
  232. RT_ASSERT(timer != RT_NULL);
  233. timer->error = err;
  234. rt_set_errno(-err);
  235. }
  236. /**
  237. * @brief Initialize timer for delay operations
  238. * @param timer Timer structure
  239. */
  240. void rt_clock_hrtimer_delay_init(struct rt_clock_hrtimer *timer);
  241. /**
  242. * @brief Detach timer after delay operations
  243. * @param timer Timer structure
  244. */
  245. void rt_clock_hrtimer_delay_detach(struct rt_clock_hrtimer *timer);
  246. /**
  247. * @brief Sleep for specified counter ticks
  248. * @param timer Timer structure to use
  249. * @param cnt Number of counter ticks to sleep
  250. * @return RT_EOK on success, error code otherwise
  251. */
  252. rt_err_t rt_clock_hrtimer_sleep(struct rt_clock_hrtimer *timer, unsigned long cnt);
  253. /**
  254. * @brief Delay for specified nanoseconds
  255. * @param timer Timer structure to use
  256. * @param ns Nanoseconds to delay
  257. * @return RT_EOK on success, error code otherwise
  258. */
  259. rt_err_t rt_clock_hrtimer_ndelay(struct rt_clock_hrtimer *timer, unsigned long ns);
  260. /**
  261. * @brief Delay for specified microseconds
  262. * @param timer Timer structure to use
  263. * @param us Microseconds to delay
  264. * @return RT_EOK on success, error code otherwise
  265. */
  266. rt_err_t rt_clock_hrtimer_udelay(struct rt_clock_hrtimer *timer, unsigned long us);
  267. /**
  268. * @brief Delay for specified milliseconds
  269. * @param timer Timer structure to use
  270. * @param ms Milliseconds to delay
  271. * @return RT_EOK on success, error code otherwise
  272. */
  273. rt_err_t rt_clock_hrtimer_mdelay(struct rt_clock_hrtimer *timer, unsigned long ms);
  274. /*
  275. * ============================================================================
  276. * Legacy Compatibility Macros (Will be removed in future versions)
  277. * ============================================================================
  278. */
  279. /* Compatibility with rt_ktime_* APIs */
  280. #define RT_KTIME_RESMUL RT_CLOCK_TIME_RESMUL
  281. #define rt_ktime_hrtimer rt_clock_hrtimer
  282. #define rt_ktime_hrtimer_t rt_clock_hrtimer_t
  283. #define rt_ktime_boottime_get_us rt_clock_boottime_get_us
  284. #define rt_ktime_boottime_get_s rt_clock_boottime_get_s
  285. #define rt_ktime_boottime_get_ns rt_clock_boottime_get_ns
  286. #define rt_ktime_cputimer_getres rt_clock_cputimer_getres
  287. #define rt_ktime_cputimer_getfrq rt_clock_cputimer_getfrq
  288. #define rt_ktime_cputimer_getcnt rt_clock_cputimer_getcnt
  289. #define rt_ktime_cputimer_init rt_clock_cputimer_init
  290. #define rt_ktime_hrtimer_getres rt_clock_hrtimer_getres
  291. #define rt_ktime_hrtimer_getfrq rt_clock_hrtimer_getfrq
  292. #define rt_ktime_hrtimer_settimeout rt_clock_hrtimer_settimeout
  293. #define rt_ktime_hrtimer_process rt_clock_hrtimer_process
  294. #define rt_ktime_hrtimer_init rt_clock_hrtimer_init
  295. #define rt_ktime_hrtimer_start rt_clock_hrtimer_start
  296. #define rt_ktime_hrtimer_stop rt_clock_hrtimer_stop
  297. #define rt_ktime_hrtimer_control rt_clock_hrtimer_control
  298. #define rt_ktime_hrtimer_detach rt_clock_hrtimer_detach
  299. #define rt_ktime_hrtimer_keep_errno rt_clock_hrtimer_keep_errno
  300. #define rt_ktime_hrtimer_delay_init rt_clock_hrtimer_delay_init
  301. #define rt_ktime_hrtimer_delay_detach rt_clock_hrtimer_delay_detach
  302. #define rt_ktime_hrtimer_sleep rt_clock_hrtimer_sleep
  303. #define rt_ktime_hrtimer_ndelay rt_clock_hrtimer_ndelay
  304. #define rt_ktime_hrtimer_udelay rt_clock_hrtimer_udelay
  305. #define rt_ktime_hrtimer_mdelay rt_clock_hrtimer_mdelay
  306. #ifdef __cplusplus
  307. }
  308. #endif
  309. #endif /* __CLOCK_TIME_H__ */