Auto Analog Audio v1.55.3
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 #if !defined ARDUINO_ARCH_RENESAS
95 uint8_t dacBuffer[MAX_BUFFER_SIZE];
96 #else
97 static uint8_t dacBuffer[MAX_BUFFER_SIZE];
98 #endif
104 #if !defined(ARDUINO_ARCH_NRF52840) && !defined(ARDUINO_ARCH_NRF52) && !defined ARDUINO_NRF52840_FEATHER && !defined ARDUINO_NRF54L15 && !defined ARDUINO_ARCH_RENESAS
105 uint8_t adcBuffer[MAX_BUFFER_SIZE];
106 #elif defined __MBED__
107 inline static uint8_t adcBuffer[MAX_BUFFER_SIZE];
108 #else
109 static uint8_t adcBuffer[MAX_BUFFER_SIZE];
110 #endif
111
117 uint16_t dacBuffer16[MAX_BUFFER_SIZE];
118
124 #if !defined(ARDUINO_ARCH_NRF52840) && !defined(ARDUINO_ARCH_NRF52) && !defined ARDUINO_NRF52840_FEATHER && !defined ARDUINO_NRF54L15 && !defined ARDUINO_ARCH_RENESAS
125 uint16_t adcBuffer16[MAX_BUFFER_SIZE];
126 #elif defined __MBED__
127 inline static uint16_t adcBuffer16[MAX_BUFFER_SIZE];
128 #else
129 static uint16_t adcBuffer16[MAX_BUFFER_SIZE];
130 #endif
131
136 void setSampleRate(uint32_t sampRate = 0, bool stereo = false);
137
139 void dacHandler(void);
140
146
155 #if !defined(ARDUINO_ARCH_NRF52840) && !defined(ARDUINO_ARCH_NRF52) && !defined ARDUINO_NRF52840_FEATHER && !defined ARDUINO_NRF54L15 && !defined ARDUINO_ARCH_RENESAS
157 #elif defined __MBED__
158 inline static uint8_t adcBitsPerSample;
159 #else
160 static uint8_t adcBitsPerSample;
161 #endif
162
173
181 void enableAdcChannel(uint8_t pinAx);
182
187 void disableAdcChannel(uint8_t pinAx);
188
193 void disableDAC(bool withinTask = false);
194
205 void adcInterrupts(bool enabled = true);
206
207 void dacInterrupts(bool enabled = true, bool withinTask = false);
208
209 #if defined(ESP32) || defined(DOXYGEN_FORCED)
213 void rampOut(uint8_t sample);
214 void rampIn(uint8_t sample);
215 TaskHandle_t dacTaskHandle;
216 #endif
217
218 #if defined(ARDUINO_ARCH_NRF52840) || defined(ARDUINO_ARCH_NRF52) && !defined ARDUINO_NRF52840_FEATHER && defined __MBED__
219 inline static uint8_t aCtr;
220 inline static uint32_t aSize;
221 inline static uint16_t* adcBuf0 = NULL;
222 inline static uint16_t* adcBuf1 = NULL;
223 inline static void (*_onReceive)(uint16_t* buf, uint32_t buf_len) = NULL;
224 inline static void adcCallback(uint16_t* buf, uint32_t buf_len);
225 inline static void set_callback(void (*function)(uint16_t* buf, uint32_t buf_len));
226 inline static bool adcReady;
227 inline static uint16_t* dacBuf0;
228 inline static uint16_t* dacBuf1;
229 inline static uint32_t sampleCounter;
230 uint8_t enableADC;
231 uint8_t enableDAC;
232 bool whichBuf;
233 inline static bool adcWhichBuf;
234
235 #elif defined(ARDUINO_ARCH_NRF52840) || defined(ARDUINO_ARCH_NRF52) || defined ARDUINO_NRF54L15 || defined(ARDUINO_NRF52840_FEATHER) && !defined __MBED__
236 uint16_t* dacBuf0;
237 uint16_t* dacBuf1;
238 static uint8_t aCtr;
239 static uint32_t aSize;
240 static uint16_t* adcBuf0;
241 static uint16_t* adcBuf1;
242 static void (*_onReceive)(uint16_t* buf, uint32_t buf_len);
243 static void adcCallback(uint16_t* buf, uint32_t buf_len);
244 void set_callback(void (*function)(uint16_t* buf, uint32_t buf_len));
245 static bool adcReady;
246 uint32_t sampleCounter;
247 uint8_t enableADC;
248 uint8_t enableDAC;
249 bool whichBuf;
250 static bool adcWhichBuf;
251
252 #elif defined ARDUINO_ARCH_RENESAS
253 static volatile uint16_t* dacBuf0;
254 static volatile uint16_t* dacBuf1;
255 volatile static uint8_t aCtr;
256 static volatile uint32_t aSize;
257 static uint16_t* adcBuf0;
258 static uint16_t* adcBuf1;
259 static void (*_onReceive)(uint16_t* buf, uint32_t buf_len);
260 static void adcCallback(uint16_t* buf, uint32_t buf_len);
261 void set_callback(void (*function)(uint16_t* buf, uint32_t buf_len));
262 static volatile bool adcReady;
263 static volatile uint32_t sampleCounter;
264 uint8_t enableADC;
265 uint8_t enableDAC;
266 bool whichBuf;
267 static volatile bool adcWhichBuf;
268 static volatile uint32_t sCounter;
269 bool dacDisabled;
270 static volatile uint8_t analogChannel;
271 #endif
272
273 #if defined(ARDUINO_ARCH_NRF52840) || defined(ARDUINO_ARCH_NRF52) || defined ARDUINO_NRF54L15 || defined ARDUINO_ARCH_RENESAS || defined(DOXYGEN_FORCED)
291
299
307
313 int8_t gain;
314
329 uint16_t I2S_PIN_MCK;
331 uint16_t I2S_PIN_SCK;
333 uint16_t I2S_PIN_LRCK;
337 uint16_t I2S_PIN_SDIN;
339
347
357
358private:
359 bool adcBuffersAllocated;
360 bool dacBuffersAllocated;
361 void startPwmI2sTimers();
362 #endif
363
366private:
375 #if defined(ARDUINO_ARCH_SAM)
376 bool whichDma = 0;
377 bool whichDac;
378 bool dacChan;
379
380 uint8_t aCtr = 0; /* Internal counter for ADC data */
381 uint16_t realBuf[MAX_BUFFER_SIZE]; /* Internal DAC buffer */
382 uint16_t adcDma[MAX_BUFFER_SIZE]; /* Buffers for ADC DMA transfers */
383 uint16_t dataReady; /* Internal indicator for DAC data */
384
385 uint32_t dataTimer; /* Internal timer tracks timing of incoming data */
386 uint32_t sampleCount; /* Internal counter for delaying analysis of timing */
387 uint32_t tcTicks; /* Stores the current TC0 Ch0 counter value */
388 uint32_t tcTicks2; /* Stores the current TC0 Ch1 counter value */
389 uint32_t adjustDivider; /* Internal variables for adjusting timers on-the-fly */
390
391 uint32_t dacNumSamples; /* Internal variable for number of samples sent to the DAC */
392 uint32_t adcNumSamples;
393 uint16_t adjustCtr; /* Internal variables for adjusting timers on-the-fly */
394 uint16_t adjustCtr2; /* Internal variables for adjusting timers on-the-fly */
395 uint32_t adcLastAdjust;
396 #endif
397 void adcSetup(void); /* Enable the ADC */
398 void dacSetup(void); /* Enable the DAC */
399
400 void dacBufferStereo(uint8_t dacChannel);
401
402 void tcSetup(uint32_t sampRate = 0); /* Sets up Timer TC0 Channel 0 */
403 void tc2Setup(uint32_t sampRate = 0); /* Sets up Timer TC0 Channel 1 */
404
405 uint32_t frequencyToTimerCount(uint32_t Frequency); /* Function to calculate timer counters */
406
407 #if defined ESP32
408
409 uint32_t sampleRate;
410 i2s_config_t i2s_cfg;
411
412 bool i2sStopped;
413 adc1_channel_t adcChannel;
414
415 bool taskCreated;
416
417 bool dacEnabled;
418 uint8_t lastDacSample;
419 i2s_event_t myI2SQueue[5];
420 //void dacTask(void *args);
421
422 bool adcTaskCreated;
423 bool adcDisabled;
424
425 #define DELAY_250MS (250 / portTICK_PERIOD_MS)
426
427 #endif
429};
430
431#endif
432
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)