-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ticker_HAL.cpp
331 lines (285 loc) · 10.2 KB
/
Ticker_HAL.cpp
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* Ticker_HAL.cpp
*
* Created on: Dic 2017
* Author: raulMrello
*
*/
#include "Ticker_HAL.h"
//------------------------------------------------------------------------------------
//---- TYPES -------------------------------------------------------------------------
//------------------------------------------------------------------------------------
List<Ticker_HAL::TickerData_t> *Ticker_HAL::_ticker_list = NULL;
Ticker_HAL::TickerData_t* Ticker_HAL::_curr_ticker = NULL;
uint64_t Ticker_HAL::_offset = 0;
//------------------------------------------------------------------------------------
//---- STATIC ------------------------------------------------------------------------
//------------------------------------------------------------------------------------
static const Ticker_HAL::TickerData_t no_more_tickdata={0, NULL, 0, 0};
//------------------------------------------------------------------------------------
void tickerAlarmISR(void *para){
ENTER_ISR();
timer_idx_t timerIdx = (timer_idx_t)(int)para;
if(timerIdx == Ticker_HAL::TimerIdx){
Ticker_HAL::tickerISR();
}
EXIT_ISR();
}
//------------------------------------------------------------------------------------
//---- PUBLIC ------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
void Ticker_HAL::start(){
esp_err_t err = ESP_OK;
// si el timer a�n no se ha iniciado...
if(!_ticker_list){
MBED_ASSERT(!IS_ISR());
// crea lista de Tickers registrados
_ticker_list = new List<Ticker_HAL::TickerData_t>();
// de momento no hay ninguno cargado
_curr_ticker = NULL;
// Inicia el contador
timer_config_t config;
config.divider = TimerDivider;
config.counter_dir = TIMER_COUNT_UP;
config.counter_en = TIMER_PAUSE;
config.alarm_en = TIMER_ALARM_EN;
config.intr_type = TIMER_INTR_LEVEL;
config.auto_reload = TIMER_AUTORELOAD_DIS;
err = timer_init(TimerGroup, TimerIdx, &config);
MBED_ASSERT(err == ESP_OK);
// Carga el valor inicial
err = timer_set_counter_value(TimerGroup, TimerIdx, 0x00000000ULL);
MBED_ASSERT(err == ESP_OK);
// Carga el valor de comparaci�n
err = timer_set_alarm_value(TimerGroup, TimerIdx, UINT64_MAX);
timer_set_alarm(TimerGroup, TimerIdx, TIMER_ALARM_EN);
MBED_ASSERT(err == ESP_OK);
// Habilita interrupci�n
err = timer_enable_intr(TimerGroup, TimerIdx);
// Registra manejador de interrupci�n
MBED_ASSERT(err == ESP_OK);
err = timer_isr_register(TimerGroup, TimerIdx, tickerAlarmISR, (void*)TimerIdx, 0, NULL);
// Arranca el timer
err = timer_start(TimerGroup, TimerIdx);
MBED_ASSERT(err == ESP_OK);
// Arranca el hilo de ejecuci�n propio
_offset = 0;
}
}
//------------------------------------------------------------------------------------
void Ticker_HAL::tickerISR(){
/* Obtiene el estado de la alarma y el valor del contador */
uint32_t intr_status;
switch(TimerGroup){
case TIMER_GROUP_0:{
intr_status = TIMERG0.int_st_timers.val;
TIMERG0.hw_timer[TimerIdx].update = 1;
uint64_t timer_counter_value = ((uint64_t) TIMERG0.hw_timer[TimerIdx].cnt_high) << 32 | TIMERG0.hw_timer[TimerIdx].cnt_low;
/* Clear the interrupt and update the alarm time */
if ((intr_status & BIT(TimerIdx)) && TimerIdx == TIMER_0) {
TIMERG0.int_clr_timers.t0 = 1;
}
else if ((intr_status & BIT(TimerIdx)) && TimerIdx == TIMER_1) {
TIMERG0.int_clr_timers.t1 = 1;
}
// en caso de que no haya asignado ning�n ticker, lo pausa y lo pone a 0 y marca una alarma en el infinito
// por �ltimo lo inicia
if(!_curr_ticker){
_curr_ticker = (TickerData_t*)&no_more_tickdata;
// lo pausa
TIMERG0.hw_timer[TimerIdx].config.enable = 0;
// establece cuenta a 0
TIMERG0.hw_timer[TimerIdx].load_high = (uint32_t)0;
TIMERG0.hw_timer[TimerIdx].load_low = (uint32_t)0;
TIMERG0.hw_timer[TimerIdx].reload = 1;
// set alarm_value
TIMERG0.hw_timer[TimerIdx].alarm_high = (uint32_t) 0xFFFFFFF;
TIMERG0.hw_timer[TimerIdx].alarm_low = (uint32_t) 0xFFFFFFF;
}
// si no hay m�s tickers, marca alarma en infinito
else if(_curr_ticker == (TickerData_t*)&no_more_tickdata){
_curr_ticker = (TickerData_t*)&no_more_tickdata;
// set alarm_value
TIMERG0.hw_timer[TimerIdx].alarm_high = (uint32_t) 0xFFFFFFF;
TIMERG0.hw_timer[TimerIdx].alarm_low = (uint32_t) 0xFFFFFFF;
}
// en otro caso, procesa evento de ticker a nivel de ISR
else{
// Se lanza el elemento en curso (primer item)
TickerData_t* tickdata = _ticker_list->getFirstItem();
if(tickdata){
// Se reinserta el ticker activo
tickdata->next_event = Ticker_HAL::getRawCounter() + tickdata->timeout;
// se arranca el nuevo ticker
executeNext();
// se invoca a la callback
tickdata->func.call();
}
}
// set alarm
TIMERG0.hw_timer[TimerIdx].config.alarm_en = TIMER_ALARM_EN;
// arranca de nuevo
TIMERG0.hw_timer[TimerIdx].config.enable = 1;
return;
}
case TIMER_GROUP_1:{
intr_status = TIMERG1.int_st_timers.val;
TIMERG1.hw_timer[TimerIdx].update = 1;
uint64_t timer_counter_value = ((uint64_t) TIMERG1.hw_timer[TimerIdx].cnt_high) << 32 | TIMERG1.hw_timer[TimerIdx].cnt_low;
/* Clear the interrupt and update the alarm time */
if ((intr_status & BIT(TimerIdx)) && TimerIdx == TIMER_0) {
TIMERG1.int_clr_timers.t0 = 1;
}
else if ((intr_status & BIT(TimerIdx)) && TimerIdx == TIMER_1) {
TIMERG1.int_clr_timers.t1 = 1;
}
// en caso de que no haya asignado ning�n ticker, lo pausa y lo pone a 0 dejando las interrupciones desactivadas
if(!_curr_ticker){
_curr_ticker = (TickerData_t*)&no_more_tickdata;
// lo pausa
TIMERG1.hw_timer[TimerIdx].config.enable = 0;
// establece cuenta a 0
TIMERG1.hw_timer[TimerIdx].load_high = (uint32_t)0;
TIMERG1.hw_timer[TimerIdx].load_low = (uint32_t)0;
TIMERG1.hw_timer[TimerIdx].reload = 1;
// set alarm_value
TIMERG1.hw_timer[TimerIdx].alarm_high = (uint32_t) 0xFFFFFFF;
TIMERG1.hw_timer[TimerIdx].alarm_low = (uint32_t) 0xFFFFFFF;
}
// si no hay m�s tickers, marca alarma en infinito
else if(_curr_ticker == (TickerData_t*)&no_more_tickdata){
_curr_ticker = (TickerData_t*)&no_more_tickdata;
// set alarm_value
TIMERG1.hw_timer[TimerIdx].alarm_high = (uint32_t) 0xFFFFFFF;
TIMERG1.hw_timer[TimerIdx].alarm_low = (uint32_t) 0xFFFFFFF;
}
// en otro caso, procesa evento de ticker a nivel de ISR
else{
// Se lanza el elemento en curso (primer item)
TickerData_t* tickdata = _ticker_list->getFirstItem();
if(tickdata){
// Se reinserta el ticker activo
tickdata->next_event = Ticker_HAL::getRawCounter() + tickdata->timeout;
// se arranca el nuevo ticker
executeNext();
// se invoca a la callback
tickdata->func.call();
}
}
// set alarm
TIMERG1.hw_timer[TimerIdx].config.alarm_en = TIMER_ALARM_EN;
// arranca de nuevo
TIMERG1.hw_timer[TimerIdx].config.enable = 1;
return;
}
}
}
//------------------------------------------------------------------------------------
uint64_t Ticker_HAL::getRawCounter(){
uint64_t now = 0;
esp_err_t err = timer_get_counter_value(TimerGroup, TimerIdx, &now);
MBED_ASSERT(err == ESP_OK);
return now;
}
//------------------------------------------------------------------------------------
uint64_t Ticker_HAL::getTimestamp(){
uint64_t now = 0;
esp_err_t err = timer_get_counter_value(TimerGroup, TimerIdx, &now);
MBED_ASSERT(err == ESP_OK);
uint64_t mulfactor = 1000000;
return (_offset + ((now * mulfactor)/Ticker_HAL::TimerScale));
}
//------------------------------------------------------------------------------------
Ticker_HAL::TickerData_t* Ticker_HAL::attach(Ticker_HAL::TickerData_t* tickdata){
timg_dev_t* tim;
switch(TimerGroup){
case TIMER_GROUP_0:{
tim = &TIMERG0;
break;
}
case TIMER_GROUP_1:{
tim = &TIMERG1;
break;
}
}
// desactiva la alarma
if(!IS_ISR()){
tim->hw_timer[TimerIdx].config.alarm_en = TIMER_ALARM_DIS;
}
_ticker_list->addItem(tickdata);
executeNext();
// set alarm
if(!IS_ISR()){
tim->hw_timer[TimerIdx].config.alarm_en = TIMER_ALARM_EN;
}
return tickdata;
}
//------------------------------------------------------------------------------------
void Ticker_HAL::detach(Ticker_HAL::TickerData_t* tickdata){
// Si es la primera vez que se arranca el ticker, no hace falta desinstalarlo
if(tickdata->timeout == 0){
return;
}
timg_dev_t* tim;
switch(TimerGroup){
case TIMER_GROUP_0:{
tim = &TIMERG0;
break;
}
case TIMER_GROUP_1:{
tim = &TIMERG1;
break;
}
}
// desactiva la alarma
if(!IS_ISR()){
tim->hw_timer[TimerIdx].config.alarm_en = TIMER_ALARM_DIS;
}
_ticker_list->removeItem(tickdata);
executeNext();
// set alarm
if(!IS_ISR()){
tim->hw_timer[TimerIdx].config.alarm_en = TIMER_ALARM_EN;
}
}
//------------------------------------------------------------------------------------
//---- PROTECTED ---------------------------------------------------------------------
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
void Ticker_HAL::executeNext(){
// busca el m�s prioritario
timg_dev_t* tim;
switch(TimerGroup){
case TIMER_GROUP_0:{
tim = &TIMERG0;
break;
}
case TIMER_GROUP_1:{
tim = &TIMERG1;
break;
}
}
// busca el pr�ximo elemento
TickerData_t *tnext = _ticker_list->getFirstItem();
// si no hay m�s elementos
if(!tnext){
_curr_ticker = (TickerData_t *)&no_more_tickdata;
// set alarm_value infinite
tim->hw_timer[TimerIdx].alarm_high = (uint32_t) 0xFFFFFFFF;
tim->hw_timer[TimerIdx].alarm_low = (uint32_t) 0xFFFFFFFF;
return;
}
TickerData_t *tdata = _ticker_list->getNextItem();
while(tdata){
// chequea si es m�s prioritario
if(tdata->next_event < tnext->next_event){
tnext = tdata;
}
tdata = _ticker_list->getNextItem();
}
// set alarm_value
_curr_ticker = tnext;
tim->hw_timer[TimerIdx].alarm_high = (uint32_t) (_curr_ticker->next_event >> 32);
tim->hw_timer[TimerIdx].alarm_low = (uint32_t) _curr_ticker->next_event;
}