Skip to content

Commit

Permalink
I2S Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Oct 23, 2023
1 parent 4fd7a65 commit 68f311e
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 76 deletions.
5 changes: 0 additions & 5 deletions src/AudioI2S/I2SConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
#include "AudioConfig.h"
#ifdef USE_I2S


namespace audio_tools {

}

#if defined(ESP32)
# if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0 , 0)
# include "I2SConfigESP32.h"
Expand Down
2 changes: 2 additions & 0 deletions src/AudioI2S/I2SConfigESP32.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace audio_tools {

/***
* @brief I2S Signal Types: Digital, Analog, PDM
* @ingroup platform
*/
enum I2SSignalType {
Digital,
Expand All @@ -25,6 +26,7 @@ INLINE_VAR const char* i2s_signal_types[] = {"Digital","Analog","PDM","TDM"};

/**
* @brief configuration for USE_ALT_PIN_SUPPORT i2s
* @ingroup platform
* @author Phil Schatzmann
* @copyright GPLv3
*/
Expand Down
2 changes: 2 additions & 0 deletions src/AudioI2S/I2SConfigESP32V1.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace audio_tools {

/***
* @brief I2S Signal Types: Digital, Analog, PDM
* @ingroup platform
*/
enum I2SSignalType {
Digital,
Expand All @@ -22,6 +23,7 @@ INLINE_VAR const char* i2s_signal_types[] = {"Digital","Analog","PDM","TDM"};

/**
* @brief configuration for ESP32 i2s for IDF > 5.0
* @ingroup platform
* @author Phil Schatzmann
* @copyright GPLv3
*/
Expand Down
1 change: 1 addition & 0 deletions src/AudioI2S/I2SConfigStd.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace audio_tools {

/**
* @brief configuration for all common i2s settings
* @ingroup platform
* @author Phil Schatzmann
* @copyright GPLv3
*/
Expand Down
20 changes: 10 additions & 10 deletions src/AudioI2S/I2SESP32.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "esp_system.h"

#ifndef I2S_MCLK_MULTIPLE_DEFAULT
#define I2S_MCLK_MULTIPLE_DEFAULT ((i2s_mclk_multiple_t)0)
# define I2S_MCLK_MULTIPLE_DEFAULT ((i2s_mclk_multiple_t)0)
#endif

namespace audio_tools {
Expand All @@ -27,8 +27,8 @@ class I2SDriverESP32 {
public:

/// Provides the default configuration
I2SConfig defaultConfig(RxTxMode mode) {
I2SConfig c(mode);
I2SConfigESP32 defaultConfig(RxTxMode mode) {
I2SConfigESP32 c(mode);
return c;
}

Expand All @@ -43,7 +43,7 @@ class I2SDriverESP32 {
}

/// starts the DAC
bool begin(I2SConfig cfg) {
bool begin(I2SConfigESP32 cfg) {
TRACED();
this->cfg = cfg;
switch(cfg.rx_tx_mode){
Expand Down Expand Up @@ -76,7 +76,7 @@ class I2SDriverESP32 {
}

/// provides the actual configuration
I2SConfig config() {
I2SConfigESP32 config() {
return cfg;
}

Expand Down Expand Up @@ -133,12 +133,12 @@ class I2SDriverESP32 {
}

protected:
I2SConfig cfg = defaultConfig(RX_MODE);
I2SConfigESP32 cfg = defaultConfig(RX_MODE);
i2s_port_t i2s_num;
i2s_config_t i2s_config;
bool is_started = false;

bool isNoChannelConversion(I2SConfig cfg) {
bool isNoChannelConversion(I2SConfigESP32 cfg) {
if (cfg.channels==2) return true;
if (cfg.channels==1 && cfg.channel_format == I2S_CHANNEL_FMT_ALL_RIGHT) return true;
if (cfg.channels==1 && cfg.channel_format == I2S_CHANNEL_FMT_ALL_LEFT) return true;
Expand All @@ -148,7 +148,7 @@ class I2SDriverESP32 {
}

/// starts the DAC
bool begin(I2SConfig cfg, int txPin, int rxPin) {
bool begin(I2SConfigESP32 cfg, int txPin, int rxPin) {
TRACED();
cfg.logInfo();
this->cfg = cfg;
Expand Down Expand Up @@ -318,7 +318,7 @@ class I2SDriverESP32 {
}
#pragma GCC diagnostic pop

int getModeDigital(I2SConfig &cfg) {
int getModeDigital(I2SConfigESP32 &cfg) {
int i2s_format = cfg.is_master ? I2S_MODE_MASTER : I2S_MODE_SLAVE;
int i2s_rx_tx = 0;
switch(cfg.rx_tx_mode){
Expand All @@ -338,7 +338,7 @@ class I2SDriverESP32 {
}

// determines the i2s_format_t
i2s_mode_t toMode(I2SConfig &cfg) {
i2s_mode_t toMode(I2SConfigESP32 &cfg) {
i2s_mode_t mode = (i2s_mode_t) (I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX );
switch (cfg.signal_type){
case Digital:
Expand Down
46 changes: 23 additions & 23 deletions src/AudioI2S/I2SESP32V1.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class I2SDriverESP32V1 {

public:
/// Provides the default configuration
I2SConfig defaultConfig(RxTxMode mode) {
I2SConfig c(mode);
I2SConfigESP32V1 defaultConfig(RxTxMode mode) {
I2SConfigESP32V1 c(mode);
return c;
}

Expand All @@ -35,7 +35,7 @@ class I2SDriverESP32V1 {
bool begin() { return (!is_started) ? begin(cfg) : true; }

/// starts the DAC
bool begin(I2SConfig cfg) {
bool begin(I2SConfigESP32V1 cfg) {
TRACED();
this->cfg = cfg;
switch (cfg.rx_tx_mode) {
Expand Down Expand Up @@ -75,7 +75,7 @@ class I2SDriverESP32V1 {
}

/// provides the actual configuration
I2SConfig config() { return cfg; }
I2SConfigESP32V1 config() { return cfg; }

/// writes the data to the I2S interface
size_t writeBytes(const void *src, size_t size_bytes) {
Expand All @@ -98,20 +98,20 @@ class I2SDriverESP32V1 {
}

protected:
I2SConfig cfg = defaultConfig(RX_MODE);
I2SConfigESP32V1 cfg = defaultConfig(RX_MODE);
i2s_std_config_t i2s_config;
i2s_chan_handle_t tx_chan; // I2S tx channel handler
i2s_chan_handle_t rx_chan; // I2S rx channel handler
bool is_started = false;

struct DriverCommon {
virtual i2s_chan_config_t getChannelConfig(I2SConfig &cfg) = 0;
virtual bool startChannels(I2SConfig &cfg, i2s_chan_handle_t &tx_chan,
virtual i2s_chan_config_t getChannelConfig(I2SConfigESP32V1 &cfg) = 0;
virtual bool startChannels(I2SConfigESP32V1 &cfg, i2s_chan_handle_t &tx_chan,
i2s_chan_handle_t &rx_chan, int txPin, int rxPin) = 0;
};

struct DriverI2S : public DriverCommon {
i2s_std_slot_config_t getSlotConfig(I2SConfig &cfg) {
i2s_std_slot_config_t getSlotConfig(I2SConfigESP32V1 &cfg) {
TRACED();
switch (cfg.i2s_format) {
case I2S_RIGHT_JUSTIFIED_FORMAT:
Expand All @@ -138,19 +138,19 @@ class I2SDriverESP32V1 {
(i2s_slot_mode_t)cfg.channels);
}

i2s_chan_config_t getChannelConfig(I2SConfig &cfg) {
i2s_chan_config_t getChannelConfig(I2SConfigESP32V1 &cfg) {
TRACED();
return I2S_CHANNEL_DEFAULT_CONFIG((i2s_port_t)cfg.port_no,
cfg.is_master ? I2S_ROLE_MASTER
: I2S_ROLE_SLAVE);
}

i2s_std_clk_config_t getClockConfig(I2SConfig &cfg) {
i2s_std_clk_config_t getClockConfig(I2SConfigESP32V1 &cfg) {
TRACED();
return I2S_STD_CLK_DEFAULT_CONFIG((uint32_t)cfg.sample_rate);
}

bool startChannels(I2SConfig &cfg, i2s_chan_handle_t &tx_chan,
bool startChannels(I2SConfigESP32V1 &cfg, i2s_chan_handle_t &tx_chan,
i2s_chan_handle_t &rx_chan, int txPin, int rxPin) {
TRACED();
i2s_std_config_t std_cfg = {
Expand Down Expand Up @@ -202,33 +202,33 @@ class I2SDriverESP32V1 {
#ifdef USE_PDM

struct DriverPDM : public DriverCommon {
i2s_pdm_rx_slot_config_t getRxSlotConfig(I2SConfig &cfg) {
i2s_pdm_rx_slot_config_t getRxSlotConfig(I2SConfigESP32V1 &cfg) {
return I2S_PDM_RX_SLOT_DEFAULT_CONFIG(
(i2s_data_bit_width_t)cfg.bits_per_sample,
(i2s_slot_mode_t)cfg.channels);
}

i2s_pdm_tx_slot_config_t getTxSlotConfig(I2SConfig &cfg) {
i2s_pdm_tx_slot_config_t getTxSlotConfig(I2SConfigESP32V1 &cfg) {
return I2S_PDM_TX_SLOT_DEFAULT_CONFIG(
(i2s_data_bit_width_t)cfg.bits_per_sample,
(i2s_slot_mode_t)cfg.channels);
}

i2s_chan_config_t getChannelConfig(I2SConfig &cfg) {
i2s_chan_config_t getChannelConfig(I2SConfigESP32V1 &cfg) {
return I2S_CHANNEL_DEFAULT_CONFIG((i2s_port_t)cfg.port_no,
cfg.is_master ? I2S_ROLE_MASTER
: I2S_ROLE_SLAVE);
}

i2s_pdm_tx_clk_config_t getTxClockConfig(I2SConfig &cfg) {
i2s_pdm_tx_clk_config_t getTxClockConfig(I2SConfigESP32V1 &cfg) {
return I2S_PDM_TX_CLK_DEFAULT_CONFIG((uint32_t)cfg.sample_rate);
}

i2s_pdm_rx_clk_config_t getRxClockConfig(I2SConfig &cfg) {
i2s_pdm_rx_clk_config_t getRxClockConfig(I2SConfigESP32V1 &cfg) {
return I2S_PDM_RX_CLK_DEFAULT_CONFIG((uint32_t)cfg.sample_rate);
}

bool startChannels(I2SConfig &cfg, i2s_chan_handle_t &tx_chan,
bool startChannels(I2SConfigESP32V1 &cfg, i2s_chan_handle_t &tx_chan,
i2s_chan_handle_t &rx_chan, int txPin, int rxPin) {

if (cfg.rx_tx_mode == TX_MODE) {
Expand Down Expand Up @@ -284,23 +284,23 @@ class I2SDriverESP32V1 {

#endif
// struct DriverTDM : public DriverCommon {
// i2s_tdm_slot_config_t getSlotConfig(I2SConfig &cfg) {
// i2s_tdm_slot_config_t getSlotConfig(I2SConfigESP32V1 &cfg) {
// return I2S_TDM_SLOT_DEFAULT_CONFIG(
// (i2s_data_bit_width_t)cfg.bits_per_sample,
// (i2s_slot_mode_t)cfg.channels);
// }

// i2s_chan_config_t getChannelConfig(I2SConfig &cfg) {
// i2s_chan_config_t getChannelConfig(I2SConfigESP32V1 &cfg) {
// return I2S_CHANNEL_DEFAULT_CONFIG((i2s_port_t)cfg.port_no,
// cfg.is_master ? I2S_ROLE_MASTER
// : I2S_ROLE_SLAVE);
// }

// i2s_tdm_clk_config_t getClockConfig(I2SConfig &cfg) {
// i2s_tdm_clk_config_t getClockConfig(I2SConfigESP32V1 &cfg) {
// return I2S_TDM_CLK_DEFAULT_CONFIG((uint32_t)cfg.sample_rate);
// }

// bool startChannels(I2SConfig &cfg, i2s_chan_handle_t &tx_chan,
// bool startChannels(I2SConfigESP32V1 &cfg, i2s_chan_handle_t &tx_chan,
// i2s_chan_handle_t &rx_chan, int txPin, int rxPin) {

// i2s_tdm_config_t tdm_cfg = {
Expand Down Expand Up @@ -336,7 +336,7 @@ class I2SDriverESP32V1 {
// } tdm;

/// starts I2S
bool begin(I2SConfig cfg, int txPin, int rxPin) {
bool begin(I2SConfigESP32V1 cfg, int txPin, int rxPin) {
TRACED();
cfg.logInfo();
this->cfg = cfg;
Expand All @@ -360,7 +360,7 @@ class I2SDriverESP32V1 {
return is_started;
}

DriverCommon &getDriver(I2SConfig &cfg) {
DriverCommon &getDriver(I2SConfigESP32V1 &cfg) {
switch (cfg.signal_type) {
case Digital:
return i2s;
Expand Down
10 changes: 5 additions & 5 deletions src/AudioI2S/I2SESP8266.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class I2SDriverESP8266 {
public:

/// Provides the default configuration
I2SConfig defaultConfig(RxTxMode mode) {
I2SConfig c(mode);
I2SConfigStd defaultConfig(RxTxMode mode) {
I2SConfigStd c(mode);
return c;
}

Expand All @@ -31,7 +31,7 @@ class I2SDriverESP8266 {
}

/// starts the DAC
bool begin(I2SConfig cfg) {
bool begin(I2SConfigStd cfg) {
this->cfg = cfg;
i2s_set_rate(cfg.sample_rate);
cfg.bits_per_sample = 16;
Expand All @@ -58,12 +58,12 @@ class I2SDriverESP8266 {
}

/// provides the actual configuration
I2SConfig config() {
I2SConfigStd config() {
return cfg;
}

protected:
I2SConfig cfg;
I2SConfigStd cfg;

/// writes the data to the I2S interface
size_t writeBytes(const void *src, size_t size_bytes){
Expand Down
Loading

0 comments on commit 68f311e

Please sign in to comment.