arduino-audio-tools
AnalogConfigESP32V1.h
1 #pragma once
2 
3 #include "AudioConfig.h"
4 #if defined(USE_ANALOG) && defined(ESP32) && \
5  ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) || \
6  defined(DOXYGEN)
7 
8 #include "esp_adc/adc_cali_scheme.h"
9 #include "esp_adc/adc_continuous.h"
10 #ifdef ARDUINO
11 #include "esp32-hal-periman.h"
12 #endif
13 
14 #if CONFIG_IDF_TARGET_ESP32
15 #define ADC_CONV_MODE ADC_CONV_SINGLE_UNIT_1
16 #define ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE1
17 #define ADC_CHANNELS \
18  {ADC_CHANNEL_0, ADC_CHANNEL_3, ADC_CHANNEL_4, \
19  ADC_CHANNEL_5, ADC_CHANNEL_6, ADC_CHANNEL_7}
20 #define NUM_ADC_CHANNELS 6
21 #define AUDIO_ADC_GET_CHANNEL(p_data) ((p_data)->type1.channel)
22 #define AUDIO_ADC_GET_DATA(p_data) ((p_data)->type1.data)
23 #define HAS_ESP32_DAC
24 #define ADC_CHANNEL_TYPE uint16_t
25 #define ADC_DATA_TYPE uint16_t
26 #elif CONFIG_IDF_TARGET_ESP32S2
27 #define ADC_CONV_MODE ADC_CONV_SINGLE_UNIT_1
28 #define ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE2
29 #define AUDIO_ADC_GET_CHANNEL(p_data) ((p_data)->type2.channel)
30 #define AUDIO_ADC_GET_DATA(p_data) ((p_data)->type2.data)
31 #define ADC_CHANNELS \
32  {ADC_CHANNEL_0, ADC_CHANNEL_1, ADC_CHANNEL_2, ADC_CHANNEL_3, ADC_CHANNEL_4, \
33  ADC_CHANNEL_5, ADC_CHANNEL_6, ADC_CHANNEL_7, ADC_CHANNEL_8, ADC_CHANNEL_9}
34 #define NUM_ADC_CHANNELS 10
35 #define HAS_ESP32_DAC
36 #define ADC_CHANNEL_TYPE uint16_t
37 #define ADC_DATA_TYPE uint16_t
38 #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2 || \
39  CONFIG_IDF_TARGET_ESP32C2 || CONFIG_IDF_TARGET_ESP32C6
40 #define ADC_CONV_MODE ADC_CONV_ALTER_UNIT // ESP32C3 only supports alter mode
41 #define ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE2
42 #define AUDIO_ADC_GET_CHANNEL(p_data) ((p_data)->type2.channel)
43 #define AUDIO_ADC_GET_DATA(p_data) ((p_data)->type2.data)
44 #define ADC_CHANNELS \
45  {ADC_CHANNEL_0, ADC_CHANNEL_1, ADC_CHANNEL_2, ADC_CHANNEL_3, ADC_CHANNEL_4}
46 #define NUM_ADC_CHANNELS 5
47 #define ADC_CHANNEL_TYPE uint32_t
48 #define ADC_DATA_TYPE uint32_t
49 #elif CONFIG_IDF_TARGET_ESP32C6
50 #define ADC_CONV_MODE ADC_CONV_ALTER_UNIT
51 #define ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE2
52 #define AUDIO_ADC_GET_CHANNEL(p_data) ((p_data)->type2.channel)
53 #define AUDIO_ADC_GET_DATA(p_data) ((p_data)->type2.data)
54 #define ADC_CHANNELS \
55  {ADC_CHANNEL_0, ADC_CHANNEL_1, ADC_CHANNEL_2, ADC_CHANNEL_3, \
56  ADC_CHANNEL_4, ADC_CHANNEL_5, ADC_CHANNEL_6}
57 #define NUM_ADC_CHANNELS 7
58 #define ADC_CHANNEL_TYPE uint32_t
59 #define ADC_DATA_TYPE uint32_t
60 #elif CONFIG_IDF_TARGET_ESP32S3
61 #define ADC_CONV_MODE ADC_CONV_SINGLE_UNIT_1
62 #define ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE2
63 #define AUDIO_ADC_GET_CHANNEL(p_data) ((p_data)->type2.channel)
64 #define AUDIO_ADC_GET_DATA(p_data) ((p_data)->type2.data)
65 #define ADC_CHANNELS \
66  {ADC_CHANNEL_0, ADC_CHANNEL_1, ADC_CHANNEL_2, ADC_CHANNEL_3, ADC_CHANNEL_4, \
67  ADC_CHANNEL_5, ADC_CHANNEL_6, ADC_CHANNEL_7, ADC_CHANNEL_8, ADC_CHANNEL_9}
68 #define NUM_ADC_CHANNELS 10
69 #define ADC_CHANNEL_TYPE uint32_t
70 #define ADC_DATA_TYPE uint32_t
71 #elif CONFIG_IDF_TARGET_ESP32P4
72 #define ADC_CONV_MODE ADC_CONV_SINGLE_UNIT_1
73 #define ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE2
74 #define AUDIO_ADC_GET_CHANNEL(p_data) ((p_data)->type2.channel)
75 #define AUDIO_ADC_GET_DATA(p_data) ((p_data)->type2.data)
76 #define ADC_CHANNELS \
77  {ADC_CHANNEL_0, ADC_CHANNEL_1, ADC_CHANNEL_2, ADC_CHANNEL_3, \
78  ADC_CHANNEL_4, ADC_CHANNEL_5, ADC_CHANNEL_6, ADC_CHANNEL_7}
79 #define NUM_ADC_CHANNELS 8
80 #define ADC_CHANNEL_TYPE uint32_t
81 #define ADC_DATA_TYPE uint32_t
82 #endif
83 
84 // continuous ADC API should run on ADC1
85 
86 #define ADC_UNIT ADC_UNIT_1
87 #ifdef HAS_ESP32_DAC
88 #include "driver/dac_continuous.h"
89 #endif
90 
91 namespace audio_tools {
92 
101  // allow ADC to access the protected methods
102  friend class AnalogDriverESP32;
103 
104  public:
105  int buffer_count = ANALOG_BUFFER_COUNT;
106  int buffer_size = ANALOG_BUFFER_SIZE;
107  RxTxMode rx_tx_mode;
108  TickType_t timeout = portMAX_DELAY;
109 
110 #ifdef HAS_ESP32_DAC
111  bool is_blocking_write = true;
112  bool use_apll = false;
114  dac_channel_mask_t dac_mono_channel = DAC_CHANNEL_MASK_CH0;
115 #endif
116 
117  // ADC config parameters
118  bool adc_calibration_active = false;
119  bool is_auto_center_read = false;
120  adc_digi_convert_mode_t adc_conversion_mode = ADC_CONV_MODE;
121  adc_digi_output_format_t adc_output_type = ADC_OUTPUT_TYPE;
122  uint8_t adc_attenuation = ADC_ATTEN_DB_12; // full voltage range of 3.9V
123  uint8_t adc_bit_width = SOC_ADC_DIGI_MAX_BITWIDTH;
124 
125  adc_channel_t adc_channels[NUM_ADC_CHANNELS] = ADC_CHANNELS;
126 
128  AnalogConfigESP32V1(RxTxMode rxtxMode = TX_MODE) {
129  sample_rate = 44100;
130  channels = 2;
131  bits_per_sample = 16;
132  rx_tx_mode = rxtxMode;
133  switch (rx_tx_mode) {
134  case RX_MODE: {
135  // make sure that the proposed sample rate is valid
136  if (sample_rate * channels > SOC_ADC_SAMPLE_FREQ_THRES_HIGH) {
137  sample_rate = SOC_ADC_SAMPLE_FREQ_THRES_HIGH / channels;
138  }
139  LOGI("I2S_MODE_ADC_BUILT_IN");
140  break;
141  }
142 #ifdef HAS_ESP32_DAC
143  case TX_MODE:
144  use_apll = true;
145  LOGI("I2S_MODE_DAC_BUILT_IN");
146  break;
147 #endif
148 
149  default:
150  LOGE("RxTxMode not supported: %d", rx_tx_mode);
151  break;
152  }
153  }
154 
157 
158  void logInfo() {
159  AudioInfo::logInfo();
160 #if !defined(ESP32X)
161  if (rx_tx_mode == TX_MODE) {
162  LOGI("analog left output pin: %d", 25);
163  LOGI("analog right output pin: %d", 26);
164  }
165 #endif
166 #ifdef HAS_ESP32_DAC
167  LOGI("use_apll: %d", use_apll);
168 #endif
169  }
170 };
171 
172 #ifndef ANALOG_CONFIG
173 #define ANALOG_CONFIG
174 using AnalogConfig = AnalogConfigESP32V1;
175 #endif
176 
177 } // namespace audio_tools
178 
179 #endif
ESP32 specific configuration for i2s input via adc using the adc_continuous API.
Definition: AnalogConfigESP32V1.h:100
AnalogConfigESP32V1(RxTxMode rxtxMode=TX_MODE)
Default constructor.
Definition: AnalogConfigESP32V1.h:128
AnalogConfigESP32V1(const AnalogConfigESP32V1 &cfg)=default
Copy constructor.
Please use AnalogAudioStream: A very fast ADC and DAC using the ESP32 I2S interface.
Definition: AnalogDriverESP32.h:35
RxTxMode
The Microcontroller is the Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Si...
Definition: AudioTypes.h:28
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823
Basic Audio information which drives e.g. I2S.
Definition: AudioTypes.h:52
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition: AudioTypes.h:55
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition: AudioTypes.h:57
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition: AudioTypes.h:59