hwtimer.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file hwtimer.h
  29. * @author Nations
  30. * @version v1.0.0
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #ifndef __HWTIMER_H__
  35. #define __HWTIMER_H__
  36. #include <rtthread.h>
  37. #include <rtdevice.h>
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /* Timer Control Command */
  42. typedef enum
  43. {
  44. HWTIMER_CTRL_FREQ_SET = 0x01, /* set the count frequency */
  45. HWTIMER_CTRL_STOP, /* stop timer */
  46. HWTIMER_CTRL_INFO_GET, /* get a timer feature information */
  47. HWTIMER_CTRL_MODE_SET /* Setting the timing mode(oneshot/period) */
  48. } rt_hwtimer_ctrl_t;
  49. /* Timing Mode */
  50. typedef enum
  51. {
  52. HWTIMER_MODE_ONESHOT = 0x01,
  53. HWTIMER_MODE_PERIOD
  54. } rt_hwtimer_mode_t;
  55. /* Time Value */
  56. typedef struct rt_hwtimerval
  57. {
  58. rt_int32_t sec; /* second */
  59. rt_int32_t usec; /* microsecond */
  60. } rt_hwtimerval_t;
  61. #define HWTIMER_CNTMODE_UP 0x01 /* increment count mode */
  62. #define HWTIMER_CNTMODE_DW 0x02 /* decreasing count mode */
  63. struct rt_hwtimer_device;
  64. struct rt_hwtimer_ops
  65. {
  66. void (*init)(struct rt_hwtimer_device *timer, rt_uint32_t state);
  67. rt_err_t (*start)(struct rt_hwtimer_device *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode);
  68. void (*stop)(struct rt_hwtimer_device *timer);
  69. rt_uint32_t (*count_get)(struct rt_hwtimer_device *timer);
  70. rt_err_t (*control)(struct rt_hwtimer_device *timer, rt_uint32_t cmd, void *args);
  71. };
  72. /* Timer Feature Information */
  73. struct rt_hwtimer_info
  74. {
  75. rt_int32_t maxfreq; /* the maximum count frequency timer support */
  76. rt_int32_t minfreq; /* the minimum count frequency timer support */
  77. rt_uint32_t maxcnt; /* counter maximum value */
  78. rt_uint8_t cntmode; /* count mode (inc/dec) */
  79. };
  80. typedef struct rt_hwtimer_device
  81. {
  82. struct rt_device parent;
  83. const struct rt_hwtimer_ops *ops;
  84. const struct rt_hwtimer_info *info;
  85. rt_int32_t freq; /* counting frequency set by the user */
  86. rt_int32_t overflow; /* timer overflows */
  87. float period_sec;
  88. rt_int32_t cycles; /* how many times will generate a timeout event after overflow */
  89. rt_int32_t reload; /* reload cycles(using in period mode) */
  90. rt_hwtimer_mode_t mode; /* timing mode(oneshot/period) */
  91. } rt_hwtimer_t;
  92. rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data);
  93. void rt_device_hwtimer_isr(rt_hwtimer_t *timer);
  94. #ifdef __cplusplus
  95. }
  96. #endif
  97. #endif