Auto Analog Audio v1.54.0
TMRh20 2020 - Automatic DAC, ADC & Timers on Arduino Due
Loading...
Searching...
No Matches
AutoAnalogAudio.h
Go to the documentation of this file.
1/*
2AutoAnalogAudio streaming via DAC & ADC by TMRh20
3Copyright (C) 2016-2024 TMRh20 - tmrh20@gmail.com, github.com/TMRh20
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 3 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along 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 #define AAA_CHANNEL0 0
33 #define AAA_CHANNEL1 1
34 #define AAA_MODE_STEREO 2
35
37{
38
39public:
40
49
64 void begin(uint8_t enADC, uint8_t enDAC, uint8_t _useI2S = 0);
65
69 void triggerADC();
70
79 void getADC(uint32_t samples = MAX_BUFFER_SIZE);
80
87 void feedDAC(uint8_t dacChannel = 0, uint32_t samples = MAX_BUFFER_SIZE, bool startInterrupts = false);
88
94 uint8_t dacBuffer[MAX_BUFFER_SIZE];
95
101 #if !defined(ARDUINO_ARCH_NRF52840) && !defined(ARDUINO_ARCH_NRF52) && !defined ARDUINO_NRF52840_FEATHER
102 uint8_t adcBuffer[MAX_BUFFER_SIZE];
103 #elif defined __MBED__
104 inline static uint8_t adcBuffer[MAX_BUFFER_SIZE];
105 #else
106 static uint8_t adcBuffer[MAX_BUFFER_SIZE];
107 #endif
108
114 uint16_t dacBuffer16[MAX_BUFFER_SIZE];
115
121 #if !defined(ARDUINO_ARCH_NRF52840) && !defined(ARDUINO_ARCH_NRF52) && !defined ARDUINO_NRF52840_FEATHER
122 uint16_t adcBuffer16[MAX_BUFFER_SIZE];
123 #elif defined __MBED__
124 inline static uint16_t adcBuffer16[MAX_BUFFER_SIZE];
125 #else
126 static uint16_t adcBuffer16[MAX_BUFFER_SIZE];
127 #endif
128
133 void setSampleRate(uint32_t sampRate = 0, bool stereo = false);
134
136 void dacHandler(void);
137
143
152 #if !defined(ARDUINO_ARCH_NRF52840) && !defined(ARDUINO_ARCH_NRF52) && !defined ARDUINO_NRF52840_FEATHER
154 #elif defined __MBED__
155 inline static uint8_t adcBitsPerSample;
156 #else
157 static uint8_t adcBitsPerSample;
158 #endif
159
170
178 void enableAdcChannel(uint8_t pinAx);
179
184 void disableAdcChannel(uint8_t pinAx);
185
190 void disableDAC(bool withinTask = false);
191
202 void adcInterrupts(bool enabled = true);
203
204 void dacInterrupts(bool enabled = true, bool withinTask = false);
205
206 #if defined(ESP32) || defined(DOXYGEN_FORCED)
210 void rampOut(uint8_t sample);
211 void rampIn(uint8_t sample);
212 TaskHandle_t dacTaskHandle;
213 #endif
214
215 #if defined(ARDUINO_ARCH_NRF52840) || defined(ARDUINO_ARCH_NRF52) && !defined ARDUINO_NRF52840_FEATHER && defined __MBED__
216 inline static uint8_t aCtr;
217 inline static uint32_t aSize;
218 inline static uint16_t* adcBuf0 = NULL;
219 inline static uint16_t* adcBuf1 = NULL;
220 inline static void (*_onReceive)(uint16_t* buf, uint32_t buf_len) = NULL;
221 inline static void adcCallback(uint16_t* buf, uint32_t buf_len);
222 inline static void set_callback(void (*function)(uint16_t* buf, uint32_t buf_len));
223 inline static bool adcReady;
224 inline static uint16_t* dacBuf0;
225 inline static uint16_t* dacBuf1;
226 inline static uint32_t sampleCounter;
227 uint8_t enableADC;
228 uint8_t enableDAC;
229 bool whichBuf;
230 inline static bool adcWhichBuf;
231
232 #elif defined(ARDUINO_ARCH_NRF52840) || defined(ARDUINO_ARCH_NRF52) || defined(ARDUINO_NRF52840_FEATHER) && !defined __MBED__
233 uint16_t* dacBuf0;
234 uint16_t* dacBuf1;
235 static uint8_t aCtr;
236 static uint32_t aSize;
237 static uint16_t* adcBuf0;
238 static uint16_t* adcBuf1;
239 static void (*_onReceive)(uint16_t* buf, uint32_t buf_len);
240 static void adcCallback(uint16_t* buf, uint32_t buf_len);
241 void set_callback(void (*function)(uint16_t* buf, uint32_t buf_len));
242 static bool adcReady;
243 uint32_t sampleCounter;
244 uint8_t enableADC;
245 uint8_t enableDAC;
246 bool whichBuf;
247 static bool adcWhichBuf;
248 #endif
249
250 #if defined(ARDUINO_ARCH_NRF52840) || defined(ARDUINO_ARCH_NRF52) || defined(DOXYGEN_FORCED)
268
276
284
290 int8_t gain;
291
306 uint16_t I2S_PIN_MCK;
308 uint16_t I2S_PIN_SCK;
310 uint16_t I2S_PIN_LRCK;
314 uint16_t I2S_PIN_SDIN;
316
324
334
335private:
336 bool adcBuffersAllocated;
337 bool dacBuffersAllocated;
338
339 #endif
340
343private:
352 #if defined(ARDUINO_ARCH_SAM)
353 bool whichDma = 0;
354 bool whichDac;
355 bool dacChan;
356
357 uint8_t aCtr = 0; /* Internal counter for ADC data */
358 uint16_t realBuf[MAX_BUFFER_SIZE]; /* Internal DAC buffer */
359 uint16_t adcDma[MAX_BUFFER_SIZE]; /* Buffers for ADC DMA transfers */
360 uint16_t dataReady; /* Internal indicator for DAC data */
361
362 uint32_t dataTimer; /* Internal timer tracks timing of incoming data */
363 uint32_t sampleCount; /* Internal counter for delaying analysis of timing */
364 uint32_t tcTicks; /* Stores the current TC0 Ch0 counter value */
365 uint32_t tcTicks2; /* Stores the current TC0 Ch1 counter value */
366 uint32_t adjustDivider; /* Internal variables for adjusting timers on-the-fly */
367
368 uint32_t dacNumSamples; /* Internal variable for number of samples sent to the DAC */
369 uint32_t adcNumSamples;
370 uint16_t adjustCtr; /* Internal variables for adjusting timers on-the-fly */
371 uint16_t adjustCtr2; /* Internal variables for adjusting timers on-the-fly */
372 uint32_t adcLastAdjust;
373 #endif
374 void adcSetup(void); /* Enable the ADC */
375 void dacSetup(void); /* Enable the DAC */
376
377 void dacBufferStereo(uint8_t dacChannel);
378
379 void tcSetup(uint32_t sampRate = 0); /* Sets up Timer TC0 Channel 0 */
380 void tc2Setup(uint32_t sampRate = 0); /* Sets up Timer TC0 Channel 1 */
381
382 uint32_t frequencyToTimerCount(uint32_t Frequency); /* Function to calculate timer counters */
383
384 #if defined ESP32
385
386 uint32_t sampleRate;
387 i2s_config_t i2s_cfg;
388
389 bool i2sStopped;
390 adc1_channel_t adcChannel;
391
392 bool taskCreated;
393
394 bool dacEnabled;
395 uint8_t lastDacSample;
396 i2s_event_t myI2SQueue[5];
397 //void dacTask(void *args);
398
399 bool adcTaskCreated;
400 bool adcDisabled;
401
402 #define DELAY_250MS (250 / portTICK_PERIOD_MS)
403
404 #endif
406};
407
408#endif
409
void adcInterrupts(bool enabled=true)
void dacHandler(void)
uint8_t dacBitsPerSample
void setSampleRate(uint32_t sampRate=0, bool stereo=false)
void rampIn(uint8_t sample)
uint8_t adcBuffer[MAX_BUFFER_SIZE]
uint8_t I2S_PORT_SDIN
uint16_t I2S_PIN_LRCK
uint8_t I2S_PORT_LRCK
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)
uint8_t I2S_PORT_SCK
void disableDAC(bool withinTask=false)
uint16_t I2S_PIN_SCK
uint16_t dacBuffer16[MAX_BUFFER_SIZE]
uint8_t I2S_PORT_SDOUT
uint16_t I2S_PIN_MCK
void begin(uint8_t enADC, uint8_t enDAC, uint8_t _useI2S=0)
void dacInterrupts(bool enabled=true, bool withinTask=false)
void enableAdcChannel(uint8_t pinAx)
uint8_t I2S_PORT_MCK
void feedDAC(uint8_t dacChannel=0, uint32_t samples=MAX_BUFFER_SIZE, bool startInterrupts=false)
uint16_t I2S_PIN_SDOUT
uint16_t I2S_PIN_SDIN
uint8_t adcBitsPerSample
void triggerADC()
uint32_t maxBufferSize
void disableAdcChannel(uint8_t pinAx)