hwtimer.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. #ifndef __HWTIMER_H__
  10. #define __HWTIMER_H__
  11. #include <rtthread.h>
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. /* Timer Control Command */
  16. typedef enum
  17. {
  18. HWTIMER_CTRL_FREQ_SET = RT_DEVICE_CTRL_BASE(Timer) + 0x01, /* set the count frequency */
  19. HWTIMER_CTRL_STOP = RT_DEVICE_CTRL_BASE(Timer) + 0x02, /* stop timer */
  20. HWTIMER_CTRL_INFO_GET = RT_DEVICE_CTRL_BASE(Timer) + 0x03, /* get a timer feature information */
  21. HWTIMER_CTRL_MODE_SET = RT_DEVICE_CTRL_BASE(Timer) + 0x04 /* Setting the timing mode(oneshot/period) */
  22. } rt_hwtimer_ctrl_t;
  23. /* Timing Mode */
  24. typedef enum
  25. {
  26. HWTIMER_MODE_ONESHOT = 0x01,
  27. HWTIMER_MODE_PERIOD
  28. } rt_hwtimer_mode_t;
  29. /* Time Value */
  30. typedef struct rt_hwtimerval
  31. {
  32. rt_int32_t sec; /* second */
  33. rt_int32_t usec; /* microsecond */
  34. } rt_hwtimerval_t;
  35. #define HWTIMER_CNTMODE_UP 0x01 /* increment count mode */
  36. #define HWTIMER_CNTMODE_DW 0x02 /* decreasing count mode */
  37. struct rt_hwtimer_device;
  38. struct rt_hwtimer_ops
  39. {
  40. void (*init)(struct rt_hwtimer_device *timer, rt_uint32_t state);
  41. rt_err_t (*start)(struct rt_hwtimer_device *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode);
  42. void (*stop)(struct rt_hwtimer_device *timer);
  43. rt_uint32_t (*count_get)(struct rt_hwtimer_device *timer);
  44. rt_err_t (*control)(struct rt_hwtimer_device *timer, rt_uint32_t cmd, void *args);
  45. };
  46. /* Timer Feature Information */
  47. struct rt_hwtimer_info
  48. {
  49. rt_int32_t maxfreq; /* the maximum count frequency timer support */
  50. rt_int32_t minfreq; /* the minimum count frequency timer support */
  51. rt_uint32_t maxcnt; /* counter maximum value */
  52. rt_uint8_t cntmode; /* count mode (inc/dec) */
  53. };
  54. typedef struct rt_hwtimer_device
  55. {
  56. struct rt_device parent;
  57. const struct rt_hwtimer_ops *ops;
  58. const struct rt_hwtimer_info *info;
  59. rt_int32_t freq; /* counting frequency set by the user */
  60. rt_int32_t overflow; /* timer overflows */
  61. float period_sec;
  62. rt_int32_t cycles; /* how many times will generate a timeout event after overflow */
  63. rt_int32_t reload; /* reload cycles(using in period mode) */
  64. rt_hwtimer_mode_t mode; /* timing mode(oneshot/period) */
  65. } rt_hwtimer_t;
  66. rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data);
  67. void rt_device_hwtimer_isr(rt_hwtimer_t *timer);
  68. #ifdef RT_USING_DM
  69. extern void (*rt_device_hwtimer_us_delay)(rt_uint32_t us);
  70. #endif
  71. #ifdef __cplusplus
  72. }
  73. #endif
  74. #endif