@@ -107,29 +107,66 @@ void timerEnd(hw_timer_t timer_handle){
107
107
log_e ("Failed to destroy GPTimer, error num=%d" , err );
108
108
}
109
109
}
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();
114
126
115
127
// some additional logic or handling may be required here to approriately yield or not
116
128
return false;
117
129
}
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 ){
120
131
esp_err_t err = ESP_OK ;
121
132
gptimer_event_callbacks_t cbs = {
122
133
.on_alarm = timerFnWrapper ,
123
134
};
124
135
136
+ interrupt_config_t config = {
137
+ .fn = (voidFuncPtr )fn ;
138
+ .arg = arg ;
139
+ }
140
+
125
141
gptimer_disable (timer );
126
- err = gptimer_register_event_callbacks (timer , & cbs , fn );
142
+ err = gptimer_register_event_callbacks (timer , & cbs , & config );
127
143
if (err != ESP_OK ){
128
144
log_e ("Timer Attach Interrupt failed, error num=%d" , err );
129
145
}
130
146
gptimer_enable (timer );
131
147
}
132
148
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
+
133
170
void timerDetachInterrupt (hw_timer_t timer ){
134
171
esp_err_t err = ESP_OK ;
135
172
err = gptimer_set_alarm_action (timer , NULL );
@@ -140,18 +177,18 @@ void timerDetachInterrupt(hw_timer_t timer){
140
177
141
178
uint64_t timerReadMicros (hw_timer_t timer ){
142
179
uint64_t timer_val = timerRead (timer );
143
- uint32_t frequency = timerGetResolution (timer );
180
+ uint32_t frequency = timerGetFrequency (timer );
144
181
return timer_val * 1000000 / frequency ;
145
182
}
146
183
147
184
uint64_t timerReadMilis (hw_timer_t timer ){
148
185
uint64_t timer_val = timerRead (timer );
149
- uint32_t frequency = timerGetResolution (timer );
186
+ uint32_t frequency = timerGetFrequency (timer );
150
187
return timer_val * 1000 / frequency ;
151
188
}
152
189
153
190
double timerReadSeconds (hw_timer_t timer ){
154
191
uint64_t timer_val = timerRead (timer );
155
- uint32_t frequency = timerGetResolution (timer );
192
+ uint32_t frequency = timerGetFrequency (timer );
156
193
return (double )timer_val / frequency ;
157
194
}
0 commit comments