arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
PWMAudioAVR.h
1
2#pragma once
3#include "AudioToolsConfig.h"
4#if defined(USE_PWM) && defined(__AVR__)
5#include "AudioTools/CoreAudio/AudioPWM/PWMAudioBase.h"
6#include "AudioTools/CoreAudio/AudioTimer/AudioTimerAVR.h"
7
8namespace audio_tools {
9
10class PWMDriverAVR;
11using PWMDriver = PWMDriverAVR;
12static PWMDriverAVR *accessAudioPWM = nullptr;
13
24
25 public:
26 PWMDriverAVR() {
27 accessAudioPWM = this;
28 }
29
30 virtual int maxChannels() { return 2; };
31
32 // Ends the output
33 virtual void end() {
34 TRACED();
35 noInterrupts();
36 // stop timer callback
37 TCCR1B = 0;
38 // stop pwm timers
39 TCCR2A = 0;
40 interrupts(); // enable all interrupts
41
42 is_timer_started = false;
43 deleteBuffer();
44 }
45
46 void setupTimer() {
47 TRACED();
48 // CPU Frequency 16 MHz
49 // prescaler 1, 256 or 1024 => no prescaling
50 uint32_t steps =
51 F_CPU / 8 / audio_config.sample_rate; // e.g. (16000000/8/44100=>45)
52 if (steps > 65535) {
53 LOGE("requested sample rate not supported: %d - we use %d",
54 audio_config.sample_rate, F_CPU / 65536);
55 steps = 65535;
56 } else {
57 LOGD("compare match register set to %d", steps);
58 }
59
60 // setup timer intterupt
61 noInterrupts();
62 TCCR1B = 0;
63 // compare match register
64 OCR1A = steps;
65 TCCR1B |= (1 << WGM12); // CTC mode
66 // TCCR1B |= (1 << CS10); // prescaler 1
67 TCCR1B |= (1 << CS11); // prescaler 8
68 TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
69 interrupts(); // enable all interrupts
70 }
71
73 void setupPWM() {
74 TRACED();
75 audio_config.pwm_frequency = 62500;
76
77 if (audio_config.channels > 2) {
78 LOGW("Max 2 channels supported - you requested %d",
79 audio_config.channels);
80 audio_config.channels = 2;
81 }
82
83 for (int j = 0; j < audio_config.channels; j++) {
84 LOGD("Processing channel %d", j);
85 setupPin(pins[j]);
86 }
87 }
88
89 void startTimer() {}
90
91 // Timer 0 is used by Arduino!
92 // Timer 1 is used to drive output in sample_rate
93 // => only Timer2 is available for PWM
94 void setupPin(int pin) {
95 switch (pin) {
96 case 3:
97 case 11:
98 // switch PWM frequency to 62500.00 Hz
99 TCCR2B = TCCR2B & B11111000 | B00000001;
100 LOGI("PWM Frequency changed for D3 and D11");
101 break;
102
103 default:
104 LOGE("PWM Unsupported pin: %d", pin);
105 break;
106 }
107 pinMode(pin, OUTPUT);
108 }
109
110 virtual void pwmWrite(int channel, int value) {
111 analogWrite(pins[channel], value);
112 }
113
114 void logConfig() {
115 audio_config.logConfig();
116 LOGI("pwm freq: %f khz", 62.5);
117 if (audio_config.channels == 1) {
118 LOGI("output pin: %d", pins[0]);
119 } else {
120 LOGI("output pins: %d / %d", pins[0], pins[1]);
121 }
122 }
123
124 protected:
125 int pins[2] = {3, 11};
126
127 virtual int maxOutputValue() { return 255; }
128};
129
133 if (accessAudioPWM != nullptr && accessAudioPWM->is_timer_started) {
134 accessAudioPWM->playNextFrame();
135 }
136}
137
139ISR(TIMER1_COMPA_vect) {
141 TimerAlarmRepeatingDriverAVR::tickerCallback();
142}
143
144} // namespace audio_tools
145
146#endif
Base Class for all PWM drivers.
Definition PWMAudioBase.h:110
void playNextFrame()
writes the next frame to the output pins
Definition PWMAudioBase.h:270
Experimental: Audio output to PWM pins for the AVR. The AVR supports only up to 2 channels.
Definition PWMAudioAVR.h:22
void setupPWM()
Setup LED PWM.
Definition PWMAudioAVR.h:73
friend void defaultPWMAudioOutputCallback()
Definition PWMAudioAVR.h:132
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
void defaultPWMAudioOutputCallback()
Definition PWMAudioAVR.h:132
ISR(TIMER1_COMPA_vect)
timer callback: write the next frame to the pins
Definition PWMAudioAVR.h:139
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:55
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:57
uint32_t pwm_frequency
additinal info which might not be used by all processors
Definition PWMAudioBase.h:46