3 #include "AudioConfig.h"
4 #if defined(ESP32) && defined(USE_ANALOG) && ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0 , 0) || defined(DOXYGEN)
5 #include "AudioTools/CoreAudio/AudioAnalog/AnalogDriverBase.h"
6 #include "driver/i2s.h"
7 #include "driver/adc.h"
8 #include "soc/dac_channel.h"
9 #include "soc/adc_channel.h"
11 #include "AudioTools/CoreAudio/AudioStreams.h"
17 static inline uint16_t convert8DAC(int64_t value,
int value_bits_per_sample){
18 uint16_t result = value;
19 if (value_bits_per_sample!=16){
50 if (adc_config.is_auto_center_read){
55 if (!is_driver_installed){
56 port_no = (i2s_port_t) cfg.port_no;
59 i2s_config_t i2s_config = {
60 .mode = (i2s_mode_t)cfg.mode_internal,
61 .sample_rate = (eps32_i2s_sample_rate_type)cfg.
sample_rate,
63 .channel_format = (cfg.
channels == 1 && cfg.rx_tx_mode == RX_MODE) ? I2S_CHANNEL_FMT_ONLY_LEFT :I2S_CHANNEL_FMT_RIGHT_LEFT,
64 .communication_format = (i2s_comm_format_t)0,
65 .intr_alloc_flags = 0,
66 .dma_buf_count = cfg.buffer_count,
67 .dma_buf_len = cfg.buffer_size,
68 .use_apll = cfg.use_apll,
69 .tx_desc_auto_clear = cfg.auto_clear,
70 #if ESP_IDF_VERSION_MAJOR >= 4
72 .mclk_multiple = I2S_MCLK_MULTIPLE_DEFAULT,
73 .bits_per_chan = I2S_BITS_PER_CHAN_DEFAULT,
78 if (i2s_driver_install(port_no, &i2s_config, 0,
nullptr)!=ESP_OK){
79 LOGE(
"%s - %s", __func__,
"i2s_driver_install");
84 is_driver_installed =
true;
87 if (i2s_zero_dma_buffer(port_no)!=ESP_OK) {
88 LOGE(
"%s - %s", __func__,
"i2s_zero_dma_buffer");
92 switch (cfg.rx_tx_mode) {
98 if (i2s_set_adc_mode(adc_unit, adc_channel)!=ESP_OK) {
99 LOGE(
"%s - %s", __func__,
"i2s_driver_install");
104 if (i2s_adc_enable(port_no)!=ESP_OK) {
105 LOGE(
"%s - %s", __func__,
"i2s_adc_enable");
113 if (i2s_set_pin(port_no,
nullptr) != ESP_OK) LOGE(
"i2s_set_pin");
114 if (i2s_set_dac_mode( I2S_DAC_CHANNEL_BOTH_EN) != ESP_OK) LOGE(
"i2s_set_dac_mode");
115 if (i2s_set_sample_rates(port_no, cfg.
sample_rate) != ESP_OK) LOGE(
"i2s_set_sample_rates");
121 LOGE(
"Unsupported MODE: %d", cfg.rx_tx_mode);
128 if (adc_config.rx_tx_mode == RX_MODE){
129 i2s_adc_enable(port_no);
140 i2s_zero_dma_buffer(port_no);
144 if (adc_config.rx_tx_mode == RX_MODE){
145 i2s_adc_disable(port_no);
147 if (adc_config.uninstall_driver_on_end){
148 i2s_driver_uninstall(port_no);
149 is_driver_installed =
false;
166 size_t write(
const uint8_t *src,
size_t size_bytes)
override {
170 if (size_bytes>0 && src!=
nullptr){
171 switch(adc_config.channels){
173 result = outputMono(src, size_bytes);
176 result = outputStereo(src, size_bytes);
179 LOGE(
"Unsupported number of channels: %d", adc_config.channels);
182 LOGD(
"converted write size: %d",result);
187 size_t readBytes(uint8_t *dest,
size_t size_bytes)
override {
190 if (i2s_read(port_no, dest, size_bytes, &result, portMAX_DELAY)!=ESP_OK){
194 if (adc_config.is_auto_center_read){
195 auto_center.convert(dest, result);
197 LOGD(
"%s - len: %d -> %d", __func__, size_bytes, result);
201 virtual int available()
override {
202 return active ? adc_config.buffer_size*adc_config.buffer_count : 0;
206 AnalogConfigESP32 adc_config;
207 ConverterAutoCenter auto_center;
210 bool is_driver_installed =
false;
215 adc1_channel_t adc_channel;
223 adc_unit = ADC_UNIT_1;
224 adc_channel = ADC1_GPIO32_CHANNEL;
227 adc_unit = ADC_UNIT_1;
228 adc_channel = ADC1_GPIO33_CHANNEL;
231 adc_unit = ADC_UNIT_1;
232 adc_channel = ADC1_GPIO34_CHANNEL;
235 adc_unit = ADC_UNIT_1;
236 adc_channel = ADC1_GPIO35_CHANNEL;
239 adc_unit = ADC_UNIT_1;
240 adc_channel = ADC1_GPIO36_CHANNEL;
243 adc_unit = ADC_UNIT_1;
244 adc_channel = ADC1_GPIO37_CHANNEL;
247 adc_unit = ADC_UNIT_1;
248 adc_channel = ADC1_GPIO38_CHANNEL;
251 adc_unit = ADC_UNIT_1;
252 adc_channel = ADC1_GPIO39_CHANNEL;
256 LOGE(
"%s - pin GPIO%d is not supported", __func__,gpio);
261 size_t outputStereo(
const void *src,
size_t size_bytes) {
263 size_t output_size = 0;
265 uint16_t *dst = (uint16_t *)src;
266 switch(adc_config.bits_per_sample){
268 int16_t *data=(int16_t *)src;
269 output_size = size_bytes;
270 for (
int j=0;j<size_bytes/2;j++){
271 dst[j] = convert8DAC(data[j], adc_config.bits_per_sample);
275 int24_t *data=(int24_t *)src;
276 output_size = (size_bytes/3) * 2;
277 for (
int j=0;j<size_bytes/3;j++){
278 dst[j] = (uint32_t)convert8DAC(data[j], adc_config.bits_per_sample);
282 int32_t *data=(int32_t *)src;
283 output_size = (size_bytes/4) * 2;
284 for (
int j=0;j<size_bytes/4;j++){
285 dst[j] = convert8DAC(data[j], adc_config.bits_per_sample);
291 if (i2s_write(port_no, src, output_size, &result, portMAX_DELAY)!=ESP_OK){
292 LOGE(
"%s: %d", LOG_METHOD, output_size);
296 LOGD(
"i2s_write %d -> %d bytes", size_bytes, result);
302 size_t outputMono(
const void *src,
size_t size_bytes) {
304 size_t output_size = 0;
306 size_t resultTotal = 0;
307 switch(adc_config.bits_per_sample){
309 int16_t *data=(int16_t *)src;
310 for (
int j=0;j<size_bytes/2;j++){
311 out[0] = convert8DAC(data[j], adc_config.bits_per_sample);
313 if (i2s_write(port_no, out, 4, &result, portMAX_DELAY)!=ESP_OK){
314 LOGE(
"%s: %d", LOG_METHOD, output_size);
316 resultTotal += result;
320 int24_t *data=(int24_t*)src;
321 for (
int j=0;j<size_bytes/3;j++){
322 out[0] = convert8DAC(data[j], adc_config.bits_per_sample);
324 if (i2s_write(port_no, out, 4, &result, portMAX_DELAY)!=ESP_OK){
325 LOGE(
"%s: %d", LOG_METHOD, output_size);
327 resultTotal += result;
331 int32_t *data=(int32_t *)src;
332 for (
int j=0;j<size_bytes/4;j++){
333 out[0] = convert8DAC(data[j], adc_config.bits_per_sample);
335 if (i2s_write(port_no, out, 4, &result, portMAX_DELAY)!=ESP_OK){
336 LOGE(
"%s: %d", LOG_METHOD, output_size);
338 resultTotal += result;
343 LOGD(
"i2s_write %d -> %d bytes", size_bytes, resultTotal);
void stop()
Public generic methods.
Definition: AudioRuntime.h:27