@@ -107,7 +107,6 @@ static IRAM_ATTR void timerCallback3() {
107
107
}
108
108
109
109
110
-
111
110
/* *
112
111
* @brief Repeating Timer functions for simple scheduling of repeated execution.
113
112
* The basic logic is taken from https://www.toptal.com/embedded/esp32-audio-sampling.
@@ -124,6 +123,11 @@ class TimerAlarmRepeatingDriverESP32 : public TimerAlarmRepeatingDriverBase {
124
123
setTimerFunction (DirectTimerCallback);
125
124
setTimer (0 );
126
125
}
126
+
127
+ TimerAlarmRepeatingDriverESP32 (int timer, TimerFunction function){
128
+ setTimerFunction (function);
129
+ setTimer (timer);
130
+ }
127
131
128
132
void setTimer (int id) override {
129
133
if (id>=0 && id<4 ) {
@@ -156,8 +160,7 @@ class TimerAlarmRepeatingDriverESP32 : public TimerAlarmRepeatingDriverBase {
156
160
break ;
157
161
}
158
162
LOGI (" Timer every: %u us" , timeUs);
159
- uint32_t cpu_freq = getCpuFrequencyMhz (); // 80 ?
160
- adc_timer = timerBegin (0 , cpu_freq, true ); // divider=80 -> 1000000 calls per second
163
+ adc_timer = timerBegin (timer_id, 80 , true ); // divider=80 -> 1000000 calls per second
161
164
162
165
163
166
switch (function) {
@@ -184,6 +187,7 @@ class TimerAlarmRepeatingDriverESP32 : public TimerAlarmRepeatingDriverBase {
184
187
bool end () override {
185
188
TRACED ();
186
189
if (started){
190
+ timerDetachInterrupt (adc_timer);
187
191
timerEnd (adc_timer);
188
192
if (handler_task!=nullptr ){
189
193
vTaskDelete (handler_task);
@@ -209,19 +213,37 @@ class TimerAlarmRepeatingDriverESP32 : public TimerAlarmRepeatingDriverBase {
209
213
int priority = configMAX_PRIORITIES -1 ;
210
214
uint32_t timeUs;
211
215
212
-
213
216
// / direct timer callback
214
217
void setupDirectTimerCallback (repeating_timer_callback_t callback_f){
218
+ TRACED ();
219
+ // We start the timer which executes the callbacks directly
220
+ if (simpleUserCallback==nullptr ){
221
+ simpleUserCallback = new UserCallback[4 ];
222
+ }
223
+ simpleUserCallback[timer_id].setup (callback_f, object, true );
224
+ if (timer_id==0 ) timerAttachInterrupt (adc_timer, userCallback0, false );
225
+ else if (timer_id==1 ) timerAttachInterrupt (adc_timer, userCallback1, false );
226
+ else if (timer_id==2 ) timerAttachInterrupt (adc_timer, userCallback2, false );
227
+ else if (timer_id==3 ) timerAttachInterrupt (adc_timer, userCallback3, false );
228
+
229
+ timerAlarmWrite (adc_timer, timeUs, true );
230
+ // timerSetAutoReload(adc_timer, true);
231
+ timerAlarmEnable (adc_timer);
232
+
233
+ }
234
+
235
+ // / timer callback is notifiying task
236
+ void setupTimerCallbackInThread (repeating_timer_callback_t callback_f){
215
237
TRACED ();
216
238
// we start the timer which runs the callback in a seprate task
217
239
if (timerCallbackArray==nullptr ){
218
240
timerCallbackArray = new TimerCallback[4 ];
219
241
}
220
242
221
- if (timer_id==0 ) timerAttachInterrupt (adc_timer, timerCallback0, true );
222
- else if (timer_id==1 ) timerAttachInterrupt (adc_timer, timerCallback1, true );
223
- else if (timer_id==2 ) timerAttachInterrupt (adc_timer, timerCallback2, true );
224
- else if (timer_id==3 ) timerAttachInterrupt (adc_timer, timerCallback3, true );
243
+ if (timer_id==0 ) timerAttachInterrupt (adc_timer, timerCallback0, false );
244
+ else if (timer_id==1 ) timerAttachInterrupt (adc_timer, timerCallback1, false );
245
+ else if (timer_id==2 ) timerAttachInterrupt (adc_timer, timerCallback2, false );
246
+ else if (timer_id==3 ) timerAttachInterrupt (adc_timer, timerCallback3, false );
225
247
226
248
// we record the callback method and user data
227
249
user_callback.setup (callback_f, object, false );
@@ -235,23 +257,6 @@ class TimerAlarmRepeatingDriverESP32 : public TimerAlarmRepeatingDriverBase {
235
257
timerAlarmEnable (adc_timer);
236
258
}
237
259
238
- // timer callback is notifiying task
239
- void setupTimerCallbackInThread (repeating_timer_callback_t callback_f){
240
- TRACED ();
241
- // We start the timer which executes the callbacks directly
242
- if (simpleUserCallback==nullptr ){
243
- simpleUserCallback = new UserCallback[4 ];
244
- }
245
- simpleUserCallback[timer_id].setup (callback_f, object, true );
246
- if (timer_id==0 ) timerAttachInterrupt (adc_timer, userCallback0, true );
247
- else if (timer_id==1 ) timerAttachInterrupt (adc_timer, userCallback1, true );
248
- else if (timer_id==2 ) timerAttachInterrupt (adc_timer, userCallback2, true );
249
- else if (timer_id==3 ) timerAttachInterrupt (adc_timer, userCallback3, true );
250
-
251
- timerAlarmWrite (adc_timer, timeUs, true );
252
- timerAlarmEnable (adc_timer);
253
-
254
- }
255
260
256
261
// / No timer - just a simple task loop
257
262
void setupSimpleThreadLoop (repeating_timer_callback_t callback_f){
0 commit comments