1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-11-15 BetMul first version
*/
#include "drv_hwtimer.h"
#include <rtthread.h>
#include <rtdevice.h>
#include "driver/gptimer.h"
#include "sdkconfig.h"
#ifdef RT_USING_HWTIMER
/**
* handle interrupt for hwtimer.
*/
static bool mcu_hwtimer_intr_handler(gptimer_handle_t gptimer, const gptimer_alarm_event_data_t *edata, void *user_ctx)
{
rt_interrupt_enter();
rt_hwtimer_t *hwtimer = (rt_hwtimer_t *)user_ctx;
rt_device_hwtimer_isr(hwtimer);
rt_interrupt_leave();
return 0;
}
/**
* init the hwtimer
*/
static void mcu_hwtimer_init(rt_hwtimer_t *timer, rt_uint32_t state)
{
gptimer_handle_t gptimer = (gptimer_handle_t)timer->parent.user_data;
// let the gptimer into enable status
ESP_ERROR_CHECK(gptimer_enable(gptimer));
}
/**
* start the hwtimer, change status into running
*/
static rt_err_t mcu_hwtimer_start(rt_hwtimer_t *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode)
{
gptimer_handle_t gptimer = (gptimer_handle_t)timer->parent.user_data;
gptimer_alarm_config_t alarm_config = {
.alarm_count = cnt,
};
if (mode == HWTIMER_MODE_ONESHOT)
{
}
else
{
alarm_config.reload_count = 0;
alarm_config.flags.auto_reload_on_alarm = true;
}
ESP_ERROR_CHECK(gptimer_set_alarm_action(gptimer, &alarm_config));
ESP_ERROR_CHECK(gptimer_start(gptimer));
return RT_EOK;
}
/**
* stop the hwtimer, change the status from running into enable
*/
static void mcu_hwtimer_stop(rt_hwtimer_t *timer)
{
gptimer_handle_t gptimer = (gptimer_handle_t)timer->parent.user_data;
ESP_ERROR_CHECK(gptimer_stop(gptimer));
}
/**
* get count
*/
static rt_uint32_t mcu_hwtimer_count_get(rt_hwtimer_t *timer)
{
gptimer_handle_t gptimer = (gptimer_handle_t)timer->parent.user_data;
// get count number
uint64_t value;
ESP_ERROR_CHECK(gptimer_get_raw_count(gptimer, &value));
return (rt_uint32_t)value;
}
/**
* control the hwtimer
*/
static rt_err_t mcu_hwtimer_control(rt_hwtimer_t *timer, rt_uint32_t cmd, void *args)
{
rt_err_t err = RT_EOK;
switch (cmd)
{
case HWTIMER_CTRL_FREQ_SET:
err = -RT_ERROR;
break;
case HWTIMER_CTRL_INFO_GET:
err = -RT_ERROR;
break;
case HWTIMER_CTRL_MODE_SET:
timer->mode = *(rt_uint32_t *)args;
break;
case HWTIMER_CTRL_STOP:
mcu_hwtimer_stop(timer);
break;
}
return err;
}
static struct rt_hwtimer_device _hwtimer;
static const struct rt_hwtimer_ops _hwtimer_ops =
{
.init = mcu_hwtimer_init,
.start = mcu_hwtimer_start,
.stop = mcu_hwtimer_stop,
.count_get = mcu_hwtimer_count_get,
.control = mcu_hwtimer_control};
static const struct rt_hwtimer_info _hwtimer_info =
{
// TODO:what is the true max and min?
.maxfreq = 1000000UL,
.minfreq = 1000000UL,
.maxcnt = 0xFFFF,
.cntmode = HWTIMER_MODE_ONESHOT};
int rt_hw_hwtimer_init(void)
{
char *name = "timer0";
gptimer_handle_t gptimer = NULL;
gptimer_config_t timer_config = {
.clk_src = GPTIMER_CLK_SRC_DEFAULT,
.direction = GPTIMER_COUNT_UP,
.resolution_hz = 1 * 1000 * 1000,
};
gptimer_event_callbacks_t cbs = {
.on_alarm = mcu_hwtimer_intr_handler,
};
ESP_ERROR_CHECK(gptimer_new_timer(&timer_config, &gptimer));
ESP_ERROR_CHECK(gptimer_register_event_callbacks(gptimer, &cbs, &_hwtimer));
_hwtimer.info = &_hwtimer_info;
_hwtimer.ops = &_hwtimer_ops;
return rt_device_hwtimer_register(&_hwtimer, name, (void *)gptimer);
}
INIT_DEVICE_EXPORT(rt_hw_hwtimer_init);
#endif /* RT_USING_HWTIMER */
|