Skip to content

Commit 3e0ab45

Browse files
committed
Refactored timer object
1 parent b39eb3e commit 3e0ab45

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

Diff for: cores/esp32/esp32-hal-timer.c

+47-10
Original file line numberDiff line numberDiff line change
@@ -107,29 +107,66 @@ void timerEnd(hw_timer_t timer_handle){
107107
log_e("Failed to destroy GPTimer, error num=%d", err);
108108
}
109109
}
110-
111-
bool IRAM_ATTR timerFnWrapper(hw_timer_t timer, const gptimer_alarm_event_data_t *edata, void *arg){
112-
void (*fn)(void) = arg;
113-
fn();
110+
typedef void (*voidFuncPtr)(void);
111+
typedef void (*voidFuncPtrArg)(void*);
112+
typedef struct {
113+
voidFuncPtr fn;
114+
void* arg;
115+
} interrupt_config_t;
116+
117+
bool IRAM_ATTR timerFnWrapper(hw_timer_t timer, const gptimer_alarm_event_data_t *edata, void *config){
118+
interrupt_config_t * isr = (interrupt_config_t*)config;
119+
if(isr->arg){
120+
((voidFuncPtrArg)isr->fn)(isr->arg);
121+
} else {
122+
isr->fn();
123+
}
124+
//void (*fn)(void) = config.fn;
125+
//fn();
114126

115127
// some additional logic or handling may be required here to approriately yield or not
116128
return false;
117129
}
118-
119-
void timerAttachInterrupt(hw_timer_t timer, void (*fn)(void)){
130+
void timerAttachInterruptFunctionalArg(hw_timer_t timer, voidFuncPtrArg fn /*void (*fn)(void*)*/, void * arg){
120131
esp_err_t err = ESP_OK;
121132
gptimer_event_callbacks_t cbs = {
122133
.on_alarm = timerFnWrapper,
123134
};
124135

136+
interrupt_config_t config = {
137+
.fn = (voidFuncPtr)fn;
138+
.arg = arg;
139+
}
140+
125141
gptimer_disable(timer);
126-
err = gptimer_register_event_callbacks(timer, &cbs, fn);
142+
err = gptimer_register_event_callbacks(timer, &cbs, &config);
127143
if (err != ESP_OK){
128144
log_e("Timer Attach Interrupt failed, error num=%d", err);
129145
}
130146
gptimer_enable(timer);
131147
}
132148

149+
void timerAttachInterruptArg(hw_timer_t timer, voidFuncPtrArg fn /*void (*fn)(void)*/, void * arg){
150+
timerAttachInterruptFunctionalArg(timer, fn, arg);
151+
}
152+
153+
void timerAttachInterrupt(hw_timer_t timer, voidFuncPtr fn/*void (*fn)(void)*/){
154+
155+
//esp_err_t err = ESP_OK;
156+
//gptimer_event_callbacks_t cbs = {
157+
// .on_alarm = timerFnWrapper,
158+
//};
159+
160+
// gptimer_disable(timer);
161+
// err = gptimer_register_event_callbacks(timer, &cbs, fn);
162+
// if (err != ESP_OK){
163+
// log_e("Timer Attach Interrupt failed, error num=%d", err);
164+
// }
165+
// gptimer_enable(timer);
166+
167+
timerAttachInterruptFunctionalArg(timer, (voidFuncPtrArg)fn, NULL);
168+
}
169+
133170
void timerDetachInterrupt(hw_timer_t timer){
134171
esp_err_t err = ESP_OK;
135172
err = gptimer_set_alarm_action(timer, NULL);
@@ -140,18 +177,18 @@ void timerDetachInterrupt(hw_timer_t timer){
140177

141178
uint64_t timerReadMicros(hw_timer_t timer){
142179
uint64_t timer_val = timerRead(timer);
143-
uint32_t frequency = timerGetResolution(timer);
180+
uint32_t frequency = timerGetFrequency(timer);
144181
return timer_val * 1000000 / frequency;
145182
}
146183

147184
uint64_t timerReadMilis(hw_timer_t timer){
148185
uint64_t timer_val = timerRead(timer);
149-
uint32_t frequency = timerGetResolution(timer);
186+
uint32_t frequency = timerGetFrequency(timer);
150187
return timer_val * 1000 / frequency;
151188
}
152189

153190
double timerReadSeconds(hw_timer_t timer){
154191
uint64_t timer_val = timerRead(timer);
155-
uint32_t frequency = timerGetResolution(timer);
192+
uint32_t frequency = timerGetFrequency(timer);
156193
return (double)timer_val / frequency;
157194
}

Diff for: cores/esp32/esp32-hal-timer.h

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ hw_timer_t timerBegin(uint32_t frequency);
3535
void timerEnd(hw_timer_t timer);
3636

3737
void timerAttachInterrupt(hw_timer_t timer, void (*fn)(void));
38+
void timerAttachInterruptArg(hw_timer_t timer, void (*fn)(void*), void * arg);
3839
void timerDetachInterrupt(hw_timer_t timer);
3940

4041
void timerStart(hw_timer_t timer);

0 commit comments

Comments
 (0)