@@ -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+
133170void 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
141178uint64_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
147184uint64_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
153190double 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}
0 commit comments