hwtimer.h 2.3 KB

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