Auto Analog Audio v1.50.0
TMRh20 2020 - Automatic DAC, ADC & Timers on Arduino Due
Loading...
Searching...
No Matches
AutoAnalogAudio.h
Go to the documentation of this file.
1 /*
2 AutoAnalogAudio streaming via DAC & ADC by TMRh20
3 Copyright (C) 2016-2020 TMRh20 - tmrh20@gmail.com, github.com/TMRh20
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
24#ifndef __AUTO_ANALOG_H__
25#define __AUTO_ANALOG_H__
26
27#include "AutoAnalog_config.h"
28#if defined (ESP32)
29 #include "driver/i2s.h"
30#endif
31
32
33#define AAA_CHANNEL0 0
34#define AAA_CHANNEL1 1
35#define AAA_MODE_STEREO 2
36
38{
39
40public:
41
50
52 void begin(bool enADC, bool enDAC);
53
57 void triggerADC();
58
67 void getADC(uint32_t samples = MAX_BUFFER_SIZE);
68
75 void feedDAC(uint8_t dacChannel = 0, uint32_t samples = MAX_BUFFER_SIZE, bool startInterrupts = false);
76
82 uint8_t dacBuffer[MAX_BUFFER_SIZE];
83
89 uint8_t adcBuffer[MAX_BUFFER_SIZE];
90
96 uint16_t dacBuffer16[MAX_BUFFER_SIZE];
97
103 #if !defined (ARDUINO_ARCH_NRF52840) && !defined (ARDUINO_ARCH_NRF52) && !defined ARDUINO_NRF52840_FEATHER
104 uint16_t adcBuffer16[MAX_BUFFER_SIZE];
105 #elif defined __MBED__
106 inline static uint16_t adcBuffer16[MAX_BUFFER_SIZE];
107 #else
108 static uint16_t adcBuffer16[MAX_BUFFER_SIZE];
109 #endif
110
115 void setSampleRate(uint32_t sampRate = 0, bool stereo = true);
116
118 void dacHandler(void);
119
125
126
136
147
155 void enableAdcChannel(uint8_t pinAx);
156
161 void disableAdcChannel(uint8_t pinAx);
162
167 void disableDAC(bool withinTask = false);
168
179 void adcInterrupts(bool enabled = true);
180
181 void dacInterrupts(bool enabled = true, bool withinTask = false);
182
183 #if defined (ESP32) || defined (DOXYGEN_FORCED)
187 void rampOut(uint8_t sample);
188 void rampIn(uint8_t sample);
189 TaskHandle_t dacTaskHandle;
190 #endif
191
194#if defined (ARDUINO_ARCH_NRF52840) || defined (ARDUINO_ARCH_NRF52) && !defined ARDUINO_NRF52840_FEATHER && defined __MBED__
195 inline static uint8_t aCtr;
196 inline static uint32_t aSize;
197 inline static uint16_t *buf0 = NULL;
198 inline static uint16_t *buf1 = NULL;
199 inline static void (*_onReceive)(uint16_t *buf, uint32_t buf_len) = NULL;
200 inline static void adcCallback(uint16_t *buf, uint32_t buf_len);
201 inline static void set_callback(void(*function)(uint16_t *buf, uint32_t buf_len));
202 inline static bool adcReady;
203 inline static uint16_t dacBuf0[MAX_BUFFER_SIZE];
204 inline static uint16_t dacBuf1[MAX_BUFFER_SIZE];
205 bool micOn;
206 int pwrPin;
207 int dinPin;
208 int clkPin;
209 int8_t gain;
210 inline static uint32_t sampleCounter;
211
212#elif defined (ARDUINO_ARCH_NRF52840) || defined (ARDUINO_ARCH_NRF52) || defined (ARDUINO_NRF52840_FEATHER) && !defined __MBED__
213 uint16_t dacBuf0[MAX_BUFFER_SIZE];
214 uint16_t dacBuf1[MAX_BUFFER_SIZE];
215 static uint8_t aCtr;
216 static uint32_t aSize;
217 static uint16_t *buf0;
218 static uint16_t *buf1;
219 static void (*_onReceive)(uint16_t *buf, uint32_t buf_len);
220 static void adcCallback(uint16_t *buf, uint32_t buf_len);
221 void set_callback(void(*function)(uint16_t *buf, uint32_t buf_len));
222 static bool adcReady;
223 bool micOn;
224 int pwrPin;
225 int dinPin;
226 int clkPin;
227 int8_t gain;
228 uint32_t sampleCounter;
229 //void DACC_Handler();
230#endif
231
232private:
233
242#if defined (ARDUINO_ARCH_SAM)
243 bool whichDma = 0;
244 bool whichDac;
245 bool dacChan;
246
247 uint8_t aCtr = 0; /* Internal counter for ADC data */
248 uint16_t realBuf[MAX_BUFFER_SIZE]; /* Internal DAC buffer */
249 uint16_t adcDma[MAX_BUFFER_SIZE]; /* Buffers for ADC DMA transfers */
250 uint16_t dataReady; /* Internal indicator for DAC data */
251
252 uint32_t dataTimer; /* Internal timer tracks timing of incoming data */
253 uint32_t sampleCount; /* Internal counter for delaying analysis of timing */
254 uint32_t tcTicks; /* Stores the current TC0 Ch0 counter value */
255 uint32_t tcTicks2; /* Stores the current TC0 Ch1 counter value */
256 uint32_t adjustDivider; /* Internal variables for adjusting timers on-the-fly */
257
258 uint32_t dacNumSamples; /* Internal variable for number of samples sent to the DAC */
259 uint32_t adcNumSamples;
260 uint16_t adjustCtr; /* Internal variables for adjusting timers on-the-fly */
261 uint16_t adjustCtr2; /* Internal variables for adjusting timers on-the-fly */
262 uint32_t adcLastAdjust;
263#endif
264 void adcSetup(void); /* Enable the ADC */
265 void dacSetup(void); /* Enable the DAC */
266
267 void dacBufferStereo(uint8_t dacChannel);
268
269 void tcSetup(uint32_t sampRate = 0); /* Sets up Timer TC0 Channel 0 */
270 void tc2Setup(uint32_t sampRate = 0); /* Sets up Timer TC0 Channel 1 */
271
272 uint32_t frequencyToTimerCount(uint32_t Frequency); /* Function to calculate timer counters */
273
274#if defined ESP32
275
276 uint32_t sampleRate;
277 i2s_config_t i2s_cfg;
278
279 bool i2sStopped;
280 adc1_channel_t adcChannel;
281
282 bool taskCreated;
283
284 bool dacEnabled;
285 uint8_t lastDacSample;
286 i2s_event_t myI2SQueue[5];
287 //void dacTask(void *args);
288
289 bool adcTaskCreated;
290 bool adcDisabled;
291
292 #define DELAY_250MS (250 / portTICK_PERIOD_MS)
293
294#endif
296};
297
298
299
300#endif
301
302
void adcInterrupts(bool enabled=true)
void dacHandler(void)
uint8_t dacBitsPerSample
void rampIn(uint8_t sample)
uint8_t adcBuffer[MAX_BUFFER_SIZE]
uint8_t dacBuffer[MAX_BUFFER_SIZE]
TaskHandle_t dacTaskHandle
void getADC(uint32_t samples=MAX_BUFFER_SIZE)
uint16_t adcBuffer16[MAX_BUFFER_SIZE]
void rampOut(uint8_t sample)
void disableDAC(bool withinTask=false)
uint16_t dacBuffer16[MAX_BUFFER_SIZE]
void setSampleRate(uint32_t sampRate=0, bool stereo=true)
void dacInterrupts(bool enabled=true, bool withinTask=false)
void enableAdcChannel(uint8_t pinAx)
void feedDAC(uint8_t dacChannel=0, uint32_t samples=MAX_BUFFER_SIZE, bool startInterrupts=false)
uint8_t adcBitsPerSample
void triggerADC()
void begin(bool enADC, bool enDAC)
void disableAdcChannel(uint8_t pinAx)