3 #include "AudioConfig.h"
5 #if defined(ESP32) && defined(USE_ANALOG) && \
6 ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) || defined(DOXYGEN)
9 #ifndef perimanClearPinBus
10 #define perimanClearPinBus(p) perimanSetPinBus(p, ESP32_BUS_TYPE_INIT, NULL)
14 #include "AudioTools/CoreAudio/AudioAnalog/AnalogDriverBase.h"
15 #include "AudioTools/CoreAudio/AudioAnalog/AnalogConfigESP32V1.h"
16 #include "AudioTools/CoreAudio/AudioStreams.h"
17 #include "AudioTools/CoreAudio/AudioStreamsConverter.h"
45 switch (cfg.rx_tx_mode) {
47 if (!setup_tx())
return false;
49 if (!converter.begin(cfg, 16)) {
56 if (!setup_rx())
return false;
88 size_t write(
const uint8_t *src,
size_t size_bytes)
override {
91 return converter.write(src, size_bytes);
96 size_t readBytes(uint8_t *dest,
size_t size_bytes)
override {
99 return io.readBytes(dest, size_bytes);
104 int available()
override {
105 return active_rx ? (uint32_t)(cfg.buffer_size *
sizeof(int16_t)) : 0;
117 FIFO() : size_(0), buffer_(
nullptr), head_(0), tail_(0), count_(0) {}
119 FIFO(
size_t size) : size_(size), buffer_(
new T[size]), head_(0), tail_(0), count_(0) {}
126 bool push(
const T& value) {
127 if (count_ < size_) {
128 buffer_[tail_] = value;
130 tail_ = (tail_ + 1) % size_;
141 value = buffer_[head_];
143 head_ = (head_ + 1) % size_;
152 size_t size()
const {
161 return count_ == size_;
178 adc_continuous_handle_t adc_handle =
nullptr;
179 adc_cali_handle_t adc_cali_handle =
nullptr;
182 bool active_tx =
false;
183 bool active_rx =
false;
186 dac_continuous_handle_t dac_handle =
nullptr;
200 size_t write(
const uint8_t *src,
size_t size_bytes)
override {
205 int16_t *data16 = (int16_t *)src;
206 uint8_t *data8 = (uint8_t *)src;
207 int samples = size_bytes / 2;
210 for (
int j = 0; j < samples; j++) {
211 data8[j] = (32768u + data16[j]) >> 8;
214 if (dac_continuous_write(self->dac_handle, data8, samples, &result, self->cfg.timeout) != ESP_OK) {
243 size_t readBytes(uint8_t *dest,
size_t size_bytes) {
246 size_t total_bytes = 0;
247 size_t bytes_provided = 0;
248 int min_samples_in_fifo_per_channel = 0;
249 int max_samples_in_fifo_per_channel = 0;
253 int samples_provided_per_channel = 0;
254 int data_milliVolts = 0;
256 int samples_requested = size_bytes /
sizeof(int16_t);
257 int samples_requested_per_channel = samples_requested/
self->cfg.
channels;
259 adc_digi_output_data_t* result_data = (adc_digi_output_data_t*)malloc(samples_requested *
sizeof(adc_digi_output_data_t));
260 if (result_data == NULL) {
261 LOGE(
"Failed to allocate memory for result_data");
264 memset(result_data, 0, samples_requested *
sizeof(adc_digi_output_data_t));
268 uint16_t *result16 = (uint16_t *)dest;
269 uint16_t *end = (uint16_t *)(dest + size_bytes);
273 if (adc_continuous_read(self->adc_handle, (uint8_t *)result_data, (uint32_t)(samples_requested *
sizeof(adc_digi_output_data_t)), &bytes_read, (uint32_t)
self->cfg.timeout) == ESP_OK) {
274 samples_read = bytes_read /
sizeof(adc_digi_output_data_t);
275 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)));
278 for (
int i = 0; i < samples_read; i++) {
279 adc_digi_output_data_t *p = &result_data[i];
280 ADC_CHANNEL_TYPE chan_num = AUDIO_ADC_GET_CHANNEL(p);
281 ADC_DATA_TYPE data = AUDIO_ADC_GET_DATA(p);
285 for (
int j = 0; j <
self->cfg.channels; ++j) {
286 if (self->cfg.adc_channels[j] == chan_num) {
293 if (self->fifo_buffers[idx]->push(data)) {
294 LOGD(
"Sample %d, FIFO %d, ch %u, d %u", i, idx, chan_num, data);
296 LOGE(
"Sample %d, FIFO buffer is full, ch %u, d %u", i, (
unsigned)chan_num, data);
299 LOGE(
"Sample %d, ch %u not found in configuration, d: %u", i, (
unsigned)chan_num, data);
300 for (
int k = 0; k <
self->cfg.channels; ++k) {
301 LOGE(
"Available config ch: %u", self->cfg.adc_channels[k]);
308 min_samples_in_fifo_per_channel =
self->fifo_buffers[0]->size();
309 for (
int i = 1; i <
self->cfg.channels; i++) {
310 fifo_size =
self->fifo_buffers[i]->size();
311 if (fifo_size < min_samples_in_fifo_per_channel) {
312 min_samples_in_fifo_per_channel = fifo_size;
317 while (samples_requested_per_channel > min_samples_in_fifo_per_channel) {
321 if (adc_continuous_read(self->adc_handle, (uint8_t *)result_data, (uint32_t)(2*self->cfg.
channels *
sizeof(adc_digi_output_data_t)), &bytes_read, (uint32_t)self->cfg.timeout) != ESP_OK) {
322 LOGE(
"Top off, adc_continuous_read unsuccessful");
327 samples_read = bytes_read /
sizeof(adc_digi_output_data_t);
328 LOGD(
"Top Off: Requested %d samples per Channel, Min samples in FIFO: %d, Read additional %d bytes / %d samples", samples_requested_per_channel, min_samples_in_fifo_per_channel, (
unsigned)bytes_read, samples_read);
330 for (
int i = 0; i < samples_read; i++) {
331 adc_digi_output_data_t *p = &result_data[i];
332 ADC_CHANNEL_TYPE chan_num = AUDIO_ADC_GET_CHANNEL(p);
333 ADC_DATA_TYPE data = AUDIO_ADC_GET_DATA(p);
337 for (
int j = 0; j <
self->cfg.channels; ++j) {
338 if (self->cfg.adc_channels[j] == chan_num) {
345 if (self->fifo_buffers[idx]->push(data)) {
346 LOGD(
"Top Off Sample %d, FIFO %d, ch %u, d %u", i, idx, chan_num, data);
348 LOGE(
"Top Off Sample %d, FIFO buffer is full, ch %u, d %u", i, chan_num, data);
351 LOGE(
"Top Off Sample %d, ch %u not found in configuration, d %u", i, chan_num, data);
352 for (
int k = 0; k <
self->cfg.channels; ++k) {
353 LOGE(
"Available config ch: %u", self->cfg.adc_channels[k]);
359 min_samples_in_fifo_per_channel =
self->fifo_buffers[0]->size();
360 max_samples_in_fifo_per_channel =
self->fifo_buffers[0]->size();
361 for (
int i = 1; i <
self->cfg.channels; i++) {
362 fifo_size =
self->fifo_buffers[i]->size();
363 if (fifo_size < min_samples_in_fifo_per_channel) {
364 min_samples_in_fifo_per_channel = fifo_size;
366 if (fifo_size > max_samples_in_fifo_per_channel) {
367 max_samples_in_fifo_per_channel = fifo_size;
370 LOGD(
"Min # of samples in FIFO: %d, Max # of samples in FIFO: %d", min_samples_in_fifo_per_channel, max_samples_in_fifo_per_channel);
374 if (samples_requested_per_channel <= min_samples_in_fifo_per_channel) {
375 LOGD(
"Going to copying %d samples of %d samples/channel to output buffer", samples_requested, samples_requested_per_channel);
376 samples_provided_per_channel = samples_requested_per_channel;
379 LOGE(
"Only %d samples per channel available for output buffer", min_samples_in_fifo_per_channel);
380 samples_provided_per_channel = min_samples_in_fifo_per_channel;
383 for (
int i = 0; i < samples_provided_per_channel; i++) {
384 for (
int j = 0; j <
self->cfg.channels; j++) {
386 self->fifo_buffers[j]->pop(data);
387 if (result16 < end) {
388 if (self->cfg.adc_calibration_active) {
390 auto err = adc_cali_raw_to_voltage(self->adc_cali_handle, (
int)data, &data_milliVolts);
392 *result16 =
static_cast<int16_t
>(data_milliVolts);
394 LOGE(
"adc_cali_raw_to_voltage error: %d", err);
402 LOGE(
"Buffer write overflow, skipping data");
407 bytes_provided = samples_provided_per_channel *
self->cfg.channels *
sizeof(int16_t);
410 if (self->cfg.is_auto_center_read) {
411 self->auto_center.convert(dest, bytes_provided);
415 LOGE(
"adc_continuous_read unsuccessful");
419 return bytes_provided;
433 dac_continuous_config_t cont_cfg = {
434 .chan_mask = cfg.
channels == 1 ? cfg.dac_mono_channel : DAC_CHANNEL_MASK_ALL,
435 .desc_num = (uint32_t)cfg.buffer_count,
436 .buf_size = (
size_t)cfg.buffer_size,
439 .clk_src = cfg.use_apll ? DAC_DIGI_CLK_SRC_APLL : DAC_DIGI_CLK_SRC_DEFAULT,
440 .chan_mode = DAC_CHANNEL_MODE_ALTER,
443 if (dac_continuous_new_channels(&cont_cfg, &dac_handle) != ESP_OK) {
444 LOGE(
"new_channels");
447 if (dac_continuous_enable(dac_handle) != ESP_OK) {
455 LOGE(
"DAC not supported");
463 adc_channel_t adc_channel;
473 if (adc_handle !=
nullptr) {
474 LOGE(
"adc unit %u continuous is already initialized. Please call end() first!", ADC_UNIT);
483 for (
int i = 0; i < cfg.
channels; i++) {
484 adc_channel = cfg.adc_channels[i];
485 adc_continuous_channel_to_io(ADC_UNIT, adc_channel, &io_pin);
486 if (!perimanClearPinBus(io_pin)) {
487 LOGE(
"perimanClearPinBus failed!");
495 uint32_t conv_frame_size = (uint32_t)cfg.buffer_size * SOC_ADC_DIGI_RESULT_BYTES;
496 #
if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
497 uint8_t calc_multiple = conv_frame_size % SOC_ADC_DIGI_DATA_BYTES_PER_CONV;
498 if (calc_multiple != 0) {
499 conv_frame_size = (uint32_t) (conv_frame_size + calc_multiple);
510 if (conv_frame_size > 4092) {
511 LOGE(
"buffer_size is too big. Please set lower buffer_size.");
514 LOGI(
"buffer_size: %u samples, conv_frame_size: %u bytes", cfg.buffer_size, (
unsigned)conv_frame_size);
518 adc_continuous_handle_cfg_t adc_config;
519 adc_config.max_store_buf_size = (uint32_t)conv_frame_size * 2;
520 adc_config.conv_frame_size = (uint32_t) conv_frame_size;
521 #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0)
522 adc_config.flags.flush_pool =
true;
524 err = adc_continuous_new_handle(&adc_config, &adc_handle);
526 LOGE(
"adc_continuous_new_handle failed with error: %d", err);
529 LOGI(
"adc_continuous_new_handle successful");
533 adc_digi_pattern_config_t adc_pattern[cfg.
channels] = {};
534 for (
int i = 0; i < cfg.
channels; i++) {
535 uint8_t ch = cfg.adc_channels[i];
536 adc_pattern[i].atten = (uint8_t) cfg.adc_attenuation;
537 adc_pattern[i].channel = (uint8_t)ch;
538 adc_pattern[i].unit = (uint8_t) ADC_UNIT;
539 adc_pattern[i].bit_width = (uint8_t) cfg.adc_bit_width;
543 adc_continuous_config_t dig_cfg = {
544 .pattern_num = (uint32_t) cfg.
channels,
545 .adc_pattern = adc_pattern,
547 .conv_mode = (adc_digi_convert_mode_t) cfg.adc_conversion_mode,
548 .format = (adc_digi_output_format_t) cfg.adc_output_type,
552 LOGI(
"dig_cfg.sample_freq_hz: %u", (
unsigned)dig_cfg.sample_freq_hz);
553 LOGI(
"dig_cfg.conv_mode: %u (1: unit 1, 2: unit 2, 3: both)", dig_cfg.conv_mode);
554 LOGI(
"dig_cfg.format: %u (0 is type1: [12bit data, 4bit channel])", dig_cfg.format);
555 for (
int i = 0; i < cfg.
channels; i++) {
556 LOGI(
"dig_cfg.adc_pattern[%d].atten: %u", i, dig_cfg.adc_pattern[i].atten);
557 LOGI(
"dig_cfg.adc_pattern[%d].channel: %u", i, dig_cfg.adc_pattern[i].channel);
558 LOGI(
"dig_cfg.adc_pattern[%d].unit: %u", i, dig_cfg.adc_pattern[i].unit);
559 LOGI(
"dig_cfg.adc_pattern[%d].bit_width: %u", i, dig_cfg.adc_pattern[i].bit_width);
563 err = adc_continuous_config(adc_handle, &dig_cfg);
565 LOGE(
"adc_continuous_config unsuccessful with error: %d", err);
568 LOGI(
"adc_continuous_config successful");
577 for (
int i = 0; i < cfg.
channels; i++) {
578 adc_channel = cfg.adc_channels[i];
579 adc_continuous_channel_to_io(ADC_UNIT, adc_channel, &io_pin);
581 if (!perimanSetPinBus(io_pin, ESP32_BUS_TYPE_ADC_CONT, (
void *)(ADC_UNIT + 1), ADC_UNIT, adc_channel)) {
582 LOGE(
"perimanSetPinBus to Continuous an ADC Unit %u failed!", ADC_UNIT);
589 err = adc_continuous_start(adc_handle);
591 LOGE(
"adc_continuous_start unsuccessful with error: %d", err);
599 size_t fifo_size = (cfg.buffer_size / cfg.
channels) + 8;
600 fifo_buffers =
new FIFO<ADC_DATA_TYPE>*[cfg.
channels];
601 for (
int i = 0; i < cfg.
channels; ++i) {
602 fifo_buffers[i] =
new FIFO<ADC_DATA_TYPE>(fifo_size);
604 LOGI(
"%d FIFO buffers allocated of size %d", cfg.
channels, fifo_size);
606 LOGI(
"Setup ADC successful");
615 if (dac_handle==
nullptr)
return true;
616 if (dac_continuous_disable(dac_handle) != ESP_OK){
618 LOGE(
"dac_continuous_disable failed");
620 if (dac_continuous_del_channels(dac_handle) != ESP_OK){
622 LOGE(
"dac_continuous_del_channels failed");
624 dac_handle =
nullptr;
631 static bool adcDetachBus(
void *bus) {
632 LOGD(
"===> adcDetachBus: %d", bus);
639 if (adc_handle==
nullptr)
return true;
640 adc_continuous_stop(adc_handle);
641 adc_continuous_deinit(adc_handle);
642 if (cfg.adc_calibration_active) {
643 #if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
644 adc_cali_delete_scheme_curve_fitting(adc_cali_handle);
645 #elif !defined(CONFIG_IDF_TARGET_ESP32H2)
646 adc_cali_delete_scheme_line_fitting(adc_cali_handle);
651 if (fifo_buffers !=
nullptr) {
652 for (
int i = 0; i < cfg.
channels; ++i) {
653 delete fifo_buffers[i];
655 delete[] fifo_buffers;
656 fifo_buffers =
nullptr;
661 perimanSetBusDeinit(ESP32_BUS_TYPE_ADC_CONT, adcDetachBus);
662 for (
int i = 0; i < cfg.
channels; i++) {
663 adc_channel_t adc_channel = cfg.adc_channels[i];
665 adc_continuous_channel_to_io(ADC_UNIT, adc_channel, &io_pin);
666 if (perimanGetPinBusType(io_pin) == ESP32_BUS_TYPE_ADC_CONT) {
667 if (!perimanClearPinBus(io_pin)) {
668 LOGE(
"perimanClearPinBus failed!");
673 adc_handle =
nullptr;
679 if ((cfg.adc_bit_width < SOC_ADC_DIGI_MIN_BITWIDTH) ||
680 (cfg.adc_bit_width > SOC_ADC_DIGI_MAX_BITWIDTH)) {
681 LOGE(
"adc bit width: %u cannot be set, range: %u to %u", cfg.adc_bit_width,
682 (
unsigned)SOC_ADC_DIGI_MIN_BITWIDTH, (
unsigned)SOC_ADC_DIGI_MAX_BITWIDTH);
685 LOGI(
"adc bit width: %u, range: %u to %u", cfg.adc_bit_width,
686 (
unsigned)SOC_ADC_DIGI_MIN_BITWIDTH, (
unsigned)SOC_ADC_DIGI_MAX_BITWIDTH);
693 adc_channel_t adc_channel;
695 int max_channels =
sizeof(cfg.adc_channels) /
sizeof(adc_channel_t);
697 LOGE(
"number of channels: %d, max: %d", cfg.
channels, max_channels);
700 LOGI(
"channels: %d, max: %d", cfg.
channels, max_channels);
703 for (
int i = 0; i < cfg.
channels; i++) {
704 adc_channel = cfg.adc_channels[i];
705 auto err = adc_continuous_channel_to_io(ADC_UNIT, adc_channel, &io_pin);
707 LOGE(
"ADC channel %u is not available on ADC unit %u", adc_channel, ADC_UNIT);
710 LOGI(
"ADC channel %u is on pin %u", adc_channel, io_pin);
719 if ((sample_rate < SOC_ADC_SAMPLE_FREQ_THRES_LOW) ||
720 (sample_rate > SOC_ADC_SAMPLE_FREQ_THRES_HIGH)) {
721 LOGE(
"sample rate eff: %u can not be set, range: %u to %u", sample_rate,
722 SOC_ADC_SAMPLE_FREQ_THRES_LOW, SOC_ADC_SAMPLE_FREQ_THRES_HIGH);
725 LOGI(
"sample rate eff: %u, range: %u to %u", sample_rate,
726 SOC_ADC_SAMPLE_FREQ_THRES_LOW, SOC_ADC_SAMPLE_FREQ_THRES_HIGH);
733 int supported_bits = 16;
743 LOGE(
"bits per sample: error. It should be: %d but is %d",
753 if (!cfg.adc_calibration_active)
760 if (adc_cali_handle == NULL) {
761 #if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
763 adc_cali_curve_fitting_config_t cali_config;
764 cali_config.unit_id = ADC_UNIT;
765 cali_config.atten = (adc_atten_t)cfg.adc_attenuation;
766 cali_config.bitwidth = (adc_bitwidth_t)cfg.adc_bit_width;
767 auto err = adc_cali_create_scheme_curve_fitting(&cali_config, &adc_cali_handle);
768 #elif !defined(CONFIG_IDF_TARGET_ESP32H2)
770 adc_cali_line_fitting_config_t cali_config;
771 cali_config.unit_id = ADC_UNIT;
772 cali_config.atten = (adc_atten_t)cfg.adc_attenuation;
773 cali_config.bitwidth = (adc_bitwidth_t)cfg.adc_bit_width;
774 auto err = adc_cali_create_scheme_line_fitting(&cali_config, &adc_cali_handle);
777 LOGE(
"creating calibration handle failed for ADC%d with atten %d and bitwidth %d",
778 ADC_UNIT, cfg.adc_attenuation, cfg.adc_bit_width);
781 LOGI(
"enabled calibration for ADC%d with atten %d and bitwidth %d",
782 ADC_UNIT, cfg.adc_attenuation, cfg.adc_bit_width);