hwtimer.h 2.5 KB

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