From 4ba7df1928dafc7cac72290931b213bcabe7d2d9 Mon Sep 17 00:00:00 2001 From: Sylwester <6614616+DatanoiseTV@users.noreply.github.com> Date: Wed, 4 Jan 2023 18:46:05 +0100 Subject: [PATCH] Add support for I2S MCLK. (#594) --- src/AudioOutputI2S.cpp | 38 +++++++++++++++++++++++++++++++++++--- src/AudioOutputI2S.h | 4 ++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/AudioOutputI2S.cpp b/src/AudioOutputI2S.cpp index 919679d..c2b8aec 100644 --- a/src/AudioOutputI2S.cpp +++ b/src/AudioOutputI2S.cpp @@ -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); } @@ -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 @@ -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, @@ -90,6 +93,8 @@ bool AudioOutputI2S::SetPinout() (void)bclkPin; (void)wclkPin; (void)doutPin; + (void)mclkPin; + (void)use_mclk; return false; #endif } @@ -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 @@ -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 @@ -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 }; diff --git a/src/AudioOutputI2S.h b/src/AudioOutputI2S.h index 33e784b..da61b74 100644 --- a/src/AudioOutputI2S.h +++ b/src/AudioOutputI2S.h @@ -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; @@ -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(); @@ -61,6 +63,7 @@ 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; @@ -68,6 +71,7 @@ class AudioOutputI2S : public AudioOutput uint8_t bclkPin; uint8_t wclkPin; uint8_t doutPin; + uint8_t mclkPin; #if defined(ARDUINO_ARCH_RP2040) I2S i2s;