Auto Analog Audio v1.53.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-2024 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
65 void begin(uint8_t enADC, uint8_t enDAC, uint8_t _useI2S = 0);
66
70 void triggerADC();
71
80 void getADC(uint32_t samples = MAX_BUFFER_SIZE);
81
88 void feedDAC(uint8_t dacChannel = 0, uint32_t samples = MAX_BUFFER_SIZE, bool startInterrupts = false);
89
95 uint8_t dacBuffer[MAX_BUFFER_SIZE];
96
102 uint8_t adcBuffer[MAX_BUFFER_SIZE];
103
109 uint16_t dacBuffer16[MAX_BUFFER_SIZE];
110
116 #if !defined (ARDUINO_ARCH_NRF52840) && !defined (ARDUINO_ARCH_NRF52) && !defined ARDUINO_NRF52840_FEATHER
117 uint16_t adcBuffer16[MAX_BUFFER_SIZE];
118 #elif defined __MBED__
119 inline static uint16_t adcBuffer16[MAX_BUFFER_SIZE];
120 #else
121 static uint16_t adcBuffer16[MAX_BUFFER_SIZE];
122 #endif
123
128 void setSampleRate(uint32_t sampRate = 0, bool stereo = false);
129
131 void dacHandler(void);
132
138
139
149
160
168 void enableAdcChannel(uint8_t pinAx);
169
174 void disableAdcChannel(uint8_t pinAx);
175
180 void disableDAC(bool withinTask = false);
181
192 void adcInterrupts(bool enabled = true);
193
194 void dacInterrupts(bool enabled = true, bool withinTask = false);
195
196 #if defined (ESP32) || defined (DOXYGEN_FORCED)
200 void rampOut(uint8_t sample);
201 void rampIn(uint8_t sample);
202 TaskHandle_t dacTaskHandle;
203 #endif
204
205
206#if defined (ARDUINO_ARCH_NRF52840) || defined (ARDUINO_ARCH_NRF52) && !defined ARDUINO_NRF52840_FEATHER && defined __MBED__
207 inline static uint8_t aCtr;
208 inline static uint32_t aSize;
209 inline static uint16_t *adcBuf0 = NULL;
210 inline static uint16_t *adcBuf1 = NULL;
211 inline static void (*_onReceive)(uint16_t *buf, uint32_t buf_len) = NULL;
212 inline static void adcCallback(uint16_t *buf, uint32_t buf_len);
213 inline static void set_callback(void(*function)(uint16_t *buf, uint32_t buf_len));
214 inline static bool adcReady;
215 inline static uint16_t *dacBuf0;
216 inline static uint16_t *dacBuf1;
217 inline static uint32_t sampleCounter;
218 uint8_t enableADC;
219 uint8_t enableDAC;
220 bool whichBuf;
221 bool adcWhichBuf;
222
223#elif defined (ARDUINO_ARCH_NRF52840) || defined (ARDUINO_ARCH_NRF52) || defined (ARDUINO_NRF52840_FEATHER) && !defined __MBED__
224 uint16_t *dacBuf0;
225 uint16_t *dacBuf1;
226 static uint8_t aCtr;
227 static uint32_t aSize;
228 static uint16_t *adcBuf0;
229 static uint16_t *adcBuf1;
230 static void (*_onReceive)(uint16_t *buf, uint32_t buf_len);
231 static void adcCallback(uint16_t *buf, uint32_t buf_len);
232 void set_callback(void(*function)(uint16_t *buf, uint32_t buf_len));
233 static bool adcReady;
234 uint32_t sampleCounter;
235 uint8_t enableADC;
236 uint8_t enableDAC;
237 bool whichBuf;
238 bool adcWhichBuf;
239#endif
240
241#if defined (ARDUINO_ARCH_NRF52840) || defined (ARDUINO_ARCH_NRF52) || defined (DOXYGEN_FORCED)
259
267
275
281 int8_t gain;
282
297 uint16_t I2S_PIN_MCK;
299 uint16_t I2S_PIN_SCK;
301 uint16_t I2S_PIN_LRCK;
305 uint16_t I2S_PIN_SDIN;
307
315#endif
316
318private:
327#if defined (ARDUINO_ARCH_SAM)
328 bool whichDma = 0;
329 bool whichDac;
330 bool dacChan;
331
332 uint8_t aCtr = 0; /* Internal counter for ADC data */
333 uint16_t realBuf[MAX_BUFFER_SIZE]; /* Internal DAC buffer */
334 uint16_t adcDma[MAX_BUFFER_SIZE]; /* Buffers for ADC DMA transfers */
335 uint16_t dataReady; /* Internal indicator for DAC data */
336
337 uint32_t dataTimer; /* Internal timer tracks timing of incoming data */
338 uint32_t sampleCount; /* Internal counter for delaying analysis of timing */
339 uint32_t tcTicks; /* Stores the current TC0 Ch0 counter value */
340 uint32_t tcTicks2; /* Stores the current TC0 Ch1 counter value */
341 uint32_t adjustDivider; /* Internal variables for adjusting timers on-the-fly */
342
343 uint32_t dacNumSamples; /* Internal variable for number of samples sent to the DAC */
344 uint32_t adcNumSamples;
345 uint16_t adjustCtr; /* Internal variables for adjusting timers on-the-fly */
346 uint16_t adjustCtr2; /* Internal variables for adjusting timers on-the-fly */
347 uint32_t adcLastAdjust;
348#endif
349 void adcSetup(void); /* Enable the ADC */
350 void dacSetup(void); /* Enable the DAC */
351
352 void dacBufferStereo(uint8_t dacChannel);
353
354 void tcSetup(uint32_t sampRate = 0); /* Sets up Timer TC0 Channel 0 */
355 void tc2Setup(uint32_t sampRate = 0); /* Sets up Timer TC0 Channel 1 */
356
357 uint32_t frequencyToTimerCount(uint32_t Frequency); /* Function to calculate timer counters */
358
359#if defined ESP32
360
361 uint32_t sampleRate;
362 i2s_config_t i2s_cfg;
363
364 bool i2sStopped;
365 adc1_channel_t adcChannel;
366
367 bool taskCreated;
368
369 bool dacEnabled;
370 uint8_t lastDacSample;
371 i2s_event_t myI2SQueue[5];
372 //void dacTask(void *args);
373
374 bool adcTaskCreated;
375 bool adcDisabled;
376
377 #define DELAY_250MS (250 / portTICK_PERIOD_MS)
378
379#endif
381};
382
383
384
385#endif
386
387
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)