Skip to content

Commit 8eb2e00

Browse files
committed
Add timer_started flag and stop before disable
1 parent cf75047 commit 8eb2e00

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

cores/esp32/esp32-hal-timer.c

+14-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ typedef struct {
2828
struct timer_struct_t {
2929
gptimer_handle_t timer_handle;
3030
interrupt_config_t interrupt_handle;
31+
bool timer_started;
3132
};
3233

3334
inline uint64_t timerRead(hw_timer_t * timer){
@@ -62,10 +63,12 @@ uint32_t timerGetFrequency(hw_timer_t * timer){
6263

6364
void timerStart(hw_timer_t * timer){
6465
gptimer_start(timer->timer_handle);
66+
timer->timer_started = true;
6567
}
6668

6769
void timerStop(hw_timer_t * timer){
6870
gptimer_stop(timer->timer_handle);
71+
timer->timer_started = false;
6972
}
7073

7174
void timerRestart(hw_timer_t * timer){
@@ -111,11 +114,15 @@ hw_timer_t * timerBegin(uint32_t frequency){
111114
}
112115
gptimer_enable(timer->timer_handle);
113116
gptimer_start(timer->timer_handle);
117+
timer->timer_started = true;
114118
return timer;
115119
}
116120

117121
void timerEnd(hw_timer_t * timer){
118122
esp_err_t err = ESP_OK;
123+
if(timer->timer_started == true){
124+
gptimer_stop(timer->timer_handle);
125+
}
119126
gptimer_disable(timer->timer_handle);
120127
err = gptimer_del_timer(timer->timer_handle);
121128
if (err != ESP_OK){
@@ -147,14 +154,20 @@ void timerAttachInterruptFunctionalArg(hw_timer_t * timer, void (*userFunc)(void
147154
timer->interrupt_handle.fn = (voidFuncPtr)userFunc;
148155
timer->interrupt_handle.arg = arg;
149156

157+
if(timer->timer_started == true){
158+
gptimer_stop(timer->timer_handle);
159+
}
150160
gptimer_disable(timer->timer_handle);
151161
err = gptimer_register_event_callbacks(timer->timer_handle, &cbs, &timer->interrupt_handle);
152162
if (err != ESP_OK){
153163
log_e("Timer Attach Interrupt failed, error num=%d", err);
154164
}
155165
gptimer_enable(timer->timer_handle);
156-
}
166+
if(timer->timer_started == true){
167+
gptimer_start(timer->timer_handle);
168+
}
157169

170+
}
158171

159172
void timerAttachInterruptArg(hw_timer_t * timer, void (*userFunc)(void*), void * arg){
160173
timerAttachInterruptFunctionalArg(timer, userFunc, arg);

0 commit comments

Comments
 (0)