@@ -28,6 +28,7 @@ typedef struct {
2828struct timer_struct_t {
2929 gptimer_handle_t timer_handle ;
3030 interrupt_config_t interrupt_handle ;
31+ bool timer_started ;
3132};
3233
3334inline uint64_t timerRead (hw_timer_t * timer ){
@@ -62,10 +63,12 @@ uint32_t timerGetFrequency(hw_timer_t * timer){
6263
6364void timerStart (hw_timer_t * timer ){
6465 gptimer_start (timer -> timer_handle );
66+ timer -> timer_started = true;
6567}
6668
6769void timerStop (hw_timer_t * timer ){
6870 gptimer_stop (timer -> timer_handle );
71+ timer -> timer_started = false;
6972}
7073
7174void 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
117121void 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
159172void timerAttachInterruptArg (hw_timer_t * timer , void (* userFunc )(void * ), void * arg ){
160173 timerAttachInterruptFunctionalArg (timer , userFunc , arg );
0 commit comments