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 
12 #include <rtthread.h>
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 /* Timer Control Command */
19 typedef enum
20 {
21     HWTIMER_CTRL_FREQ_SET = RT_DEVICE_CTRL_BASE(Timer) + 0x01,           /* set the count frequency */
22     HWTIMER_CTRL_STOP = RT_DEVICE_CTRL_BASE(Timer) + 0x02,               /* stop timer */
23     HWTIMER_CTRL_INFO_GET = RT_DEVICE_CTRL_BASE(Timer) + 0x03,           /* get a timer feature information */
24     HWTIMER_CTRL_MODE_SET = RT_DEVICE_CTRL_BASE(Timer) + 0x04            /* Setting the timing mode(oneshot/period) */
25 } rt_hwtimer_ctrl_t;
26 
27 /* Timing Mode */
28 typedef enum
29 {
30     HWTIMER_MODE_ONESHOT = 0x01,
31     HWTIMER_MODE_PERIOD
32 } rt_hwtimer_mode_t;
33 
34 /* Time Value */
35 typedef struct rt_hwtimerval
36 {
37     rt_int32_t sec;      /* second */
38     rt_int32_t usec;     /* microsecond */
39 } rt_hwtimerval_t;
40 
41 #define HWTIMER_CNTMODE_UP      0x01 /* increment count mode */
42 #define HWTIMER_CNTMODE_DW      0x02 /* decreasing count mode */
43 
44 struct rt_hwtimer_device;
45 
46 struct rt_hwtimer_ops
47 {
48     void (*init)(struct rt_hwtimer_device *timer, rt_uint32_t state);
49     rt_err_t (*start)(struct rt_hwtimer_device *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode);
50     void (*stop)(struct rt_hwtimer_device *timer);
51     rt_uint32_t (*count_get)(struct rt_hwtimer_device *timer);
52     rt_err_t (*control)(struct rt_hwtimer_device *timer, rt_uint32_t cmd, void *args);
53 };
54 
55 /* Timer Feature Information */
56 struct rt_hwtimer_info
57 {
58     rt_int32_t maxfreq;    /* the maximum count frequency timer support */
59     rt_int32_t minfreq;    /* the minimum count frequency timer support */
60     rt_uint32_t maxcnt;    /* counter maximum value */
61     rt_uint8_t  cntmode;   /* count mode (inc/dec) */
62 };
63 
64 typedef struct rt_hwtimer_device
65 {
66     struct rt_device parent;
67     const struct rt_hwtimer_ops *ops;
68     const struct rt_hwtimer_info *info;
69 
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 
78 rt_err_t rt_device_hwtimer_register(rt_hwtimer_t *timer, const char *name, void *user_data);
79 void rt_device_hwtimer_isr(rt_hwtimer_t *timer);
80 
81 #ifdef RT_USING_DM
82 extern void (*rt_device_hwtimer_us_delay)(rt_uint32_t us);
83 #endif
84 
85 #ifdef __cplusplus
86 }
87 #endif
88 
89 #endif
90