Skip to content

Commit

Permalink
Cleanup Analog Config
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Oct 12, 2023
1 parent 6a1ae1b commit 7226c7d
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 130 deletions.
130 changes: 4 additions & 126 deletions src/AudioAnalog/AnalogAudioBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,135 +2,13 @@
#include "AudioConfig.h"
#if defined(USE_ANALOG)
#if defined(ESP32)
# if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0 , 0)
# include "driver/i2s.h"
# include "driver/adc.h"
# include "soc/dac_channel.h"
# include "soc/adc_channel.h"
# else
# include "esp_adc/adc_continuous.h"
# endif
#endif

#if CONFIG_IDF_TARGET_ESP32
#define ADC_CONV_MODE ADC_CONV_SINGLE_UNIT_1 //ESP32 only supports ADC1 DMA mode
#define ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE1
#elif CONFIG_IDF_TARGET_ESP32S2
#define ADC_CONV_MODE ADC_CONV_BOTH_UNIT
#define ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE2
#elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C2
#define ADC_CONV_MODE ADC_CONV_ALTER_UNIT //ESP32C3 only supports alter mode
#define ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE2
#elif CONFIG_IDF_TARGET_ESP32S3
#define ADC_CONV_MODE ADC_CONV_BOTH_UNIT
#define ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE2
#endif



namespace audio_tools {

/**
* @brief ESP32 specific configuration for i2s input via adc. The default input pin is GPIO34. We always use int16_t values. The default
* output pins are GPIO25 and GPIO26!
*
* @author Phil Schatzmann
* @copyright GPLv3
*/
class AnalogConfig : public AudioInfo {
public:
int buffer_count = PWM_BUFFER_COUNT;
int buffer_size = PWM_BUFFER_SIZE;
RxTxMode rx_tx_mode;
bool is_blocking_write = true;
bool is_auto_center_read = true;

#if defined(ESP32) && defined(USE_ANALOG)
// allow ADC to access the protected methods
friend class AnalogDriverESP32;
bool use_apll = false;

// public config parameters
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0 , 0)
int port_no = I2S_NUM_0; // Analog input and output only supports 0!
bool auto_clear = I2S_AUTO_CLEAR;
bool uninstall_driver_on_end = true;
int mode_internal;
int adc_pin;
# include "AnalogConfigESP32.h"
# include "AnalogConfigESP32V1.h"
#else
int adc_conversion_mode = ADC_CONV_MODE;
int adc_output_type = ADC_OUTPUT_TYPE;
int adc_attenuation = ADC_ATTEN_DB_0;
int adc_bit_width = SOC_ADC_DIGI_MAX_BITWIDTH;
#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C2
adc_channel_t adc_channels[3] = {ADC_CHANNEL_2, ADC_CHANNEL_3, (ADC_CHANNEL_0 | 1 << 3)};
#endif
#if CONFIG_IDF_TARGET_ESP32S2
adc_channel_t adc_channels[3] = {ADC_CHANNEL_2, ADC_CHANNEL_3, (ADC_CHANNEL_0 | 1 << 3)};
#endif
#if CONFIG_IDF_TARGET_ESP32
adc_channel_t adc_channels[1] = {ADC_CHANNEL_7};
#endif
#endif

/// Default constructor
AnalogConfig(RxTxMode rxtxMode=TX_MODE) {
sample_rate = 44100;
bits_per_sample = 16;
channels = 2;
rx_tx_mode = rxtxMode;
if (rx_tx_mode == RX_MODE) {
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0 , 0)
mode_internal = (I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN);
adc_pin = PIN_ADC1;
auto_clear = false;
#endif
LOGI("I2S_MODE_ADC_BUILT_IN");
} else {
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0 , 0)
mode_internal = (I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN);
#endif
LOGI("I2S_MODE_DAC_BUILT_IN");
}
}

/// Copy constructor
AnalogConfig(const AnalogConfig &cfg) = default;

void logInfo() {
AudioInfo::logInfo();
if (rx_tx_mode == TX_MODE){
LOGI("analog left output pin: %d", 25);
LOGI("analog right output pin: %d", 26);
}
}

#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0 , 0)
/// Defines an alternative input pin (for the left channel)
void setInputPin1(int pin){
this->adc_pin = pin;
}
# include "AnalogConfigStd.h"
#endif

#else

AnalogConfig() {
sample_rate = 44100;
bits_per_sample = 16;
channels = 2;
buffer_size = ANALOG_BUFFER_SIZE;
buffer_count = ANALOG_BUFFERS;
rx_tx_mode = RX_MODE;
}
/// Default constructor
AnalogConfig(RxTxMode rxtxMode) : AnalogConfig() {
rx_tx_mode = rxtxMode;
}
int start_pin = PIN_ANALOG_START;

#endif

};
namespace audio_tools {

class AnalogDriverBase {
public:
Expand Down
4 changes: 2 additions & 2 deletions src/AudioAnalog/AnalogAudioESP32.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class AnalogDriverESP32 : public AnalogDriverBase {
}

/// starts the DAC
bool begin(AnalogConfig cfg) {
bool begin(AnalogConfigESP32 cfg) {
TRACEI();
cfg.logInfo();

Expand Down Expand Up @@ -203,7 +203,7 @@ class AnalogDriverESP32 : public AnalogDriverBase {
}

protected:
AnalogConfig adc_config;
AnalogConfigESP32 adc_config;
ConverterAutoCenter auto_center;
i2s_port_t port_no;
bool active = false;
Expand Down
4 changes: 2 additions & 2 deletions src/AudioAnalog/AnalogAudioESP32V1.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AnalogDriverESP32V1 : public AnalogDriverBase {
virtual ~AnalogDriverESP32V1() { end(); }

/// starts the DAC
bool begin(AnalogConfig cfg) {
bool begin(AnalogConfigESP32V1 cfg) {
TRACEI();
bool result = true;
this->cfg = cfg;
Expand Down Expand Up @@ -98,7 +98,7 @@ class AnalogDriverESP32V1 : public AnalogDriverBase {
protected:
dac_continuous_handle_t dac_handle;
adc_continuous_handle_t adc_handle;
AnalogConfig cfg;
AnalogConfigESP32V1 cfg;
bool active = false;
bool active_tx = false;
bool active_rx = false;
Expand Down
78 changes: 78 additions & 0 deletions src/AudioAnalog/AnalogConfigESP32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#pragma once

#include "AudioConfig.h"
#if defined(USE_ANALOG) && defined(ESP32) && ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0 , 0)

# include "driver/i2s.h"
# include "driver/adc.h"
# include "soc/dac_channel.h"
# include "soc/adc_channel.h"

namespace audio_tools {

/**
* @brief ESP32 specific configuration for i2s input via adc. The default input pin is GPIO34. We always use int16_t values. The default
* output pins are GPIO25 and GPIO26!
*
* @author Phil Schatzmann
* @copyright GPLv3
*/
class AnalogConfigESP32 : public AudioInfo {
public:
int buffer_count = PWM_BUFFER_COUNT;
int buffer_size = PWM_BUFFER_SIZE;
RxTxMode rx_tx_mode;
bool is_blocking_write = true;
bool is_auto_center_read = true;

// allow ADC to access the protected methods
friend class AnalogDriverESP32;
bool use_apll = false;

// public config parameters
int port_no = I2S_NUM_0; // Analog input and output only supports 0!
bool auto_clear = I2S_AUTO_CLEAR;
bool uninstall_driver_on_end = true;
int mode_internal;
int adc_pin;

/// Default constructor
AnalogConfigESP32(RxTxMode rxtxMode=TX_MODE) {
sample_rate = 44100;
bits_per_sample = 16;
channels = 2;
rx_tx_mode = rxtxMode;
if (rx_tx_mode == RX_MODE) {
mode_internal = (I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN);
adc_pin = PIN_ADC1;
auto_clear = false;
LOGI("I2S_MODE_ADC_BUILT_IN");
} else {
mode_internal = (I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN);
LOGI("I2S_MODE_DAC_BUILT_IN");
}
}

/// Copy constructor
AnalogConfigESP32(const AnalogConfigESP32 &cfg) = default;

void logInfo() {
AudioInfo::logInfo();
if (rx_tx_mode == TX_MODE){
LOGI("analog left output pin: %d", 25);
LOGI("analog right output pin: %d", 26);
}
}

/// Defines an alternative input pin (for the left channel)
void setInputPin1(int pin){
this->adc_pin = pin;
}

};

using AnalogConfig = AnalogConfigESP32;


} // ns
#endif
87 changes: 87 additions & 0 deletions src/AudioAnalog/AnalogConfigESP32V1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#pragma once

#include "AudioConfig.h"
#if defined(USE_ANALOG) && defined(ESP32) && ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0 , 0)

# include "esp_adc/adc_continuous.h"

#if CONFIG_IDF_TARGET_ESP32
#define ADC_CONV_MODE ADC_CONV_SINGLE_UNIT_1 //ESP32 only supports ADC1 DMA mode
#define ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE1
#elif CONFIG_IDF_TARGET_ESP32S2
#define ADC_CONV_MODE ADC_CONV_BOTH_UNIT
#define ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE2
#elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C2
#define ADC_CONV_MODE ADC_CONV_ALTER_UNIT //ESP32C3 only supports alter mode
#define ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE2
#elif CONFIG_IDF_TARGET_ESP32S3
#define ADC_CONV_MODE ADC_CONV_BOTH_UNIT
#define ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE2
#endif

namespace audio_tools {

/**
* @brief ESP32 specific configuration for i2s input via adc using the
* adc_continuous API
*
* @author Phil Schatzmann
* @copyright GPLv3
*/
class AnalogConfigESP32V1 : public AudioInfo {
public:
int buffer_count = PWM_BUFFER_COUNT;
int buffer_size = PWM_BUFFER_SIZE;
RxTxMode rx_tx_mode;
bool is_blocking_write = true;
bool is_auto_center_read = true;

// allow ADC to access the protected methods
friend class AnalogDriverESP32;
bool use_apll = false;

// public config parameters
int adc_conversion_mode = ADC_CONV_MODE;
int adc_output_type = ADC_OUTPUT_TYPE;
int adc_attenuation = ADC_ATTEN_DB_0;
int adc_bit_width = SOC_ADC_DIGI_MAX_BITWIDTH;
#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C2
adc_channel_t adc_channels[3] = {ADC_CHANNEL_2, ADC_CHANNEL_3, (ADC_CHANNEL_0 | 1 << 3)};
#endif
#if CONFIG_IDF_TARGET_ESP32S2
adc_channel_t adc_channels[3] = {ADC_CHANNEL_2, ADC_CHANNEL_3, (ADC_CHANNEL_0 | 1 << 3)};
#endif
#if CONFIG_IDF_TARGET_ESP32
adc_channel_t adc_channels[1] = {ADC_CHANNEL_7};
#endif

/// Default constructor
AnalogConfigESP32V1(RxTxMode rxtxMode=TX_MODE) {
sample_rate = 44100;
bits_per_sample = 16;
channels = 2;
rx_tx_mode = rxtxMode;
if (rx_tx_mode == RX_MODE) {
LOGI("I2S_MODE_ADC_BUILT_IN");
} else {
LOGI("I2S_MODE_DAC_BUILT_IN");
}
}

/// Copy constructor
AnalogConfigESP32V1(const AnalogConfigESP32V1 &cfg) = default;

void logInfo() {
AudioInfo::logInfo();
if (rx_tx_mode == TX_MODE){
LOGI("analog left output pin: %d", 25);
LOGI("analog right output pin: %d", 26);
}
}
};

using AnalogConfig = AnalogConfigESP32V1;


} // ns
#endif
43 changes: 43 additions & 0 deletions src/AudioAnalog/AnalogConfigStd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once
#include "AudioConfig.h"
#if defined(USE_ANALOG)

namespace audio_tools {

/**
* @brief Generic ADC and DAC configuration
*
* @author Phil Schatzmann
* @copyright GPLv3
*/
class AnalogConfigStd : public AudioInfo {
public:
int buffer_count = PWM_BUFFER_COUNT;
int buffer_size = PWM_BUFFER_SIZE;
RxTxMode rx_tx_mode;
bool is_blocking_write = true;
bool is_auto_center_read = true;

/// Copy constructor
AnalogConfigStd(const AnalogConfigStd &cfg) = default;

AnalogConfigStd() {
sample_rate = 44100;
bits_per_sample = 16;
channels = 2;
buffer_size = ANALOG_BUFFER_SIZE;
buffer_count = ANALOG_BUFFERS;
rx_tx_mode = RX_MODE;
}
/// Default constructor
AnalogConfigStd(RxTxMode rxtxMode) : AnalogConfig() {
rx_tx_mode = rxtxMode;
}
int start_pin = PIN_ANALOG_START;

};

using AnalogConfig = AnalogConfigStd;

} // ns
#endif

0 comments on commit 7226c7d

Please sign in to comment.