arduino-audio-tools
Loading...
Searching...
No Matches
AudioTimerTangNano.h
Go to the documentation of this file.
1#pragma once
2
3#if defined(ARDUINO_ARCH_TANGNANO20K)
5#include "tangnano20k_soc.h"
6#include "tangnano20k_timer.h"
7
8namespace audio_tools {
9
19class AudioTimerDriverTangNano : public AudioTimerDriverBase {
20 public:
21 AudioTimerDriverTangNano() = default;
22
23 bool begin(repeating_timer_callback_t callback_f, uint32_t time,
24 TimeUnit unit = MS) override {
25 LOGI("timer time: %u %s", (unsigned int)time, toString(unit));
26 if (time == 0) {
27 LOGE("Invalid time: 0");
28 return false;
29 }
30 instanceCallback = callback_f;
31
32 uint32_t ticks = 0;
33 switch (unit) {
34 case MS:
35 ticks = (uint64_t)time * TANGNANO20K_CLK_FREQ / 1000ULL;
36 break;
37 case US:
38 ticks = (uint64_t)time * TANGNANO20K_CLK_FREQ / 1000000ULL;
39 break;
40 case HZ:
41 ticks = TANGNANO20K_CLK_FREQ / time;
42 break;
43 default:
44 LOGE("Undefined Unit");
45 return false;
46 }
47 if (ticks == 0) ticks = 1;
48
49 if (handle < 0) {
50 handle = tangnano20k_sw_timer_alloc();
51 if (handle < 0 || handle >= TANGNANO20K_SW_TIMER_COUNT) {
52 LOGE("No free timer available");
53 handle = -1;
54 return false;
55 }
56 }
57 self()[handle] = this;
58 if (!tangnano20k_sw_timer_start(handle, trampolines()[handle], ticks,
59 true)) {
60 LOGE("Could not start timer");
61 end();
62 return false;
63 }
64 return true;
65 }
66
67 bool end() override {
68 if (handle < 0) return false;
69 tangnano20k_sw_timer_release(handle);
70 self()[handle] = nullptr;
71 handle = -1;
72 return true;
73 }
74
75 protected:
76 int handle = -1;
77 repeating_timer_callback_t instanceCallback = nullptr;
78 // the core callback has no parameter: so we map the timer slot to the
79 // driver instance
80 static AudioTimerDriverTangNano **self() {
81 static AudioTimerDriverTangNano *result[TANGNANO20K_SW_TIMER_COUNT] = {};
82 return result;
83 }
84
85 template <int N>
86 static void trampoline() {
87 AudioTimerDriverTangNano *p = self()[N];
88 if (p != nullptr && p->instanceCallback != nullptr)
89 p->instanceCallback(p->object);
90 }
91
92 static_assert(TANGNANO20K_SW_TIMER_COUNT == 6,
93 "trampolines() must match TANGNANO20K_SW_TIMER_COUNT");
94
95 static void (**trampolines())(void) {
96 static void (*result[TANGNANO20K_SW_TIMER_COUNT])(void) = {
97 trampoline<0>, trampoline<1>, trampoline<2>,
98 trampoline<3>, trampoline<4>, trampoline<5>};
99 return result;
100 }
101};
102
104using AudioTimerDriver = AudioTimerDriverTangNano;
105
106} // namespace audio_tools
107
108#endif
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGE(...)
Definition AudioLoggerIDF.h:30
@ US
Definition AudioTypes.h:49
@ HZ
Definition AudioTypes.h:49
@ MS
Definition AudioTypes.h:49
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
AudioTimerDriverAVR AudioTimerDriver
use AudioTimer!
Definition AudioTimerAVR.h:94
void(* repeating_timer_callback_t)(void *obj)
Definition AudioTimerAVR.h:7
constexpr const _Ep * end(initializer_list< _Ep > __il) noexcept
Definition InitializerList.h:63
constexpr const _Ep * begin(initializer_list< _Ep > __il) noexcept
Definition InitializerList.h:55