arduino-audio-tools
AudioTimerSTM32.h
1 #pragma once
2 
3 #if defined(STM32)
4 #include "AudioTools/CoreAudio/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 
21  public:
23 
25  end();
26  delete this->timer;
27  }
29  void setTimer(int timerIdx) override {
30  setTimer(timers[timerIdx]);
31  timer_index = timerIdx;
32  }
33 
35  void setTimer(TIM_TypeDef *timerDef) {
36  if (this->timer != nullptr) {
37  delete this->timer;
38  }
39  this->timer = new HardwareTimer(timerDef);
40  timer_index = -1;
41  timer->pause();
42  }
43 
44 
48  bool begin(repeating_timer_callback_t callback_f, uint32_t time,
49  TimeUnit unit = MS) override {
50  TRACEI();
51  if (timer_index>=0) LOGI("Using timer TIM%d", timer_index + 1);
52  timer->attachInterrupt(std::bind(callback_f, object));
53 
54  // we determine the time in microseconds
55  switch (unit) {
56  case MS:
57  timer->setOverflow(time * 1000, MICROSEC_FORMAT); // 10 Hz
58  break;
59  case US:
60  timer->setOverflow(time, MICROSEC_FORMAT); // 10 Hz
61  break;
62  case HZ:
63  // convert hz to time in us
64  uint64_t time_us = AudioTime::toTimeUs(time);
65  timer->setOverflow(time_us, MICROSEC_FORMAT); // 10 Hz
66  break;
67  }
68  timer->resume();
69  return true;
70  }
71 
72  // ends the timer and if necessary the task
73  bool end() override {
74  TRACEI();
75  timer->pause();
76  return true;
77  }
78 
79  protected:
80  HardwareTimer *timer = nullptr;
81  int timer_index;
82  TIM_TypeDef *timers[6] = {TIM1, TIM2, TIM3, TIM4, TIM5};
83 };
84 
86 using TimerAlarmRepeatingDriver = TimerAlarmRepeatingDriverSTM32;
87 
88 } // namespace audio_tools
89 
90 #endif
static uint32_t toTimeUs(uint32_t samplingRate, uint8_t limit=10)
converts sampling rate to delay in microseconds (μs)
Definition: AudioTypes.h:256
Definition: AudioTimerBase.h:22
STM32 Repeating Timer functions for repeated execution: Please use the typedef TimerAlarmRepeating....
Definition: AudioTimerSTM32.h:20
void setTimer(int timerIdx) override
selects the timer: 0 = TIM1, 1 = TIM2,2 = TIM3, 3 = TIM4, 4 = TIM5
Definition: AudioTimerSTM32.h:29
void setTimer(TIM_TypeDef *timerDef)
select the timer
Definition: AudioTimerSTM32.h:35
bool begin(repeating_timer_callback_t callback_f, uint32_t time, TimeUnit unit=MS) override
Definition: AudioTimerSTM32.h:48
TimeUnit
Time Units.
Definition: AudioTypes.h:45
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823
TimerAlarmRepeatingDriverAVR TimerAlarmRepeatingDriver
use TimerAlarmRepeating!
Definition: AudioTimerAVR.h:94