Skip to content

Commit

Permalink
Add support for I2S MCLK. (#594)
Browse files Browse the repository at this point in the history
  • Loading branch information
DatanoiseTV authored Jan 4, 2023
1 parent 0b5da0e commit 4ba7df1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/AudioOutputI2S.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ AudioOutputI2S::AudioOutputI2S(int port, int output_mode, int dma_buf_count, int
bclkPin = 26;
wclkPin = 25;
doutPin = 22;
mclkPin = 0;
SetGain(1.0);
}

Expand All @@ -61,6 +62,8 @@ AudioOutputI2S::AudioOutputI2S(long sampleRate, pin_size_t sck, pin_size_t data)
hertz = sampleRate;
bclkPin = sck;
doutPin = data;
mclkPin = 0;
use_mclk = false;
SetGain(1.0);
}
#endif
Expand All @@ -78,7 +81,7 @@ bool AudioOutputI2S::SetPinout()

i2s_pin_config_t pins = {
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0)
.mck_io_num = 0, // Unused
.mck_io_num = mclkPin,
#endif
.bck_io_num = bclkPin,
.ws_io_num = wclkPin,
Expand All @@ -90,6 +93,8 @@ bool AudioOutputI2S::SetPinout()
(void)bclkPin;
(void)wclkPin;
(void)doutPin;
(void)mclkPin;
(void)use_mclk;
return false;
#endif
}
Expand All @@ -104,6 +109,22 @@ bool AudioOutputI2S::SetPinout(int bclk, int wclk, int dout)

return true;
}

bool AudioOutputI2S::SetPinout(int bclk, int wclk, int dout, int mclk)
{
bclkPin = bclk;
wclkPin = wclk;
doutPin = dout;
#ifdef ESP32
mclkPin = mclk;
if (i2sOn)
return SetPinout();
#else
(void)mclk;
#endif
return true;
}

bool AudioOutputI2S::SetRate(int hz)
{
// TODO - have a list of allowable rates from constructor, check them
Expand Down Expand Up @@ -147,6 +168,17 @@ bool AudioOutputI2S::SetLsbJustified(bool lsbJustified)
return true;
}

bool AudioOutputI2S::SetMclk(bool enabled){
(void)enabled;
#ifdef ESP32
if (output_mode == INTERNAL_DAC || output_mode == INTERNAL_PDM)
return false; // Not allowed

use_mclk = enabled;
#endif
return true;
}

bool AudioOutputI2S::begin(bool txDAC)
{
#ifdef ESP32
Expand Down Expand Up @@ -219,9 +251,9 @@ bool AudioOutputI2S::begin(bool txDAC)
.dma_buf_len = 128,
.use_apll = use_apll, // Use audio PLL
.tx_desc_auto_clear = true, // Silence on underflow
.fixed_mclk = 0, // Unused
.fixed_mclk = use_mclk, // Unused
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0)
.mclk_multiple = I2S_MCLK_MULTIPLE_DEFAULT, // Unused
.mclk_multiple = I2S_MCLK_MULTIPLE_256, // Unused
.bits_per_chan = I2S_BITS_PER_CHAN_DEFAULT // Use bits per sample
#endif
};
Expand Down
4 changes: 4 additions & 0 deletions src/AudioOutputI2S.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class AudioOutputI2S : public AudioOutput
AudioOutputI2S(long sampleRate = 44100, pin_size_t sck = 26, pin_size_t data = 28);
#endif
bool SetPinout(int bclkPin, int wclkPin, int doutPin);
bool SetPinout(int bclkPin, int wclkPin, int doutPin, int mclkPin);
virtual ~AudioOutputI2S() override;
virtual bool SetRate(int hz) override;
virtual bool SetBitsPerSample(int bits) override;
Expand All @@ -50,6 +51,7 @@ class AudioOutputI2S : public AudioOutput
bool begin(bool txDAC);
bool SetOutputModeMono(bool mono); // Force mono output no matter the input
bool SetLsbJustified(bool lsbJustified); // Allow supporting non-I2S chips, e.g. PT8211
bool SetMclk(bool enabled); // Enable MCLK output (if supported)

protected:
bool SetPinout();
Expand All @@ -61,13 +63,15 @@ class AudioOutputI2S : public AudioOutput
bool i2sOn;
int dma_buf_count;
int use_apll;
bool use_mclk;
// We can restore the old values and free up these pins when in NoDAC mode
uint32_t orig_bck;
uint32_t orig_ws;

uint8_t bclkPin;
uint8_t wclkPin;
uint8_t doutPin;
uint8_t mclkPin;

#if defined(ARDUINO_ARCH_RP2040)
I2S i2s;
Expand Down

0 comments on commit 4ba7df1

Please sign in to comment.