Skip to content

Commit eaaa51f

Browse files
committed
Refactored timer object
1 parent b39eb3e commit eaaa51f

File tree

2 files changed

+111
-57
lines changed

2 files changed

+111
-57
lines changed

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

+93-39
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,77 @@
1616
#include "driver/gptimer.h"
1717
#include "soc/soc_caps.h"
1818
#include "clk_tree.h"
19-
20-
inline uint64_t timerRead(hw_timer_t timer_handle){
19+
#include "freertos/FreeRTOS.h"
20+
#include "freertos/task.h"
21+
#include "freertos/semphr.h"
22+
23+
typedef void (*voidFuncPtr)(void);
24+
typedef void (*voidFuncPtrArg)(void*);
25+
26+
// #if CONFIG_DISABLE_HAL_LOCKS
27+
// #define TIMER_MUTEX_LOCK()
28+
// #define TIMER_MUTEX_UNLOCK()
29+
// #else
30+
// #define TIMER_MUTEX_LOCK() do {} while (xSemaphoreTake(timer->lock, portMAX_DELAY) != pdPASS)
31+
// #define TIMER_MUTEX_UNLOCK() xSemaphoreGive(timer->lock)
32+
// #endif
33+
34+
typedef struct {
35+
voidFuncPtr fn;
36+
void* arg;
37+
} interrupt_config_t;
38+
39+
struct timer_struct_t {
40+
gptimer_handle_t timer_handle;
41+
interrupt_config_t interrupt_handle;
42+
// #if !CONFIG_DISABLE_HAL_LOCKS
43+
// xSemaphoreHandle lock;
44+
// #endif
45+
};
46+
47+
inline uint64_t timerRead(hw_timer_t * timer){
2148

2249
uint64_t value;
23-
gptimer_get_raw_count(timer_handle, &value);
50+
gptimer_get_raw_count(timer->timer_handle, &value);
2451
return value;
2552
}
26-
void timerWrite(hw_timer_t timer_handle, uint64_t val){
27-
gptimer_set_raw_count(timer_handle, val);
53+
54+
void timerWrite(hw_timer_t * timer, uint64_t val){
55+
gptimer_set_raw_count(timer->timer_handle, val);
2856
}
2957

30-
void timerAlarm(hw_timer_t timer, uint64_t alarm_value, bool autoreload, uint64_t reload_count){
58+
void timerAlarm(hw_timer_t * timer, uint64_t alarm_value, bool autoreload, uint64_t reload_count){
3159
esp_err_t err = ESP_OK;
3260
gptimer_alarm_config_t alarm_cfg = {
3361
.alarm_count = alarm_value,
3462
.reload_count = reload_count,
3563
.flags.auto_reload_on_alarm = autoreload,
3664
};
37-
err = gptimer_set_alarm_action(timer, &alarm_cfg);
65+
err = gptimer_set_alarm_action(timer->timer_handle, &alarm_cfg);
3866
if (err != ESP_OK){
3967
log_e("Timer Alarm Write failed, error num=%d", err);
4068
}
4169
}
4270

43-
uint32_t timerGetFrequency(hw_timer_t timer_handle){
71+
uint32_t timerGetFrequency(hw_timer_t * timer){
4472
uint32_t frequency;
45-
gptimer_get_resolution(timer_handle, &frequency);
73+
gptimer_get_resolution(timer->timer_handle, &frequency);
4674
return frequency;
4775
}
4876

49-
void timerStart(hw_timer_t timer_handle){
50-
gptimer_start(timer_handle);
77+
void timerStart(hw_timer_t * timer){
78+
gptimer_start(timer->timer_handle);
5179
}
5280

53-
void timerStop(hw_timer_t timer_handle){
54-
gptimer_stop(timer_handle);
81+
void timerStop(hw_timer_t * timer){
82+
gptimer_stop(timer->timer_handle);
5583
}
5684

57-
void timerRestart(hw_timer_t timer_handle){
58-
gptimer_set_raw_count(timer_handle,0);
85+
void timerRestart(hw_timer_t * timer){
86+
gptimer_set_raw_count(timer->timer_handle,0);
5987
}
6088

61-
hw_timer_t timerBegin(uint32_t frequency){
89+
hw_timer_t * timerBegin(uint32_t frequency){
6290

6391
esp_err_t err = ESP_OK;
6492
hw_timer_t timer_handle;
@@ -89,69 +117,95 @@ hw_timer_t timerBegin(uint32_t frequency){
89117
.flags.intr_shared = true,
90118
};
91119

92-
err = gptimer_new_timer(&config, &timer_handle);
120+
hw_timer_t *timer = malloc(sizeof(hw_timer_t));
121+
122+
err = gptimer_new_timer(&config, &timer->timer_handle);
93123
if (err != ESP_OK){
94124
log_e("Failed to create a new GPTimer, error num=%d", err);
125+
free(timer);
95126
return NULL;
96127
}
97-
gptimer_enable(timer_handle);
98-
gptimer_start(timer_handle);
99-
return timer_handle;
128+
gptimer_enable(timer->timer_handle);
129+
gptimer_start(timer->timer_handle);
130+
return timer;
100131
}
101132

102-
void timerEnd(hw_timer_t timer_handle){
133+
void timerEnd(hw_timer_t * timer){
103134
esp_err_t err = ESP_OK;
104-
gptimer_disable(timer_handle);
105-
err = gptimer_del_timer(timer_handle);
135+
gptimer_disable(timer->timer_handle);
136+
err = gptimer_del_timer(timer->timer_handle);
106137
if (err != ESP_OK){
107138
log_e("Failed to destroy GPTimer, error num=%d", err);
108-
}
139+
return;
140+
}
141+
free(timer);
109142
}
110143

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();
144+
bool IRAM_ATTR timerFnWrapper(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void * args){
145+
interrupt_config_t * isr = (interrupt_config_t*)args;
146+
if(isr->fn) {
147+
if(isr->arg){
148+
((voidFuncPtrArg)isr->fn)(isr->arg);
149+
} else {
150+
isr->fn();
151+
}
152+
}
114153

115154
// some additional logic or handling may be required here to approriately yield or not
116155
return false;
117156
}
118157

119-
void timerAttachInterrupt(hw_timer_t timer, void (*fn)(void)){
158+
159+
void timerAttachInterruptFunctionalArg(hw_timer_t * timer, void (*userFunc)(void*), void * arg){
120160
esp_err_t err = ESP_OK;
121161
gptimer_event_callbacks_t cbs = {
122162
.on_alarm = timerFnWrapper,
123163
};
124164

125-
gptimer_disable(timer);
126-
err = gptimer_register_event_callbacks(timer, &cbs, fn);
165+
timer->interrupt_handle.fn = (voidFuncPtr)userFunc;
166+
timer->interrupt_handle.arg = arg;
167+
168+
gptimer_disable(timer->timer_handle);
169+
err = gptimer_register_event_callbacks(timer->timer_handle, &cbs, &timer->interrupt_handle);
127170
if (err != ESP_OK){
128171
log_e("Timer Attach Interrupt failed, error num=%d", err);
129172
}
130-
gptimer_enable(timer);
173+
gptimer_enable(timer->timer_handle);
174+
}
175+
176+
177+
void timerAttachInterruptArg(hw_timer_t * timer, void (*userFunc)(void*), void * arg){
178+
timerAttachInterruptFunctionalArg(timer, userFunc, arg);
179+
}
180+
181+
void timerAttachInterrupt(hw_timer_t * timer, voidFuncPtr userFunc){
182+
timerAttachInterruptFunctionalArg(timer, (voidFuncPtrArg)userFunc, NULL);
131183
}
132184

133-
void timerDetachInterrupt(hw_timer_t timer){
185+
void timerDetachInterrupt(hw_timer_t * timer){
134186
esp_err_t err = ESP_OK;
135-
err = gptimer_set_alarm_action(timer, NULL);
187+
err = gptimer_set_alarm_action(timer->timer_handle, NULL);
188+
timer->interrupt_handle.fn = NULL;
189+
timer->interrupt_handle.arg = NULL;
136190
if (err != ESP_OK){
137191
log_e("Timer Detach Interrupt failed, error num=%d", err);
138192
}
139193
}
140194

141-
uint64_t timerReadMicros(hw_timer_t timer){
195+
uint64_t timerReadMicros(hw_timer_t * timer){
142196
uint64_t timer_val = timerRead(timer);
143-
uint32_t frequency = timerGetResolution(timer);
197+
uint32_t frequency = timerGetFrequency(timer);
144198
return timer_val * 1000000 / frequency;
145199
}
146200

147-
uint64_t timerReadMilis(hw_timer_t timer){
201+
uint64_t timerReadMilis(hw_timer_t * timer){
148202
uint64_t timer_val = timerRead(timer);
149-
uint32_t frequency = timerGetResolution(timer);
203+
uint32_t frequency = timerGetFrequency(timer);
150204
return timer_val * 1000 / frequency;
151205
}
152206

153-
double timerReadSeconds(hw_timer_t timer){
207+
double timerReadSeconds(hw_timer_t * timer){
154208
uint64_t timer_val = timerRead(timer);
155-
uint32_t frequency = timerGetResolution(timer);
209+
uint32_t frequency = timerGetFrequency(timer);
156210
return (double)timer_val / frequency;
157211
}

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

+18-18
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,31 @@
2626
extern "C" {
2727
#endif
2828

29-
/**
30-
* @brief Keep previous compatibility with 2.X
31-
*/
32-
typedef gptimer_handle_t hw_timer_t;
29+
struct timer_struct_t;
30+
typedef struct timer_struct_t hw_timer_t;
31+
32+
hw_timer_t * timerBegin(uint32_t frequency);
33+
void timerEnd(hw_timer_t * timer);
3334

34-
hw_timer_t timerBegin(uint32_t frequency);
35-
void timerEnd(hw_timer_t timer);
35+
void timerAttachInterrupt(hw_timer_t * timer, void (*userFunc)(void));
36+
void timerAttachInterruptArg(hw_timer_t * timer, void (*userFunc)(void*), void * arg);
3637

37-
void timerAttachInterrupt(hw_timer_t timer, void (*fn)(void));
38-
void timerDetachInterrupt(hw_timer_t timer);
38+
void timerDetachInterrupt(hw_timer_t * timer);
3939

40-
void timerStart(hw_timer_t timer);
41-
void timerStop(hw_timer_t timer);
42-
void timerRestart(hw_timer_t timer);
43-
void timerWrite(hw_timer_t timer, uint64_t val);
40+
void timerStart(hw_timer_t * timer);
41+
void timerStop(hw_timer_t * timer);
42+
void timerRestart(hw_timer_t * timer);
43+
void timerWrite(hw_timer_t * timer, uint64_t val);
4444

45-
uint64_t timerRead(hw_timer_t timer);
45+
uint64_t timerRead(hw_timer_t * timer);
4646

47-
uint64_t timerReadMicros(hw_timer_t timer);
48-
uint64_t timerReadMilis(hw_timer_t timer);
49-
double timerReadSeconds(hw_timer_t timer);
47+
uint64_t timerReadMicros(hw_timer_t * timer);
48+
uint64_t timerReadMilis(hw_timer_t * timer);
49+
double timerReadSeconds(hw_timer_t * timer);
5050

51-
uint32_t timerGetFrequency(hw_timer_t timer);
51+
uint32_t timerGetFrequency(hw_timer_t * timer);
5252

53-
void timerAlarm(hw_timer_t timer, uint64_t alarm_value, bool autoreload, uint64_t reload_count);
53+
void timerAlarm(hw_timer_t * timer, uint64_t alarm_value, bool autoreload, uint64_t reload_count);
5454

5555
#ifdef __cplusplus
5656
}

0 commit comments

Comments
 (0)