arduino-audio-tools
Loading...
Searching...
No Matches
AnalogDriverESP32V2.h
Go to the documentation of this file.
1#pragma once
2
3#include "AudioToolsConfig.h"
4
5#if defined(ESP32) && defined(USE_ANALOG) && !USE_LEGACY_I2S
6
7#ifdef ARDUINO
8 #ifndef perimanClearPinBus
9 #define perimanClearPinBus(p) perimanSetPinBus(p, ESP32_BUS_TYPE_INIT, NULL)
10 #endif
11#endif
12
17
18namespace audio_tools {
19
30public:
33
36 end();
37 }
38
42 TRACEI();
43 bool result = true;
44 this->cfg = cfg;
45
46 switch (cfg.rx_tx_mode) {
47 case TX_MODE:
48 if (!setup_tx()) return false;
49 // convert to 16 bits
50 if (!converter.begin(cfg, 16)) {
51 LOGE("converter");
52 return false;
53 }
54 active_tx = true;
55 break;
56 case RX_MODE:
57 if (!setup_rx()) return false;
58 active_rx = true;
59 break;
60 default:
61 LOGE( "Unsupported MODE: %d", cfg.rx_tx_mode);
62 return false;
63 }
64
65 active = true;
66 return active;
67 }
68
71 void end() override {
72 TRACEI();
73 if (active_tx) {
74 cleanup_tx();
75 }
76 if (active_rx) {
77 cleanup_rx();
78 }
79
80 converter.end();
81
82 active_tx = false;
83 active_rx = false;
84 active = false;
85 }
86
87 // Writes the data to the Digital to Analog Converter
88 // ----------------------------------------------------------
89 size_t write(const uint8_t *src, size_t size_bytes) override {
90 // TRACED();
91 // convert any format to int16_t
92 return converter.write(src, size_bytes);
93 }
94
95 // Reads data from DMA buffer of the Analog to Digital Converter
96 // ----------------------------------------------------------
97 size_t readBytes(uint8_t *dest, size_t size_bytes) override {
98 TRACED();
99 // Use the IO16Bit class for reading
100 return io.readBytes(dest, size_bytes);
101 }
102
103 // How much data will there be available after reading ADC buffer
104 // ----------------------------------------------------------
105 int available() override {
106 return active_rx ? configuredRxBytes() : 0;
107 }
108
109protected:
110
115 bool active = false;
116 bool active_tx = false;
117 bool active_rx = false;
118 bool rx_started = false;
121 #ifdef HAS_ESP32_DAC
123 #endif
124
125 int configuredRxBytes() const {
126 return (cfg.buffer_size > 0) ? (int)(cfg.buffer_size * sizeof(int16_t)) : 0;
127 }
128
130 if (!adc_cali_handle_active || adc_cali_handle == nullptr) return;
131#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
133#elif !defined(CONFIG_IDF_TARGET_ESP32H2)
135#endif
136 adc_cali_handle = nullptr;
138 }
139
140#ifdef ARDUINO
141 void cleanupAttachedRxPins() {
143 for (int i = 0; i < rx_pins_attached; ++i) {
144 adc_channel_t adc_channel = cfg.adc_channels[i];
145 int io_pin;
149 LOGE("perimanClearPinBus failed!");
150 }
151 }
152 }
154 }
155#endif
156
157
158 // 16Bit Audiostream for ESP32
159 // ----------------------------------------------------------
160 class IO16Bit : public AudioStream {
161 public:
162 IO16Bit(AnalogDriverESP32V2 *driver) { self = driver; }
163
165 size_t write(const uint8_t *src, size_t size_bytes) override {
166 // TRACED();
167#ifdef HAS_ESP32_DAC
168 size_t result = 0;
169 // Convert signed 16-bit to unsigned 8-bit
171 uint8_t *data8 = (uint8_t *)src;
172 int samples = size_bytes / 2;
173
174 // Process data in batches to reduce the number of conversions and writes
175 for (int j = 0; j < samples; j++) {
176 data8[j] = (32768u + data16[j]) >> 8;
177 }
178
179 if (dac_continuous_write(self->dac_handle, data8, samples, &result, self->cfg.timeout) != ESP_OK) {
180 result = 0;
181 }
182 return result * 2;
183#else
184 return 0;
185#endif
186 }
187
189 size_t readBytes(uint8_t *dest, size_t size_bytes) {
190 LOGD("readBytes: %d", (int) size_bytes);
191 size_t result = 0;
192 int channels = self->cfg.channels;
193 assert(channels > 0);
194 sampleIndex.resize(channels);
195 for(int ch=0;ch < channels;ch++){
196 sampleIndex[ch] = 0;
197 }
198 uint16_t *result16 = (uint16_t *)dest; // pointer to the destination buffer
199 int samples_requested = size_bytes / sizeof(int16_t);
201 int buffer_size = samples_requested * sizeof(adc_digi_output_data_t);
202 if (result_data.size() < buffer_size){
203 result_data.resize(buffer_size);
204 }
205
206 int read_size_bytes = buffer_size;
207
208 // read the requested bytes into the buffer
209 while (read_size_bytes > 0){
213 LOGD("adc_continuous_read -> %u bytes / %d samples of %d bytes requested", (unsigned)bytes_read, samples_read, (int)(samples_requested * sizeof(adc_digi_output_data_t)));
214
215 // Provide samples to dest
216 for (int i = 0; i < samples_read; i++) {
220 int channel = getChannelIdx(chan_num);
221
222 // provide data to dest
223 if (channel >=0 && channel < channels){
224 int idx = channel + (sampleIndex[channel] * channels);
225 LOGI("idx for %d: %d", channel, idx);
226 if(idx < samples_requested){
227 // Provide result in millivolts
230 } else {
231 result16[idx] = sample_value;
232 }
233 // increase index for the actual channel
234 sampleIndex[channel]++;
235 } else {
236 LOGE("Invalid idx: %d / max %d", idx, samples_requested);
237 }
238 } else {
239 LOGE("Invalid channel: %d", channel);
240 }
241 }
244 result = samples_available * channels * sizeof(int16_t);
245
247 read_size_bytes = result - size_bytes;
248 if (read_size_bytes > 0)
249 LOGI("read missing samples: %d", samples_requested_per_channel - samples_available);
250 } else {
251 LOGE("adc_continuous_read error");
252 break;
253 }
254 }
255
256
257 // Centering if enabled
259 self->auto_center.convert(dest, result);
260 }
261
262 return result;
263 }
264
265 protected:
269
271 for (int j = 0; j < self->cfg.channels; ++j) {
272 if (self->cfg.adc_channels[j] == chan_num) {
273 return j;
274 }
275 }
276 return -1;
277 }
278
280 int data_milliVolts = 0;
281 int16_t result = 0;
283 if (err == ESP_OK) {
284 result = static_cast<int16_t>(data_milliVolts);
285 } else {
286 LOGE("adc_cali_raw_to_voltage error: %d", err);
287 }
288 return result;
289 }
290
291 int getMinSamplesForAllChannels(int channels) {
292 int result = 100000;
293 for (int ch = 0; ch < channels; ch++){
294 if (sampleIndex[ch] < result){
295 result = sampleIndex[ch];
296 }
297 }
298 return result;
299 }
300
301 } io{this};
302
304
305 // Setup Digital to Analog
306 // ----------------------------------------------------------
307 #ifdef HAS_ESP32_DAC
308 bool setup_tx() {
310 .chan_mask = cfg.channels == 1 ? cfg.dac_mono_channel : DAC_CHANNEL_MASK_ALL,
311 .desc_num = (uint32_t)cfg.buffer_count,
312 .buf_size = (size_t)cfg.buffer_size,
313 .freq_hz = (uint32_t)cfg.sample_rate,
314 .offset = 0,
315 .clk_src = cfg.use_apll ? DAC_DIGI_CLK_SRC_APLL : DAC_DIGI_CLK_SRC_DEFAULT, // Using APLL as clock source to get a wider frequency range
317 };
318 // Allocate continuous channels
320 LOGE("new_channels");
321 return false;
322 }
324 LOGE("enable");
325 return false;
326 }
327 return true;
328 }
329 #else
330 bool setup_tx() {
331 LOGE("DAC not supported");
332 return false;
333 }
334 #endif
335
336 // Setup Analog to Digital Converter
337 // ----------------------------------------------------------
338 bool setup_rx() {
339 adc_channel_t adc_channel;
340 int io_pin;
341 esp_err_t err;
342
343 // Check the configuration
344 if (!checkADCChannels()) return false;
345 if (!checkADCSampleRate()) return false;
346 if (!checkADCBitWidth()) return false;
347 if (!checkADCBitsPerSample()) return false;
348
349 if (adc_handle != nullptr) {
350 LOGE("adc unit %u continuous is already initialized. Please call end() first!", cfg.adc_unit);
351 return false;
352 }
353
354 #ifdef ARDUINO
355 // Set periman deinit callback
356 // TODO, currently handled in end() method
357
358 // Set the pins/channels to INIT state
359 for (int i = 0; i < cfg.channels; i++) {
360 adc_channel = cfg.adc_channels[i];
363 LOGE("perimanClearPinBus failed!");
364 return false;
365 }
366 }
367 #endif
368
370 #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
372 #endif
373
377 LOGE(
378 "buffer_size is too big for one ADC DMA frame: %u samples = %u "
379 "bytes, max %u samples / %u bytes",
381 (unsigned)max_samples_per_frame,
382 (unsigned)rx_max_conv_frame_bytes);
383 return false;
384 } else {
385 LOGI(
386 "RX DMA frame: %u conversion results, %u bytes (max %u results / "
387 "%u bytes)",
389 (unsigned)max_samples_per_frame,
390 (unsigned)rx_max_conv_frame_bytes);
391 }
392
393 // Create adc_continuous handle
396 adc_config.max_store_buf_size = (uint32_t)conv_frame_size * rx_frame_count;
397 adc_config.conv_frame_size = (uint32_t) conv_frame_size;
398#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0)
399 adc_config.flags.flush_pool = true;
400#endif
401 LOGI("RX pool: %u frames, %u bytes", (unsigned)rx_frame_count,
402 (unsigned)adc_config.max_store_buf_size);
403 err = adc_continuous_new_handle(&adc_config, &adc_handle);
404 if (err != ESP_OK) {
405 LOGE("adc_continuous_new_handle failed with error: %d", err);
406 return false;
407 } else {
408 LOGI("adc_continuous_new_handle successful");
409 }
410
411 // Configure the ADC patterns
413 for (int i = 0; i < cfg.channels; i++) {
416 adc_pattern[i].channel = (uint8_t)ch;
417 adc_pattern[i].unit = (uint8_t) cfg.adc_unit;
418 adc_pattern[i].bit_width = (uint8_t) cfg.adc_bit_width;
419 }
420
421 // Configure the ADC
423 .pattern_num = (uint32_t) cfg.channels,
424 .adc_pattern = adc_pattern,
425 .sample_freq_hz = (uint32_t)cfg.sample_rate * cfg.channels,
428 };
429
430 // Log the configuration
431 LOGI("dig_cfg.sample_freq_hz: %u", (unsigned)dig_cfg.sample_freq_hz);
432 LOGI("dig_cfg.conv_mode: %u (1: unit 1, 2: unit 2, 3: both)", dig_cfg.conv_mode);
433 LOGI("dig_cfg.format: %u (0 is type1: [12bit data, 4bit channel])", dig_cfg.format);
434 for (int i = 0; i < cfg.channels; i++) {
435 LOGI("dig_cfg.adc_pattern[%d].atten: %u", i, dig_cfg.adc_pattern[i].atten);
436 LOGI("dig_cfg.adc_pattern[%d].channel: %u", i, dig_cfg.adc_pattern[i].channel);
437 LOGI("dig_cfg.adc_pattern[%d].unit: %u", i, dig_cfg.adc_pattern[i].unit);
438 LOGI("dig_cfg.adc_pattern[%d].bit_width: %u", i, dig_cfg.adc_pattern[i].bit_width);
439 }
440
441 // Initialize ADC
443 if (err != ESP_OK) {
444 LOGE("adc_continuous_config unsuccessful with error: %d", err);
445 cleanup_rx();
446 return false;
447 }
448 LOGI("adc_continuous_config successful");
449
450 // Set up optional calibration
451 if (!setupADCCalibration()) {
452 cleanup_rx();
453 return false;
454 }
455
456 // Attach the pins to the ADC unit
457#ifdef ARDUINO
458 for (int i = 0; i < cfg.channels; i++) {
459 adc_channel = cfg.adc_channels[i];
461 // perimanSetPinBus: uint8_t pin, peripheral_bus_type_t type, void * bus, int8_t bus_num, int8_t bus_channel
462 if (!perimanSetPinBus(io_pin, ESP32_BUS_TYPE_ADC_CONT, (void *)(cfg.adc_unit + 1), cfg.adc_unit, adc_channel)) {
463 LOGE("perimanSetPinBus to Continuous an ADC Unit %u failed!", cfg.adc_unit);
464 cleanup_rx();
465 return false;
466 }
467 rx_pins_attached = i + 1;
468 }
469#endif
470
471 // Start ADC
473 if (err != ESP_OK) {
474 LOGE("adc_continuous_start unsuccessful with error: %d", err);
475 cleanup_rx();
476 return false;
477 }
478 rx_started = true;
479
480 // Setup up optimal auto center which puts the avg at 0
482
483 LOGI("Setup ADC successful");
484
485 return true;
486 }
487
489 bool cleanup_tx() {
490 bool ok = true;
491#ifdef HAS_ESP32_DAC
492 if (dac_handle==nullptr) return true;
494 ok = false;
495 LOGE("dac_continuous_disable failed");
496 }
498 ok = false;
499 LOGE("dac_continuous_del_channels failed");
500 }
501 dac_handle = nullptr;
502#endif
503 return ok;
504 }
505
506#ifdef ARDUINO
507 // dummy detach: w/o this it's failing
508 static bool adcDetachBus(void *bus) {
509 LOGD("===> adcDetachBus: %d", (int) bus);
510 return true;
511 }
512#endif
513
515 bool cleanup_rx() {
516 bool ok = true;
517 if (adc_handle != nullptr && rx_started) {
519 LOGE("adc_continuous_stop failed");
520 ok = false;
521 }
522 rx_started = false;
523 }
524 if (adc_handle != nullptr) {
526 LOGE("adc_continuous_deinit failed");
527 ok = false;
528 }
529 adc_handle = nullptr;
530 }
531
533
534#ifdef ARDUINO
536#endif
537 return ok;
538 }
539
544 LOGE("adc bit width: %u cannot be set, range: %u to %u", cfg.adc_bit_width,
546 return false;
547 }
548 LOGI("adc bit width: %u, range: %u to %u", cfg.adc_bit_width,
550 return true;
551 }
552
555 int io_pin;
556 adc_channel_t adc_channel;
557
558 int max_channels = sizeof(cfg.adc_channels) / sizeof(adc_channel_t);
559 if (cfg.channels > max_channels) {
560 LOGE("number of channels: %d, max: %d", cfg.channels, max_channels);
561 return false;
562 }
563 LOGI("channels: %d, max: %d", cfg.channels, max_channels);
564
565 // Lets make sure the adc channels are available
566 for (int i = 0; i < cfg.channels; i++) {
567 adc_channel = cfg.adc_channels[i];
568 auto err = adc_continuous_channel_to_io(cfg.adc_unit, adc_channel, &io_pin);
569 if (err != ESP_OK) {
570 LOGE("ADC channel %u is not available on ADC unit %u", adc_channel, cfg.adc_unit);
571 return false;
572 } else {
573 LOGI("ADC channel %u is on pin %u", adc_channel, io_pin);
574 }
575 }
576 return true;
577 }
578
581 LOGI("sample rate (audio): %d", cfg.sample_rate);
582 int sample_rate = cfg.sample_rate * cfg.channels;
583 if ((sample_rate < SOC_ADC_SAMPLE_FREQ_THRES_LOW) ||
584 (sample_rate > SOC_ADC_SAMPLE_FREQ_THRES_HIGH)) {
585 LOGE("sample rate eff: %u can not be set, range: %u to %u", sample_rate,
587 return false;
588 }
589 LOGI("sample rate eff: %u, range: %u to %u", sample_rate,
591
592 return true;
593 }
594
597 int supported_bits = 16; // for the time being we support only 16 bits!
598
599 // calculated default value if nothing is specified
600 if (cfg.bits_per_sample == 0) {
602 LOGI("bits per sample set to: %d", cfg.bits_per_sample);
603 }
604
605 // check bits_per_sample
607 LOGE("bits per sample: error. It should be: %d but is %d",
609 return false;
610 }
611 LOGI("bits per sample: %d", cfg.bits_per_sample);
612 return true;
613 }
614
618 return true;
619
620 // Initialize ADC calibration handle
621 // Calibration is applied to an ADC unit (not per channel).
622
623 // setup calibration only when requested
624 if (adc_cali_handle_active && adc_cali_handle != nullptr) {
625 return true;
626 }
627
628 if (adc_cali_handle == NULL) {
629 #if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
630 // curve fitting is preferred
632 cali_config.unit_id = cfg.adc_unit;
636 #elif !defined(CONFIG_IDF_TARGET_ESP32H2)
637 // line fitting is the alternative
639 cali_config.unit_id = cfg.adc_unit;
643 #endif
644 if (err != ESP_OK) {
645 adc_cali_handle = nullptr;
647 LOGE("creating calibration handle failed for ADC%d with atten %d and bitwidth %d",
649 return false;
650 } else {
652 LOGI("enabled calibration for ADC%d with atten %d and bitwidth %d",
654 }
655 }
656 return true;
657 }
658
659};
660
663
664} // namespace audio_tools
665
666#endif
#define TRACEI()
Definition AudioLoggerIDF.h:32
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define assert(T)
Definition avr.h:10
ESP32 specific configuration for i2s input via adc using the adc_continuous API.
Definition AnalogConfigESP32V1.h:125
int buffer_count
Definition AnalogConfigESP32V1.h:132
uint8_t adc_bit_width
Definition AnalogConfigESP32V1.h:153
RxTxMode rx_tx_mode
Definition AnalogConfigESP32V1.h:137
adc_digi_convert_mode_t adc_conversion_mode
Definition AnalogConfigESP32V1.h:150
bool is_auto_center_read
Definition AnalogConfigESP32V1.h:149
TickType_t timeout
Definition AnalogConfigESP32V1.h:138
adc_unit_t adc_unit
Definition AnalogConfigESP32V1.h:155
uint8_t adc_attenuation
Definition AnalogConfigESP32V1.h:152
adc_channel_t adc_channels[NUM_ADC_CHANNELS]
Definition AnalogConfigESP32V1.h:156
bool adc_calibration_active
Definition AnalogConfigESP32V1.h:148
adc_digi_output_format_t adc_output_type
Definition AnalogConfigESP32V1.h:151
int buffer_size
Definition AnalogConfigESP32V1.h:136
Definition AnalogDriverBase.h:9
Definition AnalogDriverESP32V2.h:160
int getMinSamplesForAllChannels(int channels)
Definition AnalogDriverESP32V2.h:291
int16_t getCalibratedValue(ADC_DATA_TYPE sample_value)
Definition AnalogDriverESP32V2.h:279
size_t write(const uint8_t *src, size_t size_bytes) override
Write int16_t data to the Digital to Analog Converter.
Definition AnalogDriverESP32V2.h:165
IO16Bit(AnalogDriverESP32V2 *driver)
Definition AnalogDriverESP32V2.h:162
int getChannelIdx(ADC_CHANNEL_TYPE chan_num)
Definition AnalogDriverESP32V2.h:270
size_t readBytes(uint8_t *dest, size_t size_bytes)
Read int16_t data from ADC.
Definition AnalogDriverESP32V2.h:189
AnalogDriverESP32V2 * self
Definition AnalogDriverESP32V2.h:266
Vector< int > sampleIndex
Definition AnalogDriverESP32V2.h:268
Vector< uint8_t > result_data
Definition AnalogDriverESP32V2.h:267
AnalogAudioStream: A very fast DAC using DMA using the new dac_continuous API. This implementation as...
Definition AnalogDriverESP32V2.h:29
bool active
Definition AnalogDriverESP32V2.h:115
bool checkADCBitWidth()
Definition AnalogDriverESP32V2.h:541
bool active_tx
Definition AnalogDriverESP32V2.h:116
bool rx_started
Definition AnalogDriverESP32V2.h:118
virtual ~AnalogDriverESP32V2()
Destructor.
Definition AnalogDriverESP32V2.h:35
adc_cali_handle_t adc_cali_handle
Definition AnalogDriverESP32V2.h:112
bool setup_tx()
Definition AnalogDriverESP32V2.h:330
size_t write(const uint8_t *src, size_t size_bytes) override
Definition AnalogDriverESP32V2.h:89
bool active_rx
Definition AnalogDriverESP32V2.h:117
size_t readBytes(uint8_t *dest, size_t size_bytes) override
Definition AnalogDriverESP32V2.h:97
adc_continuous_handle_t adc_handle
Definition AnalogDriverESP32V2.h:111
bool setup_rx()
Definition AnalogDriverESP32V2.h:338
AnalogConfigESP32V1 cfg
Definition AnalogDriverESP32V2.h:114
bool checkADCChannels()
Definition AnalogDriverESP32V2.h:554
void end() override
Definition AnalogDriverESP32V2.h:71
int available() override
Definition AnalogDriverESP32V2.h:105
bool cleanup_tx()
Cleanup dac.
Definition AnalogDriverESP32V2.h:489
bool adc_cali_handle_active
Definition AnalogDriverESP32V2.h:113
bool cleanup_rx()
Cleanup Analog to Digital Converter.
Definition AnalogDriverESP32V2.h:515
int rx_pins_attached
Definition AnalogDriverESP32V2.h:119
bool begin(AnalogConfigESP32V1 cfg)
Definition AnalogDriverESP32V2.h:41
bool setupADCCalibration()
Definition AnalogDriverESP32V2.h:616
void cleanupADCCalibration()
Definition AnalogDriverESP32V2.h:129
bool checkADCSampleRate()
Definition AnalogDriverESP32V2.h:580
NumberFormatConverterStream converter
Definition AnalogDriverESP32V2.h:303
bool checkADCBitsPerSample()
Definition AnalogDriverESP32V2.h:596
ConverterAutoCenter auto_center
Definition AnalogDriverESP32V2.h:120
int configuredRxBytes() const
Definition AnalogDriverESP32V2.h:125
AnalogDriverESP32V2()=default
Default constructor.
Base class for all Audio Streams. It support the boolean operator to test if the object is ready with...
Definition BaseStream.h:120
Makes sure that the avg of the signal is set to 0.
Definition BaseConverter.h:199
bool begin(AudioInfo info, bool isDynamic=false)
Definition BaseConverter.h:219
size_t convert(uint8_t *src, size_t size) override
Definition BaseConverter.h:255
Converter which converts between bits_per_sample and 16 bits. The templated NumberFormatConverterStre...
Definition AudioStreamsConverter.h:471
virtual size_t write(const uint8_t *data, size_t len) override
Definition AudioStreamsConverter.h:563
bool begin(AudioInfo info, AudioInfo to, float gain=1.0f)
Definition AudioStreamsConverter.h:493
void end() override
Definition AudioStreamsConverter.h:514
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
bool resize(size_t newSize, T value)
Definition Vector.h:266
T * data()
Definition Vector.h:316
int size()
Definition Vector.h:178
@ TX_MODE
Definition AudioTypes.h:26
@ RX_MODE
Definition AudioTypes.h:26
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
AnalogDriverArduino AnalogDriver
AnalogAudioStream.
Definition AnalogDriverArduino.h:48
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:53
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:55
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:57