hwtimer_compat.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 hwtimer API
  9. *
  10. * DEPRECATED: This header provides backward compatibility for the legacy hwtimer API.
  11. * New code should use the unified clock_time subsystem instead.
  12. * This compatibility layer will be removed in a future release.
  13. */
  14. #ifndef __HWTIMER_H__
  15. #define __HWTIMER_H__
  16. #include <rtthread.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /*
  21. * For backward compatibility, we keep the hwtimer types and APIs.
  22. * If RT_USING_CLOCK_TIME is defined, these will map to clock_time.
  23. * Otherwise, we include the original hwtimer implementation.
  24. */
  25. #ifdef RT_USING_CLOCK_TIME
  26. /* Timer Control Command */
  27. typedef enum
  28. {
  29. HWTIMER_CTRL_FREQ_SET = RT_DEVICE_CTRL_BASE(Timer) + 0x01, /* set the count frequency */
  30. HWTIMER_CTRL_STOP = RT_DEVICE_CTRL_BASE(Timer) + 0x02, /* stop timer */
  31. HWTIMER_CTRL_INFO_GET = RT_DEVICE_CTRL_BASE(Timer) + 0x03, /* get a timer feature information */
  32. HWTIMER_CTRL_MODE_SET = RT_DEVICE_CTRL_BASE(Timer) + 0x04 /* Setting the timing mode(oneshot/period) */
  33. } rt_hwtimer_ctrl_t;
  34. /* Timing Mode */
  35. typedef enum
  36. {
  37. HWTIMER_MODE_ONESHOT = 0x01,
  38. HWTIMER_MODE_PERIOD
  39. } rt_hwtimer_mode_t;
  40. /* Time Value */
  41. typedef struct rt_hwtimerval
  42. {
  43. rt_int32_t sec; /* second */
  44. rt_int32_t usec; /* microsecond */
  45. } rt_hwtimerval_t;
  46. #define HWTIMER_CNTMODE_UP 0x01 /* increment count mode */
  47. #define HWTIMER_CNTMODE_DW 0x02 /* decreasing count mode */
  48. struct rt_hwtimer_device;
  49. struct rt_hwtimer_ops
  50. {
  51. void (*init)(struct rt_hwtimer_device *timer, rt_uint32_t state);
  52. rt_err_t (*start)(struct rt_hwtimer_device *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode);
  53. void (*stop)(struct rt_hwtimer_device *timer);
  54. rt_uint32_t (*count_get)(struct rt_hwtimer_device *timer);
  55. rt_err_t (*control)(struct rt_hwtimer_device *timer, rt_uint32_t cmd, void *args);
  56. };
  57. /* Timer Feature Information */
  58. struct rt_hwtimer_info
  59. {
  60. rt_int32_t maxfreq; /* the maximum count frequency timer support */
  61. rt_int32_t minfreq; /* the minimum count frequency timer support */
  62. rt_uint32_t maxcnt; /* counter maximum value */
  63. rt_uint8_t cntmode; /* count mode (inc/dec) */
  64. };
  65. typedef struct rt_hwtimer_device
  66. {
  67. struct rt_device parent;
  68. const struct rt_hwtimer_ops *ops;
  69. const struct rt_hwtimer_info *info;
  70. rt_int32_t freq; /* counting frequency set by the user */
  71. rt_int32_t overflow; /* timer overflows */
  72. float period_sec;
  73. rt_int32_t cycles; /* how many times will generate a timeout event after overflow */
  74. rt_int32_t reload; /* reload cycles(using in period mode) */
  75. rt_hwtimer_mode_t mode; /* timing mode(oneshot/period) */
  76. } rt_hwtimer_t;
  77. rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data);
  78. void rt_device_hwtimer_isr(rt_hwtimer_t *timer);
  79. #ifdef RT_USING_DM
  80. extern void (*rt_device_hwtimer_us_delay)(rt_uint32_t us);
  81. #endif
  82. #else /* !RT_USING_CLOCK_TIME */
  83. #warning "RT_USING_HWTIMER is deprecated. Please migrate to RT_USING_CLOCK_TIME."
  84. #warning "Include <drivers/clock_time.h> instead and use rt_clock_time_* APIs."
  85. /* If clock_time is not enabled, this means the old hwtimer module should still exist */
  86. /* The build system should handle this by including the old hwtimer directory */
  87. #endif /* RT_USING_CLOCK_TIME */
  88. #ifdef __cplusplus
  89. }
  90. #endif
  91. #endif /* __HWTIMER_H__ */