forked from espressif/arduino-esp32
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32-hal-timer.c
265 lines (234 loc) · 6.97 KB
/
esp32-hal-timer.c
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include"esp32-hal-timer.h"
#ifSOC_GPTIMER_SUPPORTED
#include"driver/gptimer.h"
#if defined __has_include&&__has_include("clk_tree.h")
#include"clk_tree.h"
#else
#include"esp_clk_tree.h"
#endif
typedefvoid (*voidFuncPtr)(void);
typedefvoid (*voidFuncPtrArg)(void*);
typedefstruct {
voidFuncPtrfn;
void*arg;
} interrupt_config_t;
structtimer_struct_t {
gptimer_handle_ttimer_handle;
interrupt_config_tinterrupt_handle;
booltimer_started;
};
inlineuint64_ttimerRead(hw_timer_t*timer) {
if (timer==NULL) {
log_e("Timer handle is NULL");
return0;
}
uint64_tvalue;
gptimer_get_raw_count(timer->timer_handle, &value);
returnvalue;
}
voidtimerWrite(hw_timer_t*timer, uint64_tval) {
if (timer==NULL) {
log_e("Timer handle is NULL");
return;
}
gptimer_set_raw_count(timer->timer_handle, val);
}
voidtimerAlarm(hw_timer_t*timer, uint64_talarm_value, boolautoreload, uint64_treload_count) {
if (timer==NULL) {
log_e("Timer handle is NULL");
return;
}
esp_err_terr=ESP_OK;
gptimer_alarm_config_talarm_cfg= {
.alarm_count=alarm_value,
.reload_count=reload_count,
.flags.auto_reload_on_alarm=autoreload,
};
err=gptimer_set_alarm_action(timer->timer_handle, &alarm_cfg);
if (err!=ESP_OK) {
log_e("Timer Alarm Write failed, error num=%d", err);
}
}
uint32_ttimerGetFrequency(hw_timer_t*timer) {
if (timer==NULL) {
return0;
}
uint32_tfrequency;
gptimer_get_resolution(timer->timer_handle, &frequency);
returnfrequency;
}
voidtimerStart(hw_timer_t*timer) {
if (timer==NULL) {
log_e("Timer handle is NULL");
return;
}
gptimer_start(timer->timer_handle);
timer->timer_started= true;
}
voidtimerStop(hw_timer_t*timer) {
if (timer==NULL) {
log_e("Timer handle is NULL");
return;
}
gptimer_stop(timer->timer_handle);
timer->timer_started= false;
}
voidtimerRestart(hw_timer_t*timer) {
if (timer==NULL) {
log_e("Timer handle is NULL");
return;
}
gptimer_set_raw_count(timer->timer_handle, 0);
}
hw_timer_t*timerBegin(uint32_tfrequency) {
esp_err_terr=ESP_OK;
uint32_tcounter_src_hz=0;
uint32_tdivider=0;
soc_periph_gptimer_clk_src_tclk;
soc_periph_gptimer_clk_src_tgptimer_clks[] =SOC_GPTIMER_CLKS;
for (size_ti=0; i<sizeof(gptimer_clks) / sizeof(gptimer_clks[0]); i++) {
clk=gptimer_clks[i];
#if defined __has_include&&__has_include("clk_tree.h")
clk_tree_src_get_freq_hz(clk, CLK_TREE_SRC_FREQ_PRECISION_CACHED, &counter_src_hz);
#else
esp_clk_tree_src_get_freq_hz(clk, ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED, &counter_src_hz);
#endif
divider=counter_src_hz / frequency;
if ((divider >= 2) && (divider <= 65536)) {
break;
} else {
divider=0;
}
}
if (divider==0) {
log_e("Resolution cannot be reached with any clock source, aborting!");
returnNULL;
}
gptimer_config_tconfig= {
.clk_src=clk,
.direction=GPTIMER_COUNT_UP,
.resolution_hz=frequency,
.flags.intr_shared= true,
};
hw_timer_t*timer=malloc(sizeof(hw_timer_t));
err=gptimer_new_timer(&config, &timer->timer_handle);
if (err!=ESP_OK) {
log_e("Failed to create a new GPTimer, error num=%d", err);
free(timer);
returnNULL;
}
gptimer_enable(timer->timer_handle);
gptimer_start(timer->timer_handle);
timer->timer_started= true;
returntimer;
}
voidtimerEnd(hw_timer_t*timer) {
if (timer!=NULL) {
esp_err_terr=ESP_OK;
if (timer->timer_started== true) {
gptimer_stop(timer->timer_handle);
}
gptimer_disable(timer->timer_handle);
err=gptimer_del_timer(timer->timer_handle);
if (err!=ESP_OK) {
log_e("Failed to destroy GPTimer, error num=%d", err);
return;
}
free(timer);
}
}
boolIRAM_ATTRtimerFnWrapper(gptimer_handle_ttimer, constgptimer_alarm_event_data_t*edata, void*args) {
interrupt_config_t*isr= (interrupt_config_t*)args;
if (isr->fn) {
if (isr->arg) {
((voidFuncPtrArg)isr->fn)(isr->arg);
} else {
isr->fn();
}
}
// some additional logic or handling may be required here to appropriately yield or not
return false;
}
voidtimerAttachInterruptFunctionalArg(hw_timer_t*timer, void (*userFunc)(void*), void*arg) {
if (timer==NULL) {
log_e("Timer handle is NULL");
return;
}
esp_err_terr=ESP_OK;
gptimer_event_callbacks_tcbs= {
.on_alarm=timerFnWrapper,
};
timer->interrupt_handle.fn= (voidFuncPtr)userFunc;
timer->interrupt_handle.arg=arg;
if (timer->timer_started== true) {
gptimer_stop(timer->timer_handle);
}
gptimer_disable(timer->timer_handle);
err=gptimer_register_event_callbacks(timer->timer_handle, &cbs, &timer->interrupt_handle);
if (err!=ESP_OK) {
log_e("Timer Attach Interrupt failed, error num=%d", err);
}
gptimer_enable(timer->timer_handle);
if (timer->timer_started== true) {
gptimer_start(timer->timer_handle);
}
}
voidtimerAttachInterruptArg(hw_timer_t*timer, void (*userFunc)(void*), void*arg) {
timerAttachInterruptFunctionalArg(timer, userFunc, arg);
}
voidtimerAttachInterrupt(hw_timer_t*timer, voidFuncPtruserFunc) {
timerAttachInterruptFunctionalArg(timer, (voidFuncPtrArg)userFunc, NULL);
}
voidtimerDetachInterrupt(hw_timer_t*timer) {
if (timer==NULL) {
log_e("Timer handle is NULL");
return;
}
esp_err_terr=ESP_OK;
err=gptimer_set_alarm_action(timer->timer_handle, NULL);
timer->interrupt_handle.fn=NULL;
timer->interrupt_handle.arg=NULL;
if (err!=ESP_OK) {
log_e("Timer Detach Interrupt failed, error num=%d", err);
}
}
uint64_ttimerReadMicros(hw_timer_t*timer) {
if (timer==NULL) {
log_e("Timer handle is NULL");
return0;
}
uint64_ttimer_val=timerRead(timer);
uint32_tfrequency=timerGetFrequency(timer);
returntimer_val*1000000 / frequency;
}
uint64_ttimerReadMillis(hw_timer_t*timer) {
if (timer==NULL) {
log_e("Timer handle is NULL");
return0;
}
uint64_ttimer_val=timerRead(timer);
uint32_tfrequency=timerGetFrequency(timer);
returntimer_val*1000 / frequency;
}
doubletimerReadSeconds(hw_timer_t*timer) {
if (timer==NULL) {
log_e("Timer handle is NULL");
return0;
}
uint64_ttimer_val=timerRead(timer);
uint32_tfrequency=timerGetFrequency(timer);
return (double)timer_val / frequency;
}
#endif/* SOC_GPTIMER_SUPPORTED */