arduino-audio-tools
AudioTimerSTM32.h
1 #pragma once
2 
3 #if defined(STM32)
4 #include "AudioTimer/AudioTimerBase.h"
5 
6 namespace audio_tools {
7 
8 class TimerAlarmRepeatingDriverSTM32;
9 static TimerAlarmRepeatingDriverSTM32 *timerAlarmRepeating = nullptr;
10 typedef void (*repeating_timer_callback_t)(void *obj);
11 
20  public:
22 
24  end();
25  delete this->timer;
26  }
28  void setTimer(int timerIdx) override {
29  if (this->timer != nullptr) {
30  delete this->timer;
31  }
32  this->timer = new HardwareTimer(timers[timerIdx]);
33  timer_index = timerIdx;
34  timer->pause();
35  }
36 
40  bool begin(repeating_timer_callback_t callback_f, uint32_t time,
41  TimeUnit unit = MS) override {
42  TRACEI();
43  LOGI("Using timer TIM%d", timer_index + 1);
44  timer->attachInterrupt(std::bind(callback_f, object));
45 
46  // we determine the time in microseconds
47  switch (unit) {
48  case MS:
49  timer->setOverflow(time * 1000, MICROSEC_FORMAT); // 10 Hz
50  break;
51  case US:
52  timer->setOverflow(time, MICROSEC_FORMAT); // 10 Hz
53  break;
54  }
55  timer->resume();
56  return true;
57  }
58 
59  // ends the timer and if necessary the task
60  bool end() override {
61  TRACEI();
62  timer->pause();
63  return true;
64  }
65 
66  protected:
67  HardwareTimer *timer = nullptr;
68  int timer_index;
69  TIM_TypeDef *timers[6] = {TIM1, TIM2, TIM3, TIM4, TIM5};
70 };
71 
73 using TimerAlarmRepeatingDriver = TimerAlarmRepeatingDriverSTM32;
74 
75 } // namespace audio_tools
76 
77 #endif
Definition: AudioTimerBase.h:22
STM32 Repeating Timer functions for repeated execution: Plaease use the typedef TimerAlarmRepeating.
Definition: AudioTimerSTM32.h:19
void setTimer(int timerIdx) override
selects the timer: 0 = TIM1, 1 = TIM2,2 = TIM3, 3 = TIM4, 4 = TIM5
Definition: AudioTimerSTM32.h:28
bool begin(repeating_timer_callback_t callback_f, uint32_t time, TimeUnit unit=MS) override
Definition: AudioTimerSTM32.h:40
TimeUnit
Time Units.
Definition: AudioTypes.h:43
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AnalogAudio.h:10
TimerAlarmRepeatingDriverAVR TimerAlarmRepeatingDriver
use TimerAlarmRepeating!
Definition: AudioTimerAVR.h:94