- Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathesp32-hal-timer.c
307 lines (271 loc) · 10.8 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// 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"
#include"freertos/FreeRTOS.h"
#include"freertos/xtensa_api.h"
#include"freertos/task.h"
#include"rom/ets_sys.h"
#include"soc/timer_group_struct.h"
#include"soc/dport_reg.h"
#include"esp_attr.h"
#include"esp_intr.h"
#defineHWTIMER_LOCK() portENTER_CRITICAL(timer->lock)
#defineHWTIMER_UNLOCK() portEXIT_CRITICAL(timer->lock)
typedefstruct {
union {
struct {
uint32_treserved0: 10;
uint32_talarm_en: 1; /*When set alarm is enabled*/
uint32_tlevel_int_en: 1; /*When set level type interrupt will be generated during alarm*/
uint32_tedge_int_en: 1; /*When set edge type interrupt will be generated during alarm*/
uint32_tdivider: 16; /*Timer clock (T0/1_clk) pre-scale value.*/
uint32_tautoreload: 1; /*When set timer 0/1 auto-reload at alarming is enabled*/
uint32_tincrease: 1; /*When set timer 0/1 time-base counter increment. When cleared timer 0 time-base counter decrement.*/
uint32_tenable: 1; /*When set timer 0/1 time-base counter is enabled*/
};
uint32_tval;
} config;
uint32_tcnt_low; /*Register to store timer 0/1 time-base counter current value lower 32 bits.*/
uint32_tcnt_high; /*Register to store timer 0 time-base counter current value higher 32 bits.*/
uint32_tupdate; /*Write any value will trigger a timer 0 time-base counter value update (timer 0 current value will be stored in registers above)*/
uint32_talarm_low; /*Timer 0 time-base counter value lower 32 bits that will trigger the alarm*/
uint32_talarm_high; /*Timer 0 time-base counter value higher 32 bits that will trigger the alarm*/
uint32_tload_low; /*Lower 32 bits of the value that will load into timer 0 time-base counter*/
uint32_tload_high; /*higher 32 bits of the value that will load into timer 0 time-base counter*/
uint32_treload; /*Write any value will trigger timer 0 time-base counter reload*/
} hw_timer_reg_t;
typedefstructhw_timer_s {
hw_timer_reg_t*dev;
uint8_tnum;
uint8_tgroup;
uint8_ttimer;
portMUX_TYPElock;
} hw_timer_t;
statichw_timer_thw_timer[4] = {
{(hw_timer_reg_t*)(DR_REG_TIMERGROUP0_BASE),0,0,0,portMUX_INITIALIZER_UNLOCKED},
{(hw_timer_reg_t*)(DR_REG_TIMERGROUP0_BASE+0x0024),1,0,1,portMUX_INITIALIZER_UNLOCKED},
{(hw_timer_reg_t*)(DR_REG_TIMERGROUP0_BASE+0x1000),2,1,0,portMUX_INITIALIZER_UNLOCKED},
{(hw_timer_reg_t*)(DR_REG_TIMERGROUP0_BASE+0x1024),3,1,1,portMUX_INITIALIZER_UNLOCKED}
};
typedefvoid (*voidFuncPtr)(void);
staticvoidFuncPtr__timerInterruptHandlers[4] = {0,0,0,0};
voidIRAM_ATTR__timerISR(void*arg){
uint32_ts0=TIMERG0.int_st_timers.val;
uint32_ts1=TIMERG1.int_st_timers.val;
TIMERG0.int_clr_timers.val=s0;
TIMERG1.int_clr_timers.val=s1;
uint8_tstatus= (s1&3) << 2 | (s0&3);
uint8_ti=4;
//restart the timers that should autoreload
while(i--){
hw_timer_reg_t*dev=hw_timer[i].dev;
if((status& (1 << i)) &&dev->config.autoreload){
dev->config.alarm_en=1;
}
}
i=4;
//call callbacks
while(i--){
if(__timerInterruptHandlers[i] && (status& (1 << i))){
__timerInterruptHandlers[i]();
}
}
}
uint64_ttimerRead(hw_timer_t*timer){
timer->dev->update=1;
uint64_th=timer->dev->cnt_high;
uint64_tl=timer->dev->cnt_low;
return (h << 32) | l;
}
uint64_ttimerAlarmRead(hw_timer_t*timer){
uint64_th=timer->dev->alarm_high;
uint64_tl=timer->dev->alarm_low;
return (h << 32) | l;
}
voidtimerWrite(hw_timer_t*timer, uint64_tval){
timer->dev->load_high= (uint32_t) (val >> 32);
timer->dev->load_low= (uint32_t) (val);
timer->dev->reload=1;
}
voidtimerAlarmWrite(hw_timer_t*timer, uint64_talarm_value, boolautoreload){
timer->dev->alarm_high= (uint32_t) (alarm_value >> 32);
timer->dev->alarm_low= (uint32_t) alarm_value;
timer->dev->config.autoreload=autoreload;
}
voidtimerSetConfig(hw_timer_t*timer, uint32_tconfig){
timer->dev->config.val=config;
}
uint32_ttimerGetConfig(hw_timer_t*timer){
returntimer->dev->config.val;
}
voidtimerSetCountUp(hw_timer_t*timer, boolcountUp){
timer->dev->config.increase=countUp;
}
booltimerGetCountUp(hw_timer_t*timer){
returntimer->dev->config.increase;
}
voidtimerSetAutoReload(hw_timer_t*timer, boolautoreload){
timer->dev->config.autoreload=autoreload;
}
booltimerGetAutoReload(hw_timer_t*timer){
returntimer->dev->config.autoreload;
}
voidtimerSetDivider(hw_timer_t*timer, uint16_tdivider){//2 to 65536
if(!divider){
divider=0xFFFF;
} elseif(divider==1){
divider=2;
}
inttimer_en=timer->dev->config.enable;
timer->dev->config.enable=0;
timer->dev->config.divider=divider;
timer->dev->config.enable=timer_en;
}
uint16_ttimerGetDivider(hw_timer_t*timer){
returntimer->dev->config.divider;
}
voidtimerStart(hw_timer_t*timer){
timer->dev->config.enable=1;
}
voidtimerStop(hw_timer_t*timer){
timer->dev->config.enable=0;
}
voidtimerRestart(hw_timer_t*timer){
timer->dev->config.enable=0;
timer->dev->config.enable=1;
}
booltimerStarted(hw_timer_t*timer){
returntimer->dev->config.enable;
}
voidtimerAlarmEnable(hw_timer_t*timer){
timer->dev->config.alarm_en=1;
}
voidtimerAlarmDisable(hw_timer_t*timer){
timer->dev->config.alarm_en=0;
}
booltimerAlarmEnabled(hw_timer_t*timer){
returntimer->dev->config.alarm_en;
}
staticvoid_on_apb_change(void*arg, apb_change_ev_tev_type, uint32_told_apb, uint32_tnew_apb){
hw_timer_t*timer= (hw_timer_t*)arg;
if(ev_type==APB_BEFORE_CHANGE){
timer->dev->config.enable=0;
} else {
old_apb /= 1000000;
new_apb /= 1000000;
timer->dev->config.divider= (new_apb*timer->dev->config.divider) / old_apb;
timer->dev->config.enable=1;
}
}
hw_timer_t*timerBegin(uint8_tnum, uint16_tdivider, boolcountUp){
if(num>3){
returnNULL;
}
hw_timer_t*timer=&hw_timer[num];
if(timer->group) {
DPORT_SET_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_TIMERGROUP1_CLK_EN);
DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_TIMERGROUP1_RST);
TIMERG1.int_ena.val &= ~BIT(timer->timer);
} else {
DPORT_SET_PERI_REG_MASK(DPORT_PERIP_CLK_EN_REG, DPORT_TIMERGROUP_CLK_EN);
DPORT_CLEAR_PERI_REG_MASK(DPORT_PERIP_RST_EN_REG, DPORT_TIMERGROUP_RST);
TIMERG0.int_ena.val &= ~BIT(timer->timer);
}
timer->dev->config.enable=0;
timerSetDivider(timer, divider);
timerSetCountUp(timer, countUp);
timerSetAutoReload(timer, false);
timerAttachInterrupt(timer, NULL, false);
timerWrite(timer, 0);
timer->dev->config.enable=1;
addApbChangeCallback(timer, _on_apb_change);
returntimer;
}
voidtimerEnd(hw_timer_t*timer){
timer->dev->config.enable=0;
timerAttachInterrupt(timer, NULL, false);
removeApbChangeCallback(timer, _on_apb_change);
}
voidtimerAttachInterrupt(hw_timer_t*timer, void (*fn)(void), booledge){
staticboolinitialized= false;
staticintr_handle_tintr_handle=NULL;
if(intr_handle){
esp_intr_disable(intr_handle);
}
if(fn==NULL){
timer->dev->config.level_int_en=0;
timer->dev->config.edge_int_en=0;
timer->dev->config.alarm_en=0;
if(timer->num&2){
TIMERG1.int_ena.val &= ~BIT(timer->timer);
} else {
TIMERG0.int_ena.val &= ~BIT(timer->timer);
}
__timerInterruptHandlers[timer->num] =NULL;
} else {
__timerInterruptHandlers[timer->num] =fn;
timer->dev->config.level_int_en=edge?0:1;//When set, an alarm will generate a level type interrupt.
timer->dev->config.edge_int_en=edge?1:0;//When set, an alarm will generate an edge type interrupt.
intintr_source=0;
if(!edge){
if(timer->group){
intr_source=ETS_TG1_T0_LEVEL_INTR_SOURCE+timer->timer;
} else {
intr_source=ETS_TG0_T0_LEVEL_INTR_SOURCE+timer->timer;
}
} else {
if(timer->group){
intr_source=ETS_TG1_T0_EDGE_INTR_SOURCE+timer->timer;
} else {
intr_source=ETS_TG0_T0_EDGE_INTR_SOURCE+timer->timer;
}
}
if(!initialized){
initialized= true;
esp_intr_alloc(intr_source, (int)(ESP_INTR_FLAG_IRAM|ESP_INTR_FLAG_LOWMED|ESP_INTR_FLAG_EDGE), __timerISR, NULL, &intr_handle);
} else {
intr_matrix_set(esp_intr_get_cpu(intr_handle), intr_source, esp_intr_get_intno(intr_handle));
}
if(timer->group){
TIMERG1.int_ena.val |= BIT(timer->timer);
} else {
TIMERG0.int_ena.val |= BIT(timer->timer);
}
}
if(intr_handle){
esp_intr_enable(intr_handle);
}
}
voidtimerDetachInterrupt(hw_timer_t*timer){
timerAttachInterrupt(timer, NULL, false);
}
uint64_ttimerReadMicros(hw_timer_t*timer){
uint64_ttimer_val=timerRead(timer);
uint16_tdiv=timerGetDivider(timer);
returntimer_val*div / (getApbFrequency() / 1000000);
}
doubletimerReadSeconds(hw_timer_t*timer){
uint64_ttimer_val=timerRead(timer);
uint16_tdiv=timerGetDivider(timer);
return (double)timer_val*div / getApbFrequency();
}
uint64_ttimerAlarmReadMicros(hw_timer_t*timer){
uint64_ttimer_val=timerAlarmRead(timer);
uint16_tdiv=timerGetDivider(timer);
returntimer_val*div / (getApbFrequency() / 1000000);
}
doubletimerAlarmReadSeconds(hw_timer_t*timer){
uint64_ttimer_val=timerAlarmRead(timer);
uint16_tdiv=timerGetDivider(timer);
return (double)timer_val*div / getApbFrequency();
}