Skip to content

Commit

Permalink
I2S Configuration Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Oct 23, 2023
1 parent 6df9ec2 commit 4fd7a65
Show file tree
Hide file tree
Showing 7 changed files with 362 additions and 163 deletions.
5 changes: 0 additions & 5 deletions src/AudioAnalog/AnalogAudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ class AnalogAudioStream : public AudioStream {
analog.end();
}

// /// Overides the sample rate and uses the max value which is around ~13MHz. Call this methd after begin();
// void setMaxSampleRate() {
// analog.setMaxSampleRate();
// }

AnalogConfig &config() {
return adc_config;
}
Expand Down
167 changes: 9 additions & 158 deletions src/AudioI2S/I2SConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,168 +2,19 @@
#include "AudioConfig.h"
#ifdef USE_I2S

#ifndef PIN_I2S_MCK
# define PIN_I2S_MCK -1
#endif

#if defined(ESP32) && ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0 , 0)
# include "driver/i2s.h"
#endif

#if defined(STM32) || defined(ARDUINO_ARCH_RP2040) || defined(USE_NANO33BLE)
# define I2S_BUFFER_SIZE_ONLY
#endif

namespace audio_tools {

}

/**
* @brief I2S Formats
*/
enum I2SFormat {
I2S_STD_FORMAT,
I2S_LSB_FORMAT,
I2S_MSB_FORMAT,
I2S_PHILIPS_FORMAT,
I2S_RIGHT_JUSTIFIED_FORMAT,
I2S_LEFT_JUSTIFIED_FORMAT,
I2S_PCM,
};

/**
* @brief I2S Signal Types: Digital, Analog, PDM
*/
enum I2SSignalType {
Digital,
Analog,
PDM,
TDM
};

INLINE_VAR const char* i2s_formats[] = {"I2S_STD_FORMAT","I2S_LSB_FORMAT","I2S_MSB_FORMAT","I2S_PHILIPS_FORMAT","I2S_RIGHT_JUSTIFIED_FORMAT","I2S_LEFT_JUSTIFIED_FORMAT","I2S_PCM"};
INLINE_VAR const char* i2s_signal_types[] = {"Digital","Analog","PDM","TDM"};


/**
* @brief configuration for all common i2s settings
* @author Phil Schatzmann
* @copyright GPLv3
*/
class I2SConfig : public AudioInfo {
public:

I2SConfig() {
channels = DEFAULT_CHANNELS;
sample_rate = DEFAULT_SAMPLE_RATE;
bits_per_sample = DEFAULT_BITS_PER_SAMPLE;
}

/// Default Copy Constructor
I2SConfig(const I2SConfig &cfg) = default;

/// Constructor
I2SConfig(RxTxMode mode) {
channels = DEFAULT_CHANNELS;
sample_rate = DEFAULT_SAMPLE_RATE;
bits_per_sample = DEFAULT_BITS_PER_SAMPLE;
rx_tx_mode = mode;
switch(mode){
case RX_MODE:
pin_data = PIN_I2S_DATA_IN;
#ifdef ESP32
auto_clear = false;
#endif
break;
case TX_MODE:
pin_data = PIN_I2S_DATA_OUT;
#ifdef ESP32
auto_clear = I2S_AUTO_CLEAR;
#endif
break;
default:
#ifdef ESP32
auto_clear = I2S_AUTO_CLEAR;
#endif
pin_data = PIN_I2S_DATA_OUT;
pin_data_rx = PIN_I2S_DATA_IN;
break;
}
}

/// public settings
RxTxMode rx_tx_mode = TX_MODE;
bool is_master = true;
int port_no = 0; // processor dependent port
int pin_ws = PIN_I2S_WS;
int pin_bck = PIN_I2S_BCK;
int pin_data; // rx or tx pin dependent on mode: tx pin for RXTX_MODE
int pin_data_rx; // rx pin for RXTX_MODE
int pin_mck = PIN_I2S_MCK;
I2SFormat i2s_format = I2S_STD_FORMAT;

#if defined(I2S_BUFFER_SIZE_ONLY)
int buffer_count = I2S_BUFFER_COUNT;
int buffer_size = I2S_BUFFER_SIZE;
#elif defined(ESP32)
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0 , 0)
int channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT;
#endif
int buffer_count = I2S_BUFFER_COUNT;
int buffer_size = I2S_BUFFER_SIZE;
I2SSignalType signal_type = Digital; // e.g. the ESP32 supports analog input or output or PDM picrophones
bool auto_clear = I2S_AUTO_CLEAR;
bool use_apll = I2S_USE_APLL;
uint32_t fixed_mclk = 0;
#endif

#if defined(USE_ALT_PIN_SUPPORT)
bool is_arduino_pin_numbers = true;
#endif


void logInfo(const char* source=nullptr) {
AudioInfo::logInfo(source);
LOGI("rx/tx mode: %s", RxTxModeNames[rx_tx_mode]);
LOGI("port_no: %d", port_no);
LOGI("is_master: %s", is_master ? "Master":"Slave");
LOGI("sample rate: %d", sample_rate);
LOGI("bits per sample: %d", bits_per_sample);
LOGI("number of channels: %d", channels);
#ifdef ESP32
LOGI("signal_type: %s", i2s_signal_types[signal_type]);
if (signal_type==Digital){
LOGI("i2s_format: %s", i2s_formats[i2s_format]);
}
LOGI("auto_clear: %s",auto_clear? "true" : "false");
if (use_apll) {
LOGI("use_apll: %s", use_apll ? "true" : "false");
}
if (fixed_mclk){
LOGI("fixed_mclk: %d", (int) fixed_mclk);
}
LOGI("buffer_count:%d",buffer_count);
LOGI("buffer_size:%d",buffer_size);

#endif
#if defined(I2S_BUFFER_SIZE_ONLY)
LOGI("buffer_count:%d",buffer_count);
LOGI("buffer_size:%d",buffer_size);
#if defined(ESP32)
# if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0 , 0)
# include "I2SConfigESP32.h"
# else
# include "I2SConfigESP32V1.h"
# endif
#else
# include "I2SConfigStd.h"
#endif
if (pin_mck!=-1)
LOGI("pin_mck: %d", pin_mck);
if (pin_bck!=-1)
LOGI("pin_bck: %d", pin_bck);
if (pin_ws!=-1)
LOGI("pin_ws: %d", pin_ws);
if (pin_data!=-1)
LOGI("pin_data: %d", pin_data);
if (pin_data_rx!=-1 && rx_tx_mode==RXTX_MODE){
LOGI("pin_data_rx: %d", pin_data_rx);
}
}

};

}

#endif
126 changes: 126 additions & 0 deletions src/AudioI2S/I2SConfigESP32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#pragma once
#include "AudioConfig.h"
#include "AudioTools/AudioTypes.h"

#ifndef PIN_I2S_MCK
# define PIN_I2S_MCK -1
#endif

#if defined(ESP32) && ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0 , 0)
# include "driver/i2s.h" // for I2S_CHANNEL_FMT_RIGHT_LEFT
#endif

namespace audio_tools {

/***
* @brief I2S Signal Types: Digital, Analog, PDM
*/
enum I2SSignalType {
Digital,
Analog,
PDM,
};

INLINE_VAR const char* i2s_signal_types[] = {"Digital","Analog","PDM","TDM"};

/**
* @brief configuration for USE_ALT_PIN_SUPPORT i2s
* @author Phil Schatzmann
* @copyright GPLv3
*/
class I2SConfigESP32 : public AudioInfo {
public:

I2SConfigESP32() {
channels = DEFAULT_CHANNELS;
sample_rate = DEFAULT_SAMPLE_RATE;
bits_per_sample = DEFAULT_BITS_PER_SAMPLE;
}

/// Default Copy Constructor
I2SConfigESP32(const I2SConfigESP32 &cfg) = default;

/// Constructor
I2SConfigESP32(RxTxMode mode) {
channels = DEFAULT_CHANNELS;
sample_rate = DEFAULT_SAMPLE_RATE;
bits_per_sample = DEFAULT_BITS_PER_SAMPLE;
rx_tx_mode = mode;
switch(mode){
case RX_MODE:
pin_data = PIN_I2S_DATA_IN;
auto_clear = false;
break;
case TX_MODE:
pin_data = PIN_I2S_DATA_OUT;
auto_clear = I2S_AUTO_CLEAR;
break;
default:
auto_clear = I2S_AUTO_CLEAR;
pin_data = PIN_I2S_DATA_OUT;
pin_data_rx = PIN_I2S_DATA_IN;
break;
}
}

/// public settings
RxTxMode rx_tx_mode = TX_MODE;
I2SFormat i2s_format = I2S_STD_FORMAT;
I2SSignalType signal_type = Digital; // e.g. the ESP32 supports analog input or output or PDM picrophones
bool is_master = true;
int port_no = 0; // processor dependent port
int pin_ws = PIN_I2S_WS;
int pin_bck = PIN_I2S_BCK;
int pin_data; // rx or tx pin dependent on mode: tx pin for RXTX_MODE
int pin_data_rx; // rx pin for RXTX_MODE
int pin_mck = PIN_I2S_MCK;
int buffer_count = I2S_BUFFER_COUNT;
int buffer_size = I2S_BUFFER_SIZE;
bool auto_clear = I2S_AUTO_CLEAR;
bool use_apll = I2S_USE_APLL;
uint32_t fixed_mclk = 0;
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0 , 0)
int channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT;
#endif

void logInfo(const char* source=nullptr) {
AudioInfo::logInfo(source);
LOGI("rx/tx mode: %s", RxTxModeNames[rx_tx_mode]);
LOGI("port_no: %d", port_no);
LOGI("is_master: %s", is_master ? "Master":"Slave");
LOGI("sample rate: %d", sample_rate);
LOGI("bits per sample: %d", bits_per_sample);
LOGI("number of channels: %d", channels);
LOGI("signal_type: %s", i2s_signal_types[signal_type]);
if (signal_type==Digital){
LOGI("i2s_format: %s", i2s_formats[i2s_format]);
}
//LOGI("auto_clear: %s",auto_clear? "true" : "false");
if (use_apll) {
LOGI("use_apll: %s", use_apll ? "true" : "false");
}
//if (fixed_mclk){
// LOGI("fixed_mclk: %d", (int) fixed_mclk);
//}
LOGI("buffer_count:%d",buffer_count);
LOGI("buffer_size:%d",buffer_size);

if (pin_mck!=-1)
LOGI("pin_mck: %d", pin_mck);
if (pin_bck!=-1)
LOGI("pin_bck: %d", pin_bck);
if (pin_ws!=-1)
LOGI("pin_ws: %d", pin_ws);
if (pin_data!=-1)
LOGI("pin_data: %d", pin_data);
if (pin_data_rx!=-1 && rx_tx_mode==RXTX_MODE){
LOGI("pin_data_rx: %d", pin_data_rx);
}
}

};

using I2SConfig = I2SConfigESP32;

}

Loading

0 comments on commit 4fd7a65

Please sign in to comment.