arduino-audio-tools
AudioTimerRenesas.h
1 #pragma once
2 
3 #if defined(ARDUINO_ARCH_RENESAS)
4 #include "AudioTimer/AudioTimerBase.h"
5 #include "FspTimer.h"
6 #include "IRQManager.h"
7 
8 namespace audio_tools {
9 
10 typedef void (*my_repeating_timer_callback_t)(void *obj);
11 
22  public:
24 
28  bool begin(const my_repeating_timer_callback_t callback_f, uint32_t time,
29  TimeUnit unit = MS) override {
30  bool result = false;
31  LOGI("timer time: %u %s", (unsigned int)time, TimeUnitStr[unit]);
32  this->instanceCallback = callback_f;
33 
34  float rate;
35  // we determine the time in microseconds
36  switch (unit) {
37  case MS:
38  rate = AudioTime::toRateMs(time);
39  break;
40  case US:
41  rate = AudioTime::toRateUs(time);
42  break;
43  case HZ:
44  rate = time;
45  break;
46  default:
47  LOGE("Undefined Unit");
48  return false;
49  }
50  if (rate < 550 || rate > 100000) {
51  LOGE("Unsupported rate: %f hz", rate);
52  return false;
53  } else {
54  LOGI("rate is %f hz", rate);
55  }
56 
57  // stop timer if it is active
58  if (timer_active) {
59  end();
60  }
61 
62  LOGI("Using %s", timer_type == 1 ? "AGT" : "GPT")
63  timer_active = timer_type == 1 ? startAGTTimer(rate) : startGPTTimer(rate);
64  return timer_active;
65  }
66 
67  inline static void staticCallback(timer_callback_args_t *ptr) {
69  (TimerAlarmRepeatingDriverRenesas *)ptr->p_context;
70  self->instanceCallback(self->object);
71  }
72 
74  bool end() {
75  TRACED();
76  audio_timer.end();
77  timer_active = false;
78  return true;
79  }
80 
82  void setTimer(int timer) override { timer_type = timer; }
83 
84  protected:
85  FspTimer audio_timer;
86  my_repeating_timer_callback_t instanceCallback = nullptr;
87  uint8_t timer_type = 0; // Should be 0 - but this is currently not working
88  bool timer_active = false;
89 
90  // starts Asynchronous General Purpose Timer (AGT) timer
91  bool startAGTTimer(float rate) {
92  TRACED();
93  uint8_t timer_type = AGT_TIMER;
94  // only channel 1 is available
95  int timer_channel = 1;
96  audio_timer.begin(TIMER_MODE_PERIODIC, timer_type, timer_channel,
97  rate * 2.0, 0.0f, staticCallback, this);
98  IRQManager::getInstance().addPeripheral(IRQ_AGT, audio_timer.get_cfg());
99  audio_timer.open();
100  bool result = audio_timer.start();
101  return result;
102  }
103 
104  // setup General PWM Timer (GPT timer
105  bool startGPTTimer(float rate) {
106  TRACED();
107  uint8_t timer_type = GPT_TIMER;
108  int8_t tindex = FspTimer::get_available_timer(timer_type);
109  if (tindex < 0) {
110  LOGE("Using pwm reserved timer");
111  tindex = FspTimer::get_available_timer(timer_type, true);
112  }
113  if (tindex < 0) {
114  LOGE("no timer");
115  return false;
116  }
117  FspTimer::force_use_of_pwm_reserved_timer();
118  LOGI("timer idx: %d", tindex);
119  if (!audio_timer.begin(TIMER_MODE_PERIODIC, timer_type, tindex, rate, 0.0f,
120  staticCallback, this)) {
121  LOGE("error:begin");
122  return false;
123  }
124 
125  if (!audio_timer.setup_overflow_irq()) {
126  LOGE("error:setup_overflow_irq");
127  return false;
128  }
129 
130  if (!audio_timer.open()) {
131  LOGE("error:open");
132  return false;
133  }
134  if (!audio_timer.start()) {
135  LOGE("error:start");
136  return false;
137  }
138  return true;
139  }
140 };
141 
143 using TimerAlarmRepeatingDriver = TimerAlarmRepeatingDriverRenesas;
144 
145 } // namespace audio_tools
146 
147 #endif
Definition: AudioTimerBase.h:22
Repeating Timer functions for repeated execution: Plaease use the typedef TimerAlarmRepeating....
Definition: AudioTimerRenesas.h:21
void setTimer(int timer) override
Selects the timer type: 0=GPT and 1=AGT.
Definition: AudioTimerRenesas.h:82
bool begin(const my_repeating_timer_callback_t callback_f, uint32_t time, TimeUnit unit=MS) override
Definition: AudioTimerRenesas.h:28
bool end()
ends the timer and if necessary the task
Definition: AudioTimerRenesas.h:74
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